Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more comparative operators between Enum and enumerated value #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
\
\
Expand Down
46 changes: 39 additions & 7 deletions test/cxxtest/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Channel> maybe_channel =
Expand All @@ -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<Channel> maybe_channel =
Channel::_from_string_nothrow("Green");
Expand All @@ -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);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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));
Expand All @@ -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);
}
};


Expand Down