| 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 "test_macros.h" |
| 16 |
|
| 17 |
#include <functional> |
| 18 |
#include <cassert> |
| 19 |
#include <type_traits> |
| 20 |
#include <limits> |
| 21 |
|
| 22 |
enum class Colors { red, orange, yellow, green, blue, indigo, violet }; |
| 23 |
enum class Cardinals { zero, one, two, three, five=5 }; |
| 24 |
enum class LongColors : short { red, orange, yellow, green, blue, indigo, violet }; |
| 25 |
enum class ShortColors : long { red, orange, yellow, green, blue, indigo, violet }; |
| 26 |
enum class EightBitColors : std::uint8_t { red, orange, yellow, green, blue, indigo, violet }; |
| 27 |
|
| 28 |
enum Fruits { apple, pear, grape, mango, cantaloupe }; |
| 29 |
|
| 30 |
template <class T> |
| 31 |
void |
| 32 |
test() |
| 33 |
{ |
| 34 |
typedef std::hash<T> H; |
| 35 |
#if TEST_STD_VER <= 17 |
| 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 |
#endif |
| 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 |
|
| 53 |
int main(int, char**) |
| 54 |
{ |
| 55 |
test<Cardinals>(); |
| 56 |
|
| 57 |
test<Colors>(); |
| 58 |
test<ShortColors>(); |
| 59 |
test<LongColors>(); |
| 60 |
test<EightBitColors>(); |
| 61 |
|
| 62 |
test<Fruits>(); |
| 63 |
|
| 64 |
return 0; |
| 65 |
} |