test/zserio/ReflectableUtilTest.cpp
Line | Count | Source |
1 | | #include <cmath> |
2 | | |
3 | | #include "gtest/gtest.h" |
4 | | #include "test_object/std_allocator/ReflectableUtilBitmask.h" |
5 | | #include "test_object/std_allocator/ReflectableUtilEnum.h" |
6 | | #include "test_object/std_allocator/ReflectableUtilUnion.h" |
7 | | #include "zserio/Reflectable.h" |
8 | | #include "zserio/ReflectableUtil.h" |
9 | | #include "zserio/TypeInfo.h" |
10 | | |
11 | | using test_object::std_allocator::ReflectableUtilBitmask; |
12 | | using test_object::std_allocator::ReflectableUtilChoice; |
13 | | using test_object::std_allocator::ReflectableUtilEnum; |
14 | | using test_object::std_allocator::ReflectableUtilObject; |
15 | | using test_object::std_allocator::ReflectableUtilUnion; |
16 | | |
17 | | namespace zserio |
18 | | { |
19 | | |
20 | | TEST(ReflectableUtilTest, unequalTypeInfo) |
21 | 1 | { |
22 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getBool(false), ReflectableFactory::getUInt8(13))); |
23 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getInt8(0), ReflectableFactory::getInt16(0))); |
24 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getInt8(0), ReflectableFactory::getUInt8(0))); |
25 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getUInt64(0), ReflectableFactory::getVarSize(0))); |
26 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getInt8(0), ReflectableFactory::getFloat32(0.0))); |
27 | 1 | ASSERT_FALSE( |
28 | 1 | ReflectableUtil::equal(ReflectableFactory::getFloat16(0.0F), ReflectableFactory::getFloat32(0.0F))); |
29 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getString(""), ReflectableFactory::getInt8(0))); |
30 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
31 | 1 | ReflectableFactory::getBitBuffer(BitBuffer()), ReflectableFactory::getString(""))); |
32 | 1 | } |
33 | | |
34 | | TEST(ReflectableUtilTest, equalsNullValues) |
35 | 1 | { |
36 | 1 | ASSERT_TRUE(ReflectableUtil::equal(nullptr, nullptr)); |
37 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getBool(false), nullptr)); |
38 | 1 | ASSERT_FALSE(ReflectableUtil::equal(nullptr, ReflectableFactory::getInt8(0))); |
39 | 1 | } |
40 | | |
41 | | TEST(ReflectableUtilTest, equalBools) |
42 | 1 | { |
43 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getBool(true), ReflectableFactory::getBool(true))); |
44 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getBool(false), ReflectableFactory::getBool(true))); |
45 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getBool(true), ReflectableFactory::getBool(false))); |
46 | 1 | } |
47 | | |
48 | | TEST(ReflectableUtilTest, equalSingedIntegrals) |
49 | 1 | { |
50 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
51 | 1 | ReflectableFactory::getInt8(INT8_MIN), ReflectableFactory::getInt8(INT8_MIN))); |
52 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
53 | 1 | ReflectableFactory::getInt8(INT8_MAX), ReflectableFactory::getInt8(INT8_MAX))); |
54 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getInt8(-1), ReflectableFactory::getInt8(0))); |
55 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
56 | 1 | ReflectableFactory::getInt8(INT8_MIN), ReflectableFactory::getInt8(INT8_MAX))); |
57 | | |
58 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
59 | 1 | ReflectableFactory::getInt16(INT16_MIN), ReflectableFactory::getInt16(INT16_MIN))); |
60 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
61 | 1 | ReflectableFactory::getInt16(INT16_MAX), ReflectableFactory::getInt16(INT16_MAX))); |
62 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getInt16(-1), ReflectableFactory::getInt16(0))); |
63 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
64 | 1 | ReflectableFactory::getInt16(INT16_MIN), ReflectableFactory::getInt16(INT16_MAX))); |
65 | | |
66 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
67 | 1 | ReflectableFactory::getInt32(INT32_MIN), ReflectableFactory::getInt32(INT32_MIN))); |
68 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
69 | 1 | ReflectableFactory::getInt32(INT32_MAX), ReflectableFactory::getInt32(INT32_MAX))); |
70 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getInt32(-1), ReflectableFactory::getInt32(0))); |
71 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
72 | 1 | ReflectableFactory::getInt32(INT32_MIN), ReflectableFactory::getInt32(INT32_MAX))); |
73 | | |
74 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
75 | 1 | ReflectableFactory::getInt64(INT64_MIN), ReflectableFactory::getInt64(INT64_MIN))); |
76 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
77 | 1 | ReflectableFactory::getInt64(INT64_MAX), ReflectableFactory::getInt64(INT64_MAX))); |
78 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getInt64(-1), ReflectableFactory::getInt64(0))); |
79 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
80 | 1 | ReflectableFactory::getInt64(INT64_MIN), ReflectableFactory::getInt64(INT64_MAX))); |
81 | 1 | } |
82 | | |
83 | | TEST(ReflectableUtilTest, equalUnsignedIntegrals) |
84 | 1 | { |
85 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getUInt8(0), ReflectableFactory::getUInt8(0))); |
86 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
87 | 1 | ReflectableFactory::getUInt8(UINT8_MAX), ReflectableFactory::getUInt8(UINT8_MAX))); |
88 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getUInt8(0), ReflectableFactory::getUInt8(1))); |
89 | 1 | ASSERT_FALSE( |
90 | 1 | ReflectableUtil::equal(ReflectableFactory::getUInt8(0), ReflectableFactory::getUInt8(UINT8_MAX))); |
91 | | |
92 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getUInt16(0), ReflectableFactory::getUInt16(0))); |
93 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
94 | 1 | ReflectableFactory::getUInt16(UINT16_MAX), ReflectableFactory::getUInt16(UINT16_MAX))); |
95 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getUInt16(0), ReflectableFactory::getUInt16(1))); |
96 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
97 | 1 | ReflectableFactory::getUInt16(0), ReflectableFactory::getUInt16(UINT16_MAX))); |
98 | | |
99 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getUInt32(0), ReflectableFactory::getUInt32(0))); |
100 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
101 | 1 | ReflectableFactory::getUInt32(UINT32_MAX), ReflectableFactory::getUInt32(UINT32_MAX))); |
102 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getUInt32(0), ReflectableFactory::getUInt32(1))); |
103 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
104 | 1 | ReflectableFactory::getUInt32(0), ReflectableFactory::getUInt32(UINT32_MAX))); |
105 | | |
106 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getUInt64(0), ReflectableFactory::getUInt64(0))); |
107 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
108 | 1 | ReflectableFactory::getUInt64(UINT64_MAX), ReflectableFactory::getUInt64(UINT64_MAX))); |
109 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getUInt64(0), ReflectableFactory::getUInt64(1))); |
110 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
111 | 1 | ReflectableFactory::getUInt64(0), ReflectableFactory::getUInt64(UINT64_MAX))); |
112 | 1 | } |
113 | | |
114 | | TEST(ReflectableUtilTest, equalFloatingPoints) |
115 | 1 | { |
116 | 1 | ASSERT_TRUE( |
117 | 1 | ReflectableUtil::equal(ReflectableFactory::getFloat16(0.0F), ReflectableFactory::getFloat16(0.0F))); |
118 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
119 | 1 | ReflectableFactory::getFloat16(-1.0F), ReflectableFactory::getFloat16(1.0F))); |
120 | | |
121 | 1 | ASSERT_TRUE( |
122 | 1 | ReflectableUtil::equal(ReflectableFactory::getFloat32(0.0F), ReflectableFactory::getFloat32(0.0F))); |
123 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
124 | 1 | ReflectableFactory::getFloat32(-1.0F), ReflectableFactory::getFloat32(1.0F))); |
125 | | |
126 | 1 | ASSERT_TRUE( |
127 | 1 | ReflectableUtil::equal(ReflectableFactory::getFloat64(0.0), ReflectableFactory::getFloat64(0.0))); |
128 | 1 | ASSERT_FALSE( |
129 | 1 | ReflectableUtil::equal(ReflectableFactory::getFloat64(-1.0), ReflectableFactory::getFloat64(1.0))); |
130 | | |
131 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getFloat64(std::numeric_limits<double>::epsilon()), |
132 | 1 | ReflectableFactory::getFloat64(std::numeric_limits<double>::epsilon()))); |
133 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getFloat64(std::numeric_limits<double>::min()), |
134 | 1 | ReflectableFactory::getFloat64(std::numeric_limits<double>::min()))); |
135 | | |
136 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
137 | 1 | ReflectableFactory::getFloat64(1.0 + std::numeric_limits<double>::epsilon()), |
138 | 1 | ReflectableFactory::getFloat64(1.0))); |
139 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
140 | 1 | ReflectableFactory::getFloat64(std::numeric_limits<double>::denorm_min() * 2), |
141 | 1 | ReflectableFactory::getFloat64(std::numeric_limits<double>::denorm_min()))); |
142 | | |
143 | 1 | auto testA = ReflectableFactory::getFloat64(static_cast<double>(NAN)); |
144 | 1 | auto testB = ReflectableFactory::getFloat64(static_cast<double>(NAN)); |
145 | 1 | ASSERT_TRUE(ReflectableUtil::equal(testA, testB)); |
146 | 1 | testA = ReflectableFactory::getFloat64(static_cast<double>(INFINITY)); |
147 | 1 | testB = ReflectableFactory::getFloat64(static_cast<double>(INFINITY)); |
148 | 1 | ASSERT_TRUE(ReflectableUtil::equal(testA, testB)); |
149 | 1 | testA = ReflectableFactory::getFloat64(-static_cast<double>(INFINITY)); |
150 | 1 | testB = ReflectableFactory::getFloat64(-static_cast<double>(INFINITY)); |
151 | 1 | ASSERT_TRUE(ReflectableUtil::equal(testA, testB)); |
152 | | |
153 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getFloat64(0.0), |
154 | 1 | ReflectableFactory::getFloat64(static_cast<double>(INFINITY)))); |
155 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getFloat64(static_cast<double>(INFINITY)), |
156 | 1 | ReflectableFactory::getFloat64(0.0))); |
157 | | |
158 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
159 | 1 | ReflectableFactory::getFloat64(0.0), ReflectableFactory::getFloat64(static_cast<double>(NAN)))); |
160 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
161 | 1 | ReflectableFactory::getFloat64(static_cast<double>(NAN)), ReflectableFactory::getFloat64(0.0))); |
162 | | |
163 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getFloat64(static_cast<double>(NAN)), |
164 | 1 | ReflectableFactory::getFloat64(static_cast<double>(INFINITY)))); |
165 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getFloat64(static_cast<double>(INFINITY)), |
166 | 1 | ReflectableFactory::getFloat64(static_cast<double>(NAN)))); |
167 | | |
168 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getFloat64(static_cast<double>(INFINITY)), |
169 | 1 | ReflectableFactory::getFloat64(-static_cast<double>(INFINITY)))); |
170 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getFloat64(-static_cast<double>(INFINITY)), |
171 | 1 | ReflectableFactory::getFloat64(static_cast<double>(INFINITY)))); |
172 | 1 | } |
173 | | |
174 | | TEST(ReflectableUtilTest, equalStrings) |
175 | 1 | { |
176 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getString(""), ReflectableFactory::getString(""))); |
177 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
178 | 1 | ReflectableFactory::getString("test"), ReflectableFactory::getString("test"))); |
179 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getString(""), ReflectableFactory::getString("a"))); |
180 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getString("1"), ReflectableFactory::getString(""))); |
181 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
182 | 1 | ReflectableFactory::getString("test"), ReflectableFactory::getString("test2"))); |
183 | 1 | } |
184 | | |
185 | | TEST(ReflectableUtilTest, equalBitBuffers) |
186 | 1 | { |
187 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
188 | 1 | ReflectableFactory::getBitBuffer(BitBuffer()), ReflectableFactory::getBitBuffer(BitBuffer()))); |
189 | 1 | ASSERT_TRUE(ReflectableUtil::equal(ReflectableFactory::getBitBuffer(BitBuffer({0xAB}, 8)), |
190 | 1 | ReflectableFactory::getBitBuffer(BitBuffer({0xAB}, 8)))); |
191 | 1 | ASSERT_FALSE(ReflectableUtil::equal(ReflectableFactory::getBitBuffer(BitBuffer({0xAB}, 8)), |
192 | 1 | ReflectableFactory::getBitBuffer(BitBuffer()))); |
193 | 1 | } |
194 | | |
195 | | TEST(ReflectableUtilTest, equalBytes) |
196 | 1 | { |
197 | 1 | vector<uint8_t> bytesData1{{0xCA, 0xFE}}; |
198 | 1 | vector<uint8_t> bytesData2{{0xCA, 0xDE}}; |
199 | 1 | vector<uint8_t> bytesEmpty; |
200 | | |
201 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
202 | 1 | ReflectableFactory::getBytes(bytesEmpty), ReflectableFactory::getBytes(bytesEmpty))); |
203 | 1 | ASSERT_TRUE(ReflectableUtil::equal( |
204 | 1 | ReflectableFactory::getBytes(bytesData1), ReflectableFactory::getBytes(bytesData1))); |
205 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
206 | 1 | ReflectableFactory::getBytes(bytesData1), ReflectableFactory::getBytes(bytesEmpty))); |
207 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
208 | 1 | ReflectableFactory::getBytes(bytesData1), ReflectableFactory::getBytes(bytesData2))); |
209 | 1 | } |
210 | | |
211 | | TEST(ReflectableUtilTest, equalEnums) |
212 | 1 | { |
213 | 1 | const ReflectableUtilEnum oneEnum = ReflectableUtilEnum::ONE; |
214 | 1 | const ReflectableUtilEnum twoEnum = ReflectableUtilEnum::TWO; |
215 | | |
216 | 1 | ASSERT_TRUE(ReflectableUtil::equal(enumReflectable(oneEnum), enumReflectable(oneEnum))); |
217 | 1 | ASSERT_TRUE(ReflectableUtil::equal(enumReflectable(twoEnum), enumReflectable(twoEnum))); |
218 | 1 | ASSERT_FALSE(ReflectableUtil::equal(enumReflectable(oneEnum), enumReflectable(twoEnum))); |
219 | 1 | } |
220 | | |
221 | | TEST(ReflectableUtilTest, equalBitmasks) |
222 | 1 | { |
223 | 1 | const ReflectableUtilBitmask readBitmask = ReflectableUtilBitmask::Values::READ; |
224 | 1 | const ReflectableUtilBitmask writeBitmask = ReflectableUtilBitmask::Values::WRITE; |
225 | | |
226 | 1 | ASSERT_TRUE(ReflectableUtil::equal(readBitmask.reflectable(), readBitmask.reflectable())); |
227 | 1 | ASSERT_TRUE(ReflectableUtil::equal(writeBitmask.reflectable(), writeBitmask.reflectable())); |
228 | 1 | ASSERT_FALSE(ReflectableUtil::equal(writeBitmask.reflectable(), readBitmask.reflectable())); |
229 | 1 | } |
230 | | |
231 | | TEST(ReflectableUtilTest, equalCompounds) |
232 | 1 | { |
233 | 1 | ReflectableUtilUnion compound1 = ReflectableUtilUnion(); |
234 | 1 | compound1.setReflectableUtilEnum(ReflectableUtilEnum::ONE); |
235 | 1 | compound1.initializeChildren(); |
236 | | |
237 | 1 | ReflectableUtilUnion compound2 = ReflectableUtilUnion(); |
238 | 1 | compound2.setReflectableUtilEnum(ReflectableUtilEnum::TWO); // different enum value |
239 | 1 | compound2.initializeChildren(); |
240 | | |
241 | 1 | ReflectableUtilUnion compound3 = ReflectableUtilUnion(); |
242 | 1 | compound3.setReflectableUtilBitmask(ReflectableUtilBitmask::Values::READ); |
243 | 1 | compound3.initializeChildren(); |
244 | | |
245 | 1 | ReflectableUtilUnion compound4 = ReflectableUtilUnion(); |
246 | 1 | compound4.setReflectableUtilObject(ReflectableUtilObject(0, ReflectableUtilChoice())); |
247 | 1 | compound4.initializeChildren(); |
248 | | |
249 | 1 | ReflectableUtilUnion compound5 = ReflectableUtilUnion(); |
250 | 1 | compound5.setReflectableUtilObject(ReflectableUtilObject(1, ReflectableUtilChoice())); |
251 | 1 | compound5.getReflectableUtilObject().getReflectableUtilChoice().setArray(std::vector<uint32_t>()); |
252 | 1 | compound5.initializeChildren(); |
253 | | |
254 | 1 | ReflectableUtilUnion compound6 = ReflectableUtilUnion(); |
255 | 1 | compound6.setReflectableUtilObject(ReflectableUtilObject(1, ReflectableUtilChoice())); |
256 | 1 | compound6.getReflectableUtilObject().getReflectableUtilChoice().setArray(std::vector<uint32_t>{{1, 2, 3}}); |
257 | 1 | compound6.initializeChildren(); |
258 | | |
259 | 1 | ASSERT_TRUE(ReflectableUtil::equal(compound1.reflectable(), compound1.reflectable())); |
260 | 1 | ASSERT_TRUE(ReflectableUtil::equal(compound2.reflectable(), compound2.reflectable())); |
261 | 1 | ASSERT_TRUE(ReflectableUtil::equal(compound3.reflectable(), compound3.reflectable())); |
262 | 1 | ASSERT_TRUE(ReflectableUtil::equal(compound4.reflectable(), compound4.reflectable())); |
263 | 1 | ASSERT_TRUE(ReflectableUtil::equal(compound5.reflectable(), compound5.reflectable())); |
264 | 1 | ASSERT_TRUE(ReflectableUtil::equal(compound6.reflectable(), compound6.reflectable())); |
265 | | |
266 | 1 | ASSERT_FALSE(ReflectableUtil::equal(compound1.reflectable(), compound2.reflectable())); |
267 | 1 | ASSERT_FALSE(ReflectableUtil::equal(compound1.reflectable(), compound3.reflectable())); |
268 | 1 | ASSERT_FALSE(ReflectableUtil::equal(compound1.reflectable(), compound4.reflectable())); |
269 | 1 | ASSERT_FALSE(ReflectableUtil::equal(compound1.reflectable(), compound5.reflectable())); |
270 | 1 | ASSERT_FALSE(ReflectableUtil::equal(compound1.reflectable(), compound6.reflectable())); |
271 | | |
272 | | // unequal fields in choice |
273 | 1 | ASSERT_FALSE(ReflectableUtil::equal(compound5.reflectable(), compound6.reflectable())); |
274 | | |
275 | | // unequal parameters in choice |
276 | 1 | ReflectableUtilChoice choice1; |
277 | 1 | choice1.initialize(0); |
278 | 1 | ReflectableUtilChoice choice2; |
279 | 1 | choice2.initialize(3); |
280 | 1 | ASSERT_FALSE(ReflectableUtil::equal(choice1.reflectable(), choice2.reflectable())); |
281 | | |
282 | | // array and not array |
283 | 1 | std::vector<ReflectableUtilUnion> compoundArray(1); |
284 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
285 | 1 | compound1.reflectable(), ReflectableFactory::getCompoundArray(compoundArray))); |
286 | 1 | ASSERT_FALSE(ReflectableUtil::equal( |
287 | 1 | ReflectableFactory::getCompoundArray(compoundArray), compound1.reflectable())); |
288 | 1 | } |
289 | | |
290 | | TEST(ReflectableUtilTest, equalArrays) |
291 | 1 | { |
292 | 1 | std::vector<uint32_t> array1 = {1, 2, 3}; |
293 | 1 | std::vector<uint32_t> array2 = {1, 2, 4}; |
294 | 1 | std::vector<uint32_t> array3 = {1, 2}; |
295 | | |
296 | 1 | auto array1Reflectable = ReflectableFactory::getUInt32Array(array1); |
297 | 1 | auto array2Reflectable = ReflectableFactory::getUInt32Array(array2); |
298 | 1 | auto array3Reflectable = ReflectableFactory::getUInt32Array(array3); |
299 | | |
300 | 1 | ASSERT_TRUE(ReflectableUtil::equal(array1Reflectable, array1Reflectable)); |
301 | 1 | ASSERT_TRUE(ReflectableUtil::equal(array2Reflectable, array2Reflectable)); |
302 | 1 | ASSERT_TRUE(ReflectableUtil::equal(array3Reflectable, array3Reflectable)); |
303 | | |
304 | 1 | ASSERT_FALSE(ReflectableUtil::equal(array1Reflectable, array2Reflectable)); |
305 | 1 | ASSERT_FALSE(ReflectableUtil::equal(array1Reflectable, array3Reflectable)); |
306 | 1 | } |
307 | | |
308 | | TEST(ReflectableUtilTest, equalWrong) |
309 | 1 | { |
310 | 1 | using allocator_type = ::std::allocator<uint8_t>; |
311 | 1 | class WrongTypeInfo : public TypeInfoBase<allocator_type> |
312 | 1 | { |
313 | 1 | public: |
314 | 1 | WrongTypeInfo(StringView schemaName, SchemaType schemaType, CppType cppType) : |
315 | 1 | TypeInfoBase(schemaName, schemaType, cppType) |
316 | 2 | {} |
317 | 1 | }; |
318 | | |
319 | 1 | const WrongTypeInfo wrongTypeInfo = {"int<>"_sv, SchemaType::DYNAMIC_SIGNED_BITFIELD, CppType::PUBSUB}; |
320 | 1 | const WrongTypeInfo wrongTypeInfoDiffName = { |
321 | 1 | "diff"_sv, SchemaType::DYNAMIC_SIGNED_BITFIELD, CppType::PUBSUB}; |
322 | | |
323 | 1 | class Reflectable : public ::zserio::ReflectableAllocatorHolderBase<allocator_type> |
324 | 1 | { |
325 | 1 | public: |
326 | 1 | explicit Reflectable(const TypeInfoBase<allocator_type>& typeInfo, const allocator_type& allocator) : |
327 | 1 | ::zserio::ReflectableAllocatorHolderBase<allocator_type>(typeInfo, allocator) |
328 | 2 | {} |
329 | 1 | }; |
330 | | |
331 | 1 | auto wrongReflectable = |
332 | 1 | std::allocate_shared<Reflectable>(allocator_type(), wrongTypeInfo, allocator_type()); |
333 | 1 | auto wrongReflectableDiffName = |
334 | 1 | std::allocate_shared<Reflectable>(allocator_type(), wrongTypeInfoDiffName, allocator_type()); |
335 | | |
336 | 1 | ASSERT_THROW(ReflectableUtil::equal(wrongReflectable, wrongReflectable), CppRuntimeException); |
337 | 1 | ASSERT_FALSE(ReflectableUtil::equal(wrongReflectable, wrongReflectableDiffName)); |
338 | 1 | } |
339 | | |
340 | | TEST(ReflectableUtilTest, getValueArithmeticType) |
341 | 1 | { |
342 | 1 | ASSERT_FALSE(ReflectableUtil::getValue<bool>(ReflectableFactory::getBool(false))); |
343 | | |
344 | 1 | ASSERT_EQ(1, ReflectableUtil::getValue<uint8_t>(ReflectableFactory::getUInt8(1))); |
345 | 1 | ASSERT_EQ(12, ReflectableUtil::getValue<uint16_t>(ReflectableFactory::getUInt16(12))); |
346 | 1 | ASSERT_EQ(123, ReflectableUtil::getValue<uint32_t>(ReflectableFactory::getUInt32(123))); |
347 | 1 | ASSERT_EQ(1234, ReflectableUtil::getValue<uint64_t>(ReflectableFactory::getUInt64(1234))); |
348 | | |
349 | 1 | ASSERT_EQ(-1, ReflectableUtil::getValue<int8_t>(ReflectableFactory::getInt8(-1))); |
350 | 1 | ASSERT_EQ(-12, ReflectableUtil::getValue<int16_t>(ReflectableFactory::getInt16(-12))); |
351 | 1 | ASSERT_EQ(-123, ReflectableUtil::getValue<int32_t>(ReflectableFactory::getInt32(-123))); |
352 | 1 | ASSERT_EQ(-1234, ReflectableUtil::getValue<int64_t>(ReflectableFactory::getInt64(-1234))); |
353 | | |
354 | 1 | ASSERT_EQ(12, ReflectableUtil::getValue<uint16_t>(ReflectableFactory::getVarUInt16(12))); |
355 | 1 | ASSERT_EQ(123, ReflectableUtil::getValue<uint32_t>(ReflectableFactory::getVarUInt32(123))); |
356 | 1 | ASSERT_EQ(1234, ReflectableUtil::getValue<uint64_t>(ReflectableFactory::getVarUInt64(1234))); |
357 | 1 | ASSERT_EQ(UINT64_MAX, ReflectableUtil::getValue<uint64_t>(ReflectableFactory::getVarUInt(UINT64_MAX))); |
358 | | |
359 | 1 | ASSERT_EQ(-12, ReflectableUtil::getValue<int16_t>(ReflectableFactory::getVarInt16(-12))); |
360 | 1 | ASSERT_EQ(-123, ReflectableUtil::getValue<int32_t>(ReflectableFactory::getVarInt32(-123))); |
361 | 1 | ASSERT_EQ(-1234, ReflectableUtil::getValue<int64_t>(ReflectableFactory::getVarInt64(-1234))); |
362 | 1 | ASSERT_EQ(INT64_MIN, ReflectableUtil::getValue<int64_t>(ReflectableFactory::getVarInt(INT64_MIN))); |
363 | 1 | ASSERT_EQ(INT64_MAX, ReflectableUtil::getValue<int64_t>(ReflectableFactory::getVarInt(INT64_MAX))); |
364 | | |
365 | 1 | ASSERT_EQ(0, ReflectableUtil::getValue<uint32_t>(ReflectableFactory::getVarSize(0))); |
366 | | |
367 | 1 | ASSERT_EQ(1.0F, ReflectableUtil::getValue<float>(ReflectableFactory::getFloat16(1.0F))); |
368 | 1 | ASSERT_EQ(3.5F, ReflectableUtil::getValue<float>(ReflectableFactory::getFloat32(3.5F))); |
369 | 1 | ASSERT_EQ(9.875, ReflectableUtil::getValue<double>(ReflectableFactory::getFloat64(9.875))); |
370 | 1 | } |
371 | | |
372 | | TEST(ReflectableUtilTest, getValueString) |
373 | 1 | { |
374 | 1 | ASSERT_EQ("test"_sv, ReflectableUtil::getValue<StringView>(ReflectableFactory::getString("test"))); |
375 | 1 | } |
376 | | |
377 | | TEST(ReflectableUtilTest, getValueBitBuffer) |
378 | 1 | { |
379 | 1 | const BitBuffer bitBuffer; |
380 | 1 | auto reflectable = ReflectableFactory::getBitBuffer(bitBuffer); |
381 | 1 | const BitBuffer& bitBufferRef = ReflectableUtil::getValue<BitBuffer>(reflectable); |
382 | 1 | ASSERT_EQ(bitBuffer, bitBufferRef); |
383 | 1 | } |
384 | | |
385 | | TEST(ReflectableUtilTest, getValueEnum) |
386 | 1 | { |
387 | 1 | ReflectableUtilEnum reflectableUtilEnum = ReflectableUtilEnum::ONE; |
388 | 1 | auto reflectable = enumReflectable(reflectableUtilEnum); |
389 | 1 | ASSERT_EQ(reflectableUtilEnum, ReflectableUtil::getValue<ReflectableUtilEnum>(reflectable)); |
390 | 1 | } |
391 | | |
392 | | TEST(ReflectableUtilTest, getValueBitmask) |
393 | 1 | { |
394 | 1 | ReflectableUtilBitmask reflectableUtilBitmask = ReflectableUtilBitmask::Values::READ; |
395 | 1 | auto reflectable = reflectableUtilBitmask.reflectable(); |
396 | 1 | ASSERT_EQ(reflectableUtilBitmask, ReflectableUtil::getValue<ReflectableUtilBitmask>(reflectable)); |
397 | 1 | } |
398 | | |
399 | | TEST(ReflectableUtilTest, getValueCompound) |
400 | 1 | { |
401 | 1 | ReflectableUtilUnion reflectableUtilUnion; |
402 | 1 | auto reflectable = reflectableUtilUnion.reflectable(); |
403 | 1 | ASSERT_EQ(&reflectableUtilUnion, &ReflectableUtil::getValue<ReflectableUtilUnion>(reflectable)); |
404 | | |
405 | 1 | ReflectableUtilUnion& reflectableUtilUnionRef = |
406 | 1 | ReflectableUtil::getValue<ReflectableUtilUnion>(reflectable); |
407 | 1 | reflectableUtilUnionRef.setReflectableUtilBitmask(ReflectableUtilBitmask::Values::WRITE); |
408 | 1 | ASSERT_EQ(ReflectableUtilBitmask::Values::WRITE, reflectableUtilUnion.getReflectableUtilBitmask()); |
409 | | |
410 | 1 | auto constReflectable = static_cast<const ReflectableUtilUnion&>(reflectableUtilUnion).reflectable(); |
411 | 1 | const ReflectableUtilUnion& reflectableUtilUnionConstRef = |
412 | 1 | ReflectableUtil::getValue<ReflectableUtilUnion>(constReflectable); |
413 | 1 | ASSERT_EQ(ReflectableUtilBitmask::Values::WRITE, reflectableUtilUnionConstRef.getReflectableUtilBitmask()); |
414 | 1 | ASSERT_EQ(&reflectableUtilUnion, &reflectableUtilUnionConstRef); |
415 | 1 | } |
416 | | |
417 | | TEST(ReflectableUtilTest, getValueBuiltinArray) |
418 | 1 | { |
419 | 1 | std::vector<uint8_t> uint8Array{{1, 2, 3}}; |
420 | 1 | auto reflectable = ReflectableFactory::getUInt8Array(uint8Array); |
421 | 1 | std::vector<uint8_t>& uint8ArrayRef = ReflectableUtil::getValue<std::vector<uint8_t>>(reflectable); |
422 | 1 | ASSERT_EQ(&uint8Array, &uint8ArrayRef); |
423 | | |
424 | 1 | auto constReflectable = |
425 | 1 | ReflectableFactory::getUInt8Array(static_cast<const std::vector<uint8_t>&>(uint8Array)); |
426 | 1 | const std::vector<uint8_t>& uint8ArrayConstRef = |
427 | 1 | ReflectableUtil::getValue<std::vector<uint8_t>>(constReflectable); |
428 | 1 | ASSERT_EQ(&uint8Array, &uint8ArrayConstRef); |
429 | 1 | } |
430 | | |
431 | | TEST(ReflectableUtilTest, getValueCompoundArray) |
432 | 1 | { |
433 | 1 | std::vector<ReflectableUtilUnion> reflectableUtilUnionArray; |
434 | 1 | auto reflectable = ReflectableFactory::getCompoundArray(reflectableUtilUnionArray); |
435 | 1 | std::vector<ReflectableUtilUnion>& reflectableUtilUnionArrayRef = |
436 | 1 | ReflectableUtil::getValue<std::vector<ReflectableUtilUnion>>(reflectable); |
437 | 1 | ASSERT_EQ(&reflectableUtilUnionArray, &reflectableUtilUnionArrayRef); |
438 | 1 | } |
439 | | |
440 | | } // namespace zserio |