Protobufs: Added '--oneof-union' option. (#4647)
[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
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
37 // clang-format off
38 #ifndef FLATBUFFERS_CPP98_STL
39   #include <random>
40 #endif
41
42 #include "flatbuffers/flexbuffers.h"
43
44 using namespace MyGame::Example;
45
46 #ifdef __ANDROID__
47   #include <android/log.h>
48   #define TEST_OUTPUT_LINE(...) \
49     __android_log_print(ANDROID_LOG_INFO, "FlatBuffers", __VA_ARGS__)
50   #define FLATBUFFERS_NO_FILE_TESTS
51 #else
52   #define TEST_OUTPUT_LINE(...) \
53     { printf(__VA_ARGS__); printf("\n"); }
54 #endif
55 // clang-format on
56
57 int testing_fails = 0;
58
59 void TestFail(const char *expval, const char *val, const char *exp,
60               const char *file, int line) {
61   TEST_OUTPUT_LINE("VALUE: \"%s\"", expval);
62   TEST_OUTPUT_LINE("EXPECTED: \"%s\"", val);
63   TEST_OUTPUT_LINE("TEST FAILED: %s:%d, %s", file, line, exp);
64   assert(0);
65   testing_fails++;
66 }
67
68 void TestEqStr(const char *expval, const char *val, const char *exp,
69                const char *file, int line) {
70   if (strcmp(expval, val) != 0) { TestFail(expval, val, exp, file, line); }
71 }
72
73 template<typename T, typename U>
74 void TestEq(T expval, U val, const char *exp, const char *file, int line) {
75   if (U(expval) != val) {
76     TestFail(flatbuffers::NumToString(expval).c_str(),
77              flatbuffers::NumToString(val).c_str(), exp, file, line);
78   }
79 }
80
81 #define TEST_EQ(exp, val) TestEq(exp, val, #exp, __FILE__, __LINE__)
82 #define TEST_NOTNULL(exp) TestEq(exp == NULL, false, #exp, __FILE__, __LINE__)
83 #define TEST_EQ_STR(exp, val) TestEqStr(exp, val, #exp, __FILE__, __LINE__)
84
85 // Include simple random number generator to ensure results will be the
86 // same cross platform.
87 // http://en.wikipedia.org/wiki/Park%E2%80%93Miller_random_number_generator
88 uint32_t lcg_seed = 48271;
89 uint32_t lcg_rand() {
90   return lcg_seed = ((uint64_t)lcg_seed * 279470273UL) % 4294967291UL;
91 }
92 void lcg_reset() { lcg_seed = 48271; }
93
94 std::string test_data_path = "tests/";
95
96 // example of how to build up a serialized buffer algorithmically:
97 flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
98   flatbuffers::FlatBufferBuilder builder;
99
100   auto vec = Vec3(1, 2, 3, 0, Color_Red, Test(10, 20));
101
102   auto name = builder.CreateString("MyMonster");
103
104   unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
105   auto inventory = builder.CreateVector(inv_data, 10);
106
107   // Alternatively, create the vector first, and fill in data later:
108   // unsigned char *inv_buf = nullptr;
109   // auto inventory = builder.CreateUninitializedVector<unsigned char>(
110   //                                                              10, &inv_buf);
111   // memcpy(inv_buf, inv_data, 10);
112
113   Test tests[] = { Test(10, 20), Test(30, 40) };
114   auto testv = builder.CreateVectorOfStructs(tests, 2);
115
116   // clang-format off
117   #ifndef FLATBUFFERS_CPP98_STL
118     // Create a vector of structures from a lambda.
119     auto testv2 = builder.CreateVectorOfStructs<Test>(
120           2, [&](size_t i, Test* s) -> void {
121             *s = tests[i];
122           });
123   #else
124     // Create a vector of structures using a plain old C++ function.
125     auto testv2 = builder.CreateVectorOfStructs<Test>(
126           2, [](size_t i, Test* s, void *state) -> void {
127             *s = (reinterpret_cast<Test*>(state))[i];
128           }, tests);
129   #endif  // FLATBUFFERS_CPP98_STL
130   // clang-format on
131
132   // create monster with very few fields set:
133   // (same functionality as CreateMonster below, but sets fields manually)
134   flatbuffers::Offset<Monster> mlocs[3];
135   auto fred = builder.CreateString("Fred");
136   auto barney = builder.CreateString("Barney");
137   auto wilma = builder.CreateString("Wilma");
138   MonsterBuilder mb1(builder);
139   mb1.add_name(fred);
140   mlocs[0] = mb1.Finish();
141   MonsterBuilder mb2(builder);
142   mb2.add_name(barney);
143   mb2.add_hp(1000);
144   mlocs[1] = mb2.Finish();
145   MonsterBuilder mb3(builder);
146   mb3.add_name(wilma);
147   mlocs[2] = mb3.Finish();
148
149   // Create an array of strings. Also test string pooling, and lambdas.
150   auto vecofstrings =
151       builder.CreateVector<flatbuffers::Offset<flatbuffers::String>>(
152           4,
153           [](size_t i, flatbuffers::FlatBufferBuilder *b)
154               -> flatbuffers::Offset<flatbuffers::String> {
155             static const char *names[] = { "bob", "fred", "bob", "fred" };
156             return b->CreateSharedString(names[i]);
157           },
158           &builder);
159
160   // Creating vectors of strings in one convenient call.
161   std::vector<std::string> names2;
162   names2.push_back("jane");
163   names2.push_back("mary");
164   auto vecofstrings2 = builder.CreateVectorOfStrings(names2);
165
166   // Create an array of sorted tables, can be used with binary search when read:
167   auto vecoftables = builder.CreateVectorOfSortedTables(mlocs, 3);
168
169   // Create an array of sorted structs,
170   // can be used with binary search when read:
171   std::vector<Ability> abilities;
172   abilities.push_back(Ability(4, 40));
173   abilities.push_back(Ability(3, 30));
174   abilities.push_back(Ability(2, 20));
175   abilities.push_back(Ability(1, 10));
176   auto vecofstructs = builder.CreateVectorOfSortedStructs(&abilities);
177
178   // Create a nested FlatBuffer.
179   // Nested FlatBuffers are stored in a ubyte vector, which can be convenient
180   // since they can be memcpy'd around much easier than other FlatBuffer
181   // values. They have little overhead compared to storing the table directly.
182   // As a test, create a mostly empty Monster buffer:
183   flatbuffers::FlatBufferBuilder nested_builder;
184   auto nmloc = CreateMonster(nested_builder, nullptr, 0, 0,
185                              nested_builder.CreateString("NestedMonster"));
186   FinishMonsterBuffer(nested_builder, nmloc);
187   // Now we can store the buffer in the parent. Note that by default, vectors
188   // are only aligned to their elements or size field, so in this case if the
189   // buffer contains 64-bit elements, they may not be correctly aligned. We fix
190   // that with:
191   builder.ForceVectorAlignment(nested_builder.GetSize(), sizeof(uint8_t),
192                                nested_builder.GetBufferMinAlignment());
193   // If for whatever reason you don't have the nested_builder available, you
194   // can substitute flatbuffers::largest_scalar_t (64-bit) for the alignment, or
195   // the largest force_align value in your schema if you're using it.
196   auto nested_flatbuffer_vector = builder.CreateVector(
197       nested_builder.GetBufferPointer(), nested_builder.GetSize());
198
199   // Test a nested FlexBuffer:
200   flexbuffers::Builder flexbuild;
201   flexbuild.Int(1234);
202   flexbuild.Finish();
203   auto flex = builder.CreateVector(flexbuild.GetBuffer());
204
205   // shortcut for creating monster with all fields set:
206   auto mloc = CreateMonster(builder, &vec, 150, 80, name, inventory, Color_Blue,
207                             Any_Monster, mlocs[1].Union(),  // Store a union.
208                             testv, vecofstrings, vecoftables, 0,
209                             nested_flatbuffer_vector, 0, false, 0, 0, 0, 0, 0,
210                             0, 0, 0, 0, 3.14159f, 3.0f, 0.0f, vecofstrings2,
211                             vecofstructs, flex, testv2);
212
213   FinishMonsterBuffer(builder, mloc);
214
215   // clang-format off
216   #ifdef FLATBUFFERS_TEST_VERBOSE
217   // print byte data for debugging:
218   auto p = builder.GetBufferPointer();
219   for (flatbuffers::uoffset_t i = 0; i < builder.GetSize(); i++)
220     printf("%d ", p[i]);
221   #endif
222   // clang-format on
223
224   // return the buffer for the caller to use.
225   auto bufferpointer =
226       reinterpret_cast<const char *>(builder.GetBufferPointer());
227   buffer.assign(bufferpointer, bufferpointer + builder.GetSize());
228
229   return builder.ReleaseBufferPointer();
230 }
231
232 //  example of accessing a buffer loaded in memory:
233 void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length,
234                           bool pooled = true) {
235   // First, verify the buffers integrity (optional)
236   flatbuffers::Verifier verifier(flatbuf, length);
237   TEST_EQ(VerifyMonsterBuffer(verifier), true);
238
239   std::vector<uint8_t> test_buff;
240   test_buff.resize(length * 2);
241   std::memcpy(&test_buff[0], flatbuf, length);
242   std::memcpy(&test_buff[length], flatbuf, length);
243
244   flatbuffers::Verifier verifier1(&test_buff[0], length);
245   TEST_EQ(VerifyMonsterBuffer(verifier1), true);
246   TEST_EQ(verifier1.GetComputedSize(), length);
247
248   flatbuffers::Verifier verifier2(&test_buff[length], length);
249   TEST_EQ(VerifyMonsterBuffer(verifier2), true);
250   TEST_EQ(verifier2.GetComputedSize(), length);
251
252   TEST_EQ(strcmp(MonsterIdentifier(), "MONS"), 0);
253   TEST_EQ(MonsterBufferHasIdentifier(flatbuf), true);
254   TEST_EQ(strcmp(MonsterExtension(), "mon"), 0);
255
256   // Access the buffer from the root.
257   auto monster = GetMonster(flatbuf);
258
259   TEST_EQ(monster->hp(), 80);
260   TEST_EQ(monster->mana(), 150);  // default
261   TEST_EQ_STR(monster->name()->c_str(), "MyMonster");
262   // Can't access the following field, it is deprecated in the schema,
263   // which means accessors are not generated:
264   // monster.friendly()
265
266   auto pos = monster->pos();
267   TEST_NOTNULL(pos);
268   TEST_EQ(pos->z(), 3);
269   TEST_EQ(pos->test3().a(), 10);
270   TEST_EQ(pos->test3().b(), 20);
271
272   auto inventory = monster->inventory();
273   TEST_EQ(VectorLength(inventory), 10UL);  // Works even if inventory is null.
274   TEST_NOTNULL(inventory);
275   unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
276   for (auto it = inventory->begin(); it != inventory->end(); ++it)
277     TEST_EQ(*it, inv_data[it - inventory->begin()]);
278
279   TEST_EQ(monster->color(), Color_Blue);
280
281   // Example of accessing a union:
282   TEST_EQ(monster->test_type(), Any_Monster);  // First make sure which it is.
283   auto monster2 = reinterpret_cast<const Monster *>(monster->test());
284   TEST_NOTNULL(monster2);
285   TEST_EQ_STR(monster2->name()->c_str(), "Fred");
286
287   // Example of accessing a vector of strings:
288   auto vecofstrings = monster->testarrayofstring();
289   TEST_EQ(vecofstrings->Length(), 4U);
290   TEST_EQ_STR(vecofstrings->Get(0)->c_str(), "bob");
291   TEST_EQ_STR(vecofstrings->Get(1)->c_str(), "fred");
292   if (pooled) {
293     // These should have pointer equality because of string pooling.
294     TEST_EQ(vecofstrings->Get(0)->c_str(), vecofstrings->Get(2)->c_str());
295     TEST_EQ(vecofstrings->Get(1)->c_str(), vecofstrings->Get(3)->c_str());
296   }
297
298   auto vecofstrings2 = monster->testarrayofstring2();
299   if (vecofstrings2) {
300     TEST_EQ(vecofstrings2->Length(), 2U);
301     TEST_EQ_STR(vecofstrings2->Get(0)->c_str(), "jane");
302     TEST_EQ_STR(vecofstrings2->Get(1)->c_str(), "mary");
303   }
304
305   // Example of accessing a vector of tables:
306   auto vecoftables = monster->testarrayoftables();
307   TEST_EQ(vecoftables->Length(), 3U);
308   for (auto it = vecoftables->begin(); it != vecoftables->end(); ++it)
309     TEST_EQ(strlen(it->name()->c_str()) >= 4, true);
310   TEST_EQ_STR(vecoftables->Get(0)->name()->c_str(), "Barney");
311   TEST_EQ(vecoftables->Get(0)->hp(), 1000);
312   TEST_EQ_STR(vecoftables->Get(1)->name()->c_str(), "Fred");
313   TEST_EQ_STR(vecoftables->Get(2)->name()->c_str(), "Wilma");
314   TEST_NOTNULL(vecoftables->LookupByKey("Barney"));
315   TEST_NOTNULL(vecoftables->LookupByKey("Fred"));
316   TEST_NOTNULL(vecoftables->LookupByKey("Wilma"));
317
318   // Test accessing a vector of sorted structs
319   auto vecofstructs = monster->testarrayofsortedstruct();
320   if (vecofstructs) {  // not filled in monster_test.bfbs
321     for (flatbuffers::uoffset_t i = 0; i < vecofstructs->size() - 1; i++) {
322       auto left = vecofstructs->Get(i);
323       auto right = vecofstructs->Get(i + 1);
324       TEST_EQ(true, (left->KeyCompareLessThan(right)));
325     }
326     TEST_NOTNULL(vecofstructs->LookupByKey(3));
327     TEST_EQ(static_cast<const Ability *>(nullptr),
328             vecofstructs->LookupByKey(5));
329   }
330
331   // Test nested FlatBuffers if available:
332   auto nested_buffer = monster->testnestedflatbuffer();
333   if (nested_buffer) {
334     // nested_buffer is a vector of bytes you can memcpy. However, if you
335     // actually want to access the nested data, this is a convenient
336     // accessor that directly gives you the root table:
337     auto nested_monster = monster->testnestedflatbuffer_nested_root();
338     TEST_EQ_STR(nested_monster->name()->c_str(), "NestedMonster");
339   }
340
341   // Test flexbuffer if available:
342   auto flex = monster->flex();
343   // flex is a vector of bytes you can memcpy etc.
344   TEST_EQ(flex->size(), 4);  // Encoded FlexBuffer bytes.
345   // However, if you actually want to access the nested data, this is a
346   // convenient accessor that directly gives you the root value:
347   TEST_EQ(monster->flex_flexbuffer_root().AsInt16(), 1234);
348
349   // Since Flatbuffers uses explicit mechanisms to override the default
350   // compiler alignment, double check that the compiler indeed obeys them:
351   // (Test consists of a short and byte):
352   TEST_EQ(flatbuffers::AlignOf<Test>(), 2UL);
353   TEST_EQ(sizeof(Test), 4UL);
354
355   const flatbuffers::Vector<const Test *> *tests_array[] = {
356     monster->test4(),
357     monster->test5(),
358   };
359   for (size_t i = 0; i < sizeof(tests_array) / sizeof(tests_array[0]); ++i) {
360     auto tests = tests_array[i];
361     TEST_NOTNULL(tests);
362     auto test_0 = tests->Get(0);
363     auto test_1 = tests->Get(1);
364     TEST_EQ(test_0->a(), 10);
365     TEST_EQ(test_0->b(), 20);
366     TEST_EQ(test_1->a(), 30);
367     TEST_EQ(test_1->b(), 40);
368     for (auto it = tests->begin(); it != tests->end(); ++it) {
369       TEST_EQ(it->a() == 10 || it->a() == 30, true);  // Just testing iterators.
370     }
371   }
372
373   // Checking for presence of fields:
374   TEST_EQ(flatbuffers::IsFieldPresent(monster, Monster::VT_HP), true);
375   TEST_EQ(flatbuffers::IsFieldPresent(monster, Monster::VT_MANA), false);
376
377   // Obtaining a buffer from a root:
378   TEST_EQ(GetBufferStartFromRootPointer(monster), flatbuf);
379 }
380
381 // Change a FlatBuffer in-place, after it has been constructed.
382 void MutateFlatBuffersTest(uint8_t *flatbuf, std::size_t length) {
383   // Get non-const pointer to root.
384   auto monster = GetMutableMonster(flatbuf);
385
386   // Each of these tests mutates, then tests, then set back to the original,
387   // so we can test that the buffer in the end still passes our original test.
388   auto hp_ok = monster->mutate_hp(10);
389   TEST_EQ(hp_ok, true);  // Field was present.
390   TEST_EQ(monster->hp(), 10);
391   // Mutate to default value
392   auto hp_ok_default = monster->mutate_hp(100);
393   TEST_EQ(hp_ok_default, true);  // Field was present.
394   TEST_EQ(monster->hp(), 100);
395   // Test that mutate to default above keeps field valid for further mutations
396   auto hp_ok_2 = monster->mutate_hp(20);
397   TEST_EQ(hp_ok_2, true);
398   TEST_EQ(monster->hp(), 20);
399   monster->mutate_hp(80);
400
401   // Monster originally at 150 mana (default value)
402   auto mana_default_ok = monster->mutate_mana(150);  // Mutate to default value.
403   TEST_EQ(mana_default_ok,
404           true);  // Mutation should succeed, because default value.
405   TEST_EQ(monster->mana(), 150);
406   auto mana_ok = monster->mutate_mana(10);
407   TEST_EQ(mana_ok, false);  // Field was NOT present, because default value.
408   TEST_EQ(monster->mana(), 150);
409
410   // Mutate structs.
411   auto pos = monster->mutable_pos();
412   auto test3 = pos->mutable_test3();  // Struct inside a struct.
413   test3.mutate_a(50);                 // Struct fields never fail.
414   TEST_EQ(test3.a(), 50);
415   test3.mutate_a(10);
416
417   // Mutate vectors.
418   auto inventory = monster->mutable_inventory();
419   inventory->Mutate(9, 100);
420   TEST_EQ(inventory->Get(9), 100);
421   inventory->Mutate(9, 9);
422
423   auto tables = monster->mutable_testarrayoftables();
424   auto first = tables->GetMutableObject(0);
425   TEST_EQ(first->hp(), 1000);
426   first->mutate_hp(0);
427   TEST_EQ(first->hp(), 0);
428   first->mutate_hp(1000);
429
430   // Run the verifier and the regular test to make sure we didn't trample on
431   // anything.
432   AccessFlatBufferTest(flatbuf, length);
433 }
434
435 // Unpack a FlatBuffer into objects.
436 void ObjectFlatBuffersTest(uint8_t *flatbuf) {
437   // Optional: we can specify resolver and rehasher functions to turn hashed
438   // strings into object pointers and back, to implement remote references
439   // and such.
440   auto resolver = flatbuffers::resolver_function_t(
441       [](void **pointer_adr, flatbuffers::hash_value_t hash) {
442         (void)pointer_adr;
443         (void)hash;
444         // Don't actually do anything, leave variable null.
445       });
446   auto rehasher = flatbuffers::rehasher_function_t(
447       [](void *pointer) -> flatbuffers::hash_value_t {
448         (void)pointer;
449         return 0;
450       });
451
452   // Turn a buffer into C++ objects.
453   auto monster1 = UnPackMonster(flatbuf, &resolver);
454
455   // Re-serialize the data.
456   flatbuffers::FlatBufferBuilder fbb1;
457   fbb1.Finish(CreateMonster(fbb1, monster1.get(), &rehasher),
458               MonsterIdentifier());
459
460   // Unpack again, and re-serialize again.
461   auto monster2 = UnPackMonster(fbb1.GetBufferPointer(), &resolver);
462   flatbuffers::FlatBufferBuilder fbb2;
463   fbb2.Finish(CreateMonster(fbb2, monster2.get(), &rehasher),
464               MonsterIdentifier());
465
466   // Now we've gone full round-trip, the two buffers should match.
467   auto len1 = fbb1.GetSize();
468   auto len2 = fbb2.GetSize();
469   TEST_EQ(len1, len2);
470   TEST_EQ(memcmp(fbb1.GetBufferPointer(), fbb2.GetBufferPointer(), len1), 0);
471
472   // Test it with the original buffer test to make sure all data survived.
473   AccessFlatBufferTest(fbb2.GetBufferPointer(), len2, false);
474
475   // Test accessing fields, similar to AccessFlatBufferTest above.
476   TEST_EQ(monster2->hp, 80);
477   TEST_EQ(monster2->mana, 150);  // default
478   TEST_EQ_STR(monster2->name.c_str(), "MyMonster");
479
480   auto &pos = monster2->pos;
481   TEST_NOTNULL(pos);
482   TEST_EQ(pos->z(), 3);
483   TEST_EQ(pos->test3().a(), 10);
484   TEST_EQ(pos->test3().b(), 20);
485
486   auto &inventory = monster2->inventory;
487   TEST_EQ(inventory.size(), 10UL);
488   unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
489   for (auto it = inventory.begin(); it != inventory.end(); ++it)
490     TEST_EQ(*it, inv_data[it - inventory.begin()]);
491
492   TEST_EQ(monster2->color, Color_Blue);
493
494   auto monster3 = monster2->test.AsMonster();
495   TEST_NOTNULL(monster3);
496   TEST_EQ_STR(monster3->name.c_str(), "Fred");
497
498   auto &vecofstrings = monster2->testarrayofstring;
499   TEST_EQ(vecofstrings.size(), 4U);
500   TEST_EQ_STR(vecofstrings[0].c_str(), "bob");
501   TEST_EQ_STR(vecofstrings[1].c_str(), "fred");
502
503   auto &vecofstrings2 = monster2->testarrayofstring2;
504   TEST_EQ(vecofstrings2.size(), 2U);
505   TEST_EQ_STR(vecofstrings2[0].c_str(), "jane");
506   TEST_EQ_STR(vecofstrings2[1].c_str(), "mary");
507
508   auto &vecoftables = monster2->testarrayoftables;
509   TEST_EQ(vecoftables.size(), 3U);
510   TEST_EQ_STR(vecoftables[0]->name.c_str(), "Barney");
511   TEST_EQ(vecoftables[0]->hp, 1000);
512   TEST_EQ_STR(vecoftables[1]->name.c_str(), "Fred");
513   TEST_EQ_STR(vecoftables[2]->name.c_str(), "Wilma");
514
515   auto &tests = monster2->test4;
516   TEST_EQ(tests[0].a(), 10);
517   TEST_EQ(tests[0].b(), 20);
518   TEST_EQ(tests[1].a(), 30);
519   TEST_EQ(tests[1].b(), 40);
520 }
521
522 // Prefix a FlatBuffer with a size field.
523 void SizePrefixedTest() {
524   // Create size prefixed buffer.
525   flatbuffers::FlatBufferBuilder fbb;
526   FinishSizePrefixedMonsterBuffer(
527       fbb,
528       CreateMonster(fbb, 0, 200, 300, fbb.CreateString("bob")));
529
530   // Verify it.
531   flatbuffers::Verifier verifier(fbb.GetBufferPointer(), fbb.GetSize());
532   TEST_EQ(VerifySizePrefixedMonsterBuffer(verifier), true);
533
534   // Access it.
535   auto m = GetSizePrefixedMonster(fbb.GetBufferPointer());
536   TEST_EQ(m->mana(), 200);
537   TEST_EQ(m->hp(), 300);
538   TEST_EQ_STR(m->name()->c_str(), "bob");
539 }
540
541 void TriviallyCopyableTest() {
542   // clang-format off
543   #if __GNUG__ && __GNUC__ < 5
544     TEST_EQ(__has_trivial_copy(Vec3), true);
545   #else
546     #if __cplusplus >= 201103L
547       TEST_EQ(std::is_trivially_copyable<Vec3>::value, true);
548     #endif
549   #endif
550   // clang-format on
551 }
552
553 // Check stringify of an default enum value to json
554 void JsonDefaultTest() {
555   // load FlatBuffer schema (.fbs) from disk
556   std::string schemafile;
557   TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.fbs").c_str(),
558                                 false, &schemafile), true);
559   // parse schema first, so we can use it to parse the data after
560   flatbuffers::Parser parser;
561   auto include_test_path =
562       flatbuffers::ConCatPathFileName(test_data_path, "include_test");
563   const char *include_directories[] = { test_data_path.c_str(),
564                                         include_test_path.c_str(), nullptr };
565
566   TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
567   // create incomplete monster and store to json
568   parser.opts.output_default_scalars_in_json = true;
569   parser.opts.output_enum_identifiers = true;
570   flatbuffers::FlatBufferBuilder builder;
571   auto name = builder.CreateString("default_enum");
572   MonsterBuilder color_monster(builder);
573   color_monster.add_name(name);
574   FinishMonsterBuffer(builder, color_monster.Finish());
575   std::string jsongen;
576   auto result = GenerateText(parser, builder.GetBufferPointer(), &jsongen);
577   TEST_EQ(result, true);
578   // default value of the "color" field is Blue
579   TEST_EQ(std::string::npos != jsongen.find("color: \"Blue\""), true);
580   // default value of the "testf" field is 3.14159
581   TEST_EQ(std::string::npos != jsongen.find("testf: 3.14159"), true);
582 }
583
584 // example of parsing text straight into a buffer, and generating
585 // text back from it:
586 void ParseAndGenerateTextTest() {
587   // load FlatBuffer schema (.fbs) and JSON from disk
588   std::string schemafile;
589   std::string jsonfile;
590   TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.fbs").c_str(),
591                                 false, &schemafile),
592           true);
593   TEST_EQ(flatbuffers::LoadFile(
594               (test_data_path + "monsterdata_test.golden").c_str(), false,
595               &jsonfile),
596           true);
597
598   // parse schema first, so we can use it to parse the data after
599   flatbuffers::Parser parser;
600   auto include_test_path =
601       flatbuffers::ConCatPathFileName(test_data_path, "include_test");
602   const char *include_directories[] = { test_data_path.c_str(),
603                                         include_test_path.c_str(), nullptr };
604   TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
605   TEST_EQ(parser.Parse(jsonfile.c_str(), include_directories), true);
606
607   // here, parser.builder_ contains a binary buffer that is the parsed data.
608
609   // First, verify it, just in case:
610   flatbuffers::Verifier verifier(parser.builder_.GetBufferPointer(),
611                                  parser.builder_.GetSize());
612   TEST_EQ(VerifyMonsterBuffer(verifier), true);
613
614   AccessFlatBufferTest(parser.builder_.GetBufferPointer(),
615                        parser.builder_.GetSize(), false);
616
617   // to ensure it is correct, we now generate text back from the binary,
618   // and compare the two:
619   std::string jsongen;
620   auto result =
621       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
622   TEST_EQ(result, true);
623   TEST_EQ_STR(jsongen.c_str(), jsonfile.c_str());
624
625   // We can also do the above using the convenient Registry that knows about
626   // a set of file_identifiers mapped to schemas.
627   flatbuffers::Registry registry;
628   // Make sure schemas can find their includes.
629   registry.AddIncludeDirectory(test_data_path.c_str());
630   registry.AddIncludeDirectory(include_test_path.c_str());
631   // Call this with many schemas if possible.
632   registry.Register(MonsterIdentifier(),
633                     (test_data_path + "monster_test.fbs").c_str());
634   // Now we got this set up, we can parse by just specifying the identifier,
635   // the correct schema will be loaded on the fly:
636   auto buf = registry.TextToFlatBuffer(jsonfile.c_str(), MonsterIdentifier());
637   // If this fails, check registry.lasterror_.
638   TEST_NOTNULL(buf.data());
639   // Test the buffer, to be sure:
640   AccessFlatBufferTest(buf.data(), buf.size(), false);
641   // We can use the registry to turn this back into text, in this case it
642   // will get the file_identifier from the binary:
643   std::string text;
644   auto ok = registry.FlatBufferToText(buf.data(), buf.size(), &text);
645   // If this fails, check registry.lasterror_.
646   TEST_EQ(ok, true);
647   TEST_EQ_STR(text.c_str(), jsonfile.c_str());
648 }
649
650 void ReflectionTest(uint8_t *flatbuf, size_t length) {
651   // Load a binary schema.
652   std::string bfbsfile;
653   TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.bfbs").c_str(),
654                                 true, &bfbsfile),
655           true);
656
657   // Verify it, just in case:
658   flatbuffers::Verifier verifier(
659       reinterpret_cast<const uint8_t *>(bfbsfile.c_str()), bfbsfile.length());
660   TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
661
662   // Make sure the schema is what we expect it to be.
663   auto &schema = *reflection::GetSchema(bfbsfile.c_str());
664   auto root_table = schema.root_table();
665   TEST_EQ_STR(root_table->name()->c_str(), "MyGame.Example.Monster");
666   auto fields = root_table->fields();
667   auto hp_field_ptr = fields->LookupByKey("hp");
668   TEST_NOTNULL(hp_field_ptr);
669   auto &hp_field = *hp_field_ptr;
670   TEST_EQ_STR(hp_field.name()->c_str(), "hp");
671   TEST_EQ(hp_field.id(), 2);
672   TEST_EQ(hp_field.type()->base_type(), reflection::Short);
673   auto friendly_field_ptr = fields->LookupByKey("friendly");
674   TEST_NOTNULL(friendly_field_ptr);
675   TEST_NOTNULL(friendly_field_ptr->attributes());
676   TEST_NOTNULL(friendly_field_ptr->attributes()->LookupByKey("priority"));
677
678   // Make sure the table index is what we expect it to be.
679   auto pos_field_ptr = fields->LookupByKey("pos");
680   TEST_NOTNULL(pos_field_ptr);
681   TEST_EQ(pos_field_ptr->type()->base_type(), reflection::Obj);
682   auto pos_table_ptr = schema.objects()->Get(pos_field_ptr->type()->index());
683   TEST_NOTNULL(pos_table_ptr);
684   TEST_EQ_STR(pos_table_ptr->name()->c_str(), "MyGame.Example.Vec3");
685
686   // Now use it to dynamically access a buffer.
687   auto &root = *flatbuffers::GetAnyRoot(flatbuf);
688
689   // Verify the buffer first using reflection based verification
690   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(), flatbuf, length),
691           true);
692
693   auto hp = flatbuffers::GetFieldI<uint16_t>(root, hp_field);
694   TEST_EQ(hp, 80);
695
696   // Rather than needing to know the type, we can also get the value of
697   // any field as an int64_t/double/string, regardless of what it actually is.
698   auto hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
699   TEST_EQ(hp_int64, 80);
700   auto hp_double = flatbuffers::GetAnyFieldF(root, hp_field);
701   TEST_EQ(hp_double, 80.0);
702   auto hp_string = flatbuffers::GetAnyFieldS(root, hp_field, &schema);
703   TEST_EQ_STR(hp_string.c_str(), "80");
704
705   // Get struct field through reflection
706   auto pos_struct = flatbuffers::GetFieldStruct(root, *pos_field_ptr);
707   TEST_NOTNULL(pos_struct);
708   TEST_EQ(flatbuffers::GetAnyFieldF(*pos_struct,
709                                     *pos_table_ptr->fields()->LookupByKey("z")),
710           3.0f);
711
712   auto test3_field = pos_table_ptr->fields()->LookupByKey("test3");
713   auto test3_struct = flatbuffers::GetFieldStruct(*pos_struct, *test3_field);
714   TEST_NOTNULL(test3_struct);
715   auto test3_object = schema.objects()->Get(test3_field->type()->index());
716
717   TEST_EQ(flatbuffers::GetAnyFieldF(*test3_struct,
718                                     *test3_object->fields()->LookupByKey("a")),
719           10);
720
721   // We can also modify it.
722   flatbuffers::SetField<uint16_t>(&root, hp_field, 200);
723   hp = flatbuffers::GetFieldI<uint16_t>(root, hp_field);
724   TEST_EQ(hp, 200);
725
726   // We can also set fields generically:
727   flatbuffers::SetAnyFieldI(&root, hp_field, 300);
728   hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
729   TEST_EQ(hp_int64, 300);
730   flatbuffers::SetAnyFieldF(&root, hp_field, 300.5);
731   hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
732   TEST_EQ(hp_int64, 300);
733   flatbuffers::SetAnyFieldS(&root, hp_field, "300");
734   hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
735   TEST_EQ(hp_int64, 300);
736
737   // Test buffer is valid after the modifications
738   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(), flatbuf, length),
739           true);
740
741   // Reset it, for further tests.
742   flatbuffers::SetField<uint16_t>(&root, hp_field, 80);
743
744   // More advanced functionality: changing the size of items in-line!
745   // First we put the FlatBuffer inside an std::vector.
746   std::vector<uint8_t> resizingbuf(flatbuf, flatbuf + length);
747   // Find the field we want to modify.
748   auto &name_field = *fields->LookupByKey("name");
749   // Get the root.
750   // This time we wrap the result from GetAnyRoot in a smartpointer that
751   // will keep rroot valid as resizingbuf resizes.
752   auto rroot = flatbuffers::piv(
753       flatbuffers::GetAnyRoot(flatbuffers::vector_data(resizingbuf)),
754       resizingbuf);
755   SetString(schema, "totally new string", GetFieldS(**rroot, name_field),
756             &resizingbuf);
757   // Here resizingbuf has changed, but rroot is still valid.
758   TEST_EQ_STR(GetFieldS(**rroot, name_field)->c_str(), "totally new string");
759   // Now lets extend a vector by 100 elements (10 -> 110).
760   auto &inventory_field = *fields->LookupByKey("inventory");
761   auto rinventory = flatbuffers::piv(
762       flatbuffers::GetFieldV<uint8_t>(**rroot, inventory_field), resizingbuf);
763   flatbuffers::ResizeVector<uint8_t>(schema, 110, 50, *rinventory,
764                                      &resizingbuf);
765   // rinventory still valid, so lets read from it.
766   TEST_EQ(rinventory->Get(10), 50);
767
768   // For reflection uses not covered already, there is a more powerful way:
769   // we can simply generate whatever object we want to add/modify in a
770   // FlatBuffer of its own, then add that to an existing FlatBuffer:
771   // As an example, let's add a string to an array of strings.
772   // First, find our field:
773   auto &testarrayofstring_field = *fields->LookupByKey("testarrayofstring");
774   // Find the vector value:
775   auto rtestarrayofstring = flatbuffers::piv(
776       flatbuffers::GetFieldV<flatbuffers::Offset<flatbuffers::String>>(
777           **rroot, testarrayofstring_field),
778       resizingbuf);
779   // It's a vector of 2 strings, to which we add one more, initialized to
780   // offset 0.
781   flatbuffers::ResizeVector<flatbuffers::Offset<flatbuffers::String>>(
782       schema, 3, 0, *rtestarrayofstring, &resizingbuf);
783   // Here we just create a buffer that contans a single string, but this
784   // could also be any complex set of tables and other values.
785   flatbuffers::FlatBufferBuilder stringfbb;
786   stringfbb.Finish(stringfbb.CreateString("hank"));
787   // Add the contents of it to our existing FlatBuffer.
788   // We do this last, so the pointer doesn't get invalidated (since it is
789   // at the end of the buffer):
790   auto string_ptr = flatbuffers::AddFlatBuffer(
791       resizingbuf, stringfbb.GetBufferPointer(), stringfbb.GetSize());
792   // Finally, set the new value in the vector.
793   rtestarrayofstring->MutateOffset(2, string_ptr);
794   TEST_EQ_STR(rtestarrayofstring->Get(0)->c_str(), "bob");
795   TEST_EQ_STR(rtestarrayofstring->Get(2)->c_str(), "hank");
796   // Test integrity of all resize operations above.
797   flatbuffers::Verifier resize_verifier(
798       reinterpret_cast<const uint8_t *>(flatbuffers::vector_data(resizingbuf)),
799       resizingbuf.size());
800   TEST_EQ(VerifyMonsterBuffer(resize_verifier), true);
801
802   // Test buffer is valid using reflection as well
803   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(),
804                               flatbuffers::vector_data(resizingbuf),
805                               resizingbuf.size()),
806           true);
807
808   // As an additional test, also set it on the name field.
809   // Note: unlike the name change above, this just overwrites the offset,
810   // rather than changing the string in-place.
811   SetFieldT(*rroot, name_field, string_ptr);
812   TEST_EQ_STR(GetFieldS(**rroot, name_field)->c_str(), "hank");
813
814   // Using reflection, rather than mutating binary FlatBuffers, we can also copy
815   // tables and other things out of other FlatBuffers into a FlatBufferBuilder,
816   // either part or whole.
817   flatbuffers::FlatBufferBuilder fbb;
818   auto root_offset = flatbuffers::CopyTable(
819       fbb, schema, *root_table, *flatbuffers::GetAnyRoot(flatbuf), true);
820   fbb.Finish(root_offset, MonsterIdentifier());
821   // Test that it was copied correctly:
822   AccessFlatBufferTest(fbb.GetBufferPointer(), fbb.GetSize());
823
824   // Test buffer is valid using reflection as well
825   TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(),
826                               fbb.GetBufferPointer(), fbb.GetSize()),
827           true);
828 }
829
830 void MiniReflectFlatBuffersTest(uint8_t *flatbuf) {
831   auto s = flatbuffers::FlatBufferToString(flatbuf, Monster::MiniReflectTypeTable());
832   TEST_EQ_STR(
833       s.c_str(),
834       "{ "
835       "pos: { x: 1.0, y: 2.0, z: 3.0, test1: 0.0, test2: Red, test3: "
836       "{ a: 10, b: 20 } }, "
837       "hp: 80, "
838       "name: \"MyMonster\", "
839       "inventory: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "
840       "test_type: Monster, "
841       "test: { name: \"Fred\" }, "
842       "test4: [ { a: 10, b: 20 }, { a: 30, b: 40 } ], "
843       "testarrayofstring: [ \"bob\", \"fred\", \"bob\", \"fred\" ], "
844       "testarrayoftables: [ { hp: 1000, name: \"Barney\" }, { name: \"Fred\" "
845       "}, "
846       "{ name: \"Wilma\" } ], "
847       // TODO(wvo): should really print this nested buffer correctly.
848       "testnestedflatbuffer: [ 20, 0, 0, 0, 77, 79, 78, 83, 12, 0, 12, 0, 0, "
849       "0, "
850       "4, 0, 6, 0, 8, 0, 12, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 13, 0, 0, 0, 78, "
851       "101, 115, 116, 101, 100, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0 ], "
852       "testarrayofstring2: [ \"jane\", \"mary\" ], "
853       "testarrayofsortedstruct: [ { id: 1, distance: 10 }, "
854       "{ id: 2, distance: 20 }, { id: 3, distance: 30 }, "
855       "{ id: 4, distance: 40 } ], "
856       "flex: [ 210, 4, 5, 2 ], "
857       "test5: [ { a: 10, b: 20 }, { a: 30, b: 40 } ] "
858       "}");
859 }
860
861 // Parse a .proto schema, output as .fbs
862 void ParseProtoTest() {
863   // load the .proto and the golden file from disk
864   std::string protofile;
865   std::string goldenfile;
866   std::string goldenunionfile;
867   TEST_EQ(
868       flatbuffers::LoadFile((test_data_path + "prototest/test.proto").c_str(),
869                             false, &protofile),
870       true);
871   TEST_EQ(
872       flatbuffers::LoadFile((test_data_path + "prototest/test.golden").c_str(),
873                             false, &goldenfile),
874       true);
875   TEST_EQ(
876       flatbuffers::LoadFile((test_data_path +
877                             "prototest/test_union.golden").c_str(),
878                             false, &goldenunionfile),
879       true);
880
881   flatbuffers::IDLOptions opts;
882   opts.include_dependence_headers = false;
883   opts.proto_mode = true;
884
885   // Parse proto.
886   flatbuffers::Parser parser(opts);
887   auto protopath = test_data_path + "prototest/";
888   const char *include_directories[] = { protopath.c_str(), nullptr };
889   TEST_EQ(parser.Parse(protofile.c_str(), include_directories), true);
890
891   // Generate fbs.
892   auto fbs = flatbuffers::GenerateFBS(parser, "test");
893
894   // Ensure generated file is parsable.
895   flatbuffers::Parser parser2;
896   TEST_EQ(parser2.Parse(fbs.c_str(), nullptr), true);
897   TEST_EQ_STR(fbs.c_str(), goldenfile.c_str());
898
899   // Parse proto with --oneof-union option.
900   opts.proto_oneof_union = true;
901   flatbuffers::Parser parser3(opts);
902   TEST_EQ(parser3.Parse(protofile.c_str(), include_directories), true);
903
904   // Generate fbs.
905   auto fbs_union = flatbuffers::GenerateFBS(parser3, "test");
906
907   // Ensure generated file is parsable.
908   flatbuffers::Parser parser4;
909   TEST_EQ(parser4.Parse(fbs_union.c_str(), nullptr), true);
910   TEST_EQ_STR(fbs_union.c_str(), goldenunionfile.c_str());
911 }
912
913 template<typename T>
914 void CompareTableFieldValue(flatbuffers::Table *table,
915                             flatbuffers::voffset_t voffset, T val) {
916   T read = table->GetField(voffset, static_cast<T>(0));
917   TEST_EQ(read, val);
918 }
919
920 // Low level stress/fuzz test: serialize/deserialize a variety of
921 // different kinds of data in different combinations
922 void FuzzTest1() {
923   // Values we're testing against: chosen to ensure no bits get chopped
924   // off anywhere, and also be different from eachother.
925   const uint8_t bool_val = true;
926   const int8_t char_val = -127;  // 0x81
927   const uint8_t uchar_val = 0xFF;
928   const int16_t short_val = -32222;  // 0x8222;
929   const uint16_t ushort_val = 0xFEEE;
930   const int32_t int_val = 0x83333333;
931   const uint32_t uint_val = 0xFDDDDDDD;
932   const int64_t long_val = 0x8444444444444444LL;
933   const uint64_t ulong_val = 0xFCCCCCCCCCCCCCCCULL;
934   const float float_val = 3.14159f;
935   const double double_val = 3.14159265359;
936
937   const int test_values_max = 11;
938   const flatbuffers::voffset_t fields_per_object = 4;
939   const int num_fuzz_objects = 10000;  // The higher, the more thorough :)
940
941   flatbuffers::FlatBufferBuilder builder;
942
943   lcg_reset();  // Keep it deterministic.
944
945   flatbuffers::uoffset_t objects[num_fuzz_objects];
946
947   // Generate num_fuzz_objects random objects each consisting of
948   // fields_per_object fields, each of a random type.
949   for (int i = 0; i < num_fuzz_objects; i++) {
950     auto start = builder.StartTable();
951     for (flatbuffers::voffset_t f = 0; f < fields_per_object; f++) {
952       int choice = lcg_rand() % test_values_max;
953       auto off = flatbuffers::FieldIndexToOffset(f);
954       switch (choice) {
955         case 0: builder.AddElement<uint8_t>(off, bool_val, 0); break;
956         case 1: builder.AddElement<int8_t>(off, char_val, 0); break;
957         case 2: builder.AddElement<uint8_t>(off, uchar_val, 0); break;
958         case 3: builder.AddElement<int16_t>(off, short_val, 0); break;
959         case 4: builder.AddElement<uint16_t>(off, ushort_val, 0); break;
960         case 5: builder.AddElement<int32_t>(off, int_val, 0); break;
961         case 6: builder.AddElement<uint32_t>(off, uint_val, 0); break;
962         case 7: builder.AddElement<int64_t>(off, long_val, 0); break;
963         case 8: builder.AddElement<uint64_t>(off, ulong_val, 0); break;
964         case 9: builder.AddElement<float>(off, float_val, 0); break;
965         case 10: builder.AddElement<double>(off, double_val, 0); break;
966       }
967     }
968     objects[i] = builder.EndTable(start);
969   }
970   builder.PreAlign<flatbuffers::largest_scalar_t>(0);  // Align whole buffer.
971
972   lcg_reset();  // Reset.
973
974   uint8_t *eob = builder.GetCurrentBufferPointer() + builder.GetSize();
975
976   // Test that all objects we generated are readable and return the
977   // expected values. We generate random objects in the same order
978   // so this is deterministic.
979   for (int i = 0; i < num_fuzz_objects; i++) {
980     auto table = reinterpret_cast<flatbuffers::Table *>(eob - objects[i]);
981     for (flatbuffers::voffset_t f = 0; f < fields_per_object; f++) {
982       int choice = lcg_rand() % test_values_max;
983       flatbuffers::voffset_t off = flatbuffers::FieldIndexToOffset(f);
984       switch (choice) {
985         case 0: CompareTableFieldValue(table, off, bool_val); break;
986         case 1: CompareTableFieldValue(table, off, char_val); break;
987         case 2: CompareTableFieldValue(table, off, uchar_val); break;
988         case 3: CompareTableFieldValue(table, off, short_val); break;
989         case 4: CompareTableFieldValue(table, off, ushort_val); break;
990         case 5: CompareTableFieldValue(table, off, int_val); break;
991         case 6: CompareTableFieldValue(table, off, uint_val); break;
992         case 7: CompareTableFieldValue(table, off, long_val); break;
993         case 8: CompareTableFieldValue(table, off, ulong_val); break;
994         case 9: CompareTableFieldValue(table, off, float_val); break;
995         case 10: CompareTableFieldValue(table, off, double_val); break;
996       }
997     }
998   }
999 }
1000
1001 // High level stress/fuzz test: generate a big schema and
1002 // matching json data in random combinations, then parse both,
1003 // generate json back from the binary, and compare with the original.
1004 void FuzzTest2() {
1005   lcg_reset();  // Keep it deterministic.
1006
1007   const int num_definitions = 30;
1008   const int num_struct_definitions = 5;  // Subset of num_definitions.
1009   const int fields_per_definition = 15;
1010   const int instances_per_definition = 5;
1011   const int deprecation_rate = 10;  // 1 in deprecation_rate fields will
1012                                     // be deprecated.
1013
1014   std::string schema = "namespace test;\n\n";
1015
1016   struct RndDef {
1017     std::string instances[instances_per_definition];
1018
1019     // Since we're generating schema and corresponding data in tandem,
1020     // this convenience function adds strings to both at once.
1021     static void Add(RndDef (&definitions_l)[num_definitions],
1022                     std::string &schema_l, const int instances_per_definition_l,
1023                     const char *schema_add, const char *instance_add,
1024                     int definition) {
1025       schema_l += schema_add;
1026       for (int i = 0; i < instances_per_definition_l; i++)
1027         definitions_l[definition].instances[i] += instance_add;
1028     }
1029   };
1030
1031   // clang-format off
1032   #define AddToSchemaAndInstances(schema_add, instance_add) \
1033     RndDef::Add(definitions, schema, instances_per_definition, \
1034                 schema_add, instance_add, definition)
1035
1036   #define Dummy() \
1037     RndDef::Add(definitions, schema, instances_per_definition, \
1038                 "byte", "1", definition)
1039   // clang-format on
1040
1041   RndDef definitions[num_definitions];
1042
1043   // We are going to generate num_definitions, the first
1044   // num_struct_definitions will be structs, the rest tables. For each
1045   // generate random fields, some of which may be struct/table types
1046   // referring to previously generated structs/tables.
1047   // Simultanenously, we generate instances_per_definition JSON data
1048   // definitions, which will have identical structure to the schema
1049   // being generated. We generate multiple instances such that when creating
1050   // hierarchy, we get some variety by picking one randomly.
1051   for (int definition = 0; definition < num_definitions; definition++) {
1052     std::string definition_name = "D" + flatbuffers::NumToString(definition);
1053
1054     bool is_struct = definition < num_struct_definitions;
1055
1056     AddToSchemaAndInstances(
1057         ((is_struct ? "struct " : "table ") + definition_name + " {\n").c_str(),
1058         "{\n");
1059
1060     for (int field = 0; field < fields_per_definition; field++) {
1061       const bool is_last_field = field == fields_per_definition - 1;
1062
1063       // Deprecate 1 in deprecation_rate fields. Only table fields can be
1064       // deprecated.
1065       // Don't deprecate the last field to avoid dangling commas in JSON.
1066       const bool deprecated =
1067           !is_struct && !is_last_field && (lcg_rand() % deprecation_rate == 0);
1068
1069       std::string field_name = "f" + flatbuffers::NumToString(field);
1070       AddToSchemaAndInstances(("  " + field_name + ":").c_str(),
1071                               deprecated ? "" : (field_name + ": ").c_str());
1072       // Pick random type:
1073       auto base_type = static_cast<flatbuffers::BaseType>(
1074           lcg_rand() % (flatbuffers::BASE_TYPE_UNION + 1));
1075       switch (base_type) {
1076         case flatbuffers::BASE_TYPE_STRING:
1077           if (is_struct) {
1078             Dummy();  // No strings in structs.
1079           } else {
1080             AddToSchemaAndInstances("string", deprecated ? "" : "\"hi\"");
1081           }
1082           break;
1083         case flatbuffers::BASE_TYPE_VECTOR:
1084           if (is_struct) {
1085             Dummy();  // No vectors in structs.
1086           } else {
1087             AddToSchemaAndInstances("[ubyte]",
1088                                     deprecated ? "" : "[\n0,\n1,\n255\n]");
1089           }
1090           break;
1091         case flatbuffers::BASE_TYPE_NONE:
1092         case flatbuffers::BASE_TYPE_UTYPE:
1093         case flatbuffers::BASE_TYPE_STRUCT:
1094         case flatbuffers::BASE_TYPE_UNION:
1095           if (definition) {
1096             // Pick a random previous definition and random data instance of
1097             // that definition.
1098             int defref = lcg_rand() % definition;
1099             int instance = lcg_rand() % instances_per_definition;
1100             AddToSchemaAndInstances(
1101                 ("D" + flatbuffers::NumToString(defref)).c_str(),
1102                 deprecated ? ""
1103                            : definitions[defref].instances[instance].c_str());
1104           } else {
1105             // If this is the first definition, we have no definition we can
1106             // refer to.
1107             Dummy();
1108           }
1109           break;
1110         case flatbuffers::BASE_TYPE_BOOL:
1111           AddToSchemaAndInstances(
1112               "bool", deprecated ? "" : (lcg_rand() % 2 ? "true" : "false"));
1113           break;
1114         default:
1115           // All the scalar types.
1116           schema += flatbuffers::kTypeNames[base_type];
1117
1118           if (!deprecated) {
1119             // We want each instance to use its own random value.
1120             for (int inst = 0; inst < instances_per_definition; inst++)
1121               definitions[definition].instances[inst] +=
1122                   flatbuffers::IsFloat(base_type)
1123                       ? flatbuffers::NumToString<double>(lcg_rand() % 128)
1124                             .c_str()
1125                       : flatbuffers::NumToString<int>(lcg_rand() % 128).c_str();
1126           }
1127       }
1128       AddToSchemaAndInstances(deprecated ? "(deprecated);\n" : ";\n",
1129                               deprecated ? "" : is_last_field ? "\n" : ",\n");
1130     }
1131     AddToSchemaAndInstances("}\n\n", "}");
1132   }
1133
1134   schema += "root_type D" + flatbuffers::NumToString(num_definitions - 1);
1135   schema += ";\n";
1136
1137   flatbuffers::Parser parser;
1138
1139   // Will not compare against the original if we don't write defaults
1140   parser.builder_.ForceDefaults(true);
1141
1142   // Parse the schema, parse the generated data, then generate text back
1143   // from the binary and compare against the original.
1144   TEST_EQ(parser.Parse(schema.c_str()), true);
1145
1146   const std::string &json =
1147       definitions[num_definitions - 1].instances[0] + "\n";
1148
1149   TEST_EQ(parser.Parse(json.c_str()), true);
1150
1151   std::string jsongen;
1152   parser.opts.indent_step = 0;
1153   auto result =
1154       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1155   TEST_EQ(result, true);
1156
1157   if (jsongen != json) {
1158     // These strings are larger than a megabyte, so we show the bytes around
1159     // the first bytes that are different rather than the whole string.
1160     size_t len = std::min(json.length(), jsongen.length());
1161     for (size_t i = 0; i < len; i++) {
1162       if (json[i] != jsongen[i]) {
1163         i -= std::min(static_cast<size_t>(10), i);  // show some context;
1164         size_t end = std::min(len, i + 20);
1165         for (; i < end; i++)
1166           TEST_OUTPUT_LINE("at %d: found \"%c\", expected \"%c\"\n",
1167                            static_cast<int>(i), jsongen[i], json[i]);
1168         break;
1169       }
1170     }
1171     TEST_NOTNULL(NULL);
1172   }
1173
1174   // clang-format off
1175   #ifdef FLATBUFFERS_TEST_VERBOSE
1176     TEST_OUTPUT_LINE("%dk schema tested with %dk of json\n",
1177                      static_cast<int>(schema.length() / 1024),
1178                      static_cast<int>(json.length() / 1024));
1179   #endif
1180   // clang-format on
1181 }
1182
1183 // Test that parser errors are actually generated.
1184 void TestError(const char *src, const char *error_substr,
1185                bool strict_json = false) {
1186   flatbuffers::IDLOptions opts;
1187   opts.strict_json = strict_json;
1188   flatbuffers::Parser parser(opts);
1189   TEST_EQ(parser.Parse(src), false);  // Must signal error
1190   // Must be the error we're expecting
1191   TEST_NOTNULL(strstr(parser.error_.c_str(), error_substr));
1192 }
1193
1194 // Test that parsing errors occur as we'd expect.
1195 // Also useful for coverage, making sure these paths are run.
1196 void ErrorTest() {
1197   // In order they appear in idl_parser.cpp
1198   TestError("table X { Y:byte; } root_type X; { Y: 999 }", "does not fit");
1199   TestError(".0", "floating point");
1200   TestError("\"\0", "illegal");
1201   TestError("\"\\q", "escape code");
1202   TestError("table ///", "documentation");
1203   TestError("@", "illegal");
1204   TestError("table 1", "expecting");
1205   TestError("table X { Y:[[int]]; }", "nested vector");
1206   TestError("table X { Y:1; }", "illegal type");
1207   TestError("table X { Y:int; Y:int; }", "field already");
1208   TestError("table Y {} table X { Y:int; }", "same as table");
1209   TestError("struct X { Y:string; }", "only scalar");
1210   TestError("table X { Y:string = \"\"; }", "default values");
1211   TestError("enum Y:byte { Z = 1 } table X { y:Y; }", "not part of enum");
1212   TestError("struct X { Y:int (deprecated); }", "deprecate");
1213   TestError("union Z { X } table X { Y:Z; } root_type X; { Y: {}, A:1 }",
1214             "missing type field");
1215   TestError("union Z { X } table X { Y:Z; } root_type X; { Y_type: 99, Y: {",
1216             "type id");
1217   TestError("table X { Y:int; } root_type X; { Z:", "unknown field");
1218   TestError("table X { Y:int; } root_type X; { Y:", "string constant", true);
1219   TestError("table X { Y:int; } root_type X; { \"Y\":1, }", "string constant",
1220             true);
1221   TestError(
1222       "struct X { Y:int; Z:int; } table W { V:X; } root_type W; "
1223       "{ V:{ Y:1 } }",
1224       "wrong number");
1225   TestError("enum E:byte { A } table X { Y:E; } root_type X; { Y:U }",
1226             "unknown enum value");
1227   TestError("table X { Y:byte; } root_type X; { Y:; }", "starting");
1228   TestError("enum X:byte { Y } enum X {", "enum already");
1229   TestError("enum X:float {}", "underlying");
1230   TestError("enum X:byte { Y, Y }", "value already");
1231   TestError("enum X:byte { Y=2, Z=1 }", "ascending");
1232   TestError("union X { Y = 256 }", "must fit");
1233   TestError("enum X:byte (bit_flags) { Y=8 }", "bit flag out");
1234   TestError("table X { Y:int; } table X {", "datatype already");
1235   TestError("struct X (force_align: 7) { Y:int; }", "force_align");
1236   TestError("{}", "no root");
1237   TestError("table X { Y:byte; } root_type X; { Y:1 } { Y:1 }", "one json");
1238   TestError("root_type X;", "unknown root");
1239   TestError("struct X { Y:int; } root_type X;", "a table");
1240   TestError("union X { Y }", "referenced");
1241   TestError("union Z { X } struct X { Y:int; }", "only tables");
1242   TestError("table X { Y:[int]; YLength:int; }", "clash");
1243   TestError("table X { Y:byte; } root_type X; { Y:1, Y:2 }", "more than once");
1244 }
1245
1246 template<typename T> T TestValue(const char *json, const char *type_name) {
1247   flatbuffers::Parser parser;
1248
1249   // Simple schema.
1250   TEST_EQ(parser.Parse(std::string("table X { Y:" + std::string(type_name) +
1251                                    "; } root_type X;")
1252                            .c_str()),
1253           true);
1254
1255   TEST_EQ(parser.Parse(json), true);
1256   auto root = flatbuffers::GetRoot<flatbuffers::Table>(
1257       parser.builder_.GetBufferPointer());
1258   return root->GetField<T>(flatbuffers::FieldIndexToOffset(0), 0);
1259 }
1260
1261 bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; }
1262
1263 // Additional parser testing not covered elsewhere.
1264 void ValueTest() {
1265   // Test scientific notation numbers.
1266   TEST_EQ(FloatCompare(TestValue<float>("{ Y:0.0314159e+2 }", "float"),
1267                        (float)3.14159),
1268           true);
1269
1270   // Test conversion functions.
1271   TEST_EQ(FloatCompare(TestValue<float>("{ Y:cos(rad(180)) }", "float"), -1),
1272           true);
1273
1274   // Test negative hex constant.
1275   TEST_EQ(TestValue<int>("{ Y:-0x80 }", "int"), -128);
1276
1277   // Make sure we do unsigned 64bit correctly.
1278   TEST_EQ(TestValue<uint64_t>("{ Y:12335089644688340133 }", "ulong"),
1279           12335089644688340133ULL);
1280 }
1281
1282 void NestedListTest() {
1283   flatbuffers::Parser parser1;
1284   TEST_EQ(parser1.Parse("struct Test { a:short; b:byte; } table T { F:[Test]; }"
1285                         "root_type T;"
1286                         "{ F:[ [10,20], [30,40]] }"),
1287           true);
1288 }
1289
1290 void EnumStringsTest() {
1291   flatbuffers::Parser parser1;
1292   TEST_EQ(parser1.Parse("enum E:byte { A, B, C } table T { F:[E]; }"
1293                         "root_type T;"
1294                         "{ F:[ A, B, \"C\", \"A B C\" ] }"),
1295           true);
1296   flatbuffers::Parser parser2;
1297   TEST_EQ(parser2.Parse("enum E:byte { A, B, C } table T { F:[int]; }"
1298                         "root_type T;"
1299                         "{ F:[ \"E.C\", \"E.A E.B E.C\" ] }"),
1300           true);
1301 }
1302
1303 void IntegerOutOfRangeTest() {
1304   TestError("table T { F:byte; } root_type T; { F:128 }",
1305             "constant does not fit");
1306   TestError("table T { F:byte; } root_type T; { F:-129 }",
1307             "constant does not fit");
1308   TestError("table T { F:ubyte; } root_type T; { F:256 }",
1309             "constant does not fit");
1310   TestError("table T { F:ubyte; } root_type T; { F:-1 }",
1311             "constant does not fit");
1312   TestError("table T { F:short; } root_type T; { F:32768 }",
1313             "constant does not fit");
1314   TestError("table T { F:short; } root_type T; { F:-32769 }",
1315             "constant does not fit");
1316   TestError("table T { F:ushort; } root_type T; { F:65536 }",
1317             "constant does not fit");
1318   TestError("table T { F:ushort; } root_type T; { F:-1 }",
1319             "constant does not fit");
1320   TestError("table T { F:int; } root_type T; { F:2147483648 }",
1321             "constant does not fit");
1322   TestError("table T { F:int; } root_type T; { F:-2147483649 }",
1323             "constant does not fit");
1324   TestError("table T { F:uint; } root_type T; { F:4294967296 }",
1325             "constant does not fit");
1326   TestError("table T { F:uint; } root_type T; { F:-1 }",
1327             "constant does not fit");
1328 }
1329
1330 void IntegerBoundaryTest() {
1331   TEST_EQ(TestValue<int8_t>("{ Y:127 }", "byte"), 127);
1332   TEST_EQ(TestValue<int8_t>("{ Y:-128 }", "byte"), -128);
1333   TEST_EQ(TestValue<uint8_t>("{ Y:255 }", "ubyte"), 255);
1334   TEST_EQ(TestValue<uint8_t>("{ Y:0 }", "ubyte"), 0);
1335   TEST_EQ(TestValue<int16_t>("{ Y:32767 }", "short"), 32767);
1336   TEST_EQ(TestValue<int16_t>("{ Y:-32768 }", "short"), -32768);
1337   TEST_EQ(TestValue<uint16_t>("{ Y:65535 }", "ushort"), 65535);
1338   TEST_EQ(TestValue<uint16_t>("{ Y:0 }", "ushort"), 0);
1339   TEST_EQ(TestValue<int32_t>("{ Y:2147483647 }", "int"), 2147483647);
1340   TEST_EQ(TestValue<int32_t>("{ Y:-2147483648 }", "int"), (-2147483647 - 1));
1341   TEST_EQ(TestValue<uint32_t>("{ Y:4294967295 }", "uint"), 4294967295);
1342   TEST_EQ(TestValue<uint32_t>("{ Y:0 }", "uint"), 0);
1343   TEST_EQ(TestValue<int64_t>("{ Y:9223372036854775807 }", "long"),
1344           9223372036854775807);
1345   TEST_EQ(TestValue<int64_t>("{ Y:-9223372036854775808 }", "long"),
1346           (-9223372036854775807 - 1));
1347   TEST_EQ(TestValue<uint64_t>("{ Y:18446744073709551615 }", "ulong"),
1348           18446744073709551615U);
1349   TEST_EQ(TestValue<uint64_t>("{ Y:0 }", "ulong"), 0);
1350 }
1351
1352 void UnicodeTest() {
1353   flatbuffers::Parser parser;
1354   // Without setting allow_non_utf8 = true, we treat \x sequences as byte
1355   // sequences which are then validated as UTF-8.
1356   TEST_EQ(parser.Parse("table T { F:string; }"
1357                        "root_type T;"
1358                        "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1359                        "\\u5225\\u30B5\\u30A4\\u30C8\\xE2\\x82\\xAC\\u0080\\uD8"
1360                        "3D\\uDE0E\" }"),
1361           true);
1362   std::string jsongen;
1363   parser.opts.indent_step = -1;
1364   auto result =
1365       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1366   TEST_EQ(result, true);
1367   TEST_EQ_STR(jsongen.c_str(),
1368               "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1369               "\\u5225\\u30B5\\u30A4\\u30C8\\u20AC\\u0080\\uD83D\\uDE0E\"}");
1370 }
1371
1372 void UnicodeTestAllowNonUTF8() {
1373   flatbuffers::Parser parser;
1374   parser.opts.allow_non_utf8 = true;
1375   TEST_EQ(
1376       parser.Parse(
1377           "table T { F:string; }"
1378           "root_type T;"
1379           "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1380           "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\\u0080\\uD83D\\uDE0E\" }"),
1381       true);
1382   std::string jsongen;
1383   parser.opts.indent_step = -1;
1384   auto result =
1385       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1386   TEST_EQ(result, true);
1387   TEST_EQ_STR(
1388       jsongen.c_str(),
1389       "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1390       "\\u5225\\u30B5\\u30A4\\u30C8\\u0001\\x80\\u0080\\uD83D\\uDE0E\"}");
1391 }
1392
1393 void UnicodeTestGenerateTextFailsOnNonUTF8() {
1394   flatbuffers::Parser parser;
1395   // Allow non-UTF-8 initially to model what happens when we load a binary
1396   // flatbuffer from disk which contains non-UTF-8 strings.
1397   parser.opts.allow_non_utf8 = true;
1398   TEST_EQ(
1399       parser.Parse(
1400           "table T { F:string; }"
1401           "root_type T;"
1402           "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
1403           "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\\u0080\\uD83D\\uDE0E\" }"),
1404       true);
1405   std::string jsongen;
1406   parser.opts.indent_step = -1;
1407   // Now, disallow non-UTF-8 (the default behavior) so GenerateText indicates
1408   // failure.
1409   parser.opts.allow_non_utf8 = false;
1410   auto result =
1411       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1412   TEST_EQ(result, false);
1413 }
1414
1415 void UnicodeSurrogatesTest() {
1416   flatbuffers::Parser parser;
1417
1418   TEST_EQ(parser.Parse("table T { F:string (id: 0); }"
1419                        "root_type T;"
1420                        "{ F:\"\\uD83D\\uDCA9\"}"),
1421           true);
1422   auto root = flatbuffers::GetRoot<flatbuffers::Table>(
1423       parser.builder_.GetBufferPointer());
1424   auto string = root->GetPointer<flatbuffers::String *>(
1425       flatbuffers::FieldIndexToOffset(0));
1426   TEST_EQ_STR(string->c_str(), "\xF0\x9F\x92\xA9");
1427 }
1428
1429 void UnicodeInvalidSurrogatesTest() {
1430   TestError(
1431       "table T { F:string; }"
1432       "root_type T;"
1433       "{ F:\"\\uD800\"}",
1434       "unpaired high surrogate");
1435   TestError(
1436       "table T { F:string; }"
1437       "root_type T;"
1438       "{ F:\"\\uD800abcd\"}",
1439       "unpaired high surrogate");
1440   TestError(
1441       "table T { F:string; }"
1442       "root_type T;"
1443       "{ F:\"\\uD800\\n\"}",
1444       "unpaired high surrogate");
1445   TestError(
1446       "table T { F:string; }"
1447       "root_type T;"
1448       "{ F:\"\\uD800\\uD800\"}",
1449       "multiple high surrogates");
1450   TestError(
1451       "table T { F:string; }"
1452       "root_type T;"
1453       "{ F:\"\\uDC00\"}",
1454       "unpaired low surrogate");
1455 }
1456
1457 void InvalidUTF8Test() {
1458   // "1 byte" pattern, under min length of 2 bytes
1459   TestError(
1460       "table T { F:string; }"
1461       "root_type T;"
1462       "{ F:\"\x80\"}",
1463       "illegal UTF-8 sequence");
1464   // 2 byte pattern, string too short
1465   TestError(
1466       "table T { F:string; }"
1467       "root_type T;"
1468       "{ F:\"\xDF\"}",
1469       "illegal UTF-8 sequence");
1470   // 3 byte pattern, string too short
1471   TestError(
1472       "table T { F:string; }"
1473       "root_type T;"
1474       "{ F:\"\xEF\xBF\"}",
1475       "illegal UTF-8 sequence");
1476   // 4 byte pattern, string too short
1477   TestError(
1478       "table T { F:string; }"
1479       "root_type T;"
1480       "{ F:\"\xF7\xBF\xBF\"}",
1481       "illegal UTF-8 sequence");
1482   // "5 byte" pattern, string too short
1483   TestError(
1484       "table T { F:string; }"
1485       "root_type T;"
1486       "{ F:\"\xFB\xBF\xBF\xBF\"}",
1487       "illegal UTF-8 sequence");
1488   // "6 byte" pattern, string too short
1489   TestError(
1490       "table T { F:string; }"
1491       "root_type T;"
1492       "{ F:\"\xFD\xBF\xBF\xBF\xBF\"}",
1493       "illegal UTF-8 sequence");
1494   // "7 byte" pattern, string too short
1495   TestError(
1496       "table T { F:string; }"
1497       "root_type T;"
1498       "{ F:\"\xFE\xBF\xBF\xBF\xBF\xBF\"}",
1499       "illegal UTF-8 sequence");
1500   // "5 byte" pattern, over max length of 4 bytes
1501   TestError(
1502       "table T { F:string; }"
1503       "root_type T;"
1504       "{ F:\"\xFB\xBF\xBF\xBF\xBF\"}",
1505       "illegal UTF-8 sequence");
1506   // "6 byte" pattern, over max length of 4 bytes
1507   TestError(
1508       "table T { F:string; }"
1509       "root_type T;"
1510       "{ F:\"\xFD\xBF\xBF\xBF\xBF\xBF\"}",
1511       "illegal UTF-8 sequence");
1512   // "7 byte" pattern, over max length of 4 bytes
1513   TestError(
1514       "table T { F:string; }"
1515       "root_type T;"
1516       "{ F:\"\xFE\xBF\xBF\xBF\xBF\xBF\xBF\"}",
1517       "illegal UTF-8 sequence");
1518
1519   // Three invalid encodings for U+000A (\n, aka NEWLINE)
1520   TestError(
1521       "table T { F:string; }"
1522       "root_type T;"
1523       "{ F:\"\xC0\x8A\"}",
1524       "illegal UTF-8 sequence");
1525   TestError(
1526       "table T { F:string; }"
1527       "root_type T;"
1528       "{ F:\"\xE0\x80\x8A\"}",
1529       "illegal UTF-8 sequence");
1530   TestError(
1531       "table T { F:string; }"
1532       "root_type T;"
1533       "{ F:\"\xF0\x80\x80\x8A\"}",
1534       "illegal UTF-8 sequence");
1535
1536   // Two invalid encodings for U+00A9 (COPYRIGHT SYMBOL)
1537   TestError(
1538       "table T { F:string; }"
1539       "root_type T;"
1540       "{ F:\"\xE0\x81\xA9\"}",
1541       "illegal UTF-8 sequence");
1542   TestError(
1543       "table T { F:string; }"
1544       "root_type T;"
1545       "{ F:\"\xF0\x80\x81\xA9\"}",
1546       "illegal UTF-8 sequence");
1547
1548   // Invalid encoding for U+20AC (EURO SYMBOL)
1549   TestError(
1550       "table T { F:string; }"
1551       "root_type T;"
1552       "{ F:\"\xF0\x82\x82\xAC\"}",
1553       "illegal UTF-8 sequence");
1554
1555   // UTF-16 surrogate values between U+D800 and U+DFFF cannot be encoded in
1556   // UTF-8
1557   TestError(
1558       "table T { F:string; }"
1559       "root_type T;"
1560       // U+10400 "encoded" as U+D801 U+DC00
1561       "{ F:\"\xED\xA0\x81\xED\xB0\x80\"}",
1562       "illegal UTF-8 sequence");
1563 }
1564
1565 void UnknownFieldsTest() {
1566   flatbuffers::IDLOptions opts;
1567   opts.skip_unexpected_fields_in_json = true;
1568   flatbuffers::Parser parser(opts);
1569
1570   TEST_EQ(parser.Parse("table T { str:string; i:int;}"
1571                        "root_type T;"
1572                        "{ str:\"test\","
1573                        "unknown_string:\"test\","
1574                        "\"unknown_string\":\"test\","
1575                        "unknown_int:10,"
1576                        "unknown_float:1.0,"
1577                        "unknown_array: [ 1, 2, 3, 4],"
1578                        "unknown_object: { i: 10 },"
1579                        "\"unknown_object\": { \"i\": 10 },"
1580                        "i:10}"),
1581           true);
1582
1583   std::string jsongen;
1584   parser.opts.indent_step = -1;
1585   auto result =
1586       GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1587   TEST_EQ(result, true);
1588   TEST_EQ_STR(jsongen.c_str(), "{str: \"test\",i: 10}");
1589 }
1590
1591 void ParseUnionTest() {
1592   // Unions must be parseable with the type field following the object.
1593   flatbuffers::Parser parser;
1594   TEST_EQ(parser.Parse("table T { A:int; }"
1595                        "union U { T }"
1596                        "table V { X:U; }"
1597                        "root_type V;"
1598                        "{ X:{ A:1 }, X_type: T }"),
1599           true);
1600   // Unions must be parsable with prefixed namespace.
1601   flatbuffers::Parser parser2;
1602   TEST_EQ(parser2.Parse("namespace N; table A {} namespace; union U { N.A }"
1603                         "table B { e:U; } root_type B;"
1604                         "{ e_type: N_A, e: {} }"),
1605           true);
1606 }
1607
1608 void UnionVectorTest() {
1609   // load FlatBuffer fbs schema.
1610   // TODO: load a JSON file with such a vector when JSON support is ready.
1611   std::string schemafile;
1612   TEST_EQ(flatbuffers::LoadFile(
1613               (test_data_path + "union_vector/union_vector.fbs").c_str(), false,
1614               &schemafile),
1615           true);
1616
1617   // parse schema.
1618   flatbuffers::IDLOptions idl_opts;
1619   idl_opts.lang_to_generate |= flatbuffers::IDLOptions::kCpp;
1620   flatbuffers::Parser parser(idl_opts);
1621   TEST_EQ(parser.Parse(schemafile.c_str()), true);
1622
1623   flatbuffers::FlatBufferBuilder fbb;
1624
1625   // union types.
1626   std::vector<uint8_t> types;
1627   types.push_back(static_cast<uint8_t>(Character_Belle));
1628   types.push_back(static_cast<uint8_t>(Character_MuLan));
1629   types.push_back(static_cast<uint8_t>(Character_BookFan));
1630   types.push_back(static_cast<uint8_t>(Character_Other));
1631   types.push_back(static_cast<uint8_t>(Character_Unused));
1632
1633   // union values.
1634   std::vector<flatbuffers::Offset<void>> characters;
1635   characters.push_back(fbb.CreateStruct(BookReader(/*books_read=*/7)).Union());
1636   characters.push_back(CreateAttacker(fbb, /*sword_attack_damage=*/5).Union());
1637   characters.push_back(fbb.CreateStruct(BookReader(/*books_read=*/2)).Union());
1638   characters.push_back(fbb.CreateString("Other").Union());
1639   characters.push_back(fbb.CreateString("Unused").Union());
1640
1641   // create Movie.
1642   const auto movie_offset =
1643       CreateMovie(fbb, Character_Rapunzel,
1644                   fbb.CreateStruct(Rapunzel(/*hair_length=*/6)).Union(),
1645                   fbb.CreateVector(types), fbb.CreateVector(characters));
1646   FinishMovieBuffer(fbb, movie_offset);
1647   auto buf = fbb.GetBufferPointer();
1648
1649   flatbuffers::Verifier verifier(buf, fbb.GetSize());
1650   TEST_EQ(VerifyMovieBuffer(verifier), true);
1651
1652   auto flat_movie = GetMovie(buf);
1653
1654   auto TestMovie = [](const Movie *movie) {
1655     TEST_EQ(movie->main_character_type() == Character_Rapunzel, true);
1656
1657     auto cts = movie->characters_type();
1658     TEST_EQ(movie->characters_type()->size(), 5);
1659     TEST_EQ(cts->GetEnum<Character>(0) == Character_Belle, true);
1660     TEST_EQ(cts->GetEnum<Character>(1) == Character_MuLan, true);
1661     TEST_EQ(cts->GetEnum<Character>(2) == Character_BookFan, true);
1662     TEST_EQ(cts->GetEnum<Character>(3) == Character_Other, true);
1663     TEST_EQ(cts->GetEnum<Character>(4) == Character_Unused, true);
1664
1665     auto rapunzel = movie->main_character_as_Rapunzel();
1666     TEST_EQ(rapunzel->hair_length(), 6);
1667
1668     auto cs = movie->characters();
1669     TEST_EQ(cs->size(), 5);
1670     auto belle = cs->GetAs<BookReader>(0);
1671     TEST_EQ(belle->books_read(), 7);
1672     auto mu_lan = cs->GetAs<Attacker>(1);
1673     TEST_EQ(mu_lan->sword_attack_damage(), 5);
1674     auto book_fan = cs->GetAs<BookReader>(2);
1675     TEST_EQ(book_fan->books_read(), 2);
1676     auto other = cs->GetAsString(3);
1677     TEST_EQ_STR(other->c_str(), "Other");
1678     auto unused = cs->GetAsString(4);
1679     TEST_EQ_STR(unused->c_str(), "Unused");
1680   };
1681
1682   TestMovie(flat_movie);
1683
1684   auto movie_object = flat_movie->UnPack();
1685   TEST_EQ(movie_object->main_character.AsRapunzel()->hair_length(), 6);
1686   TEST_EQ(movie_object->characters[0].AsBelle()->books_read(), 7);
1687   TEST_EQ(movie_object->characters[1].AsMuLan()->sword_attack_damage, 5);
1688   TEST_EQ(movie_object->characters[2].AsBookFan()->books_read(), 2);
1689   TEST_EQ_STR(movie_object->characters[3].AsOther()->c_str(), "Other");
1690   TEST_EQ_STR(movie_object->characters[4].AsUnused()->c_str(), "Unused");
1691
1692   fbb.Clear();
1693   fbb.Finish(Movie::Pack(fbb, movie_object));
1694
1695   delete movie_object;
1696
1697   auto repacked_movie = GetMovie(fbb.GetBufferPointer());
1698
1699   TestMovie(repacked_movie);
1700
1701   auto s =
1702       flatbuffers::FlatBufferToString(fbb.GetBufferPointer(), MovieTypeTable());
1703   TEST_EQ_STR(
1704       s.c_str(),
1705       "{ main_character_type: Rapunzel, main_character: { hair_length: 6 }, "
1706       "characters_type: [ Belle, MuLan, BookFan, Other, Unused ], "
1707       "characters: [ { books_read: 7 }, { sword_attack_damage: 5 }, "
1708       "{ books_read: 2 }, \"Other\", \"Unused\" ] }");
1709 }
1710
1711 void ConformTest() {
1712   flatbuffers::Parser parser;
1713   TEST_EQ(parser.Parse("table T { A:int; } enum E:byte { A }"), true);
1714
1715   auto test_conform = [](flatbuffers::Parser &parser1, const char *test,
1716                          const char *expected_err) {
1717     flatbuffers::Parser parser2;
1718     TEST_EQ(parser2.Parse(test), true);
1719     auto err = parser2.ConformTo(parser1);
1720     TEST_NOTNULL(strstr(err.c_str(), expected_err));
1721   };
1722
1723   test_conform(parser, "table T { A:byte; }", "types differ for field");
1724   test_conform(parser, "table T { B:int; A:int; }", "offsets differ for field");
1725   test_conform(parser, "table T { A:int = 1; }", "defaults differ for field");
1726   test_conform(parser, "table T { B:float; }",
1727                "field renamed to different type");
1728   test_conform(parser, "enum E:byte { B, A }", "values differ for enum");
1729 }
1730
1731 void ParseProtoBufAsciiTest() {
1732   // We can put the parser in a mode where it will accept JSON that looks more
1733   // like Protobuf ASCII, for users that have data in that format.
1734   // This uses no "" for field names (which we already support by default,
1735   // omits `,`, `:` before `{` and a couple of other features.
1736   flatbuffers::Parser parser;
1737   parser.opts.protobuf_ascii_alike = true;
1738   TEST_EQ(
1739       parser.Parse("table S { B:int; } table T { A:[int]; C:S; } root_type T;"),
1740       true);
1741   TEST_EQ(parser.Parse("{ A [1 2] C { B:2 }}"), true);
1742   // Similarly, in text output, it should omit these.
1743   std::string text;
1744   auto ok = flatbuffers::GenerateText(
1745       parser, parser.builder_.GetBufferPointer(), &text);
1746   TEST_EQ(ok, true);
1747   TEST_EQ_STR(text.c_str(),
1748               "{\n  A [\n    1\n    2\n  ]\n  C {\n    B: 2\n  }\n}\n");
1749 }
1750
1751 void FlexBuffersTest() {
1752   flexbuffers::Builder slb(512,
1753                            flexbuffers::BUILDER_FLAG_SHARE_KEYS_AND_STRINGS);
1754
1755   // Write the equivalent of:
1756   // { vec: [ -100, "Fred", 4.0, false ], bar: [ 1, 2, 3 ], bar3: [ 1, 2, 3 ],
1757   // foo: 100, bool: true, mymap: { foo: "Fred" } }
1758   // clang-format off
1759   #ifndef FLATBUFFERS_CPP98_STL
1760     // It's possible to do this without std::function support as well.
1761     slb.Map([&]() {
1762        slb.Vector("vec", [&]() {
1763         slb += -100;  // Equivalent to slb.Add(-100) or slb.Int(-100);
1764         slb += "Fred";
1765         slb.IndirectFloat(4.0f);
1766         uint8_t blob[] = { 77 };
1767         slb.Blob(blob, 1);
1768         slb += false;
1769       });
1770       int ints[] = { 1, 2, 3 };
1771       slb.Vector("bar", ints, 3);
1772       slb.FixedTypedVector("bar3", ints, 3);
1773       bool bools[] = {true, false, true, false};
1774       slb.Vector("bools", bools, 4);
1775       slb.Bool("bool", true);
1776       slb.Double("foo", 100);
1777       slb.Map("mymap", [&]() {
1778         slb.String("foo", "Fred");  // Testing key and string reuse.
1779       });
1780     });
1781     slb.Finish();
1782   #else
1783     // It's possible to do this without std::function support as well.
1784     slb.Map([](flexbuffers::Builder& slb2) {
1785        slb2.Vector("vec", [](flexbuffers::Builder& slb3) {
1786         slb3 += -100;  // Equivalent to slb.Add(-100) or slb.Int(-100);
1787         slb3 += "Fred";
1788         slb3.IndirectFloat(4.0f);
1789         uint8_t blob[] = { 77 };
1790         slb3.Blob(blob, 1);
1791         slb3 += false;
1792       }, slb2);
1793       int ints[] = { 1, 2, 3 };
1794       slb2.Vector("bar", ints, 3);
1795       slb2.FixedTypedVector("bar3", ints, 3);
1796       slb2.Bool("bool", true);
1797       slb2.Double("foo", 100);
1798       slb2.Map("mymap", [](flexbuffers::Builder& slb3) {
1799         slb3.String("foo", "Fred");  // Testing key and string reuse.
1800       }, slb2);
1801     }, slb);
1802     slb.Finish();
1803   #endif  // FLATBUFFERS_CPP98_STL
1804
1805   #ifdef FLATBUFFERS_TEST_VERBOSE
1806     for (size_t i = 0; i < slb.GetBuffer().size(); i++)
1807       printf("%d ", flatbuffers::vector_data(slb.GetBuffer())[i]);
1808     printf("\n");
1809   #endif
1810   // clang-format on
1811
1812   auto map = flexbuffers::GetRoot(slb.GetBuffer()).AsMap();
1813   TEST_EQ(map.size(), 7);
1814   auto vec = map["vec"].AsVector();
1815   TEST_EQ(vec.size(), 5);
1816   TEST_EQ(vec[0].AsInt64(), -100);
1817   TEST_EQ_STR(vec[1].AsString().c_str(), "Fred");
1818   TEST_EQ(vec[1].AsInt64(), 0);  // Number parsing failed.
1819   TEST_EQ(vec[2].AsDouble(), 4.0);
1820   TEST_EQ(vec[2].AsString().IsTheEmptyString(), true);  // Wrong Type.
1821   TEST_EQ_STR(vec[2].AsString().c_str(), "");     // This still works though.
1822   TEST_EQ_STR(vec[2].ToString().c_str(), "4.0");  // Or have it converted.
1823
1824   // Few tests for templated version of As.
1825   TEST_EQ(vec[0].As<int64_t>(), -100);
1826   TEST_EQ_STR(vec[1].As<std::string>().c_str(), "Fred");
1827   TEST_EQ(vec[1].As<int64_t>(), 0);  // Number parsing failed.
1828   TEST_EQ(vec[2].As<double>(), 4.0);
1829
1830   // Test that the blob can be accessed.
1831   TEST_EQ(vec[3].IsBlob(), true);
1832   auto blob = vec[3].AsBlob();
1833   TEST_EQ(blob.size(), 1);
1834   TEST_EQ(blob.data()[0], 77);
1835   TEST_EQ(vec[4].IsBool(), true);   // Check if type is a bool
1836   TEST_EQ(vec[4].AsBool(), false);  // Check if value is false
1837   auto tvec = map["bar"].AsTypedVector();
1838   TEST_EQ(tvec.size(), 3);
1839   TEST_EQ(tvec[2].AsInt8(), 3);
1840   auto tvec3 = map["bar3"].AsFixedTypedVector();
1841   TEST_EQ(tvec3.size(), 3);
1842   TEST_EQ(tvec3[2].AsInt8(), 3);
1843   TEST_EQ(map["bool"].AsBool(), true);
1844   auto tvecb = map["bools"].AsTypedVector();
1845   TEST_EQ(tvecb.ElementType(), flexbuffers::TYPE_BOOL);
1846   TEST_EQ(map["foo"].AsUInt8(), 100);
1847   TEST_EQ(map["unknown"].IsNull(), true);
1848   auto mymap = map["mymap"].AsMap();
1849   // These should be equal by pointer equality, since key and value are shared.
1850   TEST_EQ(mymap.Keys()[0].AsKey(), map.Keys()[4].AsKey());
1851   TEST_EQ(mymap.Values()[0].AsString().c_str(), vec[1].AsString().c_str());
1852   // We can mutate values in the buffer.
1853   TEST_EQ(vec[0].MutateInt(-99), true);
1854   TEST_EQ(vec[0].AsInt64(), -99);
1855   TEST_EQ(vec[1].MutateString("John"), true);  // Size must match.
1856   TEST_EQ_STR(vec[1].AsString().c_str(), "John");
1857   TEST_EQ(vec[1].MutateString("Alfred"), false);  // Too long.
1858   TEST_EQ(vec[2].MutateFloat(2.0f), true);
1859   TEST_EQ(vec[2].AsFloat(), 2.0f);
1860   TEST_EQ(vec[2].MutateFloat(3.14159), false);  // Double does not fit in float.
1861   TEST_EQ(vec[4].AsBool(), false);              // Is false before change
1862   TEST_EQ(vec[4].MutateBool(true), true);       // Can change a bool
1863   TEST_EQ(vec[4].AsBool(), true);               // Changed bool is now true
1864
1865   // Parse from JSON:
1866   flatbuffers::Parser parser;
1867   slb.Clear();
1868   auto jsontest = "{ a: [ 123, 456.0 ], b: \"hello\", c: true, d: false }";
1869   TEST_EQ(parser.ParseFlexBuffer(jsontest, nullptr, &slb), true);
1870   auto jroot = flexbuffers::GetRoot(slb.GetBuffer());
1871   auto jmap = jroot.AsMap();
1872   auto jvec = jmap["a"].AsVector();
1873   TEST_EQ(jvec[0].AsInt64(), 123);
1874   TEST_EQ(jvec[1].AsDouble(), 456.0);
1875   TEST_EQ_STR(jmap["b"].AsString().c_str(), "hello");
1876   TEST_EQ(jmap["c"].IsBool(), true);   // Parsed correctly to a bool
1877   TEST_EQ(jmap["c"].AsBool(), true);   // Parsed correctly to true
1878   TEST_EQ(jmap["d"].IsBool(), true);   // Parsed correctly to a bool
1879   TEST_EQ(jmap["d"].AsBool(), false);  // Parsed correctly to false
1880   // And from FlexBuffer back to JSON:
1881   auto jsonback = jroot.ToString();
1882   TEST_EQ_STR(jsontest, jsonback.c_str());
1883 }
1884
1885 void TypeAliasesTest() {
1886   flatbuffers::FlatBufferBuilder builder;
1887
1888   builder.Finish(CreateTypeAliases(
1889       builder, flatbuffers::numeric_limits<int8_t>::min(),
1890       flatbuffers::numeric_limits<uint8_t>::max(),
1891       flatbuffers::numeric_limits<int16_t>::min(),
1892       flatbuffers::numeric_limits<uint16_t>::max(),
1893       flatbuffers::numeric_limits<int32_t>::min(),
1894       flatbuffers::numeric_limits<uint32_t>::max(),
1895       flatbuffers::numeric_limits<int64_t>::min(),
1896       flatbuffers::numeric_limits<uint64_t>::max(), 2.3f, 2.3));
1897
1898   auto p = builder.GetBufferPointer();
1899   auto ta = flatbuffers::GetRoot<TypeAliases>(p);
1900
1901   TEST_EQ(ta->i8(), flatbuffers::numeric_limits<int8_t>::min());
1902   TEST_EQ(ta->u8(), flatbuffers::numeric_limits<uint8_t>::max());
1903   TEST_EQ(ta->i16(), flatbuffers::numeric_limits<int16_t>::min());
1904   TEST_EQ(ta->u16(), flatbuffers::numeric_limits<uint16_t>::max());
1905   TEST_EQ(ta->i32(), flatbuffers::numeric_limits<int32_t>::min());
1906   TEST_EQ(ta->u32(), flatbuffers::numeric_limits<uint32_t>::max());
1907   TEST_EQ(ta->i64(), flatbuffers::numeric_limits<int64_t>::min());
1908   TEST_EQ(ta->u64(), flatbuffers::numeric_limits<uint64_t>::max());
1909   TEST_EQ(ta->f32(), 2.3f);
1910   TEST_EQ(ta->f64(), 2.3);
1911   TEST_EQ(sizeof(ta->i8()), 1);
1912   TEST_EQ(sizeof(ta->i16()), 2);
1913   TEST_EQ(sizeof(ta->i32()), 4);
1914   TEST_EQ(sizeof(ta->i64()), 8);
1915   TEST_EQ(sizeof(ta->u8()), 1);
1916   TEST_EQ(sizeof(ta->u16()), 2);
1917   TEST_EQ(sizeof(ta->u32()), 4);
1918   TEST_EQ(sizeof(ta->u64()), 8);
1919   TEST_EQ(sizeof(ta->f32()), 4);
1920   TEST_EQ(sizeof(ta->f64()), 8);
1921 }
1922
1923 void EndianSwapTest() {
1924   TEST_EQ(flatbuffers::EndianSwap(static_cast<int16_t>(0x1234)), 0x3412);
1925   TEST_EQ(flatbuffers::EndianSwap(static_cast<int32_t>(0x12345678)),
1926           0x78563412);
1927   TEST_EQ(flatbuffers::EndianSwap(static_cast<int64_t>(0x1234567890ABCDEF)),
1928           0xEFCDAB9078563412);
1929   TEST_EQ(flatbuffers::EndianSwap(flatbuffers::EndianSwap(3.14f)), 3.14f);
1930 }
1931
1932 int main(int /*argc*/, const char * /*argv*/ []) {
1933   // clang-format off
1934   #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
1935       defined(_MSC_VER) && defined(_DEBUG)
1936     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF
1937       // For more thorough checking:
1938       //| _CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_DELAY_FREE_MEM_DF
1939     );
1940   #endif
1941
1942   // Run our various test suites:
1943
1944   std::string rawbuf;
1945   auto flatbuf1 = CreateFlatBufferTest(rawbuf);
1946   #if !defined(FLATBUFFERS_CPP98_STL)
1947     auto flatbuf = std::move(flatbuf1);  // Test move assignment.
1948   #else
1949     auto &flatbuf = flatbuf1;
1950   #endif // !defined(FLATBUFFERS_CPP98_STL)
1951
1952   TriviallyCopyableTest();
1953
1954   AccessFlatBufferTest(reinterpret_cast<const uint8_t *>(rawbuf.c_str()),
1955                        rawbuf.length());
1956   AccessFlatBufferTest(flatbuf.data(), flatbuf.size());
1957
1958   MutateFlatBuffersTest(flatbuf.data(), flatbuf.size());
1959
1960   ObjectFlatBuffersTest(flatbuf.data());
1961
1962   MiniReflectFlatBuffersTest(flatbuf.data());
1963
1964   SizePrefixedTest();
1965
1966   #ifndef FLATBUFFERS_NO_FILE_TESTS
1967     #ifdef FLATBUFFERS_TEST_PATH_PREFIX
1968       test_data_path = FLATBUFFERS_STRING(FLATBUFFERS_TEST_PATH_PREFIX) +
1969                        test_data_path;
1970     #endif
1971     ParseAndGenerateTextTest();
1972     ReflectionTest(flatbuf.data(), flatbuf.size());
1973     ParseProtoTest();
1974     UnionVectorTest();
1975   #endif
1976   // clang-format on
1977
1978   FuzzTest1();
1979   FuzzTest2();
1980
1981   ErrorTest();
1982   ValueTest();
1983   EnumStringsTest();
1984   IntegerOutOfRangeTest();
1985   IntegerBoundaryTest();
1986   UnicodeTest();
1987   UnicodeTestAllowNonUTF8();
1988   UnicodeTestGenerateTextFailsOnNonUTF8();
1989   UnicodeSurrogatesTest();
1990   UnicodeInvalidSurrogatesTest();
1991   InvalidUTF8Test();
1992   UnknownFieldsTest();
1993   ParseUnionTest();
1994   ConformTest();
1995   ParseProtoBufAsciiTest();
1996   TypeAliasesTest();
1997   EndianSwapTest();
1998
1999   JsonDefaultTest();
2000
2001   FlexBuffersTest();
2002
2003   if (!testing_fails) {
2004     TEST_OUTPUT_LINE("ALL TESTS PASSED");
2005     return 0;
2006   } else {
2007     TEST_OUTPUT_LINE("%d FAILED TESTS", testing_fails);
2008     return 1;
2009   }
2010 }