blob: b5cd6f8454d14d5debed35f4da00077c7e594537 [file] [log] [blame]
Marshall Clow5155a562013-09-03 17:55:32 +00001//===----------------------------------------------------------------------===//
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// <functional>
11
12// make sure that we can hash enumeration values
13// Not very portable
14
15#include <__config>
16
17#if _LIBCPP_STD_VER > 11
18
19#include <functional>
20#include <cassert>
21#include <type_traits>
22#include <limits>
23
24enum class Colors { red, orange, yellow, green, blue, indigo, violet };
25enum class Cardinals { zero, one, two, three, five=5 };
26enum class LongColors : short { red, orange, yellow, green, blue, indigo, violet };
27enum class ShortColors : long { red, orange, yellow, green, blue, indigo, violet };
28enum class EightBitColors : uint8_t { red, orange, yellow, green, blue, indigo, violet };
29
30enum Fruits { apple, pear, grape, mango, cantaloupe };
31
32template <class T>
33void
34test()
35{
36 static_assert((std::is_base_of<std::unary_function<T, std::size_t>,
37 std::hash<T> >::value), "");
38 typedef typename std::underlying_type<T>::type under_type;
39
40 std::hash<T> h1;
41 std::hash<under_type> h2;
42 for (int i = 0; i <= 5; ++i)
43 {
44 T t(static_cast<T> (i));
45 if (sizeof(T) <= sizeof(std::size_t))
46 assert(h1(t) == h2(static_cast<under_type>(i)));
47 }
48}
49
50int main()
51{
52 test<Cardinals>();
53
54 test<Colors>();
55 test<ShortColors>();
56 test<LongColors>();
57 test<EightBitColors>();
58
59 test<Fruits>();
60}
61#else
62int main () {}
63#endif