Coverage Report

Created: 2026-03-05 16:18

test/zserio/ZserioTreeCreatorTest.cpp
Line
Count
Source
1
#include "gtest/gtest.h"
2
#include "test_object/std_allocator/CreatorBitmask.h"
3
#include "test_object/std_allocator/CreatorEnum.h"
4
#include "test_object/std_allocator/CreatorNested.h"
5
#include "test_object/std_allocator/CreatorObject.h"
6
#include "test_object/std_allocator/CreatorUnsignedEnum.h"
7
#include "zserio/StringConvertUtil.h"
8
#include "zserio/TypeInfo.h"
9
#include "zserio/ZserioTreeCreator.h"
10
11
using test_object::std_allocator::CreatorBitmask;
12
using test_object::std_allocator::CreatorEnum;
13
using test_object::std_allocator::CreatorNested;
14
using test_object::std_allocator::CreatorObject;
15
using test_object::std_allocator::CreatorUnsignedEnum;
16
17
namespace zserio
18
{
19
20
using namespace std::literals::string_view_literals;
21
22
TEST(ZserioTreeCreator, makeAnyValue)
23
1
{
24
1
    const std::allocator<uint8_t> allocator;
25
26
    // bool
27
1
    auto any = detail::makeAnyValue(typeInfo<Bool>(), true, allocator);
28
1
    ASSERT_EQ(true, any.get<bool>());
29
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Bool>(), 1, allocator), CppRuntimeException);
30
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Bool>(), 1.0F, allocator), CppRuntimeException);
31
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Bool>(), "1", allocator), CppRuntimeException);
32
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Bool>(), BitBuffer(), allocator), CppRuntimeException);
33
34
    // uint8
35
1
    any = detail::makeAnyValue(typeInfo<UInt8>(), 8, allocator);
36
1
    ASSERT_EQ(8, any.get<uint8_t>());
37
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt8>(), -1, allocator), CppRuntimeException);
38
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt8>(), 256, allocator), CppRuntimeException);
39
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt8>(), 1.0F, allocator), CppRuntimeException);
40
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt8>(), "1", allocator), CppRuntimeException);
41
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt8>(), true, allocator), CppRuntimeException);
42
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt8>(), BitBuffer(), allocator), CppRuntimeException);
43
44
    // uint16
45
1
    any = detail::makeAnyValue(typeInfo<UInt16>(), 16, allocator);
46
1
    ASSERT_EQ(16, any.get<uint16_t>());
47
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt16>(), -1, allocator), CppRuntimeException);
48
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt16>(), 65536, allocator), CppRuntimeException);
49
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt16>(), 1.0F, allocator), CppRuntimeException);
50
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt16>(), "1", allocator), CppRuntimeException);
51
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt16>(), true, allocator), CppRuntimeException);
52
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt16>(), BitBuffer(), allocator), CppRuntimeException);
53
54
    // uint32
55
1
    any = detail::makeAnyValue(typeInfo<UInt32>(), 32, allocator);
56
1
    ASSERT_EQ(32, any.get<uint32_t>());
57
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt32>(), -1, allocator), CppRuntimeException);
58
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt32>(), 4294967296, allocator), CppRuntimeException);
59
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt32>(), 1.0F, allocator), CppRuntimeException);
60
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt32>(), "1", allocator), CppRuntimeException);
61
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt32>(), true, allocator), CppRuntimeException);
62
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt32>(), BitBuffer(), allocator), CppRuntimeException);
63
64
    // uint64
65
1
    any = detail::makeAnyValue(typeInfo<UInt64>(), 64, allocator);
66
1
    ASSERT_EQ(64, any.get<uint64_t>());
67
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt64>(), -1, allocator), CppRuntimeException);
68
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt64>(), 1.0F, allocator), CppRuntimeException);
69
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt64>(), "1", allocator), CppRuntimeException);
70
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt64>(), true, allocator), CppRuntimeException);
71
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<UInt64>(), BitBuffer(), allocator), CppRuntimeException);
72
73
    // int8
74
1
    any = detail::makeAnyValue(typeInfo<Int8>(), 8, allocator);
75
1
    ASSERT_EQ(8, any.get<int8_t>());
76
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int8>(), -129, allocator), CppRuntimeException);
77
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int8>(), 128, allocator), CppRuntimeException);
78
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int8>(), 1.0F, allocator), CppRuntimeException);
79
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int8>(), "1", allocator), CppRuntimeException);
80
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int8>(), true, allocator), CppRuntimeException);
81
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int8>(), BitBuffer(), allocator), CppRuntimeException);
82
83
    // int16
84
1
    any = detail::makeAnyValue(typeInfo<Int16>(), 16, allocator);
85
1
    ASSERT_EQ(16, any.get<int16_t>());
86
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int16>(), -32769, allocator), CppRuntimeException);
87
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int16>(), 32768, allocator), CppRuntimeException);
88
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int16>(), 1.0F, allocator), CppRuntimeException);
89
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int16>(), "1", allocator), CppRuntimeException);
90
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int16>(), true, allocator), CppRuntimeException);
91
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int16>(), BitBuffer(), allocator), CppRuntimeException);
92
93
    // int32
94
1
    any = detail::makeAnyValue(typeInfo<Int32>(), 32, allocator);
95
1
    ASSERT_EQ(32, any.get<int32_t>());
96
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int32>(), -2147483649LL, allocator), CppRuntimeException);
97
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int32>(), 2147483648LL, allocator), CppRuntimeException);
98
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int32>(), 1.0F, allocator), CppRuntimeException);
99
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int32>(), "1", allocator), CppRuntimeException);
100
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int32>(), true, allocator), CppRuntimeException);
101
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int32>(), BitBuffer(), allocator), CppRuntimeException);
102
103
    // int64
104
1
    any = detail::makeAnyValue(typeInfo<Int64>(), 64, allocator);
105
1
    ASSERT_EQ(64, any.get<int64_t>());
106
1
    ASSERT_THROW(
107
1
            detail::makeAnyValue(typeInfo<Int64>(), 9223372036854775808ULL, allocator), CppRuntimeException);
108
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int64>(), 1.0F, allocator), CppRuntimeException);
109
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int64>(), "1", allocator), CppRuntimeException);
110
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int64>(), true, allocator), CppRuntimeException);
111
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Int64>(), BitBuffer(), allocator), CppRuntimeException);
112
113
    // float
114
1
    any = detail::makeAnyValue(typeInfo<Float32>(), 3.5F, allocator);
115
1
    ASSERT_EQ(3.5F, any.get<float>());
116
1
    any = detail::makeAnyValue(typeInfo<Float32>(), 1, allocator);
117
1
    ASSERT_EQ(1.0F, any.get<float>());
118
1
    any = detail::makeAnyValue(typeInfo<Float32>(), 1.0, allocator);
119
1
    ASSERT_EQ(1.0F, any.get<float>());
120
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Float32>(), "1", allocator), CppRuntimeException);
121
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Float32>(), true, allocator), CppRuntimeException);
122
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Float32>(), BitBuffer(), allocator), CppRuntimeException);
123
124
    // double
125
1
    any = detail::makeAnyValue(typeInfo<Float64>(), 3.14, allocator);
126
1
    ASSERT_EQ(3.14, any.get<double>());
127
1
    any = detail::makeAnyValue(typeInfo<Float64>(), 1, allocator);
128
1
    ASSERT_EQ(1.0, any.get<double>());
129
1
    any = detail::makeAnyValue(typeInfo<Float64>(), 1.0F, allocator);
130
1
    ASSERT_EQ(1.0, any.get<double>());
131
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Float64>(), "1", allocator), CppRuntimeException);
132
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Float64>(), true, allocator), CppRuntimeException);
133
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<Float64>(), BitBuffer(), allocator), CppRuntimeException);
134
135
    // string
136
1
    any = detail::makeAnyValue(typeInfo<String>(), "text", allocator);
137
1
    ASSERT_EQ("text", any.get<String>());
138
1
    any = detail::makeAnyValue(typeInfo<String>(), "text"sv, allocator);
139
1
    ASSERT_EQ("text", any.get<String>());
140
1
    any = detail::makeAnyValue(typeInfo<String>(), String("text"), allocator);
141
1
    ASSERT_EQ("text", any.get<String>());
142
1
    const String stringString = "text";
143
1
    any = detail::makeAnyValue(typeInfo<String>(), stringString, allocator);
144
1
    ASSERT_EQ("text", any.get<String>());
145
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<String>(), 1, allocator), CppRuntimeException);
146
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<String>(), 1.0F, allocator), CppRuntimeException);
147
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<String>(), true, allocator), CppRuntimeException);
148
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<String>(), BitBuffer(), allocator), CppRuntimeException);
149
150
    // enum
151
1
    any = detail::makeAnyValue(typeInfo<CreatorEnum>(), CreatorEnum::ONE, allocator);
152
1
    ASSERT_EQ(CreatorEnum::ONE, any.get<CreatorEnum>());
153
1
    any = detail::makeAnyValue(typeInfo<CreatorEnum>(), 0, allocator);
154
1
    ASSERT_EQ(enumToValue(CreatorEnum::ONE), any.get<int8_t>());
155
1
    any = detail::makeAnyValue(typeInfo<CreatorEnum>(), "ONE"sv, allocator);
156
1
    ASSERT_EQ(enumToValue(CreatorEnum::ONE), any.get<int8_t>());
157
1
    any = detail::makeAnyValue(typeInfo<CreatorEnum>(), "MinusOne"sv, allocator);
158
1
    ASSERT_EQ(enumToValue(CreatorEnum::MinusOne), any.get<int8_t>());
159
1
    ASSERT_THROW(
160
1
            detail::makeAnyValue(typeInfo<CreatorEnum>(), "NONEXISTING"sv, allocator), CppRuntimeException);
161
1
    ASSERT_THROW(
162
1
            detail::makeAnyValue(typeInfo<CreatorEnum>(), "{nonexisting}"sv, allocator), CppRuntimeException);
163
1
    ASSERT_THROW(
164
1
            detail::makeAnyValue(typeInfo<CreatorEnum>(), "nonexisting"sv, allocator), CppRuntimeException);
165
1
    ASSERT_THROW(
166
1
            detail::makeAnyValue(typeInfo<CreatorEnum>(), "_nonexisting"sv, allocator), CppRuntimeException);
167
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorEnum>(), "***"sv, allocator), CppRuntimeException);
168
    // check all string overloads!
169
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorEnum>(), "10 /* no match */"sv, allocator),
170
1
            CppRuntimeException);
171
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorEnum>(), "-10 /* no match */", allocator),
172
1
            CppRuntimeException);
173
1
    ASSERT_THROW(
174
1
            detail::makeAnyValue(typeInfo<CreatorEnum>(), String("10 /* no match */", allocator), allocator),
175
1
            CppRuntimeException);
176
1
    const String enumString("10 /* no match */", allocator);
177
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorEnum>(), enumString, allocator), CppRuntimeException);
178
    // out-of-range int64_t
179
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorEnum>(), "99999999999999999999", allocator),
180
1
            CppRuntimeException);
181
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorEnum>(), "-99999999999999999999"sv, allocator),
182
1
            CppRuntimeException);
183
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorEnum>(), ""sv, allocator), CppRuntimeException);
184
185
    // unsigned enum
186
1
    any = detail::makeAnyValue(typeInfo<CreatorUnsignedEnum>(), CreatorUnsignedEnum::ONE, allocator);
187
1
    ASSERT_EQ(CreatorUnsignedEnum::ONE, any.get<CreatorUnsignedEnum>());
188
1
    any = detail::makeAnyValue(typeInfo<CreatorUnsignedEnum>(), 0, allocator);
189
1
    ASSERT_EQ(enumToValue(CreatorUnsignedEnum::ONE), any.get<uint8_t>());
190
1
    any = detail::makeAnyValue(typeInfo<CreatorUnsignedEnum>(), "ONE"sv, allocator);
191
1
    ASSERT_EQ(enumToValue(CreatorUnsignedEnum::ONE), any.get<uint8_t>());
192
1
    any = detail::makeAnyValue(typeInfo<CreatorUnsignedEnum>(), "TWO"sv, allocator);
193
1
    ASSERT_EQ(enumToValue(CreatorUnsignedEnum::TWO), any.get<uint8_t>());
194
195
    // bitmask
196
1
    any = detail::makeAnyValue(
197
1
            typeInfo<CreatorBitmask>(), CreatorBitmask(CreatorBitmask::Values::READ), allocator);
198
1
    ASSERT_EQ(CreatorBitmask::Values::READ, any.get<CreatorBitmask>());
199
1
    any = detail::makeAnyValue(typeInfo<CreatorBitmask>(), 1, allocator);
200
1
    ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ).getValue(), any.get<uint8_t>());
201
1
    any = detail::makeAnyValue(typeInfo<CreatorBitmask>(), "READ"sv, allocator);
202
1
    ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ).getValue(), any.get<uint8_t>());
203
1
    any = detail::makeAnyValue(typeInfo<CreatorBitmask>(), "READ | WRITE"sv, allocator);
204
1
    ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ | CreatorBitmask::Values::WRITE).getValue(),
205
1
            any.get<uint8_t>());
206
1
    any = detail::makeAnyValue(typeInfo<CreatorBitmask>(), "READ|WRITE"sv, allocator);
207
1
    ASSERT_EQ(CreatorBitmask(CreatorBitmask::Values::READ | CreatorBitmask::Values::WRITE).getValue(),
208
1
            any.get<uint8_t>());
209
1
    ASSERT_THROW(
210
1
            detail::makeAnyValue(typeInfo<CreatorBitmask>(), "NONEXISTING"sv, allocator), CppRuntimeException);
211
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorBitmask>(), "READ "sv, allocator), CppRuntimeException);
212
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorBitmask>(), "READ |"sv, allocator), CppRuntimeException);
213
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorBitmask>(), "READ | NONEXISTING"sv, allocator),
214
1
            CppRuntimeException);
215
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorBitmask>(), "READ * NONEXISTING"sv, allocator),
216
1
            CppRuntimeException);
217
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorBitmask>(), "***"sv, allocator), CppRuntimeException);
218
1
    any = detail::makeAnyValue(typeInfo<CreatorBitmask>(), "7 /* READ | WRITE */"sv, allocator);
219
1
    ASSERT_EQ(7, any.get<uint8_t>());
220
1
    any = detail::makeAnyValue(typeInfo<CreatorBitmask>(), "7"sv, allocator);
221
1
    ASSERT_EQ(7, any.get<uint8_t>());
222
    // check all string overloads!
223
1
    any = detail::makeAnyValue(typeInfo<CreatorBitmask>(), "4 /* no match */"sv, allocator);
224
1
    ASSERT_EQ(4, any.get<uint8_t>());
225
1
    any = detail::makeAnyValue(typeInfo<CreatorBitmask>(), "4 /* no match */", allocator);
226
1
    ASSERT_EQ(4, any.get<uint8_t>());
227
1
    any = detail::makeAnyValue(typeInfo<CreatorBitmask>(), String("4 /* no match */", allocator), allocator);
228
1
    ASSERT_EQ(4, any.get<uint8_t>());
229
1
    const String bitmaskString("4 /* no match */", allocator);
230
1
    any = detail::makeAnyValue(typeInfo<CreatorBitmask>(), bitmaskString, allocator);
231
1
    ASSERT_EQ(4, any.get<uint8_t>());
232
    // out-of-range uint64_t
233
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorBitmask>(), "99999999999999999999"sv, allocator),
234
1
            CppRuntimeException);
235
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorBitmask>(), "@WRONG"sv, allocator), CppRuntimeException);
236
1
    ASSERT_THROW(
237
1
            detail::makeAnyValue(typeInfo<CreatorBitmask>(), "_UNKNOWN"sv, allocator), CppRuntimeException);
238
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorBitmask>(), "unknown"sv, allocator), CppRuntimeException);
239
1
    ASSERT_THROW(
240
1
            detail::makeAnyValue(typeInfo<CreatorBitmask>(), "{unknown}"sv, allocator), CppRuntimeException);
241
1
    ASSERT_THROW(detail::makeAnyValue(typeInfo<CreatorBitmask>(), ""sv, allocator), CppRuntimeException);
242
1
}
243
244
TEST(ZserioTreeCreatorTest, parseBitmaskStringValue)
245
1
{
246
1
    static const ::std::array<ItemInfo, 3> values = {ItemInfo{"READ"sv, static_cast<uint64_t>(1), false, false},
247
1
            ItemInfo{"READ_EXT"sv, static_cast<uint64_t>(2), false, false},
248
1
            ItemInfo{"READ_EXT_EXT"sv, static_cast<uint64_t>(4), false, false}};
249
250
1
    static const detail::BitmaskTypeInfo<std::allocator<uint8_t>> bitmaskTypeInfo = {
251
1
            "Bitmask"sv, typeInfo<UInt8>(), values};
252
253
1
    const std::allocator<uint8_t> allocator;
254
1
    ASSERT_EQ(1, detail::parseBitmaskStringValue("READ"sv, bitmaskTypeInfo, allocator).get<uint8_t>());
255
1
    ASSERT_EQ(2, detail::parseBitmaskStringValue("READ_EXT"sv, bitmaskTypeInfo, allocator).get<uint8_t>());
256
1
    ASSERT_EQ(
257
1
            3, detail::parseBitmaskStringValue("READ_EXT | READ"sv, bitmaskTypeInfo, allocator).get<uint8_t>());
258
1
    ASSERT_EQ(6,
259
1
            detail::parseBitmaskStringValue("READ_EXT_EXT | READ_EXT"sv, bitmaskTypeInfo, allocator)
260
1
                    .get<uint8_t>());
261
1
    ASSERT_EQ(4,
262
1
            detail::parseBitmaskStringValue("READ_EXT_EXT | READ_EXT_EXT"sv, bitmaskTypeInfo, allocator)
263
1
                    .get<uint8_t>());
264
1
    ASSERT_EQ(7,
265
1
            detail::parseBitmaskStringValue("READ | READ_EXT | READ_EXT_EXT"sv, bitmaskTypeInfo, allocator)
266
1
                    .get<uint8_t>());
267
1
    ASSERT_FALSE(detail::parseBitmaskStringValue("READ|"sv, bitmaskTypeInfo, allocator).hasValue());
268
1
    ASSERT_FALSE(detail::parseBitmaskStringValue("READ | "sv, bitmaskTypeInfo, allocator).hasValue());
269
1
    ASSERT_FALSE(detail::parseBitmaskStringValue("READ|"sv, bitmaskTypeInfo, allocator).hasValue());
270
1
    ASSERT_FALSE(detail::parseBitmaskStringValue("READ_EXT | "sv, bitmaskTypeInfo, allocator).hasValue());
271
1
    ASSERT_FALSE(detail::parseBitmaskStringValue("READ_EXTABC"sv, bitmaskTypeInfo, allocator).hasValue());
272
1
}
273
274
TEST(ZserioTreeCreatorTest, createObject)
275
1
{
276
1
    ZserioTreeCreator creator(typeInfo<CreatorObject>());
277
1
    creator.beginRoot();
278
1
    IReflectableDataPtr reflectable = creator.endRoot();
279
1
    ASSERT_TRUE(reflectable);
280
1
    ASSERT_EQ(CppType::STRUCT, reflectable->getTypeInfo().getCppType());
281
1
}
282
283
TEST(ZserioTreeCreatorTest, createObjectSetFields)
284
1
{
285
1
    ZserioTreeCreator creator(typeInfo<CreatorObject>());
286
1
    creator.beginRoot();
287
1
    creator.setValue("value", 13);
288
1
    creator.setValue("text", "test");
289
1
    IReflectableDataPtr reflectable = creator.endRoot();
290
1
    ASSERT_TRUE(reflectable);
291
292
1
    ASSERT_EQ(13, reflectable->getField("value")->getUInt32());
293
1
    ASSERT_EQ("test"sv, reflectable->getField("text")->getStringView());
294
1
}
295
296
TEST(ZserioTreeCreatorTest, createObjectResetFields)
297
1
{
298
1
    ZserioTreeCreator creator(typeInfo<CreatorObject>());
299
1
    creator.beginRoot();
300
1
    creator.setValue("value", 13);
301
1
    creator.setValue("text", nullptr);
302
1
    creator.setValue("text", "test");
303
1
    IReflectableDataPtr reflectable = creator.endRoot();
304
1
    ASSERT_TRUE(reflectable);
305
306
1
    ASSERT_EQ(13, reflectable->getField("value")->getUInt32());
307
1
    ASSERT_EQ("test"sv, reflectable->getField("text")->getStringView());
308
1
}
309
310
TEST(ZserioTreeCreatorTest, createObjectFull)
311
1
{
312
1
    ZserioTreeCreator creator(typeInfo<CreatorObject>());
313
1
    creator.beginRoot();
314
1
    creator.setValue("value", 13);
315
1
    creator.setValue("text", String("test"));
316
1
    creator.beginCompound("nested");
317
1
    creator.setValue("value", 10);
318
1
    creator.setValue("text", "nested"sv);
319
1
    creator.setValue("externData", BitBuffer({0x3C}, 6));
320
1
    creator.setValue("bytesData", Vector<uint8_t>({0xFF}));
321
1
    creator.setValue("creatorEnum", CreatorEnum::ONE);
322
1
    creator.setValue("creatorBitmask", CreatorBitmask(CreatorBitmask::Values::WRITE));
323
1
    creator.endCompound();
324
1
    creator.beginArray("nestedArray");
325
1
    creator.beginCompoundElement();
326
1
    creator.setValue("value", 5);
327
1
    const String nestedArrayText = "nestedArray";
328
1
    creator.setValue("text", nestedArrayText);
329
1
    creator.setValue("creatorEnum", "MinusOne");
330
1
    creator.setValue("creatorBitmask", CreatorBitmask(CreatorBitmask::Values::READ));
331
1
    creator.endCompoundElement();
332
1
    creator.endArray();
333
1
    creator.beginArray("textArray");
334
1
    creator.addValueElement("this");
335
1
    creator.addValueElement("is");
336
1
    creator.addValueElement("text"sv);
337
1
    creator.addValueElement("array");
338
1
    creator.endArray();
339
1
    creator.beginArray("externArray");
340
1
    creator.addValueElement(BitBuffer({0x0F}, 4));
341
1
    creator.endArray();
342
1
    creator.beginArray("bytesArray");
343
1
    creator.addValueElement(Vector<uint8_t>({0xCA, 0xFE}));
344
1
    creator.endArray();
345
1
    creator.setValue("optionalBool", false);
346
1
    creator.beginCompound("optionalNested");
347
1
    creator.setValue("text", "optionalNested");
348
1
    creator.endCompound();
349
1
    IReflectableDataPtr reflectable = creator.endRoot();
350
1
    ASSERT_TRUE(reflectable);
351
352
1
    ASSERT_EQ(13, reflectable->getField("value")->getUInt32());
353
1
    ASSERT_EQ("test"sv, reflectable->getField("text")->getStringView());
354
1
    ASSERT_EQ(10, reflectable->find("nested.value")->getUInt32());
355
1
    ASSERT_EQ("nested"sv, reflectable->find("nested.text")->getStringView());
356
1
    ASSERT_EQ(0x3C, reflectable->find("nested.externData")->getBitBuffer().getData()[0]);
357
1
    ASSERT_EQ(6, reflectable->find("nested.externData")->getBitBuffer().getBitSize());
358
1
    ASSERT_EQ(1, reflectable->find("nested.bytesData")->getBytes().size());
359
1
    ASSERT_EQ(0xFF, reflectable->find("nested.bytesData")->getBytes()[0]);
360
1
    ASSERT_EQ(enumToValue(CreatorEnum::ONE), reflectable->find("nested.creatorEnum")->getInt8());
361
1
    ASSERT_EQ(CreatorBitmask::Values::WRITE,
362
1
            CreatorBitmask(reflectable->find("nested.creatorBitmask")->getUInt8()));
363
1
    ASSERT_EQ(1, reflectable->getField("nestedArray")->size());
364
1
    ASSERT_EQ(5, reflectable->getField("nestedArray")->at(0)->getField("value")->getUInt32());
365
1
    ASSERT_EQ("nestedArray"sv, reflectable->getField("nestedArray")->at(0)->getField("text")->getStringView());
366
1
    ASSERT_EQ(enumToValue(CreatorEnum::MinusOne),
367
1
            reflectable->getField("nestedArray")->at(0)->getField("creatorEnum")->getInt8());
368
1
    ASSERT_EQ(CreatorBitmask::Values::READ,
369
1
            CreatorBitmask(
370
1
                    reflectable->getField("nestedArray")->at(0)->getField("creatorBitmask")->getUInt8()));
371
1
    ASSERT_EQ(4, reflectable->getField("textArray")->size());
372
1
    ASSERT_EQ("this"sv, reflectable->getField("textArray")->at(0)->getStringView());
373
1
    ASSERT_EQ("is"sv, reflectable->getField("textArray")->at(1)->getStringView());
374
1
    ASSERT_EQ("text"sv, reflectable->getField("textArray")->at(2)->getStringView());
375
1
    ASSERT_EQ("array"sv, reflectable->getField("textArray")->at(3)->getStringView());
376
1
    ASSERT_EQ(1, reflectable->getField("externArray")->size());
377
1
    ASSERT_EQ(0x0f, reflectable->getField("externArray")->at(0)->getBitBuffer().getData()[0]);
378
1
    ASSERT_EQ(4, reflectable->getField("externArray")->at(0)->getBitBuffer().getBitSize());
379
1
    ASSERT_EQ(1, reflectable->getField("bytesArray")->size());
380
1
    ASSERT_EQ(2, reflectable->getField("bytesArray")->at(0)->getBytes().size());
381
1
    ASSERT_EQ(0xCA, reflectable->getField("bytesArray")->at(0)->getBytes()[0]);
382
1
    ASSERT_EQ(0xFE, reflectable->getField("bytesArray")->at(0)->getBytes()[1]);
383
1
    ASSERT_EQ(false, reflectable->getField("optionalBool")->getBool());
384
1
    ASSERT_TRUE(reflectable->getField("optionalNested"));
385
1
    ASSERT_EQ("optionalNested"sv, reflectable->find("optionalNested.text")->getStringView());
386
1
}
387
388
TEST(ZserioTreeCreator, exceptionsBeforeRoot)
389
1
{
390
1
    ZserioTreeCreator creator(typeInfo<CreatorObject>());
391
392
1
    ASSERT_THROW(creator.endRoot(), CppRuntimeException);
393
1
    ASSERT_THROW(creator.beginArray("nestedArray"), CppRuntimeException);
394
1
    ASSERT_THROW(creator.endArray(), CppRuntimeException);
395
1
    ASSERT_THROW(creator.getFieldType("nested"), CppRuntimeException);
396
1
    ASSERT_THROW(creator.beginCompound("nested"), CppRuntimeException);
397
1
    ASSERT_THROW(creator.endCompound(), CppRuntimeException);
398
1
    ASSERT_THROW(creator.setValue("value", 13), CppRuntimeException);
399
1
    ASSERT_THROW(creator.setValue("nonexistent", nullptr), CppRuntimeException);
400
1
    ASSERT_THROW(creator.getElementType(), CppRuntimeException);
401
1
    ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException);
402
1
    ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException);
403
1
    ASSERT_THROW(creator.addValueElement(13), CppRuntimeException);
404
1
}
405
406
TEST(ZserioTreeCreator, exceptionsInRoot)
407
1
{
408
1
    ZserioTreeCreator creator(typeInfo<CreatorObject>());
409
1
    creator.beginRoot();
410
411
1
    ASSERT_THROW(creator.beginRoot(), CppRuntimeException);
412
1
    ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException);
413
1
    ASSERT_THROW(creator.beginArray("nested"), CppRuntimeException); // not an array
414
1
    ASSERT_THROW(creator.endArray(), CppRuntimeException);
415
1
    ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException);
416
1
    ASSERT_THROW(creator.beginCompound("nestedArray"), CppRuntimeException); // is array
417
1
    ASSERT_THROW(creator.endCompound(), CppRuntimeException);
418
1
    ASSERT_THROW(creator.setValue("nonexistent", 13), CppRuntimeException);
419
1
    ASSERT_THROW(creator.setValue("nestedArray", 13), CppRuntimeException); // is array
420
1
    ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException);
421
1
    ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException);
422
1
    ASSERT_THROW(creator.addValueElement(13), CppRuntimeException);
423
1
}
424
425
TEST(ZserioTreeCreator, exceptionsInCompound)
426
1
{
427
1
    ZserioTreeCreator creator(typeInfo<CreatorObject>());
428
1
    creator.beginRoot();
429
1
    creator.beginCompound("nested");
430
431
1
    ASSERT_THROW(creator.beginRoot(), CppRuntimeException);
432
1
    ASSERT_THROW(creator.endRoot(), CppRuntimeException);
433
1
    ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException);
434
1
    ASSERT_THROW(creator.beginArray("value"), CppRuntimeException); // not an array
435
1
    ASSERT_THROW(creator.endArray(), CppRuntimeException);
436
1
    ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException);
437
1
    ASSERT_THROW(creator.beginCompound("text"), CppRuntimeException); // not a compound
438
1
    ASSERT_THROW(creator.setValue("nonexistent", "test"), CppRuntimeException);
439
1
    ASSERT_THROW(creator.setValue("value", "test"), CppRuntimeException); // wrong type
440
1
    ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException);
441
1
    ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException);
442
1
    ASSERT_THROW(creator.addValueElement(13), CppRuntimeException);
443
1
}
444
445
TEST(ZserioTreeCreator, exceptionsInCompoundArray)
446
1
{
447
1
    ZserioTreeCreator creator(typeInfo<CreatorObject>());
448
1
    creator.beginRoot();
449
1
    creator.beginArray("nestedArray");
450
451
1
    ASSERT_THROW(creator.beginRoot(), CppRuntimeException);
452
1
    ASSERT_THROW(creator.endRoot(), CppRuntimeException);
453
1
    ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException);
454
1
    ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException);
455
1
    ASSERT_THROW(creator.endCompound(), CppRuntimeException);
456
1
    ASSERT_THROW(creator.setValue("nonexistent", 13), CppRuntimeException);
457
1
    ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException);
458
1
    ASSERT_THROW(creator.addValueElement(13), CppRuntimeException);
459
1
}
460
461
TEST(ZserioTreeCreator, exceptionsInSimpleArray)
462
1
{
463
1
    ZserioTreeCreator creator(typeInfo<CreatorObject>());
464
1
    creator.beginRoot();
465
1
    creator.beginArray("textArray");
466
467
1
    ASSERT_THROW(creator.beginRoot(), CppRuntimeException);
468
1
    ASSERT_THROW(creator.endRoot(), CppRuntimeException);
469
1
    ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException);
470
1
    ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException);
471
1
    ASSERT_THROW(creator.endCompound(), CppRuntimeException);
472
1
    ASSERT_THROW(creator.setValue("nonexistent", 13), CppRuntimeException);
473
1
    ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException); // not a compound array
474
1
    ASSERT_THROW(creator.endCompoundElement(), CppRuntimeException);
475
1
    ASSERT_THROW(creator.addValueElement(13), CppRuntimeException); // wrong type
476
1
}
477
478
TEST(ZserioTreeCreator, exceptionsInCompoundElement)
479
1
{
480
1
    ZserioTreeCreator creator(typeInfo<CreatorObject>());
481
1
    creator.beginRoot();
482
1
    creator.beginArray("nestedArray");
483
1
    creator.beginCompoundElement();
484
485
1
    ASSERT_THROW(creator.beginRoot(), CppRuntimeException);
486
1
    ASSERT_THROW(creator.endRoot(), CppRuntimeException);
487
1
    ASSERT_THROW(creator.beginArray("nonexistent"), CppRuntimeException);
488
1
    ASSERT_THROW(creator.endArray(), CppRuntimeException);
489
1
    ASSERT_THROW(creator.beginCompound("nonexistent"), CppRuntimeException);
490
1
    ASSERT_THROW(creator.endCompound(), CppRuntimeException);
491
1
    ASSERT_THROW(creator.setValue("nonexistent", 13), CppRuntimeException);
492
1
    ASSERT_THROW(creator.beginCompoundElement(), CppRuntimeException);
493
1
    ASSERT_THROW(creator.addValueElement(13), CppRuntimeException);
494
1
}
495
496
} // namespace zserio