#include "namespace_test/namespace_test1_generated.h"
#include "namespace_test/namespace_test2_generated.h"
#include "union_vector/union_vector_generated.h"
-#include "monster_extra_generated.h"
#include "optional_scalars_generated.h"
#if !defined(_MSC_VER) || _MSC_VER >= 1700
+# include "monster_extra_generated.h"
# include "arrays_test_generated.h"
# include "evolution_test/evolution_v1_generated.h"
# include "evolution_test/evolution_v2_generated.h"
// clang-format on
// Shortcuts for the infinity.
-static const auto infinityf = std::numeric_limits<float>::infinity();
-static const auto infinityd = std::numeric_limits<double>::infinity();
+static const auto infinity_f = std::numeric_limits<float>::infinity();
+static const auto infinity_d = std::numeric_limits<double>::infinity();
using namespace MyGame::Example;
abilities.push_back(Ability(4, 40));
abilities.push_back(Ability(3, 30));
abilities.push_back(Ability(2, 20));
- abilities.push_back(Ability(1, 10));
+ abilities.push_back(Ability(0, 0));
auto vecofstructs = builder.CreateVectorOfSortedStructs(&abilities);
+ flatbuffers::Offset<Stat> mlocs_stats[1];
+ auto miss = builder.CreateString("miss");
+ StatBuilder mb_miss(builder);
+ mb_miss.add_id(miss);
+ mb_miss.add_val(0);
+ mb_miss.add_count(0); // key
+ mlocs_stats[0] = mb_miss.Finish();
+ auto vec_of_stats = builder.CreateVectorOfSortedTables(mlocs_stats, 1);
+
// Create a nested FlatBuffer.
// Nested FlatBuffers are stored in a ubyte vector, which can be convenient
// since they can be memcpy'd around much easier than other FlatBuffer
flexbuild.Int(1234);
flexbuild.Finish();
auto flex = builder.CreateVector(flexbuild.GetBuffer());
-
// Test vector of enums.
Color colors[] = { Color_Blue, Color_Green };
// We use this special creation function because we have an array of
testv, vecofstrings, vecoftables, 0, nested_flatbuffer_vector, 0, false,
0, 0, 0, 0, 0, 0, 0, 0, 0, 3.14159f, 3.0f, 0.0f, vecofstrings2,
vecofstructs, flex, testv2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- AnyUniqueAliases_NONE, 0, AnyAmbiguousAliases_NONE, 0, vecofcolors);
+ AnyUniqueAliases_NONE, 0, AnyAmbiguousAliases_NONE, 0, vecofcolors,
+ MyGame::Example::Race_None, 0, vec_of_stats);
FinishMonsterBuffer(builder, mloc);
auto right = vecofstructs->Get(i + 1);
TEST_EQ(true, (left->KeyCompareLessThan(right)));
}
+ TEST_NOTNULL(vecofstructs->LookupByKey(0)); // test default value
TEST_NOTNULL(vecofstructs->LookupByKey(3));
TEST_EQ(static_cast<const Ability *>(nullptr),
vecofstructs->LookupByKey(5));
}
+ if (auto vec_of_stat = monster->scalar_key_sorted_tables()) {
+ auto stat_0 = vec_of_stat->LookupByKey(static_cast<uint16_t>(0u));
+ TEST_NOTNULL(stat_0);
+ TEST_NOTNULL(stat_0->id());
+ TEST_EQ(0, stat_0->count());
+ TEST_EQ_STR("miss", stat_0->id()->c_str());
+ }
+
// Test nested FlatBuffers if available:
auto nested_buffer = monster->testnestedflatbuffer();
if (nested_buffer) {
std::memcpy(&b, &v, sizeof(T));
return ((b & qnan_base) == qnan_base);
}
-#if defined(__mips__) || defined(__hppa__)
+# if defined(__mips__) || defined(__hppa__)
static bool is_quiet_nan(float v) {
return is_quiet_nan_impl<float, uint32_t, 0x7FC00000u>(v) ||
is_quiet_nan_impl<float, uint32_t, 0x7FBFFFFFu>(v);
return is_quiet_nan_impl<double, uint64_t, 0x7FF8000000000000ul>(v) ||
is_quiet_nan_impl<double, uint64_t, 0x7FF7FFFFFFFFFFFFu>(v);
}
-#else
+# else
static bool is_quiet_nan(float v) {
return is_quiet_nan_impl<float, uint32_t, 0x7FC00000u>(v);
}
static bool is_quiet_nan(double v) {
return is_quiet_nan_impl<double, uint64_t, 0x7FF8000000000000ul>(v);
}
-#endif
+# endif
void TestMonsterExtraFloats() {
TEST_EQ(is_quiet_nan(1.0), false);
- TEST_EQ(is_quiet_nan(infinityd), false);
- TEST_EQ(is_quiet_nan(-infinityf), false);
+ TEST_EQ(is_quiet_nan(infinity_d), false);
+ TEST_EQ(is_quiet_nan(-infinity_f), false);
TEST_EQ(is_quiet_nan(std::numeric_limits<float>::quiet_NaN()), true);
TEST_EQ(is_quiet_nan(std::numeric_limits<double>::quiet_NaN()), true);
TEST_NOTNULL(def_extra);
TEST_EQ(is_quiet_nan(def_extra->f0()), true);
TEST_EQ(is_quiet_nan(def_extra->f1()), true);
- TEST_EQ(def_extra->f2(), +infinityf);
- TEST_EQ(def_extra->f3(), -infinityf);
+ TEST_EQ(def_extra->f2(), +infinity_f);
+ TEST_EQ(def_extra->f3(), -infinity_f);
TEST_EQ(is_quiet_nan(def_extra->d0()), true);
TEST_EQ(is_quiet_nan(def_extra->d1()), true);
- TEST_EQ(def_extra->d2(), +infinityd);
- TEST_EQ(def_extra->d3(), -infinityd);
+ TEST_EQ(def_extra->d2(), +infinity_d);
+ TEST_EQ(def_extra->d3(), -infinity_d);
std::string jsongen;
auto result = GenerateText(parser, def_obj, &jsongen);
TEST_EQ(result, true);
TEST_NOTNULL(extra);
TEST_EQ(is_quiet_nan(extra->f0()), true);
TEST_EQ(is_quiet_nan(extra->f1()), true);
- TEST_EQ(extra->f2(), +infinityf);
- TEST_EQ(extra->f3(), -infinityf);
+ TEST_EQ(extra->f2(), +infinity_f);
+ TEST_EQ(extra->f3(), -infinity_f);
TEST_EQ(is_quiet_nan(extra->d0()), true);
- TEST_EQ(extra->d1(), +infinityd);
- TEST_EQ(extra->d2(), -infinityd);
+ TEST_EQ(extra->d1(), +infinity_d);
+ TEST_EQ(extra->d2(), -infinity_d);
TEST_EQ(is_quiet_nan(extra->d3()), true);
TEST_NOTNULL(extra->fvec());
TEST_EQ(extra->fvec()->size(), 4);
TEST_EQ(extra->fvec()->Get(0), 1.0f);
- TEST_EQ(extra->fvec()->Get(1), -infinityf);
- TEST_EQ(extra->fvec()->Get(2), +infinityf);
+ TEST_EQ(extra->fvec()->Get(1), -infinity_f);
+ TEST_EQ(extra->fvec()->Get(2), +infinity_f);
TEST_EQ(is_quiet_nan(extra->fvec()->Get(3)), true);
TEST_NOTNULL(extra->dvec());
TEST_EQ(extra->dvec()->size(), 4);
TEST_EQ(extra->dvec()->Get(0), 2.0);
- TEST_EQ(extra->dvec()->Get(1), +infinityd);
- TEST_EQ(extra->dvec()->Get(2), -infinityd);
+ TEST_EQ(extra->dvec()->Get(1), +infinity_d);
+ TEST_EQ(extra->dvec()->Get(2), -infinity_d);
TEST_EQ(is_quiet_nan(extra->dvec()->Get(3)), true);
}
#else
} else {
TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
}
- TEST_EQ(parser.Parse(jsonfile.c_str(), include_directories), true);
+ TEST_EQ(parser.ParseJson(jsonfile.c_str()), true);
// here, parser.builder_ contains a binary buffer that is the parsed data.
"4, 0, 6, 0, 8, 0, 12, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 13, 0, 0, 0, 78, "
"101, 115, 116, 101, 100, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0 ], "
"testarrayofstring2: [ \"jane\", \"mary\" ], "
- "testarrayofsortedstruct: [ { id: 1, distance: 10 }, "
+ "testarrayofsortedstruct: [ { id: 0, distance: 0 }, "
"{ id: 2, distance: 20 }, { id: 3, distance: 30 }, "
"{ id: 4, distance: 40 } ], "
"flex: [ 210, 4, 5, 2 ], "
"test5: [ { a: 10, b: 20 }, { a: 30, b: 40 } ], "
- "vector_of_enums: [ Blue, Green ] "
+ "vector_of_enums: [ Blue, Green ], "
+ "scalar_key_sorted_tables: [ { id: \"miss\" } ] "
"}");
Test test(16, 32);
"16, b: 32 } }");
}
+void MiniReflectFixedLengthArrayTest() {
+ // VS10 does not support typed enums, exclude from tests
+#if !defined(_MSC_VER) || _MSC_VER >= 1700
+ flatbuffers::FlatBufferBuilder fbb;
+ MyGame::Example::ArrayStruct aStruct(2, 12, 1);
+ auto aTable = MyGame::Example::CreateArrayTable(fbb, &aStruct);
+ fbb.Finish(aTable);
+
+ auto flatbuf = fbb.Release();
+ auto s = flatbuffers::FlatBufferToString(
+ flatbuf.data(), MyGame::Example::ArrayTableTypeTable());
+ TEST_EQ_STR(
+ "{ "
+ "a: { a: 2.0, "
+ "b: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], "
+ "c: 12, "
+ "d: [ { a: [ 0, 0 ], b: A, c: [ A, A ], d: [ 0, 0 ] }, "
+ "{ a: [ 0, 0 ], b: A, c: [ A, A ], d: [ 0, 0 ] } ], "
+ "e: 1, f: [ 0, 0 ] } "
+ "}",
+ s.c_str());
+#endif
+}
+
// Parse a .proto schema, output as .fbs
void ParseProtoTest() {
// load the .proto and the golden file from disk
TestError("table X { Y:int; Y:int; }", "field already");
TestError("table Y {} table X { Y:int; }", "same as table");
TestError("struct X { Y:string; }", "only scalar");
- TestError("table X { Y:string = \"\"; }", "default values");
TestError("struct X { a:uint = 42; }", "default values");
TestError("enum Y:byte { Z = 1 } table X { y:Y; }", "not part of enum");
TestError("struct X { Y:int (deprecated); }", "deprecate");
"may contain only scalar or struct fields");
// Non-snake case field names
TestError("table X { Y: int; } root_type Y: {Y:1.0}", "snake_case");
+ // Complex defaults
+ TestError("table X { y: string = 1; }", "expecting: string");
+ TestError("table X { y: string = []; }", " Cannot assign token");
+ TestError("table X { y: [int] = [1]; }", "Expected `]`");
+ TestError("table X { y: [int] = [; }", "Expected `]`");
+ TestError("table X { y: [int] = \"\"; }", "type mismatch");
+ // An identifier can't start from sign (+|-)
+ TestError("table X { -Y: int; } root_type Y: {Y:1.0}", "identifier");
+ TestError("table X { +Y: int; } root_type Y: {Y:1.0}", "identifier");
}
template<typename T>
void ValidFloatTest() {
// check rounding to infinity
- TEST_EQ(TestValue<float>("{ y:+3.4029e+38 }", "float"), +infinityf);
- TEST_EQ(TestValue<float>("{ y:-3.4029e+38 }", "float"), -infinityf);
- TEST_EQ(TestValue<double>("{ y:+1.7977e+308 }", "double"), +infinityd);
- TEST_EQ(TestValue<double>("{ y:-1.7977e+308 }", "double"), -infinityd);
+ TEST_EQ(TestValue<float>("{ y:+3.4029e+38 }", "float"), +infinity_f);
+ TEST_EQ(TestValue<float>("{ y:-3.4029e+38 }", "float"), -infinity_f);
+ TEST_EQ(TestValue<double>("{ y:+1.7977e+308 }", "double"), +infinity_d);
+ TEST_EQ(TestValue<double>("{ y:-1.7977e+308 }", "double"), -infinity_d);
TEST_EQ(
FloatCompare(TestValue<float>("{ y:0.0314159e+2 }", "float"), 3.14159f),
TEST_EQ(std::isnan(TestValue<double>("{ y:nan }", "double")), true);
TEST_EQ(std::isnan(TestValue<float>("{ y:nan }", "float")), true);
TEST_EQ(std::isnan(TestValue<float>("{ y:\"nan\" }", "float")), true);
+ TEST_EQ(std::isnan(TestValue<float>("{ y:\"+nan\" }", "float")), true);
+ TEST_EQ(std::isnan(TestValue<float>("{ y:\"-nan\" }", "float")), true);
TEST_EQ(std::isnan(TestValue<float>("{ y:+nan }", "float")), true);
TEST_EQ(std::isnan(TestValue<float>("{ y:-nan }", "float")), true);
TEST_EQ(std::isnan(TestValue<float>(nullptr, "float=nan")), true);
TEST_EQ(std::isnan(TestValue<float>(nullptr, "float=-nan")), true);
// check inf
- TEST_EQ(TestValue<float>("{ y:inf }", "float"), infinityf);
- TEST_EQ(TestValue<float>("{ y:\"inf\" }", "float"), infinityf);
- TEST_EQ(TestValue<float>("{ y:+inf }", "float"), infinityf);
- TEST_EQ(TestValue<float>("{ y:-inf }", "float"), -infinityf);
- TEST_EQ(TestValue<float>(nullptr, "float=inf"), infinityf);
- TEST_EQ(TestValue<float>(nullptr, "float=-inf"), -infinityf);
+ TEST_EQ(TestValue<float>("{ y:inf }", "float"), infinity_f);
+ TEST_EQ(TestValue<float>("{ y:\"inf\" }", "float"), infinity_f);
+ TEST_EQ(TestValue<float>("{ y:\"-inf\" }", "float"), -infinity_f);
+ TEST_EQ(TestValue<float>("{ y:\"+inf\" }", "float"), infinity_f);
+ TEST_EQ(TestValue<float>("{ y:+inf }", "float"), infinity_f);
+ TEST_EQ(TestValue<float>("{ y:-inf }", "float"), -infinity_f);
+ TEST_EQ(TestValue<float>(nullptr, "float=inf"), infinity_f);
+ TEST_EQ(TestValue<float>(nullptr, "float=-inf"), -infinity_f);
TestValue<double>(
"{ y: [0.2, .2, 1.0, -1.0, -2., 2., 1e0, -1e0, 1.0e0, -1.0e0, -3.e2, "
"3.0e2] }",
TEST_EQ(ok, true);
// Test root table
const Monster *monster = GetMonster(parser.builder_.GetBufferPointer());
+ const auto abilities = monster->testarrayofsortedstruct();
+ TEST_EQ(abilities->size(), 3);
+ TEST_EQ(abilities->Get(0)->id(), 0);
+ TEST_EQ(abilities->Get(0)->distance(), 45);
+ TEST_EQ(abilities->Get(1)->id(), 1);
+ TEST_EQ(abilities->Get(1)->distance(), 21);
+ TEST_EQ(abilities->Get(2)->id(), 5);
+ TEST_EQ(abilities->Get(2)->distance(), 12);
+
std::string jsongen;
auto result = GenerateTextFromTable(parser, monster, "MyGame.Example.Monster",
&jsongen);
TEST_EQ((*a[6]) < (*a[5]), true);
}
+#if !defined(FLATBUFFERS_SPAN_MINIMAL)
+void FlatbuffersSpanTest() {
+ // Compile-time checking of non-const [] to const [] conversions.
+ using flatbuffers::internal::is_span_convertable;
+ (void)is_span_convertable<int, 1, int, 1>::type(123);
+ (void)is_span_convertable<const int, 1, int, 1>::type(123);
+ (void)is_span_convertable<const int64_t, 1, int64_t, 1>::type(123);
+ (void)is_span_convertable<const uint64_t, 1, uint64_t, 1>::type(123);
+ (void)is_span_convertable<const int, 1, const int, 1>::type(123);
+ (void)is_span_convertable<const int64_t, 1, const int64_t, 1>::type(123);
+ (void)is_span_convertable<const uint64_t, 1, const uint64_t, 1>::type(123);
+
+ using flatbuffers::span;
+ span<char, 0> c1;
+ TEST_EQ(c1.size(), 0);
+ span<char, flatbuffers::dynamic_extent> c2;
+ TEST_EQ(c2.size(), 0);
+ span<char> c3;
+ TEST_EQ(c3.size(), 0);
+ TEST_ASSERT(c1.empty() && c2.empty() && c3.empty());
+
+ int i_data7[7] = { 0, 1, 2, 3, 4, 5, 6 };
+ span<int, 7> i1(&i_data7[0], 7);
+ span<int> i2(i1); // make dynamic from static
+ TEST_EQ(i1.size(), 7);
+ TEST_EQ(i1.empty(), false);
+ TEST_EQ(i1.size(), i2.size());
+ TEST_EQ(i1.data(), i_data7);
+ TEST_EQ(i1[2], 2);
+ // Make const span from a non-const one.
+ span<const int, 7> i3(i1);
+ // Construct from a C-array.
+ span<int, 7> i4(i_data7);
+ span<const int, 7> i5(i_data7);
+ span<int> i6(i_data7);
+ span<const int> i7(i_data7);
+ TEST_EQ(i7.size(), 7);
+ // Check construction from a const array.
+ const int i_cdata5[5] = { 4, 3, 2, 1, 0 };
+ span<const int, 5> i8(i_cdata5);
+ span<const int> i9(i_cdata5);
+ TEST_EQ(i9.size(), 5);
+ // Construction from a (ptr, size) pair.
+ span<int, 7> i10(i_data7, 7);
+ span<int> i11(i_data7, 7);
+ TEST_EQ(i11.size(), 7);
+ span<const int, 5> i12(i_cdata5, 5);
+ span<const int> i13(i_cdata5, 5);
+ TEST_EQ(i13.size(), 5);
+ // Construction from std::array.
+ std::array<int, 6> i_arr6 = { { 0, 1, 2, 3, 4, 5 } };
+ span<int, 6> i14(i_arr6);
+ span<const int, 6> i15(i_arr6);
+ span<int> i16(i_arr6);
+ span<const int> i17(i_arr6);
+ TEST_EQ(i17.size(), 6);
+ const std::array<int, 8> i_carr8 = { { 0, 1, 2, 3, 4, 5, 6, 7 } };
+ span<const int, 8> i18(i_carr8);
+ span<const int> i19(i_carr8);
+ TEST_EQ(i18.size(), 8);
+ TEST_EQ(i19.size(), 8);
+ TEST_EQ(i19[7], 7);
+ // Check compatibility with flatbuffers::Array.
+ int fbs_int3_underlaying[3] = { 0 };
+ int fbs_int3_data[3] = { 1, 2, 3 };
+ auto &fbs_int3 = flatbuffers::CastToArray(fbs_int3_underlaying);
+ fbs_int3.CopyFromSpan(fbs_int3_data);
+ TEST_EQ(fbs_int3.Get(1), 2);
+ const int fbs_cint3_data[3] = { 2, 3, 4 };
+ fbs_int3.CopyFromSpan(fbs_cint3_data);
+ TEST_EQ(fbs_int3.Get(1), 3);
+ // Check with Array<Enum, N>
+ enum class Dummy : uint16_t { Zero = 0, One, Two };
+ Dummy fbs_dummy3_underlaying[3] = {};
+ Dummy fbs_dummy3_data[3] = { Dummy::One, Dummy::Two, Dummy::Two };
+ auto &fbs_dummy3 = flatbuffers::CastToArray(fbs_dummy3_underlaying);
+ fbs_dummy3.CopyFromSpan(fbs_dummy3_data);
+ TEST_EQ(fbs_dummy3.Get(1), Dummy::Two);
+}
+#else
+void FlatbuffersSpanTest() {}
+#endif
+
void FixedLengthArrayTest() {
// VS10 does not support typed enums, exclude from tests
#if !defined(_MSC_VER) || _MSC_VER >= 1700
// set memory chunk of size ArrayStruct to 1's
std::memset(static_cast<void *>(non_zero_memory), 1, arr_size);
// after placement-new it should be all 0's
-#if defined (_MSC_VER) && defined (_DEBUG)
- #undef new
-#endif
- MyGame::Example::ArrayStruct *ap = new (non_zero_memory) MyGame::Example::ArrayStruct;
-#if defined (_MSC_VER) && defined (_DEBUG)
- #define new DEBUG_NEW
-#endif
+# if defined(_MSC_VER) && defined(_DEBUG)
+# undef new
+# endif
+ MyGame::Example::ArrayStruct *ap =
+ new (non_zero_memory) MyGame::Example::ArrayStruct;
+# if defined(_MSC_VER) && defined(_DEBUG)
+# define new DEBUG_NEW
+# endif
(void)ap;
- for (size_t i = 0; i < arr_size; ++i) {
- TEST_EQ(non_zero_memory[i], 0);
- }
+ for (size_t i = 0; i < arr_size; ++i) { TEST_EQ(non_zero_memory[i], 0); }
#endif
}
+#if !defined(FLATBUFFERS_SPAN_MINIMAL) && \
+ (!defined(_MSC_VER) || _MSC_VER >= 1700)
+void FixedLengthArrayConstructorTest() {
+ const int32_t nested_a[2] = { 1, 2 };
+ MyGame::Example::TestEnum nested_c[2] = { MyGame::Example::TestEnum::A,
+ MyGame::Example::TestEnum::B };
+ const int64_t int64_2[2] = { -2, -1 };
+
+ std::array<MyGame::Example::NestedStruct, 2> init_d = {
+ { MyGame::Example::NestedStruct(nested_a, MyGame::Example::TestEnum::B,
+ nested_c, int64_2),
+ MyGame::Example::NestedStruct(nested_a, MyGame::Example::TestEnum::A,
+ nested_c,
+ std::array<int64_t, 2>{ { 12, 13 } }) }
+ };
+
+ MyGame::Example::ArrayStruct arr_struct(
+ 8.125,
+ std::array<int32_t, 0xF>{
+ { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } },
+ -17, init_d, 10, int64_2);
+ TEST_EQ(arr_struct.a(), 8.125);
+ TEST_EQ(arr_struct.b()->Get(2), 3);
+ TEST_EQ(arr_struct.c(), -17);
+
+ TEST_NOTNULL(arr_struct.d());
+ const auto &arr_d_0 = *arr_struct.d()->Get(0);
+ TEST_EQ(arr_d_0.a()->Get(0), 1);
+ TEST_EQ(arr_d_0.a()->Get(1), 2);
+ TEST_EQ(arr_d_0.b(), MyGame::Example::TestEnum::B);
+ TEST_EQ(arr_d_0.c()->Get(0), MyGame::Example::TestEnum::A);
+ TEST_EQ(arr_d_0.c()->Get(1), MyGame::Example::TestEnum::B);
+ TEST_EQ(arr_d_0.d()->Get(0), -2);
+ TEST_EQ(arr_d_0.d()->Get(1), -1);
+ const auto &arr_d_1 = *arr_struct.d()->Get(1);
+ TEST_EQ(arr_d_1.a()->Get(0), 1);
+ TEST_EQ(arr_d_1.a()->Get(1), 2);
+ TEST_EQ(arr_d_1.b(), MyGame::Example::TestEnum::A);
+ TEST_EQ(arr_d_1.c()->Get(0), MyGame::Example::TestEnum::A);
+ TEST_EQ(arr_d_1.c()->Get(1), MyGame::Example::TestEnum::B);
+ TEST_EQ(arr_d_1.d()->Get(0), 12);
+ TEST_EQ(arr_d_1.d()->Get(1), 13);
+
+ TEST_EQ(arr_struct.e(), 10);
+ TEST_EQ(arr_struct.f()->Get(0), -2);
+ TEST_EQ(arr_struct.f()->Get(1), -1);
+}
+#else
+void FixedLengthArrayConstructorTest() {}
+#endif
+
void NativeTypeTest() {
const int N = 3;
0);
}
+void StringVectorDefaultsTest() {
+ std::vector<std::string> schemas;
+ schemas.push_back("table Monster { mana: string = \"\"; }");
+ schemas.push_back("table Monster { mana: string = \"mystr\"; }");
+ schemas.push_back("table Monster { mana: string = \" \"; }");
+ schemas.push_back("table Monster { mana: [int] = []; }");
+ schemas.push_back("table Monster { mana: [uint] = [ ]; }");
+ schemas.push_back("table Monster { mana: [byte] = [\t\t\n]; }");
+ for (auto s = schemas.begin(); s < schemas.end(); s++) {
+ flatbuffers::Parser parser;
+ TEST_ASSERT(parser.Parse(s->c_str()));
+ const auto *mana = parser.structs_.Lookup("Monster")->fields.Lookup("mana");
+ TEST_EQ(mana->IsDefault(), true);
+ }
+}
+
void OptionalScalarsTest() {
// Simple schemas and a "has optional scalar" sentinal.
std::vector<std::string> schemas;
schemas.push_back("table Monster { mana : bool; }");
schemas.push_back("table Monster { mana : bool = 42; }");
schemas.push_back("table Monster { mana : bool = null; }");
+ schemas.push_back(
+ "enum Enum: int {A=0, B=1} "
+ "table Monster { mana : Enum; }");
+ schemas.push_back(
+ "enum Enum: int {A=0, B=1} "
+ "table Monster { mana : Enum = B; }");
+ schemas.push_back(
+ "enum Enum: int {A=0, B=1} "
+ "table Monster { mana : Enum = null; }");
// Check the FieldDef is correctly set.
for (auto schema = schemas.begin(); schema < schemas.end(); schema++) {
flatbuffers::Parser parser;
TEST_ASSERT(parser.Parse(schema->c_str()));
const auto *mana = parser.structs_.Lookup("Monster")->fields.Lookup("mana");
- TEST_EQ(mana->optional, has_null);
+ TEST_EQ(mana->IsOptional(), has_null);
}
// Test if nullable scalars are allowed for each language.
TEST_ASSERT(!opts->mutate_maybe_i16(-10));
optional_scalars::ScalarStuffT obj;
+ TEST_ASSERT(!obj.maybe_bool);
+ TEST_ASSERT(!obj.maybe_f32.has_value());
opts->UnPackTo(&obj);
TEST_ASSERT(!obj.maybe_bool);
TEST_ASSERT(!obj.maybe_f32.has_value());
TEST_ASSERT(obj.maybe_i8.has_value() && obj.maybe_i8.value() == 3);
TEST_ASSERT(obj.maybe_i8 && *obj.maybe_i8 == 3);
obj.maybe_i32 = -1;
+ obj.maybe_enum = optional_scalars::OptionalByte_Two;
fbb.Clear();
FinishScalarStuffBuffer(fbb, optional_scalars::ScalarStuff::Pack(fbb, &obj));
TEST_EQ(opts->maybe_i8().value(), 3);
TEST_ASSERT(opts->maybe_i32().has_value());
TEST_EQ(opts->maybe_i32().value(), -1);
+ TEST_EQ(opts->maybe_enum().value(), optional_scalars::OptionalByte_Two);
TEST_ASSERT(opts->maybe_i32() == flatbuffers::Optional<int64_t>(-1));
}
}
}
+void FieldIdentifierTest() {
+ using flatbuffers::Parser;
+ TEST_EQ(true, Parser().Parse("table T{ f: int (id:0); }"));
+ // non-integer `id` should be rejected
+ TEST_EQ(false, Parser().Parse("table T{ f: int (id:text); }"));
+ TEST_EQ(false, Parser().Parse("table T{ f: int (id:\"text\"); }"));
+ TEST_EQ(false, Parser().Parse("table T{ f: int (id:0text); }"));
+ TEST_EQ(false, Parser().Parse("table T{ f: int (id:1.0); }"));
+ TEST_EQ(false, Parser().Parse("table T{ f: int (id:-1); g: int (id:0); }"));
+ TEST_EQ(false, Parser().Parse("table T{ f: int (id:129496726); }"));
+ // A unuion filed occupys two ids: enumerator + pointer (offset).
+ TEST_EQ(false,
+ Parser().Parse("union X{} table T{ u: X(id:0); table F{x:int;\n}"));
+ // Positive tests for unions
+ TEST_EQ(true, Parser().Parse("union X{} table T{ u: X (id:1); }"));
+ TEST_EQ(true, Parser().Parse("union X{} table T{ u: X; }"));
+ // Test using 'inf' and 'nan' words both as identifiers and as default values.
+ TEST_EQ(true, Parser().Parse("table T{ nan: string; }"));
+ TEST_EQ(true, Parser().Parse("table T{ inf: string; }"));
+#if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
+ TEST_EQ(true, Parser().Parse("table T{ inf: float = inf; }"));
+ TEST_EQ(true, Parser().Parse("table T{ nan: float = inf; }"));
+#endif
+}
+
+void ParseIncorrectMonsterJsonTest() {
+ std::string schemafile;
+ TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.bfbs").c_str(),
+ true, &schemafile),
+ true);
+ flatbuffers::Parser parser;
+ flatbuffers::Verifier verifier(
+ reinterpret_cast<const uint8_t *>(schemafile.c_str()), schemafile.size());
+ TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
+ TEST_EQ(parser.Deserialize((const uint8_t *)schemafile.c_str(),
+ schemafile.size()),
+ true);
+ TEST_EQ(parser.ParseJson("{name:\"monster\"}"), true);
+ TEST_EQ(parser.ParseJson(""), false);
+ TEST_EQ(parser.ParseJson("{name: 1}"), false);
+ TEST_EQ(parser.ParseJson("{name:+1}"), false);
+ TEST_EQ(parser.ParseJson("{name:-1}"), false);
+ TEST_EQ(parser.ParseJson("{name:-f}"), false);
+ TEST_EQ(parser.ParseJson("{name:+f}"), false);
+}
+
int FlatBufferTests() {
// clang-format off
ObjectFlatBuffersTest(flatbuf.data());
MiniReflectFlatBuffersTest(flatbuf.data());
+ MiniReflectFixedLengthArrayTest();
SizePrefixedTest();
NativeTypeTest();
OptionalScalarsTest();
ParseFlexbuffersFromJsonWithNullTest();
+ FlatbuffersSpanTest();
+ FixedLengthArrayConstructorTest();
+ FieldIdentifierTest();
+ StringVectorDefaultsTest();
+ ParseIncorrectMonsterJsonTest();
return 0;
}