131 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS10 11// <functional>12 13// template <class T>14// struct hash15// : public unary_function<T, size_t>16// {17// size_t operator()(T val) const;18// };19 20#include <functional>21#include <cassert>22#include <cstddef>23#include <cstdint>24#include <limits>25#include <type_traits>26 27#include "test_macros.h"28 29#if TEST_STD_VER >= 1130# include "poisoned_hash_helper.h"31#endif32 33template <class T>34void35test()36{37#if TEST_STD_VER >= 1138 test_hash_disabled<const T>();39 test_hash_disabled<volatile T>();40 test_hash_disabled<const volatile T>();41#endif42 43 typedef std::hash<T> H;44#if TEST_STD_VER <= 1745 static_assert((std::is_same<typename H::argument_type, T>::value), "");46 static_assert((std::is_same<typename H::result_type, std::size_t>::value), "");47#endif48 ASSERT_NOEXCEPT(H()(T()));49 H h;50 51 for (int i = 0; i <= 5; ++i)52 {53 T t(static_cast<T>(i));54 const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings55 if (small)56 {57 const std::size_t result = h(t);58 LIBCPP_ASSERT(result == static_cast<std::size_t>(t));59 ((void)result); // Prevent unused warning60 }61 }62}63 64int main(int, char**)65{66 test<bool>();67 test<char>();68 test<signed char>();69 test<unsigned char>();70 test<char16_t>();71 test<char32_t>();72#ifndef TEST_HAS_NO_WIDE_CHARACTERS73 test<wchar_t>();74#endif75 test<short>();76 test<unsigned short>();77 test<int>();78 test<unsigned int>();79 test<long>();80 test<unsigned long>();81 test<long long>();82 test<unsigned long long>();83 84// LWG #211985 test<std::ptrdiff_t>();86 test<std::size_t>();87 88 test<std::int8_t>();89 test<std::int16_t>();90 test<std::int32_t>();91 test<std::int64_t>();92 93 test<std::int_fast8_t>();94 test<std::int_fast16_t>();95 test<std::int_fast32_t>();96 test<std::int_fast64_t>();97 98 test<std::int_least8_t>();99 test<std::int_least16_t>();100 test<std::int_least32_t>();101 test<std::int_least64_t>();102 103 test<std::intmax_t>();104 test<std::intptr_t>();105 106 test<std::uint8_t>();107 test<std::uint16_t>();108 test<std::uint32_t>();109 test<std::uint64_t>();110 111 test<std::uint_fast8_t>();112 test<std::uint_fast16_t>();113 test<std::uint_fast32_t>();114 test<std::uint_fast64_t>();115 116 test<std::uint_least8_t>();117 test<std::uint_least16_t>();118 test<std::uint_least32_t>();119 test<std::uint_least64_t>();120 121 test<std::uintmax_t>();122 test<std::uintptr_t>();123 124#ifndef TEST_HAS_NO_INT128125 test<__int128_t>();126 test<__uint128_t>();127#endif128 129 return 0;130}131