1// Copyright 2005-2009 Daniel James.
2// Copyright 2021 Peter Dimov.
3// Distributed under the Boost Software License, Version 1.0.
4// https://www.boost.org/LICENSE_1_0.txt
5
6#include <boost/container_hash/hash.hpp>
7#include <boost/core/lightweight_test.hpp>
8#include <boost/core/type_name.hpp>
9#include <set>
10
11#if defined(BOOST_MSVC)
12# pragma warning(disable: 4244) // conversion from int to float
13#endif
14
15#if defined(__GNUC__) || defined(__clang__)
16# pragma GCC diagnostic ignored "-Wconversion"
17# pragma GCC diagnostic ignored "-Wsign-conversion"
18#endif
19
20// This test checks for collisions in a small range of numbers
21
22template<class T, int M>
23void collision_test_()
24{
25 std::set<std::size_t> hashes;
26
27 for( int i = -128; i <= 127; ++i )
28 {
29 hashes.insert( boost::hash<T>()( i * M ) );
30 }
31
32 BOOST_TEST_EQ( hashes.size(), 256u );
33}
34
35template <class T>
36void collision_test()
37{
38 collision_test_<T, 1>();
39 collision_test_<T, 2>();
40 collision_test_<T, 3>();
41 collision_test_<T, 4>();
42 collision_test_<T, 5>();
43 collision_test_<T, 8>();
44 collision_test_<T, 10>();
45 collision_test_<T, 16>();
46 collision_test_<T, 32>();
47 collision_test_<T, 64>();
48 collision_test_<T, 100>();
49 collision_test_<T, 128>();
50}
51
52#define TEST(type) std::cerr << "Testing: " #type " (" << boost::core::type_name<type>() << ")\n"; collision_test<type>();
53
54int main()
55{
56#ifndef BOOST_NO_INTRINSIC_WCHAR_T
57 TEST(wchar_t)
58#endif
59#ifndef BOOST_NO_CXX11_CHAR16_T
60 TEST(char16_t)
61#endif
62#ifndef BOOST_NO_CXX11_CHAR32_T
63 TEST(char32_t)
64#endif
65 TEST(short)
66 TEST(unsigned short)
67 TEST(int)
68 TEST(unsigned int)
69 TEST(long)
70 TEST(unsigned long)
71
72#if !defined(BOOST_NO_LONG_LONG)
73 TEST(boost::long_long_type)
74 TEST(boost::ulong_long_type)
75#endif
76
77#if defined(BOOST_HAS_INT128)
78 TEST(boost::int128_type)
79 TEST(boost::uint128_type)
80#endif
81
82 TEST(float)
83 TEST(double)
84 TEST(long double)
85
86 TEST(std::size_t)
87 TEST(std::ptrdiff_t)
88
89 return boost::report_errors();
90}
91

source code of boost/libs/container_hash/test/hash_number_test2.cpp