diff --git a/include/flatbuffers/stl_emulation.h b/include/flatbuffers/stl_emulation.h index e68089ff9..8bae61bfd 100644 --- a/include/flatbuffers/stl_emulation.h +++ b/include/flatbuffers/stl_emulation.h @@ -96,13 +96,13 @@ inline void vector_emplace_back(std::vector *vector, V &&data) { } }; - template <> class numeric_limits : + template <> class numeric_limits : public std::numeric_limits { public: static float lowest() { return -FLT_MAX; } }; - template <> class numeric_limits : + template <> class numeric_limits : public std::numeric_limits { public: static double lowest() { return -DBL_MAX; } @@ -138,18 +138,20 @@ inline void vector_emplace_back(std::vector *vector, V &&data) { template using is_same = std::is_same; template using is_floating_point = std::is_floating_point; template using is_unsigned = std::is_unsigned; + template using is_enum = std::is_enum; template using make_unsigned = std::make_unsigned; template using conditional = std::conditional; template using integral_constant = std::integral_constant; -#else + #else // Map C++ TR1 templates defined by stlport. template using is_scalar = std::tr1::is_scalar; template using is_same = std::tr1::is_same; template using is_floating_point = std::tr1::is_floating_point; template using is_unsigned = std::tr1::is_unsigned; + template using is_enum = std::tr1::is_enum; // Android NDK doesn't have std::make_unsigned or std::tr1::make_unsigned. template struct make_unsigned { static_assert(is_unsigned::value, "Specialization not implemented!"); @@ -165,7 +167,7 @@ inline void vector_emplace_back(std::vector *vector, V &&data) { using conditional = std::tr1::conditional; template using integral_constant = std::tr1::integral_constant; -#endif // !FLATBUFFERS_CPP98_STL + #endif // !FLATBUFFERS_CPP98_STL #else // MSVC 2010 doesn't support C++11 aliases. template struct is_scalar : public std::is_scalar {}; @@ -173,6 +175,7 @@ inline void vector_emplace_back(std::vector *vector, V &&data) { template struct is_floating_point : public std::is_floating_point {}; template struct is_unsigned : public std::is_unsigned {}; + template struct is_enum : public std::is_enum {}; template struct make_unsigned : public std::make_unsigned {}; template struct conditional : public std::conditional {}; @@ -280,6 +283,23 @@ inline void vector_emplace_back(std::vector *vector, V &&data) { template bool operator==(const unique_ptr& x, intptr_t y) { return reinterpret_cast(x.get()) == y; } + + template bool operator!=(const unique_ptr& x, decltype(nullptr)) { + return !!x; + } + + template bool operator!=(decltype(nullptr), const unique_ptr& x) { + return !!x; + } + + template bool operator==(const unique_ptr& x, decltype(nullptr)) { + return !x; + } + + template bool operator==(decltype(nullptr), const unique_ptr& x) { + return !x; + } + #endif // !FLATBUFFERS_CPP98_STL } // namespace flatbuffers diff --git a/tests/test.cpp b/tests/test.cpp index 515e2c438..149831da5 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1394,7 +1394,7 @@ void FuzzTest2() { break; } } - TEST_NOTNULL(NULL); + TEST_NOTNULL(nullptr); } // clang-format off @@ -2985,32 +2985,28 @@ void FixedLengthArrayTest() { TEST_NOTNULL(mArStruct->mutable_d()->GetMutablePointer(1)); TEST_NOTNULL(mArStruct->mutable_d()->GetMutablePointer(1)->mutable_a()); mArStruct->mutable_d()->GetMutablePointer(1)->mutable_a()->Mutate(1, 5); - TEST_EQ(mArStruct->d()->Get(1)->a()->Get(1), 5); - TEST_EQ(mArStruct->d()->Get(0)->b() == MyGame::Example::TestEnum::B, true); + TEST_EQ(5, mArStruct->d()->Get(1)->a()->Get(1)); + TEST_EQ(MyGame::Example::TestEnum::B, mArStruct->d()->Get(0)->b()); TEST_NOTNULL(mArStruct->d()->Get(0)->c()); - TEST_EQ(mArStruct->d()->Get(0)->c()->Get(0) == MyGame::Example::TestEnum::C, - true); - TEST_EQ(mArStruct->d()->Get(0)->c()->Get(1) == MyGame::Example::TestEnum::A, - true); - TEST_EQ(mArStruct->d()->Get(0)->d()->Get(0), - flatbuffers::numeric_limits::max()); - TEST_EQ(mArStruct->d()->Get(0)->d()->Get(1), - flatbuffers::numeric_limits::min()); - TEST_EQ(mArStruct->d()->Get(1)->b() == MyGame::Example::TestEnum::C, true); + TEST_EQ(MyGame::Example::TestEnum::C, mArStruct->d()->Get(0)->c()->Get(0)); + TEST_EQ(MyGame::Example::TestEnum::A, mArStruct->d()->Get(0)->c()->Get(1)); + TEST_EQ(flatbuffers::numeric_limits::max(), + mArStruct->d()->Get(0)->d()->Get(0)); + TEST_EQ(flatbuffers::numeric_limits::min(), + mArStruct->d()->Get(0)->d()->Get(1)); + TEST_EQ(MyGame::Example::TestEnum::C, mArStruct->d()->Get(1)->b()); TEST_NOTNULL(mArStruct->d()->Get(1)->c()); - TEST_EQ(mArStruct->d()->Get(1)->c()->Get(0) == MyGame::Example::TestEnum::C, - true); - TEST_EQ(mArStruct->d()->Get(1)->c()->Get(1) == MyGame::Example::TestEnum::A, - true); - TEST_EQ(mArStruct->d()->Get(1)->d()->Get(0), - flatbuffers::numeric_limits::min()); - TEST_EQ(mArStruct->d()->Get(1)->d()->Get(1), - flatbuffers::numeric_limits::max()); + TEST_EQ(MyGame::Example::TestEnum::C, mArStruct->d()->Get(1)->c()->Get(0)); + TEST_EQ(MyGame::Example::TestEnum::A, mArStruct->d()->Get(1)->c()->Get(1)); + TEST_EQ(flatbuffers::numeric_limits::min(), + mArStruct->d()->Get(1)->d()->Get(0)); + TEST_EQ(flatbuffers::numeric_limits::max(), + mArStruct->d()->Get(1)->d()->Get(1)); for (int i = 0; i < mArStruct->b()->size() - 1; i++) TEST_EQ(mArStruct->b()->Get(i), i + 1); // Check alignment - TEST_EQ(reinterpret_cast(mArStruct->d()) % 8, 0); - TEST_EQ(reinterpret_cast(mArStruct->f()) % 8, 0); + TEST_EQ(0, reinterpret_cast(mArStruct->d()) % 8); + TEST_EQ(0, reinterpret_cast(mArStruct->f()) % 8); #endif } diff --git a/tests/test_assert.cpp b/tests/test_assert.cpp index eec777972..eb69a5493 100644 --- a/tests/test_assert.cpp +++ b/tests/test_assert.cpp @@ -12,8 +12,8 @@ static TestFailEventListener fail_listener_ = nullptr; void TestFail(const char *expval, const char *val, const char *exp, const char *file, int line, const char *func) { - TEST_OUTPUT_LINE("VALUE: \"%s\"", expval); - TEST_OUTPUT_LINE("EXPECTED: \"%s\"", val); + 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, func ? func : ""); testing_fails++; @@ -25,8 +25,8 @@ void TestFail(const char *expval, const char *val, const char *exp, } void TestEqStr(const char *expval, const char *val, const char *exp, - const char *file, int line) { - if (strcmp(expval, val) != 0) { TestFail(expval, val, exp, file, line); } + const char *file, int line, const char *func) { + if (strcmp(expval, val) != 0) { TestFail(expval, val, exp, file, line, func); } } #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && defined(_MSC_VER) && \ diff --git a/tests/test_assert.h b/tests/test_assert.h index 883586b0e..b2d0562f8 100644 --- a/tests/test_assert.h +++ b/tests/test_assert.h @@ -16,17 +16,17 @@ { printf(__VA_ARGS__); printf("\n"); } #endif -#define TEST_EQ(exp, val) TestEq(exp, val, #exp, __FILE__, __LINE__) -#define TEST_ASSERT(exp) TestEq(exp, true, #exp, __FILE__, __LINE__) -#define TEST_NOTNULL(exp) TestEq(exp == NULL, false, #exp, __FILE__, __LINE__) -#define TEST_EQ_STR(exp, val) TestEqStr(exp, val, #exp, __FILE__, __LINE__) +#define TEST_EQ(exp, val) TestEq(exp, val, "'" #exp "' != '" #val "'", __FILE__, __LINE__, "") +#define TEST_ASSERT(val) TestEq(true, !!(val), "'" "true" "' != '" #val "'", __FILE__, __LINE__, "") +#define TEST_NOTNULL(val) TestEq(true, (val) != nullptr, "'" "nullptr" "' == '" #val "'", __FILE__, __LINE__, "") +#define TEST_EQ_STR(exp, val) TestEqStr(exp, val, "'" #exp "' != '" #val "'", __FILE__, __LINE__, "") #ifdef _WIN32 - #define TEST_ASSERT_FUNC(exp) TestEq(exp, true, #exp, __FILE__, __LINE__, __FUNCTION__) - #define TEST_EQ_FUNC(exp, val) TestEq(exp, val, #exp, __FILE__, __LINE__, __FUNCTION__) + #define TEST_ASSERT_FUNC(val) TestEq(true, !!(val), "'" "true" "' != '" #val "'", __FILE__, __LINE__, __FUNCTION__) + #define TEST_EQ_FUNC(exp, val) TestEq(exp, val, "'" #exp "' != '" #val "'", __FILE__, __LINE__, __FUNCTION__) #else - #define TEST_ASSERT_FUNC(exp) TestEq(exp, true, #exp, __FILE__, __LINE__, __PRETTY_FUNCTION__) - #define TEST_EQ_FUNC(exp, val) TestEq(exp, val, #exp, __FILE__, __LINE__, __PRETTY_FUNCTION__) + #define TEST_ASSERT_FUNC(val) TestEq(true, !!(val), "'" "true" "' != '" #val "'", __FILE__, __LINE__, __PRETTY_FUNCTION__) + #define TEST_EQ_FUNC(exp, val) TestEq(exp, val, "'" #exp "' != '" #val "'", __FILE__, __LINE__, __PRETTY_FUNCTION__) #endif // clang-format on @@ -54,14 +54,55 @@ void TestFail(const char *expval, const char *val, const char *exp, const char *file, int line, const char *func = 0); void TestEqStr(const char *expval, const char *val, const char *exp, - const char *file, int line); + const char *file, int line, const char *func = 0); + +// Workaround for `enum class` printing. +// There is an issue with the printing of enums with a fixed underlying type. +// These enums are generated by `flatc` if `--scoped-enums` is active. +// All modern compilers have problems with `std::stringstream&<<(T v)` if T is +// an enum with fixed type. For details see DR1601: +// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1601 +// https://stackoverflow.com/questions/34336024/ambiguous-overload-when-writing-an-enum-with-an-enum-base-but-only-with-clang + +template::value> +struct underlying_of_scalar { + static_assert(flatbuffers::is_scalar::value, "invalid type T"); + typedef T type; +}; + +template struct underlying_of_scalar { +// clang-format off + // There are old compilers without full C++11 support (see stl_emulation.h). + #if defined(FLATBUFFERS_TEMPLATES_ALIASES) && !defined(FLATBUFFERS_CPP98_STL) + using type = typename std::underlying_type::type; + #else + typedef int64_t type; + #endif + // clang-format on +}; + +template +typename underlying_of_scalar::type scalar_as_underlying(T v) { + return static_cast::type>(v); +} template void TestEq(T expval, U val, const char *exp, const char *file, int line, - const char *func = 0) { - if (U(expval) != val) { - TestFail(flatbuffers::NumToString(expval).c_str(), - flatbuffers::NumToString(val).c_str(), exp, file, line, func); + const char *func) { + if (static_cast(expval) != val) { + TestFail(flatbuffers::NumToString(scalar_as_underlying(expval)).c_str(), + flatbuffers::NumToString(scalar_as_underlying(val)).c_str(), exp, + file, line, func); + } +} + +template<> +inline void TestEq(std::string expval, + std::string val, const char *exp, + const char *file, int line, + const char *func) { + if (expval != val) { + TestFail(expval.c_str(), val.c_str(), exp, file, line, func); } }