From b56a342ba002427ab70a650d35de23fc86dba7d2 Mon Sep 17 00:00:00 2001 From: Jeremy <51220084+jeremy-rifkin@users.noreply.github.com> Date: Fri, 31 May 2024 21:35:40 -0500 Subject: [PATCH] Non-magic enum enum test --- CMakeLists.txt | 10 ++++++-- tests/CMakeLists.txt | 4 ++-- tests/unit/assertion_tests.cpp | 44 ++++++++++++++++++++++++++++------ 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d4ca2f5..6186ecfc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -163,8 +163,14 @@ if(LIBASSERT_SANITIZER_BUILD) add_link_options(-fsanitize=address) endif() -# add pre-processor definitions corresponding to CMake options -# fixme: supposedly generator expressions should work for this +if( + LIBASSERT_BUILD_TESTING AND NOT ( + "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND "${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 9.0 + ) +) + set(LIBASSERT_USE_MAGIC_ENUM ON) +endif() + if(LIBASSERT_USE_MAGIC_ENUM) set(MAGIC_ENUM_OPT_INSTALL ON) if(LIBASSERT_USE_EXTERNAL_MAGIC_ENUM) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d0244621..a0a99df3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -19,7 +19,7 @@ if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) ) target_link_libraries(demo PRIVATE libassert-lib) target_compile_definitions(demo PRIVATE LIBASSERT_LOWERCASE) - if(NOT ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND "${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 9.0)) + if(LIBASSERT_USE_MAGIC_ENUM) target_compile_definitions(demo PRIVATE LIBASSERT_USE_MAGIC_ENUM) endif() target_compile_features( @@ -31,7 +31,7 @@ if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) PUBLIC LIBASSERT_SAFE_COMPARISONS ) - if(NOT ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND "${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 9.0)) + if(LIBASSERT_USE_MAGIC_ENUM) add_executable(integration tests/integration/integration.cpp tests/integration/a.cpp tests/integration/x/a.cpp) # Temporary workaround for Visual Studio 2022 bug with __builtin_LINE() and __builtin_FILE() # https://developercommunity.visualstudio.com/t/__builtin_LINE-function-is-reporting-w/10439054?space=62&q=__builtin_function diff --git a/tests/unit/assertion_tests.cpp b/tests/unit/assertion_tests.cpp index 12313a04..a384e764 100644 --- a/tests/unit/assertion_tests.cpp +++ b/tests/unit/assertion_tests.cpp @@ -708,10 +708,11 @@ TEST(LibassertBasic, LvalueForwarding) { EXPECT_EQ(x, 0); } +#ifdef LIBASSERT_USE_MAGIC_ENUM +enum foo_e { A, B }; +enum class bar_e { A, B }; TEST(LibassertBasic, EnumHandling) { - enum foo { A, B }; - enum class bar { A, B }; - foo a = A; + foo_e a = A; CHECK( DEBUG_ASSERT(a != A), R"XX( @@ -721,18 +722,47 @@ TEST(LibassertBasic, EnumHandling) { | a => A )XX" ); - bar b = bar::A; + bar_e b = bar_e::A; CHECK( - DEBUG_ASSERT(b != bar::A), + DEBUG_ASSERT(b != bar_e::A), R"XX( |Debug Assertion failed at : | DEBUG_ASSERT(b != bar::A); | Where: - | b => A - | bar::A => A + | b => A + | bar_e::A => A )XX" ); } +#else +// TODO: Also test this outside of gcc 8 +enum foo_e { A, B }; +enum class bar_e { A, B }; +TEST(LibassertBasic, EnumHandling) { + foo_e a = A; + CHECK( + DEBUG_ASSERT(a != A), + R"XX( + |Debug Assertion failed at : + | DEBUG_ASSERT(a != A); + | Where: + | a => enum foo_e: 0 + | A => enum foo_e: 0 + )XX" + ); + bar_e b = bar_e::A; + CHECK( + DEBUG_ASSERT(b != bar_e::A), + R"XX( + |Debug Assertion failed at : + | DEBUG_ASSERT(b != bar_e::A); + | Where: + | b => enum bar_e: 0 + | bar_e::A => enum bar_e: 0 + )XX" + ); +} +#endif TEST(LibassertBasic, Containers) { std::set a = { 2, 2, 4, 6, 10 };