15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
9dc066888SArthur O'Dwyer // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
10dc066888SArthur O'Dwyer 
115a83710eSEric Fiselier // <functional>
125a83710eSEric Fiselier 
135a83710eSEric Fiselier // template <class T>
145a83710eSEric Fiselier // struct hash
155a83710eSEric Fiselier //     : public unary_function<T, size_t>
165a83710eSEric Fiselier // {
175a83710eSEric Fiselier //     size_t operator()(T val) const;
185a83710eSEric Fiselier // };
195a83710eSEric Fiselier 
205a83710eSEric Fiselier #include <functional>
215a83710eSEric Fiselier #include <cassert>
225a83710eSEric Fiselier #include <type_traits>
23df5ddf2cSMarshall Clow #include <cstddef>
245a83710eSEric Fiselier #include <limits>
255a83710eSEric Fiselier 
265f15481fSEric Fiselier #include "test_macros.h"
275f15481fSEric Fiselier 
285a83710eSEric Fiselier template <class T>
295a83710eSEric Fiselier void
test()305a83710eSEric Fiselier test()
315a83710eSEric Fiselier {
32d95510ebSMarshall Clow     typedef std::hash<T> H;
33dc066888SArthur O'Dwyer #if TEST_STD_VER <= 17
34d95510ebSMarshall Clow     static_assert((std::is_same<typename H::argument_type, T>::value), "");
35d95510ebSMarshall Clow     static_assert((std::is_same<typename H::result_type, std::size_t>::value), "");
36dc066888SArthur O'Dwyer #endif
377c803385SMarshall Clow     ASSERT_NOEXCEPT(H()(T()));
38d95510ebSMarshall Clow     H h;
39d95510ebSMarshall Clow 
405a83710eSEric Fiselier     for (int i = 0; i <= 5; ++i)
415a83710eSEric Fiselier     {
42f2e24f56SStephan T. Lavavej         T t(static_cast<T>(i));
433d26ee29SStephan T. Lavavej         const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings
443d26ee29SStephan T. Lavavej         if (small)
455f15481fSEric Fiselier         {
465f15481fSEric Fiselier             const std::size_t result = h(t);
47c71bd55bSEric Fiselier             LIBCPP_ASSERT(result == static_cast<size_t>(t));
485f15481fSEric Fiselier             ((void)result); // Prevent unused warning
495f15481fSEric Fiselier         }
505a83710eSEric Fiselier     }
515a83710eSEric Fiselier }
525a83710eSEric Fiselier 
main(int,char **)532df59c50SJF Bastien int main(int, char**)
545a83710eSEric Fiselier {
555a83710eSEric Fiselier     test<bool>();
565a83710eSEric Fiselier     test<char>();
575a83710eSEric Fiselier     test<signed char>();
585a83710eSEric Fiselier     test<unsigned char>();
595a83710eSEric Fiselier     test<char16_t>();
605a83710eSEric Fiselier     test<char32_t>();
61f4c1258dSLouis Dionne #ifndef TEST_HAS_NO_WIDE_CHARACTERS
625a83710eSEric Fiselier     test<wchar_t>();
63f4c1258dSLouis Dionne #endif
645a83710eSEric Fiselier     test<short>();
655a83710eSEric Fiselier     test<unsigned short>();
665a83710eSEric Fiselier     test<int>();
675a83710eSEric Fiselier     test<unsigned int>();
685a83710eSEric Fiselier     test<long>();
695a83710eSEric Fiselier     test<unsigned long>();
705a83710eSEric Fiselier     test<long long>();
715a83710eSEric Fiselier     test<unsigned long long>();
721867554cSMarshall Clow 
731867554cSMarshall Clow //  LWG #2119
74df5ddf2cSMarshall Clow     test<std::ptrdiff_t>();
751867554cSMarshall Clow     test<size_t>();
761867554cSMarshall Clow 
771867554cSMarshall Clow     test<int8_t>();
781867554cSMarshall Clow     test<int16_t>();
791867554cSMarshall Clow     test<int32_t>();
801867554cSMarshall Clow     test<int64_t>();
811867554cSMarshall Clow 
821867554cSMarshall Clow     test<int_fast8_t>();
831867554cSMarshall Clow     test<int_fast16_t>();
841867554cSMarshall Clow     test<int_fast32_t>();
851867554cSMarshall Clow     test<int_fast64_t>();
861867554cSMarshall Clow 
871867554cSMarshall Clow     test<int_least8_t>();
881867554cSMarshall Clow     test<int_least16_t>();
891867554cSMarshall Clow     test<int_least32_t>();
901867554cSMarshall Clow     test<int_least64_t>();
911867554cSMarshall Clow 
921867554cSMarshall Clow     test<intmax_t>();
931867554cSMarshall Clow     test<intptr_t>();
941867554cSMarshall Clow 
951867554cSMarshall Clow     test<uint8_t>();
961867554cSMarshall Clow     test<uint16_t>();
971867554cSMarshall Clow     test<uint32_t>();
981867554cSMarshall Clow     test<uint64_t>();
991867554cSMarshall Clow 
1001867554cSMarshall Clow     test<uint_fast8_t>();
1011867554cSMarshall Clow     test<uint_fast16_t>();
1021867554cSMarshall Clow     test<uint_fast32_t>();
1031867554cSMarshall Clow     test<uint_fast64_t>();
1041867554cSMarshall Clow 
1051867554cSMarshall Clow     test<uint_least8_t>();
1061867554cSMarshall Clow     test<uint_least16_t>();
1071867554cSMarshall Clow     test<uint_least32_t>();
1081867554cSMarshall Clow     test<uint_least64_t>();
1091867554cSMarshall Clow 
1101867554cSMarshall Clow     test<uintmax_t>();
1111867554cSMarshall Clow     test<uintptr_t>();
112df691e18SEric Fiselier 
113*8f972cb0SMark de Wever #ifndef TEST_HAS_NO_INT128
114df691e18SEric Fiselier     test<__int128_t>();
115df691e18SEric Fiselier     test<__uint128_t>();
116df691e18SEric Fiselier #endif
1172df59c50SJF Bastien 
1182df59c50SJF Bastien   return 0;
1195a83710eSEric Fiselier }
120