• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "test_macros.h"
28 
29 template <class T>
30 void
test()31 test()
32 {
33     typedef std::hash<T> H;
34     static_assert((std::is_same<typename H::argument_type, T>::value), "" );
35     static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
36     ASSERT_NOEXCEPT(H()(T()));
37     H h;
38 
39     std::size_t t0 = h(0.);
40     std::size_t tn0 = h(-0.);
41     std::size_t tp1 = h(static_cast<T>(0.1));
42     std::size_t t1 = h(1);
43     std::size_t tn1 = h(-1);
44     std::size_t pinf = h(INFINITY);
45     std::size_t ninf = h(-INFINITY);
46     assert(t0 == tn0);
47     assert(t0 != tp1);
48     assert(t0 != t1);
49     assert(t0 != tn1);
50     assert(t0 != pinf);
51     assert(t0 != ninf);
52 
53     assert(tp1 != t1);
54     assert(tp1 != tn1);
55     assert(tp1 != pinf);
56     assert(tp1 != ninf);
57 
58     assert(t1 != tn1);
59     assert(t1 != pinf);
60     assert(t1 != ninf);
61 
62     assert(tn1 != pinf);
63     assert(tn1 != ninf);
64 
65     assert(pinf != ninf);
66 }
67 
main()68 int main()
69 {
70     test<float>();
71     test<double>();
72     test<long double>();
73 }
74