mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-29 03:50:01 +00:00
Use the Google Style for clang-format without exceptions (#8706)
This reduces the friction when merging from github and google repos by using the exact same clang style guide. MARKDOWN=true
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
#include "alignment_test.h"
|
||||
|
||||
#include "tests/alignment_test_generated.h"
|
||||
#include "flatbuffers/flatbuffer_builder.h"
|
||||
#include "flatbuffers/util.h"
|
||||
#include "test_assert.h"
|
||||
#include "tests/alignment_test_generated.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
namespace tests {
|
||||
@@ -14,8 +14,8 @@ void AlignmentTest() {
|
||||
BadAlignmentLarge large;
|
||||
Offset<OuterLarge> outer_large = CreateOuterLarge(builder, &large);
|
||||
|
||||
BadAlignmentSmall *small;
|
||||
Offset<Vector<const BadAlignmentSmall *>> small_offset =
|
||||
BadAlignmentSmall* small;
|
||||
Offset<Vector<const BadAlignmentSmall*>> small_offset =
|
||||
builder.CreateUninitializedVectorOfStructs(9, &small);
|
||||
(void)small; // We do not have to write data to trigger the test failure
|
||||
|
||||
@@ -27,22 +27,19 @@ void AlignmentTest() {
|
||||
Verifier verifier(builder.GetBufferPointer(), builder.GetSize());
|
||||
TEST_ASSERT(verifier.VerifyBuffer<BadAlignmentRoot>(nullptr));
|
||||
|
||||
|
||||
// ============= Test Small Structs Vector misalignment ========
|
||||
|
||||
builder.Clear();
|
||||
|
||||
// creating 5 structs with 2 bytes each
|
||||
// 10 bytes in total for Vector data is needed
|
||||
std::vector<EvenSmallStruct> even_vector = { { 2, 1 }, { 3, 1 }, { 4, 1 } };
|
||||
std::vector<OddSmallStruct> odd_vector = { { 6, 5, 4 },
|
||||
{ 9, 8, 7 },
|
||||
{ 1, 2, 3 } };
|
||||
std::vector<EvenSmallStruct> even_vector = {{2, 1}, {3, 1}, {4, 1}};
|
||||
std::vector<OddSmallStruct> odd_vector = {{6, 5, 4}, {9, 8, 7}, {1, 2, 3}};
|
||||
// CreateVectorOfStructs is used in the generated CreateSmallStructsDirect()
|
||||
// method, but we test it directly
|
||||
Offset<Vector<const EvenSmallStruct *>> even_structs_offset =
|
||||
Offset<Vector<const EvenSmallStruct*>> even_structs_offset =
|
||||
builder.CreateVectorOfStructs<EvenSmallStruct>(even_vector);
|
||||
Offset<Vector<const OddSmallStruct *>> odd_structs_offset =
|
||||
Offset<Vector<const OddSmallStruct*>> odd_structs_offset =
|
||||
builder.CreateVectorOfStructs<OddSmallStruct>(odd_vector);
|
||||
Offset<SmallStructs> small_structs_root =
|
||||
CreateSmallStructs(builder, even_structs_offset, odd_structs_offset);
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
namespace flatbuffers {
|
||||
namespace tests {
|
||||
|
||||
void EvolutionTest(const std::string &tests_data_path) {
|
||||
void EvolutionTest(const std::string& tests_data_path) {
|
||||
// VS10 does not support typed enums, exclude from tests
|
||||
#if !defined(_MSC_VER) || _MSC_VER >= 1700
|
||||
const int NUM_VERSIONS = 2;
|
||||
@@ -83,8 +83,8 @@ void EvolutionTest(const std::string &tests_data_path) {
|
||||
void ConformTest() {
|
||||
const char ref[] = "table T { A:int; } enum E:byte { A }";
|
||||
|
||||
auto test_conform = [](const char *ref, const char *test,
|
||||
const char *expected_err) {
|
||||
auto test_conform = [](const char* ref, const char* test,
|
||||
const char* expected_err) {
|
||||
flatbuffers::Parser parser1;
|
||||
TEST_EQ(parser1.Parse(ref), true);
|
||||
flatbuffers::Parser parser2;
|
||||
@@ -112,11 +112,13 @@ void ConformTest() {
|
||||
"field renamed to different type: T2.df (renamed from T2.f)");
|
||||
|
||||
// Check enum underlying type changes.
|
||||
test_conform("enum E:int32 {A}", "enum E: byte {A}", "underlying type differ for enum: E");
|
||||
|
||||
test_conform("enum E:int32 {A}", "enum E: byte {A}",
|
||||
"underlying type differ for enum: E");
|
||||
|
||||
// Check union underlying type changes.
|
||||
const char ref3[] = "table A {} table B {} union C {A, B}";
|
||||
test_conform(ref3, "table A {} table B {} union C:int32 {A, B}", "underlying type differ for union: C");
|
||||
test_conform(ref3, "table A {} table B {} union C:int32 {A, B}",
|
||||
"underlying type differ for union: C");
|
||||
|
||||
// Check conformity for Offset64-related changes.
|
||||
{
|
||||
@@ -151,7 +153,7 @@ void ConformTest() {
|
||||
}
|
||||
}
|
||||
|
||||
void UnionDeprecationTest(const std::string &tests_data_path) {
|
||||
void UnionDeprecationTest(const std::string& tests_data_path) {
|
||||
const int NUM_VERSIONS = 2;
|
||||
std::string schemas[NUM_VERSIONS];
|
||||
std::string jsonfiles[NUM_VERSIONS];
|
||||
|
||||
@@ -28,15 +28,15 @@ void FlexBuffersTest() {
|
||||
slb += "Fred";
|
||||
slb.IndirectFloat(4.0f);
|
||||
auto i_f = slb.LastValue();
|
||||
uint8_t blob[] = { 77 };
|
||||
uint8_t blob[] = {77};
|
||||
slb.Blob(blob, 1);
|
||||
slb += false;
|
||||
slb.ReuseValue(i_f);
|
||||
});
|
||||
int ints[] = { 1, 2, 3 };
|
||||
int ints[] = {1, 2, 3};
|
||||
slb.Vector("bar", ints, 3);
|
||||
slb.FixedTypedVector("bar3", ints, 3);
|
||||
bool bools[] = { true, false, true, false };
|
||||
bool bools[] = {true, false, true, false};
|
||||
slb.Vector("bools", bools, 4);
|
||||
slb.Bool("bool", true);
|
||||
slb.Double("foo", 100);
|
||||
@@ -136,7 +136,8 @@ void FlexBuffersTest() {
|
||||
std::string jsonback_indented;
|
||||
jroot.ToString(true, false, jsonback_indented, true, 0, " ");
|
||||
auto jsontest_indented =
|
||||
"{\n a: [\n 123,\n 456.0\n ],\n b: \"hello\",\n c: true,\n d: false\n}";
|
||||
"{\n a: [\n 123,\n 456.0\n ],\n b: \"hello\",\n c: true,\n d: "
|
||||
"false\n}";
|
||||
TEST_EQ_STR(jsontest_indented, jsonback_indented.c_str());
|
||||
|
||||
slb.Clear();
|
||||
|
||||
@@ -20,8 +20,8 @@ uint32_t lcg_rand() {
|
||||
}
|
||||
void lcg_reset() { lcg_seed = 48271; }
|
||||
|
||||
template<typename T>
|
||||
static void CompareTableFieldValue(flatbuffers::Table *table,
|
||||
template <typename T>
|
||||
static void CompareTableFieldValue(flatbuffers::Table* table,
|
||||
flatbuffers::voffset_t voffset, T val) {
|
||||
T read = table->GetField(voffset, static_cast<T>(0));
|
||||
TEST_EQ(read, val);
|
||||
@@ -64,17 +64,39 @@ void FuzzTest1() {
|
||||
int choice = lcg_rand() % test_values_max;
|
||||
auto off = flatbuffers::FieldIndexToOffset(f);
|
||||
switch (choice) {
|
||||
case 0: builder.AddElement<uint8_t>(off, bool_val, 0); break;
|
||||
case 1: builder.AddElement<int8_t>(off, char_val, 0); break;
|
||||
case 2: builder.AddElement<uint8_t>(off, uchar_val, 0); break;
|
||||
case 3: builder.AddElement<int16_t>(off, short_val, 0); break;
|
||||
case 4: builder.AddElement<uint16_t>(off, ushort_val, 0); break;
|
||||
case 5: builder.AddElement<int32_t>(off, int_val, 0); break;
|
||||
case 6: builder.AddElement<uint32_t>(off, uint_val, 0); break;
|
||||
case 7: builder.AddElement<int64_t>(off, long_val, 0); break;
|
||||
case 8: builder.AddElement<uint64_t>(off, ulong_val, 0); break;
|
||||
case 9: builder.AddElement<float>(off, float_val, 0); break;
|
||||
case 10: builder.AddElement<double>(off, double_val, 0); break;
|
||||
case 0:
|
||||
builder.AddElement<uint8_t>(off, bool_val, 0);
|
||||
break;
|
||||
case 1:
|
||||
builder.AddElement<int8_t>(off, char_val, 0);
|
||||
break;
|
||||
case 2:
|
||||
builder.AddElement<uint8_t>(off, uchar_val, 0);
|
||||
break;
|
||||
case 3:
|
||||
builder.AddElement<int16_t>(off, short_val, 0);
|
||||
break;
|
||||
case 4:
|
||||
builder.AddElement<uint16_t>(off, ushort_val, 0);
|
||||
break;
|
||||
case 5:
|
||||
builder.AddElement<int32_t>(off, int_val, 0);
|
||||
break;
|
||||
case 6:
|
||||
builder.AddElement<uint32_t>(off, uint_val, 0);
|
||||
break;
|
||||
case 7:
|
||||
builder.AddElement<int64_t>(off, long_val, 0);
|
||||
break;
|
||||
case 8:
|
||||
builder.AddElement<uint64_t>(off, ulong_val, 0);
|
||||
break;
|
||||
case 9:
|
||||
builder.AddElement<float>(off, float_val, 0);
|
||||
break;
|
||||
case 10:
|
||||
builder.AddElement<double>(off, double_val, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
objects[i] = builder.EndTable(start);
|
||||
@@ -83,28 +105,50 @@ void FuzzTest1() {
|
||||
|
||||
lcg_reset(); // Reset.
|
||||
|
||||
uint8_t *eob = builder.GetCurrentBufferPointer() + builder.GetSize();
|
||||
uint8_t* eob = builder.GetCurrentBufferPointer() + builder.GetSize();
|
||||
|
||||
// Test that all objects we generated are readable and return the
|
||||
// expected values. We generate random objects in the same order
|
||||
// so this is deterministic.
|
||||
for (int i = 0; i < num_fuzz_objects; i++) {
|
||||
auto table = reinterpret_cast<flatbuffers::Table *>(eob - objects[i]);
|
||||
auto table = reinterpret_cast<flatbuffers::Table*>(eob - objects[i]);
|
||||
for (flatbuffers::voffset_t f = 0; f < fields_per_object; f++) {
|
||||
int choice = lcg_rand() % test_values_max;
|
||||
flatbuffers::voffset_t off = flatbuffers::FieldIndexToOffset(f);
|
||||
switch (choice) {
|
||||
case 0: CompareTableFieldValue(table, off, bool_val); break;
|
||||
case 1: CompareTableFieldValue(table, off, char_val); break;
|
||||
case 2: CompareTableFieldValue(table, off, uchar_val); break;
|
||||
case 3: CompareTableFieldValue(table, off, short_val); break;
|
||||
case 4: CompareTableFieldValue(table, off, ushort_val); break;
|
||||
case 5: CompareTableFieldValue(table, off, int_val); break;
|
||||
case 6: CompareTableFieldValue(table, off, uint_val); break;
|
||||
case 7: CompareTableFieldValue(table, off, long_val); break;
|
||||
case 8: CompareTableFieldValue(table, off, ulong_val); break;
|
||||
case 9: CompareTableFieldValue(table, off, float_val); break;
|
||||
case 10: CompareTableFieldValue(table, off, double_val); break;
|
||||
case 0:
|
||||
CompareTableFieldValue(table, off, bool_val);
|
||||
break;
|
||||
case 1:
|
||||
CompareTableFieldValue(table, off, char_val);
|
||||
break;
|
||||
case 2:
|
||||
CompareTableFieldValue(table, off, uchar_val);
|
||||
break;
|
||||
case 3:
|
||||
CompareTableFieldValue(table, off, short_val);
|
||||
break;
|
||||
case 4:
|
||||
CompareTableFieldValue(table, off, ushort_val);
|
||||
break;
|
||||
case 5:
|
||||
CompareTableFieldValue(table, off, int_val);
|
||||
break;
|
||||
case 6:
|
||||
CompareTableFieldValue(table, off, uint_val);
|
||||
break;
|
||||
case 7:
|
||||
CompareTableFieldValue(table, off, long_val);
|
||||
break;
|
||||
case 8:
|
||||
CompareTableFieldValue(table, off, ulong_val);
|
||||
break;
|
||||
case 9:
|
||||
CompareTableFieldValue(table, off, float_val);
|
||||
break;
|
||||
case 10:
|
||||
CompareTableFieldValue(table, off, double_val);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,8 +175,8 @@ void FuzzTest2() {
|
||||
// Since we're generating schema and corresponding data in tandem,
|
||||
// this convenience function adds strings to both at once.
|
||||
static void Add(RndDef (&definitions_l)[num_definitions],
|
||||
std::string &schema_l, const int instances_per_definition_l,
|
||||
const char *schema_add, const char *instance_add,
|
||||
std::string& schema_l, const int instances_per_definition_l,
|
||||
const char* schema_add, const char* instance_add,
|
||||
int definition) {
|
||||
schema_l += schema_add;
|
||||
for (int i = 0; i < instances_per_definition_l; i++)
|
||||
@@ -266,7 +310,7 @@ void FuzzTest2() {
|
||||
// from the binary and compare against the original.
|
||||
TEST_EQ(parser.Parse(schema.c_str()), true);
|
||||
|
||||
const std::string &json =
|
||||
const std::string& json =
|
||||
definitions[num_definitions - 1].instances[0] + "\n";
|
||||
|
||||
TEST_EQ(parser.Parse(json.c_str()), true);
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace tests {
|
||||
using namespace MyGame::Example;
|
||||
|
||||
// Check stringify of an default enum value to json
|
||||
void JsonDefaultTest(const std::string &tests_data_path) {
|
||||
void JsonDefaultTest(const std::string& tests_data_path) {
|
||||
// load FlatBuffer schema (.fbs) from disk
|
||||
std::string schemafile;
|
||||
TEST_EQ(flatbuffers::LoadFile((tests_data_path + "monster_test.fbs").c_str(),
|
||||
@@ -23,8 +23,8 @@ void JsonDefaultTest(const std::string &tests_data_path) {
|
||||
flatbuffers::Parser parser;
|
||||
auto include_test_path =
|
||||
flatbuffers::ConCatPathFileName(tests_data_path, "include_test");
|
||||
const char *include_directories[] = { tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr };
|
||||
const char* include_directories[] = {tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr};
|
||||
|
||||
TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
|
||||
// create incomplete monster and store to json
|
||||
@@ -44,7 +44,7 @@ void JsonDefaultTest(const std::string &tests_data_path) {
|
||||
TEST_EQ(std::string::npos != jsongen.find("testf: 3.14159"), true);
|
||||
}
|
||||
|
||||
void JsonEnumsTest(const std::string &tests_data_path) {
|
||||
void JsonEnumsTest(const std::string& tests_data_path) {
|
||||
// load FlatBuffer schema (.fbs) from disk
|
||||
std::string schemafile;
|
||||
TEST_EQ(flatbuffers::LoadFile((tests_data_path + "monster_test.fbs").c_str(),
|
||||
@@ -54,8 +54,8 @@ void JsonEnumsTest(const std::string &tests_data_path) {
|
||||
flatbuffers::Parser parser;
|
||||
auto include_test_path =
|
||||
flatbuffers::ConCatPathFileName(tests_data_path, "include_test");
|
||||
const char *include_directories[] = { tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr };
|
||||
const char* include_directories[] = {tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr};
|
||||
parser.opts.output_enum_identifiers = true;
|
||||
TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
|
||||
flatbuffers::FlatBufferBuilder builder;
|
||||
@@ -83,7 +83,7 @@ void JsonEnumsTest(const std::string &tests_data_path) {
|
||||
TEST_EQ(std::string::npos != future_json.find("color: 13"), true);
|
||||
}
|
||||
|
||||
void JsonOptionalTest(const std::string &tests_data_path,
|
||||
void JsonOptionalTest(const std::string& tests_data_path,
|
||||
bool default_scalars) {
|
||||
// load FlatBuffer schema (.fbs) and JSON from disk
|
||||
std::string schemafile;
|
||||
@@ -100,8 +100,8 @@ void JsonOptionalTest(const std::string &tests_data_path,
|
||||
|
||||
auto include_test_path =
|
||||
flatbuffers::ConCatPathFileName(tests_data_path, "include_test");
|
||||
const char *include_directories[] = { tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr };
|
||||
const char* include_directories[] = {tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr};
|
||||
|
||||
// parse schema first, so we can use it to parse the data after
|
||||
flatbuffers::Parser parser;
|
||||
@@ -124,17 +124,17 @@ void JsonOptionalTest(const std::string &tests_data_path,
|
||||
TEST_EQ_STR(jsongen.c_str(), jsonfile.c_str());
|
||||
}
|
||||
|
||||
void ParseIncorrectMonsterJsonTest(const std::string &tests_data_path) {
|
||||
void ParseIncorrectMonsterJsonTest(const std::string& tests_data_path) {
|
||||
std::string schemafile;
|
||||
TEST_EQ(flatbuffers::LoadFile((tests_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());
|
||||
reinterpret_cast<const uint8_t*>(schemafile.c_str()), schemafile.size());
|
||||
TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
|
||||
TEST_EQ(
|
||||
parser.Deserialize(reinterpret_cast<const uint8_t *>(schemafile.c_str()),
|
||||
parser.Deserialize(reinterpret_cast<const uint8_t*>(schemafile.c_str()),
|
||||
schemafile.size()),
|
||||
true);
|
||||
TEST_EQ(parser.ParseJson("{name:\"monster\"}"), true);
|
||||
|
||||
@@ -15,10 +15,10 @@ using namespace keyfield::sample;
|
||||
void FixedSizedScalarKeyInStructTest() {
|
||||
flatbuffers::FlatBufferBuilder fbb;
|
||||
std::vector<Baz> bazs;
|
||||
uint8_t test_array1[4] = { 8, 2, 3, 0 };
|
||||
uint8_t test_array2[4] = { 1, 2, 3, 4 };
|
||||
uint8_t test_array3[4] = { 2, 2, 3, 4 };
|
||||
uint8_t test_array4[4] = { 3, 2, 3, 4 };
|
||||
uint8_t test_array1[4] = {8, 2, 3, 0};
|
||||
uint8_t test_array2[4] = {1, 2, 3, 4};
|
||||
uint8_t test_array3[4] = {2, 2, 3, 4};
|
||||
uint8_t test_array4[4] = {3, 2, 3, 4};
|
||||
bazs.push_back(Baz(flatbuffers::make_span(test_array1), 4));
|
||||
bazs.push_back(Baz(flatbuffers::make_span(test_array2), 1));
|
||||
bazs.push_back(Baz(flatbuffers::make_span(test_array3), 2));
|
||||
@@ -27,10 +27,10 @@ void FixedSizedScalarKeyInStructTest() {
|
||||
|
||||
auto test_string = fbb.CreateString("TEST");
|
||||
|
||||
float test_float_array1[3] = { 1.5, 2.5, 0 };
|
||||
float test_float_array2[3] = { 7.5, 2.5, 0 };
|
||||
float test_float_array3[3] = { 1.5, 2.5, -1 };
|
||||
float test_float_array4[3] = { -1.5, 2.5, 0 };
|
||||
float test_float_array1[3] = {1.5, 2.5, 0};
|
||||
float test_float_array2[3] = {7.5, 2.5, 0};
|
||||
float test_float_array3[3] = {1.5, 2.5, -1};
|
||||
float test_float_array4[3] = {-1.5, 2.5, 0};
|
||||
std::vector<Bar> bars;
|
||||
bars.push_back(Bar(flatbuffers::make_span(test_float_array1), 3));
|
||||
bars.push_back(Bar(flatbuffers::make_span(test_float_array2), 4));
|
||||
@@ -42,7 +42,7 @@ void FixedSizedScalarKeyInStructTest() {
|
||||
|
||||
fbb.Finish(t);
|
||||
|
||||
uint8_t *buf = fbb.GetBufferPointer();
|
||||
uint8_t* buf = fbb.GetBufferPointer();
|
||||
auto foo_table = GetFooTable(buf);
|
||||
|
||||
auto sorted_baz_vec = foo_table->d();
|
||||
@@ -50,37 +50,37 @@ void FixedSizedScalarKeyInStructTest() {
|
||||
TEST_EQ(sorted_baz_vec->Get(3)->b(), 4);
|
||||
|
||||
uint8_t test_array[4];
|
||||
auto *key_array = &flatbuffers::CastToArray(test_array);
|
||||
auto* key_array = &flatbuffers::CastToArray(test_array);
|
||||
key_array->CopyFromSpan(flatbuffers::make_span(test_array1));
|
||||
|
||||
TEST_NOTNULL(sorted_baz_vec->LookupByKey(key_array));
|
||||
TEST_EQ(sorted_baz_vec->LookupByKey(key_array)->b(), 4);
|
||||
uint8_t array_int[4] = { 7, 2, 3, 0 };
|
||||
uint8_t array_int[4] = {7, 2, 3, 0};
|
||||
key_array->CopyFromSpan(flatbuffers::make_span(array_int));
|
||||
TEST_EQ(sorted_baz_vec->LookupByKey(key_array),
|
||||
static_cast<const Baz *>(nullptr));
|
||||
static_cast<const Baz*>(nullptr));
|
||||
|
||||
auto sorted_bar_vec = foo_table->e();
|
||||
TEST_EQ(sorted_bar_vec->Get(0)->b(), 1);
|
||||
TEST_EQ(sorted_bar_vec->Get(3)->b(), 4);
|
||||
|
||||
float test_float_array[3];
|
||||
auto *key_float_array = &flatbuffers::CastToArray(test_float_array);
|
||||
auto* key_float_array = &flatbuffers::CastToArray(test_float_array);
|
||||
key_float_array->CopyFromSpan(flatbuffers::make_span(test_float_array1));
|
||||
TEST_NOTNULL(sorted_bar_vec->LookupByKey(key_float_array));
|
||||
TEST_EQ(sorted_bar_vec->LookupByKey(key_float_array)->b(), 3);
|
||||
float array_float[3] = { -1, -2, -3 };
|
||||
float array_float[3] = {-1, -2, -3};
|
||||
key_float_array->CopyFromSpan(flatbuffers::make_span(array_float));
|
||||
TEST_EQ(sorted_bar_vec->LookupByKey(key_float_array),
|
||||
static_cast<const Bar *>(nullptr));
|
||||
static_cast<const Bar*>(nullptr));
|
||||
}
|
||||
|
||||
void StructKeyInStructTest() {
|
||||
flatbuffers::FlatBufferBuilder fbb;
|
||||
std::vector<Apple> apples;
|
||||
float test_float_array1[3] = { 1.5, 2.5, 0 };
|
||||
float test_float_array2[3] = { 7.5, 2.5, 0 };
|
||||
float test_float_array3[3] = { 1.5, 2.5, -1 };
|
||||
float test_float_array1[3] = {1.5, 2.5, 0};
|
||||
float test_float_array2[3] = {7.5, 2.5, 0};
|
||||
float test_float_array3[3] = {1.5, 2.5, -1};
|
||||
apples.push_back(
|
||||
Apple(2, Color(flatbuffers::make_span(test_float_array1), 3)));
|
||||
apples.push_back(
|
||||
@@ -100,7 +100,7 @@ void StructKeyInStructTest() {
|
||||
auto orc = foo_builder.Finish();
|
||||
fbb.Finish(orc);
|
||||
|
||||
uint8_t *buf = fbb.GetBufferPointer();
|
||||
uint8_t* buf = fbb.GetBufferPointer();
|
||||
auto foo_table = GetFooTable(buf);
|
||||
|
||||
auto sorted_apple_vec = foo_table->f();
|
||||
@@ -113,15 +113,15 @@ void StructKeyInStructTest() {
|
||||
2);
|
||||
TEST_EQ(sorted_apple_vec->LookupByKey(
|
||||
Color(flatbuffers::make_span(test_float_array1), 0)),
|
||||
static_cast<const Apple *>(nullptr));
|
||||
static_cast<const Apple*>(nullptr));
|
||||
}
|
||||
|
||||
void NestedStructKeyInStructTest() {
|
||||
flatbuffers::FlatBufferBuilder fbb;
|
||||
std::vector<Fruit> fruits;
|
||||
float test_float_array1[3] = { 1.5, 2.5, 0 };
|
||||
float test_float_array2[3] = { 1.5, 2.5, 0 };
|
||||
float test_float_array3[3] = { 1.5, 2.5, -1 };
|
||||
float test_float_array1[3] = {1.5, 2.5, 0};
|
||||
float test_float_array2[3] = {1.5, 2.5, 0};
|
||||
float test_float_array3[3] = {1.5, 2.5, -1};
|
||||
|
||||
fruits.push_back(
|
||||
Fruit(Apple(2, Color(flatbuffers::make_span(test_float_array1), 2)), 2));
|
||||
@@ -140,7 +140,7 @@ void NestedStructKeyInStructTest() {
|
||||
|
||||
auto orc = foo_builder.Finish();
|
||||
fbb.Finish(orc);
|
||||
uint8_t *buf = fbb.GetBufferPointer();
|
||||
uint8_t* buf = fbb.GetBufferPointer();
|
||||
auto foo_table = GetFooTable(buf);
|
||||
|
||||
auto sorted_fruit_vec = foo_table->g();
|
||||
@@ -154,34 +154,31 @@ void NestedStructKeyInStructTest() {
|
||||
1);
|
||||
TEST_EQ(sorted_fruit_vec->LookupByKey(
|
||||
Apple(1, Color(flatbuffers::make_span(test_float_array2), 1))),
|
||||
static_cast<const Fruit *>(nullptr));
|
||||
static_cast<const Fruit*>(nullptr));
|
||||
}
|
||||
|
||||
void FixedSizedStructArrayKeyInStructTest() {
|
||||
flatbuffers::FlatBufferBuilder fbb;
|
||||
std::vector<Grain> grains;
|
||||
uint8_t test_char_array1[3] = { 'u', 's', 'a' };
|
||||
uint8_t test_char_array2[3] = { 'c', 'h', 'n' };
|
||||
uint8_t test_char_array3[3] = { 'c', 'h', 'l' };
|
||||
uint8_t test_char_array4[3] = { 'f', 'r', 'a' };
|
||||
uint8_t test_char_array5[3] = { 'i', 'n', 'd' };
|
||||
uint8_t test_char_array6[3] = { 'i', 't', 'a' };
|
||||
uint8_t test_char_array1[3] = {'u', 's', 'a'};
|
||||
uint8_t test_char_array2[3] = {'c', 'h', 'n'};
|
||||
uint8_t test_char_array3[3] = {'c', 'h', 'l'};
|
||||
uint8_t test_char_array4[3] = {'f', 'r', 'a'};
|
||||
uint8_t test_char_array5[3] = {'i', 'n', 'd'};
|
||||
uint8_t test_char_array6[3] = {'i', 't', 'a'};
|
||||
|
||||
Rice test_rice_array1[3] = {
|
||||
Rice(flatbuffers::make_span(test_char_array1), 2),
|
||||
Rice(flatbuffers::make_span(test_char_array2), 1),
|
||||
Rice(flatbuffers::make_span(test_char_array3), 2)
|
||||
};
|
||||
Rice(flatbuffers::make_span(test_char_array1), 2),
|
||||
Rice(flatbuffers::make_span(test_char_array2), 1),
|
||||
Rice(flatbuffers::make_span(test_char_array3), 2)};
|
||||
Rice test_rice_array2[3] = {
|
||||
Rice(flatbuffers::make_span(test_char_array4), 2),
|
||||
Rice(flatbuffers::make_span(test_char_array5), 1),
|
||||
Rice(flatbuffers::make_span(test_char_array6), 2)
|
||||
};
|
||||
Rice(flatbuffers::make_span(test_char_array4), 2),
|
||||
Rice(flatbuffers::make_span(test_char_array5), 1),
|
||||
Rice(flatbuffers::make_span(test_char_array6), 2)};
|
||||
Rice test_rice_array3[3] = {
|
||||
Rice(flatbuffers::make_span(test_char_array4), 2),
|
||||
Rice(flatbuffers::make_span(test_char_array6), 1),
|
||||
Rice(flatbuffers::make_span(test_char_array1), 2)
|
||||
};
|
||||
Rice(flatbuffers::make_span(test_char_array4), 2),
|
||||
Rice(flatbuffers::make_span(test_char_array6), 1),
|
||||
Rice(flatbuffers::make_span(test_char_array1), 2)};
|
||||
|
||||
grains.push_back(Grain(flatbuffers::make_span(test_rice_array1), 3));
|
||||
grains.push_back(Grain(flatbuffers::make_span(test_rice_array2), 1));
|
||||
@@ -196,7 +193,7 @@ void FixedSizedStructArrayKeyInStructTest() {
|
||||
|
||||
auto orc = foo_builder.Finish();
|
||||
fbb.Finish(orc);
|
||||
uint8_t *buf = fbb.GetBufferPointer();
|
||||
uint8_t* buf = fbb.GetBufferPointer();
|
||||
auto foo_table = GetFooTable(buf);
|
||||
|
||||
auto sorted_grain_vec = foo_table->h();
|
||||
@@ -207,13 +204,12 @@ void FixedSizedStructArrayKeyInStructTest() {
|
||||
sorted_grain_vec->LookupByKey(&flatbuffers::CastToArray(test_rice_array1))
|
||||
->tag(),
|
||||
3);
|
||||
Rice test_rice_array[3] = { Rice(flatbuffers::make_span(test_char_array3), 2),
|
||||
Rice(flatbuffers::make_span(test_char_array2), 1),
|
||||
Rice(flatbuffers::make_span(test_char_array1),
|
||||
2) };
|
||||
Rice test_rice_array[3] = {Rice(flatbuffers::make_span(test_char_array3), 2),
|
||||
Rice(flatbuffers::make_span(test_char_array2), 1),
|
||||
Rice(flatbuffers::make_span(test_char_array1), 2)};
|
||||
TEST_EQ(
|
||||
sorted_grain_vec->LookupByKey(&flatbuffers::CastToArray(test_rice_array)),
|
||||
static_cast<const Grain *>(nullptr));
|
||||
static_cast<const Grain*>(nullptr));
|
||||
TEST_EQ(
|
||||
sorted_grain_vec->LookupByKey(&flatbuffers::CastToArray(test_rice_array1))
|
||||
->tag(),
|
||||
|
||||
@@ -24,7 +24,7 @@ static const auto infinity_d = std::numeric_limits<double>::infinity();
|
||||
using namespace MyGame::Example;
|
||||
|
||||
// example of how to build up a serialized buffer algorithmically:
|
||||
flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
|
||||
flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string& buffer) {
|
||||
flatbuffers::FlatBufferBuilder builder;
|
||||
|
||||
auto vec = Vec3(1, 2, 3, 0, Color_Red, Test(10, 20));
|
||||
@@ -33,7 +33,7 @@ flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
|
||||
|
||||
// Use the initializer_list specialization of CreateVector.
|
||||
auto inventory =
|
||||
builder.CreateVector<uint8_t>({ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
||||
builder.CreateVector<uint8_t>({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
|
||||
|
||||
// Alternatively, create the vector first, and fill in data later:
|
||||
// unsigned char *inv_buf = nullptr;
|
||||
@@ -41,12 +41,12 @@ flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
|
||||
// 10, &inv_buf);
|
||||
// memcpy(inv_buf, inv_data, 10);
|
||||
|
||||
Test tests[] = { Test(10, 20), Test(30, 40) };
|
||||
Test tests[] = {Test(10, 20), Test(30, 40)};
|
||||
auto testv = builder.CreateVectorOfStructs(tests, 2);
|
||||
|
||||
// Create a vector of structures from a lambda.
|
||||
auto testv2 = builder.CreateVectorOfStructs<Test>(
|
||||
2, [&](size_t i, Test *s) -> void { *s = tests[i]; });
|
||||
2, [&](size_t i, Test* s) -> void { *s = tests[i]; });
|
||||
|
||||
// create monster with very few fields set:
|
||||
// (same functionality as CreateMonster below, but sets fields manually)
|
||||
@@ -69,9 +69,9 @@ flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
|
||||
auto vecofstrings =
|
||||
builder.CreateVector<flatbuffers::Offset<flatbuffers::String>>(
|
||||
4,
|
||||
[](size_t i, flatbuffers::FlatBufferBuilder *b)
|
||||
[](size_t i, flatbuffers::FlatBufferBuilder* b)
|
||||
-> flatbuffers::Offset<flatbuffers::String> {
|
||||
static const char *names[] = { "bob", "fred", "bob", "fred" };
|
||||
static const char* names[] = {"bob", "fred", "bob", "fred"};
|
||||
return b->CreateSharedString(names[i]);
|
||||
},
|
||||
&builder);
|
||||
@@ -83,7 +83,7 @@ flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
|
||||
auto vecofstrings2 = builder.CreateVectorOfStrings(names2);
|
||||
|
||||
// Creating vectors from types that are different from std::string
|
||||
std::vector<const char *> names3;
|
||||
std::vector<const char*> names3;
|
||||
names3.push_back("foo");
|
||||
names3.push_back("bar");
|
||||
builder.CreateVectorOfStrings(names3); // Also an accepted type
|
||||
@@ -96,11 +96,13 @@ flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
|
||||
#endif
|
||||
|
||||
// Make sure the template deduces an initializer as std::vector<std::string>
|
||||
builder.CreateVectorOfStrings({ "hello", "world" });
|
||||
builder.CreateVectorOfStrings({"hello", "world"});
|
||||
|
||||
// Create many vectors of strings
|
||||
std::vector<std::string> manyNames;
|
||||
for (auto i = 0; i < 100; i++) { manyNames.push_back("john_doe"); }
|
||||
for (auto i = 0; i < 100; i++) {
|
||||
manyNames.push_back("john_doe");
|
||||
}
|
||||
auto manyNamesVec = builder.CreateVectorOfStrings(manyNames);
|
||||
TEST_EQ(false, manyNamesVec.IsNull());
|
||||
auto manyNamesVec2 =
|
||||
@@ -155,7 +157,7 @@ flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
|
||||
flexbuild.Finish();
|
||||
auto flex = builder.CreateVector(flexbuild.GetBuffer());
|
||||
// Test vector of enums.
|
||||
Color colors[] = { Color_Blue, Color_Green };
|
||||
Color colors[] = {Color_Blue, Color_Green};
|
||||
// We use this special creation function because we have an array of
|
||||
// pre-C++11 (enum class) enums whose size likely is int, yet its declared
|
||||
// type in the schema is byte.
|
||||
@@ -184,14 +186,14 @@ flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
|
||||
|
||||
// return the buffer for the caller to use.
|
||||
auto bufferpointer =
|
||||
reinterpret_cast<const char *>(builder.GetBufferPointer());
|
||||
reinterpret_cast<const char*>(builder.GetBufferPointer());
|
||||
buffer.assign(bufferpointer, bufferpointer + builder.GetSize());
|
||||
|
||||
return builder.Release();
|
||||
}
|
||||
|
||||
// example of accessing a buffer loaded in memory:
|
||||
void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length, bool pooled) {
|
||||
void AccessFlatBufferTest(const uint8_t* flatbuf, size_t length, bool pooled) {
|
||||
// First, verify the buffers integrity (optional)
|
||||
flatbuffers::Verifier verifier(flatbuf, length);
|
||||
std::vector<uint8_t> flex_reuse_tracker;
|
||||
@@ -238,7 +240,7 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length, bool pooled) {
|
||||
auto inventory = monster->inventory();
|
||||
TEST_EQ(VectorLength(inventory), 10UL); // Works even if inventory is null.
|
||||
TEST_NOTNULL(inventory);
|
||||
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
unsigned char inv_data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
// Check compatibilty of iterators with STL.
|
||||
std::vector<unsigned char> inv_vec(inventory->begin(), inventory->end());
|
||||
size_t n = 0;
|
||||
@@ -277,7 +279,7 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length, bool pooled) {
|
||||
|
||||
// Example of accessing a union:
|
||||
TEST_EQ(monster->test_type(), Any_Monster); // First make sure which it is.
|
||||
auto monster2 = reinterpret_cast<const Monster *>(monster->test());
|
||||
auto monster2 = reinterpret_cast<const Monster*>(monster->test());
|
||||
TEST_NOTNULL(monster2);
|
||||
TEST_EQ_STR(monster2->name()->c_str(), "Fred");
|
||||
|
||||
@@ -350,8 +352,7 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length, bool pooled) {
|
||||
}
|
||||
TEST_NOTNULL(vecofstructs->LookupByKey(0)); // test default value
|
||||
TEST_NOTNULL(vecofstructs->LookupByKey(3));
|
||||
TEST_EQ(static_cast<const Ability *>(nullptr),
|
||||
vecofstructs->LookupByKey(5));
|
||||
TEST_EQ(static_cast<const Ability*>(nullptr), vecofstructs->LookupByKey(5));
|
||||
}
|
||||
|
||||
if (auto vec_of_stat = monster->scalar_key_sorted_tables()) {
|
||||
@@ -394,9 +395,9 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length, bool pooled) {
|
||||
TEST_EQ(flatbuffers::AlignOf<Test>(), 2UL);
|
||||
TEST_EQ(sizeof(Test), 4UL);
|
||||
|
||||
const flatbuffers::Vector<const Test *> *tests_array[] = {
|
||||
monster->test4(),
|
||||
monster->test5(),
|
||||
const flatbuffers::Vector<const Test*>* tests_array[] = {
|
||||
monster->test4(),
|
||||
monster->test5(),
|
||||
};
|
||||
for (size_t i = 0; i < sizeof(tests_array) / sizeof(tests_array[0]); ++i) {
|
||||
auto tests = tests_array[i];
|
||||
@@ -421,7 +422,7 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length, bool pooled) {
|
||||
}
|
||||
|
||||
// Change a FlatBuffer in-place, after it has been constructed.
|
||||
void MutateFlatBuffersTest(uint8_t *flatbuf, std::size_t length) {
|
||||
void MutateFlatBuffersTest(uint8_t* flatbuf, std::size_t length) {
|
||||
// Get non-const pointer to root.
|
||||
auto monster = GetMutableMonster(flatbuf);
|
||||
|
||||
@@ -451,7 +452,7 @@ void MutateFlatBuffersTest(uint8_t *flatbuf, std::size_t length) {
|
||||
|
||||
// Mutate structs.
|
||||
auto pos = monster->mutable_pos();
|
||||
auto &test3 = pos->mutable_test3(); // Struct inside a struct.
|
||||
auto& test3 = pos->mutable_test3(); // Struct inside a struct.
|
||||
test3.mutate_a(50); // Struct fields never fail.
|
||||
TEST_EQ(test3.a(), 50);
|
||||
test3.mutate_a(10);
|
||||
@@ -480,7 +481,7 @@ void MutateFlatBuffersTest(uint8_t *flatbuf, std::size_t length) {
|
||||
|
||||
// Mutate via LookupByKey
|
||||
TEST_NOTNULL(tables->MutableLookupByKey("Barney"));
|
||||
TEST_EQ(static_cast<Monster *>(nullptr),
|
||||
TEST_EQ(static_cast<Monster*>(nullptr),
|
||||
tables->MutableLookupByKey("DoesntExist"));
|
||||
TEST_EQ(tables->MutableLookupByKey("Barney")->hp(), 1000);
|
||||
TEST_EQ(tables->MutableLookupByKey("Barney")->mutate_hp(0), true);
|
||||
@@ -493,18 +494,18 @@ void MutateFlatBuffersTest(uint8_t *flatbuf, std::size_t length) {
|
||||
}
|
||||
|
||||
// Unpack a FlatBuffer into objects.
|
||||
void ObjectFlatBuffersTest(uint8_t *flatbuf) {
|
||||
void ObjectFlatBuffersTest(uint8_t* flatbuf) {
|
||||
// Optional: we can specify resolver and rehasher functions to turn hashed
|
||||
// strings into object pointers and back, to implement remote references
|
||||
// and such.
|
||||
auto resolver = flatbuffers::resolver_function_t(
|
||||
[](void **pointer_adr, flatbuffers::hash_value_t hash) {
|
||||
[](void** pointer_adr, flatbuffers::hash_value_t hash) {
|
||||
(void)pointer_adr;
|
||||
(void)hash;
|
||||
// Don't actually do anything, leave variable null.
|
||||
});
|
||||
auto rehasher = flatbuffers::rehasher_function_t(
|
||||
[](void *pointer) -> flatbuffers::hash_value_t {
|
||||
[](void* pointer) -> flatbuffers::hash_value_t {
|
||||
(void)pointer;
|
||||
return 0;
|
||||
});
|
||||
@@ -549,20 +550,20 @@ void ObjectFlatBuffersTest(uint8_t *flatbuf) {
|
||||
}
|
||||
|
||||
// Utility function to check a Monster object.
|
||||
void CheckMonsterObject(MonsterT *monster2) {
|
||||
void CheckMonsterObject(MonsterT* monster2) {
|
||||
TEST_EQ(monster2->hp, 80);
|
||||
TEST_EQ(monster2->mana, 150); // default
|
||||
TEST_EQ_STR(monster2->name.c_str(), "MyMonster");
|
||||
|
||||
auto &pos = monster2->pos;
|
||||
auto& pos = monster2->pos;
|
||||
TEST_NOTNULL(pos);
|
||||
TEST_EQ(pos->z(), 3);
|
||||
TEST_EQ(pos->test3().a(), 10);
|
||||
TEST_EQ(pos->test3().b(), 20);
|
||||
|
||||
auto &inventory = monster2->inventory;
|
||||
auto& inventory = monster2->inventory;
|
||||
TEST_EQ(inventory.size(), 10UL);
|
||||
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
unsigned char inv_data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
for (auto it = inventory.begin(); it != inventory.end(); ++it)
|
||||
TEST_EQ(*it, inv_data[it - inventory.begin()]);
|
||||
|
||||
@@ -572,24 +573,24 @@ void CheckMonsterObject(MonsterT *monster2) {
|
||||
TEST_NOTNULL(monster3);
|
||||
TEST_EQ_STR(monster3->name.c_str(), "Fred");
|
||||
|
||||
auto &vecofstrings = monster2->testarrayofstring;
|
||||
auto& vecofstrings = monster2->testarrayofstring;
|
||||
TEST_EQ(vecofstrings.size(), 4U);
|
||||
TEST_EQ_STR(vecofstrings[0].c_str(), "bob");
|
||||
TEST_EQ_STR(vecofstrings[1].c_str(), "fred");
|
||||
|
||||
auto &vecofstrings2 = monster2->testarrayofstring2;
|
||||
auto& vecofstrings2 = monster2->testarrayofstring2;
|
||||
TEST_EQ(vecofstrings2.size(), 2U);
|
||||
TEST_EQ_STR(vecofstrings2[0].c_str(), "jane");
|
||||
TEST_EQ_STR(vecofstrings2[1].c_str(), "mary");
|
||||
|
||||
auto &vecoftables = monster2->testarrayoftables;
|
||||
auto& vecoftables = monster2->testarrayoftables;
|
||||
TEST_EQ(vecoftables.size(), 3U);
|
||||
TEST_EQ_STR(vecoftables[0]->name.c_str(), "Barney");
|
||||
TEST_EQ(vecoftables[0]->hp, 1000);
|
||||
TEST_EQ_STR(vecoftables[1]->name.c_str(), "Fred");
|
||||
TEST_EQ_STR(vecoftables[2]->name.c_str(), "Wilma");
|
||||
|
||||
auto &tests = monster2->test4;
|
||||
auto& tests = monster2->test4;
|
||||
TEST_EQ(tests[0].a(), 10);
|
||||
TEST_EQ(tests[0].b(), 20);
|
||||
TEST_EQ(tests[1].a(), 30);
|
||||
@@ -634,7 +635,7 @@ void SizePrefixedTest() {
|
||||
}
|
||||
}
|
||||
|
||||
void TestMonsterExtraFloats(const std::string &tests_data_path) {
|
||||
void TestMonsterExtraFloats(const std::string& tests_data_path) {
|
||||
#if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
|
||||
TEST_EQ(is_quiet_nan(1.0), false);
|
||||
TEST_EQ(is_quiet_nan(infinity_d), false);
|
||||
@@ -652,8 +653,8 @@ void TestMonsterExtraFloats(const std::string &tests_data_path) {
|
||||
// Parse schema first, so we can use it to parse the data after.
|
||||
Parser parser;
|
||||
auto include_test_path = ConCatPathFileName(tests_data_path, "include_test");
|
||||
const char *include_directories[] = { tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr };
|
||||
const char* include_directories[] = {tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr};
|
||||
TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
|
||||
// Create empty extra and store to json.
|
||||
parser.opts.output_default_scalars_in_json = true;
|
||||
@@ -774,7 +775,7 @@ void TypeAliasesTest() {
|
||||
|
||||
// example of parsing text straight into a buffer, and generating
|
||||
// text back from it:
|
||||
void ParseAndGenerateTextTest(const std::string &tests_data_path, bool binary) {
|
||||
void ParseAndGenerateTextTest(const std::string& tests_data_path, bool binary) {
|
||||
// load FlatBuffer schema (.fbs) and JSON from disk
|
||||
std::string schemafile;
|
||||
std::string jsonfile;
|
||||
@@ -790,21 +791,21 @@ void ParseAndGenerateTextTest(const std::string &tests_data_path, bool binary) {
|
||||
|
||||
auto include_test_path =
|
||||
flatbuffers::ConCatPathFileName(tests_data_path, "include_test");
|
||||
const char *include_directories[] = { tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr };
|
||||
const char* include_directories[] = {tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr};
|
||||
|
||||
// parse schema first, so we can use it to parse the data after
|
||||
flatbuffers::Parser parser;
|
||||
if (binary) {
|
||||
flatbuffers::Verifier verifier(
|
||||
reinterpret_cast<const uint8_t *>(schemafile.c_str()),
|
||||
reinterpret_cast<const uint8_t*>(schemafile.c_str()),
|
||||
schemafile.size());
|
||||
TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
|
||||
// auto schema = reflection::GetSchema(schemafile.c_str());
|
||||
TEST_EQ(parser.Deserialize(
|
||||
reinterpret_cast<const uint8_t *>(schemafile.c_str()),
|
||||
schemafile.size()),
|
||||
true);
|
||||
TEST_EQ(
|
||||
parser.Deserialize(reinterpret_cast<const uint8_t*>(schemafile.c_str()),
|
||||
schemafile.size()),
|
||||
true);
|
||||
} else {
|
||||
TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
|
||||
}
|
||||
@@ -866,14 +867,14 @@ void ParseAndGenerateTextTest(const std::string &tests_data_path, bool binary) {
|
||||
TEST_EQ_STR(jsongen_utf8.c_str(), jsonfile_utf8.c_str());
|
||||
}
|
||||
|
||||
void UnPackTo(const uint8_t *flatbuf) {
|
||||
void UnPackTo(const uint8_t* flatbuf) {
|
||||
// Get a monster that has a name and no enemy
|
||||
auto orig_monster = GetMonster(flatbuf);
|
||||
TEST_EQ_STR(orig_monster->name()->c_str(), "MyMonster");
|
||||
TEST_ASSERT(orig_monster->enemy() == nullptr);
|
||||
|
||||
// Create an enemy
|
||||
MonsterT *enemy = new MonsterT();
|
||||
MonsterT* enemy = new MonsterT();
|
||||
enemy->name = "Enemy";
|
||||
|
||||
// And create another monster owning the enemy,
|
||||
|
||||
@@ -3,19 +3,19 @@
|
||||
#include "native_type_test_generated.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
Geometry::Vector3D Pack(const Native::Vector3D &obj) {
|
||||
Geometry::Vector3D Pack(const Native::Vector3D& obj) {
|
||||
return Geometry::Vector3D(obj.x, obj.y, obj.z);
|
||||
}
|
||||
|
||||
const Native::Vector3D UnPack(const Geometry::Vector3D &obj) {
|
||||
const Native::Vector3D UnPack(const Geometry::Vector3D& obj) {
|
||||
return Native::Vector3D(obj.x(), obj.y(), obj.z());
|
||||
}
|
||||
|
||||
Geometry::Vector3DAlt PackVector3DAlt(const Native::Vector3D &obj) {
|
||||
Geometry::Vector3DAlt PackVector3DAlt(const Native::Vector3D& obj) {
|
||||
return Geometry::Vector3DAlt(obj.x, obj.y, obj.z);
|
||||
}
|
||||
|
||||
const Native::Vector3D UnPackVector3DAlt(const Geometry::Vector3DAlt &obj) {
|
||||
const Native::Vector3D UnPackVector3DAlt(const Geometry::Vector3DAlt& obj) {
|
||||
return Native::Vector3D(obj.a(), obj.b(), obj.c());
|
||||
}
|
||||
} // namespace flatbuffers
|
||||
|
||||
@@ -43,7 +43,7 @@ void OptionalScalarsTest() {
|
||||
const bool has_null = schema->find("null") != std::string::npos;
|
||||
flatbuffers::Parser parser;
|
||||
TEST_ASSERT(parser.Parse(schema->c_str()));
|
||||
const auto *mana = parser.structs_.Lookup("Monster")->fields.Lookup("mana");
|
||||
const auto* mana = parser.structs_.Lookup("Monster")->fields.Lookup("mana");
|
||||
TEST_EQ(mana->IsOptional(), has_null);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,9 +16,9 @@ static const auto infinity_f = std::numeric_limits<float>::infinity();
|
||||
static const auto infinity_d = std::numeric_limits<double>::infinity();
|
||||
|
||||
// Test that parser errors are actually generated.
|
||||
static void TestError_(const char *src, const char *error_substr,
|
||||
bool strict_json, const char *file, int line,
|
||||
const char *func) {
|
||||
static void TestError_(const char* src, const char* error_substr,
|
||||
bool strict_json, const char* file, int line,
|
||||
const char* func) {
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.strict_json = strict_json;
|
||||
flatbuffers::Parser parser(opts);
|
||||
@@ -33,17 +33,17 @@ static void TestError_(const char *src, const char *error_substr,
|
||||
}
|
||||
}
|
||||
|
||||
static void TestError_(const char *src, const char *error_substr,
|
||||
const char *file, int line, const char *func) {
|
||||
static void TestError_(const char* src, const char* error_substr,
|
||||
const char* file, int line, const char* func) {
|
||||
TestError_(src, error_substr, false, file, line, func);
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
# define TestError(src, ...) \
|
||||
TestError_(src, __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__)
|
||||
#define TestError(src, ...) \
|
||||
TestError_(src, __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__)
|
||||
#else
|
||||
# define TestError(src, ...) \
|
||||
TestError_(src, __VA_ARGS__, __FILE__, __LINE__, __PRETTY_FUNCTION__)
|
||||
#define TestError(src, ...) \
|
||||
TestError_(src, __VA_ARGS__, __FILE__, __LINE__, __PRETTY_FUNCTION__)
|
||||
#endif
|
||||
|
||||
static bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; }
|
||||
@@ -462,13 +462,15 @@ void InvalidUTF8Test() {
|
||||
TestError(locale_ident.c_str(), "");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T TestValue(const char *json, const char *type_name,
|
||||
const char *decls = nullptr) {
|
||||
template <typename T>
|
||||
T TestValue(const char* json, const char* type_name,
|
||||
const char* decls = nullptr) {
|
||||
flatbuffers::Parser parser;
|
||||
parser.builder_.ForceDefaults(true); // return defaults
|
||||
auto check_default = json ? false : true;
|
||||
if (check_default) { parser.opts.output_default_scalars_in_json = true; }
|
||||
if (check_default) {
|
||||
parser.opts.output_default_scalars_in_json = true;
|
||||
}
|
||||
// Simple schema.
|
||||
std::string schema = std::string(decls ? decls : "") + "\n" +
|
||||
"table X { y:" + std::string(type_name) +
|
||||
@@ -486,7 +488,9 @@ T TestValue(const char *json, const char *type_name,
|
||||
parser.opts.indent_step = -1;
|
||||
TEST_NULL(GenText(parser, parser.builder_.GetBufferPointer(), &print_back));
|
||||
// restore value from its default
|
||||
if (check_default) { TEST_EQ(parser.Parse(print_back.c_str()), true); }
|
||||
if (check_default) {
|
||||
TEST_EQ(parser.Parse(print_back.c_str()), true);
|
||||
}
|
||||
|
||||
auto root = flatbuffers::GetRoot<flatbuffers::Table>(
|
||||
parser.builder_.GetBufferPointer());
|
||||
@@ -796,7 +800,7 @@ void UnicodeSurrogatesTest() {
|
||||
true);
|
||||
auto root = flatbuffers::GetRoot<flatbuffers::Table>(
|
||||
parser.builder_.GetBufferPointer());
|
||||
auto string = root->GetPointer<flatbuffers::String *>(
|
||||
auto string = root->GetPointer<flatbuffers::String*>(
|
||||
flatbuffers::FieldIndexToOffset(0));
|
||||
TEST_EQ_STR(string->c_str(), "\xF0\x9F\x92\xA9");
|
||||
}
|
||||
@@ -843,11 +847,14 @@ void ParseUnionTest() {
|
||||
true);
|
||||
|
||||
// Test union underlying type
|
||||
const char *source = "table A {} table B {} union U : int {A, B} table C {test_union: U; test_vector_of_union: [U];}";
|
||||
const char* source =
|
||||
"table A {} table B {} union U : int {A, B} table C {test_union: U; "
|
||||
"test_vector_of_union: [U];}";
|
||||
flatbuffers::Parser parser3;
|
||||
parser3.opts.lang_to_generate = flatbuffers::IDLOptions::kCpp | flatbuffers::IDLOptions::kTs;
|
||||
parser3.opts.lang_to_generate =
|
||||
flatbuffers::IDLOptions::kCpp | flatbuffers::IDLOptions::kTs;
|
||||
TEST_EQ(parser3.Parse(source), true);
|
||||
|
||||
|
||||
parser3.opts.lang_to_generate &= flatbuffers::IDLOptions::kJava;
|
||||
TEST_EQ(parser3.Parse(source), false);
|
||||
}
|
||||
@@ -898,7 +905,7 @@ void StringVectorDefaultsTest() {
|
||||
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");
|
||||
const auto* mana = parser.structs_.Lookup("Monster")->fields.Lookup("mana");
|
||||
TEST_EQ(mana->IsDefault(), true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,10 @@
|
||||
namespace flatbuffers {
|
||||
namespace tests {
|
||||
|
||||
void RunTest(const flatbuffers::IDLOptions &opts, const std::string &proto_path,
|
||||
const std::string &proto_file, const std::string &golden_file,
|
||||
void RunTest(const flatbuffers::IDLOptions& opts, const std::string& proto_path,
|
||||
const std::string& proto_file, const std::string& golden_file,
|
||||
const std::string import_proto_file) {
|
||||
const char *include_directories[] = { proto_path.c_str(), nullptr };
|
||||
const char* include_directories[] = {proto_path.c_str(), nullptr};
|
||||
|
||||
// Parse proto.
|
||||
flatbuffers::Parser parser(opts);
|
||||
@@ -48,7 +48,7 @@ void RunTest(const flatbuffers::IDLOptions &opts, const std::string &proto_path,
|
||||
TEST_EQ_STR(fbs.c_str(), golden_file.c_str());
|
||||
}
|
||||
|
||||
void proto_test(const std::string &proto_path, const std::string &proto_file) {
|
||||
void proto_test(const std::string& proto_path, const std::string& proto_file) {
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.include_dependence_headers = false;
|
||||
opts.proto_mode = true;
|
||||
@@ -63,8 +63,8 @@ void proto_test(const std::string &proto_path, const std::string &proto_file) {
|
||||
RunTest(opts, proto_path, proto_file, golden_file);
|
||||
}
|
||||
|
||||
void proto_test_id(const std::string &proto_path,
|
||||
const std::string &proto_file) {
|
||||
void proto_test_id(const std::string& proto_path,
|
||||
const std::string& proto_file) {
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.include_dependence_headers = false;
|
||||
opts.proto_mode = true;
|
||||
@@ -80,8 +80,8 @@ void proto_test_id(const std::string &proto_path,
|
||||
RunTest(opts, proto_path, proto_file, golden_file);
|
||||
}
|
||||
|
||||
void proto_test_union(const std::string &proto_path,
|
||||
const std::string &proto_file) {
|
||||
void proto_test_union(const std::string& proto_path,
|
||||
const std::string& proto_file) {
|
||||
// Parse proto with --oneof-union option.
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.include_dependence_headers = false;
|
||||
@@ -96,8 +96,8 @@ void proto_test_union(const std::string &proto_path,
|
||||
RunTest(opts, proto_path, proto_file, golden_file);
|
||||
}
|
||||
|
||||
void proto_test_union_id(const std::string &proto_path,
|
||||
const std::string &proto_file) {
|
||||
void proto_test_union_id(const std::string& proto_path,
|
||||
const std::string& proto_file) {
|
||||
// Parse proto with --oneof-union option.
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.include_dependence_headers = false;
|
||||
@@ -114,8 +114,8 @@ void proto_test_union_id(const std::string &proto_path,
|
||||
RunTest(opts, proto_path, proto_file, golden_file);
|
||||
}
|
||||
|
||||
void proto_test_union_suffix(const std::string &proto_path,
|
||||
const std::string &proto_file) {
|
||||
void proto_test_union_suffix(const std::string& proto_path,
|
||||
const std::string& proto_file) {
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.include_dependence_headers = false;
|
||||
opts.proto_mode = true;
|
||||
@@ -131,8 +131,8 @@ void proto_test_union_suffix(const std::string &proto_path,
|
||||
RunTest(opts, proto_path, proto_file, golden_file);
|
||||
}
|
||||
|
||||
void proto_test_union_suffix_id(const std::string &proto_path,
|
||||
const std::string &proto_file) {
|
||||
void proto_test_union_suffix_id(const std::string& proto_path,
|
||||
const std::string& proto_file) {
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.include_dependence_headers = false;
|
||||
opts.proto_mode = true;
|
||||
@@ -149,9 +149,9 @@ void proto_test_union_suffix_id(const std::string &proto_path,
|
||||
RunTest(opts, proto_path, proto_file, golden_file);
|
||||
}
|
||||
|
||||
void proto_test_include(const std::string &proto_path,
|
||||
const std::string &proto_file,
|
||||
const std::string &import_proto_file) {
|
||||
void proto_test_include(const std::string& proto_path,
|
||||
const std::string& proto_file,
|
||||
const std::string& import_proto_file) {
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.include_dependence_headers = true;
|
||||
opts.proto_mode = true;
|
||||
@@ -166,9 +166,9 @@ void proto_test_include(const std::string &proto_path,
|
||||
RunTest(opts, proto_path, proto_file, golden_file, import_proto_file);
|
||||
}
|
||||
|
||||
void proto_test_include_id(const std::string &proto_path,
|
||||
const std::string &proto_file,
|
||||
const std::string &import_proto_file) {
|
||||
void proto_test_include_id(const std::string& proto_path,
|
||||
const std::string& proto_file,
|
||||
const std::string& import_proto_file) {
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.include_dependence_headers = true;
|
||||
opts.proto_mode = true;
|
||||
@@ -184,9 +184,9 @@ void proto_test_include_id(const std::string &proto_path,
|
||||
RunTest(opts, proto_path, proto_file, golden_file, import_proto_file);
|
||||
}
|
||||
|
||||
void proto_test_include_union(const std::string &proto_path,
|
||||
const std::string &proto_file,
|
||||
const std::string &import_proto_file) {
|
||||
void proto_test_include_union(const std::string& proto_path,
|
||||
const std::string& proto_file,
|
||||
const std::string& import_proto_file) {
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.include_dependence_headers = true;
|
||||
opts.proto_mode = true;
|
||||
@@ -202,9 +202,9 @@ void proto_test_include_union(const std::string &proto_path,
|
||||
RunTest(opts, proto_path, proto_file, golden_file, import_proto_file);
|
||||
}
|
||||
|
||||
void proto_test_include_union_id(const std::string &proto_path,
|
||||
const std::string &proto_file,
|
||||
const std::string &import_proto_file) {
|
||||
void proto_test_include_union_id(const std::string& proto_path,
|
||||
const std::string& proto_file,
|
||||
const std::string& import_proto_file) {
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.include_dependence_headers = true;
|
||||
opts.proto_mode = true;
|
||||
@@ -221,8 +221,8 @@ void proto_test_include_union_id(const std::string &proto_path,
|
||||
RunTest(opts, proto_path, proto_file, golden_file, import_proto_file);
|
||||
}
|
||||
|
||||
void ParseCorruptedProto(const std::string &proto_path) {
|
||||
const char *include_directories[] = { proto_path.c_str(), nullptr };
|
||||
void ParseCorruptedProto(const std::string& proto_path) {
|
||||
const char* include_directories[] = {proto_path.c_str(), nullptr};
|
||||
|
||||
flatbuffers::IDLOptions opts;
|
||||
opts.include_dependence_headers = true;
|
||||
@@ -282,7 +282,7 @@ void ParseCorruptedProto(const std::string &proto_path) {
|
||||
}
|
||||
|
||||
// Parse a .proto schema, output as .fbs
|
||||
void ParseProtoTest(const std::string &tests_data_path) {
|
||||
void ParseProtoTest(const std::string& tests_data_path) {
|
||||
auto proto_path = tests_data_path + "prototest/";
|
||||
std::string proto_file;
|
||||
TEST_EQ(
|
||||
@@ -325,8 +325,7 @@ void ParseProtoBufAsciiTest() {
|
||||
// Similarly, in text output, it should omit these.
|
||||
std::string text;
|
||||
auto err =
|
||||
flatbuffers::GenText(
|
||||
parser, parser.builder_.GetBufferPointer(), &text);
|
||||
flatbuffers::GenText(parser, parser.builder_.GetBufferPointer(), &text);
|
||||
TEST_NULL(err);
|
||||
TEST_EQ_STR(text.c_str(),
|
||||
"{\n A [\n 1\n 2\n ]\n C {\n B: 2\n }\n}\n");
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "reflection_test.h"
|
||||
|
||||
#include "tests/arrays_test_generated.h"
|
||||
#include "flatbuffers/minireflect.h"
|
||||
#include "flatbuffers/reflection.h"
|
||||
#include "flatbuffers/reflection_generated.h"
|
||||
@@ -8,13 +7,14 @@
|
||||
#include "monster_test.h"
|
||||
#include "monster_test_generated.h"
|
||||
#include "test_assert.h"
|
||||
#include "tests/arrays_test_generated.h"
|
||||
|
||||
namespace flatbuffers {
|
||||
namespace tests {
|
||||
|
||||
using namespace MyGame::Example;
|
||||
|
||||
void ReflectionTest(const std::string &tests_data_path, uint8_t *flatbuf,
|
||||
void ReflectionTest(const std::string& tests_data_path, uint8_t* flatbuf,
|
||||
size_t length) {
|
||||
// Load a binary schema.
|
||||
std::string bfbsfile;
|
||||
@@ -24,11 +24,11 @@ void ReflectionTest(const std::string &tests_data_path, uint8_t *flatbuf,
|
||||
|
||||
// Verify it, just in case:
|
||||
flatbuffers::Verifier verifier(
|
||||
reinterpret_cast<const uint8_t *>(bfbsfile.c_str()), bfbsfile.length());
|
||||
reinterpret_cast<const uint8_t*>(bfbsfile.c_str()), bfbsfile.length());
|
||||
TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
|
||||
|
||||
// Make sure the schema is what we expect it to be.
|
||||
auto &schema = *reflection::GetSchema(bfbsfile.c_str());
|
||||
auto& schema = *reflection::GetSchema(bfbsfile.c_str());
|
||||
auto root_table = schema.root_table();
|
||||
|
||||
// Check the declaration files.
|
||||
@@ -83,7 +83,7 @@ void ReflectionTest(const std::string &tests_data_path, uint8_t *flatbuf,
|
||||
auto fields = root_table->fields();
|
||||
auto hp_field_ptr = fields->LookupByKey("hp");
|
||||
TEST_NOTNULL(hp_field_ptr);
|
||||
auto &hp_field = *hp_field_ptr;
|
||||
auto& hp_field = *hp_field_ptr;
|
||||
TEST_EQ_STR(hp_field.name()->c_str(), "hp");
|
||||
TEST_EQ(hp_field.id(), 2);
|
||||
TEST_EQ(hp_field.type()->base_type(), reflection::Short);
|
||||
@@ -108,7 +108,7 @@ void ReflectionTest(const std::string &tests_data_path, uint8_t *flatbuf,
|
||||
TEST_EQ(fields->LookupByKey("name")->optional(), false);
|
||||
|
||||
// Now use it to dynamically access a buffer.
|
||||
auto &root = *flatbuffers::GetAnyRoot(flatbuf);
|
||||
auto& root = *flatbuffers::GetAnyRoot(flatbuf);
|
||||
|
||||
// Verify the buffer first using reflection based verification
|
||||
TEST_EQ(flatbuffers::Verify(schema, *schema.root_table(), flatbuf, length),
|
||||
@@ -169,7 +169,7 @@ void ReflectionTest(const std::string &tests_data_path, uint8_t *flatbuf,
|
||||
// First we put the FlatBuffer inside an std::vector.
|
||||
std::vector<uint8_t> resizingbuf(flatbuf, flatbuf + length);
|
||||
// Find the field we want to modify.
|
||||
auto &name_field = *fields->LookupByKey("name");
|
||||
auto& name_field = *fields->LookupByKey("name");
|
||||
// Get the root.
|
||||
// This time we wrap the result from GetAnyRoot in a smartpointer that
|
||||
// will keep rroot valid as resizingbuf resizes.
|
||||
@@ -180,7 +180,7 @@ void ReflectionTest(const std::string &tests_data_path, uint8_t *flatbuf,
|
||||
// Here resizingbuf has changed, but rroot is still valid.
|
||||
TEST_EQ_STR(GetFieldS(**rroot, name_field)->c_str(), "totally new string");
|
||||
// Now lets extend a vector by 100 elements (10 -> 110).
|
||||
auto &inventory_field = *fields->LookupByKey("inventory");
|
||||
auto& inventory_field = *fields->LookupByKey("inventory");
|
||||
auto rinventory = flatbuffers::piv(
|
||||
flatbuffers::GetFieldV<uint8_t>(**rroot, inventory_field), resizingbuf);
|
||||
flatbuffers::ResizeVector<uint8_t>(schema, 110, 50, *rinventory,
|
||||
@@ -193,7 +193,7 @@ void ReflectionTest(const std::string &tests_data_path, uint8_t *flatbuf,
|
||||
// FlatBuffer of its own, then add that to an existing FlatBuffer:
|
||||
// As an example, let's add a string to an array of strings.
|
||||
// First, find our field:
|
||||
auto &testarrayofstring_field = *fields->LookupByKey("testarrayofstring");
|
||||
auto& testarrayofstring_field = *fields->LookupByKey("testarrayofstring");
|
||||
// Find the vector value:
|
||||
auto rtestarrayofstring = flatbuffers::piv(
|
||||
flatbuffers::GetFieldV<flatbuffers::Offset<flatbuffers::String>>(
|
||||
@@ -218,8 +218,7 @@ void ReflectionTest(const std::string &tests_data_path, uint8_t *flatbuf,
|
||||
TEST_EQ_STR(rtestarrayofstring->Get(2)->c_str(), "hank");
|
||||
// Test integrity of all resize operations above.
|
||||
flatbuffers::Verifier resize_verifier(
|
||||
reinterpret_cast<const uint8_t *>(resizingbuf.data()),
|
||||
resizingbuf.size());
|
||||
reinterpret_cast<const uint8_t*>(resizingbuf.data()), resizingbuf.size());
|
||||
TEST_EQ(VerifyMonsterBuffer(resize_verifier), true);
|
||||
|
||||
// Test buffer is valid using reflection as well
|
||||
@@ -249,7 +248,7 @@ void ReflectionTest(const std::string &tests_data_path, uint8_t *flatbuf,
|
||||
true);
|
||||
}
|
||||
|
||||
void MiniReflectFlatBuffersTest(uint8_t *flatbuf) {
|
||||
void MiniReflectFlatBuffersTest(uint8_t* flatbuf) {
|
||||
auto s =
|
||||
flatbuffers::FlatBufferToString(flatbuf, Monster::MiniReflectTypeTable());
|
||||
TEST_EQ_STR(
|
||||
|
||||
311
tests/test.cpp
311
tests/test.cpp
@@ -47,7 +47,7 @@
|
||||
#include "tests/union_vector/union_vector_generated.h"
|
||||
#include "union_underlying_type_test_generated.h"
|
||||
#if !defined(_MSC_VER) || _MSC_VER >= 1700
|
||||
# include "tests/arrays_test_generated.h"
|
||||
#include "tests/arrays_test_generated.h"
|
||||
#endif
|
||||
#if INCLUDE_64_BIT_TESTS
|
||||
#include "tests/64bit/offset64_test.h"
|
||||
@@ -97,7 +97,7 @@ void TriviallyCopyableTest() {
|
||||
|
||||
// Guard against -Wunused-function on platforms without file tests.
|
||||
#ifndef FLATBUFFERS_NO_FILE_TESTS
|
||||
void GenerateTableTextTest(const std::string &tests_data_path) {
|
||||
void GenerateTableTextTest(const std::string& tests_data_path) {
|
||||
std::string schemafile;
|
||||
std::string jsonfile;
|
||||
bool ok =
|
||||
@@ -108,8 +108,8 @@ void GenerateTableTextTest(const std::string &tests_data_path) {
|
||||
TEST_EQ(ok, true);
|
||||
auto include_test_path =
|
||||
flatbuffers::ConCatPathFileName(tests_data_path, "include_test");
|
||||
const char *include_directories[] = { tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr };
|
||||
const char* include_directories[] = {tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr};
|
||||
flatbuffers::IDLOptions opt;
|
||||
opt.indent_step = -1;
|
||||
flatbuffers::Parser parser(opt);
|
||||
@@ -117,7 +117,7 @@ void GenerateTableTextTest(const std::string &tests_data_path) {
|
||||
parser.Parse(jsonfile.c_str(), include_directories);
|
||||
TEST_EQ(ok, true);
|
||||
// Test root table
|
||||
const Monster *monster = GetMonster(parser.builder_.GetBufferPointer());
|
||||
const Monster* monster = GetMonster(parser.builder_.GetBufferPointer());
|
||||
const auto abilities = monster->testarrayofsortedstruct();
|
||||
TEST_EQ(abilities->size(), 3);
|
||||
TEST_EQ(abilities->Get(0)->id(), 0);
|
||||
@@ -128,35 +128,33 @@ void GenerateTableTextTest(const std::string &tests_data_path) {
|
||||
TEST_EQ(abilities->Get(2)->distance(), 12);
|
||||
|
||||
std::string jsongen;
|
||||
auto result = GenTextFromTable(parser, monster, "MyGame.Example.Monster",
|
||||
&jsongen);
|
||||
auto result =
|
||||
GenTextFromTable(parser, monster, "MyGame.Example.Monster", &jsongen);
|
||||
TEST_NULL(result);
|
||||
// Test sub table
|
||||
const Vec3 *pos = monster->pos();
|
||||
const Vec3* pos = monster->pos();
|
||||
jsongen.clear();
|
||||
result = GenTextFromTable(parser, pos, "MyGame.Example.Vec3", &jsongen);
|
||||
TEST_NULL(result);
|
||||
TEST_EQ_STR(
|
||||
jsongen.c_str(),
|
||||
"{x: 1.0,y: 2.0,z: 3.0,test1: 3.0,test2: \"Green\",test3: {a: 5,b: 6}}");
|
||||
const Test &test3 = pos->test3();
|
||||
const Test& test3 = pos->test3();
|
||||
jsongen.clear();
|
||||
result =
|
||||
GenTextFromTable(parser, &test3, "MyGame.Example.Test", &jsongen);
|
||||
result = GenTextFromTable(parser, &test3, "MyGame.Example.Test", &jsongen);
|
||||
TEST_NULL(result);
|
||||
TEST_EQ_STR(jsongen.c_str(), "{a: 5,b: 6}");
|
||||
const Test *test4 = monster->test4()->Get(0);
|
||||
const Test* test4 = monster->test4()->Get(0);
|
||||
jsongen.clear();
|
||||
result =
|
||||
GenTextFromTable(parser, test4, "MyGame.Example.Test", &jsongen);
|
||||
result = GenTextFromTable(parser, test4, "MyGame.Example.Test", &jsongen);
|
||||
TEST_NULL(result);
|
||||
TEST_EQ_STR(jsongen.c_str(), "{a: 10,b: 20}");
|
||||
}
|
||||
|
||||
void MultiFileNameClashTest(const std::string &tests_data_path) {
|
||||
void MultiFileNameClashTest(const std::string& tests_data_path) {
|
||||
const auto name_clash_path =
|
||||
flatbuffers::ConCatPathFileName(tests_data_path, "name_clash_test");
|
||||
const char *include_directories[] = { name_clash_path.c_str() };
|
||||
const char* include_directories[] = {name_clash_path.c_str()};
|
||||
|
||||
// Load valid 2 file Flatbuffer schema
|
||||
const auto valid_path =
|
||||
@@ -182,7 +180,7 @@ void MultiFileNameClashTest(const std::string &tests_data_path) {
|
||||
false);
|
||||
}
|
||||
|
||||
void InvalidNestedFlatbufferTest(const std::string &tests_data_path) {
|
||||
void InvalidNestedFlatbufferTest(const std::string& tests_data_path) {
|
||||
// First, load and parse FlatBuffer schema (.fbs)
|
||||
std::string schemafile;
|
||||
TEST_EQ(flatbuffers::LoadFile((tests_data_path + "monster_test.fbs").c_str(),
|
||||
@@ -190,8 +188,8 @@ void InvalidNestedFlatbufferTest(const std::string &tests_data_path) {
|
||||
true);
|
||||
auto include_test_path =
|
||||
flatbuffers::ConCatPathFileName(tests_data_path, "include_test");
|
||||
const char *include_directories[] = { tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr };
|
||||
const char* include_directories[] = {tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr};
|
||||
flatbuffers::Parser parser1;
|
||||
TEST_EQ(parser1.Parse(schemafile.c_str(), include_directories), true);
|
||||
|
||||
@@ -201,7 +199,7 @@ void InvalidNestedFlatbufferTest(const std::string &tests_data_path) {
|
||||
false);
|
||||
}
|
||||
|
||||
void UnionVectorTest(const std::string &tests_data_path) {
|
||||
void UnionVectorTest(const std::string& tests_data_path) {
|
||||
// load FlatBuffer fbs schema and json.
|
||||
std::string schemafile, jsonfile;
|
||||
TEST_EQ(flatbuffers::LoadFile(
|
||||
@@ -249,7 +247,7 @@ void UnionVectorTest(const std::string &tests_data_path) {
|
||||
|
||||
auto flat_movie = GetMovie(fbb.GetBufferPointer());
|
||||
|
||||
auto TestMovie = [](const Movie *movie) {
|
||||
auto TestMovie = [](const Movie* movie) {
|
||||
TEST_EQ(movie->main_character_type() == Character_Rapunzel, true);
|
||||
|
||||
auto cts = movie->characters_type();
|
||||
@@ -405,7 +403,7 @@ void EndianSwapTest() {
|
||||
void UninitializedVectorTest() {
|
||||
flatbuffers::FlatBufferBuilder builder;
|
||||
|
||||
Test *buf = nullptr;
|
||||
Test* buf = nullptr;
|
||||
auto vector_offset =
|
||||
builder.CreateUninitializedVectorOfStructs<Test>(2, &buf);
|
||||
TEST_NOTNULL(buf);
|
||||
@@ -550,8 +548,8 @@ void CreateSharedStringTest() {
|
||||
TEST_EQ(onetwo.o != two.o, true);
|
||||
|
||||
// Support for embedded nulls
|
||||
const char chars_b[] = { 'a', '\0', 'b' };
|
||||
const char chars_c[] = { 'a', '\0', 'c' };
|
||||
const char chars_b[] = {'a', '\0', 'b'};
|
||||
const char chars_c[] = {'a', '\0', 'c'};
|
||||
const auto null_b1 = builder.CreateSharedString(chars_b, sizeof(chars_b));
|
||||
const auto null_c = builder.CreateSharedString(chars_c, sizeof(chars_c));
|
||||
const auto null_b2 = builder.CreateSharedString(chars_b, sizeof(chars_b));
|
||||
@@ -560,8 +558,7 @@ void CreateSharedStringTest() {
|
||||
|
||||
// Put the strings into an array for round trip verification.
|
||||
std::array<flatbuffers::Offset<flatbuffers::String>, 7> array = {
|
||||
one1, two, one2, onetwo, null_b1, null_c, null_b2
|
||||
};
|
||||
one1, two, one2, onetwo, null_b1, null_c, null_b2};
|
||||
const auto vector_offset =
|
||||
builder.CreateVector<flatbuffers::Offset<flatbuffers::String>>(array);
|
||||
MonsterBuilder monster_builder(builder);
|
||||
@@ -570,12 +567,12 @@ void CreateSharedStringTest() {
|
||||
builder.Finish(monster_builder.Finish());
|
||||
|
||||
// Read the Monster back.
|
||||
const auto *monster =
|
||||
const auto* monster =
|
||||
flatbuffers::GetRoot<Monster>(builder.GetBufferPointer());
|
||||
TEST_EQ_STR(monster->name()->c_str(), "two");
|
||||
const auto *testarrayofstring = monster->testarrayofstring();
|
||||
const auto* testarrayofstring = monster->testarrayofstring();
|
||||
TEST_EQ(testarrayofstring->size(), flatbuffers::uoffset_t(7));
|
||||
const auto &a = *testarrayofstring;
|
||||
const auto& a = *testarrayofstring;
|
||||
TEST_EQ_STR(a[0]->c_str(), "one");
|
||||
TEST_EQ_STR(a[1]->c_str(), "two");
|
||||
TEST_EQ_STR(a[2]->c_str(), "one");
|
||||
@@ -617,7 +614,7 @@ void FlatbuffersSpanTest() {
|
||||
TEST_EQ(c3.size(), 0);
|
||||
TEST_ASSERT(c1.empty() && c2.empty() && c3.empty());
|
||||
|
||||
int i_data7[7] = { 0, 1, 2, 3, 4, 5, 6 };
|
||||
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);
|
||||
@@ -634,7 +631,7 @@ void FlatbuffersSpanTest() {
|
||||
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 };
|
||||
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);
|
||||
@@ -646,32 +643,32 @@ void FlatbuffersSpanTest() {
|
||||
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 } };
|
||||
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 } };
|
||||
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);
|
||||
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 };
|
||||
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);
|
||||
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);
|
||||
}
|
||||
@@ -771,18 +768,20 @@ void FixedLengthArrayTest() {
|
||||
const size_t arr_size = sizeof(MyGame::Example::ArrayStruct);
|
||||
char non_zero_memory[arr_size];
|
||||
// set memory chunk of size ArrayStruct to 1's
|
||||
std::memset(static_cast<void *>(non_zero_memory), 1, arr_size);
|
||||
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 =
|
||||
#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)
|
||||
#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);
|
||||
}
|
||||
}
|
||||
#else
|
||||
void FixedLengthArrayTest() {}
|
||||
@@ -791,30 +790,29 @@ void FixedLengthArrayTest() {}
|
||||
#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 };
|
||||
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::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 } },
|
||||
{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);
|
||||
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);
|
||||
@@ -822,7 +820,7 @@ void FixedLengthArrayConstructorTest() {
|
||||
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);
|
||||
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);
|
||||
@@ -858,48 +856,46 @@ void FixedLengthArrayConstructorTest() {}
|
||||
#endif
|
||||
|
||||
void FixedLengthArrayOperatorEqualTest() {
|
||||
const int32_t nested_a[2] = { 1, 2 };
|
||||
MyGame::Example::TestEnum nested_c[2] = { MyGame::Example::TestEnum::A,
|
||||
MyGame::Example::TestEnum::B };
|
||||
const int32_t nested_a[2] = {1, 2};
|
||||
MyGame::Example::TestEnum nested_c[2] = {MyGame::Example::TestEnum::A,
|
||||
MyGame::Example::TestEnum::B};
|
||||
|
||||
MyGame::Example::TestEnum nested_cc[2] = { MyGame::Example::TestEnum::A,
|
||||
MyGame::Example::TestEnum::C };
|
||||
const int64_t int64_2[2] = { -2, -1 };
|
||||
MyGame::Example::TestEnum nested_cc[2] = {MyGame::Example::TestEnum::A,
|
||||
MyGame::Example::TestEnum::C};
|
||||
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::B,
|
||||
nested_c,
|
||||
std::array<int64_t, 2>{ { -2, -1 } }) }
|
||||
};
|
||||
{MyGame::Example::NestedStruct(nested_a, MyGame::Example::TestEnum::B,
|
||||
nested_c, int64_2),
|
||||
MyGame::Example::NestedStruct(nested_a, MyGame::Example::TestEnum::B,
|
||||
nested_c,
|
||||
std::array<int64_t, 2>{{-2, -1}})}};
|
||||
|
||||
auto different = MyGame::Example::NestedStruct(
|
||||
nested_a, MyGame::Example::TestEnum::B, nested_cc,
|
||||
std::array<int64_t, 2>{ { -2, -1 } });
|
||||
std::array<int64_t, 2>{{-2, -1}});
|
||||
|
||||
TEST_ASSERT(init_d[0] == init_d[1]);
|
||||
TEST_ASSERT(init_d[0] != different);
|
||||
|
||||
std::array<MyGame::Example::ArrayStruct, 3> arr_struct = {
|
||||
MyGame::Example::ArrayStruct(
|
||||
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),
|
||||
MyGame::Example::ArrayStruct(
|
||||
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),
|
||||
|
||||
MyGame::Example::ArrayStruct(
|
||||
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),
|
||||
MyGame::Example::ArrayStruct(
|
||||
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),
|
||||
|
||||
MyGame::Example::ArrayStruct(
|
||||
8.125,
|
||||
std::array<int32_t, 0xF>{
|
||||
{ 1000, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } },
|
||||
-17, init_d, 10, int64_2)
|
||||
};
|
||||
MyGame::Example::ArrayStruct(
|
||||
8.125,
|
||||
std::array<int32_t, 0xF>{
|
||||
{1000, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
|
||||
-17, init_d, 10, int64_2)};
|
||||
|
||||
TEST_ASSERT(arr_struct[0] == arr_struct[1]);
|
||||
TEST_ASSERT(arr_struct[1] != arr_struct[2]);
|
||||
@@ -935,12 +931,12 @@ void NativeTypeTest() {
|
||||
TEST_EQ(dstDataT->position_inline.z, 6.0f);
|
||||
|
||||
for (int i = 0; i < N; ++i) {
|
||||
const Native::Vector3D &v = dstDataT->vectors[i];
|
||||
const Native::Vector3D& v = dstDataT->vectors[i];
|
||||
TEST_EQ(v.x, 10 * i + 0.1f);
|
||||
TEST_EQ(v.y, 10 * i + 0.2f);
|
||||
TEST_EQ(v.z, 10 * i + 0.3f);
|
||||
|
||||
const Native::Vector3D &v2 = dstDataT->vectors_alt[i];
|
||||
const Native::Vector3D& v2 = dstDataT->vectors_alt[i];
|
||||
TEST_EQ(v2.x, 20 * i + 0.1f);
|
||||
TEST_EQ(v2.y, 20 * i + 0.2f);
|
||||
TEST_EQ(v2.z, 20 * i + 0.3f);
|
||||
@@ -950,8 +946,8 @@ void NativeTypeTest() {
|
||||
// Guard against -Wunused-function on platforms without file tests.
|
||||
#ifndef FLATBUFFERS_NO_FILE_TESTS
|
||||
// VS10 does not support typed enums, exclude from tests
|
||||
# if !defined(_MSC_VER) || _MSC_VER >= 1700
|
||||
void FixedLengthArrayJsonTest(const std::string &tests_data_path, bool binary) {
|
||||
#if !defined(_MSC_VER) || _MSC_VER >= 1700
|
||||
void FixedLengthArrayJsonTest(const std::string& tests_data_path, bool binary) {
|
||||
// load FlatBuffer schema (.fbs) and JSON from disk
|
||||
std::string schemafile;
|
||||
std::string jsonfile;
|
||||
@@ -969,15 +965,15 @@ void FixedLengthArrayJsonTest(const std::string &tests_data_path, bool binary) {
|
||||
flatbuffers::Parser parserOrg, parserGen;
|
||||
if (binary) {
|
||||
flatbuffers::Verifier verifier(
|
||||
reinterpret_cast<const uint8_t *>(schemafile.c_str()),
|
||||
reinterpret_cast<const uint8_t*>(schemafile.c_str()),
|
||||
schemafile.size());
|
||||
TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
|
||||
TEST_EQ(parserOrg.Deserialize(
|
||||
reinterpret_cast<const uint8_t *>(schemafile.c_str()),
|
||||
reinterpret_cast<const uint8_t*>(schemafile.c_str()),
|
||||
schemafile.size()),
|
||||
true);
|
||||
TEST_EQ(parserGen.Deserialize(
|
||||
reinterpret_cast<const uint8_t *>(schemafile.c_str()),
|
||||
reinterpret_cast<const uint8_t*>(schemafile.c_str()),
|
||||
schemafile.size()),
|
||||
true);
|
||||
} else {
|
||||
@@ -1012,7 +1008,7 @@ void FixedLengthArrayJsonTest(const std::string &tests_data_path, bool binary) {
|
||||
0);
|
||||
}
|
||||
|
||||
void FixedLengthArraySpanTest(const std::string &tests_data_path) {
|
||||
void FixedLengthArraySpanTest(const std::string& tests_data_path) {
|
||||
// load FlatBuffer schema (.fbs) and JSON from disk
|
||||
std::string schemafile;
|
||||
std::string jsonfile;
|
||||
@@ -1028,7 +1024,7 @@ void FixedLengthArraySpanTest(const std::string &tests_data_path) {
|
||||
flatbuffers::Parser parser;
|
||||
TEST_EQ(parser.Parse(schemafile.c_str()), true);
|
||||
TEST_EQ(parser.Parse(jsonfile.c_str()), true);
|
||||
auto &fbb = parser.builder_;
|
||||
auto& fbb = parser.builder_;
|
||||
auto verifier = flatbuffers::Verifier(fbb.GetBufferPointer(), fbb.GetSize());
|
||||
TEST_EQ(true, VerifyArrayTableBuffer(verifier));
|
||||
|
||||
@@ -1048,8 +1044,8 @@ void FixedLengthArraySpanTest(const std::string &tests_data_path) {
|
||||
TEST_ASSERT(const_d[1] == mutable_d[1]);
|
||||
mutable_d[0] = const_d[0]; // mutate
|
||||
// test scalars
|
||||
auto &const_nested = const_d[0];
|
||||
auto &mutable_nested = mutable_d[0];
|
||||
auto& const_nested = const_d[0];
|
||||
auto& mutable_nested = mutable_d[0];
|
||||
static_assert(sizeof(MyGame::Example::TestEnum) == sizeof(uint8_t),
|
||||
"TestEnum's underlaying type must by byte");
|
||||
TEST_NOTNULL(const_nested.d());
|
||||
@@ -1069,7 +1065,7 @@ void FixedLengthArraySpanTest(const std::string &tests_data_path) {
|
||||
std::equal(const_d_c.begin(), const_d_c.end(), mutable_d_c.begin()));
|
||||
}
|
||||
// test little endian array of int32
|
||||
# if FLATBUFFERS_LITTLEENDIAN
|
||||
#if FLATBUFFERS_LITTLEENDIAN
|
||||
{
|
||||
flatbuffers::span<const int32_t, 2> const_d_a =
|
||||
flatbuffers::make_span(*const_nested.a());
|
||||
@@ -1084,14 +1080,14 @@ void FixedLengthArraySpanTest(const std::string &tests_data_path) {
|
||||
TEST_ASSERT(
|
||||
std::equal(const_d_a.begin(), const_d_a.end(), mutable_d_a.begin()));
|
||||
}
|
||||
# endif
|
||||
#endif
|
||||
}
|
||||
# else
|
||||
#else
|
||||
void FixedLengthArrayJsonTest(bool /*binary*/) {}
|
||||
void FixedLengthArraySpanTest() {}
|
||||
# endif
|
||||
#endif
|
||||
|
||||
void TestEmbeddedBinarySchema(const std::string &tests_data_path) {
|
||||
void TestEmbeddedBinarySchema(const std::string& tests_data_path) {
|
||||
// load JSON from disk
|
||||
std::string jsonfile;
|
||||
TEST_EQ(flatbuffers::LoadFile(
|
||||
@@ -1139,7 +1135,8 @@ void TestEmbeddedBinarySchema(const std::string &tests_data_path) {
|
||||
}
|
||||
#endif
|
||||
|
||||
template<typename T> void EmbeddedSchemaAccessByType() {
|
||||
template <typename T>
|
||||
void EmbeddedSchemaAccessByType() {
|
||||
// Get the binary schema from the Type itself.
|
||||
// Verify the schema is OK.
|
||||
flatbuffers::Verifier verifierEmbeddedSchema(
|
||||
@@ -1240,7 +1237,7 @@ void NestedVerifierTest() {
|
||||
|
||||
// Purposely invalidate the nested flatbuffer setting its length to 0, an
|
||||
// invalid length.
|
||||
uint8_t *invalid_nested_buffer = nullptr;
|
||||
uint8_t* invalid_nested_buffer = nullptr;
|
||||
auto nested_monster_bytes = builder.CreateVector(invalid_nested_buffer, 0);
|
||||
|
||||
auto name = builder.CreateString("OuterMonster");
|
||||
@@ -1257,8 +1254,8 @@ void NestedVerifierTest() {
|
||||
}
|
||||
}
|
||||
|
||||
template<class T, class Container>
|
||||
void TestIterators(const std::vector<T> &expected, const Container &tested) {
|
||||
template <class T, class Container>
|
||||
void TestIterators(const std::vector<T>& expected, const Container& tested) {
|
||||
TEST_ASSERT(tested.rbegin().base() == tested.end());
|
||||
TEST_ASSERT(tested.crbegin().base() == tested.cend());
|
||||
TEST_ASSERT(tested.rend().base() == tested.begin());
|
||||
@@ -1266,14 +1263,14 @@ void TestIterators(const std::vector<T> &expected, const Container &tested) {
|
||||
|
||||
size_t k = 0;
|
||||
for (auto it = tested.begin(); it != tested.end(); ++it, ++k) {
|
||||
const auto &e = expected.at(k);
|
||||
const auto& e = expected.at(k);
|
||||
TEST_EQ(*it, e);
|
||||
}
|
||||
TEST_EQ(k, expected.size());
|
||||
|
||||
k = expected.size();
|
||||
for (auto it = tested.rbegin(); it != tested.rend(); ++it, --k) {
|
||||
const auto &e = expected.at(k - 1);
|
||||
const auto& e = expected.at(k - 1);
|
||||
TEST_EQ(*it, e);
|
||||
}
|
||||
TEST_EQ(k, 0);
|
||||
@@ -1282,19 +1279,19 @@ void TestIterators(const std::vector<T> &expected, const Container &tested) {
|
||||
void FlatbuffersIteratorsTest() {
|
||||
{
|
||||
flatbuffers::FlatBufferBuilder fbb;
|
||||
const std::vector<unsigned char> inv_data = { 1, 2, 3 };
|
||||
const std::vector<unsigned char> inv_data = {1, 2, 3};
|
||||
{
|
||||
auto mon_name = fbb.CreateString("MyMonster"); // key, mandatory
|
||||
auto inv_vec = fbb.CreateVector(inv_data);
|
||||
auto empty_i64_vec =
|
||||
fbb.CreateVector(static_cast<const int64_t *>(nullptr), 0);
|
||||
fbb.CreateVector(static_cast<const int64_t*>(nullptr), 0);
|
||||
MonsterBuilder mb(fbb);
|
||||
mb.add_name(mon_name);
|
||||
mb.add_inventory(inv_vec);
|
||||
mb.add_vector_of_longs(empty_i64_vec);
|
||||
FinishMonsterBuffer(fbb, mb.Finish());
|
||||
}
|
||||
const auto &mon = *flatbuffers::GetRoot<Monster>(fbb.GetBufferPointer());
|
||||
const auto& mon = *flatbuffers::GetRoot<Monster>(fbb.GetBufferPointer());
|
||||
|
||||
TEST_EQ_STR("MyMonster", mon.name()->c_str());
|
||||
TEST_ASSERT(mon.inventory());
|
||||
@@ -1308,10 +1305,10 @@ void FlatbuffersIteratorsTest() {
|
||||
MyGame::Example::ArrayStruct aStruct;
|
||||
MyGame::Example::FinishArrayTableBuffer(
|
||||
fbb, MyGame::Example::CreateArrayTable(fbb, &aStruct));
|
||||
const auto &array_table =
|
||||
const auto& array_table =
|
||||
*flatbuffers::GetRoot<ArrayTable>(fbb.GetBufferPointer());
|
||||
TEST_ASSERT(array_table.a());
|
||||
auto &int_15 = *array_table.a()->b();
|
||||
auto& int_15 = *array_table.a()->b();
|
||||
TestIterators(std::vector<int>(15, 0), int_15);
|
||||
}
|
||||
}
|
||||
@@ -1444,7 +1441,7 @@ void VectorSpanTest() {
|
||||
|
||||
auto mloc = CreateMonster(
|
||||
builder, nullptr, 0, 0, builder.CreateString("Monster"),
|
||||
builder.CreateVector<uint8_t>({ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }));
|
||||
builder.CreateVector<uint8_t>({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}));
|
||||
|
||||
FinishMonsterBuffer(builder, mloc);
|
||||
|
||||
@@ -1522,34 +1519,36 @@ void NativeInlineTableVectorTest() {
|
||||
auto offset = TestNativeInlineTable::Pack(fbb, &test);
|
||||
fbb.Finish(offset);
|
||||
|
||||
auto *root =
|
||||
auto* root =
|
||||
flatbuffers::GetRoot<TestNativeInlineTable>(fbb.GetBufferPointer());
|
||||
TestNativeInlineTableT unpacked;
|
||||
root->UnPackTo(&unpacked);
|
||||
|
||||
for (int i = 0; i < 10; ++i) { TEST_ASSERT(unpacked.t[i] == test.t[i]); }
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
TEST_ASSERT(unpacked.t[i] == test.t[i]);
|
||||
}
|
||||
|
||||
TEST_ASSERT(unpacked.t == test.t);
|
||||
}
|
||||
|
||||
// Guard against -Wunused-function on platforms without file tests.
|
||||
#ifndef FLATBUFFERS_NO_FILE_TESTS
|
||||
void DoNotRequireEofTest(const std::string &tests_data_path) {
|
||||
void DoNotRequireEofTest(const std::string& tests_data_path) {
|
||||
std::string schemafile;
|
||||
bool ok = flatbuffers::LoadFile(
|
||||
(tests_data_path + "monster_test.fbs").c_str(), false, &schemafile);
|
||||
TEST_EQ(ok, true);
|
||||
auto include_test_path =
|
||||
flatbuffers::ConCatPathFileName(tests_data_path, "include_test");
|
||||
const char *include_directories[] = { tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr };
|
||||
const char* include_directories[] = {tests_data_path.c_str(),
|
||||
include_test_path.c_str(), nullptr};
|
||||
flatbuffers::IDLOptions opt;
|
||||
opt.require_json_eof = false;
|
||||
flatbuffers::Parser parser(opt);
|
||||
ok = parser.Parse(schemafile.c_str(), include_directories);
|
||||
TEST_EQ(ok, true);
|
||||
|
||||
const char *str = R"(Some text at the beginning. {
|
||||
const char* str = R"(Some text at the beginning. {
|
||||
"name": "Blob",
|
||||
"hp": 5
|
||||
}{
|
||||
@@ -1558,11 +1557,11 @@ void DoNotRequireEofTest(const std::string &tests_data_path) {
|
||||
}
|
||||
Some extra text at the end too.
|
||||
)";
|
||||
const char *tableStart = std::strchr(str, '{');
|
||||
const char* tableStart = std::strchr(str, '{');
|
||||
ok = parser.ParseJson(tableStart);
|
||||
TEST_EQ(ok, true);
|
||||
|
||||
const Monster *monster = GetMonster(parser.builder_.GetBufferPointer());
|
||||
const Monster* monster = GetMonster(parser.builder_.GetBufferPointer());
|
||||
TEST_EQ_STR(monster->name()->c_str(), "Blob");
|
||||
TEST_EQ(monster->hp(), 5);
|
||||
|
||||
@@ -1578,38 +1577,36 @@ void DoNotRequireEofTest(const std::string &tests_data_path) {
|
||||
#endif
|
||||
|
||||
void UnionUnderlyingTypeTest() {
|
||||
using namespace UnionUnderlyingType;
|
||||
TEST_ASSERT(sizeof(ABC) == sizeof(uint32_t));
|
||||
TEST_ASSERT(static_cast<int32_t>(ABC::A) == 555);
|
||||
TEST_ASSERT(static_cast<int32_t>(ABC::B) == 666);
|
||||
TEST_ASSERT(static_cast<int32_t>(ABC::C) == 777);
|
||||
using namespace UnionUnderlyingType;
|
||||
TEST_ASSERT(sizeof(ABC) == sizeof(uint32_t));
|
||||
TEST_ASSERT(static_cast<int32_t>(ABC::A) == 555);
|
||||
TEST_ASSERT(static_cast<int32_t>(ABC::B) == 666);
|
||||
TEST_ASSERT(static_cast<int32_t>(ABC::C) == 777);
|
||||
|
||||
DT buffer;
|
||||
AT a;
|
||||
a.a = 42;
|
||||
BT b;
|
||||
b.b = "foo";
|
||||
CT c;
|
||||
c.c = true;
|
||||
buffer.test_union = ABCUnion();
|
||||
buffer.test_union.Set(a);
|
||||
buffer.test_vector_of_union.resize(3);
|
||||
buffer.test_vector_of_union[0].Set(a);
|
||||
buffer.test_vector_of_union[1].Set(b);
|
||||
buffer.test_vector_of_union[2].Set(c);
|
||||
DT buffer;
|
||||
AT a;
|
||||
a.a = 42;
|
||||
BT b;
|
||||
b.b = "foo";
|
||||
CT c;
|
||||
c.c = true;
|
||||
buffer.test_union = ABCUnion();
|
||||
buffer.test_union.Set(a);
|
||||
buffer.test_vector_of_union.resize(3);
|
||||
buffer.test_vector_of_union[0].Set(a);
|
||||
buffer.test_vector_of_union[1].Set(b);
|
||||
buffer.test_vector_of_union[2].Set(c);
|
||||
|
||||
flatbuffers::FlatBufferBuilder fbb;
|
||||
auto offset = D::Pack(fbb, &buffer);
|
||||
fbb.Finish(offset);
|
||||
flatbuffers::FlatBufferBuilder fbb;
|
||||
auto offset = D::Pack(fbb, &buffer);
|
||||
fbb.Finish(offset);
|
||||
|
||||
auto *root =
|
||||
flatbuffers::GetRoot<D>(fbb.GetBufferPointer());
|
||||
DT unpacked;
|
||||
root->UnPackTo(&unpacked);
|
||||
|
||||
TEST_ASSERT(unpacked.test_union == buffer.test_union);
|
||||
TEST_ASSERT(unpacked.test_vector_of_union == buffer.test_vector_of_union);
|
||||
auto* root = flatbuffers::GetRoot<D>(fbb.GetBufferPointer());
|
||||
DT unpacked;
|
||||
root->UnPackTo(&unpacked);
|
||||
|
||||
TEST_ASSERT(unpacked.test_union == buffer.test_union);
|
||||
TEST_ASSERT(unpacked.test_vector_of_union == buffer.test_vector_of_union);
|
||||
}
|
||||
|
||||
static void Offset64Tests() {
|
||||
@@ -1626,14 +1623,14 @@ static void Offset64Tests() {
|
||||
#endif
|
||||
}
|
||||
|
||||
int FlatBufferTests(const std::string &tests_data_path) {
|
||||
int FlatBufferTests(const std::string& tests_data_path) {
|
||||
// Run our various test suites:
|
||||
|
||||
std::string rawbuf;
|
||||
auto flatbuf1 = CreateFlatBufferTest(rawbuf);
|
||||
auto flatbuf = std::move(flatbuf1); // Test move assignment.
|
||||
|
||||
AccessFlatBufferTest(reinterpret_cast<const uint8_t *>(rawbuf.c_str()),
|
||||
AccessFlatBufferTest(reinterpret_cast<const uint8_t*>(rawbuf.c_str()),
|
||||
rawbuf.length());
|
||||
AccessFlatBufferTest(flatbuf.data(), flatbuf.size());
|
||||
|
||||
@@ -1745,7 +1742,7 @@ int FlatBufferTests(const std::string &tests_data_path) {
|
||||
} // namespace tests
|
||||
} // namespace flatbuffers
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
int main(int argc, const char* argv[]) {
|
||||
std::string tests_data_path = "tests/";
|
||||
|
||||
for (int argi = 1; argi < argc; argi++) {
|
||||
|
||||
@@ -3,15 +3,15 @@
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# include <crtdbg.h>
|
||||
# include <windows.h>
|
||||
#include <crtdbg.h>
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
int testing_fails = 0;
|
||||
static TestFailEventListener fail_listener_ = nullptr;
|
||||
|
||||
void TestFail(const char *expval, const char *val, const char *exp,
|
||||
const char *file, int line, const char *func) {
|
||||
void TestFail(const char* expval, const char* val, const char* exp,
|
||||
const char* file, int line, const char* func) {
|
||||
TEST_OUTPUT_LINE("EXPECTED: \"%s\"", expval);
|
||||
TEST_OUTPUT_LINE("VALUE: \"%s\"", val);
|
||||
TEST_OUTPUT_LINE("TEST FAILED: %s:%d, %s in %s", file, line, exp,
|
||||
@@ -24,8 +24,8 @@ void TestFail(const char *expval, const char *val, const char *exp,
|
||||
assert(0); // ignored in Release if NDEBUG defined
|
||||
}
|
||||
|
||||
void TestEqStr(const char *expval, const char *val, const char *exp,
|
||||
const char *file, int line, const char *func) {
|
||||
void TestEqStr(const char* expval, const char* val, const char* exp,
|
||||
const char* file, int line, const char* func) {
|
||||
if (strcmp(expval, val) != 0) {
|
||||
TestFail(expval, val, exp, file, line, func);
|
||||
}
|
||||
@@ -33,7 +33,7 @@ void TestEqStr(const char *expval, const char *val, const char *exp,
|
||||
|
||||
#if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && defined(_MSC_VER) && \
|
||||
defined(_DEBUG)
|
||||
# define FLATBUFFERS_MEMORY_LEAK_TRACKING_MSVC
|
||||
#define FLATBUFFERS_MEMORY_LEAK_TRACKING_MSVC
|
||||
#endif
|
||||
|
||||
void InitTestEngine(TestFailEventListener listener) {
|
||||
|
||||
@@ -11,16 +11,16 @@ struct OwnedAllocator : public DefaultAllocator {};
|
||||
|
||||
class TestHeapBuilder : public FlatBufferBuilder {
|
||||
private:
|
||||
TestHeapBuilder(const TestHeapBuilder &);
|
||||
TestHeapBuilder &operator=(const TestHeapBuilder &);
|
||||
TestHeapBuilder(const TestHeapBuilder&);
|
||||
TestHeapBuilder& operator=(const TestHeapBuilder&);
|
||||
|
||||
public:
|
||||
TestHeapBuilder() : FlatBufferBuilder(2048, new OwnedAllocator(), true) {}
|
||||
|
||||
TestHeapBuilder(TestHeapBuilder &&other)
|
||||
TestHeapBuilder(TestHeapBuilder&& other)
|
||||
: FlatBufferBuilder(std::move(other)) {}
|
||||
|
||||
TestHeapBuilder &operator=(TestHeapBuilder &&other) {
|
||||
TestHeapBuilder& operator=(TestHeapBuilder&& other) {
|
||||
FlatBufferBuilder::operator=(std::move(other));
|
||||
return *this;
|
||||
}
|
||||
@@ -34,27 +34,27 @@ struct AllocatorMember {
|
||||
struct GrpcLikeMessageBuilder : private AllocatorMember,
|
||||
public FlatBufferBuilder {
|
||||
private:
|
||||
GrpcLikeMessageBuilder(const GrpcLikeMessageBuilder &);
|
||||
GrpcLikeMessageBuilder &operator=(const GrpcLikeMessageBuilder &);
|
||||
GrpcLikeMessageBuilder(const GrpcLikeMessageBuilder&);
|
||||
GrpcLikeMessageBuilder& operator=(const GrpcLikeMessageBuilder&);
|
||||
|
||||
public:
|
||||
GrpcLikeMessageBuilder()
|
||||
: FlatBufferBuilder(1024, &member_allocator_, false) {}
|
||||
|
||||
GrpcLikeMessageBuilder(GrpcLikeMessageBuilder &&other)
|
||||
GrpcLikeMessageBuilder(GrpcLikeMessageBuilder&& other)
|
||||
: FlatBufferBuilder(1024, &member_allocator_, false) {
|
||||
// Default construct and swap idiom.
|
||||
Swap(other);
|
||||
}
|
||||
|
||||
GrpcLikeMessageBuilder &operator=(GrpcLikeMessageBuilder &&other) {
|
||||
GrpcLikeMessageBuilder& operator=(GrpcLikeMessageBuilder&& other) {
|
||||
// Construct temporary and swap idiom
|
||||
GrpcLikeMessageBuilder temp(std::move(other));
|
||||
Swap(temp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Swap(GrpcLikeMessageBuilder &other) {
|
||||
void Swap(GrpcLikeMessageBuilder& other) {
|
||||
// No need to swap member_allocator_ because it's stateless.
|
||||
FlatBufferBuilder::Swap(other);
|
||||
// After swapping the FlatBufferBuilder, we swap back the allocator, which
|
||||
@@ -67,23 +67,23 @@ struct GrpcLikeMessageBuilder : private AllocatorMember,
|
||||
};
|
||||
|
||||
flatbuffers::Offset<Monster> populate1(
|
||||
flatbuffers::FlatBufferBuilder &builder) {
|
||||
flatbuffers::FlatBufferBuilder& builder) {
|
||||
auto name_offset = builder.CreateString(m1_name());
|
||||
return CreateMonster(builder, nullptr, 0, 0, name_offset, 0, m1_color());
|
||||
}
|
||||
|
||||
flatbuffers::Offset<Monster> populate2(
|
||||
flatbuffers::FlatBufferBuilder &builder) {
|
||||
flatbuffers::FlatBufferBuilder& builder) {
|
||||
auto name_offset = builder.CreateString(m2_name());
|
||||
return CreateMonster(builder, nullptr, 0, 0, name_offset, 0, m2_color());
|
||||
}
|
||||
|
||||
uint8_t *release_raw_base(flatbuffers::FlatBufferBuilder &fbb, size_t &size,
|
||||
size_t &offset) {
|
||||
uint8_t* release_raw_base(flatbuffers::FlatBufferBuilder& fbb, size_t& size,
|
||||
size_t& offset) {
|
||||
return fbb.ReleaseRaw(size, offset);
|
||||
}
|
||||
|
||||
void free_raw(flatbuffers::grpc::MessageBuilder &, uint8_t *) {
|
||||
void free_raw(flatbuffers::grpc::MessageBuilder&, uint8_t*) {
|
||||
// release_raw_base calls FlatBufferBuilder::ReleaseRaw on the argument
|
||||
// MessageBuilder. It's semantically wrong as MessageBuilder has its own
|
||||
// ReleaseRaw member function that takes three arguments. In such cases
|
||||
@@ -94,26 +94,26 @@ void free_raw(flatbuffers::grpc::MessageBuilder &, uint8_t *) {
|
||||
// not occur with FlatBufferBuilder.
|
||||
}
|
||||
|
||||
void free_raw(flatbuffers::FlatBufferBuilder &, uint8_t *buf) {
|
||||
void free_raw(flatbuffers::FlatBufferBuilder&, uint8_t* buf) {
|
||||
flatbuffers::DefaultAllocator().deallocate(buf, 0);
|
||||
}
|
||||
|
||||
bool verify(const flatbuffers::DetachedBuffer &buf,
|
||||
const std::string &expected_name, Color color) {
|
||||
const Monster *monster = flatbuffers::GetRoot<Monster>(buf.data());
|
||||
bool verify(const flatbuffers::DetachedBuffer& buf,
|
||||
const std::string& expected_name, Color color) {
|
||||
const Monster* monster = flatbuffers::GetRoot<Monster>(buf.data());
|
||||
return (monster->name()->str() == expected_name) &&
|
||||
(monster->color() == color);
|
||||
}
|
||||
|
||||
bool verify(const uint8_t *buf, size_t offset, const std::string &expected_name,
|
||||
bool verify(const uint8_t* buf, size_t offset, const std::string& expected_name,
|
||||
Color color) {
|
||||
const Monster *monster = flatbuffers::GetRoot<Monster>(buf + offset);
|
||||
const Monster* monster = flatbuffers::GetRoot<Monster>(buf + offset);
|
||||
return (monster->name()->str() == expected_name) &&
|
||||
(monster->color() == color);
|
||||
}
|
||||
|
||||
bool release_n_verify(flatbuffers::FlatBufferBuilder &fbb,
|
||||
const std::string &expected_name, Color color) {
|
||||
bool release_n_verify(flatbuffers::FlatBufferBuilder& fbb,
|
||||
const std::string& expected_name, Color color) {
|
||||
flatbuffers::DetachedBuffer buf = fbb.Release();
|
||||
return verify(buf, expected_name, color);
|
||||
}
|
||||
@@ -129,10 +129,9 @@ void FlatBufferBuilderTest() {
|
||||
BuilderTests<GrpcLikeMessageBuilder>::all_tests();
|
||||
|
||||
BuilderReuseTestSelector tests[4] = {
|
||||
REUSABLE_AFTER_RELEASE, REUSABLE_AFTER_RELEASE_RAW,
|
||||
REUSABLE_AFTER_RELEASE_AND_MOVE_ASSIGN,
|
||||
REUSABLE_AFTER_RELEASE_RAW_AND_MOVE_ASSIGN
|
||||
};
|
||||
REUSABLE_AFTER_RELEASE, REUSABLE_AFTER_RELEASE_RAW,
|
||||
REUSABLE_AFTER_RELEASE_AND_MOVE_ASSIGN,
|
||||
REUSABLE_AFTER_RELEASE_RAW_AND_MOVE_ASSIGN};
|
||||
|
||||
BuilderReuseTests<FlatBufferBuilder, FlatBufferBuilder>::run_tests(
|
||||
TestSelector(tests, tests + 4));
|
||||
@@ -143,7 +142,7 @@ void FlatBufferBuilderTest() {
|
||||
}
|
||||
|
||||
// forward-declared in test_builder.h
|
||||
void CheckTestGeneratedIsValid(const MyGame::Example::Color &);
|
||||
void CheckTestGeneratedIsValid(const MyGame::Example::Color&);
|
||||
|
||||
// Link-time check using pointer type.
|
||||
void CheckTestGeneratedIsValid(const MyGame::Example::Color &) {}
|
||||
void CheckTestGeneratedIsValid(const MyGame::Example::Color&) {}
|
||||
|
||||
@@ -7,8 +7,8 @@ namespace flatbuffers {
|
||||
namespace tests {
|
||||
namespace {
|
||||
|
||||
template<typename T>
|
||||
void NumericUtilsTestInteger(const char *lower, const char *upper) {
|
||||
template <typename T>
|
||||
void NumericUtilsTestInteger(const char* lower, const char* upper) {
|
||||
T x;
|
||||
TEST_EQ(flatbuffers::StringToNumber("1q", &x), false);
|
||||
TEST_EQ(x, 0);
|
||||
@@ -21,8 +21,8 @@ void NumericUtilsTestInteger(const char *lower, const char *upper) {
|
||||
TEST_EQ(x, expval);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void NumericUtilsTestFloat(const char *lower, const char *upper) {
|
||||
template <typename T>
|
||||
void NumericUtilsTestFloat(const char* lower, const char* upper) {
|
||||
T f;
|
||||
TEST_EQ(flatbuffers::StringToNumber("", &f), false);
|
||||
TEST_EQ(flatbuffers::StringToNumber("1q", &f), false);
|
||||
@@ -69,53 +69,51 @@ void UtilConvertCase() {
|
||||
std::vector<TestCase> cases;
|
||||
|
||||
// Tests for the common cases
|
||||
cases.push_back({ "the_quick_brown_fox", flatbuffers::Case::kUpperCamel,
|
||||
"TheQuickBrownFox" });
|
||||
cases.push_back({ "the_quick_brown_fox", flatbuffers::Case::kLowerCamel,
|
||||
"theQuickBrownFox" });
|
||||
cases.push_back({ "the_quick_brown_fox", flatbuffers::Case::kSnake,
|
||||
"the_quick_brown_fox" });
|
||||
cases.push_back({ "the_quick_brown_fox", flatbuffers::Case::kScreamingSnake,
|
||||
"THE_QUICK_BROWN_FOX" });
|
||||
cases.push_back({ "the_quick_brown_fox", flatbuffers::Case::kAllLower,
|
||||
"the_quick_brown_fox" });
|
||||
cases.push_back({ "the_quick_brown_fox", flatbuffers::Case::kAllUpper,
|
||||
"THE_QUICK_BROWN_FOX" });
|
||||
cases.push_back({ "the_quick_brown_fox", flatbuffers::Case::kUnknown,
|
||||
"the_quick_brown_fox" });
|
||||
cases.push_back({ "the_quick_brown_fox", flatbuffers::Case::kKeep,
|
||||
"the_quick_brown_fox" });
|
||||
cases.push_back({ "the_quick_brown_fox", flatbuffers::Case::kSnake2,
|
||||
"the_quick_brown_fox" });
|
||||
cases.push_back({"the_quick_brown_fox", flatbuffers::Case::kUpperCamel,
|
||||
"TheQuickBrownFox"});
|
||||
cases.push_back({"the_quick_brown_fox", flatbuffers::Case::kLowerCamel,
|
||||
"theQuickBrownFox"});
|
||||
cases.push_back({"the_quick_brown_fox", flatbuffers::Case::kSnake,
|
||||
"the_quick_brown_fox"});
|
||||
cases.push_back({"the_quick_brown_fox", flatbuffers::Case::kScreamingSnake,
|
||||
"THE_QUICK_BROWN_FOX"});
|
||||
cases.push_back({"the_quick_brown_fox", flatbuffers::Case::kAllLower,
|
||||
"the_quick_brown_fox"});
|
||||
cases.push_back({"the_quick_brown_fox", flatbuffers::Case::kAllUpper,
|
||||
"THE_QUICK_BROWN_FOX"});
|
||||
cases.push_back({"the_quick_brown_fox", flatbuffers::Case::kUnknown,
|
||||
"the_quick_brown_fox"});
|
||||
cases.push_back({"the_quick_brown_fox", flatbuffers::Case::kKeep,
|
||||
"the_quick_brown_fox"});
|
||||
cases.push_back({"the_quick_brown_fox", flatbuffers::Case::kSnake2,
|
||||
"the_quick_brown_fox"});
|
||||
|
||||
// Tests for some snake_cases where the _ is oddly placed or
|
||||
// missing.
|
||||
cases.push_back({ "single", flatbuffers::Case::kUpperCamel, "Single" });
|
||||
cases.push_back({ "Single", flatbuffers::Case::kUpperCamel, "Single" });
|
||||
cases.push_back({ "_leading", flatbuffers::Case::kUpperCamel, "_Leading" });
|
||||
cases.push_back(
|
||||
{ "trailing_", flatbuffers::Case::kUpperCamel, "Trailing_" });
|
||||
cases.push_back({ "double__underscore", flatbuffers::Case::kUpperCamel,
|
||||
"Double_underscore" });
|
||||
cases.push_back({ "single", flatbuffers::Case::kLowerCamel, "single" });
|
||||
cases.push_back({ "Single", flatbuffers::Case::kLowerCamel, "single" });
|
||||
cases.push_back({ "_leading", flatbuffers::Case::kLowerCamel, "_Leading" });
|
||||
cases.push_back(
|
||||
{ "trailing_", flatbuffers::Case::kLowerCamel, "trailing_" });
|
||||
cases.push_back({ "double__underscore", flatbuffers::Case::kLowerCamel,
|
||||
"double_underscore" });
|
||||
cases.push_back({"single", flatbuffers::Case::kUpperCamel, "Single"});
|
||||
cases.push_back({"Single", flatbuffers::Case::kUpperCamel, "Single"});
|
||||
cases.push_back({"_leading", flatbuffers::Case::kUpperCamel, "_Leading"});
|
||||
cases.push_back({"trailing_", flatbuffers::Case::kUpperCamel, "Trailing_"});
|
||||
cases.push_back({"double__underscore", flatbuffers::Case::kUpperCamel,
|
||||
"Double_underscore"});
|
||||
cases.push_back({"single", flatbuffers::Case::kLowerCamel, "single"});
|
||||
cases.push_back({"Single", flatbuffers::Case::kLowerCamel, "single"});
|
||||
cases.push_back({"_leading", flatbuffers::Case::kLowerCamel, "_Leading"});
|
||||
cases.push_back({"trailing_", flatbuffers::Case::kLowerCamel, "trailing_"});
|
||||
cases.push_back({"double__underscore", flatbuffers::Case::kLowerCamel,
|
||||
"double_underscore"});
|
||||
|
||||
// Tests for some output snake_cases
|
||||
cases.push_back({ "single", flatbuffers::Case::kSnake, "single" });
|
||||
cases.push_back({ "single", flatbuffers::Case::kScreamingSnake, "SINGLE" });
|
||||
cases.push_back({"single", flatbuffers::Case::kSnake, "single"});
|
||||
cases.push_back({"single", flatbuffers::Case::kScreamingSnake, "SINGLE"});
|
||||
cases.push_back(
|
||||
{ "_leading", flatbuffers::Case::kScreamingSnake, "_LEADING" });
|
||||
{"_leading", flatbuffers::Case::kScreamingSnake, "_LEADING"});
|
||||
cases.push_back(
|
||||
{ "trailing_", flatbuffers::Case::kScreamingSnake, "TRAILING_" });
|
||||
cases.push_back({ "double__underscore", flatbuffers::Case::kScreamingSnake,
|
||||
"DOUBLE__UNDERSCORE" });
|
||||
{"trailing_", flatbuffers::Case::kScreamingSnake, "TRAILING_"});
|
||||
cases.push_back({"double__underscore", flatbuffers::Case::kScreamingSnake,
|
||||
"DOUBLE__UNDERSCORE"});
|
||||
|
||||
for (auto &test_case : cases) {
|
||||
for (auto& test_case : cases) {
|
||||
TEST_EQ(test_case.expected_output,
|
||||
flatbuffers::ConvertCase(test_case.input, test_case.output_case));
|
||||
}
|
||||
@@ -132,38 +130,38 @@ void UtilConvertCase() {
|
||||
|
||||
std::vector<TestCase> cases;
|
||||
|
||||
cases.push_back({ flatbuffers::Case::kUpperCamel, "TheQuickBrownFox",
|
||||
flatbuffers::Case::kSnake, "the_quick_brown_fox" });
|
||||
cases.push_back({ flatbuffers::Case::kLowerCamel, "theQuickBrownFox",
|
||||
flatbuffers::Case::kSnake, "the_quick_brown_fox" });
|
||||
cases.push_back({ flatbuffers::Case::kSnake, "the_quick_brown_fox",
|
||||
flatbuffers::Case::kSnake, "the_quick_brown_fox" });
|
||||
cases.push_back({ flatbuffers::Case::kScreamingSnake, "THE_QUICK_BROWN_FOX",
|
||||
flatbuffers::Case::kSnake, "THE_QUICK_BROWN_FOX" });
|
||||
cases.push_back({ flatbuffers::Case::kAllUpper, "SINGLE",
|
||||
flatbuffers::Case::kSnake, "SINGLE" });
|
||||
cases.push_back({ flatbuffers::Case::kAllLower, "single",
|
||||
flatbuffers::Case::kSnake, "single" });
|
||||
cases.push_back({ flatbuffers::Case::kUpperCamel, "ABCtest",
|
||||
flatbuffers::Case::kSnake, "abctest" });
|
||||
cases.push_back({ flatbuffers::Case::kUpperCamel, "tHe_qUiCk_BrOwN_fOx",
|
||||
flatbuffers::Case::kKeep, "tHe_qUiCk_BrOwN_fOx" });
|
||||
cases.push_back({ flatbuffers::Case::kLowerCamel, "theQuick12345Fox",
|
||||
flatbuffers::Case::kSnake, "the_quick_12345_fox" });
|
||||
cases.push_back({ flatbuffers::Case::kLowerCamel, "a12b34c45",
|
||||
flatbuffers::Case::kSnake, "a_12b_34c_45" });
|
||||
cases.push_back({ flatbuffers::Case::kLowerCamel, "a12b34c45",
|
||||
flatbuffers::Case::kSnake2, "a12_b34_c45" });
|
||||
cases.push_back({ flatbuffers::Case::kUpperCamel, "Int32Stamped",
|
||||
flatbuffers::Case::kSnake, "int_32_stamped" });
|
||||
cases.push_back({ flatbuffers::Case::kUpperCamel, "101DogsTest",
|
||||
flatbuffers::Case::kSnake, "101_dogs_test" });
|
||||
cases.push_back({ flatbuffers::Case::kUpperCamel, "Int32Stamped",
|
||||
flatbuffers::Case::kScreamingSnake, "INT_32_STAMPED" });
|
||||
cases.push_back({ flatbuffers::Case::kUpperCamel, "101DogsTest",
|
||||
flatbuffers::Case::kScreamingSnake, "101_DOGS_TEST" });
|
||||
cases.push_back({flatbuffers::Case::kUpperCamel, "TheQuickBrownFox",
|
||||
flatbuffers::Case::kSnake, "the_quick_brown_fox"});
|
||||
cases.push_back({flatbuffers::Case::kLowerCamel, "theQuickBrownFox",
|
||||
flatbuffers::Case::kSnake, "the_quick_brown_fox"});
|
||||
cases.push_back({flatbuffers::Case::kSnake, "the_quick_brown_fox",
|
||||
flatbuffers::Case::kSnake, "the_quick_brown_fox"});
|
||||
cases.push_back({flatbuffers::Case::kScreamingSnake, "THE_QUICK_BROWN_FOX",
|
||||
flatbuffers::Case::kSnake, "THE_QUICK_BROWN_FOX"});
|
||||
cases.push_back({flatbuffers::Case::kAllUpper, "SINGLE",
|
||||
flatbuffers::Case::kSnake, "SINGLE"});
|
||||
cases.push_back({flatbuffers::Case::kAllLower, "single",
|
||||
flatbuffers::Case::kSnake, "single"});
|
||||
cases.push_back({flatbuffers::Case::kUpperCamel, "ABCtest",
|
||||
flatbuffers::Case::kSnake, "abctest"});
|
||||
cases.push_back({flatbuffers::Case::kUpperCamel, "tHe_qUiCk_BrOwN_fOx",
|
||||
flatbuffers::Case::kKeep, "tHe_qUiCk_BrOwN_fOx"});
|
||||
cases.push_back({flatbuffers::Case::kLowerCamel, "theQuick12345Fox",
|
||||
flatbuffers::Case::kSnake, "the_quick_12345_fox"});
|
||||
cases.push_back({flatbuffers::Case::kLowerCamel, "a12b34c45",
|
||||
flatbuffers::Case::kSnake, "a_12b_34c_45"});
|
||||
cases.push_back({flatbuffers::Case::kLowerCamel, "a12b34c45",
|
||||
flatbuffers::Case::kSnake2, "a12_b34_c45"});
|
||||
cases.push_back({flatbuffers::Case::kUpperCamel, "Int32Stamped",
|
||||
flatbuffers::Case::kSnake, "int_32_stamped"});
|
||||
cases.push_back({flatbuffers::Case::kUpperCamel, "101DogsTest",
|
||||
flatbuffers::Case::kSnake, "101_dogs_test"});
|
||||
cases.push_back({flatbuffers::Case::kUpperCamel, "Int32Stamped",
|
||||
flatbuffers::Case::kScreamingSnake, "INT_32_STAMPED"});
|
||||
cases.push_back({flatbuffers::Case::kUpperCamel, "101DogsTest",
|
||||
flatbuffers::Case::kScreamingSnake, "101_DOGS_TEST"});
|
||||
|
||||
for (auto &test_case : cases) {
|
||||
for (auto& test_case : cases) {
|
||||
TEST_EQ(test_case.expected_output,
|
||||
flatbuffers::ConvertCase(test_case.input, test_case.output_case,
|
||||
test_case.input_case));
|
||||
|
||||
Reference in New Issue
Block a user