Skip to content

Commit

Permalink
Non-magic enum enum test
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed Jun 1, 2024
1 parent 69684ac commit b56a342
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
44 changes: 37 additions & 7 deletions tests/unit/assertion_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 <LOCATION>:
| 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 <LOCATION>:
| 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 <LOCATION>:
| 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<int> a = { 2, 2, 4, 6, 10 };
Expand Down

0 comments on commit b56a342

Please sign in to comment.