2 * Copyright 2014 Google Inc. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
24 #ifdef FLATBUFFERS_CPP98_STL
25 #include "flatbuffers/stl_emulation.h"
27 using flatbuffers::unique_ptr;
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 #if !defined(_MSC_VER) || _MSC_VER >= 1700
38 #include "arrays_test_generated.h"
41 #include "native_type_test_generated.h"
42 #include "test_assert.h"
44 #include "flatbuffers/flexbuffers.h"
48 // Check that char* and uint8_t* are interoperable types.
49 // The reinterpret_cast<> between the pointers are used to simplify data loading.
50 static_assert(flatbuffers::is_same<uint8_t, char>::value ||
51 flatbuffers::is_same<uint8_t, unsigned char>::value,
52 "unexpected uint8_t type");
54 #if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
55 // Ensure IEEE-754 support if tests of floats with NaN/Inf will run.
56 static_assert(std::numeric_limits<float>::is_iec559 &&
57 std::numeric_limits<double>::is_iec559,
58 "IEC-559 (IEEE-754) standard required");
62 // Shortcuts for the infinity.
63 static const auto infinityf = std::numeric_limits<float>::infinity();
64 static const auto infinityd = std::numeric_limits<double>::infinity();
66 using namespace MyGame::Example;
68 void FlatBufferBuilderTest();
70 // Include simple random number generator to ensure results will be the
71 // same cross platform.
72 // http://en.wikipedia.org/wiki/Park%E2%80%93Miller_random_number_generator
73 uint32_t lcg_seed = 48271;
75 return lcg_seed = (static_cast<uint64_t>(lcg_seed) * 279470273UL) % 4294967291UL;
77 void lcg_reset() { lcg_seed = 48271; }
79 std::string test_data_path =
80 #ifdef BAZEL_TEST_DATA_PATH
81 "../com_github_google_flatbuffers/tests/";
86 // example of how to build up a serialized buffer algorithmically:
87 flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
88 flatbuffers::FlatBufferBuilder builder;
90 auto vec = Vec3(1, 2, 3, 0, Color_Red, Test(10, 20));
92 auto name = builder.CreateString("MyMonster");
94 unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
95 auto inventory = builder.CreateVector(inv_data, 10);
97 // Alternatively, create the vector first, and fill in data later:
98 // unsigned char *inv_buf = nullptr;
99 // auto inventory = builder.CreateUninitializedVector<unsigned char>(
101 // memcpy(inv_buf, inv_data, 10);
103 Test tests[] = { Test(10, 20), Test(30, 40) };
104 auto testv = builder.CreateVectorOfStructs(tests, 2);
107 #ifndef FLATBUFFERS_CPP98_STL
108 // Create a vector of structures from a lambda.
109 auto testv2 = builder.CreateVectorOfStructs<Test>(
110 2, [&](size_t i, Test* s) -> void {
114 // Create a vector of structures using a plain old C++ function.
115 auto testv2 = builder.CreateVectorOfStructs<Test>(
116 2, [](size_t i, Test* s, void *state) -> void {
117 *s = (reinterpret_cast<Test*>(state))[i];
119 #endif // FLATBUFFERS_CPP98_STL
122 // create monster with very few fields set:
123 // (same functionality as CreateMonster below, but sets fields manually)
124 flatbuffers::Offset<Monster> mlocs[3];
125 auto fred = builder.CreateString("Fred");
126 auto barney = builder.CreateString("Barney");
127 auto wilma = builder.CreateString("Wilma");
128 MonsterBuilder mb1(builder);
130 mlocs[0] = mb1.Finish();
131 MonsterBuilder mb2(builder);
132 mb2.add_name(barney);
134 mlocs[1] = mb2.Finish();
135 MonsterBuilder mb3(builder);
137 mlocs[2] = mb3.Finish();
139 // Create an array of strings. Also test string pooling, and lambdas.
141 builder.CreateVector<flatbuffers::Offset<flatbuffers::String>>(
143 [](size_t i, flatbuffers::FlatBufferBuilder *b)
144 -> flatbuffers::Offset<flatbuffers::String> {
145 static const char *names[] = { "bob", "fred", "bob", "fred" };
146 return b->CreateSharedString(names[i]);
150 // Creating vectors of strings in one convenient call.
151 std::vector<std::string> names2;
152 names2.push_back("jane");
153 names2.push_back("mary");
154 auto vecofstrings2 = builder.CreateVectorOfStrings(names2);
156 // Create an array of sorted tables, can be used with binary search when read:
157 auto vecoftables = builder.CreateVectorOfSortedTables(mlocs, 3);
159 // Create an array of sorted structs,
160 // can be used with binary search when read:
161 std::vector<Ability> abilities;
162 abilities.push_back(Ability(4, 40));
163 abilities.push_back(Ability(3, 30));
164 abilities.push_back(Ability(2, 20));
165 abilities.push_back(Ability(1, 10));
166 auto vecofstructs = builder.CreateVectorOfSortedStructs(&abilities);
168 // Create a nested FlatBuffer.
169 // Nested FlatBuffers are stored in a ubyte vector, which can be convenient
170 // since they can be memcpy'd around much easier than other FlatBuffer
171 // values. They have little overhead compared to storing the table directly.
172 // As a test, create a mostly empty Monster buffer:
173 flatbuffers::FlatBufferBuilder nested_builder;
174 auto nmloc = CreateMonster(nested_builder, nullptr, 0, 0,
175 nested_builder.CreateString("NestedMonster"));
176 FinishMonsterBuffer(nested_builder, nmloc);
177 // Now we can store the buffer in the parent. Note that by default, vectors
178 // are only aligned to their elements or size field, so in this case if the
179 // buffer contains 64-bit elements, they may not be correctly aligned. We fix
181 builder.ForceVectorAlignment(nested_builder.GetSize(), sizeof(uint8_t),
182 nested_builder.GetBufferMinAlignment());
183 // If for whatever reason you don't have the nested_builder available, you
184 // can substitute flatbuffers::largest_scalar_t (64-bit) for the alignment, or
185 // the largest force_align value in your schema if you're using it.
186 auto nested_flatbuffer_vector = builder.CreateVector(
187 nested_builder.GetBufferPointer(), nested_builder.GetSize());
189 // Test a nested FlexBuffer:
190 flexbuffers::Builder flexbuild;
193 auto flex = builder.CreateVector(flexbuild.GetBuffer());
195 // Test vector of enums.
196 Color colors[] = { Color_Blue, Color_Green };
197 // We use this special creation function because we have an array of
198 // pre-C++11 (enum class) enums whose size likely is int, yet its declared
199 // type in the schema is byte.
200 auto vecofcolors = builder.CreateVectorScalarCast<uint8_t, Color>(colors, 2);
202 // shortcut for creating monster with all fields set:
203 auto mloc = CreateMonster(builder, &vec, 150, 80, name, inventory, Color_Blue,
204 Any_Monster, mlocs[1].Union(), // Store a union.
205 testv, vecofstrings, vecoftables, 0,
206 nested_flatbuffer_vector, 0, false, 0, 0, 0, 0, 0,
207 0, 0, 0, 0, 3.14159f, 3.0f, 0.0f, vecofstrings2,
208 vecofstructs, flex, testv2, 0, 0, 0, 0, 0, 0, 0, 0,
209 0, 0, 0, AnyUniqueAliases_NONE, 0,
210 AnyAmbiguousAliases_NONE, 0, vecofcolors);
212 FinishMonsterBuffer(builder, mloc);
215 #ifdef FLATBUFFERS_TEST_VERBOSE
216 // print byte data for debugging:
217 auto p = builder.GetBufferPointer();
218 for (flatbuffers::uoffset_t i = 0; i < builder.GetSize(); i++)
223 // return the buffer for the caller to use.
225 reinterpret_cast<const char *>(builder.GetBufferPointer());
226 buffer.assign(bufferpointer, bufferpointer + builder.GetSize());
228 return builder.Release();
231 // example of accessing a buffer loaded in memory:
232 void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length,
233 bool pooled = true) {
234 // First, verify the buffers integrity (optional)
235 flatbuffers::Verifier verifier(flatbuf, length);
236 TEST_EQ(VerifyMonsterBuffer(verifier), true);
239 #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
240 std::vector<uint8_t> test_buff;
241 test_buff.resize(length * 2);
242 std::memcpy(&test_buff[0], flatbuf, length);
243 std::memcpy(&test_buff[length], flatbuf, length);
245 flatbuffers::Verifier verifier1(&test_buff[0], length);
246 TEST_EQ(VerifyMonsterBuffer(verifier1), true);
247 TEST_EQ(verifier1.GetComputedSize(), length);
249 flatbuffers::Verifier verifier2(&test_buff[length], length);
250 TEST_EQ(VerifyMonsterBuffer(verifier2), true);
251 TEST_EQ(verifier2.GetComputedSize(), length);
255 TEST_EQ(strcmp(MonsterIdentifier(), "MONS"), 0);
256 TEST_EQ(MonsterBufferHasIdentifier(flatbuf), true);
257 TEST_EQ(strcmp(MonsterExtension(), "mon"), 0);
259 // Access the buffer from the root.
260 auto monster = GetMonster(flatbuf);
262 TEST_EQ(monster->hp(), 80);
263 TEST_EQ(monster->mana(), 150); // default
264 TEST_EQ_STR(monster->name()->c_str(), "MyMonster");
265 // Can't access the following field, it is deprecated in the schema,
266 // which means accessors are not generated:
267 // monster.friendly()
269 auto pos = monster->pos();
271 TEST_EQ(pos->z(), 3);
272 TEST_EQ(pos->test3().a(), 10);
273 TEST_EQ(pos->test3().b(), 20);
275 auto inventory = monster->inventory();
276 TEST_EQ(VectorLength(inventory), 10UL); // Works even if inventory is null.
277 TEST_NOTNULL(inventory);
278 unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
279 // Check compatibilty of iterators with STL.
280 std::vector<unsigned char> inv_vec(inventory->begin(), inventory->end());
282 for (auto it = inventory->begin(); it != inventory->end(); ++it, ++n) {
283 auto indx = it - inventory->begin();
284 TEST_EQ(*it, inv_vec.at(indx)); // Use bounds-check.
285 TEST_EQ(*it, inv_data[indx]);
287 TEST_EQ(n, inv_vec.size());
290 for (auto it = inventory->cbegin(); it != inventory->cend(); ++it, ++n) {
291 auto indx = it - inventory->cbegin();
292 TEST_EQ(*it, inv_vec.at(indx)); // Use bounds-check.
293 TEST_EQ(*it, inv_data[indx]);
295 TEST_EQ(n, inv_vec.size());
298 for (auto it = inventory->rbegin(); it != inventory->rend(); ++it, ++n) {
299 auto indx = inventory->rend() - it - 1;
300 TEST_EQ(*it, inv_vec.at(indx)); // Use bounds-check.
301 TEST_EQ(*it, inv_data[indx]);
303 TEST_EQ(n, inv_vec.size());
306 for (auto it = inventory->crbegin(); it != inventory->crend(); ++it, ++n) {
307 auto indx = inventory->crend() - it - 1;
308 TEST_EQ(*it, inv_vec.at(indx)); // Use bounds-check.
309 TEST_EQ(*it, inv_data[indx]);
311 TEST_EQ(n, inv_vec.size());
313 TEST_EQ(monster->color(), Color_Blue);
315 // Example of accessing a union:
316 TEST_EQ(monster->test_type(), Any_Monster); // First make sure which it is.
317 auto monster2 = reinterpret_cast<const Monster *>(monster->test());
318 TEST_NOTNULL(monster2);
319 TEST_EQ_STR(monster2->name()->c_str(), "Fred");
321 // Example of accessing a vector of strings:
322 auto vecofstrings = monster->testarrayofstring();
323 TEST_EQ(vecofstrings->size(), 4U);
324 TEST_EQ_STR(vecofstrings->Get(0)->c_str(), "bob");
325 TEST_EQ_STR(vecofstrings->Get(1)->c_str(), "fred");
327 // These should have pointer equality because of string pooling.
328 TEST_EQ(vecofstrings->Get(0)->c_str(), vecofstrings->Get(2)->c_str());
329 TEST_EQ(vecofstrings->Get(1)->c_str(), vecofstrings->Get(3)->c_str());
332 auto vecofstrings2 = monster->testarrayofstring2();
334 TEST_EQ(vecofstrings2->size(), 2U);
335 TEST_EQ_STR(vecofstrings2->Get(0)->c_str(), "jane");
336 TEST_EQ_STR(vecofstrings2->Get(1)->c_str(), "mary");
339 // Example of accessing a vector of tables:
340 auto vecoftables = monster->testarrayoftables();
341 TEST_EQ(vecoftables->size(), 3U);
342 for (auto it = vecoftables->begin(); it != vecoftables->end(); ++it) {
343 TEST_EQ(strlen(it->name()->c_str()) >= 4, true);
345 TEST_EQ_STR(vecoftables->Get(0)->name()->c_str(), "Barney");
346 TEST_EQ(vecoftables->Get(0)->hp(), 1000);
347 TEST_EQ_STR(vecoftables->Get(1)->name()->c_str(), "Fred");
348 TEST_EQ_STR(vecoftables->Get(2)->name()->c_str(), "Wilma");
349 TEST_NOTNULL(vecoftables->LookupByKey("Barney"));
350 TEST_NOTNULL(vecoftables->LookupByKey("Fred"));
351 TEST_NOTNULL(vecoftables->LookupByKey("Wilma"));
353 // Test accessing a vector of sorted structs
354 auto vecofstructs = monster->testarrayofsortedstruct();
355 if (vecofstructs) { // not filled in monster_test.bfbs
356 for (flatbuffers::uoffset_t i = 0; i < vecofstructs->size() - 1; i++) {
357 auto left = vecofstructs->Get(i);
358 auto right = vecofstructs->Get(i + 1);
359 TEST_EQ(true, (left->KeyCompareLessThan(right)));
361 TEST_NOTNULL(vecofstructs->LookupByKey(3));
362 TEST_EQ(static_cast<const Ability *>(nullptr),
363 vecofstructs->LookupByKey(5));
366 // Test nested FlatBuffers if available:
367 auto nested_buffer = monster->testnestedflatbuffer();
369 // nested_buffer is a vector of bytes you can memcpy. However, if you
370 // actually want to access the nested data, this is a convenient
371 // accessor that directly gives you the root table:
372 auto nested_monster = monster->testnestedflatbuffer_nested_root();
373 TEST_EQ_STR(nested_monster->name()->c_str(), "NestedMonster");
376 // Test flexbuffer if available:
377 auto flex = monster->flex();
378 // flex is a vector of bytes you can memcpy etc.
379 TEST_EQ(flex->size(), 4); // Encoded FlexBuffer bytes.
380 // However, if you actually want to access the nested data, this is a
381 // convenient accessor that directly gives you the root value:
382 TEST_EQ(monster->flex_flexbuffer_root().AsInt16(), 1234);
384 // Test vector of enums:
385 auto colors = monster->vector_of_enums();
387 TEST_EQ(colors->size(), 2);
388 TEST_EQ(colors->Get(0), Color_Blue);
389 TEST_EQ(colors->Get(1), Color_Green);
392 // Since Flatbuffers uses explicit mechanisms to override the default
393 // compiler alignment, double check that the compiler indeed obeys them:
394 // (Test consists of a short and byte):
395 TEST_EQ(flatbuffers::AlignOf<Test>(), 2UL);
396 TEST_EQ(sizeof(Test), 4UL);
398 const flatbuffers::Vector<const Test *> *tests_array[] = {
402 for (size_t i = 0; i < sizeof(tests_array) / sizeof(tests_array[0]); ++i) {
403 auto tests = tests_array[i];
405 auto test_0 = tests->Get(0);
406 auto test_1 = tests->Get(1);
407 TEST_EQ(test_0->a(), 10);
408 TEST_EQ(test_0->b(), 20);
409 TEST_EQ(test_1->a(), 30);
410 TEST_EQ(test_1->b(), 40);
411 for (auto it = tests->begin(); it != tests->end(); ++it) {
412 TEST_EQ(it->a() == 10 || it->a() == 30, true); // Just testing iterators.
416 // Checking for presence of fields:
417 TEST_EQ(flatbuffers::IsFieldPresent(monster, Monster::VT_HP), true);
418 TEST_EQ(flatbuffers::IsFieldPresent(monster, Monster::VT_MANA), false);
420 // Obtaining a buffer from a root:
421 TEST_EQ(GetBufferStartFromRootPointer(monster), flatbuf);
424 // Change a FlatBuffer in-place, after it has been constructed.
425 void MutateFlatBuffersTest(uint8_t *flatbuf, std::size_t length) {
426 // Get non-const pointer to root.
427 auto monster = GetMutableMonster(flatbuf);
429 // Each of these tests mutates, then tests, then set back to the original,
430 // so we can test that the buffer in the end still passes our original test.
431 auto hp_ok = monster->mutate_hp(10);
432 TEST_EQ(hp_ok, true); // Field was present.
433 TEST_EQ(monster->hp(), 10);
434 // Mutate to default value
435 auto hp_ok_default = monster->mutate_hp(100);
436 TEST_EQ(hp_ok_default, true); // Field was present.
437 TEST_EQ(monster->hp(), 100);
438 // Test that mutate to default above keeps field valid for further mutations
439 auto hp_ok_2 = monster->mutate_hp(20);
440 TEST_EQ(hp_ok_2, true);
441 TEST_EQ(monster->hp(), 20);
442 monster->mutate_hp(80);
444 // Monster originally at 150 mana (default value)
445 auto mana_default_ok = monster->mutate_mana(150); // Mutate to default value.
446 TEST_EQ(mana_default_ok,
447 true); // Mutation should succeed, because default value.
448 TEST_EQ(monster->mana(), 150);
449 auto mana_ok = monster->mutate_mana(10);
450 TEST_EQ(mana_ok, false); // Field was NOT present, because default value.
451 TEST_EQ(monster->mana(), 150);
454 auto pos = monster->mutable_pos();
455 auto test3 = pos->mutable_test3(); // Struct inside a struct.
456 test3.mutate_a(50); // Struct fields never fail.
457 TEST_EQ(test3.a(), 50);
461 auto inventory = monster->mutable_inventory();
462 inventory->Mutate(9, 100);
463 TEST_EQ(inventory->Get(9), 100);
464 inventory->Mutate(9, 9);
466 auto tables = monster->mutable_testarrayoftables();
467 auto first = tables->GetMutableObject(0);
468 TEST_EQ(first->hp(), 1000);
470 TEST_EQ(first->hp(), 0);
471 first->mutate_hp(1000);
473 // Run the verifier and the regular test to make sure we didn't trample on
475 AccessFlatBufferTest(flatbuf, length);
478 // Unpack a FlatBuffer into objects.
479 void ObjectFlatBuffersTest(uint8_t *flatbuf) {
480 // Optional: we can specify resolver and rehasher functions to turn hashed
481 // strings into object pointers and back, to implement remote references
483 auto resolver = flatbuffers::resolver_function_t(
484 [](void **pointer_adr, flatbuffers::hash_value_t hash) {
487 // Don't actually do anything, leave variable null.
489 auto rehasher = flatbuffers::rehasher_function_t(
490 [](void *pointer) -> flatbuffers::hash_value_t {
495 // Turn a buffer into C++ objects.
496 auto monster1 = UnPackMonster(flatbuf, &resolver);
498 // Re-serialize the data.
499 flatbuffers::FlatBufferBuilder fbb1;
500 fbb1.Finish(CreateMonster(fbb1, monster1.get(), &rehasher),
501 MonsterIdentifier());
503 // Unpack again, and re-serialize again.
504 auto monster2 = UnPackMonster(fbb1.GetBufferPointer(), &resolver);
505 flatbuffers::FlatBufferBuilder fbb2;
506 fbb2.Finish(CreateMonster(fbb2, monster2.get(), &rehasher),
507 MonsterIdentifier());
509 // Now we've gone full round-trip, the two buffers should match.
510 auto len1 = fbb1.GetSize();
511 auto len2 = fbb2.GetSize();
513 TEST_EQ(memcmp(fbb1.GetBufferPointer(), fbb2.GetBufferPointer(), len1), 0);
515 // Test it with the original buffer test to make sure all data survived.
516 AccessFlatBufferTest(fbb2.GetBufferPointer(), len2, false);
518 // Test accessing fields, similar to AccessFlatBufferTest above.
519 TEST_EQ(monster2->hp, 80);
520 TEST_EQ(monster2->mana, 150); // default
521 TEST_EQ_STR(monster2->name.c_str(), "MyMonster");
523 auto &pos = monster2->pos;
525 TEST_EQ(pos->z(), 3);
526 TEST_EQ(pos->test3().a(), 10);
527 TEST_EQ(pos->test3().b(), 20);
529 auto &inventory = monster2->inventory;
530 TEST_EQ(inventory.size(), 10UL);
531 unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
532 for (auto it = inventory.begin(); it != inventory.end(); ++it)
533 TEST_EQ(*it, inv_data[it - inventory.begin()]);
535 TEST_EQ(monster2->color, Color_Blue);
537 auto monster3 = monster2->test.AsMonster();
538 TEST_NOTNULL(monster3);
539 TEST_EQ_STR(monster3->name.c_str(), "Fred");
541 auto &vecofstrings = monster2->testarrayofstring;
542 TEST_EQ(vecofstrings.size(), 4U);
543 TEST_EQ_STR(vecofstrings[0].c_str(), "bob");
544 TEST_EQ_STR(vecofstrings[1].c_str(), "fred");
546 auto &vecofstrings2 = monster2->testarrayofstring2;
547 TEST_EQ(vecofstrings2.size(), 2U);
548 TEST_EQ_STR(vecofstrings2[0].c_str(), "jane");
549 TEST_EQ_STR(vecofstrings2[1].c_str(), "mary");
551 auto &vecoftables = monster2->testarrayoftables;
552 TEST_EQ(vecoftables.size(), 3U);
553 TEST_EQ_STR(vecoftables[0]->name.c_str(), "Barney");
554 TEST_EQ(vecoftables[0]->hp, 1000);
555 TEST_EQ_STR(vecoftables[1]->name.c_str(), "Fred");
556 TEST_EQ_STR(vecoftables[2]->name.c_str(), "Wilma");
558 auto &tests = monster2->test4;
559 TEST_EQ(tests[0].a(), 10);
560 TEST_EQ(tests[0].b(), 20);
561 TEST_EQ(tests[1].a(), 30);
562 TEST_EQ(tests[1].b(), 40);
565 // Prefix a FlatBuffer with a size field.
566 void SizePrefixedTest() {
567 // Create size prefixed buffer.
568 flatbuffers::FlatBufferBuilder fbb;
569 FinishSizePrefixedMonsterBuffer(
571 CreateMonster(fbb, 0, 200, 300, fbb.CreateString("bob")));
574 flatbuffers::Verifier verifier(fbb.GetBufferPointer(), fbb.GetSize());
575 TEST_EQ(VerifySizePrefixedMonsterBuffer(verifier), true);
578 auto m = GetSizePrefixedMonster(fbb.GetBufferPointer());
579 TEST_EQ(m->mana(), 200);
580 TEST_EQ(m->hp(), 300);
581 TEST_EQ_STR(m->name()->c_str(), "bob");
584 void TriviallyCopyableTest() {
586 #if __GNUG__ && __GNUC__ < 5
587 TEST_EQ(__has_trivial_copy(Vec3), true);
589 #if __cplusplus >= 201103L
590 TEST_EQ(std::is_trivially_copyable<Vec3>::value, true);
596 // Check stringify of an default enum value to json
597 void JsonDefaultTest() {
598 // load FlatBuffer schema (.fbs) from disk
599 std::string schemafile;
600 TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.fbs").c_str(),
601 false, &schemafile), true);
602 // parse schema first, so we can use it to parse the data after
603 flatbuffers::Parser parser;
604 auto include_test_path =
605 flatbuffers::ConCatPathFileName(test_data_path, "include_test");
606 const char *include_directories[] = { test_data_path.c_str(),
607 include_test_path.c_str(), nullptr };
609 TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
610 // create incomplete monster and store to json
611 parser.opts.output_default_scalars_in_json = true;
612 parser.opts.output_enum_identifiers = true;
613 flatbuffers::FlatBufferBuilder builder;
614 auto name = builder.CreateString("default_enum");
615 MonsterBuilder color_monster(builder);
616 color_monster.add_name(name);
617 FinishMonsterBuffer(builder, color_monster.Finish());
619 auto result = GenerateText(parser, builder.GetBufferPointer(), &jsongen);
620 TEST_EQ(result, true);
621 // default value of the "color" field is Blue
622 TEST_EQ(std::string::npos != jsongen.find("color: \"Blue\""), true);
623 // default value of the "testf" field is 3.14159
624 TEST_EQ(std::string::npos != jsongen.find("testf: 3.14159"), true);
627 void JsonEnumsTest() {
628 // load FlatBuffer schema (.fbs) from disk
629 std::string schemafile;
630 TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.fbs").c_str(),
633 // parse schema first, so we can use it to parse the data after
634 flatbuffers::Parser parser;
635 auto include_test_path =
636 flatbuffers::ConCatPathFileName(test_data_path, "include_test");
637 const char *include_directories[] = { test_data_path.c_str(),
638 include_test_path.c_str(), nullptr };
639 parser.opts.output_enum_identifiers = true;
640 TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
641 flatbuffers::FlatBufferBuilder builder;
642 auto name = builder.CreateString("bitflag_enum");
643 MonsterBuilder color_monster(builder);
644 color_monster.add_name(name);
645 color_monster.add_color(Color(Color_Blue | Color_Red));
646 FinishMonsterBuffer(builder, color_monster.Finish());
648 auto result = GenerateText(parser, builder.GetBufferPointer(), &jsongen);
649 TEST_EQ(result, true);
650 TEST_EQ(std::string::npos != jsongen.find("color: \"Red Blue\""), true);
653 #if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
654 // The IEEE-754 quiet_NaN is not simple binary constant.
655 // All binary NaN bit strings have all the bits of the biased exponent field E
656 // set to 1. A quiet NaN bit string should be encoded with the first bit d[1]
657 // of the trailing significand field T being 1 (d[0] is implicit bit).
658 // It is assumed that endianness of floating-point is same as integer.
659 template<typename T, typename U, U qnan_base> bool is_quiet_nan_impl(T v) {
660 static_assert(sizeof(T) == sizeof(U), "unexpected");
662 std::memcpy(&b, &v, sizeof(T));
663 return ((b & qnan_base) == qnan_base);
665 static bool is_quiet_nan(float v) {
666 return is_quiet_nan_impl<float, uint32_t, 0x7FC00000u>(v);
668 static bool is_quiet_nan(double v) {
669 return is_quiet_nan_impl<double, uint64_t, 0x7FF8000000000000ul>(v);
672 void TestMonsterExtraFloats() {
673 TEST_EQ(is_quiet_nan(1.0), false);
674 TEST_EQ(is_quiet_nan(infinityd), false);
675 TEST_EQ(is_quiet_nan(-infinityf), false);
676 TEST_EQ(is_quiet_nan(std::numeric_limits<float>::quiet_NaN()), true);
677 TEST_EQ(is_quiet_nan(std::numeric_limits<double>::quiet_NaN()), true);
679 using namespace flatbuffers;
680 using namespace MyGame;
681 // Load FlatBuffer schema (.fbs) from disk.
682 std::string schemafile;
683 TEST_EQ(LoadFile((test_data_path + "monster_extra.fbs").c_str(), false,
686 // Parse schema first, so we can use it to parse the data after.
688 auto include_test_path = ConCatPathFileName(test_data_path, "include_test");
689 const char *include_directories[] = { test_data_path.c_str(),
690 include_test_path.c_str(), nullptr };
691 TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
692 // Create empty extra and store to json.
693 parser.opts.output_default_scalars_in_json = true;
694 parser.opts.output_enum_identifiers = true;
695 FlatBufferBuilder builder;
696 const auto def_root = MonsterExtraBuilder(builder).Finish();
697 FinishMonsterExtraBuffer(builder, def_root);
698 const auto def_obj = builder.GetBufferPointer();
699 const auto def_extra = GetMonsterExtra(def_obj);
700 TEST_NOTNULL(def_extra);
701 TEST_EQ(is_quiet_nan(def_extra->f0()), true);
702 TEST_EQ(is_quiet_nan(def_extra->f1()), true);
703 TEST_EQ(def_extra->f2(), +infinityf);
704 TEST_EQ(def_extra->f3(), -infinityf);
705 TEST_EQ(is_quiet_nan(def_extra->d0()), true);
706 TEST_EQ(is_quiet_nan(def_extra->d1()), true);
707 TEST_EQ(def_extra->d2(), +infinityd);
708 TEST_EQ(def_extra->d3(), -infinityd);
710 auto result = GenerateText(parser, def_obj, &jsongen);
711 TEST_EQ(result, true);
712 // Check expected default values.
713 TEST_EQ(std::string::npos != jsongen.find("f0: nan"), true);
714 TEST_EQ(std::string::npos != jsongen.find("f1: nan"), true);
715 TEST_EQ(std::string::npos != jsongen.find("f2: inf"), true);
716 TEST_EQ(std::string::npos != jsongen.find("f3: -inf"), true);
717 TEST_EQ(std::string::npos != jsongen.find("d0: nan"), true);
718 TEST_EQ(std::string::npos != jsongen.find("d1: nan"), true);
719 TEST_EQ(std::string::npos != jsongen.find("d2: inf"), true);
720 TEST_EQ(std::string::npos != jsongen.find("d3: -inf"), true);
721 // Parse 'mosterdata_extra.json'.
722 const auto extra_base = test_data_path + "monsterdata_extra";
724 TEST_EQ(LoadFile((extra_base + ".json").c_str(), false, &jsongen), true);
725 TEST_EQ(parser.Parse(jsongen.c_str()), true);
726 const auto test_file = parser.builder_.GetBufferPointer();
727 const auto test_size = parser.builder_.GetSize();
728 Verifier verifier(test_file, test_size);
729 TEST_ASSERT(VerifyMonsterExtraBuffer(verifier));
730 const auto extra = GetMonsterExtra(test_file);
732 TEST_EQ(is_quiet_nan(extra->f0()), true);
733 TEST_EQ(is_quiet_nan(extra->f1()), true);
734 TEST_EQ(extra->f2(), +infinityf);
735 TEST_EQ(extra->f3(), -infinityf);
736 TEST_EQ(is_quiet_nan(extra->d0()), true);
737 TEST_EQ(extra->d1(), +infinityd);
738 TEST_EQ(extra->d2(), -infinityd);
739 TEST_EQ(is_quiet_nan(extra->d3()), true);
740 TEST_NOTNULL(extra->fvec());
741 TEST_EQ(extra->fvec()->size(), 4);
742 TEST_EQ(extra->fvec()->Get(0), 1.0f);
743 TEST_EQ(extra->fvec()->Get(1), -infinityf);
744 TEST_EQ(extra->fvec()->Get(2), +infinityf);
745 TEST_EQ(is_quiet_nan(extra->fvec()->Get(3)), true);
746 TEST_NOTNULL(extra->dvec());
747 TEST_EQ(extra->dvec()->size(), 4);
748 TEST_EQ(extra->dvec()->Get(0), 2.0);
749 TEST_EQ(extra->dvec()->Get(1), +infinityd);
750 TEST_EQ(extra->dvec()->Get(2), -infinityd);
751 TEST_EQ(is_quiet_nan(extra->dvec()->Get(3)), true);
754 void TestMonsterExtraFloats() {}
757 // example of parsing text straight into a buffer, and generating
758 // text back from it:
759 void ParseAndGenerateTextTest(bool binary) {
760 // load FlatBuffer schema (.fbs) and JSON from disk
761 std::string schemafile;
762 std::string jsonfile;
763 TEST_EQ(flatbuffers::LoadFile(
764 (test_data_path + "monster_test." + (binary ? "bfbs" : "fbs"))
766 binary, &schemafile),
768 TEST_EQ(flatbuffers::LoadFile(
769 (test_data_path + "monsterdata_test.golden").c_str(), false,
773 auto include_test_path =
774 flatbuffers::ConCatPathFileName(test_data_path, "include_test");
775 const char *include_directories[] = { test_data_path.c_str(),
776 include_test_path.c_str(), nullptr };
778 // parse schema first, so we can use it to parse the data after
779 flatbuffers::Parser parser;
781 flatbuffers::Verifier verifier(
782 reinterpret_cast<const uint8_t *>(schemafile.c_str()),
784 TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
785 //auto schema = reflection::GetSchema(schemafile.c_str());
786 TEST_EQ(parser.Deserialize((const uint8_t *)schemafile.c_str(), schemafile.size()), true);
788 TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
790 TEST_EQ(parser.Parse(jsonfile.c_str(), include_directories), true);
792 // here, parser.builder_ contains a binary buffer that is the parsed data.
794 // First, verify it, just in case:
795 flatbuffers::Verifier verifier(parser.builder_.GetBufferPointer(),
796 parser.builder_.GetSize());
797 TEST_EQ(VerifyMonsterBuffer(verifier), true);
799 AccessFlatBufferTest(parser.builder_.GetBufferPointer(),
800 parser.builder_.GetSize(), false);
802 // to ensure it is correct, we now generate text back from the binary,
803 // and compare the two:
806 GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
807 TEST_EQ(result, true);
808 TEST_EQ_STR(jsongen.c_str(), jsonfile.c_str());
810 // We can also do the above using the convenient Registry that knows about
811 // a set of file_identifiers mapped to schemas.
812 flatbuffers::Registry registry;
813 // Make sure schemas can find their includes.
814 registry.AddIncludeDirectory(test_data_path.c_str());
815 registry.AddIncludeDirectory(include_test_path.c_str());
816 // Call this with many schemas if possible.
817 registry.Register(MonsterIdentifier(),
818 (test_data_path + "monster_test.fbs").c_str());
819 // Now we got this set up, we can parse by just specifying the identifier,
820 // the correct schema will be loaded on the fly:
821 auto buf = registry.TextToFlatBuffer(jsonfile.c_str(), MonsterIdentifier());
822 // If this fails, check registry.lasterror_.
823 TEST_NOTNULL(buf.data());
824 // Test the buffer, to be sure:
825 AccessFlatBufferTest(buf.data(), buf.size(), false);
826 // We can use the registry to turn this back into text, in this case it
827 // will get the file_identifier from the binary:
829 auto ok = registry.FlatBufferToText(buf.data(), buf.size(), &text);
830 // If this fails, check registry.lasterror_.
832 TEST_EQ_STR(text.c_str(), jsonfile.c_str());
834 // Generate text for UTF-8 strings without escapes.
835 std::string jsonfile_utf8;
836 TEST_EQ(flatbuffers::LoadFile((test_data_path + "unicode_test.json").c_str(),
837 false, &jsonfile_utf8),
839 TEST_EQ(parser.Parse(jsonfile_utf8.c_str(), include_directories), true);
840 // To ensure it is correct, generate utf-8 text back from the binary.
841 std::string jsongen_utf8;
842 // request natural printing for utf-8 strings
843 parser.opts.natural_utf8 = true;
844 parser.opts.strict_json = true;
846 GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen_utf8),
848 TEST_EQ_STR(jsongen_utf8.c_str(), jsonfile_utf8.c_str());
851 void ReflectionTest(uint8_t *flatbuf, size_t length) {
852 // Load a binary schema.
853 std::string bfbsfile;
854 TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.bfbs").c_str(),
858 // Verify it, just in case:
859 flatbuffers::Verifier verifier(
860 reinterpret_cast<const uint8_t *>(bfbsfile.c_str()), bfbsfile.length());
861 TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
863 // Make sure the schema is what we expect it to be.
864 auto &schema = *reflection::GetSchema(bfbsfile.c_str());
865 auto root_table = schema.root_table();
866 TEST_EQ_STR(root_table->name()->c_str(), "MyGame.Example.Monster");
867 auto fields = root_table->fields();
868 auto hp_field_ptr = fields->LookupByKey("hp");
869 TEST_NOTNULL(hp_field_ptr);
870 auto &hp_field = *hp_field_ptr;
871 TEST_EQ_STR(hp_field.name()->c_str(), "hp");
872 TEST_EQ(hp_field.id(), 2);
873 TEST_EQ(hp_field.type()->base_type(), reflection::Short);
874 auto friendly_field_ptr = fields->LookupByKey("friendly");
875 TEST_NOTNULL(friendly_field_ptr);
876 TEST_NOTNULL(friendly_field_ptr->attributes());
877 TEST_NOTNULL(friendly_field_ptr->attributes()->LookupByKey("priority"));
879 // Make sure the table index is what we expect it to be.
880 auto pos_field_ptr = fields->LookupByKey("pos");
881 TEST_NOTNULL(pos_field_ptr);
882 TEST_EQ(pos_field_ptr->type()->base_type(), reflection::Obj);
883 auto pos_table_ptr = schema.objects()->Get(pos_field_ptr->type()->index());
884 TEST_NOTNULL(pos_table_ptr);
885 TEST_EQ_STR(pos_table_ptr->name()->c_str(), "MyGame.Example.Vec3");
887 // Now use it to dynamically access a buffer.
888 auto &root = *flatbuffers::GetAnyRoot(flatbuf);
890 // Verify the buffer first using reflection based verification
891 TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(), flatbuf, length),
894 auto hp = flatbuffers::GetFieldI<uint16_t>(root, hp_field);
897 // Rather than needing to know the type, we can also get the value of
898 // any field as an int64_t/double/string, regardless of what it actually is.
899 auto hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
900 TEST_EQ(hp_int64, 80);
901 auto hp_double = flatbuffers::GetAnyFieldF(root, hp_field);
902 TEST_EQ(hp_double, 80.0);
903 auto hp_string = flatbuffers::GetAnyFieldS(root, hp_field, &schema);
904 TEST_EQ_STR(hp_string.c_str(), "80");
906 // Get struct field through reflection
907 auto pos_struct = flatbuffers::GetFieldStruct(root, *pos_field_ptr);
908 TEST_NOTNULL(pos_struct);
909 TEST_EQ(flatbuffers::GetAnyFieldF(*pos_struct,
910 *pos_table_ptr->fields()->LookupByKey("z")),
913 auto test3_field = pos_table_ptr->fields()->LookupByKey("test3");
914 auto test3_struct = flatbuffers::GetFieldStruct(*pos_struct, *test3_field);
915 TEST_NOTNULL(test3_struct);
916 auto test3_object = schema.objects()->Get(test3_field->type()->index());
918 TEST_EQ(flatbuffers::GetAnyFieldF(*test3_struct,
919 *test3_object->fields()->LookupByKey("a")),
922 // We can also modify it.
923 flatbuffers::SetField<uint16_t>(&root, hp_field, 200);
924 hp = flatbuffers::GetFieldI<uint16_t>(root, hp_field);
927 // We can also set fields generically:
928 flatbuffers::SetAnyFieldI(&root, hp_field, 300);
929 hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
930 TEST_EQ(hp_int64, 300);
931 flatbuffers::SetAnyFieldF(&root, hp_field, 300.5);
932 hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
933 TEST_EQ(hp_int64, 300);
934 flatbuffers::SetAnyFieldS(&root, hp_field, "300");
935 hp_int64 = flatbuffers::GetAnyFieldI(root, hp_field);
936 TEST_EQ(hp_int64, 300);
938 // Test buffer is valid after the modifications
939 TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(), flatbuf, length),
942 // Reset it, for further tests.
943 flatbuffers::SetField<uint16_t>(&root, hp_field, 80);
945 // More advanced functionality: changing the size of items in-line!
946 // First we put the FlatBuffer inside an std::vector.
947 std::vector<uint8_t> resizingbuf(flatbuf, flatbuf + length);
948 // Find the field we want to modify.
949 auto &name_field = *fields->LookupByKey("name");
951 // This time we wrap the result from GetAnyRoot in a smartpointer that
952 // will keep rroot valid as resizingbuf resizes.
953 auto rroot = flatbuffers::piv(
954 flatbuffers::GetAnyRoot(flatbuffers::vector_data(resizingbuf)),
956 SetString(schema, "totally new string", GetFieldS(**rroot, name_field),
958 // Here resizingbuf has changed, but rroot is still valid.
959 TEST_EQ_STR(GetFieldS(**rroot, name_field)->c_str(), "totally new string");
960 // Now lets extend a vector by 100 elements (10 -> 110).
961 auto &inventory_field = *fields->LookupByKey("inventory");
962 auto rinventory = flatbuffers::piv(
963 flatbuffers::GetFieldV<uint8_t>(**rroot, inventory_field), resizingbuf);
964 flatbuffers::ResizeVector<uint8_t>(schema, 110, 50, *rinventory,
966 // rinventory still valid, so lets read from it.
967 TEST_EQ(rinventory->Get(10), 50);
969 // For reflection uses not covered already, there is a more powerful way:
970 // we can simply generate whatever object we want to add/modify in a
971 // FlatBuffer of its own, then add that to an existing FlatBuffer:
972 // As an example, let's add a string to an array of strings.
973 // First, find our field:
974 auto &testarrayofstring_field = *fields->LookupByKey("testarrayofstring");
975 // Find the vector value:
976 auto rtestarrayofstring = flatbuffers::piv(
977 flatbuffers::GetFieldV<flatbuffers::Offset<flatbuffers::String>>(
978 **rroot, testarrayofstring_field),
980 // It's a vector of 2 strings, to which we add one more, initialized to
982 flatbuffers::ResizeVector<flatbuffers::Offset<flatbuffers::String>>(
983 schema, 3, 0, *rtestarrayofstring, &resizingbuf);
984 // Here we just create a buffer that contans a single string, but this
985 // could also be any complex set of tables and other values.
986 flatbuffers::FlatBufferBuilder stringfbb;
987 stringfbb.Finish(stringfbb.CreateString("hank"));
988 // Add the contents of it to our existing FlatBuffer.
989 // We do this last, so the pointer doesn't get invalidated (since it is
990 // at the end of the buffer):
991 auto string_ptr = flatbuffers::AddFlatBuffer(
992 resizingbuf, stringfbb.GetBufferPointer(), stringfbb.GetSize());
993 // Finally, set the new value in the vector.
994 rtestarrayofstring->MutateOffset(2, string_ptr);
995 TEST_EQ_STR(rtestarrayofstring->Get(0)->c_str(), "bob");
996 TEST_EQ_STR(rtestarrayofstring->Get(2)->c_str(), "hank");
997 // Test integrity of all resize operations above.
998 flatbuffers::Verifier resize_verifier(
999 reinterpret_cast<const uint8_t *>(flatbuffers::vector_data(resizingbuf)),
1000 resizingbuf.size());
1001 TEST_EQ(VerifyMonsterBuffer(resize_verifier), true);
1003 // Test buffer is valid using reflection as well
1004 TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(),
1005 flatbuffers::vector_data(resizingbuf),
1006 resizingbuf.size()),
1009 // As an additional test, also set it on the name field.
1010 // Note: unlike the name change above, this just overwrites the offset,
1011 // rather than changing the string in-place.
1012 SetFieldT(*rroot, name_field, string_ptr);
1013 TEST_EQ_STR(GetFieldS(**rroot, name_field)->c_str(), "hank");
1015 // Using reflection, rather than mutating binary FlatBuffers, we can also copy
1016 // tables and other things out of other FlatBuffers into a FlatBufferBuilder,
1017 // either part or whole.
1018 flatbuffers::FlatBufferBuilder fbb;
1019 auto root_offset = flatbuffers::CopyTable(
1020 fbb, schema, *root_table, *flatbuffers::GetAnyRoot(flatbuf), true);
1021 fbb.Finish(root_offset, MonsterIdentifier());
1022 // Test that it was copied correctly:
1023 AccessFlatBufferTest(fbb.GetBufferPointer(), fbb.GetSize());
1025 // Test buffer is valid using reflection as well
1026 TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(),
1027 fbb.GetBufferPointer(), fbb.GetSize()),
1031 void MiniReflectFlatBuffersTest(uint8_t *flatbuf) {
1032 auto s = flatbuffers::FlatBufferToString(flatbuf, Monster::MiniReflectTypeTable());
1036 "pos: { x: 1.0, y: 2.0, z: 3.0, test1: 0.0, test2: Red, test3: "
1037 "{ a: 10, b: 20 } }, "
1039 "name: \"MyMonster\", "
1040 "inventory: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "
1041 "test_type: Monster, "
1042 "test: { name: \"Fred\" }, "
1043 "test4: [ { a: 10, b: 20 }, { a: 30, b: 40 } ], "
1044 "testarrayofstring: [ \"bob\", \"fred\", \"bob\", \"fred\" ], "
1045 "testarrayoftables: [ { hp: 1000, name: \"Barney\" }, { name: \"Fred\" "
1047 "{ name: \"Wilma\" } ], "
1048 // TODO(wvo): should really print this nested buffer correctly.
1049 "testnestedflatbuffer: [ 20, 0, 0, 0, 77, 79, 78, 83, 12, 0, 12, 0, 0, "
1051 "4, 0, 6, 0, 8, 0, 12, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 13, 0, 0, 0, 78, "
1052 "101, 115, 116, 101, 100, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0 ], "
1053 "testarrayofstring2: [ \"jane\", \"mary\" ], "
1054 "testarrayofsortedstruct: [ { id: 1, distance: 10 }, "
1055 "{ id: 2, distance: 20 }, { id: 3, distance: 30 }, "
1056 "{ id: 4, distance: 40 } ], "
1057 "flex: [ 210, 4, 5, 2 ], "
1058 "test5: [ { a: 10, b: 20 }, { a: 30, b: 40 } ], "
1059 "vector_of_enums: [ Blue, Green ] "
1063 Vec3 vec(1,2,3, 1.5, Color_Red, test);
1064 flatbuffers::FlatBufferBuilder vec_builder;
1065 vec_builder.Finish(vec_builder.CreateStruct(vec));
1066 auto vec_buffer = vec_builder.Release();
1067 auto vec_str = flatbuffers::FlatBufferToString(vec_buffer.data(),
1068 Vec3::MiniReflectTypeTable());
1071 "{ x: 1.0, y: 2.0, z: 3.0, test1: 1.5, test2: Red, test3: { a: 16, b: 32 } }");
1074 // Parse a .proto schema, output as .fbs
1075 void ParseProtoTest() {
1076 // load the .proto and the golden file from disk
1077 std::string protofile;
1078 std::string goldenfile;
1079 std::string goldenunionfile;
1081 flatbuffers::LoadFile((test_data_path + "prototest/test.proto").c_str(),
1085 flatbuffers::LoadFile((test_data_path + "prototest/test.golden").c_str(),
1086 false, &goldenfile),
1089 flatbuffers::LoadFile((test_data_path +
1090 "prototest/test_union.golden").c_str(),
1091 false, &goldenunionfile),
1094 flatbuffers::IDLOptions opts;
1095 opts.include_dependence_headers = false;
1096 opts.proto_mode = true;
1099 flatbuffers::Parser parser(opts);
1100 auto protopath = test_data_path + "prototest/";
1101 const char *include_directories[] = { protopath.c_str(), nullptr };
1102 TEST_EQ(parser.Parse(protofile.c_str(), include_directories), true);
1105 auto fbs = flatbuffers::GenerateFBS(parser, "test");
1107 // Ensure generated file is parsable.
1108 flatbuffers::Parser parser2;
1109 TEST_EQ(parser2.Parse(fbs.c_str(), nullptr), true);
1110 TEST_EQ_STR(fbs.c_str(), goldenfile.c_str());
1112 // Parse proto with --oneof-union option.
1113 opts.proto_oneof_union = true;
1114 flatbuffers::Parser parser3(opts);
1115 TEST_EQ(parser3.Parse(protofile.c_str(), include_directories), true);
1118 auto fbs_union = flatbuffers::GenerateFBS(parser3, "test");
1120 // Ensure generated file is parsable.
1121 flatbuffers::Parser parser4;
1122 TEST_EQ(parser4.Parse(fbs_union.c_str(), nullptr), true);
1123 TEST_EQ_STR(fbs_union.c_str(), goldenunionfile.c_str());
1126 template<typename T>
1127 void CompareTableFieldValue(flatbuffers::Table *table,
1128 flatbuffers::voffset_t voffset, T val) {
1129 T read = table->GetField(voffset, static_cast<T>(0));
1133 // Low level stress/fuzz test: serialize/deserialize a variety of
1134 // different kinds of data in different combinations
1136 // Values we're testing against: chosen to ensure no bits get chopped
1137 // off anywhere, and also be different from eachother.
1138 const uint8_t bool_val = true;
1139 const int8_t char_val = -127; // 0x81
1140 const uint8_t uchar_val = 0xFF;
1141 const int16_t short_val = -32222; // 0x8222;
1142 const uint16_t ushort_val = 0xFEEE;
1143 const int32_t int_val = 0x83333333;
1144 const uint32_t uint_val = 0xFDDDDDDD;
1145 const int64_t long_val = 0x8444444444444444LL;
1146 const uint64_t ulong_val = 0xFCCCCCCCCCCCCCCCULL;
1147 const float float_val = 3.14159f;
1148 const double double_val = 3.14159265359;
1150 const int test_values_max = 11;
1151 const flatbuffers::voffset_t fields_per_object = 4;
1152 const int num_fuzz_objects = 10000; // The higher, the more thorough :)
1154 flatbuffers::FlatBufferBuilder builder;
1156 lcg_reset(); // Keep it deterministic.
1158 flatbuffers::uoffset_t objects[num_fuzz_objects];
1160 // Generate num_fuzz_objects random objects each consisting of
1161 // fields_per_object fields, each of a random type.
1162 for (int i = 0; i < num_fuzz_objects; i++) {
1163 auto start = builder.StartTable();
1164 for (flatbuffers::voffset_t f = 0; f < fields_per_object; f++) {
1165 int choice = lcg_rand() % test_values_max;
1166 auto off = flatbuffers::FieldIndexToOffset(f);
1168 case 0: builder.AddElement<uint8_t>(off, bool_val, 0); break;
1169 case 1: builder.AddElement<int8_t>(off, char_val, 0); break;
1170 case 2: builder.AddElement<uint8_t>(off, uchar_val, 0); break;
1171 case 3: builder.AddElement<int16_t>(off, short_val, 0); break;
1172 case 4: builder.AddElement<uint16_t>(off, ushort_val, 0); break;
1173 case 5: builder.AddElement<int32_t>(off, int_val, 0); break;
1174 case 6: builder.AddElement<uint32_t>(off, uint_val, 0); break;
1175 case 7: builder.AddElement<int64_t>(off, long_val, 0); break;
1176 case 8: builder.AddElement<uint64_t>(off, ulong_val, 0); break;
1177 case 9: builder.AddElement<float>(off, float_val, 0); break;
1178 case 10: builder.AddElement<double>(off, double_val, 0); break;
1181 objects[i] = builder.EndTable(start);
1183 builder.PreAlign<flatbuffers::largest_scalar_t>(0); // Align whole buffer.
1185 lcg_reset(); // Reset.
1187 uint8_t *eob = builder.GetCurrentBufferPointer() + builder.GetSize();
1189 // Test that all objects we generated are readable and return the
1190 // expected values. We generate random objects in the same order
1191 // so this is deterministic.
1192 for (int i = 0; i < num_fuzz_objects; i++) {
1193 auto table = reinterpret_cast<flatbuffers::Table *>(eob - objects[i]);
1194 for (flatbuffers::voffset_t f = 0; f < fields_per_object; f++) {
1195 int choice = lcg_rand() % test_values_max;
1196 flatbuffers::voffset_t off = flatbuffers::FieldIndexToOffset(f);
1198 case 0: CompareTableFieldValue(table, off, bool_val); break;
1199 case 1: CompareTableFieldValue(table, off, char_val); break;
1200 case 2: CompareTableFieldValue(table, off, uchar_val); break;
1201 case 3: CompareTableFieldValue(table, off, short_val); break;
1202 case 4: CompareTableFieldValue(table, off, ushort_val); break;
1203 case 5: CompareTableFieldValue(table, off, int_val); break;
1204 case 6: CompareTableFieldValue(table, off, uint_val); break;
1205 case 7: CompareTableFieldValue(table, off, long_val); break;
1206 case 8: CompareTableFieldValue(table, off, ulong_val); break;
1207 case 9: CompareTableFieldValue(table, off, float_val); break;
1208 case 10: CompareTableFieldValue(table, off, double_val); break;
1214 // High level stress/fuzz test: generate a big schema and
1215 // matching json data in random combinations, then parse both,
1216 // generate json back from the binary, and compare with the original.
1218 lcg_reset(); // Keep it deterministic.
1220 const int num_definitions = 30;
1221 const int num_struct_definitions = 5; // Subset of num_definitions.
1222 const int fields_per_definition = 15;
1223 const int instances_per_definition = 5;
1224 const int deprecation_rate = 10; // 1 in deprecation_rate fields will
1227 std::string schema = "namespace test;\n\n";
1230 std::string instances[instances_per_definition];
1232 // Since we're generating schema and corresponding data in tandem,
1233 // this convenience function adds strings to both at once.
1234 static void Add(RndDef (&definitions_l)[num_definitions],
1235 std::string &schema_l, const int instances_per_definition_l,
1236 const char *schema_add, const char *instance_add,
1238 schema_l += schema_add;
1239 for (int i = 0; i < instances_per_definition_l; i++)
1240 definitions_l[definition].instances[i] += instance_add;
1245 #define AddToSchemaAndInstances(schema_add, instance_add) \
1246 RndDef::Add(definitions, schema, instances_per_definition, \
1247 schema_add, instance_add, definition)
1250 RndDef::Add(definitions, schema, instances_per_definition, \
1251 "byte", "1", definition)
1254 RndDef definitions[num_definitions];
1256 // We are going to generate num_definitions, the first
1257 // num_struct_definitions will be structs, the rest tables. For each
1258 // generate random fields, some of which may be struct/table types
1259 // referring to previously generated structs/tables.
1260 // Simultanenously, we generate instances_per_definition JSON data
1261 // definitions, which will have identical structure to the schema
1262 // being generated. We generate multiple instances such that when creating
1263 // hierarchy, we get some variety by picking one randomly.
1264 for (int definition = 0; definition < num_definitions; definition++) {
1265 std::string definition_name = "D" + flatbuffers::NumToString(definition);
1267 bool is_struct = definition < num_struct_definitions;
1269 AddToSchemaAndInstances(
1270 ((is_struct ? "struct " : "table ") + definition_name + " {\n").c_str(),
1273 for (int field = 0; field < fields_per_definition; field++) {
1274 const bool is_last_field = field == fields_per_definition - 1;
1276 // Deprecate 1 in deprecation_rate fields. Only table fields can be
1278 // Don't deprecate the last field to avoid dangling commas in JSON.
1279 const bool deprecated =
1280 !is_struct && !is_last_field && (lcg_rand() % deprecation_rate == 0);
1282 std::string field_name = "f" + flatbuffers::NumToString(field);
1283 AddToSchemaAndInstances((" " + field_name + ":").c_str(),
1284 deprecated ? "" : (field_name + ": ").c_str());
1285 // Pick random type:
1286 auto base_type = static_cast<flatbuffers::BaseType>(
1287 lcg_rand() % (flatbuffers::BASE_TYPE_UNION + 1));
1288 switch (base_type) {
1289 case flatbuffers::BASE_TYPE_STRING:
1291 Dummy(); // No strings in structs.
1293 AddToSchemaAndInstances("string", deprecated ? "" : "\"hi\"");
1296 case flatbuffers::BASE_TYPE_VECTOR:
1298 Dummy(); // No vectors in structs.
1300 AddToSchemaAndInstances("[ubyte]",
1301 deprecated ? "" : "[\n0,\n1,\n255\n]");
1304 case flatbuffers::BASE_TYPE_NONE:
1305 case flatbuffers::BASE_TYPE_UTYPE:
1306 case flatbuffers::BASE_TYPE_STRUCT:
1307 case flatbuffers::BASE_TYPE_UNION:
1309 // Pick a random previous definition and random data instance of
1311 int defref = lcg_rand() % definition;
1312 int instance = lcg_rand() % instances_per_definition;
1313 AddToSchemaAndInstances(
1314 ("D" + flatbuffers::NumToString(defref)).c_str(),
1316 : definitions[defref].instances[instance].c_str());
1318 // If this is the first definition, we have no definition we can
1323 case flatbuffers::BASE_TYPE_BOOL:
1324 AddToSchemaAndInstances(
1325 "bool", deprecated ? "" : (lcg_rand() % 2 ? "true" : "false"));
1327 case flatbuffers::BASE_TYPE_ARRAY:
1329 AddToSchemaAndInstances(
1331 deprecated ? "" : "255"); // No fixed-length arrays in tables.
1333 AddToSchemaAndInstances("[int:3]", deprecated ? "" : "[\n,\n,\n]");
1337 // All the scalar types.
1338 schema += flatbuffers::kTypeNames[base_type];
1341 // We want each instance to use its own random value.
1342 for (int inst = 0; inst < instances_per_definition; inst++)
1343 definitions[definition].instances[inst] +=
1344 flatbuffers::IsFloat(base_type)
1345 ? flatbuffers::NumToString<double>(lcg_rand() % 128)
1347 : flatbuffers::NumToString<int>(lcg_rand() % 128).c_str();
1350 AddToSchemaAndInstances(deprecated ? "(deprecated);\n" : ";\n",
1351 deprecated ? "" : is_last_field ? "\n" : ",\n");
1353 AddToSchemaAndInstances("}\n\n", "}");
1356 schema += "root_type D" + flatbuffers::NumToString(num_definitions - 1);
1359 flatbuffers::Parser parser;
1361 // Will not compare against the original if we don't write defaults
1362 parser.builder_.ForceDefaults(true);
1364 // Parse the schema, parse the generated data, then generate text back
1365 // from the binary and compare against the original.
1366 TEST_EQ(parser.Parse(schema.c_str()), true);
1368 const std::string &json =
1369 definitions[num_definitions - 1].instances[0] + "\n";
1371 TEST_EQ(parser.Parse(json.c_str()), true);
1373 std::string jsongen;
1374 parser.opts.indent_step = 0;
1376 GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
1377 TEST_EQ(result, true);
1379 if (jsongen != json) {
1380 // These strings are larger than a megabyte, so we show the bytes around
1381 // the first bytes that are different rather than the whole string.
1382 size_t len = std::min(json.length(), jsongen.length());
1383 for (size_t i = 0; i < len; i++) {
1384 if (json[i] != jsongen[i]) {
1385 i -= std::min(static_cast<size_t>(10), i); // show some context;
1386 size_t end = std::min(len, i + 20);
1387 for (; i < end; i++)
1388 TEST_OUTPUT_LINE("at %d: found \"%c\", expected \"%c\"\n",
1389 static_cast<int>(i), jsongen[i], json[i]);
1397 #ifdef FLATBUFFERS_TEST_VERBOSE
1398 TEST_OUTPUT_LINE("%dk schema tested with %dk of json\n",
1399 static_cast<int>(schema.length() / 1024),
1400 static_cast<int>(json.length() / 1024));
1405 // Test that parser errors are actually generated.
1406 void TestError_(const char *src, const char *error_substr, bool strict_json,
1407 const char *file, int line, const char *func) {
1408 flatbuffers::IDLOptions opts;
1409 opts.strict_json = strict_json;
1410 flatbuffers::Parser parser(opts);
1411 if (parser.Parse(src)) {
1412 TestFail("true", "false",
1413 ("parser.Parse(\"" + std::string(src) + "\")").c_str(), file, line,
1415 } else if (!strstr(parser.error_.c_str(), error_substr)) {
1416 TestFail(parser.error_.c_str(), error_substr,
1417 ("parser.Parse(\"" + std::string(src) + "\")").c_str(), file, line,
1422 void TestError_(const char *src, const char *error_substr, const char *file,
1423 int line, const char *func) {
1424 TestError_(src, error_substr, false, file, line, func);
1428 # define TestError(src, ...) \
1429 TestError_(src, __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__)
1431 # define TestError(src, ...) \
1432 TestError_(src, __VA_ARGS__, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1435 // Test that parsing errors occur as we'd expect.
1436 // Also useful for coverage, making sure these paths are run.
1438 // In order they appear in idl_parser.cpp
1439 TestError("table X { Y:byte; } root_type X; { Y: 999 }", "does not fit");
1440 TestError("\"\0", "illegal");
1441 TestError("\"\\q", "escape code");
1442 TestError("table ///", "documentation");
1443 TestError("@", "illegal");
1444 TestError("table 1", "expecting");
1445 TestError("table X { Y:[[int]]; }", "nested vector");
1446 TestError("table X { Y:1; }", "illegal type");
1447 TestError("table X { Y:int; Y:int; }", "field already");
1448 TestError("table Y {} table X { Y:int; }", "same as table");
1449 TestError("struct X { Y:string; }", "only scalar");
1450 TestError("table X { Y:string = \"\"; }", "default values");
1451 TestError("struct X { a:uint = 42; }", "default values");
1452 TestError("enum Y:byte { Z = 1 } table X { y:Y; }", "not part of enum");
1453 TestError("struct X { Y:int (deprecated); }", "deprecate");
1454 TestError("union Z { X } table X { Y:Z; } root_type X; { Y: {}, A:1 }",
1455 "missing type field");
1456 TestError("union Z { X } table X { Y:Z; } root_type X; { Y_type: 99, Y: {",
1458 TestError("table X { Y:int; } root_type X; { Z:", "unknown field");
1459 TestError("table X { Y:int; } root_type X; { Y:", "string constant", true);
1460 TestError("table X { Y:int; } root_type X; { \"Y\":1, }", "string constant",
1463 "struct X { Y:int; Z:int; } table W { V:X; } root_type W; "
1466 TestError("enum E:byte { A } table X { Y:E; } root_type X; { Y:U }",
1467 "unknown enum value");
1468 TestError("table X { Y:byte; } root_type X; { Y:; }", "starting");
1469 TestError("enum X:byte { Y } enum X {", "enum already");
1470 TestError("enum X:float {}", "underlying");
1471 TestError("enum X:byte { Y, Y }", "value already");
1472 TestError("enum X:byte { Y=2, Z=1 }", "ascending");
1473 TestError("table X { Y:int; } table X {", "datatype already");
1474 TestError("struct X (force_align: 7) { Y:int; }", "force_align");
1475 TestError("struct X {}", "size 0");
1476 TestError("{}", "no root");
1477 TestError("table X { Y:byte; } root_type X; { Y:1 } { Y:1 }", "end of file");
1478 TestError("table X { Y:byte; } root_type X; { Y:1 } table Y{ Z:int }",
1480 TestError("root_type X;", "unknown root");
1481 TestError("struct X { Y:int; } root_type X;", "a table");
1482 TestError("union X { Y }", "referenced");
1483 TestError("union Z { X } struct X { Y:int; }", "only tables");
1484 TestError("table X { Y:[int]; YLength:int; }", "clash");
1485 TestError("table X { Y:byte; } root_type X; { Y:1, Y:2 }", "more than once");
1486 // float to integer conversion is forbidden
1487 TestError("table X { Y:int; } root_type X; { Y:1.0 }", "float");
1488 TestError("table X { Y:bool; } root_type X; { Y:1.0 }", "float");
1489 TestError("enum X:bool { Y = true }", "must be integral");
1492 template<typename T>
1493 T TestValue(const char *json, const char *type_name,
1494 const char *decls = nullptr) {
1495 flatbuffers::Parser parser;
1496 parser.builder_.ForceDefaults(true); // return defaults
1497 auto check_default = json ? false : true;
1498 if (check_default) { parser.opts.output_default_scalars_in_json = true; }
1500 std::string schema = std::string(decls ? decls : "") + "\n" +
1501 "table X { Y:" + std::string(type_name) +
1503 auto schema_done = parser.Parse(schema.c_str());
1504 TEST_EQ_STR(parser.error_.c_str(), "");
1505 TEST_EQ(schema_done, true);
1507 auto done = parser.Parse(check_default ? "{}" : json);
1508 TEST_EQ_STR(parser.error_.c_str(), "");
1509 TEST_EQ(done, true);
1511 // Check with print.
1512 std::string print_back;
1513 parser.opts.indent_step = -1;
1514 TEST_EQ(GenerateText(parser, parser.builder_.GetBufferPointer(), &print_back),
1516 // restore value from its default
1517 if (check_default) { TEST_EQ(parser.Parse(print_back.c_str()), true); }
1519 auto root = flatbuffers::GetRoot<flatbuffers::Table>(
1520 parser.builder_.GetBufferPointer());
1521 return root->GetField<T>(flatbuffers::FieldIndexToOffset(0), 0);
1524 bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; }
1526 // Additional parser testing not covered elsewhere.
1528 // Test scientific notation numbers.
1529 TEST_EQ(FloatCompare(TestValue<float>("{ Y:0.0314159e+2 }", "float"),
1533 TEST_EQ(FloatCompare(TestValue<float>("{ Y:\"0.0314159e+2\" }", "float"),
1537 // Test conversion functions.
1538 TEST_EQ(FloatCompare(TestValue<float>("{ Y:cos(rad(180)) }", "float"), -1),
1541 // int embedded to string
1542 TEST_EQ(TestValue<int>("{ Y:\"-876\" }", "int=-123"), -876);
1543 TEST_EQ(TestValue<int>("{ Y:\"876\" }", "int=-123"), 876);
1545 // Test negative hex constant.
1546 TEST_EQ(TestValue<int>("{ Y:-0x8ea0 }", "int=-0x8ea0"), -36512);
1547 TEST_EQ(TestValue<int>(nullptr, "int=-0x8ea0"), -36512);
1549 // positive hex constant
1550 TEST_EQ(TestValue<int>("{ Y:0x1abcdef }", "int=0x1"), 0x1abcdef);
1551 // with optional '+' sign
1552 TEST_EQ(TestValue<int>("{ Y:+0x1abcdef }", "int=+0x1"), 0x1abcdef);
1554 TEST_EQ(TestValue<int>("{ Y:\"0x1abcdef\" }", "int=+0x1"), 0x1abcdef);
1556 // Make sure we do unsigned 64bit correctly.
1557 TEST_EQ(TestValue<uint64_t>("{ Y:12335089644688340133 }", "ulong"),
1558 12335089644688340133ULL);
1561 TEST_EQ(TestValue<bool>("{ Y:\"false\" }", "bool=true"), false);
1562 TEST_EQ(TestValue<bool>("{ Y:\"true\" }", "bool=\"true\""), true);
1563 TEST_EQ(TestValue<bool>("{ Y:'false' }", "bool=true"), false);
1564 TEST_EQ(TestValue<bool>("{ Y:'true' }", "bool=\"true\""), true);
1566 // check comments before and after json object
1567 TEST_EQ(TestValue<int>("/*before*/ { Y:1 } /*after*/", "int"), 1);
1568 TEST_EQ(TestValue<int>("//before \n { Y:1 } //after", "int"), 1);
1572 void NestedListTest() {
1573 flatbuffers::Parser parser1;
1574 TEST_EQ(parser1.Parse("struct Test { a:short; b:byte; } table T { F:[Test]; }"
1576 "{ F:[ [10,20], [30,40]] }"),
1580 void EnumStringsTest() {
1581 flatbuffers::Parser parser1;
1582 TEST_EQ(parser1.Parse("enum E:byte { A, B, C } table T { F:[E]; }"
1584 "{ F:[ A, B, \"C\", \"A B C\" ] }"),
1586 flatbuffers::Parser parser2;
1587 TEST_EQ(parser2.Parse("enum E:byte { A, B, C } table T { F:[int]; }"
1589 "{ F:[ \"E.C\", \"E.A E.B E.C\" ] }"),
1591 // unsigned bit_flags
1592 flatbuffers::Parser parser3;
1594 parser3.Parse("enum E:uint16 (bit_flags) { F0, F07=7, F08, F14=14, F15 }"
1595 " table T { F: E = \"F15 F08\"; }"
1600 void EnumNamesTest() {
1601 TEST_EQ_STR("Red", EnumNameColor(Color_Red));
1602 TEST_EQ_STR("Green", EnumNameColor(Color_Green));
1603 TEST_EQ_STR("Blue", EnumNameColor(Color_Blue));
1604 // Check that Color to string don't crash while decode a mixture of Colors.
1605 // 1) Example::Color enum is enum with unfixed underlying type.
1606 // 2) Valid enum range: [0; 2^(ceil(log2(Color_ANY))) - 1].
1607 // Consequence: A value is out of this range will lead to UB (since C++17).
1608 // For details see C++17 standard or explanation on the SO:
1609 // stackoverflow.com/questions/18195312/what-happens-if-you-static-cast-invalid-value-to-enum-class
1610 TEST_EQ_STR("", EnumNameColor(static_cast<Color>(0)));
1611 TEST_EQ_STR("", EnumNameColor(static_cast<Color>(Color_ANY-1)));
1612 TEST_EQ_STR("", EnumNameColor(static_cast<Color>(Color_ANY+1)));
1615 void EnumOutOfRangeTest() {
1616 TestError("enum X:byte { Y = 128 }", "enum value does not fit");
1617 TestError("enum X:byte { Y = -129 }", "enum value does not fit");
1618 TestError("enum X:byte { Y = 126, Z0, Z1 }", "enum value does not fit");
1619 TestError("enum X:ubyte { Y = -1 }", "enum value does not fit");
1620 TestError("enum X:ubyte { Y = 256 }", "enum value does not fit");
1621 TestError("enum X:ubyte { Y = 255, Z }", "enum value does not fit");
1622 // Unions begin with an implicit "NONE = 0".
1623 TestError("table Y{} union X { Y = -1 }",
1624 "enum values must be specified in ascending order");
1625 TestError("table Y{} union X { Y = 256 }", "enum value does not fit");
1626 TestError("table Y{} union X { Y = 255, Z:Y }", "enum value does not fit");
1627 TestError("enum X:int { Y = -2147483649 }", "enum value does not fit");
1628 TestError("enum X:int { Y = 2147483648 }", "enum value does not fit");
1629 TestError("enum X:uint { Y = -1 }", "enum value does not fit");
1630 TestError("enum X:uint { Y = 4294967297 }", "enum value does not fit");
1631 TestError("enum X:long { Y = 9223372036854775808 }", "does not fit");
1632 TestError("enum X:long { Y = 9223372036854775807, Z }", "enum value does not fit");
1633 TestError("enum X:ulong { Y = -1 }", "does not fit");
1634 TestError("enum X:ubyte (bit_flags) { Y=8 }", "bit flag out");
1635 TestError("enum X:byte (bit_flags) { Y=7 }", "must be unsigned"); // -128
1636 // bit_flgs out of range
1637 TestError("enum X:ubyte (bit_flags) { Y0,Y1,Y2,Y3,Y4,Y5,Y6,Y7,Y8 }", "out of range");
1640 void EnumValueTest() {
1641 // json: "{ Y:0 }", schema: table X { Y : "E"}
1642 // 0 in enum (V=0) E then Y=0 is valid.
1643 TEST_EQ(TestValue<int>("{ Y:0 }", "E", "enum E:int { V }"), 0);
1644 TEST_EQ(TestValue<int>("{ Y:V }", "E", "enum E:int { V }"), 0);
1645 // A default value of Y is 0.
1646 TEST_EQ(TestValue<int>("{ }", "E", "enum E:int { V }"), 0);
1647 TEST_EQ(TestValue<int>("{ Y:5 }", "E=V", "enum E:int { V=5 }"), 5);
1648 // Generate json with defaults and check.
1649 TEST_EQ(TestValue<int>(nullptr, "E=V", "enum E:int { V=5 }"), 5);
1651 TEST_EQ(TestValue<int>("{ Y:5 }", "E", "enum E:int { Z, V=5 }"), 5);
1652 TEST_EQ(TestValue<int>("{ Y:5 }", "E=V", "enum E:int { Z, V=5 }"), 5);
1653 // Generate json with defaults and check.
1654 TEST_EQ(TestValue<int>(nullptr, "E", "enum E:int { Z, V=5 }"), 0);
1655 TEST_EQ(TestValue<int>(nullptr, "E=V", "enum E:int { Z, V=5 }"), 5);
1657 TEST_EQ(TestValue<uint64_t>(nullptr, "E=V",
1658 "enum E:ulong { V = 13835058055282163712 }"),
1659 13835058055282163712ULL);
1660 TEST_EQ(TestValue<uint64_t>(nullptr, "E=V",
1661 "enum E:ulong { V = 18446744073709551615 }"),
1662 18446744073709551615ULL);
1663 // Assign non-enum value to enum field. Is it right?
1664 TEST_EQ(TestValue<int>("{ Y:7 }", "E", "enum E:int { V = 0 }"), 7);
1667 void IntegerOutOfRangeTest() {
1668 TestError("table T { F:byte; } root_type T; { F:128 }",
1669 "constant does not fit");
1670 TestError("table T { F:byte; } root_type T; { F:-129 }",
1671 "constant does not fit");
1672 TestError("table T { F:ubyte; } root_type T; { F:256 }",
1673 "constant does not fit");
1674 TestError("table T { F:ubyte; } root_type T; { F:-1 }",
1675 "constant does not fit");
1676 TestError("table T { F:short; } root_type T; { F:32768 }",
1677 "constant does not fit");
1678 TestError("table T { F:short; } root_type T; { F:-32769 }",
1679 "constant does not fit");
1680 TestError("table T { F:ushort; } root_type T; { F:65536 }",
1681 "constant does not fit");
1682 TestError("table T { F:ushort; } root_type T; { F:-1 }",
1683 "constant does not fit");
1684 TestError("table T { F:int; } root_type T; { F:2147483648 }",
1685 "constant does not fit");
1686 TestError("table T { F:int; } root_type T; { F:-2147483649 }",
1687 "constant does not fit");
1688 TestError("table T { F:uint; } root_type T; { F:4294967296 }",
1689 "constant does not fit");
1690 TestError("table T { F:uint; } root_type T; { F:-1 }",
1691 "constant does not fit");
1692 // Check fixed width aliases
1693 TestError("table X { Y:uint8; } root_type X; { Y: -1 }", "does not fit");
1694 TestError("table X { Y:uint8; } root_type X; { Y: 256 }", "does not fit");
1695 TestError("table X { Y:uint16; } root_type X; { Y: -1 }", "does not fit");
1696 TestError("table X { Y:uint16; } root_type X; { Y: 65536 }", "does not fit");
1697 TestError("table X { Y:uint32; } root_type X; { Y: -1 }", "");
1698 TestError("table X { Y:uint32; } root_type X; { Y: 4294967296 }",
1700 TestError("table X { Y:uint64; } root_type X; { Y: -1 }", "");
1701 TestError("table X { Y:uint64; } root_type X; { Y: -9223372036854775809 }",
1703 TestError("table X { Y:uint64; } root_type X; { Y: 18446744073709551616 }",
1706 TestError("table X { Y:int8; } root_type X; { Y: -129 }", "does not fit");
1707 TestError("table X { Y:int8; } root_type X; { Y: 128 }", "does not fit");
1708 TestError("table X { Y:int16; } root_type X; { Y: -32769 }", "does not fit");
1709 TestError("table X { Y:int16; } root_type X; { Y: 32768 }", "does not fit");
1710 TestError("table X { Y:int32; } root_type X; { Y: -2147483649 }", "");
1711 TestError("table X { Y:int32; } root_type X; { Y: 2147483648 }",
1713 TestError("table X { Y:int64; } root_type X; { Y: -9223372036854775809 }",
1715 TestError("table X { Y:int64; } root_type X; { Y: 9223372036854775808 }",
1717 // check out-of-int64 as int8
1718 TestError("table X { Y:int8; } root_type X; { Y: -9223372036854775809 }",
1720 TestError("table X { Y:int8; } root_type X; { Y: 9223372036854775808 }",
1723 // Check default values
1724 TestError("table X { Y:int64=-9223372036854775809; } root_type X; {}",
1726 TestError("table X { Y:int64= 9223372036854775808; } root_type X; {}",
1728 TestError("table X { Y:uint64; } root_type X; { Y: -1 }", "");
1729 TestError("table X { Y:uint64=-9223372036854775809; } root_type X; {}",
1731 TestError("table X { Y:uint64= 18446744073709551616; } root_type X; {}",
1735 void IntegerBoundaryTest() {
1736 // Check numerical compatibility with non-C++ languages.
1737 // By the C++ standard, std::numerical_limits<int64_t>::min() == -9223372036854775807 (-2^63+1) or less*
1738 // The Flatbuffers grammar and most of the languages (C#, Java, Rust) expect
1739 // that minimum values are: -128, -32768,.., -9223372036854775808.
1740 // Since C++20, static_cast<int64>(0x8000000000000000ULL) is well-defined two's complement cast.
1741 // Therefore -9223372036854775808 should be valid negative value.
1742 TEST_EQ(flatbuffers::numeric_limits<int8_t>::min(), -128);
1743 TEST_EQ(flatbuffers::numeric_limits<int8_t>::max(), 127);
1744 TEST_EQ(flatbuffers::numeric_limits<int16_t>::min(), -32768);
1745 TEST_EQ(flatbuffers::numeric_limits<int16_t>::max(), 32767);
1746 TEST_EQ(flatbuffers::numeric_limits<int32_t>::min() + 1, -2147483647);
1747 TEST_EQ(flatbuffers::numeric_limits<int32_t>::max(), 2147483647ULL);
1748 TEST_EQ(flatbuffers::numeric_limits<int64_t>::min() + 1LL,
1749 -9223372036854775807LL);
1750 TEST_EQ(flatbuffers::numeric_limits<int64_t>::max(), 9223372036854775807ULL);
1751 TEST_EQ(flatbuffers::numeric_limits<uint8_t>::max(), 255);
1752 TEST_EQ(flatbuffers::numeric_limits<uint16_t>::max(), 65535);
1753 TEST_EQ(flatbuffers::numeric_limits<uint32_t>::max(), 4294967295ULL);
1754 TEST_EQ(flatbuffers::numeric_limits<uint64_t>::max(),
1755 18446744073709551615ULL);
1757 TEST_EQ(TestValue<int8_t>("{ Y:127 }", "byte"), 127);
1758 TEST_EQ(TestValue<int8_t>("{ Y:-128 }", "byte"), -128);
1759 TEST_EQ(TestValue<uint8_t>("{ Y:255 }", "ubyte"), 255);
1760 TEST_EQ(TestValue<uint8_t>("{ Y:0 }", "ubyte"), 0);
1761 TEST_EQ(TestValue<int16_t>("{ Y:32767 }", "short"), 32767);
1762 TEST_EQ(TestValue<int16_t>("{ Y:-32768 }", "short"), -32768);
1763 TEST_EQ(TestValue<uint16_t>("{ Y:65535 }", "ushort"), 65535);
1764 TEST_EQ(TestValue<uint16_t>("{ Y:0 }", "ushort"), 0);
1765 TEST_EQ(TestValue<int32_t>("{ Y:2147483647 }", "int"), 2147483647);
1766 TEST_EQ(TestValue<int32_t>("{ Y:-2147483648 }", "int") + 1, -2147483647);
1767 TEST_EQ(TestValue<uint32_t>("{ Y:4294967295 }", "uint"), 4294967295);
1768 TEST_EQ(TestValue<uint32_t>("{ Y:0 }", "uint"), 0);
1769 TEST_EQ(TestValue<int64_t>("{ Y:9223372036854775807 }", "long"),
1770 9223372036854775807LL);
1771 TEST_EQ(TestValue<int64_t>("{ Y:-9223372036854775808 }", "long") + 1LL,
1772 -9223372036854775807LL);
1773 TEST_EQ(TestValue<uint64_t>("{ Y:18446744073709551615 }", "ulong"),
1774 18446744073709551615ULL);
1775 TEST_EQ(TestValue<uint64_t>("{ Y:0 }", "ulong"), 0);
1776 TEST_EQ(TestValue<uint64_t>("{ Y: 18446744073709551615 }", "uint64"),
1777 18446744073709551615ULL);
1778 // check that the default works
1779 TEST_EQ(TestValue<uint64_t>(nullptr, "uint64 = 18446744073709551615"),
1780 18446744073709551615ULL);
1783 void ValidFloatTest() {
1784 // check rounding to infinity
1785 TEST_EQ(TestValue<float>("{ Y:+3.4029e+38 }", "float"), +infinityf);
1786 TEST_EQ(TestValue<float>("{ Y:-3.4029e+38 }", "float"), -infinityf);
1787 TEST_EQ(TestValue<double>("{ Y:+1.7977e+308 }", "double"), +infinityd);
1788 TEST_EQ(TestValue<double>("{ Y:-1.7977e+308 }", "double"), -infinityd);
1791 FloatCompare(TestValue<float>("{ Y:0.0314159e+2 }", "float"), 3.14159f),
1794 TEST_EQ(FloatCompare(TestValue<float>("{ Y:\" 0.0314159e+2 \" }", "float"),
1798 TEST_EQ(TestValue<float>("{ Y:1 }", "float"), 1.0f);
1799 TEST_EQ(TestValue<float>("{ Y:1.0 }", "float"), 1.0f);
1800 TEST_EQ(TestValue<float>("{ Y:1. }", "float"), 1.0f);
1801 TEST_EQ(TestValue<float>("{ Y:+1. }", "float"), 1.0f);
1802 TEST_EQ(TestValue<float>("{ Y:-1. }", "float"), -1.0f);
1803 TEST_EQ(TestValue<float>("{ Y:1.e0 }", "float"), 1.0f);
1804 TEST_EQ(TestValue<float>("{ Y:1.e+0 }", "float"), 1.0f);
1805 TEST_EQ(TestValue<float>("{ Y:1.e-0 }", "float"), 1.0f);
1806 TEST_EQ(TestValue<float>("{ Y:0.125 }", "float"), 0.125f);
1807 TEST_EQ(TestValue<float>("{ Y:.125 }", "float"), 0.125f);
1808 TEST_EQ(TestValue<float>("{ Y:-.125 }", "float"), -0.125f);
1809 TEST_EQ(TestValue<float>("{ Y:+.125 }", "float"), +0.125f);
1810 TEST_EQ(TestValue<float>("{ Y:5 }", "float"), 5.0f);
1811 TEST_EQ(TestValue<float>("{ Y:\"5\" }", "float"), 5.0f);
1813 #if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
1814 // Old MSVC versions may have problem with this check.
1815 // https://www.exploringbinary.com/visual-c-plus-plus-strtod-still-broken/
1816 TEST_EQ(TestValue<double>("{ Y:6.9294956446009195e15 }", "double"),
1817 6929495644600920.0);
1819 TEST_EQ(std::isnan(TestValue<double>("{ Y:nan }", "double")), true);
1820 TEST_EQ(std::isnan(TestValue<float>("{ Y:nan }", "float")), true);
1821 TEST_EQ(std::isnan(TestValue<float>("{ Y:\"nan\" }", "float")), true);
1822 TEST_EQ(std::isnan(TestValue<float>("{ Y:+nan }", "float")), true);
1823 TEST_EQ(std::isnan(TestValue<float>("{ Y:-nan }", "float")), true);
1824 TEST_EQ(std::isnan(TestValue<float>(nullptr, "float=nan")), true);
1825 TEST_EQ(std::isnan(TestValue<float>(nullptr, "float=-nan")), true);
1827 TEST_EQ(TestValue<float>("{ Y:inf }", "float"), infinityf);
1828 TEST_EQ(TestValue<float>("{ Y:\"inf\" }", "float"), infinityf);
1829 TEST_EQ(TestValue<float>("{ Y:+inf }", "float"), infinityf);
1830 TEST_EQ(TestValue<float>("{ Y:-inf }", "float"), -infinityf);
1831 TEST_EQ(TestValue<float>(nullptr, "float=inf"), infinityf);
1832 TEST_EQ(TestValue<float>(nullptr, "float=-inf"), -infinityf);
1834 "{ Y : [0.2, .2, 1.0, -1.0, -2., 2., 1e0, -1e0, 1.0e0, -1.0e0, -3.e2, "
1838 "{ Y : [0.2, .2, 1.0, -1.0, -2., 2., 1e0, -1e0, 1.0e0, -1.0e0, -3.e2, "
1842 // Test binary format of float point.
1843 // https://en.cppreference.com/w/cpp/language/floating_literal
1844 // 0x11.12p-1 = (1*16^1 + 2*16^0 + 3*16^-1 + 4*16^-2) * 2^-1 =
1845 TEST_EQ(TestValue<double>("{ Y:0x12.34p-1 }", "double"), 9.1015625);
1846 // hex fraction 1.2 (decimal 1.125) scaled by 2^3, that is 9.0
1847 TEST_EQ(TestValue<float>("{ Y:-0x0.2p0 }", "float"), -0.125f);
1848 TEST_EQ(TestValue<float>("{ Y:-0x.2p1 }", "float"), -0.25f);
1849 TEST_EQ(TestValue<float>("{ Y:0x1.2p3 }", "float"), 9.0f);
1850 TEST_EQ(TestValue<float>("{ Y:0x10.1p0 }", "float"), 16.0625f);
1851 TEST_EQ(TestValue<double>("{ Y:0x1.2p3 }", "double"), 9.0);
1852 TEST_EQ(TestValue<double>("{ Y:0x10.1p0 }", "double"), 16.0625);
1853 TEST_EQ(TestValue<double>("{ Y:0xC.68p+2 }", "double"), 49.625);
1854 TestValue<double>("{ Y : [0x20.4ep1, +0x20.4ep1, -0x20.4ep1] }", "[double]");
1855 TestValue<float>("{ Y : [0x20.4ep1, +0x20.4ep1, -0x20.4ep1] }", "[float]");
1857 #else // FLATBUFFERS_HAS_NEW_STRTOD
1858 TEST_OUTPUT_LINE("FLATBUFFERS_HAS_NEW_STRTOD tests skipped");
1859 #endif // !FLATBUFFERS_HAS_NEW_STRTOD
1862 void InvalidFloatTest() {
1863 auto invalid_msg = "invalid number";
1864 auto comma_msg = "expecting: ,";
1865 TestError("table T { F:float; } root_type T; { F:1,0 }", "");
1866 TestError("table T { F:float; } root_type T; { F:. }", "");
1867 TestError("table T { F:float; } root_type T; { F:- }", invalid_msg);
1868 TestError("table T { F:float; } root_type T; { F:+ }", invalid_msg);
1869 TestError("table T { F:float; } root_type T; { F:-. }", invalid_msg);
1870 TestError("table T { F:float; } root_type T; { F:+. }", invalid_msg);
1871 TestError("table T { F:float; } root_type T; { F:.e }", "");
1872 TestError("table T { F:float; } root_type T; { F:-e }", invalid_msg);
1873 TestError("table T { F:float; } root_type T; { F:+e }", invalid_msg);
1874 TestError("table T { F:float; } root_type T; { F:-.e }", invalid_msg);
1875 TestError("table T { F:float; } root_type T; { F:+.e }", invalid_msg);
1876 TestError("table T { F:float; } root_type T; { F:-e1 }", invalid_msg);
1877 TestError("table T { F:float; } root_type T; { F:+e1 }", invalid_msg);
1878 TestError("table T { F:float; } root_type T; { F:1.0e+ }", invalid_msg);
1879 TestError("table T { F:float; } root_type T; { F:1.0e- }", invalid_msg);
1880 // exponent pP is mandatory for hex-float
1881 TestError("table T { F:float; } root_type T; { F:0x0 }", invalid_msg);
1882 TestError("table T { F:float; } root_type T; { F:-0x. }", invalid_msg);
1883 TestError("table T { F:float; } root_type T; { F:0x. }", invalid_msg);
1884 // eE not exponent in hex-float!
1885 TestError("table T { F:float; } root_type T; { F:0x0.0e+ }", invalid_msg);
1886 TestError("table T { F:float; } root_type T; { F:0x0.0e- }", invalid_msg);
1887 TestError("table T { F:float; } root_type T; { F:0x0.0p }", invalid_msg);
1888 TestError("table T { F:float; } root_type T; { F:0x0.0p+ }", invalid_msg);
1889 TestError("table T { F:float; } root_type T; { F:0x0.0p- }", invalid_msg);
1890 TestError("table T { F:float; } root_type T; { F:0x0.0pa1 }", invalid_msg);
1891 TestError("table T { F:float; } root_type T; { F:0x0.0e+ }", invalid_msg);
1892 TestError("table T { F:float; } root_type T; { F:0x0.0e- }", invalid_msg);
1893 TestError("table T { F:float; } root_type T; { F:0x0.0e+0 }", invalid_msg);
1894 TestError("table T { F:float; } root_type T; { F:0x0.0e-0 }", invalid_msg);
1895 TestError("table T { F:float; } root_type T; { F:0x0.0ep+ }", invalid_msg);
1896 TestError("table T { F:float; } root_type T; { F:0x0.0ep- }", invalid_msg);
1897 TestError("table T { F:float; } root_type T; { F:1.2.3 }", invalid_msg);
1898 TestError("table T { F:float; } root_type T; { F:1.2.e3 }", invalid_msg);
1899 TestError("table T { F:float; } root_type T; { F:1.2e.3 }", invalid_msg);
1900 TestError("table T { F:float; } root_type T; { F:1.2e0.3 }", invalid_msg);
1901 TestError("table T { F:float; } root_type T; { F:1.2e3. }", invalid_msg);
1902 TestError("table T { F:float; } root_type T; { F:1.2e3.0 }", invalid_msg);
1903 TestError("table T { F:float; } root_type T; { F:+-1.0 }", invalid_msg);
1904 TestError("table T { F:float; } root_type T; { F:1.0e+-1 }", invalid_msg);
1905 TestError("table T { F:float; } root_type T; { F:\"1.0e+-1\" }", invalid_msg);
1906 TestError("table T { F:float; } root_type T; { F:1.e0e }", comma_msg);
1907 TestError("table T { F:float; } root_type T; { F:0x1.p0e }", comma_msg);
1908 TestError("table T { F:float; } root_type T; { F:\" 0x10 \" }", invalid_msg);
1910 TestError("table T { F:float; } root_type T; { F:\"1,2.\" }", invalid_msg);
1911 TestError("table T { F:float; } root_type T; { F:\"1.2e3.\" }", invalid_msg);
1912 TestError("table T { F:float; } root_type T; { F:\"0x1.p0e\" }", invalid_msg);
1913 TestError("table T { F:float; } root_type T; { F:\"0x1.0\" }", invalid_msg);
1914 TestError("table T { F:float; } root_type T; { F:\" 0x1.0\" }", invalid_msg);
1915 TestError("table T { F:float; } root_type T; { F:\"+ 0\" }", invalid_msg);
1916 // disable escapes for "number-in-string"
1917 TestError("table T { F:float; } root_type T; { F:\"\\f1.2e3.\" }", "invalid");
1918 TestError("table T { F:float; } root_type T; { F:\"\\t1.2e3.\" }", "invalid");
1919 TestError("table T { F:float; } root_type T; { F:\"\\n1.2e3.\" }", "invalid");
1920 TestError("table T { F:float; } root_type T; { F:\"\\r1.2e3.\" }", "invalid");
1921 TestError("table T { F:float; } root_type T; { F:\"4\\x005\" }", "invalid");
1922 TestError("table T { F:float; } root_type T; { F:\"\'12\'\" }", invalid_msg);
1923 // null is not a number constant!
1924 TestError("table T { F:float; } root_type T; { F:\"null\" }", invalid_msg);
1925 TestError("table T { F:float; } root_type T; { F:null }", invalid_msg);
1928 void GenerateTableTextTest() {
1929 std::string schemafile;
1930 std::string jsonfile;
1932 flatbuffers::LoadFile((test_data_path + "monster_test.fbs").c_str(),
1933 false, &schemafile) &&
1934 flatbuffers::LoadFile((test_data_path + "monsterdata_test.json").c_str(),
1937 auto include_test_path =
1938 flatbuffers::ConCatPathFileName(test_data_path, "include_test");
1939 const char *include_directories[] = {test_data_path.c_str(),
1940 include_test_path.c_str(), nullptr};
1941 flatbuffers::IDLOptions opt;
1942 opt.indent_step = -1;
1943 flatbuffers::Parser parser(opt);
1944 ok = parser.Parse(schemafile.c_str(), include_directories) &&
1945 parser.Parse(jsonfile.c_str(), include_directories);
1948 const Monster *monster = GetMonster(parser.builder_.GetBufferPointer());
1949 std::string jsongen;
1950 auto result = GenerateTextFromTable(parser, monster, "MyGame.Example.Monster",
1952 TEST_EQ(result, true);
1954 const Vec3 *pos = monster->pos();
1956 result = GenerateTextFromTable(parser, pos, "MyGame.Example.Vec3", &jsongen);
1957 TEST_EQ(result, true);
1960 "{x: 1.0,y: 2.0,z: 3.0,test1: 3.0,test2: \"Green\",test3: {a: 5,b: 6}}");
1961 const Test &test3 = pos->test3();
1964 GenerateTextFromTable(parser, &test3, "MyGame.Example.Test", &jsongen);
1965 TEST_EQ(result, true);
1966 TEST_EQ_STR(jsongen.c_str(), "{a: 5,b: 6}");
1967 const Test *test4 = monster->test4()->Get(0);
1970 GenerateTextFromTable(parser, test4, "MyGame.Example.Test", &jsongen);
1971 TEST_EQ(result, true);
1972 TEST_EQ_STR(jsongen.c_str(), "{a: 10,b: 20}");
1975 template<typename T>
1976 void NumericUtilsTestInteger(const char *lower, const char *upper) {
1978 TEST_EQ(flatbuffers::StringToNumber("1q", &x), false);
1980 TEST_EQ(flatbuffers::StringToNumber(upper, &x), false);
1981 TEST_EQ(x, flatbuffers::numeric_limits<T>::max());
1982 TEST_EQ(flatbuffers::StringToNumber(lower, &x), false);
1983 auto expval = flatbuffers::is_unsigned<T>::value
1984 ? flatbuffers::numeric_limits<T>::max()
1985 : flatbuffers::numeric_limits<T>::lowest();
1989 template<typename T>
1990 void NumericUtilsTestFloat(const char *lower, const char *upper) {
1992 TEST_EQ(flatbuffers::StringToNumber("", &f), false);
1993 TEST_EQ(flatbuffers::StringToNumber("1q", &f), false);
1995 TEST_EQ(flatbuffers::StringToNumber(upper, &f), true);
1996 TEST_EQ(f, +flatbuffers::numeric_limits<T>::infinity());
1997 TEST_EQ(flatbuffers::StringToNumber(lower, &f), true);
1998 TEST_EQ(f, -flatbuffers::numeric_limits<T>::infinity());
2001 void NumericUtilsTest() {
2002 NumericUtilsTestInteger<uint64_t>("-1", "18446744073709551616");
2003 NumericUtilsTestInteger<uint8_t>("-1", "256");
2004 NumericUtilsTestInteger<int64_t>("-9223372036854775809",
2005 "9223372036854775808");
2006 NumericUtilsTestInteger<int8_t>("-129", "128");
2007 NumericUtilsTestFloat<float>("-3.4029e+38", "+3.4029e+38");
2008 NumericUtilsTestFloat<float>("-1.7977e+308", "+1.7977e+308");
2011 void IsAsciiUtilsTest() {
2013 for (int cnt = 0; cnt < 256; cnt++) {
2014 auto alpha = (('a' <= c) && (c <= 'z')) || (('A' <= c) && (c <= 'Z'));
2015 auto dec = (('0' <= c) && (c <= '9'));
2016 auto hex = (('a' <= c) && (c <= 'f')) || (('A' <= c) && (c <= 'F'));
2017 TEST_EQ(flatbuffers::is_alpha(c), alpha);
2018 TEST_EQ(flatbuffers::is_alnum(c), alpha || dec);
2019 TEST_EQ(flatbuffers::is_digit(c), dec);
2020 TEST_EQ(flatbuffers::is_xdigit(c), dec || hex);
2025 void UnicodeTest() {
2026 flatbuffers::Parser parser;
2027 // Without setting allow_non_utf8 = true, we treat \x sequences as byte
2028 // sequences which are then validated as UTF-8.
2029 TEST_EQ(parser.Parse("table T { F:string; }"
2031 "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
2032 "\\u5225\\u30B5\\u30A4\\u30C8\\xE2\\x82\\xAC\\u0080\\uD8"
2035 std::string jsongen;
2036 parser.opts.indent_step = -1;
2038 GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
2039 TEST_EQ(result, true);
2040 TEST_EQ_STR(jsongen.c_str(),
2041 "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
2042 "\\u5225\\u30B5\\u30A4\\u30C8\\u20AC\\u0080\\uD83D\\uDE0E\"}");
2045 void UnicodeTestAllowNonUTF8() {
2046 flatbuffers::Parser parser;
2047 parser.opts.allow_non_utf8 = true;
2050 "table T { F:string; }"
2052 "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
2053 "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\\u0080\\uD83D\\uDE0E\" }"),
2055 std::string jsongen;
2056 parser.opts.indent_step = -1;
2058 GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
2059 TEST_EQ(result, true);
2062 "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
2063 "\\u5225\\u30B5\\u30A4\\u30C8\\u0001\\x80\\u0080\\uD83D\\uDE0E\"}");
2066 void UnicodeTestGenerateTextFailsOnNonUTF8() {
2067 flatbuffers::Parser parser;
2068 // Allow non-UTF-8 initially to model what happens when we load a binary
2069 // flatbuffer from disk which contains non-UTF-8 strings.
2070 parser.opts.allow_non_utf8 = true;
2073 "table T { F:string; }"
2075 "{ F:\"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC"
2076 "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\\u0080\\uD83D\\uDE0E\" }"),
2078 std::string jsongen;
2079 parser.opts.indent_step = -1;
2080 // Now, disallow non-UTF-8 (the default behavior) so GenerateText indicates
2082 parser.opts.allow_non_utf8 = false;
2084 GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
2085 TEST_EQ(result, false);
2088 void UnicodeSurrogatesTest() {
2089 flatbuffers::Parser parser;
2091 TEST_EQ(parser.Parse("table T { F:string (id: 0); }"
2093 "{ F:\"\\uD83D\\uDCA9\"}"),
2095 auto root = flatbuffers::GetRoot<flatbuffers::Table>(
2096 parser.builder_.GetBufferPointer());
2097 auto string = root->GetPointer<flatbuffers::String *>(
2098 flatbuffers::FieldIndexToOffset(0));
2099 TEST_EQ_STR(string->c_str(), "\xF0\x9F\x92\xA9");
2102 void UnicodeInvalidSurrogatesTest() {
2104 "table T { F:string; }"
2107 "unpaired high surrogate");
2109 "table T { F:string; }"
2111 "{ F:\"\\uD800abcd\"}",
2112 "unpaired high surrogate");
2114 "table T { F:string; }"
2116 "{ F:\"\\uD800\\n\"}",
2117 "unpaired high surrogate");
2119 "table T { F:string; }"
2121 "{ F:\"\\uD800\\uD800\"}",
2122 "multiple high surrogates");
2124 "table T { F:string; }"
2127 "unpaired low surrogate");
2130 void InvalidUTF8Test() {
2131 // "1 byte" pattern, under min length of 2 bytes
2133 "table T { F:string; }"
2136 "illegal UTF-8 sequence");
2137 // 2 byte pattern, string too short
2139 "table T { F:string; }"
2142 "illegal UTF-8 sequence");
2143 // 3 byte pattern, string too short
2145 "table T { F:string; }"
2147 "{ F:\"\xEF\xBF\"}",
2148 "illegal UTF-8 sequence");
2149 // 4 byte pattern, string too short
2151 "table T { F:string; }"
2153 "{ F:\"\xF7\xBF\xBF\"}",
2154 "illegal UTF-8 sequence");
2155 // "5 byte" pattern, string too short
2157 "table T { F:string; }"
2159 "{ F:\"\xFB\xBF\xBF\xBF\"}",
2160 "illegal UTF-8 sequence");
2161 // "6 byte" pattern, string too short
2163 "table T { F:string; }"
2165 "{ F:\"\xFD\xBF\xBF\xBF\xBF\"}",
2166 "illegal UTF-8 sequence");
2167 // "7 byte" pattern, string too short
2169 "table T { F:string; }"
2171 "{ F:\"\xFE\xBF\xBF\xBF\xBF\xBF\"}",
2172 "illegal UTF-8 sequence");
2173 // "5 byte" pattern, over max length of 4 bytes
2175 "table T { F:string; }"
2177 "{ F:\"\xFB\xBF\xBF\xBF\xBF\"}",
2178 "illegal UTF-8 sequence");
2179 // "6 byte" pattern, over max length of 4 bytes
2181 "table T { F:string; }"
2183 "{ F:\"\xFD\xBF\xBF\xBF\xBF\xBF\"}",
2184 "illegal UTF-8 sequence");
2185 // "7 byte" pattern, over max length of 4 bytes
2187 "table T { F:string; }"
2189 "{ F:\"\xFE\xBF\xBF\xBF\xBF\xBF\xBF\"}",
2190 "illegal UTF-8 sequence");
2192 // Three invalid encodings for U+000A (\n, aka NEWLINE)
2194 "table T { F:string; }"
2196 "{ F:\"\xC0\x8A\"}",
2197 "illegal UTF-8 sequence");
2199 "table T { F:string; }"
2201 "{ F:\"\xE0\x80\x8A\"}",
2202 "illegal UTF-8 sequence");
2204 "table T { F:string; }"
2206 "{ F:\"\xF0\x80\x80\x8A\"}",
2207 "illegal UTF-8 sequence");
2209 // Two invalid encodings for U+00A9 (COPYRIGHT SYMBOL)
2211 "table T { F:string; }"
2213 "{ F:\"\xE0\x81\xA9\"}",
2214 "illegal UTF-8 sequence");
2216 "table T { F:string; }"
2218 "{ F:\"\xF0\x80\x81\xA9\"}",
2219 "illegal UTF-8 sequence");
2221 // Invalid encoding for U+20AC (EURO SYMBOL)
2223 "table T { F:string; }"
2225 "{ F:\"\xF0\x82\x82\xAC\"}",
2226 "illegal UTF-8 sequence");
2228 // UTF-16 surrogate values between U+D800 and U+DFFF cannot be encoded in
2231 "table T { F:string; }"
2233 // U+10400 "encoded" as U+D801 U+DC00
2234 "{ F:\"\xED\xA0\x81\xED\xB0\x80\"}",
2235 "illegal UTF-8 sequence");
2237 // Check independence of identifier from locale.
2238 std::string locale_ident;
2239 locale_ident += "table T { F";
2240 locale_ident += static_cast<char>(-32); // unsigned 0xE0
2241 locale_ident += " :string; }";
2242 locale_ident += "root_type T;";
2243 locale_ident += "{}";
2244 TestError(locale_ident.c_str(), "");
2247 void UnknownFieldsTest() {
2248 flatbuffers::IDLOptions opts;
2249 opts.skip_unexpected_fields_in_json = true;
2250 flatbuffers::Parser parser(opts);
2252 TEST_EQ(parser.Parse("table T { str:string; i:int;}"
2255 "unknown_string:\"test\","
2256 "\"unknown_string\":\"test\","
2258 "unknown_float:1.0,"
2259 "unknown_array: [ 1, 2, 3, 4],"
2260 "unknown_object: { i: 10 },"
2261 "\"unknown_object\": { \"i\": 10 },"
2265 std::string jsongen;
2266 parser.opts.indent_step = -1;
2268 GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
2269 TEST_EQ(result, true);
2270 TEST_EQ_STR(jsongen.c_str(), "{str: \"test\",i: 10}");
2273 void ParseUnionTest() {
2274 // Unions must be parseable with the type field following the object.
2275 flatbuffers::Parser parser;
2276 TEST_EQ(parser.Parse("table T { A:int; }"
2280 "{ X:{ A:1 }, X_type: T }"),
2282 // Unions must be parsable with prefixed namespace.
2283 flatbuffers::Parser parser2;
2284 TEST_EQ(parser2.Parse("namespace N; table A {} namespace; union U { N.A }"
2285 "table B { e:U; } root_type B;"
2286 "{ e_type: N_A, e: {} }"),
2290 void InvalidNestedFlatbufferTest() {
2291 // First, load and parse FlatBuffer schema (.fbs)
2292 std::string schemafile;
2293 TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.fbs").c_str(),
2294 false, &schemafile),
2296 auto include_test_path =
2297 flatbuffers::ConCatPathFileName(test_data_path, "include_test");
2298 const char *include_directories[] = { test_data_path.c_str(),
2299 include_test_path.c_str(), nullptr };
2300 flatbuffers::Parser parser1;
2301 TEST_EQ(parser1.Parse(schemafile.c_str(), include_directories), true);
2303 // "color" inside nested flatbuffer contains invalid enum value
2304 TEST_EQ(parser1.Parse("{ name: \"Bender\", testnestedflatbuffer: { name: "
2305 "\"Leela\", color: \"nonexistent\"}}"),
2307 // Check that Parser is destroyed correctly after parsing invalid json
2310 void UnionVectorTest() {
2311 // load FlatBuffer fbs schema and json.
2312 std::string schemafile, jsonfile;
2313 TEST_EQ(flatbuffers::LoadFile(
2314 (test_data_path + "union_vector/union_vector.fbs").c_str(),
2315 false, &schemafile),
2317 TEST_EQ(flatbuffers::LoadFile(
2318 (test_data_path + "union_vector/union_vector.json").c_str(),
2323 flatbuffers::IDLOptions idl_opts;
2324 idl_opts.lang_to_generate |= flatbuffers::IDLOptions::kBinary;
2325 flatbuffers::Parser parser(idl_opts);
2326 TEST_EQ(parser.Parse(schemafile.c_str()), true);
2328 flatbuffers::FlatBufferBuilder fbb;
2331 std::vector<uint8_t> types;
2332 types.push_back(static_cast<uint8_t>(Character_Belle));
2333 types.push_back(static_cast<uint8_t>(Character_MuLan));
2334 types.push_back(static_cast<uint8_t>(Character_BookFan));
2335 types.push_back(static_cast<uint8_t>(Character_Other));
2336 types.push_back(static_cast<uint8_t>(Character_Unused));
2339 std::vector<flatbuffers::Offset<void>> characters;
2340 characters.push_back(fbb.CreateStruct(BookReader(/*books_read=*/7)).Union());
2341 characters.push_back(CreateAttacker(fbb, /*sword_attack_damage=*/5).Union());
2342 characters.push_back(fbb.CreateStruct(BookReader(/*books_read=*/2)).Union());
2343 characters.push_back(fbb.CreateString("Other").Union());
2344 characters.push_back(fbb.CreateString("Unused").Union());
2347 const auto movie_offset =
2348 CreateMovie(fbb, Character_Rapunzel,
2349 fbb.CreateStruct(Rapunzel(/*hair_length=*/6)).Union(),
2350 fbb.CreateVector(types), fbb.CreateVector(characters));
2351 FinishMovieBuffer(fbb, movie_offset);
2353 flatbuffers::Verifier verifier(fbb.GetBufferPointer(), fbb.GetSize());
2354 TEST_EQ(VerifyMovieBuffer(verifier), true);
2356 auto flat_movie = GetMovie(fbb.GetBufferPointer());
2358 auto TestMovie = [](const Movie *movie) {
2359 TEST_EQ(movie->main_character_type() == Character_Rapunzel, true);
2361 auto cts = movie->characters_type();
2362 TEST_EQ(movie->characters_type()->size(), 5);
2363 TEST_EQ(cts->GetEnum<Character>(0) == Character_Belle, true);
2364 TEST_EQ(cts->GetEnum<Character>(1) == Character_MuLan, true);
2365 TEST_EQ(cts->GetEnum<Character>(2) == Character_BookFan, true);
2366 TEST_EQ(cts->GetEnum<Character>(3) == Character_Other, true);
2367 TEST_EQ(cts->GetEnum<Character>(4) == Character_Unused, true);
2369 auto rapunzel = movie->main_character_as_Rapunzel();
2370 TEST_NOTNULL(rapunzel);
2371 TEST_EQ(rapunzel->hair_length(), 6);
2373 auto cs = movie->characters();
2374 TEST_EQ(cs->size(), 5);
2375 auto belle = cs->GetAs<BookReader>(0);
2376 TEST_EQ(belle->books_read(), 7);
2377 auto mu_lan = cs->GetAs<Attacker>(1);
2378 TEST_EQ(mu_lan->sword_attack_damage(), 5);
2379 auto book_fan = cs->GetAs<BookReader>(2);
2380 TEST_EQ(book_fan->books_read(), 2);
2381 auto other = cs->GetAsString(3);
2382 TEST_EQ_STR(other->c_str(), "Other");
2383 auto unused = cs->GetAsString(4);
2384 TEST_EQ_STR(unused->c_str(), "Unused");
2387 TestMovie(flat_movie);
2389 // Also test the JSON we loaded above.
2390 TEST_EQ(parser.Parse(jsonfile.c_str()), true);
2391 auto jbuf = parser.builder_.GetBufferPointer();
2392 flatbuffers::Verifier jverifier(jbuf, parser.builder_.GetSize());
2393 TEST_EQ(VerifyMovieBuffer(jverifier), true);
2394 TestMovie(GetMovie(jbuf));
2396 auto movie_object = flat_movie->UnPack();
2397 TEST_EQ(movie_object->main_character.AsRapunzel()->hair_length(), 6);
2398 TEST_EQ(movie_object->characters[0].AsBelle()->books_read(), 7);
2399 TEST_EQ(movie_object->characters[1].AsMuLan()->sword_attack_damage, 5);
2400 TEST_EQ(movie_object->characters[2].AsBookFan()->books_read(), 2);
2401 TEST_EQ_STR(movie_object->characters[3].AsOther()->c_str(), "Other");
2402 TEST_EQ_STR(movie_object->characters[4].AsUnused()->c_str(), "Unused");
2405 fbb.Finish(Movie::Pack(fbb, movie_object));
2407 delete movie_object;
2409 auto repacked_movie = GetMovie(fbb.GetBufferPointer());
2411 TestMovie(repacked_movie);
2413 // Generate text using mini-reflection.
2415 flatbuffers::FlatBufferToString(fbb.GetBufferPointer(), MovieTypeTable());
2418 "{ main_character_type: Rapunzel, main_character: { hair_length: 6 }, "
2419 "characters_type: [ Belle, MuLan, BookFan, Other, Unused ], "
2420 "characters: [ { books_read: 7 }, { sword_attack_damage: 5 }, "
2421 "{ books_read: 2 }, \"Other\", \"Unused\" ] }");
2424 flatbuffers::ToStringVisitor visitor("\n", true, " ");
2425 IterateFlatBuffer(fbb.GetBufferPointer(), MovieTypeTable(), &visitor);
2429 " \"main_character_type\": \"Rapunzel\",\n"
2430 " \"main_character\": {\n"
2431 " \"hair_length\": 6\n"
2433 " \"characters_type\": [\n"
2440 " \"characters\": [\n"
2442 " \"books_read\": 7\n"
2445 " \"sword_attack_damage\": 5\n"
2448 " \"books_read\": 2\n"
2455 // Generate text using parsed schema.
2456 std::string jsongen;
2457 auto result = GenerateText(parser, fbb.GetBufferPointer(), &jsongen);
2458 TEST_EQ(result, true);
2462 " main_character_type: \"Rapunzel\",\n"
2463 " main_character: {\n"
2466 " characters_type: [\n"
2478 " sword_attack_damage: 5\n"
2488 // Simple test with reflection.
2490 auto schema = reflection::GetSchema(parser.builder_.GetBufferPointer());
2491 auto ok = flatbuffers::Verify(*schema, *schema->root_table(),
2492 fbb.GetBufferPointer(), fbb.GetSize());
2495 flatbuffers::Parser parser2(idl_opts);
2496 TEST_EQ(parser2.Parse("struct Bool { b:bool; }"
2497 "union Any { Bool }"
2498 "table Root { a:Any; }"
2499 "root_type Root;"), true);
2500 TEST_EQ(parser2.Parse("{a_type:Bool,a:{b:true}}"), true);
2503 void ConformTest() {
2504 flatbuffers::Parser parser;
2505 TEST_EQ(parser.Parse("table T { A:int; } enum E:byte { A }"), true);
2507 auto test_conform = [](flatbuffers::Parser &parser1, const char *test,
2508 const char *expected_err) {
2509 flatbuffers::Parser parser2;
2510 TEST_EQ(parser2.Parse(test), true);
2511 auto err = parser2.ConformTo(parser1);
2512 TEST_NOTNULL(strstr(err.c_str(), expected_err));
2515 test_conform(parser, "table T { A:byte; }", "types differ for field");
2516 test_conform(parser, "table T { B:int; A:int; }", "offsets differ for field");
2517 test_conform(parser, "table T { A:int = 1; }", "defaults differ for field");
2518 test_conform(parser, "table T { B:float; }",
2519 "field renamed to different type");
2520 test_conform(parser, "enum E:byte { B, A }", "values differ for enum");
2523 void ParseProtoBufAsciiTest() {
2524 // We can put the parser in a mode where it will accept JSON that looks more
2525 // like Protobuf ASCII, for users that have data in that format.
2526 // This uses no "" for field names (which we already support by default,
2527 // omits `,`, `:` before `{` and a couple of other features.
2528 flatbuffers::Parser parser;
2529 parser.opts.protobuf_ascii_alike = true;
2531 parser.Parse("table S { B:int; } table T { A:[int]; C:S; } root_type T;"),
2533 TEST_EQ(parser.Parse("{ A [1 2] C { B:2 }}"), true);
2534 // Similarly, in text output, it should omit these.
2536 auto ok = flatbuffers::GenerateText(
2537 parser, parser.builder_.GetBufferPointer(), &text);
2539 TEST_EQ_STR(text.c_str(),
2540 "{\n A [\n 1\n 2\n ]\n C {\n B: 2\n }\n}\n");
2543 void FlexBuffersTest() {
2544 flexbuffers::Builder slb(512,
2545 flexbuffers::BUILDER_FLAG_SHARE_KEYS_AND_STRINGS);
2547 // Write the equivalent of:
2548 // { vec: [ -100, "Fred", 4.0, false ], bar: [ 1, 2, 3 ], bar3: [ 1, 2, 3 ],
2549 // foo: 100, bool: true, mymap: { foo: "Fred" } }
2551 #ifndef FLATBUFFERS_CPP98_STL
2552 // It's possible to do this without std::function support as well.
2554 slb.Vector("vec", [&]() {
2555 slb += -100; // Equivalent to slb.Add(-100) or slb.Int(-100);
2557 slb.IndirectFloat(4.0f);
2558 auto i_f = slb.LastValue();
2559 uint8_t blob[] = { 77 };
2562 slb.ReuseValue(i_f);
2564 int ints[] = { 1, 2, 3 };
2565 slb.Vector("bar", ints, 3);
2566 slb.FixedTypedVector("bar3", ints, 3);
2567 bool bools[] = {true, false, true, false};
2568 slb.Vector("bools", bools, 4);
2569 slb.Bool("bool", true);
2570 slb.Double("foo", 100);
2571 slb.Map("mymap", [&]() {
2572 slb.String("foo", "Fred"); // Testing key and string reuse.
2577 // It's possible to do this without std::function support as well.
2578 slb.Map([](flexbuffers::Builder& slb2) {
2579 slb2.Vector("vec", [](flexbuffers::Builder& slb3) {
2580 slb3 += -100; // Equivalent to slb.Add(-100) or slb.Int(-100);
2582 slb3.IndirectFloat(4.0f);
2583 auto i_f = slb3.LastValue();
2584 uint8_t blob[] = { 77 };
2587 slb3.ReuseValue(i_f);
2589 int ints[] = { 1, 2, 3 };
2590 slb2.Vector("bar", ints, 3);
2591 slb2.FixedTypedVector("bar3", ints, 3);
2592 slb2.Bool("bool", true);
2593 slb2.Double("foo", 100);
2594 slb2.Map("mymap", [](flexbuffers::Builder& slb3) {
2595 slb3.String("foo", "Fred"); // Testing key and string reuse.
2599 #endif // FLATBUFFERS_CPP98_STL
2601 #ifdef FLATBUFFERS_TEST_VERBOSE
2602 for (size_t i = 0; i < slb.GetBuffer().size(); i++)
2603 printf("%d ", flatbuffers::vector_data(slb.GetBuffer())[i]);
2608 auto map = flexbuffers::GetRoot(slb.GetBuffer()).AsMap();
2609 TEST_EQ(map.size(), 7);
2610 auto vec = map["vec"].AsVector();
2611 TEST_EQ(vec.size(), 6);
2612 TEST_EQ(vec[0].AsInt64(), -100);
2613 TEST_EQ_STR(vec[1].AsString().c_str(), "Fred");
2614 TEST_EQ(vec[1].AsInt64(), 0); // Number parsing failed.
2615 TEST_EQ(vec[2].AsDouble(), 4.0);
2616 TEST_EQ(vec[2].AsString().IsTheEmptyString(), true); // Wrong Type.
2617 TEST_EQ_STR(vec[2].AsString().c_str(), ""); // This still works though.
2618 TEST_EQ_STR(vec[2].ToString().c_str(), "4.0"); // Or have it converted.
2619 // Few tests for templated version of As.
2620 TEST_EQ(vec[0].As<int64_t>(), -100);
2621 TEST_EQ_STR(vec[1].As<std::string>().c_str(), "Fred");
2622 TEST_EQ(vec[1].As<int64_t>(), 0); // Number parsing failed.
2623 TEST_EQ(vec[2].As<double>(), 4.0);
2624 // Test that the blob can be accessed.
2625 TEST_EQ(vec[3].IsBlob(), true);
2626 auto blob = vec[3].AsBlob();
2627 TEST_EQ(blob.size(), 1);
2628 TEST_EQ(blob.data()[0], 77);
2629 TEST_EQ(vec[4].IsBool(), true); // Check if type is a bool
2630 TEST_EQ(vec[4].AsBool(), false); // Check if value is false
2631 TEST_EQ(vec[5].AsDouble(), 4.0); // This is shared with vec[2] !
2632 auto tvec = map["bar"].AsTypedVector();
2633 TEST_EQ(tvec.size(), 3);
2634 TEST_EQ(tvec[2].AsInt8(), 3);
2635 auto tvec3 = map["bar3"].AsFixedTypedVector();
2636 TEST_EQ(tvec3.size(), 3);
2637 TEST_EQ(tvec3[2].AsInt8(), 3);
2638 TEST_EQ(map["bool"].AsBool(), true);
2639 auto tvecb = map["bools"].AsTypedVector();
2640 TEST_EQ(tvecb.ElementType(), flexbuffers::FBT_BOOL);
2641 TEST_EQ(map["foo"].AsUInt8(), 100);
2642 TEST_EQ(map["unknown"].IsNull(), true);
2643 auto mymap = map["mymap"].AsMap();
2644 // These should be equal by pointer equality, since key and value are shared.
2645 TEST_EQ(mymap.Keys()[0].AsKey(), map.Keys()[4].AsKey());
2646 TEST_EQ(mymap.Values()[0].AsString().c_str(), vec[1].AsString().c_str());
2647 // We can mutate values in the buffer.
2648 TEST_EQ(vec[0].MutateInt(-99), true);
2649 TEST_EQ(vec[0].AsInt64(), -99);
2650 TEST_EQ(vec[1].MutateString("John"), true); // Size must match.
2651 TEST_EQ_STR(vec[1].AsString().c_str(), "John");
2652 TEST_EQ(vec[1].MutateString("Alfred"), false); // Too long.
2653 TEST_EQ(vec[2].MutateFloat(2.0f), true);
2654 TEST_EQ(vec[2].AsFloat(), 2.0f);
2655 TEST_EQ(vec[2].MutateFloat(3.14159), false); // Double does not fit in float.
2656 TEST_EQ(vec[4].AsBool(), false); // Is false before change
2657 TEST_EQ(vec[4].MutateBool(true), true); // Can change a bool
2658 TEST_EQ(vec[4].AsBool(), true); // Changed bool is now true
2661 flatbuffers::Parser parser;
2663 auto jsontest = "{ a: [ 123, 456.0 ], b: \"hello\", c: true, d: false }";
2664 TEST_EQ(parser.ParseFlexBuffer(jsontest, nullptr, &slb), true);
2665 auto jroot = flexbuffers::GetRoot(slb.GetBuffer());
2666 auto jmap = jroot.AsMap();
2667 auto jvec = jmap["a"].AsVector();
2668 TEST_EQ(jvec[0].AsInt64(), 123);
2669 TEST_EQ(jvec[1].AsDouble(), 456.0);
2670 TEST_EQ_STR(jmap["b"].AsString().c_str(), "hello");
2671 TEST_EQ(jmap["c"].IsBool(), true); // Parsed correctly to a bool
2672 TEST_EQ(jmap["c"].AsBool(), true); // Parsed correctly to true
2673 TEST_EQ(jmap["d"].IsBool(), true); // Parsed correctly to a bool
2674 TEST_EQ(jmap["d"].AsBool(), false); // Parsed correctly to false
2675 // And from FlexBuffer back to JSON:
2676 auto jsonback = jroot.ToString();
2677 TEST_EQ_STR(jsontest, jsonback.c_str());
2680 void TypeAliasesTest() {
2681 flatbuffers::FlatBufferBuilder builder;
2683 builder.Finish(CreateTypeAliases(
2684 builder, flatbuffers::numeric_limits<int8_t>::min(),
2685 flatbuffers::numeric_limits<uint8_t>::max(),
2686 flatbuffers::numeric_limits<int16_t>::min(),
2687 flatbuffers::numeric_limits<uint16_t>::max(),
2688 flatbuffers::numeric_limits<int32_t>::min(),
2689 flatbuffers::numeric_limits<uint32_t>::max(),
2690 flatbuffers::numeric_limits<int64_t>::min(),
2691 flatbuffers::numeric_limits<uint64_t>::max(), 2.3f, 2.3));
2693 auto p = builder.GetBufferPointer();
2694 auto ta = flatbuffers::GetRoot<TypeAliases>(p);
2696 TEST_EQ(ta->i8(), flatbuffers::numeric_limits<int8_t>::min());
2697 TEST_EQ(ta->u8(), flatbuffers::numeric_limits<uint8_t>::max());
2698 TEST_EQ(ta->i16(), flatbuffers::numeric_limits<int16_t>::min());
2699 TEST_EQ(ta->u16(), flatbuffers::numeric_limits<uint16_t>::max());
2700 TEST_EQ(ta->i32(), flatbuffers::numeric_limits<int32_t>::min());
2701 TEST_EQ(ta->u32(), flatbuffers::numeric_limits<uint32_t>::max());
2702 TEST_EQ(ta->i64(), flatbuffers::numeric_limits<int64_t>::min());
2703 TEST_EQ(ta->u64(), flatbuffers::numeric_limits<uint64_t>::max());
2704 TEST_EQ(ta->f32(), 2.3f);
2705 TEST_EQ(ta->f64(), 2.3);
2706 using namespace flatbuffers; // is_same
2707 static_assert(is_same<decltype(ta->i8()), int8_t>::value, "invalid type");
2708 static_assert(is_same<decltype(ta->i16()), int16_t>::value, "invalid type");
2709 static_assert(is_same<decltype(ta->i32()), int32_t>::value, "invalid type");
2710 static_assert(is_same<decltype(ta->i64()), int64_t>::value, "invalid type");
2711 static_assert(is_same<decltype(ta->u8()), uint8_t>::value, "invalid type");
2712 static_assert(is_same<decltype(ta->u16()), uint16_t>::value, "invalid type");
2713 static_assert(is_same<decltype(ta->u32()), uint32_t>::value, "invalid type");
2714 static_assert(is_same<decltype(ta->u64()), uint64_t>::value, "invalid type");
2715 static_assert(is_same<decltype(ta->f32()), float>::value, "invalid type");
2716 static_assert(is_same<decltype(ta->f64()), double>::value, "invalid type");
2719 void EndianSwapTest() {
2720 TEST_EQ(flatbuffers::EndianSwap(static_cast<int16_t>(0x1234)), 0x3412);
2721 TEST_EQ(flatbuffers::EndianSwap(static_cast<int32_t>(0x12345678)),
2723 TEST_EQ(flatbuffers::EndianSwap(static_cast<int64_t>(0x1234567890ABCDEF)),
2724 0xEFCDAB9078563412);
2725 TEST_EQ(flatbuffers::EndianSwap(flatbuffers::EndianSwap(3.14f)), 3.14f);
2728 void UninitializedVectorTest() {
2729 flatbuffers::FlatBufferBuilder builder;
2731 Test *buf = nullptr;
2732 auto vector_offset = builder.CreateUninitializedVectorOfStructs<Test>(2, &buf);
2734 buf[0] = Test(10, 20);
2735 buf[1] = Test(30, 40);
2737 auto required_name = builder.CreateString("myMonster");
2738 auto monster_builder = MonsterBuilder(builder);
2739 monster_builder.add_name(required_name); // required field mandated for monster.
2740 monster_builder.add_test4(vector_offset);
2741 builder.Finish(monster_builder.Finish());
2743 auto p = builder.GetBufferPointer();
2744 auto uvt = flatbuffers::GetRoot<Monster>(p);
2746 auto vec = uvt->test4();
2748 auto test_0 = vec->Get(0);
2749 auto test_1 = vec->Get(1);
2750 TEST_EQ(test_0->a(), 10);
2751 TEST_EQ(test_0->b(), 20);
2752 TEST_EQ(test_1->a(), 30);
2753 TEST_EQ(test_1->b(), 40);
2756 void EqualOperatorTest() {
2759 TEST_EQ(b == a, true);
2760 TEST_EQ(b != a, false);
2763 TEST_EQ(b == a, false);
2764 TEST_EQ(b != a, true);
2766 TEST_EQ(b == a, true);
2767 TEST_EQ(b != a, false);
2769 b.inventory.push_back(3);
2770 TEST_EQ(b == a, false);
2771 TEST_EQ(b != a, true);
2772 b.inventory.clear();
2773 TEST_EQ(b == a, true);
2774 TEST_EQ(b != a, false);
2776 b.test.type = Any_Monster;
2777 TEST_EQ(b == a, false);
2778 TEST_EQ(b != a, true);
2781 // For testing any binaries, e.g. from fuzzing.
2782 void LoadVerifyBinaryTest() {
2784 if (flatbuffers::LoadFile((test_data_path +
2785 "fuzzer/your-filename-here").c_str(),
2787 flatbuffers::Verifier verifier(
2788 reinterpret_cast<const uint8_t *>(binary.data()), binary.size());
2789 TEST_EQ(VerifyMonsterBuffer(verifier), true);
2793 void CreateSharedStringTest() {
2794 flatbuffers::FlatBufferBuilder builder;
2795 const auto one1 = builder.CreateSharedString("one");
2796 const auto two = builder.CreateSharedString("two");
2797 const auto one2 = builder.CreateSharedString("one");
2798 TEST_EQ(one1.o, one2.o);
2799 const auto onetwo = builder.CreateSharedString("onetwo");
2800 TEST_EQ(onetwo.o != one1.o, true);
2801 TEST_EQ(onetwo.o != two.o, true);
2803 // Support for embedded nulls
2804 const char chars_b[] = {'a', '\0', 'b'};
2805 const char chars_c[] = {'a', '\0', 'c'};
2806 const auto null_b1 = builder.CreateSharedString(chars_b, sizeof(chars_b));
2807 const auto null_c = builder.CreateSharedString(chars_c, sizeof(chars_c));
2808 const auto null_b2 = builder.CreateSharedString(chars_b, sizeof(chars_b));
2809 TEST_EQ(null_b1.o != null_c.o, true); // Issue#5058 repro
2810 TEST_EQ(null_b1.o, null_b2.o);
2812 // Put the strings into an array for round trip verification.
2813 const flatbuffers::Offset<flatbuffers::String> array[7] = { one1, two, one2, onetwo, null_b1, null_c, null_b2 };
2814 const auto vector_offset = builder.CreateVector(array, flatbuffers::uoffset_t(7));
2815 MonsterBuilder monster_builder(builder);
2816 monster_builder.add_name(two);
2817 monster_builder.add_testarrayofstring(vector_offset);
2818 builder.Finish(monster_builder.Finish());
2820 // Read the Monster back.
2821 const auto *monster = flatbuffers::GetRoot<Monster>(builder.GetBufferPointer());
2822 TEST_EQ_STR(monster->name()->c_str(), "two");
2823 const auto *testarrayofstring = monster->testarrayofstring();
2824 TEST_EQ(testarrayofstring->size(), flatbuffers::uoffset_t(7));
2825 const auto &a = *testarrayofstring;
2826 TEST_EQ_STR(a[0]->c_str(), "one");
2827 TEST_EQ_STR(a[1]->c_str(), "two");
2828 TEST_EQ_STR(a[2]->c_str(), "one");
2829 TEST_EQ_STR(a[3]->c_str(), "onetwo");
2830 TEST_EQ(a[4]->str(), (std::string(chars_b, sizeof(chars_b))));
2831 TEST_EQ(a[5]->str(), (std::string(chars_c, sizeof(chars_c))));
2832 TEST_EQ(a[6]->str(), (std::string(chars_b, sizeof(chars_b))));
2834 // Make sure String::operator< works, too, since it is related to StringOffsetCompare.
2835 TEST_EQ((*a[0]) < (*a[1]), true);
2836 TEST_EQ((*a[1]) < (*a[0]), false);
2837 TEST_EQ((*a[1]) < (*a[2]), false);
2838 TEST_EQ((*a[2]) < (*a[1]), true);
2839 TEST_EQ((*a[4]) < (*a[3]), true);
2840 TEST_EQ((*a[5]) < (*a[4]), false);
2841 TEST_EQ((*a[5]) < (*a[4]), false);
2842 TEST_EQ((*a[6]) < (*a[5]), true);
2845 void FixedLengthArrayTest() {
2846 // VS10 does not support typed enums, exclude from tests
2847 #if !defined(_MSC_VER) || _MSC_VER >= 1700
2848 // Generate an ArrayTable containing one ArrayStruct.
2849 flatbuffers::FlatBufferBuilder fbb;
2850 MyGame::Example::NestedStruct nStruct0(MyGame::Example::TestEnum::B);
2851 TEST_NOTNULL(nStruct0.mutable_a());
2852 nStruct0.mutable_a()->Mutate(0, 1);
2853 nStruct0.mutable_a()->Mutate(1, 2);
2854 TEST_NOTNULL(nStruct0.mutable_c());
2855 nStruct0.mutable_c()->Mutate(0, MyGame::Example::TestEnum::C);
2856 nStruct0.mutable_c()->Mutate(1, MyGame::Example::TestEnum::A);
2857 TEST_NOTNULL(nStruct0.mutable_d());
2858 nStruct0.mutable_d()->Mutate(0, flatbuffers::numeric_limits<int64_t>::max());
2859 nStruct0.mutable_d()->Mutate(1, flatbuffers::numeric_limits<int64_t>::min());
2860 MyGame::Example::NestedStruct nStruct1(MyGame::Example::TestEnum::C);
2861 TEST_NOTNULL(nStruct1.mutable_a());
2862 nStruct1.mutable_a()->Mutate(0, 3);
2863 nStruct1.mutable_a()->Mutate(1, 4);
2864 TEST_NOTNULL(nStruct1.mutable_c());
2865 nStruct1.mutable_c()->Mutate(0, MyGame::Example::TestEnum::C);
2866 nStruct1.mutable_c()->Mutate(1, MyGame::Example::TestEnum::A);
2867 TEST_NOTNULL(nStruct1.mutable_d());
2868 nStruct1.mutable_d()->Mutate(0, flatbuffers::numeric_limits<int64_t>::min());
2869 nStruct1.mutable_d()->Mutate(1, flatbuffers::numeric_limits<int64_t>::max());
2870 MyGame::Example::ArrayStruct aStruct(2, 12, 1);
2871 TEST_NOTNULL(aStruct.b());
2872 TEST_NOTNULL(aStruct.mutable_b());
2873 TEST_NOTNULL(aStruct.mutable_d());
2874 TEST_NOTNULL(aStruct.mutable_f());
2875 for (int i = 0; i < aStruct.b()->size(); i++)
2876 aStruct.mutable_b()->Mutate(i, i + 1);
2877 aStruct.mutable_d()->Mutate(0, nStruct0);
2878 aStruct.mutable_d()->Mutate(1, nStruct1);
2879 auto aTable = MyGame::Example::CreateArrayTable(fbb, &aStruct);
2882 // Verify correctness of the ArrayTable.
2883 flatbuffers::Verifier verifier(fbb.GetBufferPointer(), fbb.GetSize());
2884 MyGame::Example::VerifyArrayTableBuffer(verifier);
2885 auto p = MyGame::Example::GetMutableArrayTable(fbb.GetBufferPointer());
2886 auto mArStruct = p->mutable_a();
2887 TEST_NOTNULL(mArStruct);
2888 TEST_NOTNULL(mArStruct->b());
2889 TEST_NOTNULL(mArStruct->d());
2890 TEST_NOTNULL(mArStruct->f());
2891 TEST_NOTNULL(mArStruct->mutable_b());
2892 TEST_NOTNULL(mArStruct->mutable_d());
2893 TEST_NOTNULL(mArStruct->mutable_f());
2894 mArStruct->mutable_b()->Mutate(14, -14);
2895 TEST_EQ(mArStruct->a(), 2);
2896 TEST_EQ(mArStruct->b()->size(), 15);
2897 TEST_EQ(mArStruct->b()->Get(aStruct.b()->size() - 1), -14);
2898 TEST_EQ(mArStruct->c(), 12);
2899 TEST_NOTNULL(mArStruct->d()->Get(0));
2900 TEST_NOTNULL(mArStruct->d()->Get(0)->a());
2901 TEST_EQ(mArStruct->d()->Get(0)->a()->Get(0), 1);
2902 TEST_EQ(mArStruct->d()->Get(0)->a()->Get(1), 2);
2903 TEST_NOTNULL(mArStruct->d()->Get(1));
2904 TEST_NOTNULL(mArStruct->d()->Get(1)->a());
2905 TEST_EQ(mArStruct->d()->Get(1)->a()->Get(0), 3);
2906 TEST_EQ(mArStruct->d()->Get(1)->a()->Get(1), 4);
2907 TEST_NOTNULL(mArStruct->mutable_d()->GetMutablePointer(1));
2908 TEST_NOTNULL(mArStruct->mutable_d()->GetMutablePointer(1)->mutable_a());
2909 mArStruct->mutable_d()->GetMutablePointer(1)->mutable_a()->Mutate(1, 5);
2910 TEST_EQ(mArStruct->d()->Get(1)->a()->Get(1), 5);
2911 TEST_EQ(mArStruct->d()->Get(0)->b() == MyGame::Example::TestEnum::B, true);
2912 TEST_NOTNULL(mArStruct->d()->Get(0)->c());
2913 TEST_EQ(mArStruct->d()->Get(0)->c()->Get(0) == MyGame::Example::TestEnum::C,
2915 TEST_EQ(mArStruct->d()->Get(0)->c()->Get(1) == MyGame::Example::TestEnum::A,
2917 TEST_EQ(mArStruct->d()->Get(0)->d()->Get(0),
2918 flatbuffers::numeric_limits<int64_t>::max());
2919 TEST_EQ(mArStruct->d()->Get(0)->d()->Get(1),
2920 flatbuffers::numeric_limits<int64_t>::min());
2921 TEST_EQ(mArStruct->d()->Get(1)->b() == MyGame::Example::TestEnum::C, true);
2922 TEST_NOTNULL(mArStruct->d()->Get(1)->c());
2923 TEST_EQ(mArStruct->d()->Get(1)->c()->Get(0) == MyGame::Example::TestEnum::C,
2925 TEST_EQ(mArStruct->d()->Get(1)->c()->Get(1) == MyGame::Example::TestEnum::A,
2927 TEST_EQ(mArStruct->d()->Get(1)->d()->Get(0),
2928 flatbuffers::numeric_limits<int64_t>::min());
2929 TEST_EQ(mArStruct->d()->Get(1)->d()->Get(1),
2930 flatbuffers::numeric_limits<int64_t>::max());
2931 for (int i = 0; i < mArStruct->b()->size() - 1; i++)
2932 TEST_EQ(mArStruct->b()->Get(i), i + 1);
2934 TEST_EQ(reinterpret_cast<uintptr_t>(mArStruct->d()) % 8, 0);
2935 TEST_EQ(reinterpret_cast<uintptr_t>(mArStruct->f()) % 8, 0);
2939 void NativeTypeTest() {
2942 Geometry::ApplicationDataT src_data;
2943 src_data.vectors.reserve(N);
2945 for (int i = 0; i < N; ++i) {
2946 src_data.vectors.push_back (Native::Vector3D(10 * i + 0.1f, 10 * i + 0.2f, 10 * i + 0.3f));
2949 flatbuffers::FlatBufferBuilder fbb;
2950 fbb.Finish(Geometry::ApplicationData::Pack(fbb, &src_data));
2952 auto dstDataT = Geometry::UnPackApplicationData(fbb.GetBufferPointer());
2954 for (int i = 0; i < N; ++i) {
2955 Native::Vector3D& v = dstDataT->vectors[i];
2956 TEST_EQ(v.x, 10 * i + 0.1f);
2957 TEST_EQ(v.y, 10 * i + 0.2f);
2958 TEST_EQ(v.z, 10 * i + 0.3f);
2962 void FixedLengthArrayJsonTest(bool binary) {
2963 // VS10 does not support typed enums, exclude from tests
2964 #if !defined(_MSC_VER) || _MSC_VER >= 1700
2965 // load FlatBuffer schema (.fbs) and JSON from disk
2966 std::string schemafile;
2967 std::string jsonfile;
2969 flatbuffers::LoadFile(
2970 (test_data_path + "arrays_test." + (binary ? "bfbs" : "fbs")).c_str(),
2971 binary, &schemafile),
2973 TEST_EQ(flatbuffers::LoadFile((test_data_path + "arrays_test.golden").c_str(),
2977 // parse schema first, so we can use it to parse the data after
2978 flatbuffers::Parser parserOrg, parserGen;
2980 flatbuffers::Verifier verifier(
2981 reinterpret_cast<const uint8_t *>(schemafile.c_str()),
2983 TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
2984 TEST_EQ(parserOrg.Deserialize((const uint8_t *)schemafile.c_str(),
2987 TEST_EQ(parserGen.Deserialize((const uint8_t *)schemafile.c_str(),
2991 TEST_EQ(parserOrg.Parse(schemafile.c_str()), true);
2992 TEST_EQ(parserGen.Parse(schemafile.c_str()), true);
2994 TEST_EQ(parserOrg.Parse(jsonfile.c_str()), true);
2996 // First, verify it, just in case:
2997 flatbuffers::Verifier verifierOrg(parserOrg.builder_.GetBufferPointer(),
2998 parserOrg.builder_.GetSize());
2999 TEST_EQ(VerifyArrayTableBuffer(verifierOrg), true);
3002 std::string jsonGen;
3004 GenerateText(parserOrg, parserOrg.builder_.GetBufferPointer(), &jsonGen),
3008 TEST_EQ(parserGen.Parse(jsonGen.c_str()), true);
3010 // Verify buffer from generated JSON
3011 flatbuffers::Verifier verifierGen(parserGen.builder_.GetBufferPointer(),
3012 parserGen.builder_.GetSize());
3013 TEST_EQ(VerifyArrayTableBuffer(verifierGen), true);
3015 // Compare generated buffer to original
3016 TEST_EQ(parserOrg.builder_.GetSize(), parserGen.builder_.GetSize());
3017 TEST_EQ(std::memcmp(parserOrg.builder_.GetBufferPointer(),
3018 parserGen.builder_.GetBufferPointer(),
3019 parserOrg.builder_.GetSize()),
3026 int FlatBufferTests() {
3029 // Run our various test suites:
3032 auto flatbuf1 = CreateFlatBufferTest(rawbuf);
3033 #if !defined(FLATBUFFERS_CPP98_STL)
3034 auto flatbuf = std::move(flatbuf1); // Test move assignment.
3036 auto &flatbuf = flatbuf1;
3037 #endif // !defined(FLATBUFFERS_CPP98_STL)
3039 TriviallyCopyableTest();
3041 AccessFlatBufferTest(reinterpret_cast<const uint8_t *>(rawbuf.c_str()),
3043 AccessFlatBufferTest(flatbuf.data(), flatbuf.size());
3045 MutateFlatBuffersTest(flatbuf.data(), flatbuf.size());
3047 ObjectFlatBuffersTest(flatbuf.data());
3049 MiniReflectFlatBuffersTest(flatbuf.data());
3053 #ifndef FLATBUFFERS_NO_FILE_TESTS
3054 #ifdef FLATBUFFERS_TEST_PATH_PREFIX
3055 test_data_path = FLATBUFFERS_STRING(FLATBUFFERS_TEST_PATH_PREFIX) +
3058 ParseAndGenerateTextTest(false);
3059 ParseAndGenerateTextTest(true);
3060 FixedLengthArrayJsonTest(false);
3061 FixedLengthArrayJsonTest(true);
3062 ReflectionTest(flatbuf.data(), flatbuf.size());
3065 LoadVerifyBinaryTest();
3066 GenerateTableTextTest();
3078 EnumOutOfRangeTest();
3079 IntegerOutOfRangeTest();
3080 IntegerBoundaryTest();
3082 UnicodeTestAllowNonUTF8();
3083 UnicodeTestGenerateTextFailsOnNonUTF8();
3084 UnicodeSurrogatesTest();
3085 UnicodeInvalidSurrogatesTest();
3087 UnknownFieldsTest();
3089 InvalidNestedFlatbufferTest();
3091 ParseProtoBufAsciiTest();
3094 CreateSharedStringTest();
3098 UninitializedVectorTest();
3099 EqualOperatorTest();
3104 TestMonsterExtraFloats();
3105 FixedLengthArrayTest();
3110 int main(int /*argc*/, const char * /*argv*/ []) {
3113 std::string req_locale;
3114 if (flatbuffers::ReadEnvironmentVariable("FLATBUFFERS_TEST_LOCALE",
3116 TEST_OUTPUT_LINE("The environment variable FLATBUFFERS_TEST_LOCALE=%s",
3117 req_locale.c_str());
3118 req_locale = flatbuffers::RemoveStringQuotes(req_locale);
3119 std::string the_locale;
3121 flatbuffers::SetGlobalTestLocale(req_locale.c_str(), &the_locale));
3122 TEST_OUTPUT_LINE("The global C-locale changed: %s", the_locale.c_str());
3126 FlatBufferBuilderTest();
3128 if (!testing_fails) {
3129 TEST_OUTPUT_LINE("ALL TESTS PASSED");
3131 TEST_OUTPUT_LINE("%d FAILED TESTS", testing_fails);
3133 return CloseTestEngine();