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