blob: 4d9e0762bc14b61b531e4c6a32e8fea247f287f2 [file] [log] [blame]
Austin Schuh2dd86a92022-09-14 21:19:23 -07001#include "parser_test.h"
2
3#include <cmath>
James Kuszmaul3b15b0c2022-11-08 14:03:16 -08004#include <limits>
Austin Schuh2dd86a92022-09-14 21:19:23 -07005#include <string>
6
7#include "flatbuffers/idl.h"
8#include "test_assert.h"
9
10namespace flatbuffers {
11namespace tests {
12namespace {
13
14// Shortcuts for the infinity.
15static const auto infinity_f = std::numeric_limits<float>::infinity();
16static const auto infinity_d = std::numeric_limits<double>::infinity();
17
18// Test that parser errors are actually generated.
19static void TestError_(const char *src, const char *error_substr, bool strict_json,
20 const char *file, int line, const char *func) {
21 flatbuffers::IDLOptions opts;
22 opts.strict_json = strict_json;
23 flatbuffers::Parser parser(opts);
24 if (parser.Parse(src)) {
25 TestFail("true", "false",
26 ("parser.Parse(\"" + std::string(src) + "\")").c_str(), file, line,
27 func);
28 } else if (!strstr(parser.error_.c_str(), error_substr)) {
29 TestFail(error_substr, parser.error_.c_str(),
30 ("parser.Parse(\"" + std::string(src) + "\")").c_str(), file, line,
31 func);
32 }
33}
34
35static void TestError_(const char *src, const char *error_substr, const char *file,
36 int line, const char *func) {
37 TestError_(src, error_substr, false, file, line, func);
38}
39
40#ifdef _WIN32
41# define TestError(src, ...) \
42 TestError_(src, __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__)
43#else
44# define TestError(src, ...) \
45 TestError_(src, __VA_ARGS__, __FILE__, __LINE__, __PRETTY_FUNCTION__)
46#endif
47
48static bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; }
49
50} // namespace
51
52// Test that parsing errors occur as we'd expect.
53// Also useful for coverage, making sure these paths are run.
54void ErrorTest() {
55 // In order they appear in idl_parser.cpp
56 TestError("table X { Y:byte; } root_type X; { Y: 999 }", "does not fit");
57 TestError("\"\0", "illegal");
58 TestError("\"\\q", "escape code");
59 TestError("table ///", "documentation");
60 TestError("@", "illegal");
61 TestError("table 1", "expecting");
62 TestError("table X { Y:[[int]]; }", "nested vector");
63 TestError("table X { Y:1; }", "illegal type");
64 TestError("table X { Y:int; Y:int; }", "field already");
65 TestError("table Y {} table X { Y:int; }", "same as table");
66 TestError("struct X { Y:string; }", "only scalar");
67 TestError("struct X { a:uint = 42; }", "default values");
68 TestError("enum Y:byte { Z = 1 } table X { y:Y; }", "not part of enum");
69 TestError("struct X { Y:int (deprecated); }", "deprecate");
70 TestError("union Z { X } table X { Y:Z; } root_type X; { Y: {}, A:1 }",
71 "missing type field");
72 TestError("union Z { X } table X { Y:Z; } root_type X; { Y_type: 99, Y: {",
73 "type id");
74 TestError("table X { Y:int; } root_type X; { Z:", "unknown field");
75 TestError("table X { Y:int; } root_type X; { Y:", "string constant", true);
76 TestError("table X { Y:int; } root_type X; { \"Y\":1, }", "string constant",
77 true);
78 TestError(
79 "struct X { Y:int; Z:int; } table W { V:X; } root_type W; "
80 "{ V:{ Y:1 } }",
81 "wrong number");
82 TestError("enum E:byte { A } table X { Y:E; } root_type X; { Y:U }",
83 "unknown enum value");
84 TestError("table X { Y:byte; } root_type X; { Y:; }", "starting");
85 TestError("enum X:byte { Y } enum X {", "enum already");
86 TestError("enum X:float {}", "underlying");
87 TestError("enum X:byte { Y, Y }", "value already");
88 TestError("enum X:byte { Y=2, Z=2 }", "unique");
89 TestError("enum X:byte (force_align: 4) { Y }", "force_align");
90 TestError("table X { Y:int; } table X {", "datatype already");
91 TestError("table X { } union X { }", "datatype already");
92 TestError("union X { } table X { }", "datatype already");
93 TestError("namespace A; table X { } namespace A; union X { }",
94 "datatype already");
95 TestError("namespace A; union X { } namespace A; table X { }",
96 "datatype already");
97 TestError("struct X (force_align: 7) { Y:int; }", "force_align");
98 TestError("struct X {}", "size 0");
99 TestError("{}", "no root");
100 TestError("table X { Y:byte; } root_type X; { Y:1 } { Y:1 }", "end of file");
101 TestError("table X { Y:byte; } root_type X; { Y:1 } table Y{ Z:int }",
102 "end of file");
103 TestError("root_type X;", "unknown root");
104 TestError("struct X { Y:int; } root_type X;", "a table");
105 TestError("union X { Y }", "referenced");
106 TestError("union Z { X } struct X { Y:int; }", "only tables");
107 TestError("table X { Y:[int]; YLength:int; }", "clash");
108 TestError("table X { Y:byte; } root_type X; { Y:1, Y:2 }", "more than once");
109 // float to integer conversion is forbidden
110 TestError("table X { Y:int; } root_type X; { Y:1.0 }", "float");
111 TestError("table X { Y:bool; } root_type X; { Y:1.0 }", "float");
112 TestError("enum X:bool { Y = true }", "must be integral");
113 // Array of non-scalar
114 TestError("table X { x:int; } struct Y { y:[X:2]; }",
115 "may contain only scalar or struct fields");
116 // Non-snake case field names
117 TestError("table X { Y: int; } root_type Y: {Y:1.0}", "snake_case");
118 // Complex defaults
119 TestError("table X { y: string = 1; }", "expecting: string");
120 TestError("table X { y: string = []; }", " Cannot assign token");
121 TestError("table X { y: [int] = [1]; }", "Expected `]`");
122 TestError("table X { y: [int] = [; }", "Expected `]`");
123 TestError("table X { y: [int] = \"\"; }", "type mismatch");
124 // An identifier can't start from sign (+|-)
125 TestError("table X { -Y: int; } root_type Y: {Y:1.0}", "identifier");
126 TestError("table X { +Y: int; } root_type Y: {Y:1.0}", "identifier");
127}
128
129void EnumOutOfRangeTest() {
130 TestError("enum X:byte { Y = 128 }", "enum value does not fit");
131 TestError("enum X:byte { Y = -129 }", "enum value does not fit");
132 TestError("enum X:byte { Y = 126, Z0, Z1 }", "enum value does not fit");
133 TestError("enum X:ubyte { Y = -1 }", "enum value does not fit");
134 TestError("enum X:ubyte { Y = 256 }", "enum value does not fit");
135 TestError("enum X:ubyte { Y = 255, Z }", "enum value does not fit");
136 TestError("table Y{} union X { Y = -1 }", "enum value does not fit");
137 TestError("table Y{} union X { Y = 256 }", "enum value does not fit");
138 TestError("table Y{} union X { Y = 255, Z:Y }", "enum value does not fit");
139 TestError("enum X:int { Y = -2147483649 }", "enum value does not fit");
140 TestError("enum X:int { Y = 2147483648 }", "enum value does not fit");
141 TestError("enum X:uint { Y = -1 }", "enum value does not fit");
142 TestError("enum X:uint { Y = 4294967297 }", "enum value does not fit");
143 TestError("enum X:long { Y = 9223372036854775808 }", "does not fit");
144 TestError("enum X:long { Y = 9223372036854775807, Z }",
145 "enum value does not fit");
146 TestError("enum X:ulong { Y = -1 }", "does not fit");
147 TestError("enum X:ubyte (bit_flags) { Y=8 }", "bit flag out");
148 TestError("enum X:byte (bit_flags) { Y=7 }", "must be unsigned"); // -128
149 // bit_flgs out of range
150 TestError("enum X:ubyte (bit_flags) { Y0,Y1,Y2,Y3,Y4,Y5,Y6,Y7,Y8 }",
151 "out of range");
152}
153
154void IntegerOutOfRangeTest() {
155 TestError("table T { F:byte; } root_type T; { F:128 }",
156 "constant does not fit");
157 TestError("table T { F:byte; } root_type T; { F:-129 }",
158 "constant does not fit");
159 TestError("table T { F:ubyte; } root_type T; { F:256 }",
160 "constant does not fit");
161 TestError("table T { F:ubyte; } root_type T; { F:-1 }",
162 "constant does not fit");
163 TestError("table T { F:short; } root_type T; { F:32768 }",
164 "constant does not fit");
165 TestError("table T { F:short; } root_type T; { F:-32769 }",
166 "constant does not fit");
167 TestError("table T { F:ushort; } root_type T; { F:65536 }",
168 "constant does not fit");
169 TestError("table T { F:ushort; } root_type T; { F:-1 }",
170 "constant does not fit");
171 TestError("table T { F:int; } root_type T; { F:2147483648 }",
172 "constant does not fit");
173 TestError("table T { F:int; } root_type T; { F:-2147483649 }",
174 "constant does not fit");
175 TestError("table T { F:uint; } root_type T; { F:4294967296 }",
176 "constant does not fit");
177 TestError("table T { F:uint; } root_type T; { F:-1 }",
178 "constant does not fit");
179 // Check fixed width aliases
180 TestError("table X { Y:uint8; } root_type X; { Y: -1 }", "does not fit");
181 TestError("table X { Y:uint8; } root_type X; { Y: 256 }", "does not fit");
182 TestError("table X { Y:uint16; } root_type X; { Y: -1 }", "does not fit");
183 TestError("table X { Y:uint16; } root_type X; { Y: 65536 }", "does not fit");
184 TestError("table X { Y:uint32; } root_type X; { Y: -1 }", "");
185 TestError("table X { Y:uint32; } root_type X; { Y: 4294967296 }",
186 "does not fit");
187 TestError("table X { Y:uint64; } root_type X; { Y: -1 }", "");
188 TestError("table X { Y:uint64; } root_type X; { Y: -9223372036854775809 }",
189 "does not fit");
190 TestError("table X { Y:uint64; } root_type X; { Y: 18446744073709551616 }",
191 "does not fit");
192
193 TestError("table X { Y:int8; } root_type X; { Y: -129 }", "does not fit");
194 TestError("table X { Y:int8; } root_type X; { Y: 128 }", "does not fit");
195 TestError("table X { Y:int16; } root_type X; { Y: -32769 }", "does not fit");
196 TestError("table X { Y:int16; } root_type X; { Y: 32768 }", "does not fit");
197 TestError("table X { Y:int32; } root_type X; { Y: -2147483649 }", "");
198 TestError("table X { Y:int32; } root_type X; { Y: 2147483648 }",
199 "does not fit");
200 TestError("table X { Y:int64; } root_type X; { Y: -9223372036854775809 }",
201 "does not fit");
202 TestError("table X { Y:int64; } root_type X; { Y: 9223372036854775808 }",
203 "does not fit");
204 // check out-of-int64 as int8
205 TestError("table X { Y:int8; } root_type X; { Y: -9223372036854775809 }",
206 "does not fit");
207 TestError("table X { Y:int8; } root_type X; { Y: 9223372036854775808 }",
208 "does not fit");
209
210 // Check default values
211 TestError("table X { Y:int64=-9223372036854775809; } root_type X; {}",
212 "does not fit");
213 TestError("table X { Y:int64= 9223372036854775808; } root_type X; {}",
214 "does not fit");
215 TestError("table X { Y:uint64; } root_type X; { Y: -1 }", "");
216 TestError("table X { Y:uint64=-9223372036854775809; } root_type X; {}",
217 "does not fit");
218 TestError("table X { Y:uint64= 18446744073709551616; } root_type X; {}",
219 "does not fit");
220}
221
222void InvalidFloatTest() {
223 auto invalid_msg = "invalid number";
224 auto comma_msg = "expecting: ,";
225 TestError("table T { F:float; } root_type T; { F:1,0 }", "");
226 TestError("table T { F:float; } root_type T; { F:. }", "");
227 TestError("table T { F:float; } root_type T; { F:- }", invalid_msg);
228 TestError("table T { F:float; } root_type T; { F:+ }", invalid_msg);
229 TestError("table T { F:float; } root_type T; { F:-. }", invalid_msg);
230 TestError("table T { F:float; } root_type T; { F:+. }", invalid_msg);
231 TestError("table T { F:float; } root_type T; { F:.e }", "");
232 TestError("table T { F:float; } root_type T; { F:-e }", invalid_msg);
233 TestError("table T { F:float; } root_type T; { F:+e }", invalid_msg);
234 TestError("table T { F:float; } root_type T; { F:-.e }", invalid_msg);
235 TestError("table T { F:float; } root_type T; { F:+.e }", invalid_msg);
236 TestError("table T { F:float; } root_type T; { F:-e1 }", invalid_msg);
237 TestError("table T { F:float; } root_type T; { F:+e1 }", invalid_msg);
238 TestError("table T { F:float; } root_type T; { F:1.0e+ }", invalid_msg);
239 TestError("table T { F:float; } root_type T; { F:1.0e- }", invalid_msg);
240 // exponent pP is mandatory for hex-float
241 TestError("table T { F:float; } root_type T; { F:0x0 }", invalid_msg);
242 TestError("table T { F:float; } root_type T; { F:-0x. }", invalid_msg);
243 TestError("table T { F:float; } root_type T; { F:0x. }", invalid_msg);
244 TestError("table T { F:float; } root_type T; { F:0Xe }", invalid_msg);
245 TestError("table T { F:float; } root_type T; { F:\"0Xe\" }", invalid_msg);
246 TestError("table T { F:float; } root_type T; { F:\"nan(1)\" }", invalid_msg);
247 // eE not exponent in hex-float!
248 TestError("table T { F:float; } root_type T; { F:0x0.0e+ }", invalid_msg);
249 TestError("table T { F:float; } root_type T; { F:0x0.0e- }", invalid_msg);
250 TestError("table T { F:float; } root_type T; { F:0x0.0p }", invalid_msg);
251 TestError("table T { F:float; } root_type T; { F:0x0.0p+ }", invalid_msg);
252 TestError("table T { F:float; } root_type T; { F:0x0.0p- }", invalid_msg);
253 TestError("table T { F:float; } root_type T; { F:0x0.0pa1 }", invalid_msg);
254 TestError("table T { F:float; } root_type T; { F:0x0.0e+ }", invalid_msg);
255 TestError("table T { F:float; } root_type T; { F:0x0.0e- }", invalid_msg);
256 TestError("table T { F:float; } root_type T; { F:0x0.0e+0 }", invalid_msg);
257 TestError("table T { F:float; } root_type T; { F:0x0.0e-0 }", invalid_msg);
258 TestError("table T { F:float; } root_type T; { F:0x0.0ep+ }", invalid_msg);
259 TestError("table T { F:float; } root_type T; { F:0x0.0ep- }", invalid_msg);
260 TestError("table T { F:float; } root_type T; { F:1.2.3 }", invalid_msg);
261 TestError("table T { F:float; } root_type T; { F:1.2.e3 }", invalid_msg);
262 TestError("table T { F:float; } root_type T; { F:1.2e.3 }", invalid_msg);
263 TestError("table T { F:float; } root_type T; { F:1.2e0.3 }", invalid_msg);
264 TestError("table T { F:float; } root_type T; { F:1.2e3. }", invalid_msg);
265 TestError("table T { F:float; } root_type T; { F:1.2e3.0 }", invalid_msg);
266 TestError("table T { F:float; } root_type T; { F:+-1.0 }", invalid_msg);
267 TestError("table T { F:float; } root_type T; { F:1.0e+-1 }", invalid_msg);
268 TestError("table T { F:float; } root_type T; { F:\"1.0e+-1\" }", invalid_msg);
269 TestError("table T { F:float; } root_type T; { F:1.e0e }", comma_msg);
270 TestError("table T { F:float; } root_type T; { F:0x1.p0e }", comma_msg);
271 TestError("table T { F:float; } root_type T; { F:\" 0x10 \" }", invalid_msg);
272 // floats in string
273 TestError("table T { F:float; } root_type T; { F:\"1,2.\" }", invalid_msg);
274 TestError("table T { F:float; } root_type T; { F:\"1.2e3.\" }", invalid_msg);
275 TestError("table T { F:float; } root_type T; { F:\"0x1.p0e\" }", invalid_msg);
276 TestError("table T { F:float; } root_type T; { F:\"0x1.0\" }", invalid_msg);
277 TestError("table T { F:float; } root_type T; { F:\" 0x1.0\" }", invalid_msg);
278 TestError("table T { F:float; } root_type T; { F:\"+ 0\" }", invalid_msg);
279 // disable escapes for "number-in-string"
280 TestError("table T { F:float; } root_type T; { F:\"\\f1.2e3.\" }", "invalid");
281 TestError("table T { F:float; } root_type T; { F:\"\\t1.2e3.\" }", "invalid");
282 TestError("table T { F:float; } root_type T; { F:\"\\n1.2e3.\" }", "invalid");
283 TestError("table T { F:float; } root_type T; { F:\"\\r1.2e3.\" }", "invalid");
284 TestError("table T { F:float; } root_type T; { F:\"4\\x005\" }", "invalid");
285 TestError("table T { F:float; } root_type T; { F:\"\'12\'\" }", invalid_msg);
286 // null is not a number constant!
287 TestError("table T { F:float; } root_type T; { F:\"null\" }", invalid_msg);
288 TestError("table T { F:float; } root_type T; { F:null }", invalid_msg);
289}
290
291void UnicodeInvalidSurrogatesTest() {
292 TestError(
293 "table T { F:string; }"
294 "root_type T;"
295 "{ F:\"\\uD800\"}",
296 "unpaired high surrogate");
297 TestError(
298 "table T { F:string; }"
299 "root_type T;"
300 "{ F:\"\\uD800abcd\"}",
301 "unpaired high surrogate");
302 TestError(
303 "table T { F:string; }"
304 "root_type T;"
305 "{ F:\"\\uD800\\n\"}",
306 "unpaired high surrogate");
307 TestError(
308 "table T { F:string; }"
309 "root_type T;"
310 "{ F:\"\\uD800\\uD800\"}",
311 "multiple high surrogates");
312 TestError(
313 "table T { F:string; }"
314 "root_type T;"
315 "{ F:\"\\uDC00\"}",
316 "unpaired low surrogate");
317}
318
319void InvalidUTF8Test() {
320 // "1 byte" pattern, under min length of 2 bytes
321 TestError(
322 "table T { F:string; }"
323 "root_type T;"
324 "{ F:\"\x80\"}",
325 "illegal UTF-8 sequence");
326 // 2 byte pattern, string too short
327 TestError(
328 "table T { F:string; }"
329 "root_type T;"
330 "{ F:\"\xDF\"}",
331 "illegal UTF-8 sequence");
332 // 3 byte pattern, string too short
333 TestError(
334 "table T { F:string; }"
335 "root_type T;"
336 "{ F:\"\xEF\xBF\"}",
337 "illegal UTF-8 sequence");
338 // 4 byte pattern, string too short
339 TestError(
340 "table T { F:string; }"
341 "root_type T;"
342 "{ F:\"\xF7\xBF\xBF\"}",
343 "illegal UTF-8 sequence");
344 // "5 byte" pattern, string too short
345 TestError(
346 "table T { F:string; }"
347 "root_type T;"
348 "{ F:\"\xFB\xBF\xBF\xBF\"}",
349 "illegal UTF-8 sequence");
350 // "6 byte" pattern, string too short
351 TestError(
352 "table T { F:string; }"
353 "root_type T;"
354 "{ F:\"\xFD\xBF\xBF\xBF\xBF\"}",
355 "illegal UTF-8 sequence");
356 // "7 byte" pattern, string too short
357 TestError(
358 "table T { F:string; }"
359 "root_type T;"
360 "{ F:\"\xFE\xBF\xBF\xBF\xBF\xBF\"}",
361 "illegal UTF-8 sequence");
362 // "5 byte" pattern, over max length of 4 bytes
363 TestError(
364 "table T { F:string; }"
365 "root_type T;"
366 "{ F:\"\xFB\xBF\xBF\xBF\xBF\"}",
367 "illegal UTF-8 sequence");
368 // "6 byte" pattern, over max length of 4 bytes
369 TestError(
370 "table T { F:string; }"
371 "root_type T;"
372 "{ F:\"\xFD\xBF\xBF\xBF\xBF\xBF\"}",
373 "illegal UTF-8 sequence");
374 // "7 byte" pattern, over max length of 4 bytes
375 TestError(
376 "table T { F:string; }"
377 "root_type T;"
378 "{ F:\"\xFE\xBF\xBF\xBF\xBF\xBF\xBF\"}",
379 "illegal UTF-8 sequence");
380
381 // Three invalid encodings for U+000A (\n, aka NEWLINE)
382 TestError(
383 "table T { F:string; }"
384 "root_type T;"
385 "{ F:\"\xC0\x8A\"}",
386 "illegal UTF-8 sequence");
387 TestError(
388 "table T { F:string; }"
389 "root_type T;"
390 "{ F:\"\xE0\x80\x8A\"}",
391 "illegal UTF-8 sequence");
392 TestError(
393 "table T { F:string; }"
394 "root_type T;"
395 "{ F:\"\xF0\x80\x80\x8A\"}",
396 "illegal UTF-8 sequence");
397
398 // Two invalid encodings for U+00A9 (COPYRIGHT SYMBOL)
399 TestError(
400 "table T { F:string; }"
401 "root_type T;"
402 "{ F:\"\xE0\x81\xA9\"}",
403 "illegal UTF-8 sequence");
404 TestError(
405 "table T { F:string; }"
406 "root_type T;"
407 "{ F:\"\xF0\x80\x81\xA9\"}",
408 "illegal UTF-8 sequence");
409
410 // Invalid encoding for U+20AC (EURO SYMBOL)
411 TestError(
412 "table T { F:string; }"
413 "root_type T;"
414 "{ F:\"\xF0\x82\x82\xAC\"}",
415 "illegal UTF-8 sequence");
416
417 // UTF-16 surrogate values between U+D800 and U+DFFF cannot be encoded in
418 // UTF-8
419 TestError(
420 "table T { F:string; }"
421 "root_type T;"
422 // U+10400 "encoded" as U+D801 U+DC00
423 "{ F:\"\xED\xA0\x81\xED\xB0\x80\"}",
424 "illegal UTF-8 sequence");
425
426 // Check independence of identifier from locale.
427 std::string locale_ident;
428 locale_ident += "table T { F";
429 locale_ident += static_cast<char>(-32); // unsigned 0xE0
430 locale_ident += " :string; }";
431 locale_ident += "root_type T;";
432 locale_ident += "{}";
433 TestError(locale_ident.c_str(), "");
434}
435
436template<typename T>
437T TestValue(const char *json, const char *type_name,
438 const char *decls = nullptr) {
439 flatbuffers::Parser parser;
440 parser.builder_.ForceDefaults(true); // return defaults
441 auto check_default = json ? false : true;
442 if (check_default) { parser.opts.output_default_scalars_in_json = true; }
443 // Simple schema.
444 std::string schema = std::string(decls ? decls : "") + "\n" +
445 "table X { y:" + std::string(type_name) +
446 "; } root_type X;";
447 auto schema_done = parser.Parse(schema.c_str());
448 TEST_EQ_STR(parser.error_.c_str(), "");
449 TEST_EQ(schema_done, true);
450
451 auto done = parser.Parse(check_default ? "{}" : json);
452 TEST_EQ_STR(parser.error_.c_str(), "");
453 TEST_EQ(done, true);
454
455 // Check with print.
456 std::string print_back;
457 parser.opts.indent_step = -1;
458 TEST_EQ(GenerateText(parser, parser.builder_.GetBufferPointer(), &print_back),
459 true);
460 // restore value from its default
461 if (check_default) { TEST_EQ(parser.Parse(print_back.c_str()), true); }
462
463 auto root = flatbuffers::GetRoot<flatbuffers::Table>(
464 parser.builder_.GetBufferPointer());
465 return root->GetField<T>(flatbuffers::FieldIndexToOffset(0), 0);
466}
467
468// Additional parser testing not covered elsewhere.
469void ValueTest() {
470 // Test scientific notation numbers.
471 TEST_EQ(
472 FloatCompare(TestValue<float>("{ y:0.0314159e+2 }", "float"), 3.14159f),
473 true);
474 // number in string
475 TEST_EQ(FloatCompare(TestValue<float>("{ y:\"0.0314159e+2\" }", "float"),
476 3.14159f),
477 true);
478
479 // Test conversion functions.
480 TEST_EQ(FloatCompare(TestValue<float>("{ y:cos(rad(180)) }", "float"), -1),
481 true);
482
483 // int embedded to string
484 TEST_EQ(TestValue<int>("{ y:\"-876\" }", "int=-123"), -876);
485 TEST_EQ(TestValue<int>("{ y:\"876\" }", "int=-123"), 876);
486
487 // Test negative hex constant.
488 TEST_EQ(TestValue<int>("{ y:-0x8ea0 }", "int=-0x8ea0"), -36512);
489 TEST_EQ(TestValue<int>(nullptr, "int=-0x8ea0"), -36512);
490
491 // positive hex constant
492 TEST_EQ(TestValue<int>("{ y:0x1abcdef }", "int=0x1"), 0x1abcdef);
493 // with optional '+' sign
494 TEST_EQ(TestValue<int>("{ y:+0x1abcdef }", "int=+0x1"), 0x1abcdef);
495 // hex in string
496 TEST_EQ(TestValue<int>("{ y:\"0x1abcdef\" }", "int=+0x1"), 0x1abcdef);
497
498 // Make sure we do unsigned 64bit correctly.
499 TEST_EQ(TestValue<uint64_t>("{ y:12335089644688340133 }", "ulong"),
500 12335089644688340133ULL);
501
502 // bool in string
503 TEST_EQ(TestValue<bool>("{ y:\"false\" }", "bool=true"), false);
504 TEST_EQ(TestValue<bool>("{ y:\"true\" }", "bool=\"true\""), true);
505 TEST_EQ(TestValue<bool>("{ y:'false' }", "bool=true"), false);
506 TEST_EQ(TestValue<bool>("{ y:'true' }", "bool=\"true\""), true);
507
508 // check comments before and after json object
509 TEST_EQ(TestValue<int>("/*before*/ { y:1 } /*after*/", "int"), 1);
510 TEST_EQ(TestValue<int>("//before \n { y:1 } //after", "int"), 1);
511}
512
513void NestedListTest() {
514 flatbuffers::Parser parser1;
515 TEST_EQ(parser1.Parse("struct Test { a:short; b:byte; } table T { F:[Test]; }"
516 "root_type T;"
517 "{ F:[ [10,20], [30,40]] }"),
518 true);
519}
520
521void EnumStringsTest() {
522 flatbuffers::Parser parser1;
523 TEST_EQ(parser1.Parse("enum E:byte { A, B, C } table T { F:[E]; }"
524 "root_type T;"
525 "{ F:[ A, B, \"C\", \"A B C\" ] }"),
526 true);
527 flatbuffers::Parser parser2;
528 TEST_EQ(parser2.Parse("enum E:byte { A, B, C } table T { F:[int]; }"
529 "root_type T;"
530 "{ F:[ \"E.C\", \"E.A E.B E.C\" ] }"),
531 true);
532 // unsigned bit_flags
533 flatbuffers::Parser parser3;
534 TEST_EQ(
535 parser3.Parse("enum E:uint16 (bit_flags) { F0, F07=7, F08, F14=14, F15 }"
536 " table T { F: E = \"F15 F08\"; }"
537 "root_type T;"),
538 true);
539}
540
541void EnumValueTest() {
542 // json: "{ Y:0 }", schema: table X { y: "E"}
543 // 0 in enum (V=0) E then Y=0 is valid.
544 TEST_EQ(TestValue<int>("{ y:0 }", "E", "enum E:int { V }"), 0);
545 TEST_EQ(TestValue<int>("{ y:V }", "E", "enum E:int { V }"), 0);
546 // A default value of Y is 0.
547 TEST_EQ(TestValue<int>("{ }", "E", "enum E:int { V }"), 0);
548 TEST_EQ(TestValue<int>("{ y:5 }", "E=V", "enum E:int { V=5 }"), 5);
549 // Generate json with defaults and check.
550 TEST_EQ(TestValue<int>(nullptr, "E=V", "enum E:int { V=5 }"), 5);
551 // 5 in enum
552 TEST_EQ(TestValue<int>("{ y:5 }", "E", "enum E:int { Z, V=5 }"), 5);
553 TEST_EQ(TestValue<int>("{ y:5 }", "E=V", "enum E:int { Z, V=5 }"), 5);
554 // Generate json with defaults and check.
555 TEST_EQ(TestValue<int>(nullptr, "E", "enum E:int { Z, V=5 }"), 0);
556 TEST_EQ(TestValue<int>(nullptr, "E=V", "enum E:int { Z, V=5 }"), 5);
557 // u84 test
558 TEST_EQ(TestValue<uint64_t>(nullptr, "E=V",
559 "enum E:ulong { V = 13835058055282163712 }"),
560 13835058055282163712ULL);
561 TEST_EQ(TestValue<uint64_t>(nullptr, "E=V",
562 "enum E:ulong { V = 18446744073709551615 }"),
563 18446744073709551615ULL);
564 // Assign non-enum value to enum field. Is it right?
565 TEST_EQ(TestValue<int>("{ y:7 }", "E", "enum E:int { V = 0 }"), 7);
566 // Check that non-ascending values are valid.
567 TEST_EQ(TestValue<int>("{ y:5 }", "E=V", "enum E:int { Z=10, V=5 }"), 5);
568}
569
570void IntegerBoundaryTest() {
571 // Check numerical compatibility with non-C++ languages.
572 // By the C++ standard, std::numerical_limits<int64_t>::min() ==
573 // -9223372036854775807 (-2^63+1) or less* The Flatbuffers grammar and most of
574 // the languages (C#, Java, Rust) expect that minimum values are: -128,
575 // -32768,.., -9223372036854775808. Since C++20,
576 // static_cast<int64>(0x8000000000000000ULL) is well-defined two's complement
577 // cast. Therefore -9223372036854775808 should be valid negative value.
578 TEST_EQ(flatbuffers::numeric_limits<int8_t>::min(), -128);
579 TEST_EQ(flatbuffers::numeric_limits<int8_t>::max(), 127);
580 TEST_EQ(flatbuffers::numeric_limits<int16_t>::min(), -32768);
581 TEST_EQ(flatbuffers::numeric_limits<int16_t>::max(), 32767);
582 TEST_EQ(flatbuffers::numeric_limits<int32_t>::min() + 1, -2147483647);
583 TEST_EQ(flatbuffers::numeric_limits<int32_t>::max(), 2147483647ULL);
584 TEST_EQ(flatbuffers::numeric_limits<int64_t>::min() + 1LL,
585 -9223372036854775807LL);
586 TEST_EQ(flatbuffers::numeric_limits<int64_t>::max(), 9223372036854775807ULL);
587 TEST_EQ(flatbuffers::numeric_limits<uint8_t>::max(), 255);
588 TEST_EQ(flatbuffers::numeric_limits<uint16_t>::max(), 65535);
589 TEST_EQ(flatbuffers::numeric_limits<uint32_t>::max(), 4294967295ULL);
590 TEST_EQ(flatbuffers::numeric_limits<uint64_t>::max(),
591 18446744073709551615ULL);
592
593 TEST_EQ(TestValue<int8_t>("{ y:127 }", "byte"), 127);
594 TEST_EQ(TestValue<int8_t>("{ y:-128 }", "byte"), -128);
595 TEST_EQ(TestValue<uint8_t>("{ y:255 }", "ubyte"), 255);
596 TEST_EQ(TestValue<uint8_t>("{ y:0 }", "ubyte"), 0);
597 TEST_EQ(TestValue<int16_t>("{ y:32767 }", "short"), 32767);
598 TEST_EQ(TestValue<int16_t>("{ y:-32768 }", "short"), -32768);
599 TEST_EQ(TestValue<uint16_t>("{ y:65535 }", "ushort"), 65535);
600 TEST_EQ(TestValue<uint16_t>("{ y:0 }", "ushort"), 0);
601 TEST_EQ(TestValue<int32_t>("{ y:2147483647 }", "int"), 2147483647);
602 TEST_EQ(TestValue<int32_t>("{ y:-2147483648 }", "int") + 1, -2147483647);
603 TEST_EQ(TestValue<uint32_t>("{ y:4294967295 }", "uint"), 4294967295);
604 TEST_EQ(TestValue<uint32_t>("{ y:0 }", "uint"), 0);
605 TEST_EQ(TestValue<int64_t>("{ y:9223372036854775807 }", "long"),
606 9223372036854775807LL);
607 TEST_EQ(TestValue<int64_t>("{ y:-9223372036854775808 }", "long") + 1LL,
608 -9223372036854775807LL);
609 TEST_EQ(TestValue<uint64_t>("{ y:18446744073709551615 }", "ulong"),
610 18446744073709551615ULL);
611 TEST_EQ(TestValue<uint64_t>("{ y:0 }", "ulong"), 0);
612 TEST_EQ(TestValue<uint64_t>("{ y: 18446744073709551615 }", "uint64"),
613 18446744073709551615ULL);
614 // check that the default works
615 TEST_EQ(TestValue<uint64_t>(nullptr, "uint64 = 18446744073709551615"),
616 18446744073709551615ULL);
617}
618
619void ValidFloatTest() {
620 // check rounding to infinity
621 TEST_EQ(TestValue<float>("{ y:+3.4029e+38 }", "float"), +infinity_f);
622 TEST_EQ(TestValue<float>("{ y:-3.4029e+38 }", "float"), -infinity_f);
623 TEST_EQ(TestValue<double>("{ y:+1.7977e+308 }", "double"), +infinity_d);
624 TEST_EQ(TestValue<double>("{ y:-1.7977e+308 }", "double"), -infinity_d);
625
626 TEST_EQ(
627 FloatCompare(TestValue<float>("{ y:0.0314159e+2 }", "float"), 3.14159f),
628 true);
629 // float in string
630 TEST_EQ(FloatCompare(TestValue<float>("{ y:\" 0.0314159e+2 \" }", "float"),
631 3.14159f),
632 true);
633
634 TEST_EQ(TestValue<float>("{ y:1 }", "float"), 1.0f);
635 TEST_EQ(TestValue<float>("{ y:1.0 }", "float"), 1.0f);
636 TEST_EQ(TestValue<float>("{ y:1. }", "float"), 1.0f);
637 TEST_EQ(TestValue<float>("{ y:+1. }", "float"), 1.0f);
638 TEST_EQ(TestValue<float>("{ y:-1. }", "float"), -1.0f);
639 TEST_EQ(TestValue<float>("{ y:1.e0 }", "float"), 1.0f);
640 TEST_EQ(TestValue<float>("{ y:1.e+0 }", "float"), 1.0f);
641 TEST_EQ(TestValue<float>("{ y:1.e-0 }", "float"), 1.0f);
642 TEST_EQ(TestValue<float>("{ y:0.125 }", "float"), 0.125f);
643 TEST_EQ(TestValue<float>("{ y:.125 }", "float"), 0.125f);
644 TEST_EQ(TestValue<float>("{ y:-.125 }", "float"), -0.125f);
645 TEST_EQ(TestValue<float>("{ y:+.125 }", "float"), +0.125f);
646 TEST_EQ(TestValue<float>("{ y:5 }", "float"), 5.0f);
647 TEST_EQ(TestValue<float>("{ y:\"5\" }", "float"), 5.0f);
648
649#if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
650 // Old MSVC versions may have problem with this check.
651 // https://www.exploringbinary.com/visual-c-plus-plus-strtod-still-broken/
652 TEST_EQ(TestValue<double>("{ y:6.9294956446009195e15 }", "double"),
653 6929495644600920.0);
654 // check nan's
655 TEST_EQ(std::isnan(TestValue<double>("{ y:nan }", "double")), true);
656 TEST_EQ(std::isnan(TestValue<float>("{ y:nan }", "float")), true);
657 TEST_EQ(std::isnan(TestValue<float>("{ y:\"nan\" }", "float")), true);
658 TEST_EQ(std::isnan(TestValue<float>("{ y:\"+nan\" }", "float")), true);
659 TEST_EQ(std::isnan(TestValue<float>("{ y:\"-nan\" }", "float")), true);
660 TEST_EQ(std::isnan(TestValue<float>("{ y:+nan }", "float")), true);
661 TEST_EQ(std::isnan(TestValue<float>("{ y:-nan }", "float")), true);
662 TEST_EQ(std::isnan(TestValue<float>(nullptr, "float=nan")), true);
663 TEST_EQ(std::isnan(TestValue<float>(nullptr, "float=-nan")), true);
664 // check inf
665 TEST_EQ(TestValue<float>("{ y:inf }", "float"), infinity_f);
666 TEST_EQ(TestValue<float>("{ y:\"inf\" }", "float"), infinity_f);
667 TEST_EQ(TestValue<float>("{ y:\"-inf\" }", "float"), -infinity_f);
668 TEST_EQ(TestValue<float>("{ y:\"+inf\" }", "float"), infinity_f);
669 TEST_EQ(TestValue<float>("{ y:+inf }", "float"), infinity_f);
670 TEST_EQ(TestValue<float>("{ y:-inf }", "float"), -infinity_f);
671 TEST_EQ(TestValue<float>(nullptr, "float=inf"), infinity_f);
672 TEST_EQ(TestValue<float>(nullptr, "float=-inf"), -infinity_f);
673 TestValue<double>(
674 "{ y: [0.2, .2, 1.0, -1.0, -2., 2., 1e0, -1e0, 1.0e0, -1.0e0, -3.e2, "
675 "3.0e2] }",
676 "[double]");
677 TestValue<float>(
678 "{ y: [0.2, .2, 1.0, -1.0, -2., 2., 1e0, -1e0, 1.0e0, -1.0e0, -3.e2, "
679 "3.0e2] }",
680 "[float]");
681
682 // Test binary format of float point.
683 // https://en.cppreference.com/w/cpp/language/floating_literal
684 // 0x11.12p-1 = (1*16^1 + 2*16^0 + 3*16^-1 + 4*16^-2) * 2^-1 =
685 TEST_EQ(TestValue<double>("{ y:0x12.34p-1 }", "double"), 9.1015625);
686 // hex fraction 1.2 (decimal 1.125) scaled by 2^3, that is 9.0
687 TEST_EQ(TestValue<float>("{ y:-0x0.2p0 }", "float"), -0.125f);
688 TEST_EQ(TestValue<float>("{ y:-0x.2p1 }", "float"), -0.25f);
689 TEST_EQ(TestValue<float>("{ y:0x1.2p3 }", "float"), 9.0f);
690 TEST_EQ(TestValue<float>("{ y:0x10.1p0 }", "float"), 16.0625f);
691 TEST_EQ(TestValue<double>("{ y:0x1.2p3 }", "double"), 9.0);
692 TEST_EQ(TestValue<double>("{ y:0x10.1p0 }", "double"), 16.0625);
693 TEST_EQ(TestValue<double>("{ y:0xC.68p+2 }", "double"), 49.625);
694 TestValue<double>("{ y: [0x20.4ep1, +0x20.4ep1, -0x20.4ep1] }", "[double]");
695 TestValue<float>("{ y: [0x20.4ep1, +0x20.4ep1, -0x20.4ep1] }", "[float]");
696
697#else // FLATBUFFERS_HAS_NEW_STRTOD
698 TEST_OUTPUT_LINE("FLATBUFFERS_HAS_NEW_STRTOD tests skipped");
699#endif // !FLATBUFFERS_HAS_NEW_STRTOD
700}
701
702void UnicodeTest() {
703 flatbuffers::Parser parser;
704 // Without setting allow_non_utf8 = true, we treat \x sequences as byte
705 // sequences which are then validated as UTF-8.
706 TEST_EQ(parser.Parse("table T { F:string; }"
707 "root_type T;"
708 "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
709 "\\u5225\\u30B5\\u30A4\\u30C8\\xE2\\x82\\xAC\\u0080\\uD8"
710 "3D\\uDE0E\" }"),
711 true);
712 std::string jsongen;
713 parser.opts.indent_step = -1;
714 auto result =
715 GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
716 TEST_EQ(result, true);
717 TEST_EQ_STR(jsongen.c_str(),
718 "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
719 "\\u5225\\u30B5\\u30A4\\u30C8\\u20AC\\u0080\\uD83D\\uDE0E\"}");
720}
721
722void UnicodeTestAllowNonUTF8() {
723 flatbuffers::Parser parser;
724 parser.opts.allow_non_utf8 = true;
725 TEST_EQ(
726 parser.Parse(
727 "table T { F:string; }"
728 "root_type T;"
729 "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
730 "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\\u0080\\uD83D\\uDE0E\" }"),
731 true);
732 std::string jsongen;
733 parser.opts.indent_step = -1;
734 auto result =
735 GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
736 TEST_EQ(result, true);
737 TEST_EQ_STR(
738 jsongen.c_str(),
739 "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
740 "\\u5225\\u30B5\\u30A4\\u30C8\\u0001\\x80\\u0080\\uD83D\\uDE0E\"}");
741}
742
743void UnicodeTestGenerateTextFailsOnNonUTF8() {
744 flatbuffers::Parser parser;
745 // Allow non-UTF-8 initially to model what happens when we load a binary
746 // flatbuffer from disk which contains non-UTF-8 strings.
747 parser.opts.allow_non_utf8 = true;
748 TEST_EQ(
749 parser.Parse(
750 "table T { F:string; }"
751 "root_type T;"
752 "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
753 "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\\u0080\\uD83D\\uDE0E\" }"),
754 true);
755 std::string jsongen;
756 parser.opts.indent_step = -1;
757 // Now, disallow non-UTF-8 (the default behavior) so GenerateText indicates
758 // failure.
759 parser.opts.allow_non_utf8 = false;
760 auto result =
761 GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
762 TEST_EQ(result, false);
763}
764
765void UnicodeSurrogatesTest() {
766 flatbuffers::Parser parser;
767
768 TEST_EQ(parser.Parse("table T { F:string (id: 0); }"
769 "root_type T;"
770 "{ F:\"\\uD83D\\uDCA9\"}"),
771 true);
772 auto root = flatbuffers::GetRoot<flatbuffers::Table>(
773 parser.builder_.GetBufferPointer());
774 auto string = root->GetPointer<flatbuffers::String *>(
775 flatbuffers::FieldIndexToOffset(0));
776 TEST_EQ_STR(string->c_str(), "\xF0\x9F\x92\xA9");
777}
778
779
780
781void UnknownFieldsTest() {
782 flatbuffers::IDLOptions opts;
783 opts.skip_unexpected_fields_in_json = true;
784 flatbuffers::Parser parser(opts);
785
786 TEST_EQ(parser.Parse("table T { str:string; i:int;}"
787 "root_type T;"
788 "{ str:\"test\","
789 "unknown_string:\"test\","
790 "\"unknown_string\":\"test\","
791 "unknown_int:10,"
792 "unknown_float:1.0,"
793 "unknown_array: [ 1, 2, 3, 4],"
794 "unknown_object: { i: 10 },"
795 "\"unknown_object\": { \"i\": 10 },"
796 "i:10}"),
797 true);
798
799 std::string jsongen;
800 parser.opts.indent_step = -1;
801 auto result =
802 GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
803 TEST_EQ(result, true);
804 TEST_EQ_STR(jsongen.c_str(), "{str: \"test\",i: 10}");
805}
806
807void ParseUnionTest() {
808 // Unions must be parseable with the type field following the object.
809 flatbuffers::Parser parser;
810 TEST_EQ(parser.Parse("table T { A:int; }"
811 "union U { T }"
812 "table V { X:U; }"
813 "root_type V;"
814 "{ X:{ A:1 }, X_type: T }"),
815 true);
816 // Unions must be parsable with prefixed namespace.
817 flatbuffers::Parser parser2;
818 TEST_EQ(parser2.Parse("namespace N; table A {} namespace; union U { N.A }"
819 "table B { e:U; } root_type B;"
820 "{ e_type: N_A, e: {} }"),
821 true);
822}
823
824void ValidSameNameDifferentNamespaceTest() {
825 // Duplicate table names in different namespaces must be parsable
826 TEST_ASSERT(flatbuffers::Parser().Parse(
827 "namespace A; table X {} namespace B; table X {}"));
828 // Duplicate union names in different namespaces must be parsable
829 TEST_ASSERT(flatbuffers::Parser().Parse(
830 "namespace A; union X {} namespace B; union X {}"));
831 // Clashing table and union names in different namespaces must be parsable
832 TEST_ASSERT(flatbuffers::Parser().Parse(
833 "namespace A; table X {} namespace B; union X {}"));
834 TEST_ASSERT(flatbuffers::Parser().Parse(
835 "namespace A; union X {} namespace B; table X {}"));
836}
837
838void WarningsAsErrorsTest() {
839 {
840 flatbuffers::IDLOptions opts;
841 // opts.warnings_as_errors should default to false
842 flatbuffers::Parser parser(opts);
843 TEST_EQ(parser.Parse("table T { THIS_NAME_CAUSES_A_WARNING:string;}\n"
844 "root_type T;"),
845 true);
846 }
847 {
848 flatbuffers::IDLOptions opts;
849 opts.warnings_as_errors = true;
850 flatbuffers::Parser parser(opts);
851 TEST_EQ(parser.Parse("table T { THIS_NAME_CAUSES_A_WARNING:string;}\n"
852 "root_type T;"),
853 false);
854 }
855}
856
857void StringVectorDefaultsTest() {
858 std::vector<std::string> schemas;
859 schemas.push_back("table Monster { mana: string = \"\"; }");
860 schemas.push_back("table Monster { mana: string = \"mystr\"; }");
861 schemas.push_back("table Monster { mana: string = \" \"; }");
862 schemas.push_back("table Monster { mana: string = \"null\"; }");
863 schemas.push_back("table Monster { mana: [int] = []; }");
864 schemas.push_back("table Monster { mana: [uint] = [ ]; }");
865 schemas.push_back("table Monster { mana: [byte] = [\t\t\n]; }");
866 schemas.push_back("enum E:int{}table Monster{mana:[E]=[];}");
867 for (auto s = schemas.begin(); s < schemas.end(); s++) {
868 flatbuffers::Parser parser;
869 TEST_ASSERT(parser.Parse(s->c_str()));
870 const auto *mana = parser.structs_.Lookup("Monster")->fields.Lookup("mana");
871 TEST_EQ(mana->IsDefault(), true);
872 }
873}
874
875void FieldIdentifierTest() {
876 using flatbuffers::Parser;
877 TEST_EQ(true, Parser().Parse("table T{ f: int (id:0); }"));
878 // non-integer `id` should be rejected
879 TEST_EQ(false, Parser().Parse("table T{ f: int (id:text); }"));
880 TEST_EQ(false, Parser().Parse("table T{ f: int (id:\"text\"); }"));
881 TEST_EQ(false, Parser().Parse("table T{ f: int (id:0text); }"));
882 TEST_EQ(false, Parser().Parse("table T{ f: int (id:1.0); }"));
883 TEST_EQ(false, Parser().Parse("table T{ f: int (id:-1); g: int (id:0); }"));
884 TEST_EQ(false, Parser().Parse("table T{ f: int (id:129496726); }"));
885 // A unuion filed occupys two ids: enumerator + pointer (offset).
886 TEST_EQ(false,
887 Parser().Parse("union X{} table T{ u: X(id:0); table F{x:int;\n}"));
888 // Positive tests for unions
889 TEST_EQ(true, Parser().Parse("union X{} table T{ u: X (id:1); }"));
890 TEST_EQ(true, Parser().Parse("union X{} table T{ u: X; }"));
891 // Test using 'inf' and 'nan' words both as identifiers and as default values.
892 TEST_EQ(true, Parser().Parse("table T{ nan: string; }"));
893 TEST_EQ(true, Parser().Parse("table T{ inf: string; }"));
894#if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
895 TEST_EQ(true, Parser().Parse("table T{ inf: float = inf; }"));
896 TEST_EQ(true, Parser().Parse("table T{ nan: float = inf; }"));
897#endif
898}
899
900} // namespace tests
901} // namespace flatbuffers