Add `NaN` and `Inf` defaults to the C++ generated code. (#5102)
[platform/upstream/flatbuffers.git] / tests / test.cpp
1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <cmath>
17 #include "flatbuffers/flatbuffers.h"
18 #include "flatbuffers/idl.h"
19 #include "flatbuffers/minireflect.h"
20 #include "flatbuffers/registry.h"
21 #include "flatbuffers/util.h"
22
23 // clang-format off
24 #ifdef FLATBUFFERS_CPP98_STL
25   #include "flatbuffers/stl_emulation.h"
26   namespace std {
27     using flatbuffers::unique_ptr;
28   }
29 #endif
30 // clang-format on
31
32 #include "monster_test_generated.h"
33 #include "namespace_test/namespace_test1_generated.h"
34 #include "namespace_test/namespace_test2_generated.h"
35 #include "union_vector/union_vector_generated.h"
36 #include "monster_extra_generated.h"
37 #include "test_assert.h"
38
39 #include "flatbuffers/flexbuffers.h"
40
41 using namespace MyGame::Example;
42
43 void FlatBufferBuilderTest();
44
45 // Include simple random number generator to ensure results will be the
46 // same cross platform.
47 // http://en.wikipedia.org/wiki/Park%E2%80%93Miller_random_number_generator
48 uint32_t lcg_seed = 48271;
49 uint32_t lcg_rand() {
50   return lcg_seed = (static_cast<uint64_t>(lcg_seed) * 279470273UL) % 4294967291UL;
51 }
52 void lcg_reset() { lcg_seed = 48271; }
53
54 std::string test_data_path =
55 #ifdef BAZEL_TEST_DATA_PATH
56     "../com_github_google_flatbuffers/tests/";
57 #else
58     "tests/";
59 #endif
60
61 // example of how to build up a serialized buffer algorithmically:
62 flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
63   flatbuffers::FlatBufferBuilder builder;
64
65   auto vec = Vec3(1, 2, 3, 0, Color_Red, Test(10, 20));
66
67   auto name = builder.CreateString("MyMonster");
68
69   unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
70   auto inventory = builder.CreateVector(inv_data, 10);
71
72   // Alternatively, create the vector first, and fill in data later:
73   // unsigned char *inv_buf = nullptr;
74   // auto inventory = builder.CreateUninitializedVector<unsigned char>(
75   //                                                              10, &inv_buf);
76   // memcpy(inv_buf, inv_data, 10);
77
78   Test tests[] = { Test(10, 20), Test(30, 40) };
79   auto testv = builder.CreateVectorOfStructs(tests, 2);
80
81   // clang-format off
82   #ifndef FLATBUFFERS_CPP98_STL
83     // Create a vector of structures from a lambda.
84     auto testv2 = builder.CreateVectorOfStructs<Test>(
85           2, [&](size_t i, Test* s) -> void {
86             *s = tests[i];
87           });
88   #else
89     // Create a vector of structures using a plain old C++ function.
90     auto testv2 = builder.CreateVectorOfStructs<Test>(
91           2, [](size_t i, Test* s, void *state) -> void {
92             *s = (reinterpret_cast<Test*>(state))[i];
93           }, tests);
94   #endif  // FLATBUFFERS_CPP98_STL
95   // clang-format on
96
97   // create monster with very few fields set:
98   // (same functionality as CreateMonster below, but sets fields manually)
99   flatbuffers::Offset<Monster> mlocs[3];
100   auto fred = builder.CreateString("Fred");
101   auto barney = builder.CreateString("Barney");
102   auto wilma = builder.CreateString("Wilma");
103   MonsterBuilder mb1(builder);
104   mb1.add_name(fred);
105   mlocs[0] = mb1.Finish();
106   MonsterBuilder mb2(builder);
107   mb2.add_name(barney);
108   mb2.add_hp(1000);
109   mlocs[1] = mb2.Finish();
110   MonsterBuilder mb3(builder);
111   mb3.add_name(wilma);
112   mlocs[2] = mb3.Finish();
113
114   // Create an array of strings. Also test string pooling, and lambdas.
115   auto vecofstrings =
116       builder.CreateVector<flatbuffers::Offset<flatbuffers::String>>(
117           4,
118           [](size_t i, flatbuffers::FlatBufferBuilder *b)
119               -> flatbuffers::Offset<flatbuffers::String> {
120             static const char *names[] = { "bob", "fred", "bob", "fred" };
121             return b->CreateSharedString(names[i]);
122           },
123           &builder);
124
125   // Creating vectors of strings in one convenient call.
126   std::vector<std::string> names2;
127   names2.push_back("jane");
128   names2.push_back("mary");
129   auto vecofstrings2 = builder.CreateVectorOfStrings(names2);
130
131   // Create an array of sorted tables, can be used with binary search when read:
132   auto vecoftables = builder.CreateVectorOfSortedTables(mlocs, 3);
133
134   // Create an array of sorted structs,
135   // can be used with binary search when read:
136   std::vector<Ability> abilities;
137   abilities.push_back(Ability(4, 40));
138   abilities.push_back(Ability(3, 30));
139   abilities.push_back(Ability(2, 20));
140   abilities.push_back(Ability(1, 10));
141   auto vecofstructs = builder.CreateVectorOfSortedStructs(&abilities);
142
143   // Create a nested FlatBuffer.
144   // Nested FlatBuffers are stored in a ubyte vector, which can be convenient
145   // since they can be memcpy'd around much easier than other FlatBuffer
146   // values. They have little overhead compared to storing the table directly.
147   // As a test, create a mostly empty Monster buffer:
148   flatbuffers::FlatBufferBuilder nested_builder;
149   auto nmloc = CreateMonster(nested_builder, nullptr, 0, 0,
150                              nested_builder.CreateString("NestedMonster"));
151   FinishMonsterBuffer(nested_builder, nmloc);
152   // Now we can store the buffer in the parent. Note that by default, vectors
153   // are only aligned to their elements or size field, so in this case if the
154   // buffer contains 64-bit elements, they may not be correctly aligned. We fix
155   // that with:
156   builder.ForceVectorAlignment(nested_builder.GetSize(), sizeof(uint8_t),
157                                nested_builder.GetBufferMinAlignment());
158   // If for whatever reason you don't have the nested_builder available, you
159   // can substitute flatbuffers::largest_scalar_t (64-bit) for the alignment, or
160   // the largest force_align value in your schema if you're using it.
161   auto nested_flatbuffer_vector = builder.CreateVector(
162       nested_builder.GetBufferPointer(), nested_builder.GetSize());
163
164   // Test a nested FlexBuffer:
165   flexbuffers::Builder flexbuild;
166   flexbuild.Int(1234);
167   flexbuild.Finish();
168   auto flex = builder.CreateVector(flexbuild.GetBuffer());
169
170   // Test vector of enums.
171   Color colors[] = { Color_Blue, Color_Green };
172   // We use this special creation function because we have an array of
173   // pre-C++11 (enum class) enums whose size likely is int, yet its declared
174   // type in the schema is byte.
175   auto vecofcolors = builder.CreateVectorScalarCast<int8_t, Color>(colors, 2);
176
177   // shortcut for creating monster with all fields set:
178   auto mloc = CreateMonster(builder, &vec, 150, 80, name, inventory, Color_Blue,
179                             Any_Monster, mlocs[1].Union(),  // Store a union.
180                             testv, vecofstrings, vecoftables, 0,
181                             nested_flatbuffer_vector, 0, false, 0, 0, 0, 0, 0,
182                             0, 0, 0, 0, 3.14159f, 3.0f, 0.0f, vecofstrings2,
183                             vecofstructs, flex, testv2, 0, 0, 0, 0, 0, 0, 0, 0,
184                             0, 0, 0, AnyUniqueAliases_NONE, 0,
185                             AnyAmbiguousAliases_NONE, 0, vecofcolors);
186
187   FinishMonsterBuffer(builder, mloc);
188
189   // clang-format off
190   #ifdef FLATBUFFERS_TEST_VERBOSE
191   // print byte data for debugging:
192   auto p = builder.GetBufferPointer();
193   for (flatbuffers::uoffset_t i = 0; i < builder.GetSize(); i++)
194     printf("%d ", p[i]);
195   #endif
196   // clang-format on
197
198   // return the buffer for the caller to use.
199   auto bufferpointer =
200       reinterpret_cast<const char *>(builder.GetBufferPointer());
201   buffer.assign(bufferpointer, bufferpointer + builder.GetSize());
202
203   return builder.ReleaseBufferPointer();
204 }
205
206 //  example of accessing a buffer loaded in memory:
207 void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length,
208                           bool pooled = true) {
209   // First, verify the buffers integrity (optional)
210   flatbuffers::Verifier verifier(flatbuf, length);
211   TEST_EQ(VerifyMonsterBuffer(verifier), true);
212
213   std::vector<uint8_t> test_buff;
214   test_buff.resize(length * 2);
215   std::memcpy(&test_buff[0], flatbuf, length);
216   std::memcpy(&test_buff[length], flatbuf, length);
217
218   flatbuffers::Verifier verifier1(&test_buff[0], length);
219   TEST_EQ(VerifyMonsterBuffer(verifier1), true);
220   TEST_EQ(verifier1.GetComputedSize(), length);
221
222   flatbuffers::Verifier verifier2(&test_buff[length], length);
223   TEST_EQ(VerifyMonsterBuffer(verifier2), true);
224   TEST_EQ(verifier2.GetComputedSize(), length);
225
226   TEST_EQ(strcmp(MonsterIdentifier(), "MONS"), 0);
227   TEST_EQ(MonsterBufferHasIdentifier(flatbuf), true);
228   TEST_EQ(strcmp(MonsterExtension(), "mon"), 0);
229
230   // Access the buffer from the root.
231   auto monster = GetMonster(flatbuf);
232
233   TEST_EQ(monster->hp(), 80);
234   TEST_EQ(monster->mana(), 150);  // default
235   TEST_EQ_STR(monster->name()->c_str(), "MyMonster");
236   // Can't access the following field, it is deprecated in the schema,
237   // which means accessors are not generated:
238   // monster.friendly()
239
240   auto pos = monster->pos();
241   TEST_NOTNULL(pos);
242   TEST_EQ(pos->z(), 3);
243   TEST_EQ(pos->test3().a(), 10);
244   TEST_EQ(pos->test3().b(), 20);
245
246   auto inventory = monster->inventory();
247   TEST_EQ(VectorLength(inventory), 10UL);  // Works even if inventory is null.
248   TEST_NOTNULL(inventory);
249   unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
250   // Check compatibilty of iterators with STL.
251   std::vector<unsigned char> inv_vec(inventory->begin(), inventory->end());
252   for (auto it = inventory->begin(); it != inventory->end(); ++it) {
253     auto indx = it - inventory->begin();
254     TEST_EQ(*it, inv_vec.at(indx));  // Use bounds-check.
255     TEST_EQ(*it, inv_data[indx]);
256   }
257
258   TEST_EQ(monster->color(), Color_Blue);
259
260   // Example of accessing a union:
261   TEST_EQ(monster->test_type(), Any_Monster);  // First make sure which it is.
262   auto monster2 = reinterpret_cast<const Monster *>(monster->test());
263   TEST_NOTNULL(monster2);
264   TEST_EQ_STR(monster2->name()->c_str(), "Fred");
265
266   // Example of accessing a vector of strings:
267   auto vecofstrings = monster->testarrayofstring();
268   TEST_EQ(vecofstrings->Length(), 4U);
269   TEST_EQ_STR(vecofstrings->Get(0)->c_str(), "bob");
270   TEST_EQ_STR(vecofstrings->Get(1)->c_str(), "fred");
271   if (pooled) {
272     // These should have pointer equality because of string pooling.
273     TEST_EQ(vecofstrings->Get(0)->c_str(), vecofstrings->Get(2)->c_str());
274     TEST_EQ(vecofstrings->Get(1)->c_str(), vecofstrings->Get(3)->c_str());
275   }
276
277   auto vecofstrings2 = monster->testarrayofstring2();
278   if (vecofstrings2) {
279     TEST_EQ(vecofstrings2->Length(), 2U);
280     TEST_EQ_STR(vecofstrings2->Get(0)->c_str(), "jane");
281     TEST_EQ_STR(vecofstrings2->Get(1)->c_str(), "mary");
282   }
283
284   // Example of accessing a vector of tables:
285   auto vecoftables = monster->testarrayoftables();
286   TEST_EQ(vecoftables->Length(), 3U);
287   for (auto it = vecoftables->begin(); it != vecoftables->end(); ++it)
288     TEST_EQ(strlen(it->name()->c_str()) >= 4, true);
289   TEST_EQ_STR(vecoftables->Get(0)->name()->c_str(), "Barney");
290   TEST_EQ(vecoftables->Get(0)->hp(), 1000);
291   TEST_EQ_STR(vecoftables->Get(1)->name()->c_str(), "Fred");
292   TEST_EQ_STR(vecoftables->Get(2)->name()->c_str(), "Wilma");
293   TEST_NOTNULL(vecoftables->LookupByKey("Barney"));
294   TEST_NOTNULL(vecoftables->LookupByKey("Fred"));
295   TEST_NOTNULL(vecoftables->LookupByKey("Wilma"));
296
297   // Test accessing a vector of sorted structs
298   auto vecofstructs = monster->testarrayofsortedstruct();
299   if (vecofstructs) {  // not filled in monster_test.bfbs
300     for (flatbuffers::uoffset_t i = 0; i < vecofstructs->size() - 1; i++) {
301       auto left = vecofstructs->Get(i);
302       auto right = vecofstructs->Get(i + 1);
303       TEST_EQ(true, (left->KeyCompareLessThan(right)));
304     }
305     TEST_NOTNULL(vecofstructs->LookupByKey(3));
306     TEST_EQ(static_cast<const Ability *>(nullptr),
307             vecofstructs->LookupByKey(5));
308   }
309
310   // Test nested FlatBuffers if available:
311   auto nested_buffer = monster->testnestedflatbuffer();
312   if (nested_buffer) {
313     // nested_buffer is a vector of bytes you can memcpy. However, if you
314     // actually want to access the nested data, this is a convenient
315     // accessor that directly gives you the root table:
316     auto nested_monster = monster->testnestedflatbuffer_nested_root();
317     TEST_EQ_STR(nested_monster->name()->c_str(), "NestedMonster");
318   }
319
320   // Test flexbuffer if available:
321   auto flex = monster->flex();
322   // flex is a vector of bytes you can memcpy etc.
323   TEST_EQ(flex->size(), 4);  // Encoded FlexBuffer bytes.
324   // However, if you actually want to access the nested data, this is a
325   // convenient accessor that directly gives you the root value:
326   TEST_EQ(monster->flex_flexbuffer_root().AsInt16(), 1234);
327
328   // Test vector of enums:
329   auto colors = monster->vector_of_enums();
330   if (colors) {
331     TEST_EQ(colors->size(), 2);
332     TEST_EQ(colors->Get(0), Color_Blue);
333     TEST_EQ(colors->Get(1), Color_Green);
334   }
335
336   // Since Flatbuffers uses explicit mechanisms to override the default
337   // compiler alignment, double check that the compiler indeed obeys them:
338   // (Test consists of a short and byte):
339   TEST_EQ(flatbuffers::AlignOf<Test>(), 2UL);
340   TEST_EQ(sizeof(Test), 4UL);
341
342   const flatbuffers::Vector<const Test *> *tests_array[] = {
343     monster->test4(),
344     monster->test5(),
345   };
346   for (size_t i = 0; i < sizeof(tests_array) / sizeof(tests_array[0]); ++i) {
347     auto tests = tests_array[i];
348     TEST_NOTNULL(tests);
349     auto test_0 = tests->Get(0);
350     auto test_1 = tests->Get(1);
351     TEST_EQ(test_0->a(), 10);
352     TEST_EQ(test_0->b(), 20);
353     TEST_EQ(test_1->a(), 30);
354     TEST_EQ(test_1->b(), 40);
355     for (auto it = tests->begin(); it != tests->end(); ++it) {
356       TEST_EQ(it->a() == 10 || it->a() == 30, true);  // Just testing iterators.
357     }
358   }
359
360   // Checking for presence of fields:
361   TEST_EQ(flatbuffers::IsFieldPresent(monster, Monster::VT_HP), true);
362   TEST_EQ(flatbuffers::IsFieldPresent(monster, Monster::VT_MANA), false);
363
364   // Obtaining a buffer from a root:
365   TEST_EQ(GetBufferStartFromRootPointer(monster), flatbuf);
366 }
367
368 // Change a FlatBuffer in-place, after it has been constructed.
369 void MutateFlatBuffersTest(uint8_t *flatbuf, std::size_t length) {
370   // Get non-const pointer to root.
371   auto monster = GetMutableMonster(flatbuf);
372
373   // Each of these tests mutates, then tests, then set back to the original,
374   // so we can test that the buffer in the end still passes our original test.
375   auto hp_ok = monster->mutate_hp(10);
376   TEST_EQ(hp_ok, true);  // Field was present.
377   TEST_EQ(monster->hp(), 10);
378   // Mutate to default value
379   auto hp_ok_default = monster->mutate_hp(100);
380   TEST_EQ(hp_ok_default, true);  // Field was present.
381   TEST_EQ(monster->hp(), 100);
382   // Test that mutate to default above keeps field valid for further mutations
383   auto hp_ok_2 = monster->mutate_hp(20);
384   TEST_EQ(hp_ok_2, true);
385   TEST_EQ(monster->hp(), 20);
386   monster->mutate_hp(80);
387
388   // Monster originally at 150 mana (default value)
389   auto mana_default_ok = monster->mutate_mana(150);  // Mutate to default value.
390   TEST_EQ(mana_default_ok,
391           true);  // Mutation should succeed, because default value.
392   TEST_EQ(monster->mana(), 150);
393   auto mana_ok = monster->mutate_mana(10);
394   TEST_EQ(mana_ok, false);  // Field was NOT present, because default value.
395   TEST_EQ(monster->mana(), 150);
396
397   // Mutate structs.
398   auto pos = monster->mutable_pos();
399   auto test3 = pos->mutable_test3();  // Struct inside a struct.
400   test3.mutate_a(50);                 // Struct fields never fail.
401   TEST_EQ(test3.a(), 50);
402   test3.mutate_a(10);
403
404   // Mutate vectors.
405   auto inventory = monster->mutable_inventory();
406   inventory->Mutate(9, 100);
407   TEST_EQ(inventory->Get(9), 100);
408   inventory->Mutate(9, 9);
409
410   auto tables = monster->mutable_testarrayoftables();
411   auto first = tables->GetMutableObject(0);
412   TEST_EQ(first->hp(), 1000);
413   first->mutate_hp(0);
414   TEST_EQ(first->hp(), 0);
415   first->mutate_hp(1000);
416
417   // Run the verifier and the regular test to make sure we didn't trample on
418   // anything.
419   AccessFlatBufferTest(flatbuf, length);
420 }
421
422 // Unpack a FlatBuffer into objects.
423 void ObjectFlatBuffersTest(uint8_t *flatbuf) {
424   // Optional: we can specify resolver and rehasher functions to turn hashed
425   // strings into object pointers and back, to implement remote references
426   // and such.
427   auto resolver = flatbuffers::resolver_function_t(
428       [](void **pointer_adr, flatbuffers::hash_value_t hash) {
429         (void)pointer_adr;
430         (void)hash;
431         // Don't actually do anything, leave variable null.
432       });
433   auto rehasher = flatbuffers::rehasher_function_t(
434       [](void *pointer) -> flatbuffers::hash_value_t {
435         (void)pointer;
436         return 0;
437       });
438
439   // Turn a buffer into C++ objects.
440   auto monster1 = UnPackMonster(flatbuf, &resolver);
441
442   // Re-serialize the data.
443   flatbuffers::FlatBufferBuilder fbb1;
444   fbb1.Finish(CreateMonster(fbb1, monster1.get(), &rehasher),
445               MonsterIdentifier());
446
447   // Unpack again, and re-serialize again.
448   auto monster2 = UnPackMonster(fbb1.GetBufferPointer(), &resolver);
449   flatbuffers::FlatBufferBuilder fbb2;
450   fbb2.Finish(CreateMonster(fbb2, monster2.get(), &rehasher),
451               MonsterIdentifier());
452
453   // Now we've gone full round-trip, the two buffers should match.
454   auto len1 = fbb1.GetSize();
455   auto len2 = fbb2.GetSize();
456   TEST_EQ(len1, len2);
457   TEST_EQ(memcmp(fbb1.GetBufferPointer(), fbb2.GetBufferPointer(), len1), 0);
458
459   // Test it with the original buffer test to make sure all data survived.
460   AccessFlatBufferTest(fbb2.GetBufferPointer(), len2, false);
461
462   // Test accessing fields, similar to AccessFlatBufferTest above.
463   TEST_EQ(monster2->hp, 80);
464   TEST_EQ(monster2->mana, 150);  // default
465   TEST_EQ_STR(monster2->name.c_str(), "MyMonster");
466
467   auto &pos = monster2->pos;
468   TEST_NOTNULL(pos);
469   TEST_EQ(pos->z(), 3);
470   TEST_EQ(pos->test3().a(), 10);
471   TEST_EQ(pos->test3().b(), 20);
472
473   auto &inventory = monster2->inventory;
474   TEST_EQ(inventory.size(), 10UL);
475   unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
476   for (auto it = inventory.begin(); it != inventory.end(); ++it)
477     TEST_EQ(*it, inv_data[it - inventory.begin()]);
478
479   TEST_EQ(monster2->color, Color_Blue);
480
481   auto monster3 = monster2->test.AsMonster();
482   TEST_NOTNULL(monster3);
483   TEST_EQ_STR(monster3->name.c_str(), "Fred");
484
485   auto &vecofstrings = monster2->testarrayofstring;
486   TEST_EQ(vecofstrings.size(), 4U);
487   TEST_EQ_STR(vecofstrings[0].c_str(), "bob");
488   TEST_EQ_STR(vecofstrings[1].c_str(), "fred");
489
490   auto &vecofstrings2 = monster2->testarrayofstring2;
491   TEST_EQ(vecofstrings2.size(), 2U);
492   TEST_EQ_STR(vecofstrings2[0].c_str(), "jane");
493   TEST_EQ_STR(vecofstrings2[1].c_str(), "mary");
494
495   auto &vecoftables = monster2->testarrayoftables;
496   TEST_EQ(vecoftables.size(), 3U);
497   TEST_EQ_STR(vecoftables[0]->name.c_str(), "Barney");
498   TEST_EQ(vecoftables[0]->hp, 1000);
499   TEST_EQ_STR(vecoftables[1]->name.c_str(), "Fred");
500   TEST_EQ_STR(vecoftables[2]->name.c_str(), "Wilma");
501
502   auto &tests = monster2->test4;
503   TEST_EQ(tests[0].a(), 10);
504   TEST_EQ(tests[0].b(), 20);
505   TEST_EQ(tests[1].a(), 30);
506   TEST_EQ(tests[1].b(), 40);
507 }
508
509 // Prefix a FlatBuffer with a size field.
510 void SizePrefixedTest() {
511   // Create size prefixed buffer.
512   flatbuffers::FlatBufferBuilder fbb;
513   FinishSizePrefixedMonsterBuffer(
514       fbb,
515       CreateMonster(fbb, 0, 200, 300, fbb.CreateString("bob")));
516
517   // Verify it.
518   flatbuffers::Verifier verifier(fbb.GetBufferPointer(), fbb.GetSize());
519   TEST_EQ(VerifySizePrefixedMonsterBuffer(verifier), true);
520
521   // Access it.
522   auto m = GetSizePrefixedMonster(fbb.GetBufferPointer());
523   TEST_EQ(m->mana(), 200);
524   TEST_EQ(m->hp(), 300);
525   TEST_EQ_STR(m->name()->c_str(), "bob");
526 }
527
528 void TriviallyCopyableTest() {
529   // clang-format off
530   #if __GNUG__ && __GNUC__ < 5
531     TEST_EQ(__has_trivial_copy(Vec3), true);
532   #else
533     #if __cplusplus >= 201103L
534       TEST_EQ(std::is_trivially_copyable<Vec3>::value, true);
535     #endif
536   #endif
537   // clang-format on
538 }
539
540 // Check stringify of an default enum value to json
541 void JsonDefaultTest() {
542   // load FlatBuffer schema (.fbs) from disk
543   std::string schemafile;
544   TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.fbs").c_str(),
545                                 false, &schemafile), true);
546   // parse schema first, so we can use it to parse the data after
547   flatbuffers::Parser parser;
548   auto include_test_path =
549       flatbuffers::ConCatPathFileName(test_data_path, "include_test");
550   const char *include_directories[] = { test_data_path.c_str(),
551                                         include_test_path.c_str(), nullptr };
552
553   TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
554   // create incomplete monster and store to json
555   parser.opts.output_default_scalars_in_json = true;
556   parser.opts.output_enum_identifiers = true;
557   flatbuffers::FlatBufferBuilder builder;
558   auto name = builder.CreateString("default_enum");
559   MonsterBuilder color_monster(builder);
560   color_monster.add_name(name);
561   FinishMonsterBuffer(builder, color_monster.Finish());
562   std::string jsongen;
563   auto result = GenerateText(parser, builder.GetBufferPointer(), &jsongen);
564   TEST_EQ(result, true);
565   // default value of the "color" field is Blue
566   TEST_EQ(std::string::npos != jsongen.find("color: \"Blue\""), true);
567   // default value of the "testf" field is 3.14159
568   TEST_EQ(std::string::npos != jsongen.find("testf: 3.14159"), true);
569 }
570
571 // example of parsing text straight into a buffer, and generating
572 // text back from it:
573 void ParseAndGenerateTextTest(bool binary) {
574   // load FlatBuffer schema (.fbs) and JSON from disk
575   std::string schemafile;
576   std::string jsonfile;
577   TEST_EQ(flatbuffers::LoadFile(
578               (test_data_path + "monster_test." + (binary ? "bfbs" : "fbs"))
579                   .c_str(),
580               binary, &schemafile),
581           true);
582   TEST_EQ(flatbuffers::LoadFile(
583               (test_data_path + "monsterdata_test.golden").c_str(), false,
584               &jsonfile),
585           true);
586
587   auto include_test_path =
588     flatbuffers::ConCatPathFileName(test_data_path, "include_test");
589   const char *include_directories[] = { test_data_path.c_str(),
590                                         include_test_path.c_str(), nullptr };
591
592   // parse schema first, so we can use it to parse the data after
593   flatbuffers::Parser parser;
594   if (binary) {
595     flatbuffers::Verifier verifier(
596         reinterpret_cast<const uint8_t *>(schemafile.c_str()),
597         schemafile.size());
598     TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
599     //auto schema = reflection::GetSchema(schemafile.c_str());
600     TEST_EQ(parser.Deserialize((const uint8_t *)schemafile.c_str(), schemafile.size()), true);
601   } else {
602     TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
603   }
604   TEST_EQ(parser.Parse(jsonfile.c_str(), include_directories), true);
605
606   // here, parser.builder_ contains a binary buffer that is the parsed data.
607
608   // First, verify it, just in case:
609   flatbuffers::Verifier verifier(parser.builder_.GetBufferPointer(),
610                                  parser.builder_.GetSize());
611   TEST_EQ(VerifyMonsterBuffer(verifier), true);
612
613   AccessFlatBufferTest(parser.builder_.GetBufferPointer(),
614                        parser.builder_.GetSize(), false);
615
616   // to ensure it is correct, we now generate text back from the binary,
617   // and compare the two:
618   std::string jsongen;
619   auto result =
620       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
621   TEST_EQ(result, true);
622   TEST_EQ_STR(jsongen.c_str(), jsonfile.c_str());
623
624   // We can also do the above using the convenient Registry that knows about
625   // a set of file_identifiers mapped to schemas.
626   flatbuffers::Registry registry;
627   // Make sure schemas can find their includes.
628   registry.AddIncludeDirectory(test_data_path.c_str());
629   registry.AddIncludeDirectory(include_test_path.c_str());
630   // Call this with many schemas if possible.
631   registry.Register(MonsterIdentifier(),
632                     (test_data_path + "monster_test.fbs").c_str());
633   // Now we got this set up, we can parse by just specifying the identifier,
634   // the correct schema will be loaded on the fly:
635   auto buf = registry.TextToFlatBuffer(jsonfile.c_str(), MonsterIdentifier());
636   // If this fails, check registry.lasterror_.
637   TEST_NOTNULL(buf.data());
638   // Test the buffer, to be sure:
639   AccessFlatBufferTest(buf.data(), buf.size(), false);
640   // We can use the registry to turn this back into text, in this case it
641   // will get the file_identifier from the binary:
642   std::string text;
643   auto ok = registry.FlatBufferToText(buf.data(), buf.size(), &text);
644   // If this fails, check registry.lasterror_.
645   TEST_EQ(ok, true);
646   TEST_EQ_STR(text.c_str(), jsonfile.c_str());
647
648   // Generate text for UTF-8 strings without escapes.
649   std::string jsonfile_utf8;
650   TEST_EQ(flatbuffers::LoadFile((test_data_path + "unicode_test.json").c_str(),
651                                 false, &jsonfile_utf8),
652           true);
653   TEST_EQ(parser.Parse(jsonfile_utf8.c_str(), include_directories), true);
654   // To ensure it is correct, generate utf-8 text back from the binary.
655   std::string jsongen_utf8;
656   // request natural printing for utf-8 strings
657   parser.opts.natural_utf8 = true;
658   parser.opts.strict_json = true;
659   TEST_EQ(
660       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen_utf8),
661       true);
662   TEST_EQ_STR(jsongen_utf8.c_str(), jsonfile_utf8.c_str());
663 }
664
665 void ReflectionTest(uint8_t *flatbuf, size_t length) {
666   // Load a binary schema.
667   std::string bfbsfile;
668   TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.bfbs").c_str(),
669                                 true, &bfbsfile),
670           true);
671
672   // Verify it, just in case:
673   flatbuffers::Verifier verifier(
674       reinterpret_cast<const uint8_t *>(bfbsfile.c_str()), bfbsfile.length());
675   TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
676
677   // Make sure the schema is what we expect it to be.
678   auto &schema = *reflection::GetSchema(bfbsfile.c_str());
679   auto root_table = schema.root_table();
680   TEST_EQ_STR(root_table->name()->c_str(), "MyGame.Example.Monster");
681   auto fields = root_table->fields();
682   auto hp_field_ptr = fields->LookupByKey("hp");
683   TEST_NOTNULL(hp_field_ptr);
684   auto &hp_field = *hp_field_ptr;
685   TEST_EQ_STR(hp_field.name()->c_str(), "hp");
686   TEST_EQ(hp_field.id(), 2);
687   TEST_EQ(hp_field.type()->base_type(), reflection::Short);
688   auto friendly_field_ptr = fields->LookupByKey("friendly");
689   TEST_NOTNULL(friendly_field_ptr);
690   TEST_NOTNULL(friendly_field_ptr->attributes());
691   TEST_NOTNULL(friendly_field_ptr->attributes()->LookupByKey("priority"));
692
693   // Make sure the table index is what we expect it to be.
694   auto pos_field_ptr = fields->LookupByKey("pos");
695   TEST_NOTNULL(pos_field_ptr);
696   TEST_EQ(pos_field_ptr->type()->base_type(), reflection::Obj);
697   auto pos_table_ptr = schema.objects()->Get(pos_field_ptr->type()->index());
698   TEST_NOTNULL(pos_table_ptr);
699   TEST_EQ_STR(pos_table_ptr->name()->c_str(), "MyGame.Example.Vec3");
700
701   // Now use it to dynamically access a buffer.
702   auto &root = *flatbuffers::GetAnyRoot(flatbuf);
703
704   // Verify the buffer first using reflection based verification
705   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(), flatbuf, length),
706           true);
707
708   auto hp = flatbuffers::GetFieldI<uint16_t>(root, hp_field);
709   TEST_EQ(hp, 80);
710
711   // Rather than needing to know the type, we can also get the value of
712   // any field as an int64_t/double/string, regardless of what it actually is.
713   auto hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
714   TEST_EQ(hp_int64, 80);
715   auto hp_double = flatbuffers::GetAnyFieldF(root, hp_field);
716   TEST_EQ(hp_double, 80.0);
717   auto hp_string = flatbuffers::GetAnyFieldS(root, hp_field, &schema);
718   TEST_EQ_STR(hp_string.c_str(), "80");
719
720   // Get struct field through reflection
721   auto pos_struct = flatbuffers::GetFieldStruct(root, *pos_field_ptr);
722   TEST_NOTNULL(pos_struct);
723   TEST_EQ(flatbuffers::GetAnyFieldF(*pos_struct,
724                                     *pos_table_ptr->fields()->LookupByKey("z")),
725           3.0f);
726
727   auto test3_field = pos_table_ptr->fields()->LookupByKey("test3");
728   auto test3_struct = flatbuffers::GetFieldStruct(*pos_struct, *test3_field);
729   TEST_NOTNULL(test3_struct);
730   auto test3_object = schema.objects()->Get(test3_field->type()->index());
731
732   TEST_EQ(flatbuffers::GetAnyFieldF(*test3_struct,
733                                     *test3_object->fields()->LookupByKey("a")),
734           10);
735
736   // We can also modify it.
737   flatbuffers::SetField<uint16_t>(&root, hp_field, 200);
738   hp = flatbuffers::GetFieldI<uint16_t>(root, hp_field);
739   TEST_EQ(hp, 200);
740
741   // We can also set fields generically:
742   flatbuffers::SetAnyFieldI(&root, hp_field, 300);
743   hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
744   TEST_EQ(hp_int64, 300);
745   flatbuffers::SetAnyFieldF(&root, hp_field, 300.5);
746   hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
747   TEST_EQ(hp_int64, 300);
748   flatbuffers::SetAnyFieldS(&root, hp_field, "300");
749   hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
750   TEST_EQ(hp_int64, 300);
751
752   // Test buffer is valid after the modifications
753   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(), flatbuf, length),
754           true);
755
756   // Reset it, for further tests.
757   flatbuffers::SetField<uint16_t>(&root, hp_field, 80);
758
759   // More advanced functionality: changing the size of items in-line!
760   // First we put the FlatBuffer inside an std::vector.
761   std::vector<uint8_t> resizingbuf(flatbuf, flatbuf + length);
762   // Find the field we want to modify.
763   auto &name_field = *fields->LookupByKey("name");
764   // Get the root.
765   // This time we wrap the result from GetAnyRoot in a smartpointer that
766   // will keep rroot valid as resizingbuf resizes.
767   auto rroot = flatbuffers::piv(
768       flatbuffers::GetAnyRoot(flatbuffers::vector_data(resizingbuf)),
769       resizingbuf);
770   SetString(schema, "totally new string", GetFieldS(**rroot, name_field),
771             &resizingbuf);
772   // Here resizingbuf has changed, but rroot is still valid.
773   TEST_EQ_STR(GetFieldS(**rroot, name_field)->c_str(), "totally new string");
774   // Now lets extend a vector by 100 elements (10 -> 110).
775   auto &inventory_field = *fields->LookupByKey("inventory");
776   auto rinventory = flatbuffers::piv(
777       flatbuffers::GetFieldV<uint8_t>(**rroot, inventory_field), resizingbuf);
778   flatbuffers::ResizeVector<uint8_t>(schema, 110, 50, *rinventory,
779                                      &resizingbuf);
780   // rinventory still valid, so lets read from it.
781   TEST_EQ(rinventory->Get(10), 50);
782
783   // For reflection uses not covered already, there is a more powerful way:
784   // we can simply generate whatever object we want to add/modify in a
785   // FlatBuffer of its own, then add that to an existing FlatBuffer:
786   // As an example, let's add a string to an array of strings.
787   // First, find our field:
788   auto &testarrayofstring_field = *fields->LookupByKey("testarrayofstring");
789   // Find the vector value:
790   auto rtestarrayofstring = flatbuffers::piv(
791       flatbuffers::GetFieldV<flatbuffers::Offset<flatbuffers::String>>(
792           **rroot, testarrayofstring_field),
793       resizingbuf);
794   // It's a vector of 2 strings, to which we add one more, initialized to
795   // offset 0.
796   flatbuffers::ResizeVector<flatbuffers::Offset<flatbuffers::String>>(
797       schema, 3, 0, *rtestarrayofstring, &resizingbuf);
798   // Here we just create a buffer that contans a single string, but this
799   // could also be any complex set of tables and other values.
800   flatbuffers::FlatBufferBuilder stringfbb;
801   stringfbb.Finish(stringfbb.CreateString("hank"));
802   // Add the contents of it to our existing FlatBuffer.
803   // We do this last, so the pointer doesn't get invalidated (since it is
804   // at the end of the buffer):
805   auto string_ptr = flatbuffers::AddFlatBuffer(
806       resizingbuf, stringfbb.GetBufferPointer(), stringfbb.GetSize());
807   // Finally, set the new value in the vector.
808   rtestarrayofstring->MutateOffset(2, string_ptr);
809   TEST_EQ_STR(rtestarrayofstring->Get(0)->c_str(), "bob");
810   TEST_EQ_STR(rtestarrayofstring->Get(2)->c_str(), "hank");
811   // Test integrity of all resize operations above.
812   flatbuffers::Verifier resize_verifier(
813       reinterpret_cast<const uint8_t *>(flatbuffers::vector_data(resizingbuf)),
814       resizingbuf.size());
815   TEST_EQ(VerifyMonsterBuffer(resize_verifier), true);
816
817   // Test buffer is valid using reflection as well
818   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(),
819                               flatbuffers::vector_data(resizingbuf),
820                               resizingbuf.size()),
821           true);
822
823   // As an additional test, also set it on the name field.
824   // Note: unlike the name change above, this just overwrites the offset,
825   // rather than changing the string in-place.
826   SetFieldT(*rroot, name_field, string_ptr);
827   TEST_EQ_STR(GetFieldS(**rroot, name_field)->c_str(), "hank");
828
829   // Using reflection, rather than mutating binary FlatBuffers, we can also copy
830   // tables and other things out of other FlatBuffers into a FlatBufferBuilder,
831   // either part or whole.
832   flatbuffers::FlatBufferBuilder fbb;
833   auto root_offset = flatbuffers::CopyTable(
834       fbb, schema, *root_table, *flatbuffers::GetAnyRoot(flatbuf), true);
835   fbb.Finish(root_offset, MonsterIdentifier());
836   // Test that it was copied correctly:
837   AccessFlatBufferTest(fbb.GetBufferPointer(), fbb.GetSize());
838
839   // Test buffer is valid using reflection as well
840   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(),
841                               fbb.GetBufferPointer(), fbb.GetSize()),
842           true);
843 }
844
845 void MiniReflectFlatBuffersTest(uint8_t *flatbuf) {
846   auto s = flatbuffers::FlatBufferToString(flatbuf, Monster::MiniReflectTypeTable());
847   TEST_EQ_STR(
848       s.c_str(),
849       "{ "
850       "pos: { x: 1.0, y: 2.0, z: 3.0, test1: 0.0, test2: Red, test3: "
851       "{ a: 10, b: 20 } }, "
852       "hp: 80, "
853       "name: \"MyMonster\", "
854       "inventory: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "
855       "test_type: Monster, "
856       "test: { name: \"Fred\" }, "
857       "test4: [ { a: 10, b: 20 }, { a: 30, b: 40 } ], "
858       "testarrayofstring: [ \"bob\", \"fred\", \"bob\", \"fred\" ], "
859       "testarrayoftables: [ { hp: 1000, name: \"Barney\" }, { name: \"Fred\" "
860       "}, "
861       "{ name: \"Wilma\" } ], "
862       // TODO(wvo): should really print this nested buffer correctly.
863       "testnestedflatbuffer: [ 20, 0, 0, 0, 77, 79, 78, 83, 12, 0, 12, 0, 0, "
864       "0, "
865       "4, 0, 6, 0, 8, 0, 12, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 13, 0, 0, 0, 78, "
866       "101, 115, 116, 101, 100, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0 ], "
867       "testarrayofstring2: [ \"jane\", \"mary\" ], "
868       "testarrayofsortedstruct: [ { id: 1, distance: 10 }, "
869       "{ id: 2, distance: 20 }, { id: 3, distance: 30 }, "
870       "{ id: 4, distance: 40 } ], "
871       "flex: [ 210, 4, 5, 2 ], "
872       "test5: [ { a: 10, b: 20 }, { a: 30, b: 40 } ], "
873       "vector_of_enums: [ Blue, Green ] "
874       "}");
875 }
876
877 // Parse a .proto schema, output as .fbs
878 void ParseProtoTest() {
879   // load the .proto and the golden file from disk
880   std::string protofile;
881   std::string goldenfile;
882   std::string goldenunionfile;
883   TEST_EQ(
884       flatbuffers::LoadFile((test_data_path + "prototest/test.proto").c_str(),
885                             false, &protofile),
886       true);
887   TEST_EQ(
888       flatbuffers::LoadFile((test_data_path + "prototest/test.golden").c_str(),
889                             false, &goldenfile),
890       true);
891   TEST_EQ(
892       flatbuffers::LoadFile((test_data_path +
893                             "prototest/test_union.golden").c_str(),
894                             false, &goldenunionfile),
895       true);
896
897   flatbuffers::IDLOptions opts;
898   opts.include_dependence_headers = false;
899   opts.proto_mode = true;
900
901   // Parse proto.
902   flatbuffers::Parser parser(opts);
903   auto protopath = test_data_path + "prototest/";
904   const char *include_directories[] = { protopath.c_str(), nullptr };
905   TEST_EQ(parser.Parse(protofile.c_str(), include_directories), true);
906
907   // Generate fbs.
908   auto fbs = flatbuffers::GenerateFBS(parser, "test");
909
910   // Ensure generated file is parsable.
911   flatbuffers::Parser parser2;
912   TEST_EQ(parser2.Parse(fbs.c_str(), nullptr), true);
913   TEST_EQ_STR(fbs.c_str(), goldenfile.c_str());
914
915   // Parse proto with --oneof-union option.
916   opts.proto_oneof_union = true;
917   flatbuffers::Parser parser3(opts);
918   TEST_EQ(parser3.Parse(protofile.c_str(), include_directories), true);
919
920   // Generate fbs.
921   auto fbs_union = flatbuffers::GenerateFBS(parser3, "test");
922
923   // Ensure generated file is parsable.
924   flatbuffers::Parser parser4;
925   TEST_EQ(parser4.Parse(fbs_union.c_str(), nullptr), true);
926   TEST_EQ_STR(fbs_union.c_str(), goldenunionfile.c_str());
927 }
928
929 template<typename T>
930 void CompareTableFieldValue(flatbuffers::Table *table,
931                             flatbuffers::voffset_t voffset, T val) {
932   T read = table->GetField(voffset, static_cast<T>(0));
933   TEST_EQ(read, val);
934 }
935
936 // Low level stress/fuzz test: serialize/deserialize a variety of
937 // different kinds of data in different combinations
938 void FuzzTest1() {
939   // Values we're testing against: chosen to ensure no bits get chopped
940   // off anywhere, and also be different from eachother.
941   const uint8_t bool_val = true;
942   const int8_t char_val = -127;  // 0x81
943   const uint8_t uchar_val = 0xFF;
944   const int16_t short_val = -32222;  // 0x8222;
945   const uint16_t ushort_val = 0xFEEE;
946   const int32_t int_val = 0x83333333;
947   const uint32_t uint_val = 0xFDDDDDDD;
948   const int64_t long_val = 0x8444444444444444LL;
949   const uint64_t ulong_val = 0xFCCCCCCCCCCCCCCCULL;
950   const float float_val = 3.14159f;
951   const double double_val = 3.14159265359;
952
953   const int test_values_max = 11;
954   const flatbuffers::voffset_t fields_per_object = 4;
955   const int num_fuzz_objects = 10000;  // The higher, the more thorough :)
956
957   flatbuffers::FlatBufferBuilder builder;
958
959   lcg_reset();  // Keep it deterministic.
960
961   flatbuffers::uoffset_t objects[num_fuzz_objects];
962
963   // Generate num_fuzz_objects random objects each consisting of
964   // fields_per_object fields, each of a random type.
965   for (int i = 0; i < num_fuzz_objects; i++) {
966     auto start = builder.StartTable();
967     for (flatbuffers::voffset_t f = 0; f < fields_per_object; f++) {
968       int choice = lcg_rand() % test_values_max;
969       auto off = flatbuffers::FieldIndexToOffset(f);
970       switch (choice) {
971         case 0: builder.AddElement<uint8_t>(off, bool_val, 0); break;
972         case 1: builder.AddElement<int8_t>(off, char_val, 0); break;
973         case 2: builder.AddElement<uint8_t>(off, uchar_val, 0); break;
974         case 3: builder.AddElement<int16_t>(off, short_val, 0); break;
975         case 4: builder.AddElement<uint16_t>(off, ushort_val, 0); break;
976         case 5: builder.AddElement<int32_t>(off, int_val, 0); break;
977         case 6: builder.AddElement<uint32_t>(off, uint_val, 0); break;
978         case 7: builder.AddElement<int64_t>(off, long_val, 0); break;
979         case 8: builder.AddElement<uint64_t>(off, ulong_val, 0); break;
980         case 9: builder.AddElement<float>(off, float_val, 0); break;
981         case 10: builder.AddElement<double>(off, double_val, 0); break;
982       }
983     }
984     objects[i] = builder.EndTable(start);
985   }
986   builder.PreAlign<flatbuffers::largest_scalar_t>(0);  // Align whole buffer.
987
988   lcg_reset();  // Reset.
989
990   uint8_t *eob = builder.GetCurrentBufferPointer() + builder.GetSize();
991
992   // Test that all objects we generated are readable and return the
993   // expected values. We generate random objects in the same order
994   // so this is deterministic.
995   for (int i = 0; i < num_fuzz_objects; i++) {
996     auto table = reinterpret_cast<flatbuffers::Table *>(eob - objects[i]);
997     for (flatbuffers::voffset_t f = 0; f < fields_per_object; f++) {
998       int choice = lcg_rand() % test_values_max;
999       flatbuffers::voffset_t off = flatbuffers::FieldIndexToOffset(f);
1000       switch (choice) {
1001         case 0: CompareTableFieldValue(table, off, bool_val); break;
1002         case 1: CompareTableFieldValue(table, off, char_val); break;
1003         case 2: CompareTableFieldValue(table, off, uchar_val); break;
1004         case 3: CompareTableFieldValue(table, off, short_val); break;
1005         case 4: CompareTableFieldValue(table, off, ushort_val); break;
1006         case 5: CompareTableFieldValue(table, off, int_val); break;
1007         case 6: CompareTableFieldValue(table, off, uint_val); break;
1008         case 7: CompareTableFieldValue(table, off, long_val); break;
1009         case 8: CompareTableFieldValue(table, off, ulong_val); break;
1010         case 9: CompareTableFieldValue(table, off, float_val); break;
1011         case 10: CompareTableFieldValue(table, off, double_val); break;
1012       }
1013     }
1014   }
1015 }
1016
1017 // High level stress/fuzz test: generate a big schema and
1018 // matching json data in random combinations, then parse both,
1019 // generate json back from the binary, and compare with the original.
1020 void FuzzTest2() {
1021   lcg_reset();  // Keep it deterministic.
1022
1023   const int num_definitions = 30;
1024   const int num_struct_definitions = 5;  // Subset of num_definitions.
1025   const int fields_per_definition = 15;
1026   const int instances_per_definition = 5;
1027   const int deprecation_rate = 10;  // 1 in deprecation_rate fields will
1028                                     // be deprecated.
1029
1030   std::string schema = "namespace test;\n\n";
1031
1032   struct RndDef {
1033     std::string instances[instances_per_definition];
1034
1035     // Since we're generating schema and corresponding data in tandem,
1036     // this convenience function adds strings to both at once.
1037     static void Add(RndDef (&definitions_l)[num_definitions],
1038                     std::string &schema_l, const int instances_per_definition_l,
1039                     const char *schema_add, const char *instance_add,
1040                     int definition) {
1041       schema_l += schema_add;
1042       for (int i = 0; i < instances_per_definition_l; i++)
1043         definitions_l[definition].instances[i] += instance_add;
1044     }
1045   };
1046
1047   // clang-format off
1048   #define AddToSchemaAndInstances(schema_add, instance_add) \
1049     RndDef::Add(definitions, schema, instances_per_definition, \
1050                 schema_add, instance_add, definition)
1051
1052   #define Dummy() \
1053     RndDef::Add(definitions, schema, instances_per_definition, \
1054                 "byte", "1", definition)
1055   // clang-format on
1056
1057   RndDef definitions[num_definitions];
1058
1059   // We are going to generate num_definitions, the first
1060   // num_struct_definitions will be structs, the rest tables. For each
1061   // generate random fields, some of which may be struct/table types
1062   // referring to previously generated structs/tables.
1063   // Simultanenously, we generate instances_per_definition JSON data
1064   // definitions, which will have identical structure to the schema
1065   // being generated. We generate multiple instances such that when creating
1066   // hierarchy, we get some variety by picking one randomly.
1067   for (int definition = 0; definition < num_definitions; definition++) {
1068     std::string definition_name = "D" + flatbuffers::NumToString(definition);
1069
1070     bool is_struct = definition < num_struct_definitions;
1071
1072     AddToSchemaAndInstances(
1073         ((is_struct ? "struct " : "table ") + definition_name + " {\n").c_str(),
1074         "{\n");
1075
1076     for (int field = 0; field < fields_per_definition; field++) {
1077       const bool is_last_field = field == fields_per_definition - 1;
1078
1079       // Deprecate 1 in deprecation_rate fields. Only table fields can be
1080       // deprecated.
1081       // Don't deprecate the last field to avoid dangling commas in JSON.
1082       const bool deprecated =
1083           !is_struct && !is_last_field && (lcg_rand() % deprecation_rate == 0);
1084
1085       std::string field_name = "f" + flatbuffers::NumToString(field);
1086       AddToSchemaAndInstances(("  " + field_name + ":").c_str(),
1087                               deprecated ? "" : (field_name + ": ").c_str());
1088       // Pick random type:
1089       auto base_type = static_cast<flatbuffers::BaseType>(
1090           lcg_rand() % (flatbuffers::BASE_TYPE_UNION + 1));
1091       switch (base_type) {
1092         case flatbuffers::BASE_TYPE_STRING:
1093           if (is_struct) {
1094             Dummy();  // No strings in structs.
1095           } else {
1096             AddToSchemaAndInstances("string", deprecated ? "" : "\"hi\"");
1097           }
1098           break;
1099         case flatbuffers::BASE_TYPE_VECTOR:
1100           if (is_struct) {
1101             Dummy();  // No vectors in structs.
1102           } else {
1103             AddToSchemaAndInstances("[ubyte]",
1104                                     deprecated ? "" : "[\n0,\n1,\n255\n]");
1105           }
1106           break;
1107         case flatbuffers::BASE_TYPE_NONE:
1108         case flatbuffers::BASE_TYPE_UTYPE:
1109         case flatbuffers::BASE_TYPE_STRUCT:
1110         case flatbuffers::BASE_TYPE_UNION:
1111           if (definition) {
1112             // Pick a random previous definition and random data instance of
1113             // that definition.
1114             int defref = lcg_rand() % definition;
1115             int instance = lcg_rand() % instances_per_definition;
1116             AddToSchemaAndInstances(
1117                 ("D" + flatbuffers::NumToString(defref)).c_str(),
1118                 deprecated ? ""
1119                            : definitions[defref].instances[instance].c_str());
1120           } else {
1121             // If this is the first definition, we have no definition we can
1122             // refer to.
1123             Dummy();
1124           }
1125           break;
1126         case flatbuffers::BASE_TYPE_BOOL:
1127           AddToSchemaAndInstances(
1128               "bool", deprecated ? "" : (lcg_rand() % 2 ? "true" : "false"));
1129           break;
1130         default:
1131           // All the scalar types.
1132           schema += flatbuffers::kTypeNames[base_type];
1133
1134           if (!deprecated) {
1135             // We want each instance to use its own random value.
1136             for (int inst = 0; inst < instances_per_definition; inst++)
1137               definitions[definition].instances[inst] +=
1138                   flatbuffers::IsFloat(base_type)
1139                       ? flatbuffers::NumToString<double>(lcg_rand() % 128)
1140                             .c_str()
1141                       : flatbuffers::NumToString<int>(lcg_rand() % 128).c_str();
1142           }
1143       }
1144       AddToSchemaAndInstances(deprecated ? "(deprecated);\n" : ";\n",
1145                               deprecated ? "" : is_last_field ? "\n" : ",\n");
1146     }
1147     AddToSchemaAndInstances("}\n\n", "}");
1148   }
1149
1150   schema += "root_type D" + flatbuffers::NumToString(num_definitions - 1);
1151   schema += ";\n";
1152
1153   flatbuffers::Parser parser;
1154
1155   // Will not compare against the original if we don't write defaults
1156   parser.builder_.ForceDefaults(true);
1157
1158   // Parse the schema, parse the generated data, then generate text back
1159   // from the binary and compare against the original.
1160   TEST_EQ(parser.Parse(schema.c_str()), true);
1161
1162   const std::string &json =
1163       definitions[num_definitions - 1].instances[0] + "\n";
1164
1165   TEST_EQ(parser.Parse(json.c_str()), true);
1166
1167   std::string jsongen;
1168   parser.opts.indent_step = 0;
1169   auto result =
1170       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1171   TEST_EQ(result, true);
1172
1173   if (jsongen != json) {
1174     // These strings are larger than a megabyte, so we show the bytes around
1175     // the first bytes that are different rather than the whole string.
1176     size_t len = std::min(json.length(), jsongen.length());
1177     for (size_t i = 0; i < len; i++) {
1178       if (json[i] != jsongen[i]) {
1179         i -= std::min(static_cast<size_t>(10), i);  // show some context;
1180         size_t end = std::min(len, i + 20);
1181         for (; i < end; i++)
1182           TEST_OUTPUT_LINE("at %d: found \"%c\", expected \"%c\"\n",
1183                            static_cast<int>(i), jsongen[i], json[i]);
1184         break;
1185       }
1186     }
1187     TEST_NOTNULL(NULL);
1188   }
1189
1190   // clang-format off
1191   #ifdef FLATBUFFERS_TEST_VERBOSE
1192     TEST_OUTPUT_LINE("%dk schema tested with %dk of json\n",
1193                      static_cast<int>(schema.length() / 1024),
1194                      static_cast<int>(json.length() / 1024));
1195   #endif
1196   // clang-format on
1197 }
1198
1199 // Test that parser errors are actually generated.
1200 void TestError_(const char *src, const char *error_substr, bool strict_json,
1201                 const char *file, int line, const char *func) {
1202   flatbuffers::IDLOptions opts;
1203   opts.strict_json = strict_json;
1204   flatbuffers::Parser parser(opts);
1205   if (parser.Parse(src)) {
1206     TestFail("true", "false",
1207              ("parser.Parse(\"" + std::string(src) + "\")").c_str(), file, line,
1208              func);
1209   } else if (!strstr(parser.error_.c_str(), error_substr)) {
1210     TestFail(parser.error_.c_str(), error_substr,
1211              ("parser.Parse(\"" + std::string(src) + "\")").c_str(), file, line,
1212              func);
1213   }
1214 }
1215
1216 void TestError_(const char *src, const char *error_substr, const char *file,
1217                 int line, const char *func) {
1218   TestError_(src, error_substr, false, file, line, func);
1219 }
1220
1221 #ifdef WIN32
1222 #  define TestError(src, ...) \
1223     TestError_(src, __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__)
1224 #else
1225 #  define TestError(src, ...) \
1226     TestError_(src, __VA_ARGS__, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1227 #endif
1228
1229 // Test that parsing errors occur as we'd expect.
1230 // Also useful for coverage, making sure these paths are run.
1231 void ErrorTest() {
1232   // In order they appear in idl_parser.cpp
1233   TestError("table X { Y:byte; } root_type X; { Y: 999 }", "does not fit");
1234   TestError("\"\0", "illegal");
1235   TestError("\"\\q", "escape code");
1236   TestError("table ///", "documentation");
1237   TestError("@", "illegal");
1238   TestError("table 1", "expecting");
1239   TestError("table X { Y:[[int]]; }", "nested vector");
1240   TestError("table X { Y:1; }", "illegal type");
1241   TestError("table X { Y:int; Y:int; }", "field already");
1242   TestError("table Y {} table X { Y:int; }", "same as table");
1243   TestError("struct X { Y:string; }", "only scalar");
1244   TestError("table X { Y:string = \"\"; }", "default values");
1245   TestError("enum Y:byte { Z = 1 } table X { y:Y; }", "not part of enum");
1246   TestError("struct X { Y:int (deprecated); }", "deprecate");
1247   TestError("union Z { X } table X { Y:Z; } root_type X; { Y: {}, A:1 }",
1248             "missing type field");
1249   TestError("union Z { X } table X { Y:Z; } root_type X; { Y_type: 99, Y: {",
1250             "type id");
1251   TestError("table X { Y:int; } root_type X; { Z:", "unknown field");
1252   TestError("table X { Y:int; } root_type X; { Y:", "string constant", true);
1253   TestError("table X { Y:int; } root_type X; { \"Y\":1, }", "string constant",
1254             true);
1255   TestError(
1256       "struct X { Y:int; Z:int; } table W { V:X; } root_type W; "
1257       "{ V:{ Y:1 } }",
1258       "wrong number");
1259   TestError("enum E:byte { A } table X { Y:E; } root_type X; { Y:U }",
1260             "unknown enum value");
1261   TestError("table X { Y:byte; } root_type X; { Y:; }", "starting");
1262   TestError("enum X:byte { Y } enum X {", "enum already");
1263   TestError("enum X:float {}", "underlying");
1264   TestError("enum X:byte { Y, Y }", "value already");
1265   TestError("enum X:byte { Y=2, Z=1 }", "ascending");
1266   TestError("enum X:byte (bit_flags) { Y=8 }", "bit flag out");
1267   TestError("table X { Y:int; } table X {", "datatype already");
1268   TestError("struct X (force_align: 7) { Y:int; }", "force_align");
1269   TestError("struct X {}", "size 0");
1270   TestError("{}", "no root");
1271   TestError("table X { Y:byte; } root_type X; { Y:1 } { Y:1 }", "end of file");
1272   TestError("table X { Y:byte; } root_type X; { Y:1 } table Y{ Z:int }",
1273             "end of file");
1274   TestError("root_type X;", "unknown root");
1275   TestError("struct X { Y:int; } root_type X;", "a table");
1276   TestError("union X { Y }", "referenced");
1277   TestError("union Z { X } struct X { Y:int; }", "only tables");
1278   TestError("table X { Y:[int]; YLength:int; }", "clash");
1279   TestError("table X { Y:byte; } root_type X; { Y:1, Y:2 }", "more than once");
1280   // float to integer conversion is forbidden
1281   TestError("table X { Y:int; } root_type X; { Y:1.0 }", "float");
1282   TestError("table X { Y:bool; } root_type X; { Y:1.0 }", "float");
1283   TestError("enum X:bool { Y = true }", "must be integral");
1284 }
1285
1286 template<typename T> T TestValue(const char *json, const char *type_name) {
1287   flatbuffers::Parser parser;
1288   parser.builder_.ForceDefaults(true);  // return defaults
1289   auto check_default = json ? false : true;
1290   if (check_default) { parser.opts.output_default_scalars_in_json = true; }
1291   // Simple schema.
1292   std::string schema =
1293       "table X { Y:" + std::string(type_name) + "; } root_type X;";
1294   TEST_EQ(parser.Parse(schema.c_str()), true);
1295
1296   auto done = parser.Parse(check_default ? "{}" : json);
1297   TEST_EQ_STR(parser.error_.c_str(), "");
1298   TEST_EQ(done, true);
1299
1300   // Check with print.
1301   std::string print_back;
1302   parser.opts.indent_step = -1;
1303   TEST_EQ(GenerateText(parser, parser.builder_.GetBufferPointer(), &print_back),
1304           true);
1305   // restore value from its default
1306   if (check_default) { TEST_EQ(parser.Parse(print_back.c_str()), true); }
1307
1308   auto root = flatbuffers::GetRoot<flatbuffers::Table>(
1309       parser.builder_.GetBufferPointer());
1310   return root->GetField<T>(flatbuffers::FieldIndexToOffset(0), 0);
1311 }
1312
1313 bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; }
1314
1315 // Additional parser testing not covered elsewhere.
1316 void ValueTest() {
1317   // Test scientific notation numbers.
1318   TEST_EQ(FloatCompare(TestValue<float>("{ Y:0.0314159e+2 }", "float"),
1319                        3.14159f),
1320           true);
1321   // number in string
1322   TEST_EQ(FloatCompare(TestValue<float>("{ Y:\"0.0314159e+2\" }", "float"),
1323                        3.14159f),
1324           true);
1325
1326   // Test conversion functions.
1327   TEST_EQ(FloatCompare(TestValue<float>("{ Y:cos(rad(180)) }", "float"), -1),
1328           true);
1329
1330   // int embedded to string
1331   TEST_EQ(TestValue<int>("{ Y:\"-876\" }", "int=-123"), -876);
1332   TEST_EQ(TestValue<int>("{ Y:\"876\" }", "int=-123"), 876);
1333
1334   // Test negative hex constant.
1335   TEST_EQ(TestValue<int>("{ Y:-0x8ea0 }", "int=-0x8ea0"), -36512);
1336   TEST_EQ(TestValue<int>(nullptr, "int=-0x8ea0"), -36512);
1337
1338   // positive hex constant
1339   TEST_EQ(TestValue<int>("{ Y:0x1abcdef }", "int=0x1"), 0x1abcdef);
1340   // with optional '+' sign
1341   TEST_EQ(TestValue<int>("{ Y:+0x1abcdef }", "int=+0x1"), 0x1abcdef);
1342   // hex in string
1343   TEST_EQ(TestValue<int>("{ Y:\"0x1abcdef\" }", "int=+0x1"), 0x1abcdef);
1344
1345   // Make sure we do unsigned 64bit correctly.
1346   TEST_EQ(TestValue<uint64_t>("{ Y:12335089644688340133 }", "ulong"),
1347           12335089644688340133ULL);
1348
1349   // bool in string
1350   TEST_EQ(TestValue<bool>("{ Y:\"false\" }", "bool=true"), false);
1351   TEST_EQ(TestValue<bool>("{ Y:\"true\" }", "bool=\"true\""), true);
1352   TEST_EQ(TestValue<bool>("{ Y:'false' }", "bool=true"), false);
1353   TEST_EQ(TestValue<bool>("{ Y:'true' }", "bool=\"true\""), true);
1354
1355   // check comments before and after json object
1356   TEST_EQ(TestValue<int>("/*before*/ { Y:1 } /*after*/", "int"), 1);
1357   TEST_EQ(TestValue<int>("//before \n { Y:1 } //after", "int"), 1);
1358
1359 }
1360
1361 void NestedListTest() {
1362   flatbuffers::Parser parser1;
1363   TEST_EQ(parser1.Parse("struct Test { a:short; b:byte; } table T { F:[Test]; }"
1364                         "root_type T;"
1365                         "{ F:[ [10,20], [30,40]] }"),
1366           true);
1367 }
1368
1369 void EnumStringsTest() {
1370   flatbuffers::Parser parser1;
1371   TEST_EQ(parser1.Parse("enum E:byte { A, B, C } table T { F:[E]; }"
1372                         "root_type T;"
1373                         "{ F:[ A, B, \"C\", \"A B C\" ] }"),
1374           true);
1375   flatbuffers::Parser parser2;
1376   TEST_EQ(parser2.Parse("enum E:byte { A, B, C } table T { F:[int]; }"
1377                         "root_type T;"
1378                         "{ F:[ \"E.C\", \"E.A E.B E.C\" ] }"),
1379           true);
1380 }
1381
1382 void EnumNamesTest() {
1383   TEST_EQ_STR("Red", EnumNameColor(Color_Red));
1384   TEST_EQ_STR("Green", EnumNameColor(Color_Green));
1385   TEST_EQ_STR("Blue", EnumNameColor(Color_Blue));
1386   // Check that Color to string don't crash while decode a mixture of Colors.
1387   // 1) Example::Color enum is enum with unfixed underlying type.
1388   // 2) Valid enum range: [0; 2^(ceil(log2(Color_ANY))) - 1].
1389   // Consequence: A value is out of this range will lead to UB (since C++17).
1390   // For details see C++17 standard or explanation on the SO:
1391   // stackoverflow.com/questions/18195312/what-happens-if-you-static-cast-invalid-value-to-enum-class
1392   TEST_EQ_STR("", EnumNameColor(static_cast<Color>(0)));
1393   TEST_EQ_STR("", EnumNameColor(static_cast<Color>(Color_ANY-1)));
1394   TEST_EQ_STR("", EnumNameColor(static_cast<Color>(Color_ANY+1)));
1395 }
1396
1397 void EnumOutOfRangeTest() {
1398   TestError("enum X:byte { Y = 128 }", "enum value does not fit");
1399   TestError("enum X:byte { Y = -129 }", "enum value does not fit");
1400   TestError("enum X:byte { Y = 127, Z }", "enum value does not fit");
1401   TestError("enum X:ubyte { Y = -1 }", "enum value does not fit");
1402   TestError("enum X:ubyte { Y = 256 }", "enum value does not fit");
1403   // Unions begin with an implicit "NONE = 0".
1404   TestError("table Y{} union X { Y = -1 }",
1405             "enum values must be specified in ascending order");
1406   TestError("table Y{} union X { Y = 256 }", "enum value does not fit");
1407   TestError("table Y{} union X { Y = 255, Z:Y }", "enum value does not fit");
1408   TestError("enum X:int { Y = -2147483649 }", "enum value does not fit");
1409   TestError("enum X:int { Y = 2147483648 }", "enum value does not fit");
1410   TestError("enum X:uint { Y = -1 }", "enum value does not fit");
1411   TestError("enum X:uint { Y = 4294967297 }", "enum value does not fit");
1412   TestError("enum X:long { Y = 9223372036854775808 }", "constant does not fit");
1413   TestError("enum X:long { Y = 9223372036854775807, Z }", "enum value overflows");
1414   TestError("enum X:ulong { Y = -1 }", "enum value does not fit");
1415   // TODO: these are perfectly valid constants that shouldn't fail
1416   TestError("enum X:ulong { Y = 13835058055282163712 }", "constant does not fit");
1417   TestError("enum X:ulong { Y = 18446744073709551615 }", "constant does not fit");
1418 }
1419
1420 void IntegerOutOfRangeTest() {
1421   TestError("table T { F:byte; } root_type T; { F:128 }",
1422             "constant does not fit");
1423   TestError("table T { F:byte; } root_type T; { F:-129 }",
1424             "constant does not fit");
1425   TestError("table T { F:ubyte; } root_type T; { F:256 }",
1426             "constant does not fit");
1427   TestError("table T { F:ubyte; } root_type T; { F:-1 }",
1428             "constant does not fit");
1429   TestError("table T { F:short; } root_type T; { F:32768 }",
1430             "constant does not fit");
1431   TestError("table T { F:short; } root_type T; { F:-32769 }",
1432             "constant does not fit");
1433   TestError("table T { F:ushort; } root_type T; { F:65536 }",
1434             "constant does not fit");
1435   TestError("table T { F:ushort; } root_type T; { F:-1 }",
1436             "constant does not fit");
1437   TestError("table T { F:int; } root_type T; { F:2147483648 }",
1438             "constant does not fit");
1439   TestError("table T { F:int; } root_type T; { F:-2147483649 }",
1440             "constant does not fit");
1441   TestError("table T { F:uint; } root_type T; { F:4294967296 }",
1442             "constant does not fit");
1443   TestError("table T { F:uint; } root_type T; { F:-1 }",
1444             "constant does not fit");
1445   // Check fixed width aliases
1446   TestError("table X { Y:uint8; } root_type X; { Y: -1 }", "does not fit");
1447   TestError("table X { Y:uint8; } root_type X; { Y: 256 }", "does not fit");
1448   TestError("table X { Y:uint16; } root_type X; { Y: -1 }", "does not fit");
1449   TestError("table X { Y:uint16; } root_type X; { Y: 65536 }", "does not fit");
1450   TestError("table X { Y:uint32; } root_type X; { Y: -1 }", "");
1451   TestError("table X { Y:uint32; } root_type X; { Y: 4294967296 }",
1452             "does not fit");
1453   TestError("table X { Y:uint64; } root_type X; { Y: -1 }", "");
1454   TestError("table X { Y:uint64; } root_type X; { Y: -9223372036854775809 }",
1455             "does not fit");
1456   TestError("table X { Y:uint64; } root_type X; { Y: 18446744073709551616 }",
1457             "does not fit");
1458
1459   TestError("table X { Y:int8; } root_type X; { Y: -129 }", "does not fit");
1460   TestError("table X { Y:int8; } root_type X; { Y: 128 }", "does not fit");
1461   TestError("table X { Y:int16; } root_type X; { Y: -32769 }", "does not fit");
1462   TestError("table X { Y:int16; } root_type X; { Y: 32768 }", "does not fit");
1463   TestError("table X { Y:int32; } root_type X; { Y: -2147483649 }", "");
1464   TestError("table X { Y:int32; } root_type X; { Y: 2147483648 }",
1465             "does not fit");
1466   TestError("table X { Y:int64; } root_type X; { Y: -9223372036854775809 }",
1467             "does not fit");
1468   TestError("table X { Y:int64; } root_type X; { Y: 9223372036854775808 }",
1469             "does not fit");
1470   // check out-of-int64 as int8
1471   TestError("table X { Y:int8; } root_type X; { Y: -9223372036854775809 }",
1472             "does not fit");
1473   TestError("table X { Y:int8; } root_type X; { Y: 9223372036854775808 }",
1474             "does not fit");
1475
1476   // Check default values
1477   TestError("table X { Y:int64=-9223372036854775809; } root_type X; {}",
1478             "does not fit");
1479   TestError("table X { Y:int64= 9223372036854775808; } root_type X; {}",
1480             "does not fit");
1481   TestError("table X { Y:uint64; } root_type X; { Y: -1 }", "");
1482   TestError("table X { Y:uint64=-9223372036854775809; } root_type X; {}",
1483             "does not fit");
1484   TestError("table X { Y:uint64= 18446744073709551616; } root_type X; {}",
1485             "does not fit");
1486 }
1487
1488 void IntegerBoundaryTest() {
1489   TEST_EQ(TestValue<int8_t>("{ Y:127 }", "byte"), 127);
1490   TEST_EQ(TestValue<int8_t>("{ Y:-128 }", "byte"), -128);
1491   TEST_EQ(TestValue<uint8_t>("{ Y:255 }", "ubyte"), 255);
1492   TEST_EQ(TestValue<uint8_t>("{ Y:0 }", "ubyte"), 0);
1493   TEST_EQ(TestValue<int16_t>("{ Y:32767 }", "short"), 32767);
1494   TEST_EQ(TestValue<int16_t>("{ Y:-32768 }", "short"), -32768);
1495   TEST_EQ(TestValue<uint16_t>("{ Y:65535 }", "ushort"), 65535);
1496   TEST_EQ(TestValue<uint16_t>("{ Y:0 }", "ushort"), 0);
1497   TEST_EQ(TestValue<int32_t>("{ Y:2147483647 }", "int"), 2147483647);
1498   TEST_EQ(TestValue<int32_t>("{ Y:-2147483648 }", "int"), (-2147483647 - 1));
1499   TEST_EQ(TestValue<uint32_t>("{ Y:4294967295 }", "uint"), 4294967295);
1500   TEST_EQ(TestValue<uint32_t>("{ Y:0 }", "uint"), 0);
1501   TEST_EQ(TestValue<int64_t>("{ Y:9223372036854775807 }", "long"),
1502           9223372036854775807);
1503   TEST_EQ(TestValue<int64_t>("{ Y:-9223372036854775808 }", "long"),
1504           (-9223372036854775807 - 1));
1505   TEST_EQ(TestValue<uint64_t>("{ Y:18446744073709551615 }", "ulong"),
1506           18446744073709551615U);
1507   TEST_EQ(TestValue<uint64_t>("{ Y:0 }", "ulong"), 0);
1508   TEST_EQ(TestValue<uint64_t>("{ Y: 18446744073709551615 }", "uint64"),
1509           18446744073709551615ULL);
1510   // check that the default works
1511   TEST_EQ(TestValue<uint64_t>(nullptr, "uint64 = 18446744073709551615"),
1512           18446744073709551615ULL);
1513 }
1514
1515 void ValidFloatTest() {
1516   const auto infinityf = flatbuffers::numeric_limits<float>::infinity();
1517   const auto infinityd = flatbuffers::numeric_limits<double>::infinity();
1518   // check rounding to infinity
1519   TEST_EQ(TestValue<float>("{ Y:+3.4029e+38 }", "float"), +infinityf);
1520   TEST_EQ(TestValue<float>("{ Y:-3.4029e+38 }", "float"), -infinityf);
1521   TEST_EQ(TestValue<double>("{ Y:+1.7977e+308 }", "double"), +infinityd);
1522   TEST_EQ(TestValue<double>("{ Y:-1.7977e+308 }", "double"), -infinityd);
1523
1524   TEST_EQ(
1525       FloatCompare(TestValue<float>("{ Y:0.0314159e+2 }", "float"), 3.14159f),
1526       true);
1527   // float in string
1528   TEST_EQ(FloatCompare(TestValue<float>("{ Y:\" 0.0314159e+2  \" }", "float"),
1529                        3.14159f),
1530           true);
1531
1532   TEST_EQ(TestValue<float>("{ Y:1 }", "float"), 1.0f);
1533   TEST_EQ(TestValue<float>("{ Y:1.0 }", "float"), 1.0f);
1534   TEST_EQ(TestValue<float>("{ Y:1. }", "float"), 1.0f);
1535   TEST_EQ(TestValue<float>("{ Y:+1. }", "float"), 1.0f);
1536   TEST_EQ(TestValue<float>("{ Y:-1. }", "float"), -1.0f);
1537   TEST_EQ(TestValue<float>("{ Y:1.e0 }", "float"), 1.0f);
1538   TEST_EQ(TestValue<float>("{ Y:1.e+0 }", "float"), 1.0f);
1539   TEST_EQ(TestValue<float>("{ Y:1.e-0 }", "float"), 1.0f);
1540   TEST_EQ(TestValue<float>("{ Y:0.125 }", "float"), 0.125f);
1541   TEST_EQ(TestValue<float>("{ Y:.125 }", "float"), 0.125f);
1542   TEST_EQ(TestValue<float>("{ Y:-.125 }", "float"), -0.125f);
1543   TEST_EQ(TestValue<float>("{ Y:+.125 }", "float"), +0.125f);
1544   TEST_EQ(TestValue<float>("{ Y:5 }", "float"), 5.0f);
1545   TEST_EQ(TestValue<float>("{ Y:\"5\" }", "float"), 5.0f);
1546
1547   #if defined(FLATBUFFERS_HAS_NEW_STRTOD)
1548   // Old MSVC versions may have problem with this check.
1549   // https://www.exploringbinary.com/visual-c-plus-plus-strtod-still-broken/
1550   TEST_EQ(TestValue<double>("{ Y:6.9294956446009195e15 }", "double"),
1551     6929495644600920.0);
1552   // check nan's
1553   TEST_EQ(std::isnan(TestValue<double>("{ Y:nan }", "double")), true);
1554   TEST_EQ(std::isnan(TestValue<float>("{ Y:nan }", "float")), true);
1555   TEST_EQ(std::isnan(TestValue<float>("{ Y:\"nan\" }", "float")), true);
1556   TEST_EQ(std::isnan(TestValue<float>("{ Y:+nan }", "float")), true);
1557   TEST_EQ(std::isnan(TestValue<float>("{ Y:-nan }", "float")), true);
1558   TEST_EQ(std::isnan(TestValue<float>(nullptr, "float=nan")), true);
1559   TEST_EQ(std::isnan(TestValue<float>(nullptr, "float=-nan")), true);
1560   // check inf
1561   TEST_EQ(TestValue<float>("{ Y:inf }", "float"), infinityf);
1562   TEST_EQ(TestValue<float>("{ Y:\"inf\" }", "float"), infinityf);
1563   TEST_EQ(TestValue<float>("{ Y:+inf }", "float"), infinityf);
1564   TEST_EQ(TestValue<float>("{ Y:-inf }", "float"), -infinityf);
1565   TEST_EQ(TestValue<float>(nullptr, "float=inf"), infinityf);
1566   TEST_EQ(TestValue<float>(nullptr, "float=-inf"), -infinityf);
1567   TestValue<double>(
1568       "{ Y : [0.2, .2, 1.0, -1.0, -2., 2., 1e0, -1e0, 1.0e0, -1.0e0, -3.e2, "
1569       "3.0e2] }",
1570       "[double]");
1571   TestValue<float>(
1572       "{ Y : [0.2, .2, 1.0, -1.0, -2., 2., 1e0, -1e0, 1.0e0, -1.0e0, -3.e2, "
1573       "3.0e2] }",
1574       "[float]");
1575
1576   // Test binary format of float point.
1577   // https://en.cppreference.com/w/cpp/language/floating_literal
1578   // 0x11.12p-1 = (1*16^1 + 2*16^0 + 3*16^-1 + 4*16^-2) * 2^-1 =
1579   TEST_EQ(TestValue<double>("{ Y:0x12.34p-1 }", "double"), 9.1015625);
1580   // hex fraction 1.2 (decimal 1.125) scaled by 2^3, that is 9.0
1581   TEST_EQ(TestValue<float>("{ Y:-0x0.2p0 }", "float"), -0.125f);
1582   TEST_EQ(TestValue<float>("{ Y:-0x.2p1 }", "float"), -0.25f);
1583   TEST_EQ(TestValue<float>("{ Y:0x1.2p3 }", "float"), 9.0f);
1584   TEST_EQ(TestValue<float>("{ Y:0x10.1p0 }", "float"), 16.0625f);
1585   TEST_EQ(TestValue<double>("{ Y:0x1.2p3 }", "double"), 9.0);
1586   TEST_EQ(TestValue<double>("{ Y:0x10.1p0 }", "double"), 16.0625);
1587   TEST_EQ(TestValue<double>("{ Y:0xC.68p+2 }", "double"), 49.625);
1588   TestValue<double>("{ Y : [0x20.4ep1, +0x20.4ep1, -0x20.4ep1] }", "[double]");
1589   TestValue<float>("{ Y : [0x20.4ep1, +0x20.4ep1, -0x20.4ep1] }", "[float]");
1590
1591 #else   // FLATBUFFERS_HAS_NEW_STRTOD
1592   TEST_OUTPUT_LINE("FLATBUFFERS_HAS_NEW_STRTOD tests skipped");
1593 #endif  // FLATBUFFERS_HAS_NEW_STRTOD
1594 }
1595
1596 void InvalidFloatTest() {
1597   auto invalid_msg = "invalid number";
1598   auto comma_msg = "expecting: ,";
1599   TestError("table T { F:float; } root_type T; { F:1,0 }", "");
1600   TestError("table T { F:float; } root_type T; { F:. }", "");
1601   TestError("table T { F:float; } root_type T; { F:- }", invalid_msg);
1602   TestError("table T { F:float; } root_type T; { F:+ }", invalid_msg);
1603   TestError("table T { F:float; } root_type T; { F:-. }", invalid_msg);
1604   TestError("table T { F:float; } root_type T; { F:+. }", invalid_msg);
1605   TestError("table T { F:float; } root_type T; { F:.e }", "");
1606   TestError("table T { F:float; } root_type T; { F:-e }", invalid_msg);
1607   TestError("table T { F:float; } root_type T; { F:+e }", invalid_msg);
1608   TestError("table T { F:float; } root_type T; { F:-.e }", invalid_msg);
1609   TestError("table T { F:float; } root_type T; { F:+.e }", invalid_msg);
1610   TestError("table T { F:float; } root_type T; { F:-e1 }", invalid_msg);
1611   TestError("table T { F:float; } root_type T; { F:+e1 }", invalid_msg);
1612   TestError("table T { F:float; } root_type T; { F:1.0e+ }", invalid_msg);
1613   TestError("table T { F:float; } root_type T; { F:1.0e- }", invalid_msg);
1614   // exponent pP is mandatory for hex-float
1615   TestError("table T { F:float; } root_type T; { F:0x0 }", invalid_msg);
1616   TestError("table T { F:float; } root_type T; { F:-0x. }", invalid_msg);
1617   TestError("table T { F:float; } root_type T; { F:0x. }", invalid_msg);
1618   // eE not exponent in hex-float!
1619   TestError("table T { F:float; } root_type T; { F:0x0.0e+ }", invalid_msg);
1620   TestError("table T { F:float; } root_type T; { F:0x0.0e- }", invalid_msg);
1621   TestError("table T { F:float; } root_type T; { F:0x0.0p }", invalid_msg);
1622   TestError("table T { F:float; } root_type T; { F:0x0.0p+ }", invalid_msg);
1623   TestError("table T { F:float; } root_type T; { F:0x0.0p- }", invalid_msg);
1624   TestError("table T { F:float; } root_type T; { F:0x0.0pa1 }", invalid_msg);
1625   TestError("table T { F:float; } root_type T; { F:0x0.0e+ }", invalid_msg);
1626   TestError("table T { F:float; } root_type T; { F:0x0.0e- }", invalid_msg);
1627   TestError("table T { F:float; } root_type T; { F:0x0.0e+0 }", invalid_msg);
1628   TestError("table T { F:float; } root_type T; { F:0x0.0e-0 }", invalid_msg);
1629   TestError("table T { F:float; } root_type T; { F:0x0.0ep+ }", invalid_msg);
1630   TestError("table T { F:float; } root_type T; { F:0x0.0ep- }", invalid_msg);
1631   TestError("table T { F:float; } root_type T; { F:1.2.3 }", invalid_msg);
1632   TestError("table T { F:float; } root_type T; { F:1.2.e3 }", invalid_msg);
1633   TestError("table T { F:float; } root_type T; { F:1.2e.3 }", invalid_msg);
1634   TestError("table T { F:float; } root_type T; { F:1.2e0.3 }", invalid_msg);
1635   TestError("table T { F:float; } root_type T; { F:1.2e3. }", invalid_msg);
1636   TestError("table T { F:float; } root_type T; { F:1.2e3.0 }", invalid_msg);
1637   TestError("table T { F:float; } root_type T; { F:+-1.0 }", invalid_msg);
1638   TestError("table T { F:float; } root_type T; { F:1.0e+-1 }", invalid_msg);
1639   TestError("table T { F:float; } root_type T; { F:\"1.0e+-1\" }", invalid_msg);
1640   TestError("table T { F:float; } root_type T; { F:1.e0e }", comma_msg);
1641   TestError("table T { F:float; } root_type T; { F:0x1.p0e }", comma_msg);
1642   TestError("table T { F:float; } root_type T; { F:\" 0x10 \" }", invalid_msg);
1643   // floats in string
1644   TestError("table T { F:float; } root_type T; { F:\"1,2.\" }", invalid_msg);
1645   TestError("table T { F:float; } root_type T; { F:\"1.2e3.\" }", invalid_msg);
1646   TestError("table T { F:float; } root_type T; { F:\"0x1.p0e\" }", invalid_msg);
1647   TestError("table T { F:float; } root_type T; { F:\"0x1.0\" }", invalid_msg);
1648   TestError("table T { F:float; } root_type T; { F:\" 0x1.0\" }", invalid_msg);
1649   TestError("table T { F:float; } root_type T; { F:\"+ 0\" }", invalid_msg);
1650   // disable escapes for "number-in-string"
1651   TestError("table T { F:float; } root_type T; { F:\"\\f1.2e3.\" }", "invalid");
1652   TestError("table T { F:float; } root_type T; { F:\"\\t1.2e3.\" }", "invalid");
1653   TestError("table T { F:float; } root_type T; { F:\"\\n1.2e3.\" }", "invalid");
1654   TestError("table T { F:float; } root_type T; { F:\"\\r1.2e3.\" }", "invalid");
1655   TestError("table T { F:float; } root_type T; { F:\"4\\x005\" }", "invalid");
1656   TestError("table T { F:float; } root_type T; { F:\"\'12\'\" }", invalid_msg);
1657   // null is not a number constant!
1658   TestError("table T { F:float; } root_type T; { F:\"null\" }", invalid_msg);
1659   TestError("table T { F:float; } root_type T; { F:null }", invalid_msg);
1660 }
1661
1662 template<typename T>
1663 void NumericUtilsTestInteger(const char *lower, const char *upper) {
1664   T x;
1665   TEST_EQ(flatbuffers::StringToNumber("1q", &x), false);
1666   TEST_EQ(x, 0);
1667   TEST_EQ(flatbuffers::StringToNumber(upper, &x), false);
1668   TEST_EQ(x, flatbuffers::numeric_limits<T>::max());
1669   TEST_EQ(flatbuffers::StringToNumber(lower, &x), false);
1670   auto expval = flatbuffers::is_unsigned<T>::value
1671                     ? flatbuffers::numeric_limits<T>::max()
1672                     : flatbuffers::numeric_limits<T>::lowest();
1673   TEST_EQ(x, expval);
1674 }
1675
1676 template<typename T>
1677 void NumericUtilsTestFloat(const char *lower, const char *upper) {
1678   T f;
1679   TEST_EQ(flatbuffers::StringToNumber("", &f), false);
1680   TEST_EQ(flatbuffers::StringToNumber("1q", &f), false);
1681   TEST_EQ(f, 0);
1682   TEST_EQ(flatbuffers::StringToNumber(upper, &f), true);
1683   TEST_EQ(f, +flatbuffers::numeric_limits<T>::infinity());
1684   TEST_EQ(flatbuffers::StringToNumber(lower, &f), true);
1685   TEST_EQ(f, -flatbuffers::numeric_limits<T>::infinity());
1686 }
1687
1688 void NumericUtilsTest() {
1689   NumericUtilsTestInteger<uint64_t>("-1", "18446744073709551616");
1690   NumericUtilsTestInteger<uint8_t>("-1", "256");
1691   NumericUtilsTestInteger<int64_t>("-9223372036854775809",
1692                                    "9223372036854775808");
1693   NumericUtilsTestInteger<int8_t>("-129", "128");
1694   NumericUtilsTestFloat<float>("-3.4029e+38", "+3.4029e+38");
1695   NumericUtilsTestFloat<float>("-1.7977e+308", "+1.7977e+308");
1696 }
1697
1698 void IsAsciiUtilsTest() {
1699   char c = -128;
1700   for (int cnt = 0; cnt < 256; cnt++) {
1701     auto alpha = (('a' <= c) && (c <= 'z')) || (('A' <= c) && (c <= 'Z'));
1702     auto dec = (('0' <= c) && (c <= '9'));
1703     auto hex = (('a' <= c) && (c <= 'f')) || (('A' <= c) && (c <= 'F'));
1704     TEST_EQ(flatbuffers::is_alpha(c), alpha);
1705     TEST_EQ(flatbuffers::is_alnum(c), alpha || dec);
1706     TEST_EQ(flatbuffers::is_digit(c), dec);
1707     TEST_EQ(flatbuffers::is_xdigit(c), dec || hex);
1708     c += 1;
1709   }
1710 }
1711
1712 void UnicodeTest() {
1713   flatbuffers::Parser parser;
1714   // Without setting allow_non_utf8 = true, we treat \x sequences as byte
1715   // sequences which are then validated as UTF-8.
1716   TEST_EQ(parser.Parse("table T { F:string; }"
1717                        "root_type T;"
1718                        "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1719                        "\\u5225\\u30B5\\u30A4\\u30C8\\xE2\\x82\\xAC\\u0080\\uD8"
1720                        "3D\\uDE0E\" }"),
1721           true);
1722   std::string jsongen;
1723   parser.opts.indent_step = -1;
1724   auto result =
1725       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1726   TEST_EQ(result, true);
1727   TEST_EQ_STR(jsongen.c_str(),
1728               "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1729               "\\u5225\\u30B5\\u30A4\\u30C8\\u20AC\\u0080\\uD83D\\uDE0E\"}");
1730 }
1731
1732 void UnicodeTestAllowNonUTF8() {
1733   flatbuffers::Parser parser;
1734   parser.opts.allow_non_utf8 = true;
1735   TEST_EQ(
1736       parser.Parse(
1737           "table T { F:string; }"
1738           "root_type T;"
1739           "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1740           "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\\u0080\\uD83D\\uDE0E\" }"),
1741       true);
1742   std::string jsongen;
1743   parser.opts.indent_step = -1;
1744   auto result =
1745       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1746   TEST_EQ(result, true);
1747   TEST_EQ_STR(
1748       jsongen.c_str(),
1749       "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1750       "\\u5225\\u30B5\\u30A4\\u30C8\\u0001\\x80\\u0080\\uD83D\\uDE0E\"}");
1751 }
1752
1753 void UnicodeTestGenerateTextFailsOnNonUTF8() {
1754   flatbuffers::Parser parser;
1755   // Allow non-UTF-8 initially to model what happens when we load a binary
1756   // flatbuffer from disk which contains non-UTF-8 strings.
1757   parser.opts.allow_non_utf8 = true;
1758   TEST_EQ(
1759       parser.Parse(
1760           "table T { F:string; }"
1761           "root_type T;"
1762           "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1763           "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\\u0080\\uD83D\\uDE0E\" }"),
1764       true);
1765   std::string jsongen;
1766   parser.opts.indent_step = -1;
1767   // Now, disallow non-UTF-8 (the default behavior) so GenerateText indicates
1768   // failure.
1769   parser.opts.allow_non_utf8 = false;
1770   auto result =
1771       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1772   TEST_EQ(result, false);
1773 }
1774
1775 void UnicodeSurrogatesTest() {
1776   flatbuffers::Parser parser;
1777
1778   TEST_EQ(parser.Parse("table T { F:string (id: 0); }"
1779                        "root_type T;"
1780                        "{ F:\"\\uD83D\\uDCA9\"}"),
1781           true);
1782   auto root = flatbuffers::GetRoot<flatbuffers::Table>(
1783       parser.builder_.GetBufferPointer());
1784   auto string = root->GetPointer<flatbuffers::String *>(
1785       flatbuffers::FieldIndexToOffset(0));
1786   TEST_EQ_STR(string->c_str(), "\xF0\x9F\x92\xA9");
1787 }
1788
1789 void UnicodeInvalidSurrogatesTest() {
1790   TestError(
1791       "table T { F:string; }"
1792       "root_type T;"
1793       "{ F:\"\\uD800\"}",
1794       "unpaired high surrogate");
1795   TestError(
1796       "table T { F:string; }"
1797       "root_type T;"
1798       "{ F:\"\\uD800abcd\"}",
1799       "unpaired high surrogate");
1800   TestError(
1801       "table T { F:string; }"
1802       "root_type T;"
1803       "{ F:\"\\uD800\\n\"}",
1804       "unpaired high surrogate");
1805   TestError(
1806       "table T { F:string; }"
1807       "root_type T;"
1808       "{ F:\"\\uD800\\uD800\"}",
1809       "multiple high surrogates");
1810   TestError(
1811       "table T { F:string; }"
1812       "root_type T;"
1813       "{ F:\"\\uDC00\"}",
1814       "unpaired low surrogate");
1815 }
1816
1817 void InvalidUTF8Test() {
1818   // "1 byte" pattern, under min length of 2 bytes
1819   TestError(
1820       "table T { F:string; }"
1821       "root_type T;"
1822       "{ F:\"\x80\"}",
1823       "illegal UTF-8 sequence");
1824   // 2 byte pattern, string too short
1825   TestError(
1826       "table T { F:string; }"
1827       "root_type T;"
1828       "{ F:\"\xDF\"}",
1829       "illegal UTF-8 sequence");
1830   // 3 byte pattern, string too short
1831   TestError(
1832       "table T { F:string; }"
1833       "root_type T;"
1834       "{ F:\"\xEF\xBF\"}",
1835       "illegal UTF-8 sequence");
1836   // 4 byte pattern, string too short
1837   TestError(
1838       "table T { F:string; }"
1839       "root_type T;"
1840       "{ F:\"\xF7\xBF\xBF\"}",
1841       "illegal UTF-8 sequence");
1842   // "5 byte" pattern, string too short
1843   TestError(
1844       "table T { F:string; }"
1845       "root_type T;"
1846       "{ F:\"\xFB\xBF\xBF\xBF\"}",
1847       "illegal UTF-8 sequence");
1848   // "6 byte" pattern, string too short
1849   TestError(
1850       "table T { F:string; }"
1851       "root_type T;"
1852       "{ F:\"\xFD\xBF\xBF\xBF\xBF\"}",
1853       "illegal UTF-8 sequence");
1854   // "7 byte" pattern, string too short
1855   TestError(
1856       "table T { F:string; }"
1857       "root_type T;"
1858       "{ F:\"\xFE\xBF\xBF\xBF\xBF\xBF\"}",
1859       "illegal UTF-8 sequence");
1860   // "5 byte" pattern, over max length of 4 bytes
1861   TestError(
1862       "table T { F:string; }"
1863       "root_type T;"
1864       "{ F:\"\xFB\xBF\xBF\xBF\xBF\"}",
1865       "illegal UTF-8 sequence");
1866   // "6 byte" pattern, over max length of 4 bytes
1867   TestError(
1868       "table T { F:string; }"
1869       "root_type T;"
1870       "{ F:\"\xFD\xBF\xBF\xBF\xBF\xBF\"}",
1871       "illegal UTF-8 sequence");
1872   // "7 byte" pattern, over max length of 4 bytes
1873   TestError(
1874       "table T { F:string; }"
1875       "root_type T;"
1876       "{ F:\"\xFE\xBF\xBF\xBF\xBF\xBF\xBF\"}",
1877       "illegal UTF-8 sequence");
1878
1879   // Three invalid encodings for U+000A (\n, aka NEWLINE)
1880   TestError(
1881       "table T { F:string; }"
1882       "root_type T;"
1883       "{ F:\"\xC0\x8A\"}",
1884       "illegal UTF-8 sequence");
1885   TestError(
1886       "table T { F:string; }"
1887       "root_type T;"
1888       "{ F:\"\xE0\x80\x8A\"}",
1889       "illegal UTF-8 sequence");
1890   TestError(
1891       "table T { F:string; }"
1892       "root_type T;"
1893       "{ F:\"\xF0\x80\x80\x8A\"}",
1894       "illegal UTF-8 sequence");
1895
1896   // Two invalid encodings for U+00A9 (COPYRIGHT SYMBOL)
1897   TestError(
1898       "table T { F:string; }"
1899       "root_type T;"
1900       "{ F:\"\xE0\x81\xA9\"}",
1901       "illegal UTF-8 sequence");
1902   TestError(
1903       "table T { F:string; }"
1904       "root_type T;"
1905       "{ F:\"\xF0\x80\x81\xA9\"}",
1906       "illegal UTF-8 sequence");
1907
1908   // Invalid encoding for U+20AC (EURO SYMBOL)
1909   TestError(
1910       "table T { F:string; }"
1911       "root_type T;"
1912       "{ F:\"\xF0\x82\x82\xAC\"}",
1913       "illegal UTF-8 sequence");
1914
1915   // UTF-16 surrogate values between U+D800 and U+DFFF cannot be encoded in
1916   // UTF-8
1917   TestError(
1918       "table T { F:string; }"
1919       "root_type T;"
1920       // U+10400 "encoded" as U+D801 U+DC00
1921       "{ F:\"\xED\xA0\x81\xED\xB0\x80\"}",
1922       "illegal UTF-8 sequence");
1923
1924   // Check independence of identifier from locale.
1925   std::string locale_ident;
1926   locale_ident += "table T { F";
1927   locale_ident += static_cast<char>(-32); // unsigned 0xE0
1928   locale_ident += " :string; }";
1929   locale_ident += "root_type T;";
1930   locale_ident += "{}";
1931   TestError(locale_ident.c_str(), "");
1932 }
1933
1934 void UnknownFieldsTest() {
1935   flatbuffers::IDLOptions opts;
1936   opts.skip_unexpected_fields_in_json = true;
1937   flatbuffers::Parser parser(opts);
1938
1939   TEST_EQ(parser.Parse("table T { str:string; i:int;}"
1940                        "root_type T;"
1941                        "{ str:\"test\","
1942                        "unknown_string:\"test\","
1943                        "\"unknown_string\":\"test\","
1944                        "unknown_int:10,"
1945                        "unknown_float:1.0,"
1946                        "unknown_array: [ 1, 2, 3, 4],"
1947                        "unknown_object: { i: 10 },"
1948                        "\"unknown_object\": { \"i\": 10 },"
1949                        "i:10}"),
1950           true);
1951
1952   std::string jsongen;
1953   parser.opts.indent_step = -1;
1954   auto result =
1955       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1956   TEST_EQ(result, true);
1957   TEST_EQ_STR(jsongen.c_str(), "{str: \"test\",i: 10}");
1958 }
1959
1960 void ParseUnionTest() {
1961   // Unions must be parseable with the type field following the object.
1962   flatbuffers::Parser parser;
1963   TEST_EQ(parser.Parse("table T { A:int; }"
1964                        "union U { T }"
1965                        "table V { X:U; }"
1966                        "root_type V;"
1967                        "{ X:{ A:1 }, X_type: T }"),
1968           true);
1969   // Unions must be parsable with prefixed namespace.
1970   flatbuffers::Parser parser2;
1971   TEST_EQ(parser2.Parse("namespace N; table A {} namespace; union U { N.A }"
1972                         "table B { e:U; } root_type B;"
1973                         "{ e_type: N_A, e: {} }"),
1974           true);
1975 }
1976
1977 void UnionVectorTest() {
1978   // load FlatBuffer fbs schema.
1979   // TODO: load a JSON file with such a vector when JSON support is ready.
1980   std::string schemafile;
1981   TEST_EQ(flatbuffers::LoadFile(
1982               (test_data_path + "union_vector/union_vector.fbs").c_str(), false,
1983               &schemafile),
1984           true);
1985
1986   // parse schema.
1987   flatbuffers::IDLOptions idl_opts;
1988   idl_opts.lang_to_generate |= flatbuffers::IDLOptions::kCpp;
1989   flatbuffers::Parser parser(idl_opts);
1990   TEST_EQ(parser.Parse(schemafile.c_str()), true);
1991
1992   flatbuffers::FlatBufferBuilder fbb;
1993
1994   // union types.
1995   std::vector<uint8_t> types;
1996   types.push_back(static_cast<uint8_t>(Character_Belle));
1997   types.push_back(static_cast<uint8_t>(Character_MuLan));
1998   types.push_back(static_cast<uint8_t>(Character_BookFan));
1999   types.push_back(static_cast<uint8_t>(Character_Other));
2000   types.push_back(static_cast<uint8_t>(Character_Unused));
2001
2002   // union values.
2003   std::vector<flatbuffers::Offset<void>> characters;
2004   characters.push_back(fbb.CreateStruct(BookReader(/*books_read=*/7)).Union());
2005   characters.push_back(CreateAttacker(fbb, /*sword_attack_damage=*/5).Union());
2006   characters.push_back(fbb.CreateStruct(BookReader(/*books_read=*/2)).Union());
2007   characters.push_back(fbb.CreateString("Other").Union());
2008   characters.push_back(fbb.CreateString("Unused").Union());
2009
2010   // create Movie.
2011   const auto movie_offset =
2012       CreateMovie(fbb, Character_Rapunzel,
2013                   fbb.CreateStruct(Rapunzel(/*hair_length=*/6)).Union(),
2014                   fbb.CreateVector(types), fbb.CreateVector(characters));
2015   FinishMovieBuffer(fbb, movie_offset);
2016   auto buf = fbb.GetBufferPointer();
2017
2018   flatbuffers::Verifier verifier(buf, fbb.GetSize());
2019   TEST_EQ(VerifyMovieBuffer(verifier), true);
2020
2021   auto flat_movie = GetMovie(buf);
2022
2023   auto TestMovie = [](const Movie *movie) {
2024     TEST_EQ(movie->main_character_type() == Character_Rapunzel, true);
2025
2026     auto cts = movie->characters_type();
2027     TEST_EQ(movie->characters_type()->size(), 5);
2028     TEST_EQ(cts->GetEnum<Character>(0) == Character_Belle, true);
2029     TEST_EQ(cts->GetEnum<Character>(1) == Character_MuLan, true);
2030     TEST_EQ(cts->GetEnum<Character>(2) == Character_BookFan, true);
2031     TEST_EQ(cts->GetEnum<Character>(3) == Character_Other, true);
2032     TEST_EQ(cts->GetEnum<Character>(4) == Character_Unused, true);
2033
2034     auto rapunzel = movie->main_character_as_Rapunzel();
2035     TEST_EQ(rapunzel->hair_length(), 6);
2036
2037     auto cs = movie->characters();
2038     TEST_EQ(cs->size(), 5);
2039     auto belle = cs->GetAs<BookReader>(0);
2040     TEST_EQ(belle->books_read(), 7);
2041     auto mu_lan = cs->GetAs<Attacker>(1);
2042     TEST_EQ(mu_lan->sword_attack_damage(), 5);
2043     auto book_fan = cs->GetAs<BookReader>(2);
2044     TEST_EQ(book_fan->books_read(), 2);
2045     auto other = cs->GetAsString(3);
2046     TEST_EQ_STR(other->c_str(), "Other");
2047     auto unused = cs->GetAsString(4);
2048     TEST_EQ_STR(unused->c_str(), "Unused");
2049   };
2050
2051   TestMovie(flat_movie);
2052
2053   auto movie_object = flat_movie->UnPack();
2054   TEST_EQ(movie_object->main_character.AsRapunzel()->hair_length(), 6);
2055   TEST_EQ(movie_object->characters[0].AsBelle()->books_read(), 7);
2056   TEST_EQ(movie_object->characters[1].AsMuLan()->sword_attack_damage, 5);
2057   TEST_EQ(movie_object->characters[2].AsBookFan()->books_read(), 2);
2058   TEST_EQ_STR(movie_object->characters[3].AsOther()->c_str(), "Other");
2059   TEST_EQ_STR(movie_object->characters[4].AsUnused()->c_str(), "Unused");
2060
2061   fbb.Clear();
2062   fbb.Finish(Movie::Pack(fbb, movie_object));
2063
2064   delete movie_object;
2065
2066   auto repacked_movie = GetMovie(fbb.GetBufferPointer());
2067
2068   TestMovie(repacked_movie);
2069
2070   auto s =
2071       flatbuffers::FlatBufferToString(fbb.GetBufferPointer(), MovieTypeTable());
2072   TEST_EQ_STR(
2073       s.c_str(),
2074       "{ main_character_type: Rapunzel, main_character: { hair_length: 6 }, "
2075       "characters_type: [ Belle, MuLan, BookFan, Other, Unused ], "
2076       "characters: [ { books_read: 7 }, { sword_attack_damage: 5 }, "
2077       "{ books_read: 2 }, \"Other\", \"Unused\" ] }");
2078
2079
2080   flatbuffers::ToStringVisitor visitor("\n", true, "  ");
2081   IterateFlatBuffer(fbb.GetBufferPointer(), MovieTypeTable(), &visitor);
2082   TEST_EQ_STR(
2083       visitor.s.c_str(),
2084       "{\n"
2085       "  \"main_character_type\": \"Rapunzel\",\n"
2086       "  \"main_character\": {\n"
2087       "    \"hair_length\": 6\n"
2088       "  },\n"
2089       "  \"characters_type\": [\n"
2090       "    \"Belle\",\n"
2091       "    \"MuLan\",\n"
2092       "    \"BookFan\",\n"
2093       "    \"Other\",\n"
2094       "    \"Unused\"\n"
2095       "  ],\n"
2096       "  \"characters\": [\n"
2097       "    {\n"
2098       "      \"books_read\": 7\n"
2099       "    },\n"
2100       "    {\n"
2101       "      \"sword_attack_damage\": 5\n"
2102       "    },\n"
2103       "    {\n"
2104       "      \"books_read\": 2\n"
2105       "    },\n"
2106       "    \"Other\",\n"
2107       "    \"Unused\"\n"
2108       "  ]\n"
2109       "}");
2110 }
2111
2112 void ConformTest() {
2113   flatbuffers::Parser parser;
2114   TEST_EQ(parser.Parse("table T { A:int; } enum E:byte { A }"), true);
2115
2116   auto test_conform = [](flatbuffers::Parser &parser1, const char *test,
2117                          const char *expected_err) {
2118     flatbuffers::Parser parser2;
2119     TEST_EQ(parser2.Parse(test), true);
2120     auto err = parser2.ConformTo(parser1);
2121     TEST_NOTNULL(strstr(err.c_str(), expected_err));
2122   };
2123
2124   test_conform(parser, "table T { A:byte; }", "types differ for field");
2125   test_conform(parser, "table T { B:int; A:int; }", "offsets differ for field");
2126   test_conform(parser, "table T { A:int = 1; }", "defaults differ for field");
2127   test_conform(parser, "table T { B:float; }",
2128                "field renamed to different type");
2129   test_conform(parser, "enum E:byte { B, A }", "values differ for enum");
2130 }
2131
2132 void ParseProtoBufAsciiTest() {
2133   // We can put the parser in a mode where it will accept JSON that looks more
2134   // like Protobuf ASCII, for users that have data in that format.
2135   // This uses no "" for field names (which we already support by default,
2136   // omits `,`, `:` before `{` and a couple of other features.
2137   flatbuffers::Parser parser;
2138   parser.opts.protobuf_ascii_alike = true;
2139   TEST_EQ(
2140       parser.Parse("table S { B:int; } table T { A:[int]; C:S; } root_type T;"),
2141       true);
2142   TEST_EQ(parser.Parse("{ A [1 2] C { B:2 }}"), true);
2143   // Similarly, in text output, it should omit these.
2144   std::string text;
2145   auto ok = flatbuffers::GenerateText(
2146       parser, parser.builder_.GetBufferPointer(), &text);
2147   TEST_EQ(ok, true);
2148   TEST_EQ_STR(text.c_str(),
2149               "{\n  A [\n    1\n    2\n  ]\n  C {\n    B: 2\n  }\n}\n");
2150 }
2151
2152 void FlexBuffersTest() {
2153   flexbuffers::Builder slb(512,
2154                            flexbuffers::BUILDER_FLAG_SHARE_KEYS_AND_STRINGS);
2155
2156   // Write the equivalent of:
2157   // { vec: [ -100, "Fred", 4.0, false ], bar: [ 1, 2, 3 ], bar3: [ 1, 2, 3 ],
2158   // foo: 100, bool: true, mymap: { foo: "Fred" } }
2159   // clang-format off
2160   #ifndef FLATBUFFERS_CPP98_STL
2161     // It's possible to do this without std::function support as well.
2162     slb.Map([&]() {
2163        slb.Vector("vec", [&]() {
2164         slb += -100;  // Equivalent to slb.Add(-100) or slb.Int(-100);
2165         slb += "Fred";
2166         slb.IndirectFloat(4.0f);
2167         uint8_t blob[] = { 77 };
2168         slb.Blob(blob, 1);
2169         slb += false;
2170       });
2171       int ints[] = { 1, 2, 3 };
2172       slb.Vector("bar", ints, 3);
2173       slb.FixedTypedVector("bar3", ints, 3);
2174       bool bools[] = {true, false, true, false};
2175       slb.Vector("bools", bools, 4);
2176       slb.Bool("bool", true);
2177       slb.Double("foo", 100);
2178       slb.Map("mymap", [&]() {
2179         slb.String("foo", "Fred");  // Testing key and string reuse.
2180       });
2181     });
2182     slb.Finish();
2183   #else
2184     // It's possible to do this without std::function support as well.
2185     slb.Map([](flexbuffers::Builder& slb2) {
2186        slb2.Vector("vec", [](flexbuffers::Builder& slb3) {
2187         slb3 += -100;  // Equivalent to slb.Add(-100) or slb.Int(-100);
2188         slb3 += "Fred";
2189         slb3.IndirectFloat(4.0f);
2190         uint8_t blob[] = { 77 };
2191         slb3.Blob(blob, 1);
2192         slb3 += false;
2193       }, slb2);
2194       int ints[] = { 1, 2, 3 };
2195       slb2.Vector("bar", ints, 3);
2196       slb2.FixedTypedVector("bar3", ints, 3);
2197       slb2.Bool("bool", true);
2198       slb2.Double("foo", 100);
2199       slb2.Map("mymap", [](flexbuffers::Builder& slb3) {
2200         slb3.String("foo", "Fred");  // Testing key and string reuse.
2201       }, slb2);
2202     }, slb);
2203     slb.Finish();
2204   #endif  // FLATBUFFERS_CPP98_STL
2205
2206   #ifdef FLATBUFFERS_TEST_VERBOSE
2207     for (size_t i = 0; i < slb.GetBuffer().size(); i++)
2208       printf("%d ", flatbuffers::vector_data(slb.GetBuffer())[i]);
2209     printf("\n");
2210   #endif
2211   // clang-format on
2212
2213   auto map = flexbuffers::GetRoot(slb.GetBuffer()).AsMap();
2214   TEST_EQ(map.size(), 7);
2215   auto vec = map["vec"].AsVector();
2216   TEST_EQ(vec.size(), 5);
2217   TEST_EQ(vec[0].AsInt64(), -100);
2218   TEST_EQ_STR(vec[1].AsString().c_str(), "Fred");
2219   TEST_EQ(vec[1].AsInt64(), 0);  // Number parsing failed.
2220   TEST_EQ(vec[2].AsDouble(), 4.0);
2221   TEST_EQ(vec[2].AsString().IsTheEmptyString(), true);  // Wrong Type.
2222   TEST_EQ_STR(vec[2].AsString().c_str(), "");     // This still works though.
2223   TEST_EQ_STR(vec[2].ToString().c_str(), "4.0");  // Or have it converted.
2224
2225   // Few tests for templated version of As.
2226   TEST_EQ(vec[0].As<int64_t>(), -100);
2227   TEST_EQ_STR(vec[1].As<std::string>().c_str(), "Fred");
2228   TEST_EQ(vec[1].As<int64_t>(), 0);  // Number parsing failed.
2229   TEST_EQ(vec[2].As<double>(), 4.0);
2230
2231   // Test that the blob can be accessed.
2232   TEST_EQ(vec[3].IsBlob(), true);
2233   auto blob = vec[3].AsBlob();
2234   TEST_EQ(blob.size(), 1);
2235   TEST_EQ(blob.data()[0], 77);
2236   TEST_EQ(vec[4].IsBool(), true);   // Check if type is a bool
2237   TEST_EQ(vec[4].AsBool(), false);  // Check if value is false
2238   auto tvec = map["bar"].AsTypedVector();
2239   TEST_EQ(tvec.size(), 3);
2240   TEST_EQ(tvec[2].AsInt8(), 3);
2241   auto tvec3 = map["bar3"].AsFixedTypedVector();
2242   TEST_EQ(tvec3.size(), 3);
2243   TEST_EQ(tvec3[2].AsInt8(), 3);
2244   TEST_EQ(map["bool"].AsBool(), true);
2245   auto tvecb = map["bools"].AsTypedVector();
2246   TEST_EQ(tvecb.ElementType(), flexbuffers::FBT_BOOL);
2247   TEST_EQ(map["foo"].AsUInt8(), 100);
2248   TEST_EQ(map["unknown"].IsNull(), true);
2249   auto mymap = map["mymap"].AsMap();
2250   // These should be equal by pointer equality, since key and value are shared.
2251   TEST_EQ(mymap.Keys()[0].AsKey(), map.Keys()[4].AsKey());
2252   TEST_EQ(mymap.Values()[0].AsString().c_str(), vec[1].AsString().c_str());
2253   // We can mutate values in the buffer.
2254   TEST_EQ(vec[0].MutateInt(-99), true);
2255   TEST_EQ(vec[0].AsInt64(), -99);
2256   TEST_EQ(vec[1].MutateString("John"), true);  // Size must match.
2257   TEST_EQ_STR(vec[1].AsString().c_str(), "John");
2258   TEST_EQ(vec[1].MutateString("Alfred"), false);  // Too long.
2259   TEST_EQ(vec[2].MutateFloat(2.0f), true);
2260   TEST_EQ(vec[2].AsFloat(), 2.0f);
2261   TEST_EQ(vec[2].MutateFloat(3.14159), false);  // Double does not fit in float.
2262   TEST_EQ(vec[4].AsBool(), false);              // Is false before change
2263   TEST_EQ(vec[4].MutateBool(true), true);       // Can change a bool
2264   TEST_EQ(vec[4].AsBool(), true);               // Changed bool is now true
2265
2266   // Parse from JSON:
2267   flatbuffers::Parser parser;
2268   slb.Clear();
2269   auto jsontest = "{ a: [ 123, 456.0 ], b: \"hello\", c: true, d: false }";
2270   TEST_EQ(parser.ParseFlexBuffer(jsontest, nullptr, &slb), true);
2271   auto jroot = flexbuffers::GetRoot(slb.GetBuffer());
2272   auto jmap = jroot.AsMap();
2273   auto jvec = jmap["a"].AsVector();
2274   TEST_EQ(jvec[0].AsInt64(), 123);
2275   TEST_EQ(jvec[1].AsDouble(), 456.0);
2276   TEST_EQ_STR(jmap["b"].AsString().c_str(), "hello");
2277   TEST_EQ(jmap["c"].IsBool(), true);   // Parsed correctly to a bool
2278   TEST_EQ(jmap["c"].AsBool(), true);   // Parsed correctly to true
2279   TEST_EQ(jmap["d"].IsBool(), true);   // Parsed correctly to a bool
2280   TEST_EQ(jmap["d"].AsBool(), false);  // Parsed correctly to false
2281   // And from FlexBuffer back to JSON:
2282   auto jsonback = jroot.ToString();
2283   TEST_EQ_STR(jsontest, jsonback.c_str());
2284 }
2285
2286 void TypeAliasesTest() {
2287   flatbuffers::FlatBufferBuilder builder;
2288
2289   builder.Finish(CreateTypeAliases(
2290       builder, flatbuffers::numeric_limits<int8_t>::min(),
2291       flatbuffers::numeric_limits<uint8_t>::max(),
2292       flatbuffers::numeric_limits<int16_t>::min(),
2293       flatbuffers::numeric_limits<uint16_t>::max(),
2294       flatbuffers::numeric_limits<int32_t>::min(),
2295       flatbuffers::numeric_limits<uint32_t>::max(),
2296       flatbuffers::numeric_limits<int64_t>::min(),
2297       flatbuffers::numeric_limits<uint64_t>::max(), 2.3f, 2.3));
2298
2299   auto p = builder.GetBufferPointer();
2300   auto ta = flatbuffers::GetRoot<TypeAliases>(p);
2301
2302   TEST_EQ(ta->i8(), flatbuffers::numeric_limits<int8_t>::min());
2303   TEST_EQ(ta->u8(), flatbuffers::numeric_limits<uint8_t>::max());
2304   TEST_EQ(ta->i16(), flatbuffers::numeric_limits<int16_t>::min());
2305   TEST_EQ(ta->u16(), flatbuffers::numeric_limits<uint16_t>::max());
2306   TEST_EQ(ta->i32(), flatbuffers::numeric_limits<int32_t>::min());
2307   TEST_EQ(ta->u32(), flatbuffers::numeric_limits<uint32_t>::max());
2308   TEST_EQ(ta->i64(), flatbuffers::numeric_limits<int64_t>::min());
2309   TEST_EQ(ta->u64(), flatbuffers::numeric_limits<uint64_t>::max());
2310   TEST_EQ(ta->f32(), 2.3f);
2311   TEST_EQ(ta->f64(), 2.3);
2312   TEST_EQ(sizeof(ta->i8()), 1);
2313   TEST_EQ(sizeof(ta->i16()), 2);
2314   TEST_EQ(sizeof(ta->i32()), 4);
2315   TEST_EQ(sizeof(ta->i64()), 8);
2316   TEST_EQ(sizeof(ta->u8()), 1);
2317   TEST_EQ(sizeof(ta->u16()), 2);
2318   TEST_EQ(sizeof(ta->u32()), 4);
2319   TEST_EQ(sizeof(ta->u64()), 8);
2320   TEST_EQ(sizeof(ta->f32()), 4);
2321   TEST_EQ(sizeof(ta->f64()), 8);
2322 }
2323
2324 void EndianSwapTest() {
2325   TEST_EQ(flatbuffers::EndianSwap(static_cast<int16_t>(0x1234)), 0x3412);
2326   TEST_EQ(flatbuffers::EndianSwap(static_cast<int32_t>(0x12345678)),
2327           0x78563412);
2328   TEST_EQ(flatbuffers::EndianSwap(static_cast<int64_t>(0x1234567890ABCDEF)),
2329           0xEFCDAB9078563412);
2330   TEST_EQ(flatbuffers::EndianSwap(flatbuffers::EndianSwap(3.14f)), 3.14f);
2331 }
2332
2333 void UninitializedVectorTest() {
2334   flatbuffers::FlatBufferBuilder builder;
2335
2336   Test *buf = nullptr;
2337   auto vector_offset = builder.CreateUninitializedVectorOfStructs<Test>(2, &buf);
2338   TEST_NOTNULL(buf);
2339   buf[0] = Test(10, 20);
2340   buf[1] = Test(30, 40);
2341
2342   auto required_name = builder.CreateString("myMonster");
2343   auto monster_builder = MonsterBuilder(builder);
2344   monster_builder.add_name(required_name); // required field mandated for monster.
2345   monster_builder.add_test4(vector_offset);
2346   builder.Finish(monster_builder.Finish());
2347
2348   auto p = builder.GetBufferPointer();
2349   auto uvt = flatbuffers::GetRoot<Monster>(p);
2350   TEST_NOTNULL(uvt);
2351   auto vec = uvt->test4();
2352   TEST_NOTNULL(vec);
2353   auto test_0 = vec->Get(0);
2354   auto test_1 = vec->Get(1);
2355   TEST_EQ(test_0->a(), 10);
2356   TEST_EQ(test_0->b(), 20);
2357   TEST_EQ(test_1->a(), 30);
2358   TEST_EQ(test_1->b(), 40);
2359 }
2360
2361 void EqualOperatorTest() {
2362   MonsterT a;
2363   MonsterT b;
2364   TEST_EQ(b == a, true);
2365
2366   b.mana = 33;
2367   TEST_EQ(b == a, false);
2368   b.mana = 150;
2369   TEST_EQ(b == a, true);
2370
2371   b.inventory.push_back(3);
2372   TEST_EQ(b == a, false);
2373   b.inventory.clear();
2374   TEST_EQ(b == a, true);
2375
2376   b.test.type = Any_Monster;
2377   TEST_EQ(b == a, false);
2378 }
2379
2380 // For testing any binaries, e.g. from fuzzing.
2381 void LoadVerifyBinaryTest() {
2382   std::string binary;
2383   if (flatbuffers::LoadFile((test_data_path +
2384                              "fuzzer/your-filename-here").c_str(),
2385                             true, &binary)) {
2386     flatbuffers::Verifier verifier(
2387           reinterpret_cast<const uint8_t *>(binary.data()), binary.size());
2388     TEST_EQ(VerifyMonsterBuffer(verifier), true);
2389   }
2390 }
2391
2392 void CreateSharedStringTest() {
2393   flatbuffers::FlatBufferBuilder builder;
2394   const auto one1 = builder.CreateSharedString("one");
2395   const auto two = builder.CreateSharedString("two");
2396   const auto one2 = builder.CreateSharedString("one");
2397   TEST_EQ(one1.o, one2.o);
2398   const auto onetwo = builder.CreateSharedString("onetwo");
2399   TEST_EQ(onetwo.o != one1.o, true);
2400   TEST_EQ(onetwo.o != two.o, true);
2401
2402   // Support for embedded nulls
2403   const char chars_b[] = {'a', '\0', 'b'};
2404   const char chars_c[] = {'a', '\0', 'c'};
2405   const auto null_b1 = builder.CreateSharedString(chars_b, sizeof(chars_b));
2406   const auto null_c = builder.CreateSharedString(chars_c, sizeof(chars_c));
2407   const auto null_b2 = builder.CreateSharedString(chars_b, sizeof(chars_b));
2408   TEST_EQ(null_b1.o != null_c.o, true); // Issue#5058 repro
2409   TEST_EQ(null_b1.o, null_b2.o);
2410
2411   // Put the strings into an array for round trip verification.
2412   const flatbuffers::Offset<flatbuffers::String> array[7] = { one1, two, one2, onetwo, null_b1, null_c, null_b2 };
2413   const auto vector_offset = builder.CreateVector(array, flatbuffers::uoffset_t(7));
2414   MonsterBuilder monster_builder(builder);
2415   monster_builder.add_name(two);
2416   monster_builder.add_testarrayofstring(vector_offset);
2417   builder.Finish(monster_builder.Finish());
2418
2419   // Read the Monster back.
2420   const auto *monster = flatbuffers::GetRoot<Monster>(builder.GetBufferPointer());
2421   TEST_EQ_STR(monster->name()->c_str(), "two");
2422   const auto *testarrayofstring = monster->testarrayofstring();
2423   TEST_EQ(testarrayofstring->size(), flatbuffers::uoffset_t(7));
2424   const auto &a = *testarrayofstring;
2425   TEST_EQ_STR(a[0]->c_str(), "one");
2426   TEST_EQ_STR(a[1]->c_str(), "two");
2427   TEST_EQ_STR(a[2]->c_str(), "one");
2428   TEST_EQ_STR(a[3]->c_str(), "onetwo");
2429   TEST_EQ(a[4]->str(), (std::string(chars_b, sizeof(chars_b))));
2430   TEST_EQ(a[5]->str(), (std::string(chars_c, sizeof(chars_c))));
2431   TEST_EQ(a[6]->str(), (std::string(chars_b, sizeof(chars_b))));
2432
2433   // Make sure String::operator< works, too, since it is related to StringOffsetCompare.
2434   TEST_EQ((*a[0]) < (*a[1]), true);
2435   TEST_EQ((*a[1]) < (*a[0]), false);
2436   TEST_EQ((*a[1]) < (*a[2]), false);
2437   TEST_EQ((*a[2]) < (*a[1]), true);
2438   TEST_EQ((*a[4]) < (*a[3]), true);
2439   TEST_EQ((*a[5]) < (*a[4]), false);
2440   TEST_EQ((*a[5]) < (*a[4]), false);
2441   TEST_EQ((*a[6]) < (*a[5]), true);
2442 }
2443
2444 int FlatBufferTests() {
2445   // clang-format off
2446   #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
2447       defined(_MSC_VER) && defined(_DEBUG)
2448     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF
2449       // For more thorough checking:
2450       //| _CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_DELAY_FREE_MEM_DF
2451     );
2452   #endif
2453
2454   // Run our various test suites:
2455
2456   std::string rawbuf;
2457   auto flatbuf1 = CreateFlatBufferTest(rawbuf);
2458   #if !defined(FLATBUFFERS_CPP98_STL)
2459     auto flatbuf = std::move(flatbuf1);  // Test move assignment.
2460   #else
2461     auto &flatbuf = flatbuf1;
2462   #endif // !defined(FLATBUFFERS_CPP98_STL)
2463
2464   TriviallyCopyableTest();
2465
2466   AccessFlatBufferTest(reinterpret_cast<const uint8_t *>(rawbuf.c_str()),
2467                        rawbuf.length());
2468   AccessFlatBufferTest(flatbuf.data(), flatbuf.size());
2469
2470   MutateFlatBuffersTest(flatbuf.data(), flatbuf.size());
2471
2472   ObjectFlatBuffersTest(flatbuf.data());
2473
2474   MiniReflectFlatBuffersTest(flatbuf.data());
2475
2476   SizePrefixedTest();
2477
2478   #ifndef FLATBUFFERS_NO_FILE_TESTS
2479     #ifdef FLATBUFFERS_TEST_PATH_PREFIX
2480       test_data_path = FLATBUFFERS_STRING(FLATBUFFERS_TEST_PATH_PREFIX) +
2481                        test_data_path;
2482     #endif
2483     ParseAndGenerateTextTest(false);
2484     ParseAndGenerateTextTest(true);
2485     ReflectionTest(flatbuf.data(), flatbuf.size());
2486     ParseProtoTest();
2487     UnionVectorTest();
2488     LoadVerifyBinaryTest();
2489   #endif
2490   // clang-format on
2491
2492   FuzzTest1();
2493   FuzzTest2();
2494
2495   ErrorTest();
2496   ValueTest();
2497   EnumStringsTest();
2498   EnumNamesTest();
2499   EnumOutOfRangeTest();
2500   IntegerOutOfRangeTest();
2501   IntegerBoundaryTest();
2502   UnicodeTest();
2503   UnicodeTestAllowNonUTF8();
2504   UnicodeTestGenerateTextFailsOnNonUTF8();
2505   UnicodeSurrogatesTest();
2506   UnicodeInvalidSurrogatesTest();
2507   InvalidUTF8Test();
2508   UnknownFieldsTest();
2509   ParseUnionTest();
2510   ConformTest();
2511   ParseProtoBufAsciiTest();
2512   TypeAliasesTest();
2513   EndianSwapTest();
2514   CreateSharedStringTest();
2515   JsonDefaultTest();
2516   FlexBuffersTest();
2517   UninitializedVectorTest();
2518   EqualOperatorTest();
2519   NumericUtilsTest();
2520   IsAsciiUtilsTest();
2521   ValidFloatTest();
2522   InvalidFloatTest();
2523   return 0;
2524 }
2525
2526 int main(int /*argc*/, const char * /*argv*/ []) {
2527   InitTestEngine();
2528
2529   std::string req_locale;
2530   if (flatbuffers::ReadEnvironmentVariable("FLATBUFFERS_TEST_LOCALE",
2531                                           &req_locale)) {
2532     TEST_OUTPUT_LINE("The environment variable FLATBUFFERS_TEST_LOCALE=%s",
2533                      req_locale.c_str());
2534     req_locale = flatbuffers::RemoveStringQuotes(req_locale);
2535     std::string the_locale;
2536     TEST_ASSERT_FUNC(
2537         flatbuffers::SetGlobalTestLocale(req_locale.c_str(), &the_locale));
2538     TEST_OUTPUT_LINE("The global C-locale changed: %s", the_locale.c_str());
2539   }
2540
2541   FlatBufferTests();
2542   FlatBufferBuilderTest();
2543
2544   if (!testing_fails) {
2545     TEST_OUTPUT_LINE("ALL TESTS PASSED");
2546     return 0;
2547   } else {
2548     TEST_OUTPUT_LINE("%d FAILED TESTS", testing_fails);
2549     return 1;
2550   }
2551 }