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 // <functional> 12 13 // Make sure that we can hash enumeration values. 14 15 #include <functional> 16 #include <cassert> 17 #include <cstddef> 18 #include <cstdint> 19 #include <limits> 20 #include <type_traits> 21 22 #include "test_macros.h" 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 : std::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 34 test() 35 { 36 typedef std::hash<T> H; 37 #if TEST_STD_VER <= 17 38 static_assert((std::is_same<typename H::argument_type, T>::value), ""); 39 static_assert((std::is_same<typename H::result_type, std::size_t>::value), ""); 40 #endif 41 ASSERT_NOEXCEPT(H()(T())); 42 typedef typename std::underlying_type<T>::type under_type; 43 44 H h1; 45 std::hash<under_type> h2; 46 for (int i = 0; i <= 5; ++i) 47 { 48 T t(static_cast<T> (i)); 49 const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings 50 if (small) 51 assert(h1(t) == h2(static_cast<under_type>(i))); 52 } 53 } 54 55 int main(int, char**) 56 { 57 test<Cardinals>(); 58 59 test<Colors>(); 60 test<ShortColors>(); 61 test<LongColors>(); 62 test<EightBitColors>(); 63 64 test<Fruits>(); 65 66 return 0; 67 } 68