xref: /openbmc/libbej/test/bej_tree_test.cpp (revision dc3f214b)
12ebe82f5Skasunath #include "bej_tree.h"
22ebe82f5Skasunath 
32ebe82f5Skasunath #include <gmock/gmock-matchers.h>
42ebe82f5Skasunath #include <gmock/gmock.h>
52ebe82f5Skasunath #include <gtest/gtest.h>
62ebe82f5Skasunath 
72ebe82f5Skasunath namespace libbej
82ebe82f5Skasunath {
92ebe82f5Skasunath 
TEST(BejTreeTest,InitSet)102ebe82f5Skasunath TEST(BejTreeTest, InitSet)
112ebe82f5Skasunath {
122ebe82f5Skasunath     const char* name = "SomeProperty";
132ebe82f5Skasunath     struct RedfishPropertyParent node;
142ebe82f5Skasunath     bejTreeInitSet(&node, name);
152ebe82f5Skasunath 
162ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.name, name);
172ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.format.principalDataType, bejSet);
182ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.format.deferredBinding, 0);
192ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.format.readOnlyProperty, 0);
202ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.format.nullableProperty, 0);
212ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.sibling, nullptr);
222ebe82f5Skasunath     EXPECT_THAT(node.nChildren, 0);
232ebe82f5Skasunath     EXPECT_THAT(node.firstChild, nullptr);
242ebe82f5Skasunath     EXPECT_THAT(node.lastChild, nullptr);
252ebe82f5Skasunath }
262ebe82f5Skasunath 
TEST(BejTreeTest,InitArray)272ebe82f5Skasunath TEST(BejTreeTest, InitArray)
282ebe82f5Skasunath {
292ebe82f5Skasunath     const char* name = "SomeProperty";
302ebe82f5Skasunath     struct RedfishPropertyParent node;
312ebe82f5Skasunath     bejTreeInitArray(&node, name);
322ebe82f5Skasunath 
332ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.name, name);
342ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.format.principalDataType, bejArray);
352ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.format.deferredBinding, 0);
362ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.format.readOnlyProperty, 0);
372ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.format.nullableProperty, 0);
382ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.sibling, nullptr);
392ebe82f5Skasunath     EXPECT_THAT(node.nChildren, 0);
402ebe82f5Skasunath     EXPECT_THAT(node.firstChild, nullptr);
412ebe82f5Skasunath     EXPECT_THAT(node.lastChild, nullptr);
422ebe82f5Skasunath }
432ebe82f5Skasunath 
TEST(BejTreeTest,InitAnnotatedProp)442ebe82f5Skasunath TEST(BejTreeTest, InitAnnotatedProp)
452ebe82f5Skasunath {
462ebe82f5Skasunath     const char* name = "SomeProperty";
472ebe82f5Skasunath     struct RedfishPropertyParent node;
482ebe82f5Skasunath     bejTreeInitPropertyAnnotated(&node, name);
492ebe82f5Skasunath 
502ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.name, name);
512ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.format.principalDataType, bejPropertyAnnotation);
522ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.format.deferredBinding, 0);
532ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.format.readOnlyProperty, 0);
542ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.format.nullableProperty, 0);
552ebe82f5Skasunath     EXPECT_THAT(node.nodeAttr.sibling, nullptr);
562ebe82f5Skasunath     EXPECT_THAT(node.nChildren, 0);
572ebe82f5Skasunath     EXPECT_THAT(node.firstChild, nullptr);
582ebe82f5Skasunath     EXPECT_THAT(node.lastChild, nullptr);
592ebe82f5Skasunath }
602ebe82f5Skasunath 
TEST(BejTreeTest,ChildLinking)612ebe82f5Skasunath TEST(BejTreeTest, ChildLinking)
622ebe82f5Skasunath {
632ebe82f5Skasunath     struct RedfishPropertyParent parent;
642ebe82f5Skasunath     struct RedfishPropertyLeafInt child1;
652ebe82f5Skasunath     struct RedfishPropertyLeafInt child2;
662ebe82f5Skasunath 
672ebe82f5Skasunath     bejTreeInitSet(&parent, nullptr);
682ebe82f5Skasunath     EXPECT_THAT(parent.nChildren, 0);
692ebe82f5Skasunath     EXPECT_THAT(parent.firstChild, nullptr);
702ebe82f5Skasunath     EXPECT_THAT(parent.lastChild, nullptr);
712ebe82f5Skasunath 
722ebe82f5Skasunath     bejTreeAddInteger(&parent, &child1, nullptr, 1024);
732ebe82f5Skasunath     EXPECT_THAT(parent.nChildren, 1);
742ebe82f5Skasunath     EXPECT_THAT(parent.firstChild, &child1);
752ebe82f5Skasunath     EXPECT_THAT(parent.lastChild, &child1);
762ebe82f5Skasunath 
772ebe82f5Skasunath     bejTreeAddInteger(&parent, &child2, nullptr, 20);
782ebe82f5Skasunath     EXPECT_THAT(parent.nChildren, 2);
792ebe82f5Skasunath     EXPECT_THAT(parent.firstChild, &child1);
802ebe82f5Skasunath     EXPECT_THAT(parent.lastChild, &child2);
812ebe82f5Skasunath 
822ebe82f5Skasunath     // child2 should be a sibling of child1.
832ebe82f5Skasunath     EXPECT_THAT(child1.leaf.nodeAttr.sibling, &child2);
842ebe82f5Skasunath }
852ebe82f5Skasunath 
TEST(BejTreeTest,AddInteger)862ebe82f5Skasunath TEST(BejTreeTest, AddInteger)
872ebe82f5Skasunath {
882ebe82f5Skasunath     const char* name = "SomeProperty";
892ebe82f5Skasunath     struct RedfishPropertyParent parent;
902ebe82f5Skasunath     struct RedfishPropertyLeafInt child;
912ebe82f5Skasunath 
922ebe82f5Skasunath     bejTreeInitSet(&parent, nullptr);
932ebe82f5Skasunath     bejTreeAddInteger(&parent, &child, name, 1024);
942ebe82f5Skasunath 
952ebe82f5Skasunath     EXPECT_THAT(child.leaf.nodeAttr.name, name);
962ebe82f5Skasunath     EXPECT_THAT(child.leaf.nodeAttr.format.principalDataType, bejInteger);
972ebe82f5Skasunath     EXPECT_THAT(child.leaf.nodeAttr.format.deferredBinding, 0);
982ebe82f5Skasunath     EXPECT_THAT(child.leaf.nodeAttr.format.readOnlyProperty, 0);
992ebe82f5Skasunath     EXPECT_THAT(child.leaf.nodeAttr.format.nullableProperty, 0);
1002ebe82f5Skasunath     EXPECT_THAT(child.leaf.nodeAttr.sibling, nullptr);
1012ebe82f5Skasunath     EXPECT_THAT(child.value, 1024);
1022ebe82f5Skasunath }
1032ebe82f5Skasunath 
TEST(BejTreeTest,SetInteger)1042ebe82f5Skasunath TEST(BejTreeTest, SetInteger)
1052ebe82f5Skasunath {
1062ebe82f5Skasunath     const char* name = "SomeProperty";
1072ebe82f5Skasunath     struct RedfishPropertyParent parent;
1082ebe82f5Skasunath     struct RedfishPropertyLeafInt child;
1092ebe82f5Skasunath 
1102ebe82f5Skasunath     bejTreeInitSet(&parent, nullptr);
1112ebe82f5Skasunath     bejTreeAddInteger(&parent, &child, name, 1024);
1122ebe82f5Skasunath 
1132ebe82f5Skasunath     EXPECT_THAT(child.value, 1024);
1142ebe82f5Skasunath     bejTreeSetInteger(&child, 20);
1152ebe82f5Skasunath     EXPECT_THAT(child.value, 20);
1162ebe82f5Skasunath }
1172ebe82f5Skasunath 
TEST(BejTreeTest,AddEnum)1182ebe82f5Skasunath TEST(BejTreeTest, AddEnum)
1192ebe82f5Skasunath {
1202ebe82f5Skasunath     const char* name = "SomeProperty";
1212ebe82f5Skasunath     const char* enumValue = "EnumValue";
1222ebe82f5Skasunath     struct RedfishPropertyParent parent;
1232ebe82f5Skasunath     struct RedfishPropertyLeafEnum child;
1242ebe82f5Skasunath 
1252ebe82f5Skasunath     bejTreeInitSet(&parent, nullptr);
1262ebe82f5Skasunath     bejTreeAddEnum(&parent, &child, name, enumValue);
1272ebe82f5Skasunath 
1282ebe82f5Skasunath     EXPECT_THAT(child.leaf.nodeAttr.name, name);
1292ebe82f5Skasunath     EXPECT_THAT(child.leaf.nodeAttr.format.principalDataType, bejEnum);
1302ebe82f5Skasunath     EXPECT_THAT(child.leaf.nodeAttr.format.deferredBinding, 0);
1312ebe82f5Skasunath     EXPECT_THAT(child.leaf.nodeAttr.format.readOnlyProperty, 0);
1322ebe82f5Skasunath     EXPECT_THAT(child.leaf.nodeAttr.format.nullableProperty, 0);
1332ebe82f5Skasunath     EXPECT_THAT(child.leaf.nodeAttr.sibling, nullptr);
1342ebe82f5Skasunath     EXPECT_THAT(child.value, enumValue);
1352ebe82f5Skasunath }
1362ebe82f5Skasunath 
TEST(BejTreeTest,AddString)137*dc3f214bSkasunath TEST(BejTreeTest, AddString)
138*dc3f214bSkasunath {
139*dc3f214bSkasunath     const char* name = "SomeProperty";
140*dc3f214bSkasunath     const char* stringValue = "StringValue";
141*dc3f214bSkasunath     struct RedfishPropertyParent parent;
142*dc3f214bSkasunath     struct RedfishPropertyLeafString child;
143*dc3f214bSkasunath 
144*dc3f214bSkasunath     bejTreeInitSet(&parent, nullptr);
145*dc3f214bSkasunath     bejTreeAddString(&parent, &child, name, stringValue);
146*dc3f214bSkasunath 
147*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.name, name);
148*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.format.principalDataType, bejString);
149*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.format.deferredBinding, 0);
150*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.format.readOnlyProperty, 0);
151*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.format.nullableProperty, 0);
152*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.sibling, nullptr);
153*dc3f214bSkasunath     EXPECT_THAT(child.value, stringValue);
154*dc3f214bSkasunath }
155*dc3f214bSkasunath 
TEST(BejTreeTest,AddReal)156*dc3f214bSkasunath TEST(BejTreeTest, AddReal)
157*dc3f214bSkasunath {
158*dc3f214bSkasunath     const char* name = "SomeProperty";
159*dc3f214bSkasunath     double value = 10.50;
160*dc3f214bSkasunath     struct RedfishPropertyParent parent;
161*dc3f214bSkasunath     struct RedfishPropertyLeafReal child;
162*dc3f214bSkasunath 
163*dc3f214bSkasunath     bejTreeInitSet(&parent, nullptr);
164*dc3f214bSkasunath     bejTreeAddReal(&parent, &child, name, value);
165*dc3f214bSkasunath 
166*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.name, name);
167*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.format.principalDataType, bejReal);
168*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.format.deferredBinding, 0);
169*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.format.readOnlyProperty, 0);
170*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.format.nullableProperty, 0);
171*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.sibling, nullptr);
172*dc3f214bSkasunath     EXPECT_THAT(child.value, value);
173*dc3f214bSkasunath }
174*dc3f214bSkasunath 
TEST(BejTreeTest,AddBool)175*dc3f214bSkasunath TEST(BejTreeTest, AddBool)
176*dc3f214bSkasunath {
177*dc3f214bSkasunath     const char* name = "SomeProperty";
178*dc3f214bSkasunath     bool value = true;
179*dc3f214bSkasunath     struct RedfishPropertyParent parent;
180*dc3f214bSkasunath     struct RedfishPropertyLeafBool child;
181*dc3f214bSkasunath 
182*dc3f214bSkasunath     bejTreeInitSet(&parent, nullptr);
183*dc3f214bSkasunath     bejTreeAddBool(&parent, &child, name, value);
184*dc3f214bSkasunath 
185*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.name, name);
186*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.format.principalDataType, bejBoolean);
187*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.format.deferredBinding, 0);
188*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.format.readOnlyProperty, 0);
189*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.format.nullableProperty, 0);
190*dc3f214bSkasunath     EXPECT_THAT(child.leaf.nodeAttr.sibling, nullptr);
191*dc3f214bSkasunath     EXPECT_THAT(child.value, value);
192*dc3f214bSkasunath }
193*dc3f214bSkasunath 
TEST(BejTreeTest,NodeFlags)194*dc3f214bSkasunath TEST(BejTreeTest, NodeFlags)
195*dc3f214bSkasunath {
196*dc3f214bSkasunath     struct RedfishPropertyParent parent;
197*dc3f214bSkasunath     bejTreeInitSet(&parent, nullptr);
198*dc3f214bSkasunath     EXPECT_THAT(parent.nodeAttr.format.deferredBinding, 0);
199*dc3f214bSkasunath     EXPECT_THAT(parent.nodeAttr.format.readOnlyProperty, 0);
200*dc3f214bSkasunath     EXPECT_THAT(parent.nodeAttr.format.nullableProperty, 0);
201*dc3f214bSkasunath 
202*dc3f214bSkasunath     bejTreeUpdateNodeFlags(&parent.nodeAttr, true, true, true);
203*dc3f214bSkasunath     EXPECT_THAT(parent.nodeAttr.format.deferredBinding, 1);
204*dc3f214bSkasunath     EXPECT_THAT(parent.nodeAttr.format.readOnlyProperty, 1);
205*dc3f214bSkasunath     EXPECT_THAT(parent.nodeAttr.format.nullableProperty, 1);
206*dc3f214bSkasunath }
207*dc3f214bSkasunath 
TEST(BejTreeTest,NodeType)208*dc3f214bSkasunath TEST(BejTreeTest, NodeType)
209*dc3f214bSkasunath {
210*dc3f214bSkasunath     struct RedfishPropertyParent parent;
211*dc3f214bSkasunath     struct RedfishPropertyLeafBool child1;
212*dc3f214bSkasunath     struct RedfishPropertyLeafReal child2;
213*dc3f214bSkasunath 
214*dc3f214bSkasunath     bejTreeInitSet(&parent, nullptr);
215*dc3f214bSkasunath     bejTreeAddBool(&parent, &child1, nullptr, true);
216*dc3f214bSkasunath     bejTreeAddReal(&parent, &child2, nullptr, 10.5);
217*dc3f214bSkasunath 
218*dc3f214bSkasunath     EXPECT_THAT(bejTreeIsParentType(&parent.nodeAttr), true);
219*dc3f214bSkasunath     EXPECT_THAT(bejTreeIsParentType(&child1.leaf.nodeAttr), false);
220*dc3f214bSkasunath     EXPECT_THAT(bejTreeIsParentType(&child2.leaf.nodeAttr), false);
221*dc3f214bSkasunath }
222*dc3f214bSkasunath 
2232ebe82f5Skasunath } // namespace libbej
224