1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <functional>
11 
12 // template <class T>
13 // struct hash
14 //     : public unary_function<T, size_t>
15 // {
16 //     size_t operator()(T val) const;
17 // };
18 
19 // Not very portable
20 
21 #include <functional>
22 #include <cassert>
23 #include <type_traits>
24 #include <limits>
25 #include <cmath>
26 
27 template <class T>
28 void
test()29 test()
30 {
31     typedef std::hash<T> H;
32     static_assert((std::is_same<typename H::argument_type, T>::value), "" );
33     static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
34     H h;
35 
36     std::size_t t0 = h(0.);
37     std::size_t tn0 = h(-0.);
38     std::size_t tp1 = h(0.1);
39     std::size_t t1 = h(1);
40     std::size_t tn1 = h(-1);
41     std::size_t pinf = h(INFINITY);
42     std::size_t ninf = h(-INFINITY);
43     assert(t0 == tn0);
44     assert(t0 != tp1);
45     assert(t0 != t1);
46     assert(t0 != tn1);
47     assert(t0 != pinf);
48     assert(t0 != ninf);
49 
50     assert(tp1 != t1);
51     assert(tp1 != tn1);
52     assert(tp1 != pinf);
53     assert(tp1 != ninf);
54 
55     assert(t1 != tn1);
56     assert(t1 != pinf);
57     assert(t1 != ninf);
58 
59     assert(tn1 != pinf);
60     assert(tn1 != ninf);
61 
62     assert(pinf != ninf);
63 }
64 
main()65 int main()
66 {
67     test<float>();
68     test<double>();
69     test<long double>();
70 }
71