• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright 2005-2009 Daniel James.
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #include "./config.hpp"
7 
8 #ifdef BOOST_HASH_TEST_STD_INCLUDES
9 #  include <functional>
10 #else
11 #  include <boost/container_hash/hash.hpp>
12 #endif
13 
14 #include <boost/core/lightweight_test.hpp>
15 #include <string>
16 #include "./compile_time.hpp"
17 
string_tests()18 void string_tests()
19 {
20     compile_time_tests((std::string*) 0);
21 
22     BOOST_HASH_TEST_NAMESPACE::hash<std::string> x1;
23     BOOST_HASH_TEST_NAMESPACE::hash<std::string> x2;
24 
25     BOOST_TEST(x1("Hello") == x2(std::string("Hel") + "lo"));
26     BOOST_TEST(x1("") == x2(std::string()));
27 
28 #if defined(BOOST_HASH_TEST_EXTENSIONS)
29     std::string value1;
30     std::string value2("Hello");
31 
32     BOOST_TEST(x1(value1) == BOOST_HASH_TEST_NAMESPACE::hash_value(value1));
33     BOOST_TEST(x1(value2) == BOOST_HASH_TEST_NAMESPACE::hash_value(value2));
34     BOOST_TEST(BOOST_HASH_TEST_NAMESPACE::hash_value(value1) ==
35             BOOST_HASH_TEST_NAMESPACE::hash_range(value1.begin(), value1.end()));
36     BOOST_TEST(BOOST_HASH_TEST_NAMESPACE::hash_value(value2) ==
37             BOOST_HASH_TEST_NAMESPACE::hash_range(value2.begin(), value2.end()));
38 #endif
39 }
40 
string0_tests()41 void string0_tests()
42 {
43     std::string x1(1, '\0');
44     std::string x2(2, '\0');
45     std::string x3(3, '\0');
46     std::string x4(10, '\0');
47 
48     BOOST_HASH_TEST_NAMESPACE::hash<std::string> hasher;
49 
50     BOOST_TEST(hasher(x1) != hasher(x2));
51     BOOST_TEST(hasher(x1) != hasher(x3));
52     BOOST_TEST(hasher(x1) != hasher(x4));
53     BOOST_TEST(hasher(x2) != hasher(x3));
54     BOOST_TEST(hasher(x2) != hasher(x4));
55     BOOST_TEST(hasher(x3) != hasher(x4));
56 }
57 
58 #if !defined(BOOST_NO_STD_WSTRING) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
wstring_tests()59 void wstring_tests()
60 {
61     compile_time_tests((std::wstring*) 0);
62 
63     BOOST_HASH_TEST_NAMESPACE::hash<std::wstring> x1;
64     BOOST_HASH_TEST_NAMESPACE::hash<std::wstring> x2;
65 
66     BOOST_TEST(x1(L"Hello") == x2(std::wstring(L"Hel") + L"lo"));
67     BOOST_TEST(x1(L"") == x2(std::wstring()));
68 
69 #if defined(BOOST_HASH_TEST_EXTENSIONS)
70     std::wstring value1;
71     std::wstring value2(L"Hello");
72 
73     BOOST_TEST(x1(value1) == BOOST_HASH_TEST_NAMESPACE::hash_value(value1));
74     BOOST_TEST(x1(value2) == BOOST_HASH_TEST_NAMESPACE::hash_value(value2));
75     BOOST_TEST(BOOST_HASH_TEST_NAMESPACE::hash_value(value1) ==
76             BOOST_HASH_TEST_NAMESPACE::hash_range(value1.begin(), value1.end()));
77     BOOST_TEST(BOOST_HASH_TEST_NAMESPACE::hash_value(value2) ==
78             BOOST_HASH_TEST_NAMESPACE::hash_range(value2.begin(), value2.end()));
79 #endif
80 }
81 #endif
82 
83 #if !defined(BOOST_NO_CXX11_CHAR16_T)
u16string_tests()84 void u16string_tests()
85 {
86     compile_time_tests((std::u16string*) 0);
87 
88     BOOST_HASH_TEST_NAMESPACE::hash<std::u16string> x1;
89     BOOST_HASH_TEST_NAMESPACE::hash<std::u16string> x2;
90 
91     BOOST_TEST(x1(u"Hello") == x2(std::u16string(u"Hel") + u"lo"));
92     BOOST_TEST(x1(u"") == x2(std::u16string()));
93 
94 #if defined(BOOST_HASH_TEST_EXTENSIONS)
95     std::u16string value1;
96     std::u16string value2(u"Hello");
97 
98     BOOST_TEST(x1(value1) == BOOST_HASH_TEST_NAMESPACE::hash_value(value1));
99     BOOST_TEST(x1(value2) == BOOST_HASH_TEST_NAMESPACE::hash_value(value2));
100     BOOST_TEST(BOOST_HASH_TEST_NAMESPACE::hash_value(value1) ==
101             BOOST_HASH_TEST_NAMESPACE::hash_range(value1.begin(), value1.end()));
102     BOOST_TEST(BOOST_HASH_TEST_NAMESPACE::hash_value(value2) ==
103             BOOST_HASH_TEST_NAMESPACE::hash_range(value2.begin(), value2.end()));
104 #endif
105 }
106 #endif
107 
108 #if !defined(BOOST_NO_CXX11_CHAR32_T)
u32string_tests()109 void u32string_tests()
110 {
111     compile_time_tests((std::u32string*) 0);
112 
113     BOOST_HASH_TEST_NAMESPACE::hash<std::u32string> x1;
114     BOOST_HASH_TEST_NAMESPACE::hash<std::u32string> x2;
115 
116     BOOST_TEST(x1(U"Hello") == x2(std::u32string(U"Hel") + U"lo"));
117     BOOST_TEST(x1(U"") == x2(std::u32string()));
118 
119 #if defined(BOOST_HASH_TEST_EXTENSIONS)
120     std::u32string value1;
121     std::u32string value2(U"Hello");
122 
123     BOOST_TEST(x1(value1) == BOOST_HASH_TEST_NAMESPACE::hash_value(value1));
124     BOOST_TEST(x1(value2) == BOOST_HASH_TEST_NAMESPACE::hash_value(value2));
125     BOOST_TEST(BOOST_HASH_TEST_NAMESPACE::hash_value(value1) ==
126             BOOST_HASH_TEST_NAMESPACE::hash_range(value1.begin(), value1.end()));
127     BOOST_TEST(BOOST_HASH_TEST_NAMESPACE::hash_value(value2) ==
128             BOOST_HASH_TEST_NAMESPACE::hash_range(value2.begin(), value2.end()));
129 #endif
130 }
131 #endif
132 
133 template <typename StringType>
generic_string_tests(StringType *)134 void generic_string_tests(StringType*)
135 {
136     std::string x1(1, '\0');
137     std::string x2(2, '\0');
138     std::string x3(3, '\0');
139     std::string x4(10, '\0');
140     std::string x5 = x2 + "hello" + x2;
141 
142     StringType strings[] = {
143         "",
144         "hello",
145         x1,
146         x2,
147         x3,
148         x4,
149         x5
150     };
151 
152     std::size_t const strings_length = sizeof(strings) / sizeof(StringType);
153     boost::hash<StringType> hash;
154 
155     for (std::size_t i = 0; i < strings_length; ++i) {
156         std::size_t hash_i = hash(strings[i]);
157         for (std::size_t j = 0; j < strings_length; ++j) {
158             std::size_t hash_j = hash(strings[j]);
159             BOOST_TEST((hash_i == hash_j) == (i == j));
160         }
161     }
162 }
163 
main()164 int main()
165 {
166     string_tests();
167     string0_tests();
168 #if !defined(BOOST_NO_STD_WSTRING) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
169     wstring_tests();
170 #endif
171 #if !defined(BOOST_NO_CXX11_CHAR16_T)
172     u16string_tests();
173 #endif
174 #if !defined(BOOST_NO_CXX11_CHAR32_T)
175     u32string_tests();
176 #endif
177 
178     generic_string_tests((std::string*) 0);
179 #if BOOST_HASH_HAS_STRING_VIEW
180     generic_string_tests((std::string_view*) 0);
181 #endif
182 
183     return boost::report_errors();
184 }
185