Coverage Report

Created: 2023-12-13 14:58

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