jkriegshauser | 961a542 | 2020-11-04 08:13:29 -0800 | [diff] [blame] | 1 | #include <doctest/doctest.h> |
| 2 | |
| 3 | #include "header.h" |
| 4 | |
| 5 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN |
| 6 | #include <cstdint> |
| 7 | #include <sstream> |
| 8 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END |
| 9 | |
| 10 | namespace |
| 11 | { |
| 12 | |
| 13 | enum StandardEnum |
| 14 | { |
| 15 | Zero, |
| 16 | One, |
| 17 | Two, |
| 18 | }; |
| 19 | |
| 20 | enum TypedEnum : int64_t |
| 21 | { |
| 22 | TypedZero, |
| 23 | TypedOne, |
| 24 | TypedTwo, |
| 25 | }; |
| 26 | |
| 27 | enum class EnumClassC : char |
| 28 | { |
| 29 | Zero = '0', |
| 30 | One = '1', |
| 31 | Two = '2', |
| 32 | }; |
| 33 | |
| 34 | enum class EnumClassSC : signed char |
| 35 | { |
| 36 | Zero = '0', |
| 37 | One = '1', |
| 38 | Two = '2', |
| 39 | }; |
| 40 | |
| 41 | enum class EnumClassUC : unsigned char |
| 42 | { |
| 43 | Zero = '0', |
| 44 | One = '1', |
| 45 | Two = '2', |
| 46 | }; |
| 47 | |
| 48 | enum class EnumClassU8 : uint8_t |
| 49 | { |
| 50 | Zero, |
| 51 | One, |
| 52 | Two, |
| 53 | }; |
| 54 | |
| 55 | template<class E, class T = typename std::underlying_type<E>::type> |
| 56 | T printable(E val) |
| 57 | { |
| 58 | return T(val); |
| 59 | } |
| 60 | |
| 61 | } |
| 62 | |
| 63 | TEST_CASE("enum 1") |
| 64 | { |
| 65 | std::ostringstream ostr; |
| 66 | ostr << Zero << One << Two; |
| 67 | ostr << TypedZero << TypedOne << TypedTwo; |
| 68 | static_assert(std::is_enum<EnumClassSC>::value, ""); |
| 69 | ostr << printable(EnumClassSC::Zero) << printable(EnumClassSC::One) << printable(EnumClassSC::Two); |
| 70 | |
| 71 | CHECK_EQ(Zero, 0); |
| 72 | CHECK_EQ(One, 1); |
| 73 | CHECK_EQ(Two, 2); |
| 74 | |
| 75 | CHECK_EQ(TypedZero, 0); |
| 76 | CHECK_EQ(TypedOne, 1); |
| 77 | CHECK_EQ(TypedTwo, 2); |
| 78 | |
| 79 | CHECK_EQ(EnumClassSC::Zero, EnumClassSC::Zero); |
| 80 | CHECK_EQ(EnumClassSC::One, EnumClassSC::One); |
| 81 | CHECK_EQ(EnumClassSC::Two, EnumClassSC::Two); |
| 82 | } |
| 83 | |
| 84 | TEST_CASE("enum 2" * doctest::should_fail()) |
| 85 | { |
| 86 | CHECK_EQ(Zero, 1); |
| 87 | CHECK_EQ(One, 2); |
| 88 | CHECK_EQ(Two, 3); |
| 89 | |
| 90 | CHECK_EQ(TypedZero, 1); |
| 91 | CHECK_EQ(TypedOne, 2); |
| 92 | CHECK_EQ(TypedTwo, 3); |
| 93 | |
| 94 | CHECK_EQ(EnumClassC::Zero, EnumClassC::One); |
| 95 | CHECK_EQ(EnumClassC::One, EnumClassC::Two); |
| 96 | CHECK_EQ(EnumClassC::Two, EnumClassC::Zero); |
| 97 | |
| 98 | CHECK_EQ(EnumClassSC::Zero, EnumClassSC::One); |
| 99 | CHECK_EQ(EnumClassSC::One, EnumClassSC::Two); |
| 100 | CHECK_EQ(EnumClassSC::Two, EnumClassSC::Zero); |
| 101 | |
| 102 | CHECK_EQ(EnumClassUC::Zero, EnumClassUC::One); |
| 103 | CHECK_EQ(EnumClassUC::One, EnumClassUC::Two); |
| 104 | CHECK_EQ(EnumClassUC::Two, EnumClassUC::Zero); |
| 105 | |
| 106 | CHECK_EQ(EnumClassU8::Zero, EnumClassU8::One); |
| 107 | CHECK_EQ(EnumClassU8::One, EnumClassU8::Two); |
| 108 | CHECK_EQ(EnumClassU8::Two, EnumClassU8::Zero); |
| 109 | } |