diff --git a/test/test.cpp b/test/test.cpp index 359767b..9451bac 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -33,7 +33,7 @@ enum class Color { RED = -12, GREEN = 7, BLUE = 15 }; -enum class Numbers : int { one = 10, two = 20, three = 30, many = 127 }; +enum class Numbers : int { one = 1, two, three, many = 127 }; enum Directions { Up = 85, Down = -42, Right = 120, Left = -120 }; @@ -53,10 +53,6 @@ static_assert(is_magic_enum_supported, "magic_enum: Unsupported compiler (https: TEST_CASE("enum_cast") { SECTION("string") { -#if defined(_MSC_VER) && _MSC_VER < 1920 -# define constexpr // Visual Studio 2017 have bug with string_view constexpr compare. -#endif - constexpr auto cr = enum_cast("RED"); REQUIRE(cr.value() == Color::RED); REQUIRE(enum_cast("GREEN").value() == Color::GREEN); @@ -83,21 +79,20 @@ TEST_CASE("enum_cast") { REQUIRE(nt.value() == number::three); REQUIRE_FALSE(enum_cast("four").has_value()); REQUIRE_FALSE(enum_cast("None").has_value()); - -#undef constexpr } SECTION("integer") { + Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE}; constexpr auto cr = enum_cast(-12); REQUIRE(cr.value() == Color::RED); REQUIRE(enum_cast(7).value() == Color::GREEN); - REQUIRE(enum_cast(15).value() == Color::BLUE); + REQUIRE(enum_cast((int)cm[2]).value() == Color::BLUE); REQUIRE_FALSE(enum_cast(0).has_value()); - constexpr auto no = enum_cast(10); + constexpr auto no = enum_cast(1); REQUIRE(no.value() == Numbers::one); - REQUIRE(enum_cast(20).value() == Numbers::two); - REQUIRE(enum_cast(30).value() == Numbers::three); + REQUIRE(enum_cast(2).value() == Numbers::two); + REQUIRE(enum_cast(3).value() == Numbers::three); REQUIRE_FALSE(enum_cast(127).has_value()); REQUIRE_FALSE(enum_cast(0).has_value()); @@ -118,16 +113,17 @@ TEST_CASE("enum_cast") { } TEST_CASE("enum_integer") { + Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE}; constexpr auto cr = enum_integer(Color::RED); REQUIRE(cr == -12); REQUIRE(enum_integer(Color::GREEN) == 7); - REQUIRE(enum_integer(Color::BLUE) == 15); + REQUIRE(enum_integer(cm[2]) == 15); REQUIRE(enum_integer(static_cast(0)) == 0); constexpr auto no = enum_integer(Numbers::one); - REQUIRE(no == 10); - REQUIRE(enum_integer(Numbers::two) == 20); - REQUIRE(enum_integer(Numbers::three) == 30); + REQUIRE(no == 1); + REQUIRE(enum_integer(Numbers::two) == 2); + REQUIRE(enum_integer(Numbers::three) == 3); REQUIRE(enum_integer(Numbers::many) == 127); REQUIRE(enum_integer(static_cast(0)) == 0); @@ -146,6 +142,36 @@ TEST_CASE("enum_integer") { REQUIRE(enum_integer(static_cast(0)) == 0); } +TEST_CASE("enum_index") { + Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE}; + constexpr auto cr = enum_index(Color::RED); + REQUIRE(cr.value() == 0); + REQUIRE(enum_index(Color::GREEN).value() == 1); + REQUIRE(enum_index(cm[2]).value() == 2); + REQUIRE_FALSE(enum_index(static_cast(0)).has_value()); + + constexpr auto no = enum_index(Numbers::one); + REQUIRE(no.value() == 0); + REQUIRE(enum_index(Numbers::two).value() == 1); + REQUIRE(enum_index(Numbers::three).value() == 2); + REQUIRE_FALSE(enum_index(Numbers::many).has_value()); + REQUIRE_FALSE(enum_index(static_cast(0)).has_value()); + + constexpr auto dr = enum_index(Directions::Right); + REQUIRE(enum_index(Directions::Left).value() == 0); + REQUIRE(enum_index(Directions::Down).value() == 1); + REQUIRE(enum_index(Directions::Up).value() == 2); + REQUIRE(dr.value() == 3); + REQUIRE_FALSE(enum_index(static_cast(0)).has_value()); + + constexpr auto nt = enum_index(number::three); + REQUIRE(enum_index(number::one).value() == 0); + REQUIRE(enum_index(number::two).value() == 1); + REQUIRE(nt.value() == 2); + REQUIRE_FALSE(enum_index(number::four).has_value()); + REQUIRE_FALSE(enum_index(static_cast(0)).has_value()); +} + TEST_CASE("enum_value") { constexpr auto cr = enum_value(0); REQUIRE(cr == Color::RED); @@ -433,3 +459,12 @@ TEST_CASE("type_traits") { REQUIRE_FALSE(is_fixed_enum_v); REQUIRE(is_fixed_enum_v); } + +TEST_CASE("enum_traits") { + SECTION("type_name") { + REQUIRE(enum_traits::type_name == "Color"); + REQUIRE(enum_traits::type_name == "Numbers"); + REQUIRE(enum_traits::type_name == "Directions"); + REQUIRE(enum_traits::type_name == "number"); + } +}