#include #include #include namespace NYT { namespace { //////////////////////////////////////////////////////////////////////////////// DEFINE_ENUM(ESimple, (X) (Y) (Z) ); DEFINE_ENUM(EColor, ((Red) (10)) ((Green)(20)) ((Blue) (30)) (Black) (White) ); DEFINE_BIT_ENUM(EFlag, ((_1)(0x0001)) ((_2)(0x0002)) ((_3)(0x0004)) ((_4)(0x0008)) ); DEFINE_AMBIGUOUS_ENUM_WITH_UNDERLYING_TYPE(EMultipleNames, int, (A1) ((A2)(0)) (B) (C) ((D1)(100)) ((D2)(100)) ); DEFINE_ENUM(ECustomString, ((A) (1) ("1_a")) ((B) (2) ("1_b")) ); DEFINE_ENUM_WITH_UNDERLYING_TYPE(ECardinal, char, ((West) (0)) ((North) (1)) ((East) (2)) ((South) (3)) ); //////////////////////////////////////////////////////////////////////////////// template std::vector ToVector(std::array array) { return std::vector(array.begin(), array.end()); } TEST(TEnumTest, Domain) { EXPECT_EQ(3, TEnumTraits::GetDomainSize()); std::vector v { ESimple::X, ESimple::Y, ESimple::Z }; EXPECT_EQ(v, ToVector(TEnumTraits::GetDomainValues())); EXPECT_EQ(ESimple::X, TEnumTraits::GetMinValue()); EXPECT_EQ(ESimple::Z, TEnumTraits::GetMaxValue()); } TEST(TEnumTest, Basic) { EXPECT_EQ(0, static_cast(ESimple::X)); EXPECT_EQ(1, static_cast(ESimple::Y)); EXPECT_EQ(2, static_cast(ESimple::Z)); EXPECT_EQ(0, static_cast(EColor( ))); EXPECT_EQ(5, static_cast(EColor(5))); EXPECT_EQ(10, static_cast(EColor::Red )); EXPECT_EQ(20, static_cast(EColor::Green)); EXPECT_EQ(30, static_cast(EColor::Blue )); EXPECT_EQ(31, static_cast(EColor::Black)); EXPECT_EQ(32, static_cast(EColor::White)); } TEST(TEnumTest, ToString) { EXPECT_EQ("EColor(0)", ToString(EColor( ))); EXPECT_EQ("EColor(5)", ToString(EColor(5))); EXPECT_EQ("Red", ToString(EColor(EColor::Red ))); EXPECT_EQ("Green", ToString(EColor::Green)); EXPECT_EQ("Blue", ToString(EColor(EColor::Blue ))); EXPECT_EQ("Black", ToString(EColor::Black)); EXPECT_EQ("White", ToString(EColor::White)); } TEST(TEnumTest, FromString) { EXPECT_EQ(EColor::Red , TEnumTraits::FromString("Red" )); EXPECT_EQ(EColor::Green, TEnumTraits::FromString("Green")); EXPECT_EQ(EColor::Blue , TEnumTraits::FromString("Blue" )); EXPECT_EQ(EColor::Black, TEnumTraits::FromString("Black")); EXPECT_EQ(EColor::White, TEnumTraits::FromString("White")); EXPECT_THROW(TEnumTraits::FromString("Pink"), std::exception); EXPECT_EQ(EColor::Red, TEnumTraits::FindValueByLiteral("Red")); EXPECT_EQ(std::nullopt, TEnumTraits::FindValueByLiteral("Pink")); } TEST(TEnumTest, Ordering) { ESimple a(ESimple::X); ESimple b(ESimple::Y); ESimple c(ESimple::Y); ESimple d(ESimple::Z); EXPECT_FALSE(a < a); EXPECT_FALSE(a > a); EXPECT_TRUE (a < b); EXPECT_TRUE (b > a); EXPECT_TRUE (a < c); EXPECT_TRUE (c > a); EXPECT_TRUE (a < d); EXPECT_TRUE (d > a); EXPECT_FALSE(b < a); EXPECT_FALSE(a > b); EXPECT_FALSE(b < b); EXPECT_FALSE(b > b); EXPECT_FALSE(b < c); EXPECT_FALSE(c > b); EXPECT_TRUE (b < d); EXPECT_TRUE (d > b); EXPECT_FALSE(c < a); EXPECT_FALSE(a > c); EXPECT_FALSE(c < b); EXPECT_FALSE(b > c); EXPECT_FALSE(c < c); EXPECT_FALSE(c > c); EXPECT_TRUE (c < d); EXPECT_TRUE (d > c); EXPECT_FALSE(d < a); EXPECT_FALSE(a > d); EXPECT_FALSE(d < b); EXPECT_FALSE(b > d); EXPECT_FALSE(d < c); EXPECT_FALSE(c > d); EXPECT_FALSE(d < d); EXPECT_FALSE(d > d); EXPECT_TRUE (a <= b); EXPECT_TRUE (b <= c); EXPECT_TRUE (c <= d); EXPECT_TRUE (a == a); EXPECT_FALSE(a == b); EXPECT_TRUE (b == c); EXPECT_FALSE(c == d); EXPECT_FALSE(d == a); EXPECT_FALSE(a != a); EXPECT_TRUE (a != b); EXPECT_FALSE(b != c); EXPECT_TRUE (c != d); EXPECT_TRUE (d != a); } TEST(TEnumTest, OrderingWithDomainValues) { EColor color(EColor::Black); EXPECT_LT(EColor::Red, color); EXPECT_LT(color, EColor::White); EXPECT_GT(color, EColor::Red); EXPECT_GT(EColor::White, color); EXPECT_LE(EColor::Red, color); EXPECT_LE(color, EColor::White); EXPECT_GE(EColor::White, color); EXPECT_GE(color, EColor::Red); EXPECT_EQ(color, EColor::Black); EXPECT_EQ(EColor::Black, color); EXPECT_NE(color, EColor::Blue); EXPECT_NE(EColor::Blue, color); } TEST(TEnumTest, DomainSize) { EXPECT_EQ(3, TEnumTraits::GetDomainSize()); EXPECT_EQ(5, TEnumTraits::GetDomainSize()); } TEST(TEnumTest, DomainValues) { std::vector simpleValues; simpleValues.push_back(ESimple::X); simpleValues.push_back(ESimple::Y); simpleValues.push_back(ESimple::Z); EXPECT_EQ(simpleValues, ToVector(TEnumTraits::GetDomainValues())); std::vector colorValues; colorValues.push_back(EColor::Red); colorValues.push_back(EColor::Green); colorValues.push_back(EColor::Blue); colorValues.push_back(EColor::Black); colorValues.push_back(EColor::White); EXPECT_EQ(colorValues, ToVector(TEnumTraits::GetDomainValues())); } TEST(TEnumTest, Decompose1) { auto f = EFlag(0); std::vector ff { }; EXPECT_EQ(TEnumTraits::Decompose(f), ff); } TEST(TEnumTest, Decompose2) { auto f = EFlag::_1; std::vector ff {EFlag::_1}; EXPECT_EQ(TEnumTraits::Decompose(f), ff); } TEST(TEnumTest, Decompose3) { auto f = EFlag(EFlag::_1|EFlag::_2); std::vector ff{EFlag::_1, EFlag::_2}; EXPECT_EQ(TEnumTraits::Decompose(f), ff); } TEST(TEnumTest, Decompose4) { auto f = EFlag(EFlag::_2|EFlag::_4); std::vector ff{EFlag::_2, EFlag::_4}; EXPECT_EQ(TEnumTraits::Decompose(f), ff); } TEST(TEnumTest, MultipleNames) { EXPECT_EQ(EMultipleNames::A1, TEnumTraits::FromString("A1")); EXPECT_EQ(EMultipleNames::A1, TEnumTraits::FromString("A2")); EXPECT_EQ(EMultipleNames::B, TEnumTraits::FromString("B")); EXPECT_EQ(EMultipleNames::C, TEnumTraits::FromString("C")); EXPECT_EQ(EMultipleNames::D1, TEnumTraits::FromString("D1")); EXPECT_EQ(EMultipleNames::D1, TEnumTraits::FromString("D2")); EXPECT_EQ("A1", ToString(EMultipleNames::A1)); EXPECT_EQ("A1", ToString(EMultipleNames::A2)); EXPECT_EQ("B", ToString(EMultipleNames::B)); EXPECT_EQ("C", ToString(EMultipleNames::C)); EXPECT_EQ("D1", ToString(EMultipleNames::D1)); EXPECT_EQ("D1", ToString(EMultipleNames::D2)); } TEST(TEnumTest, CustomString) { EXPECT_EQ(ECustomString::A, TEnumTraits::FromString("1_a")); EXPECT_EQ(ECustomString::B, TEnumTraits::FromString("1_b")); EXPECT_EQ("1_a", ToString(ECustomString::A)); EXPECT_EQ("1_b", ToString(ECustomString::B)); } TEST(TEnumTest, Cast) { ECardinal cardinal; { char validValue = 2; EXPECT_TRUE(TryEnumCast(validValue, &cardinal)); EXPECT_EQ(cardinal, ECardinal::East); } { char invalidValue = 100; EXPECT_FALSE(TryEnumCast(invalidValue, &cardinal)); } { int widerTypeValidValue = 3; EXPECT_TRUE(TryEnumCast(widerTypeValidValue, &cardinal)); EXPECT_EQ(cardinal, ECardinal::South); } { int widerTypeInvalueValue = (1 << 8) + 100; EXPECT_FALSE(TryEnumCast(widerTypeInvalueValue, &cardinal)); } } //////////////////////////////////////////////////////////////////////////////// } // namespace } // namespace NYT