Home | History | Annotate | Download | only in unord.hash
      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
     29 test()
     30 {
     31     static_assert((std::is_base_of<std::unary_function<T, std::size_t>,
     32                                    std::hash<T> >::value), "");
     33     std::hash<T> h;
     34     std::size_t t0 = h(0.);
     35     std::size_t tn0 = h(-0.);
     36     std::size_t tp1 = h(0.1);
     37     std::size_t t1 = h(1);
     38     std::size_t tn1 = h(-1);
     39     std::size_t pinf = h(INFINITY);
     40     std::size_t ninf = h(-INFINITY);
     41     assert(t0 == tn0);
     42     assert(t0 != tp1);
     43     assert(t0 != t1);
     44     assert(t0 != tn1);
     45     assert(t0 != pinf);
     46     assert(t0 != ninf);
     47 
     48     assert(tp1 != t1);
     49     assert(tp1 != tn1);
     50     assert(tp1 != pinf);
     51     assert(tp1 != ninf);
     52 
     53     assert(t1 != tn1);
     54     assert(t1 != pinf);
     55     assert(t1 != ninf);
     56 
     57     assert(tn1 != pinf);
     58     assert(tn1 != ninf);
     59 
     60     assert(pinf != ninf);
     61 }
     62 
     63 int main()
     64 {
     65     test<float>();
     66     test<double>();
     67     test<long double>();
     68 }
     69