1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03, c++11
11
12 // <functional>
13
14 // make sure that we can hash enumeration values
15 // Not very portable
16
17 #include "test_macros.h"
18
19 #include <functional>
20 #include <cassert>
21 #include <type_traits>
22 #include <limits>
23
24 enum class Colors { red, orange, yellow, green, blue, indigo, violet };
25 enum class Cardinals { zero, one, two, three, five=5 };
26 enum class LongColors : short { red, orange, yellow, green, blue, indigo, violet };
27 enum class ShortColors : long { red, orange, yellow, green, blue, indigo, violet };
28 enum class EightBitColors : uint8_t { red, orange, yellow, green, blue, indigo, violet };
29
30 enum Fruits { apple, pear, grape, mango, cantaloupe };
31
32 template <class T>
33 void
test()34 test()
35 {
36 typedef std::hash<T> H;
37 static_assert((std::is_same<typename H::argument_type, T>::value), "" );
38 static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
39 ASSERT_NOEXCEPT(H()(T()));
40 typedef typename std::underlying_type<T>::type under_type;
41
42 H h1;
43 std::hash<under_type> h2;
44 for (int i = 0; i <= 5; ++i)
45 {
46 T t(static_cast<T> (i));
47 const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings
48 if (small)
49 assert(h1(t) == h2(static_cast<under_type>(i)));
50 }
51 }
52
main()53 int main()
54 {
55 test<Cardinals>();
56
57 test<Colors>();
58 test<ShortColors>();
59 test<LongColors>();
60 test<EightBitColors>();
61
62 test<Fruits>();
63 }
64