mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-23 03:40:00 +00:00
Fix ambiguity of a type deduction in TEST_EQ macro if arguments have enum class type. (#5630)
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
075e8d676b
commit
c3c32ec942
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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) && \
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user