1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-09 23:34:23 +00:00

add compile time enum_index (#159)

This commit is contained in:
Daniil Goncharov 2022-03-09 10:07:09 +02:00 committed by GitHub
parent 04a3d32d0f
commit b0c0e02bdc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 28 deletions

View file

@ -230,12 +230,14 @@ TEST_CASE("enum_index") {
constexpr auto cr = enum_index(Color::RED);
Color cg = Color::GREEN;
REQUIRE(cr.value() == 0);
REQUIRE(enum_index<Color::RED>() == 0);
REQUIRE(enum_index<Color&>(cg).value() == 1);
REQUIRE(enum_index(cm[2]).value() == 2);
REQUIRE_FALSE(enum_index(static_cast<Color>(0)).has_value());
constexpr auto no = enum_index(Numbers::one);
REQUIRE(no.value() == 0);
REQUIRE(enum_index<Numbers::one>() == 0);
REQUIRE(enum_index(Numbers::two).value() == 1);
REQUIRE(enum_index(Numbers::three).value() == 2);
REQUIRE_FALSE(enum_index(Numbers::many).has_value());
@ -243,6 +245,7 @@ TEST_CASE("enum_index") {
constexpr auto dr = enum_index(Directions::Right);
Directions dl = Directions::Left;
REQUIRE(enum_index<Directions::Left>() == 0);
REQUIRE(enum_index<Directions&>(dl).value() == 0);
REQUIRE(enum_index<const Directions>(Directions::Down).value() == 1);
REQUIRE(enum_index(Directions::Up).value() == 2);
@ -260,6 +263,7 @@ TEST_CASE("enum_index") {
#endif
constexpr auto nt = enum_index(number::three);
REQUIRE(enum_index<number::one>() == 0);
REQUIRE(enum_index(number::one).value() == 0);
REQUIRE(enum_index(number::two).value() == 1);
REQUIRE(nt.value() == 2);