89 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS10 11// <functional>12 13// template <class T>14// struct hash15// : public unary_function<T, size_t>16// {17// size_t operator()(T val) const;18// };19 20// Not very portable21 22#include <functional>23#include <cassert>24#include <type_traits>25#include <limits>26#include <cmath>27 28#include "test_macros.h"29 30#if TEST_STD_VER >= 1131# include "poisoned_hash_helper.h"32#endif33 34template <class T>35void36test()37{38#if TEST_STD_VER >= 1139 test_hash_disabled<const T>();40 test_hash_disabled<volatile T>();41 test_hash_disabled<const volatile T>();42#endif43 44 typedef std::hash<T> H;45#if TEST_STD_VER <= 1746 static_assert((std::is_same<typename H::argument_type, T>::value), "");47 static_assert((std::is_same<typename H::result_type, std::size_t>::value), "");48#endif49 ASSERT_NOEXCEPT(H()(T()));50 H h;51 52 std::size_t t0 = h(0.);53 std::size_t tn0 = h(-0.);54 std::size_t tp1 = h(static_cast<T>(0.1));55 std::size_t t1 = h(1);56 std::size_t tn1 = h(-1);57 std::size_t pinf = h(INFINITY);58 std::size_t ninf = h(-INFINITY);59 assert(t0 == tn0);60 assert(t0 != tp1);61 assert(t0 != t1);62 assert(t0 != tn1);63 assert(t0 != pinf);64 assert(t0 != ninf);65 66 assert(tp1 != t1);67 assert(tp1 != tn1);68 assert(tp1 != pinf);69 assert(tp1 != ninf);70 71 assert(t1 != tn1);72 assert(t1 != pinf);73 assert(t1 != ninf);74 75 assert(tn1 != pinf);76 assert(tn1 != ninf);77 78 assert(pinf != ninf);79}80 81int main(int, char**)82{83 test<float>();84 test<double>();85 test<long double>();86 87 return 0;88}89