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