[C++] Refactor to conform to Google C++ style guide (#5608)

* Automatic refractor of C++ headers to Google C++ style guide

* Automatic refractor of C++ source to Google C++ style guide

* Automatic refractor of C++ tests to Google C++ style guide

* Fixed clang-format issues by running clang-format twice to correct itself. Kotlin was missing clang-format on after turning it off, so it was changed,
This commit is contained in:
Derek Bailey
2019-11-07 12:22:54 -08:00
committed by Wouter van Oortmerssen
parent e837d5a296
commit f0f0efe7b8
36 changed files with 3157 additions and 3294 deletions

View File

@@ -14,6 +14,7 @@
* limitations under the License.
*/
#include <cmath>
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/minireflect.h"
@@ -35,7 +36,7 @@
#include "union_vector/union_vector_generated.h"
#include "monster_extra_generated.h"
#if !defined(_MSC_VER) || _MSC_VER >= 1700
#include "arrays_test_generated.h"
# include "arrays_test_generated.h"
#endif
#include "native_type_test_generated.h"
@@ -43,7 +44,6 @@
#include "flatbuffers/flexbuffers.h"
// clang-format off
// Check that char* and uint8_t* are interoperable types.
// The reinterpret_cast<> between the pointers are used to simplify data loading.
@@ -72,7 +72,8 @@ void FlatBufferBuilderTest();
// http://en.wikipedia.org/wiki/Park%E2%80%93Miller_random_number_generator
uint32_t lcg_seed = 48271;
uint32_t lcg_rand() {
return lcg_seed = (static_cast<uint64_t>(lcg_seed) * 279470273UL) % 4294967291UL;
return lcg_seed =
(static_cast<uint64_t>(lcg_seed) * 279470273UL) % 4294967291UL;
}
void lcg_reset() { lcg_seed = 48271; }
@@ -200,14 +201,13 @@ flatbuffers::DetachedBuffer CreateFlatBufferTest(std::string &buffer) {
auto vecofcolors = builder.CreateVectorScalarCast<uint8_t, Color>(colors, 2);
// shortcut for creating monster with all fields set:
auto mloc = CreateMonster(builder, &vec, 150, 80, name, inventory, Color_Blue,
Any_Monster, mlocs[1].Union(), // Store a union.
testv, vecofstrings, vecoftables, 0,
nested_flatbuffer_vector, 0, false, 0, 0, 0, 0, 0,
0, 0, 0, 0, 3.14159f, 3.0f, 0.0f, vecofstrings2,
vecofstructs, flex, testv2, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, AnyUniqueAliases_NONE, 0,
AnyAmbiguousAliases_NONE, 0, vecofcolors);
auto mloc = CreateMonster(
builder, &vec, 150, 80, name, inventory, Color_Blue, Any_Monster,
mlocs[1].Union(), // Store a union.
testv, vecofstrings, vecoftables, 0, nested_flatbuffer_vector, 0, false,
0, 0, 0, 0, 0, 0, 0, 0, 0, 3.14159f, 3.0f, 0.0f, vecofstrings2,
vecofstructs, flex, testv2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
AnyUniqueAliases_NONE, 0, AnyAmbiguousAliases_NONE, 0, vecofcolors);
FinishMonsterBuffer(builder, mloc);
@@ -567,8 +567,7 @@ void SizePrefixedTest() {
// Create size prefixed buffer.
flatbuffers::FlatBufferBuilder fbb;
FinishSizePrefixedMonsterBuffer(
fbb,
CreateMonster(fbb, 0, 200, 300, fbb.CreateString("bob")));
fbb, CreateMonster(fbb, 0, 200, 300, fbb.CreateString("bob")));
// Verify it.
flatbuffers::Verifier verifier(fbb.GetBufferPointer(), fbb.GetSize());
@@ -598,7 +597,8 @@ void JsonDefaultTest() {
// load FlatBuffer schema (.fbs) from disk
std::string schemafile;
TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_test.fbs").c_str(),
false, &schemafile), true);
false, &schemafile),
true);
// parse schema first, so we can use it to parse the data after
flatbuffers::Parser parser;
auto include_test_path =
@@ -771,7 +771,7 @@ void ParseAndGenerateTextTest(bool binary) {
true);
auto include_test_path =
flatbuffers::ConCatPathFileName(test_data_path, "include_test");
flatbuffers::ConCatPathFileName(test_data_path, "include_test");
const char *include_directories[] = { test_data_path.c_str(),
include_test_path.c_str(), nullptr };
@@ -782,8 +782,10 @@ void ParseAndGenerateTextTest(bool binary) {
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((const uint8_t *)schemafile.c_str(), schemafile.size()), true);
// auto schema = reflection::GetSchema(schemafile.c_str());
TEST_EQ(parser.Deserialize((const uint8_t *)schemafile.c_str(),
schemafile.size()),
true);
} else {
TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
}
@@ -1029,7 +1031,8 @@ void ReflectionTest(uint8_t *flatbuf, size_t length) {
}
void MiniReflectFlatBuffersTest(uint8_t *flatbuf) {
auto s = flatbuffers::FlatBufferToString(flatbuf, Monster::MiniReflectTypeTable());
auto s =
flatbuffers::FlatBufferToString(flatbuf, Monster::MiniReflectTypeTable());
TEST_EQ_STR(
s.c_str(),
"{ "
@@ -1060,15 +1063,15 @@ void MiniReflectFlatBuffersTest(uint8_t *flatbuf) {
"}");
Test test(16, 32);
Vec3 vec(1,2,3, 1.5, Color_Red, test);
Vec3 vec(1, 2, 3, 1.5, Color_Red, test);
flatbuffers::FlatBufferBuilder vec_builder;
vec_builder.Finish(vec_builder.CreateStruct(vec));
auto vec_buffer = vec_builder.Release();
auto vec_str = flatbuffers::FlatBufferToString(vec_buffer.data(),
Vec3::MiniReflectTypeTable());
TEST_EQ_STR(
vec_str.c_str(),
"{ x: 1.0, y: 2.0, z: 3.0, test1: 1.5, test2: Red, test3: { a: 16, b: 32 } }");
TEST_EQ_STR(vec_str.c_str(),
"{ x: 1.0, y: 2.0, z: 3.0, test1: 1.5, test2: Red, test3: { a: "
"16, b: 32 } }");
}
// Parse a .proto schema, output as .fbs
@@ -1085,11 +1088,10 @@ void ParseProtoTest() {
flatbuffers::LoadFile((test_data_path + "prototest/test.golden").c_str(),
false, &goldenfile),
true);
TEST_EQ(
flatbuffers::LoadFile((test_data_path +
"prototest/test_union.golden").c_str(),
false, &goldenunionfile),
true);
TEST_EQ(flatbuffers::LoadFile(
(test_data_path + "prototest/test_union.golden").c_str(), false,
&goldenunionfile),
true);
flatbuffers::IDLOptions opts;
opts.include_dependence_headers = false;
@@ -1526,9 +1528,9 @@ bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; }
// Additional parser testing not covered elsewhere.
void ValueTest() {
// Test scientific notation numbers.
TEST_EQ(FloatCompare(TestValue<float>("{ Y:0.0314159e+2 }", "float"),
3.14159f),
true);
TEST_EQ(
FloatCompare(TestValue<float>("{ Y:0.0314159e+2 }", "float"), 3.14159f),
true);
// number in string
TEST_EQ(FloatCompare(TestValue<float>("{ Y:\"0.0314159e+2\" }", "float"),
3.14159f),
@@ -1566,7 +1568,6 @@ void ValueTest() {
// check comments before and after json object
TEST_EQ(TestValue<int>("/*before*/ { Y:1 } /*after*/", "int"), 1);
TEST_EQ(TestValue<int>("//before \n { Y:1 } //after", "int"), 1);
}
void NestedListTest() {
@@ -1608,8 +1609,8 @@ void EnumNamesTest() {
// For details see C++17 standard or explanation on the SO:
// stackoverflow.com/questions/18195312/what-happens-if-you-static-cast-invalid-value-to-enum-class
TEST_EQ_STR("", EnumNameColor(static_cast<Color>(0)));
TEST_EQ_STR("", EnumNameColor(static_cast<Color>(Color_ANY-1)));
TEST_EQ_STR("", EnumNameColor(static_cast<Color>(Color_ANY+1)));
TEST_EQ_STR("", EnumNameColor(static_cast<Color>(Color_ANY - 1)));
TEST_EQ_STR("", EnumNameColor(static_cast<Color>(Color_ANY + 1)));
}
void EnumOutOfRangeTest() {
@@ -1629,12 +1630,14 @@ void EnumOutOfRangeTest() {
TestError("enum X:uint { Y = -1 }", "enum value does not fit");
TestError("enum X:uint { Y = 4294967297 }", "enum value does not fit");
TestError("enum X:long { Y = 9223372036854775808 }", "does not fit");
TestError("enum X:long { Y = 9223372036854775807, Z }", "enum value does not fit");
TestError("enum X:long { Y = 9223372036854775807, Z }",
"enum value does not fit");
TestError("enum X:ulong { Y = -1 }", "does not fit");
TestError("enum X:ubyte (bit_flags) { Y=8 }", "bit flag out");
TestError("enum X:byte (bit_flags) { Y=7 }", "must be unsigned"); // -128
TestError("enum X:byte (bit_flags) { Y=7 }", "must be unsigned"); // -128
// bit_flgs out of range
TestError("enum X:ubyte (bit_flags) { Y0,Y1,Y2,Y3,Y4,Y5,Y6,Y7,Y8 }", "out of range");
TestError("enum X:ubyte (bit_flags) { Y0,Y1,Y2,Y3,Y4,Y5,Y6,Y7,Y8 }",
"out of range");
}
void EnumValueTest() {
@@ -1734,11 +1737,12 @@ void IntegerOutOfRangeTest() {
void IntegerBoundaryTest() {
// Check numerical compatibility with non-C++ languages.
// By the C++ standard, std::numerical_limits<int64_t>::min() == -9223372036854775807 (-2^63+1) or less*
// The Flatbuffers grammar and most of the languages (C#, Java, Rust) expect
// that minimum values are: -128, -32768,.., -9223372036854775808.
// Since C++20, static_cast<int64>(0x8000000000000000ULL) is well-defined two's complement cast.
// Therefore -9223372036854775808 should be valid negative value.
// By the C++ standard, std::numerical_limits<int64_t>::min() ==
// -9223372036854775807 (-2^63+1) or less* The Flatbuffers grammar and most of
// the languages (C#, Java, Rust) expect that minimum values are: -128,
// -32768,.., -9223372036854775808. Since C++20,
// static_cast<int64>(0x8000000000000000ULL) is well-defined two's complement
// cast. Therefore -9223372036854775808 should be valid negative value.
TEST_EQ(flatbuffers::numeric_limits<int8_t>::min(), -128);
TEST_EQ(flatbuffers::numeric_limits<int8_t>::max(), 127);
TEST_EQ(flatbuffers::numeric_limits<int16_t>::min(), -32768);
@@ -1810,11 +1814,11 @@ void ValidFloatTest() {
TEST_EQ(TestValue<float>("{ Y:5 }", "float"), 5.0f);
TEST_EQ(TestValue<float>("{ Y:\"5\" }", "float"), 5.0f);
#if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
#if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
// Old MSVC versions may have problem with this check.
// https://www.exploringbinary.com/visual-c-plus-plus-strtod-still-broken/
TEST_EQ(TestValue<double>("{ Y:6.9294956446009195e15 }", "double"),
6929495644600920.0);
6929495644600920.0);
// check nan's
TEST_EQ(std::isnan(TestValue<double>("{ Y:nan }", "double")), true);
TEST_EQ(std::isnan(TestValue<float>("{ Y:nan }", "float")), true);
@@ -1936,8 +1940,8 @@ void GenerateTableTextTest() {
TEST_EQ(ok, true);
auto include_test_path =
flatbuffers::ConCatPathFileName(test_data_path, "include_test");
const char *include_directories[] = {test_data_path.c_str(),
include_test_path.c_str(), nullptr};
const char *include_directories[] = { test_data_path.c_str(),
include_test_path.c_str(), nullptr };
flatbuffers::IDLOptions opt;
opt.indent_step = -1;
flatbuffers::Parser parser(opt);
@@ -2237,7 +2241,7 @@ void InvalidUTF8Test() {
// Check independence of identifier from locale.
std::string locale_ident;
locale_ident += "table T { F";
locale_ident += static_cast<char>(-32); // unsigned 0xE0
locale_ident += static_cast<char>(-32); // unsigned 0xE0
locale_ident += " :string; }";
locale_ident += "root_type T;";
locale_ident += "{}";
@@ -2311,8 +2315,8 @@ void UnionVectorTest() {
// load FlatBuffer fbs schema and json.
std::string schemafile, jsonfile;
TEST_EQ(flatbuffers::LoadFile(
(test_data_path + "union_vector/union_vector.fbs").c_str(),
false, &schemafile),
(test_data_path + "union_vector/union_vector.fbs").c_str(), false,
&schemafile),
true);
TEST_EQ(flatbuffers::LoadFile(
(test_data_path + "union_vector/union_vector.json").c_str(),
@@ -2420,70 +2424,67 @@ void UnionVectorTest() {
"characters: [ { books_read: 7 }, { sword_attack_damage: 5 }, "
"{ books_read: 2 }, \"Other\", \"Unused\" ] }");
flatbuffers::ToStringVisitor visitor("\n", true, " ");
IterateFlatBuffer(fbb.GetBufferPointer(), MovieTypeTable(), &visitor);
TEST_EQ_STR(
visitor.s.c_str(),
"{\n"
" \"main_character_type\": \"Rapunzel\",\n"
" \"main_character\": {\n"
" \"hair_length\": 6\n"
" },\n"
" \"characters_type\": [\n"
" \"Belle\",\n"
" \"MuLan\",\n"
" \"BookFan\",\n"
" \"Other\",\n"
" \"Unused\"\n"
" ],\n"
" \"characters\": [\n"
" {\n"
" \"books_read\": 7\n"
" },\n"
" {\n"
" \"sword_attack_damage\": 5\n"
" },\n"
" {\n"
" \"books_read\": 2\n"
" },\n"
" \"Other\",\n"
" \"Unused\"\n"
" ]\n"
"}");
TEST_EQ_STR(visitor.s.c_str(),
"{\n"
" \"main_character_type\": \"Rapunzel\",\n"
" \"main_character\": {\n"
" \"hair_length\": 6\n"
" },\n"
" \"characters_type\": [\n"
" \"Belle\",\n"
" \"MuLan\",\n"
" \"BookFan\",\n"
" \"Other\",\n"
" \"Unused\"\n"
" ],\n"
" \"characters\": [\n"
" {\n"
" \"books_read\": 7\n"
" },\n"
" {\n"
" \"sword_attack_damage\": 5\n"
" },\n"
" {\n"
" \"books_read\": 2\n"
" },\n"
" \"Other\",\n"
" \"Unused\"\n"
" ]\n"
"}");
// Generate text using parsed schema.
std::string jsongen;
auto result = GenerateText(parser, fbb.GetBufferPointer(), &jsongen);
TEST_EQ(result, true);
TEST_EQ_STR(
jsongen.c_str(),
"{\n"
" main_character_type: \"Rapunzel\",\n"
" main_character: {\n"
" hair_length: 6\n"
" },\n"
" characters_type: [\n"
" \"Belle\",\n"
" \"MuLan\",\n"
" \"BookFan\",\n"
" \"Other\",\n"
" \"Unused\"\n"
" ],\n"
" characters: [\n"
" {\n"
" books_read: 7\n"
" },\n"
" {\n"
" sword_attack_damage: 5\n"
" },\n"
" {\n"
" books_read: 2\n"
" },\n"
" \"Other\",\n"
" \"Unused\"\n"
" ]\n"
"}\n");
TEST_EQ_STR(jsongen.c_str(),
"{\n"
" main_character_type: \"Rapunzel\",\n"
" main_character: {\n"
" hair_length: 6\n"
" },\n"
" characters_type: [\n"
" \"Belle\",\n"
" \"MuLan\",\n"
" \"BookFan\",\n"
" \"Other\",\n"
" \"Unused\"\n"
" ],\n"
" characters: [\n"
" {\n"
" books_read: 7\n"
" },\n"
" {\n"
" sword_attack_damage: 5\n"
" },\n"
" {\n"
" books_read: 2\n"
" },\n"
" \"Other\",\n"
" \"Unused\"\n"
" ]\n"
"}\n");
// Simple test with reflection.
parser.Serialize();
@@ -2496,7 +2497,8 @@ void UnionVectorTest() {
TEST_EQ(parser2.Parse("struct Bool { b:bool; }"
"union Any { Bool }"
"table Root { a:Any; }"
"root_type Root;"), true);
"root_type Root;"),
true);
TEST_EQ(parser2.Parse("{a_type:Bool,a:{b:true}}"), true);
}
@@ -2703,7 +2705,7 @@ void TypeAliasesTest() {
TEST_EQ(ta->u64(), flatbuffers::numeric_limits<uint64_t>::max());
TEST_EQ(ta->f32(), 2.3f);
TEST_EQ(ta->f64(), 2.3);
using namespace flatbuffers; // is_same
using namespace flatbuffers; // is_same
static_assert(is_same<decltype(ta->i8()), int8_t>::value, "invalid type");
static_assert(is_same<decltype(ta->i16()), int16_t>::value, "invalid type");
static_assert(is_same<decltype(ta->i32()), int32_t>::value, "invalid type");
@@ -2729,14 +2731,16 @@ void UninitializedVectorTest() {
flatbuffers::FlatBufferBuilder builder;
Test *buf = nullptr;
auto vector_offset = builder.CreateUninitializedVectorOfStructs<Test>(2, &buf);
auto vector_offset =
builder.CreateUninitializedVectorOfStructs<Test>(2, &buf);
TEST_NOTNULL(buf);
buf[0] = Test(10, 20);
buf[1] = Test(30, 40);
auto required_name = builder.CreateString("myMonster");
auto monster_builder = MonsterBuilder(builder);
monster_builder.add_name(required_name); // required field mandated for monster.
monster_builder.add_name(
required_name); // required field mandated for monster.
monster_builder.add_test4(vector_offset);
builder.Finish(monster_builder.Finish());
@@ -2781,11 +2785,11 @@ void EqualOperatorTest() {
// For testing any binaries, e.g. from fuzzing.
void LoadVerifyBinaryTest() {
std::string binary;
if (flatbuffers::LoadFile((test_data_path +
"fuzzer/your-filename-here").c_str(),
true, &binary)) {
if (flatbuffers::LoadFile(
(test_data_path + "fuzzer/your-filename-here").c_str(), true,
&binary)) {
flatbuffers::Verifier verifier(
reinterpret_cast<const uint8_t *>(binary.data()), binary.size());
reinterpret_cast<const uint8_t *>(binary.data()), binary.size());
TEST_EQ(VerifyMonsterBuffer(verifier), true);
}
}
@@ -2801,24 +2805,28 @@ 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));
TEST_EQ(null_b1.o != null_c.o, true); // Issue#5058 repro
TEST_EQ(null_b1.o != null_c.o, true); // Issue#5058 repro
TEST_EQ(null_b1.o, null_b2.o);
// Put the strings into an array for round trip verification.
const flatbuffers::Offset<flatbuffers::String> array[7] = { one1, two, one2, onetwo, null_b1, null_c, null_b2 };
const auto vector_offset = builder.CreateVector(array, flatbuffers::uoffset_t(7));
const flatbuffers::Offset<flatbuffers::String> array[7] = {
one1, two, one2, onetwo, null_b1, null_c, null_b2
};
const auto vector_offset =
builder.CreateVector(array, flatbuffers::uoffset_t(7));
MonsterBuilder monster_builder(builder);
monster_builder.add_name(two);
monster_builder.add_testarrayofstring(vector_offset);
builder.Finish(monster_builder.Finish());
// Read the Monster back.
const auto *monster = flatbuffers::GetRoot<Monster>(builder.GetBufferPointer());
const auto *monster =
flatbuffers::GetRoot<Monster>(builder.GetBufferPointer());
TEST_EQ_STR(monster->name()->c_str(), "two");
const auto *testarrayofstring = monster->testarrayofstring();
TEST_EQ(testarrayofstring->size(), flatbuffers::uoffset_t(7));
@@ -2831,7 +2839,8 @@ void CreateSharedStringTest() {
TEST_EQ(a[5]->str(), (std::string(chars_c, sizeof(chars_c))));
TEST_EQ(a[6]->str(), (std::string(chars_b, sizeof(chars_b))));
// Make sure String::operator< works, too, since it is related to StringOffsetCompare.
// Make sure String::operator< works, too, since it is related to
// StringOffsetCompare.
TEST_EQ((*a[0]) < (*a[1]), true);
TEST_EQ((*a[1]) < (*a[0]), false);
TEST_EQ((*a[1]) < (*a[2]), false);
@@ -2943,16 +2952,17 @@ void NativeTypeTest() {
src_data.vectors.reserve(N);
for (int i = 0; i < N; ++i) {
src_data.vectors.push_back (Native::Vector3D(10 * i + 0.1f, 10 * i + 0.2f, 10 * i + 0.3f));
src_data.vectors.push_back(
Native::Vector3D(10 * i + 0.1f, 10 * i + 0.2f, 10 * i + 0.3f));
}
flatbuffers::FlatBufferBuilder fbb;
fbb.Finish(Geometry::ApplicationData::Pack(fbb, &src_data));
auto dstDataT = Geometry::UnPackApplicationData(fbb.GetBufferPointer());
auto dstDataT = Geometry::UnPackApplicationData(fbb.GetBufferPointer());
for (int i = 0; i < N; ++i) {
Native::Vector3D& v = dstDataT->vectors[i];
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);
@@ -3107,7 +3117,7 @@ int FlatBufferTests() {
return 0;
}
int main(int /*argc*/, const char * /*argv*/ []) {
int main(int /*argc*/, const char * /*argv*/[]) {
InitTestEngine();
std::string req_locale;