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