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:
Derek Bailey
2025-09-23 21:19:33 -07:00
committed by GitHub
parent 881eaab706
commit 0e047869da
84 changed files with 8178 additions and 6653 deletions

View File

@@ -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++) {