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