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 // <functional>
10 
11 // template <class T>
12 // struct hash
13 //     : public unary_function<T, size_t>
14 // {
15 //     size_t operator()(T val) const;
16 // };
17 
18 // Not very portable
19 
20 #include <functional>
21 #include <cassert>
22 #include <type_traits>
23 #include <limits>
24 #include <cmath>
25 
26 #include "test_macros.h"
27 
28 template <class T>
29 void
30 test()
31 {
32     typedef std::hash<T> H;
33     static_assert((std::is_same<typename H::argument_type, T>::value), "" );
34     static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
35     ASSERT_NOEXCEPT(H()(T()));
36     H h;
37 
38     std::size_t t0 = h(0.);
39     std::size_t tn0 = h(-0.);
40     std::size_t tp1 = h(static_cast<T>(0.1));
41     std::size_t t1 = h(1);
42     std::size_t tn1 = h(-1);
43     std::size_t pinf = h(INFINITY);
44     std::size_t ninf = h(-INFINITY);
45     assert(t0 == tn0);
46     assert(t0 != tp1);
47     assert(t0 != t1);
48     assert(t0 != tn1);
49     assert(t0 != pinf);
50     assert(t0 != ninf);
51 
52     assert(tp1 != t1);
53     assert(tp1 != tn1);
54     assert(tp1 != pinf);
55     assert(tp1 != ninf);
56 
57     assert(t1 != tn1);
58     assert(t1 != pinf);
59     assert(t1 != ninf);
60 
61     assert(tn1 != pinf);
62     assert(tn1 != ninf);
63 
64     assert(pinf != ninf);
65 }
66 
67 int main(int, char**)
68 {
69     test<float>();
70     test<double>();
71     test<long double>();
72 
73   return 0;
74 }
75