Skip to content

Commit

Permalink
Support using better-enum as key in dictionaries and maps (#77)
Browse files Browse the repository at this point in the history
This enabled hashing so that unordered_map are supported.
  • Loading branch information
svnscha authored and aantron committed Jul 16, 2019
1 parent 8a0d376 commit 5a677ac
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
11 changes: 11 additions & 0 deletions enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -1283,4 +1283,15 @@ BETTER_ENUMS_CONSTEXPR_ map<Enum, T> make_map(T (*f)(Enum))

}

#define BETTER_ENUMS_DECLARE_STD_HASH(type) \
namespace std { \
template <> struct hash<type> \
{ \
size_t operator()(const type &x) const \
{ \
return std::hash<size_t>()(x._to_integral()); \
} \
}; \
}

#endif // #ifndef BETTER_ENUMS_ENUM_H
55 changes: 38 additions & 17 deletions test/cxxtest/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ BETTER_ENUM(Namespaced, short, One, Two)
// be changed to be more precise in the future.
#ifdef BETTER_ENUMS_HAVE_CONSTEXPR_

BETTER_ENUMS_DECLARE_STD_HASH(Channel)

#include <type_traits>
#include <functional>

// Type properties.
static_assert_1(std::is_class<Channel>());
Expand Down Expand Up @@ -161,6 +164,24 @@ static_assert_1(same_string(Depth::_names()[0], "HighColor"));


// Run-time testing.
class HashTests : public CxxTest::TestSuite {
public:
void test_same_values()
{
#ifdef _ENUM_HAVE_CONSTEXPR
TS_ASSERT_EQUALS(
std::hash<Channel>().operator()(Channel::Red),
std::hash<int>().operator()(0));
TS_ASSERT_EQUALS(
std::hash<Channel>().operator()(Channel::Green),
std::hash<int>().operator()(1));
TS_ASSERT_EQUALS(
std::hash<Channel>().operator()(Channel::Blue),
std::hash<int>().operator()(2));
#endif // #ifdef _ENUM_HAVE_CONSTEXPR
}
};

class EnumTests : public CxxTest::TestSuite {
public:
void test_constant_values()
Expand Down Expand Up @@ -313,59 +334,59 @@ class EnumTests : public CxxTest::TestSuite {
+test::Namespaced::One);
TS_ASSERT_EQUALS(strcmp(*test::Namespaced::_names().begin(), "One"), 0);
}

void test_to_index()
{
TS_ASSERT_EQUALS((+Channel::Red)._to_index(), 0);
TS_ASSERT_EQUALS((+Channel::Green)._to_index(), 1);
TS_ASSERT_EQUALS((+Channel::Blue)._to_index(), 2);

TS_ASSERT_EQUALS((+Depth::HighColor)._to_index(), 0);
TS_ASSERT_EQUALS((+Depth::TrueColor)._to_index(), 1);

TS_ASSERT_EQUALS((+Compression::None)._to_index(), 0);
TS_ASSERT_EQUALS((+Compression::Huffman)._to_index(), 1);
// TS_ASSERT_EQUALS((+Compression::Default)._to_index(), 2); // This won't pass as Compression::Huffman == Compression::Default
}

void test_from_index()
{
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_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_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);
}

void test_from_index_nothrow()
{
better_enums::optional<Channel> maybe_channel = Channel::_from_index_nothrow(0);
TS_ASSERT(maybe_channel);
TS_ASSERT_EQUALS(*maybe_channel, +Channel::Red);

maybe_channel = Channel::_from_index_nothrow(1);
TS_ASSERT(maybe_channel);
TS_ASSERT_EQUALS(*maybe_channel, +Channel::Green);

maybe_channel = Channel::_from_index_nothrow(2);
TS_ASSERT(maybe_channel);
TS_ASSERT_EQUALS(*maybe_channel, +Channel::Blue);

maybe_channel = Channel::_from_index_nothrow(45);
TS_ASSERT(!maybe_channel);

better_enums::optional<Depth> maybe_depth = Depth::_from_index_nothrow(0);
TS_ASSERT(maybe_depth);
TS_ASSERT_EQUALS(*maybe_depth, +Depth::HighColor);

maybe_depth = Depth::_from_index_nothrow(1);
TS_ASSERT(maybe_depth);
TS_ASSERT_EQUALS(*maybe_depth, +Depth::TrueColor);
Expand All @@ -376,29 +397,29 @@ class EnumTests : public CxxTest::TestSuite {
better_enums::optional<Compression> maybe_compression = Compression::_from_index_nothrow(0);
TS_ASSERT(maybe_compression);
TS_ASSERT_EQUALS(*maybe_compression, +Compression::None);

maybe_compression = Compression::_from_index_nothrow(1);
TS_ASSERT(maybe_compression);
TS_ASSERT_EQUALS(*maybe_compression, +Compression::Huffman);

maybe_compression = Compression::_from_index_nothrow(2);
TS_ASSERT(maybe_compression);
TS_ASSERT_EQUALS(*maybe_compression, +Compression::Default);

maybe_compression = Compression::_from_index_nothrow(45);
TS_ASSERT(!maybe_compression);
}

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));

TS_ASSERT_EQUALS((+Depth::HighColor), Depth::_from_index_unchecked(0));
TS_ASSERT_EQUALS((+Depth::TrueColor), Depth::_from_index_unchecked(1));

TS_ASSERT_EQUALS((+Compression::None), Compression::_from_index_unchecked(0));
TS_ASSERT_EQUALS((+Compression::Huffman), Compression::_from_index_unchecked(1));
TS_ASSERT_EQUALS((+Compression::Default), Compression::_from_index_unchecked(2));
Expand Down

0 comments on commit 5a677ac

Please sign in to comment.