diff options
-rw-r--r-- | include/flatbuffers/stl_emulation.h | 28 | ||||
-rw-r--r-- | tests/test.cpp | 40 | ||||
-rw-r--r-- | tests/test_assert.cpp | 8 | ||||
-rw-r--r-- | tests/test_assert.h | 67 |
4 files changed, 100 insertions, 43 deletions
diff --git a/include/flatbuffers/stl_emulation.h b/include/flatbuffers/stl_emulation.h index e68089ff..8bae61bf 100644 --- a/include/flatbuffers/stl_emulation.h +++ b/include/flatbuffers/stl_emulation.h @@ -96,13 +96,13 @@ inline void vector_emplace_back(std::vector<T> *vector, V &&data) { } }; - template <> class numeric_limits<float> : + template <> class numeric_limits<float> : public std::numeric_limits<float> { public: static float lowest() { return -FLT_MAX; } }; - template <> class numeric_limits<double> : + template <> class numeric_limits<double> : public std::numeric_limits<double> { public: static double lowest() { return -DBL_MAX; } @@ -138,18 +138,20 @@ inline void vector_emplace_back(std::vector<T> *vector, V &&data) { template <typename T, typename U> using is_same = std::is_same<T,U>; template <typename T> using is_floating_point = std::is_floating_point<T>; template <typename T> using is_unsigned = std::is_unsigned<T>; + template <typename T> using is_enum = std::is_enum<T>; template <typename T> using make_unsigned = std::make_unsigned<T>; template<bool B, class T, class F> using conditional = std::conditional<B, T, F>; template<class T, T v> using integral_constant = std::integral_constant<T, v>; -#else + #else // Map C++ TR1 templates defined by stlport. template <typename T> using is_scalar = std::tr1::is_scalar<T>; template <typename T, typename U> using is_same = std::tr1::is_same<T,U>; template <typename T> using is_floating_point = std::tr1::is_floating_point<T>; template <typename T> using is_unsigned = std::tr1::is_unsigned<T>; + template <typename T> using is_enum = std::tr1::is_enum<T>; // Android NDK doesn't have std::make_unsigned or std::tr1::make_unsigned. template<typename T> struct make_unsigned { static_assert(is_unsigned<T>::value, "Specialization not implemented!"); @@ -165,7 +167,7 @@ inline void vector_emplace_back(std::vector<T> *vector, V &&data) { using conditional = std::tr1::conditional<B, T, F>; template<class T, T v> using integral_constant = std::tr1::integral_constant<T, v>; -#endif // !FLATBUFFERS_CPP98_STL + #endif // !FLATBUFFERS_CPP98_STL #else // MSVC 2010 doesn't support C++11 aliases. template <typename T> struct is_scalar : public std::is_scalar<T> {}; @@ -173,6 +175,7 @@ inline void vector_emplace_back(std::vector<T> *vector, V &&data) { template <typename T> struct is_floating_point : public std::is_floating_point<T> {}; template <typename T> struct is_unsigned : public std::is_unsigned<T> {}; + template <typename T> struct is_enum : public std::is_enum<T> {}; template <typename T> struct make_unsigned : public std::make_unsigned<T> {}; template<bool B, class T, class F> struct conditional : public std::conditional<B, T, F> {}; @@ -280,6 +283,23 @@ inline void vector_emplace_back(std::vector<T> *vector, V &&data) { template <class T> bool operator==(const unique_ptr<T>& x, intptr_t y) { return reinterpret_cast<intptr_t>(x.get()) == y; } + + template <class T> bool operator!=(const unique_ptr<T>& x, decltype(nullptr)) { + return !!x; + } + + template <class T> bool operator!=(decltype(nullptr), const unique_ptr<T>& x) { + return !!x; + } + + template <class T> bool operator==(const unique_ptr<T>& x, decltype(nullptr)) { + return !x; + } + + template <class T> bool operator==(decltype(nullptr), const unique_ptr<T>& x) { + return !x; + } + #endif // !FLATBUFFERS_CPP98_STL } // namespace flatbuffers diff --git a/tests/test.cpp b/tests/test.cpp index 515e2c43..149831da 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<int64_t>::max()); - TEST_EQ(mArStruct->d()->Get(0)->d()->Get(1), - flatbuffers::numeric_limits<int64_t>::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<int64_t>::max(), + mArStruct->d()->Get(0)->d()->Get(0)); + TEST_EQ(flatbuffers::numeric_limits<int64_t>::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<int64_t>::min()); - TEST_EQ(mArStruct->d()->Get(1)->d()->Get(1), - flatbuffers::numeric_limits<int64_t>::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<int64_t>::min(), + mArStruct->d()->Get(1)->d()->Get(0)); + TEST_EQ(flatbuffers::numeric_limits<int64_t>::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<uintptr_t>(mArStruct->d()) % 8, 0); - TEST_EQ(reinterpret_cast<uintptr_t>(mArStruct->f()) % 8, 0); + TEST_EQ(0, reinterpret_cast<uintptr_t>(mArStruct->d()) % 8); + TEST_EQ(0, reinterpret_cast<uintptr_t>(mArStruct->f()) % 8); #endif } diff --git a/tests/test_assert.cpp b/tests/test_assert.cpp index eec77797..eb69a549 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 883586b0..b2d0562f 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<typename T, bool is_enum_type = flatbuffers::is_enum<T>::value> +struct underlying_of_scalar { + static_assert(flatbuffers::is_scalar<T>::value, "invalid type T"); + typedef T type; +}; + +template<typename T> struct underlying_of_scalar<T, true> { +// 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<T>::type; + #else + typedef int64_t type; + #endif + // clang-format on +}; + +template<typename T> +typename underlying_of_scalar<T>::type scalar_as_underlying(T v) { + return static_cast<typename underlying_of_scalar<T>::type>(v); +} template<typename T, typename U> 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<U>(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, std::string>(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); } } |