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