Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ambiguity of a type deduction in TEST_EQ macro for enum class args. #5630

Merged
merged 1 commit into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix ambiguity of a type deduction in TEST_EQ macro if arguments have …
…`enum class` type.
  • Loading branch information
vglavnyy committed Nov 22, 2019
commit 3bc7b814143e4e2930786292b6b843f931f9769e
28 changes: 24 additions & 4 deletions include/flatbuffers/stl_emulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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!");
Expand All @@ -165,14 +167,15 @@ 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> {};
template <typename T, typename U> struct is_same : public std::is_same<T,U> {};
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> {};
Expand Down Expand Up @@ -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
Expand Down
40 changes: 18 additions & 22 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,7 @@ void FuzzTest2() {
break;
}
}
TEST_NOTNULL(NULL);
TEST_NOTNULL(nullptr);
}

// clang-format off
Expand Down Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions tests/test_assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -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) && \
Expand Down
67 changes: 54 additions & 13 deletions tests/test_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
vglavnyy marked this conversation as resolved.
Show resolved Hide resolved
#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);
}
}

Expand Down