diff --git a/enum.h b/enum.h index 9232e9d7..743a7315 100644 --- a/enum.h +++ b/enum.h @@ -942,16 +942,48 @@ inline bool operator <(const Enum &a, const Enum &b) \ { return a._to_integral() < b._to_integral(); } \ \ BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \ +inline bool operator <(const Enum &a, const Enum::_enumerated &b) \ + { return a._to_integral() < b; } \ + \ +BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \ +inline bool operator <(const Enum::_enumerated &a, const Enum &b) \ + { return a < b._to_integral(); } \ + \ +BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \ inline bool operator <=(const Enum &a, const Enum &b) \ { return a._to_integral() <= b._to_integral(); } \ \ BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \ +inline bool operator <=(const Enum &a, const Enum::_enumerated &b) \ + { return a._to_integral() <= b; } \ + \ +BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \ +inline bool operator <=(const Enum::_enumerated &a, const Enum &b) \ + { return a <= b._to_integral(); } \ + \ +BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \ inline bool operator >(const Enum &a, const Enum &b) \ { return a._to_integral() > b._to_integral(); } \ \ BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \ +inline bool operator >(const Enum &a, const Enum::_enumerated &b) \ + { return a._to_integral() > b; } \ + \ +BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \ +inline bool operator >(const Enum::_enumerated &a, const Enum &b) \ + { return a > b._to_integral(); } \ + \ +BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \ inline bool operator >=(const Enum &a, const Enum &b) \ { return a._to_integral() >= b._to_integral(); } \ + \ +BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \ +inline bool operator >=(const Enum &a, const Enum::_enumerated &b) \ + { return a._to_integral() >= b; } \ + \ +BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \ +inline bool operator >=(const Enum::_enumerated &a, const Enum &b) \ + { return a >= b._to_integral(); } \ BETTER_ENUMS_IGNORE_ATTRIBUTES_END \ \ \ diff --git a/test/cxxtest/general.h b/test/cxxtest/general.h index 014fcec3..6be26ec3 100644 --- a/test/cxxtest/general.h +++ b/test/cxxtest/general.h @@ -200,7 +200,7 @@ class EnumTests : public CxxTest::TestSuite { TS_ASSERT_EQUALS(Channel::_from_integral_unchecked(1), +Channel::Green); TS_ASSERT_DIFFERS(Channel::_from_integral_unchecked(1), +Channel::Blue); - TS_ASSERT_THROWS(Channel::_from_integral(3), std::runtime_error); + TS_ASSERT_THROWS(Channel::_from_integral(3), std::runtime_error const&); TS_ASSERT_THROWS_NOTHING(Channel::_from_integral_unchecked(3)); better_enums::optional maybe_channel = @@ -225,7 +225,7 @@ class EnumTests : public CxxTest::TestSuite { TS_ASSERT_DIFFERS(Channel::_from_string("Green"), +Channel::Blue); TS_ASSERT_EQUALS(Channel::_from_string("Blue"), +Channel::Blue); TS_ASSERT_DIFFERS(Channel::_from_string("Blue"), +Channel::Green); - TS_ASSERT_THROWS(Channel::_from_string("green"), std::runtime_error); + TS_ASSERT_THROWS(Channel::_from_string("green"), std::runtime_error const&); better_enums::optional maybe_channel = Channel::_from_string_nothrow("Green"); @@ -241,7 +241,7 @@ class EnumTests : public CxxTest::TestSuite { +Channel::Blue); TS_ASSERT_DIFFERS(Channel::_from_string_nocase("blue"), +Channel::Green); - TS_ASSERT_THROWS(Channel::_from_string_nocase("a"), std::runtime_error); + TS_ASSERT_THROWS(Channel::_from_string_nocase("a"), std::runtime_error const&); maybe_channel = Channel::_from_string_nocase_nothrow("green"); TS_ASSERT(maybe_channel); @@ -354,16 +354,16 @@ class EnumTests : public CxxTest::TestSuite { TS_ASSERT_EQUALS((+Channel::Red), Channel::_from_index(0)); TS_ASSERT_EQUALS((+Channel::Green), Channel::_from_index(1)); TS_ASSERT_EQUALS((+Channel::Blue), Channel::_from_index(2)); - TS_ASSERT_THROWS(Channel::_from_index(42), std::runtime_error); + TS_ASSERT_THROWS(Channel::_from_index(42), std::runtime_error const&); TS_ASSERT_EQUALS((+Depth::HighColor), Depth::_from_index(0)); TS_ASSERT_EQUALS((+Depth::TrueColor), Depth::_from_index(1)); - TS_ASSERT_THROWS(Depth::_from_index(42), std::runtime_error); + TS_ASSERT_THROWS(Depth::_from_index(42), std::runtime_error const&); TS_ASSERT_EQUALS((+Compression::None), Compression::_from_index(0)); TS_ASSERT_EQUALS((+Compression::Huffman), Compression::_from_index(1)); TS_ASSERT_EQUALS((+Compression::Default), Compression::_from_index(2)); - TS_ASSERT_THROWS(Compression::_from_index(42), std::runtime_error); + TS_ASSERT_THROWS(Compression::_from_index(42), std::runtime_error const&); } void test_from_index_nothrow() @@ -412,7 +412,6 @@ class EnumTests : public CxxTest::TestSuite { void test_from_index_unchecked() { - TS_ASSERT_EQUALS((+Channel::Red), Channel::_from_index_unchecked(0)); TS_ASSERT_EQUALS((+Channel::Green), Channel::_from_index_unchecked(1)); TS_ASSERT_EQUALS((+Channel::Blue), Channel::_from_index_unchecked(2)); @@ -424,6 +423,39 @@ class EnumTests : public CxxTest::TestSuite { TS_ASSERT_EQUALS((+Compression::Huffman), Compression::_from_index_unchecked(1)); TS_ASSERT_EQUALS((+Compression::Default), Compression::_from_index_unchecked(2)); } + + void test_comparator_operators() + { + Channel red = Channel::Red, blue = Channel::Blue; + + TS_ASSERT_LESS_THAN(red, Channel::Green); + TS_ASSERT_LESS_THAN(Channel::Green, blue); + TS_ASSERT(!(red < Channel::Red)); + TS_ASSERT(!(Channel::Blue < blue)); + TS_ASSERT(!(blue < Channel::Green)); + TS_ASSERT(!(Channel::Green < red)); + + TS_ASSERT_LESS_THAN_EQUALS(red, Channel::Green); + TS_ASSERT_LESS_THAN_EQUALS(Channel::Green, blue); + TS_ASSERT_LESS_THAN_EQUALS(red, Channel::Red); + TS_ASSERT_LESS_THAN_EQUALS(Channel::Blue, blue); + TS_ASSERT(!(blue <= Channel::Green)); + TS_ASSERT(!(Channel::Green <= red)); + + TS_ASSERT(!(red > Channel::Green)); + TS_ASSERT(!(Channel::Green > blue)); + TS_ASSERT(!(red > Channel::Red)); + TS_ASSERT(!(Channel::Blue > blue)); + TS_ASSERT(blue > Channel::Green); + TS_ASSERT(Channel::Green > red); + + TS_ASSERT(!(red >= Channel::Green)); + TS_ASSERT(!(Channel::Green >= blue)); + TS_ASSERT(red >= Channel::Red); + TS_ASSERT(Channel::Blue >= blue); + TS_ASSERT(blue >= Channel::Green); + TS_ASSERT(Channel::Green >= red); + } };