diff options
Diffstat (limited to 'tests/test_assert.h')
-rw-r--r-- | tests/test_assert.h | 67 |
1 files changed, 54 insertions, 13 deletions
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); } } |