1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
10 
11 // UNSUPPORTED: c++03, c++11
12 
13 // <functional>
14 
15 // make sure that we can hash enumeration values
16 // Not very portable
17 
18 #include "test_macros.h"
19 
20 #include <functional>
21 #include <cassert>
22 #include <type_traits>
23 #include <limits>
24 
25 enum class Colors { red, orange, yellow, green, blue, indigo, violet };
26 enum class Cardinals { zero, one, two, three, five=5 };
27 enum class LongColors : short { red, orange, yellow, green, blue, indigo, violet };
28 enum class ShortColors : long { red, orange, yellow, green, blue, indigo, violet };
29 enum class EightBitColors : uint8_t { red, orange, yellow, green, blue, indigo, violet };
30 
31 enum Fruits { apple, pear, grape, mango, cantaloupe };
32 
33 template <class T>
34 void
35 test()
36 {
37     typedef std::hash<T> H;
38 #if TEST_STD_VER <= 17
39     static_assert((std::is_same<typename H::argument_type, T>::value), "");
40     static_assert((std::is_same<typename H::result_type, std::size_t>::value), "");
41 #endif
42     ASSERT_NOEXCEPT(H()(T()));
43     typedef typename std::underlying_type<T>::type under_type;
44 
45     H h1;
46     std::hash<under_type> h2;
47     for (int i = 0; i <= 5; ++i)
48     {
49         T t(static_cast<T> (i));
50         const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings
51         if (small)
52             assert(h1(t) == h2(static_cast<under_type>(i)));
53     }
54 }
55 
56 int main(int, char**)
57 {
58     test<Cardinals>();
59 
60     test<Colors>();
61     test<ShortColors>();
62     test<LongColors>();
63     test<EightBitColors>();
64 
65     test<Fruits>();
66 
67   return 0;
68 }
69