kasunath | 2ebe82f | 2023-01-31 13:59:22 -0800 | [diff] [blame] | 1 | #include "bej_tree.h" |
| 2 | |
| 3 | #include <gmock/gmock-matchers.h> |
| 4 | #include <gmock/gmock.h> |
| 5 | #include <gtest/gtest.h> |
| 6 | |
| 7 | namespace libbej |
| 8 | { |
| 9 | |
| 10 | TEST(BejTreeTest, InitSet) |
| 11 | { |
| 12 | const char* name = "SomeProperty"; |
| 13 | struct RedfishPropertyParent node; |
| 14 | bejTreeInitSet(&node, name); |
| 15 | |
| 16 | EXPECT_THAT(node.nodeAttr.name, name); |
| 17 | EXPECT_THAT(node.nodeAttr.format.principalDataType, bejSet); |
| 18 | EXPECT_THAT(node.nodeAttr.format.deferredBinding, 0); |
| 19 | EXPECT_THAT(node.nodeAttr.format.readOnlyProperty, 0); |
| 20 | EXPECT_THAT(node.nodeAttr.format.nullableProperty, 0); |
| 21 | EXPECT_THAT(node.nodeAttr.sibling, nullptr); |
| 22 | EXPECT_THAT(node.nChildren, 0); |
| 23 | EXPECT_THAT(node.firstChild, nullptr); |
| 24 | EXPECT_THAT(node.lastChild, nullptr); |
| 25 | } |
| 26 | |
| 27 | TEST(BejTreeTest, InitArray) |
| 28 | { |
| 29 | const char* name = "SomeProperty"; |
| 30 | struct RedfishPropertyParent node; |
| 31 | bejTreeInitArray(&node, name); |
| 32 | |
| 33 | EXPECT_THAT(node.nodeAttr.name, name); |
| 34 | EXPECT_THAT(node.nodeAttr.format.principalDataType, bejArray); |
| 35 | EXPECT_THAT(node.nodeAttr.format.deferredBinding, 0); |
| 36 | EXPECT_THAT(node.nodeAttr.format.readOnlyProperty, 0); |
| 37 | EXPECT_THAT(node.nodeAttr.format.nullableProperty, 0); |
| 38 | EXPECT_THAT(node.nodeAttr.sibling, nullptr); |
| 39 | EXPECT_THAT(node.nChildren, 0); |
| 40 | EXPECT_THAT(node.firstChild, nullptr); |
| 41 | EXPECT_THAT(node.lastChild, nullptr); |
| 42 | } |
| 43 | |
| 44 | TEST(BejTreeTest, InitAnnotatedProp) |
| 45 | { |
| 46 | const char* name = "SomeProperty"; |
| 47 | struct RedfishPropertyParent node; |
| 48 | bejTreeInitPropertyAnnotated(&node, name); |
| 49 | |
| 50 | EXPECT_THAT(node.nodeAttr.name, name); |
| 51 | EXPECT_THAT(node.nodeAttr.format.principalDataType, bejPropertyAnnotation); |
| 52 | EXPECT_THAT(node.nodeAttr.format.deferredBinding, 0); |
| 53 | EXPECT_THAT(node.nodeAttr.format.readOnlyProperty, 0); |
| 54 | EXPECT_THAT(node.nodeAttr.format.nullableProperty, 0); |
| 55 | EXPECT_THAT(node.nodeAttr.sibling, nullptr); |
| 56 | EXPECT_THAT(node.nChildren, 0); |
| 57 | EXPECT_THAT(node.firstChild, nullptr); |
| 58 | EXPECT_THAT(node.lastChild, nullptr); |
| 59 | } |
| 60 | |
| 61 | TEST(BejTreeTest, ChildLinking) |
| 62 | { |
| 63 | struct RedfishPropertyParent parent; |
| 64 | struct RedfishPropertyLeafInt child1; |
| 65 | struct RedfishPropertyLeafInt child2; |
| 66 | |
| 67 | bejTreeInitSet(&parent, nullptr); |
| 68 | EXPECT_THAT(parent.nChildren, 0); |
| 69 | EXPECT_THAT(parent.firstChild, nullptr); |
| 70 | EXPECT_THAT(parent.lastChild, nullptr); |
| 71 | |
| 72 | bejTreeAddInteger(&parent, &child1, nullptr, 1024); |
| 73 | EXPECT_THAT(parent.nChildren, 1); |
| 74 | EXPECT_THAT(parent.firstChild, &child1); |
| 75 | EXPECT_THAT(parent.lastChild, &child1); |
| 76 | |
| 77 | bejTreeAddInteger(&parent, &child2, nullptr, 20); |
| 78 | EXPECT_THAT(parent.nChildren, 2); |
| 79 | EXPECT_THAT(parent.firstChild, &child1); |
| 80 | EXPECT_THAT(parent.lastChild, &child2); |
| 81 | |
| 82 | // child2 should be a sibling of child1. |
| 83 | EXPECT_THAT(child1.leaf.nodeAttr.sibling, &child2); |
| 84 | } |
| 85 | |
| 86 | TEST(BejTreeTest, AddInteger) |
| 87 | { |
| 88 | const char* name = "SomeProperty"; |
| 89 | struct RedfishPropertyParent parent; |
| 90 | struct RedfishPropertyLeafInt child; |
| 91 | |
| 92 | bejTreeInitSet(&parent, nullptr); |
| 93 | bejTreeAddInteger(&parent, &child, name, 1024); |
| 94 | |
| 95 | EXPECT_THAT(child.leaf.nodeAttr.name, name); |
| 96 | EXPECT_THAT(child.leaf.nodeAttr.format.principalDataType, bejInteger); |
| 97 | EXPECT_THAT(child.leaf.nodeAttr.format.deferredBinding, 0); |
| 98 | EXPECT_THAT(child.leaf.nodeAttr.format.readOnlyProperty, 0); |
| 99 | EXPECT_THAT(child.leaf.nodeAttr.format.nullableProperty, 0); |
| 100 | EXPECT_THAT(child.leaf.nodeAttr.sibling, nullptr); |
| 101 | EXPECT_THAT(child.value, 1024); |
| 102 | } |
| 103 | |
| 104 | TEST(BejTreeTest, SetInteger) |
| 105 | { |
| 106 | const char* name = "SomeProperty"; |
| 107 | struct RedfishPropertyParent parent; |
| 108 | struct RedfishPropertyLeafInt child; |
| 109 | |
| 110 | bejTreeInitSet(&parent, nullptr); |
| 111 | bejTreeAddInteger(&parent, &child, name, 1024); |
| 112 | |
| 113 | EXPECT_THAT(child.value, 1024); |
| 114 | bejTreeSetInteger(&child, 20); |
| 115 | EXPECT_THAT(child.value, 20); |
| 116 | } |
| 117 | |
| 118 | TEST(BejTreeTest, AddEnum) |
| 119 | { |
| 120 | const char* name = "SomeProperty"; |
| 121 | const char* enumValue = "EnumValue"; |
| 122 | struct RedfishPropertyParent parent; |
| 123 | struct RedfishPropertyLeafEnum child; |
| 124 | |
| 125 | bejTreeInitSet(&parent, nullptr); |
| 126 | bejTreeAddEnum(&parent, &child, name, enumValue); |
| 127 | |
| 128 | EXPECT_THAT(child.leaf.nodeAttr.name, name); |
| 129 | EXPECT_THAT(child.leaf.nodeAttr.format.principalDataType, bejEnum); |
| 130 | EXPECT_THAT(child.leaf.nodeAttr.format.deferredBinding, 0); |
| 131 | EXPECT_THAT(child.leaf.nodeAttr.format.readOnlyProperty, 0); |
| 132 | EXPECT_THAT(child.leaf.nodeAttr.format.nullableProperty, 0); |
| 133 | EXPECT_THAT(child.leaf.nodeAttr.sibling, nullptr); |
| 134 | EXPECT_THAT(child.value, enumValue); |
| 135 | } |
| 136 | |
kasunath | dc3f214 | 2023-01-31 15:09:10 -0800 | [diff] [blame^] | 137 | TEST(BejTreeTest, AddString) |
| 138 | { |
| 139 | const char* name = "SomeProperty"; |
| 140 | const char* stringValue = "StringValue"; |
| 141 | struct RedfishPropertyParent parent; |
| 142 | struct RedfishPropertyLeafString child; |
| 143 | |
| 144 | bejTreeInitSet(&parent, nullptr); |
| 145 | bejTreeAddString(&parent, &child, name, stringValue); |
| 146 | |
| 147 | EXPECT_THAT(child.leaf.nodeAttr.name, name); |
| 148 | EXPECT_THAT(child.leaf.nodeAttr.format.principalDataType, bejString); |
| 149 | EXPECT_THAT(child.leaf.nodeAttr.format.deferredBinding, 0); |
| 150 | EXPECT_THAT(child.leaf.nodeAttr.format.readOnlyProperty, 0); |
| 151 | EXPECT_THAT(child.leaf.nodeAttr.format.nullableProperty, 0); |
| 152 | EXPECT_THAT(child.leaf.nodeAttr.sibling, nullptr); |
| 153 | EXPECT_THAT(child.value, stringValue); |
| 154 | } |
| 155 | |
| 156 | TEST(BejTreeTest, AddReal) |
| 157 | { |
| 158 | const char* name = "SomeProperty"; |
| 159 | double value = 10.50; |
| 160 | struct RedfishPropertyParent parent; |
| 161 | struct RedfishPropertyLeafReal child; |
| 162 | |
| 163 | bejTreeInitSet(&parent, nullptr); |
| 164 | bejTreeAddReal(&parent, &child, name, value); |
| 165 | |
| 166 | EXPECT_THAT(child.leaf.nodeAttr.name, name); |
| 167 | EXPECT_THAT(child.leaf.nodeAttr.format.principalDataType, bejReal); |
| 168 | EXPECT_THAT(child.leaf.nodeAttr.format.deferredBinding, 0); |
| 169 | EXPECT_THAT(child.leaf.nodeAttr.format.readOnlyProperty, 0); |
| 170 | EXPECT_THAT(child.leaf.nodeAttr.format.nullableProperty, 0); |
| 171 | EXPECT_THAT(child.leaf.nodeAttr.sibling, nullptr); |
| 172 | EXPECT_THAT(child.value, value); |
| 173 | } |
| 174 | |
| 175 | TEST(BejTreeTest, AddBool) |
| 176 | { |
| 177 | const char* name = "SomeProperty"; |
| 178 | bool value = true; |
| 179 | struct RedfishPropertyParent parent; |
| 180 | struct RedfishPropertyLeafBool child; |
| 181 | |
| 182 | bejTreeInitSet(&parent, nullptr); |
| 183 | bejTreeAddBool(&parent, &child, name, value); |
| 184 | |
| 185 | EXPECT_THAT(child.leaf.nodeAttr.name, name); |
| 186 | EXPECT_THAT(child.leaf.nodeAttr.format.principalDataType, bejBoolean); |
| 187 | EXPECT_THAT(child.leaf.nodeAttr.format.deferredBinding, 0); |
| 188 | EXPECT_THAT(child.leaf.nodeAttr.format.readOnlyProperty, 0); |
| 189 | EXPECT_THAT(child.leaf.nodeAttr.format.nullableProperty, 0); |
| 190 | EXPECT_THAT(child.leaf.nodeAttr.sibling, nullptr); |
| 191 | EXPECT_THAT(child.value, value); |
| 192 | } |
| 193 | |
| 194 | TEST(BejTreeTest, NodeFlags) |
| 195 | { |
| 196 | struct RedfishPropertyParent parent; |
| 197 | bejTreeInitSet(&parent, nullptr); |
| 198 | EXPECT_THAT(parent.nodeAttr.format.deferredBinding, 0); |
| 199 | EXPECT_THAT(parent.nodeAttr.format.readOnlyProperty, 0); |
| 200 | EXPECT_THAT(parent.nodeAttr.format.nullableProperty, 0); |
| 201 | |
| 202 | bejTreeUpdateNodeFlags(&parent.nodeAttr, true, true, true); |
| 203 | EXPECT_THAT(parent.nodeAttr.format.deferredBinding, 1); |
| 204 | EXPECT_THAT(parent.nodeAttr.format.readOnlyProperty, 1); |
| 205 | EXPECT_THAT(parent.nodeAttr.format.nullableProperty, 1); |
| 206 | } |
| 207 | |
| 208 | TEST(BejTreeTest, NodeType) |
| 209 | { |
| 210 | struct RedfishPropertyParent parent; |
| 211 | struct RedfishPropertyLeafBool child1; |
| 212 | struct RedfishPropertyLeafReal child2; |
| 213 | |
| 214 | bejTreeInitSet(&parent, nullptr); |
| 215 | bejTreeAddBool(&parent, &child1, nullptr, true); |
| 216 | bejTreeAddReal(&parent, &child2, nullptr, 10.5); |
| 217 | |
| 218 | EXPECT_THAT(bejTreeIsParentType(&parent.nodeAttr), true); |
| 219 | EXPECT_THAT(bejTreeIsParentType(&child1.leaf.nodeAttr), false); |
| 220 | EXPECT_THAT(bejTreeIsParentType(&child2.leaf.nodeAttr), false); |
| 221 | } |
| 222 | |
kasunath | 2ebe82f | 2023-01-31 13:59:22 -0800 | [diff] [blame] | 223 | } // namespace libbej |