1 
2 // Copyright 2006-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 // This test creates the containers with members that meet their minimum
7 // requirements. Makes sure everything compiles and is defined correctly.
8 
9 // clang-format off
10 #include "../helpers/prefix.hpp"
11 #include <boost/unordered_map.hpp>
12 #include "../helpers/postfix.hpp"
13 // clang-format on
14 
15 #include "../helpers/test.hpp"
16 #include "../objects/minimal.hpp"
17 #include "./compile_tests.hpp"
18 
19 // Explicit instantiation to catch compile-time errors
20 
21 #define INSTANTIATE(type)                                                      \
22   template class boost::unordered::detail::instantiate_##type
23 
24 INSTANTIATE(map)<int, int, boost::hash<int>, std::equal_to<int>,
25   test::minimal::allocator<int> >;
26 INSTANTIATE(multimap)<int const, int const, boost::hash<int>,
27   std::equal_to<int>, test::minimal::allocator<int> >;
28 
29 INSTANTIATE(
30   map)<test::minimal::assignable const, test::minimal::default_assignable const,
31   test::minimal::hash<test::minimal::assignable>,
32   test::minimal::equal_to<test::minimal::assignable>,
33   test::minimal::allocator<int> >;
34 INSTANTIATE(multimap)<test::minimal::assignable, test::minimal::assignable,
35   test::minimal::hash<test::minimal::assignable>,
36   test::minimal::equal_to<test::minimal::assignable>,
37   test::minimal::allocator<int> >;
38 
UNORDERED_AUTO_TEST(test0)39 UNORDERED_AUTO_TEST (test0) {
40   test::minimal::constructor_param x;
41 
42   typedef std::pair<test::minimal::assignable const, test::minimal::assignable>
43     value_type;
44   value_type value(x, x);
45 
46   BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Test unordered_map.\n";
47 
48   boost::unordered_map<int, int> int_map;
49 
50   boost::unordered_map<int, int, boost::hash<int>, std::equal_to<int>,
51     test::minimal::cxx11_allocator<std::pair<int const, int> > >
52     int_map2;
53 
54   boost::unordered_map<test::minimal::assignable, test::minimal::assignable,
55     test::minimal::hash<test::minimal::assignable>,
56     test::minimal::equal_to<test::minimal::assignable>,
57     test::minimal::allocator<value_type> >
58     map;
59 
60   container_test(int_map, std::pair<int const, int>(0, 0));
61   container_test(int_map2, std::pair<int const, int>(0, 0));
62   container_test(map, value);
63 
64   BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Test unordered_multimap.\n";
65 
66   boost::unordered_multimap<int, int> int_multimap;
67 
68   boost::unordered_multimap<int, int, boost::hash<int>, std::equal_to<int>,
69     test::minimal::cxx11_allocator<std::pair<int const, int> > >
70     int_multimap2;
71 
72   boost::unordered_multimap<test::minimal::assignable,
73     test::minimal::assignable, test::minimal::hash<test::minimal::assignable>,
74     test::minimal::equal_to<test::minimal::assignable>,
75     test::minimal::allocator<value_type> >
76     multimap;
77 
78   container_test(int_multimap, std::pair<int const, int>(0, 0));
79   container_test(int_multimap2, std::pair<int const, int>(0, 0));
80   container_test(multimap, value);
81 }
82 
UNORDERED_AUTO_TEST(equality_tests)83 UNORDERED_AUTO_TEST (equality_tests) {
84   typedef std::pair<test::minimal::copy_constructible_equality_comparable const,
85     test::minimal::copy_constructible_equality_comparable>
86     value_type;
87 
88   boost::unordered_map<int, int> int_map;
89 
90   boost::unordered_map<int, int, boost::hash<int>, std::equal_to<int>,
91     test::minimal::cxx11_allocator<std::pair<int const, int> > >
92     int_map2;
93 
94   boost::unordered_map<test::minimal::copy_constructible_equality_comparable,
95     test::minimal::copy_constructible_equality_comparable,
96     test::minimal::hash<test::minimal::copy_constructible_equality_comparable>,
97     test::minimal::equal_to<
98       test::minimal::copy_constructible_equality_comparable>,
99     test::minimal::allocator<value_type> >
100     map;
101 
102   equality_test(int_map);
103   equality_test(int_map2);
104   equality_test(map);
105 
106   boost::unordered_multimap<int, int> int_multimap;
107 
108   boost::unordered_multimap<int, int, boost::hash<int>, std::equal_to<int>,
109     test::minimal::cxx11_allocator<std::pair<int const, int> > >
110     int_multimap2;
111 
112   boost::unordered_multimap<
113     test::minimal::copy_constructible_equality_comparable,
114     test::minimal::copy_constructible_equality_comparable,
115     test::minimal::hash<test::minimal::copy_constructible_equality_comparable>,
116     test::minimal::equal_to<
117       test::minimal::copy_constructible_equality_comparable>,
118     test::minimal::allocator<value_type> >
119     multimap;
120 
121   equality_test(int_multimap);
122   equality_test(int_multimap2);
123   equality_test(multimap);
124 }
125 
UNORDERED_AUTO_TEST(test1)126 UNORDERED_AUTO_TEST (test1) {
127   boost::hash<int> hash;
128   std::equal_to<int> equal_to;
129   int value = 0;
130   std::pair<int const, int> map_value(0, 0);
131 
132   BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Test unordered_map.\n";
133 
134   boost::unordered_map<int, int> map;
135 
136   boost::unordered_map<int, int, boost::hash<int>, std::equal_to<int>,
137     test::minimal::cxx11_allocator<std::pair<int const, int> > >
138     map2;
139 
140   unordered_unique_test(map, map_value);
141   unordered_map_test(map, value, value);
142   unordered_copyable_test(map, value, map_value, hash, equal_to);
143   unordered_map_functions(map, value, value);
144 
145   unordered_unique_test(map2, map_value);
146   unordered_map_test(map2, value, value);
147   unordered_copyable_test(map2, value, map_value, hash, equal_to);
148   unordered_map_functions(map2, value, value);
149 
150   BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Test unordered_multimap.\n";
151 
152   boost::unordered_multimap<int, int> multimap;
153 
154   boost::unordered_multimap<int, int, boost::hash<int>, std::equal_to<int>,
155     test::minimal::cxx11_allocator<std::pair<int const, int> > >
156     multimap2;
157 
158   unordered_equivalent_test(multimap, map_value);
159   unordered_map_test(multimap, value, value);
160   unordered_copyable_test(multimap, value, map_value, hash, equal_to);
161 
162   unordered_equivalent_test(multimap2, map_value);
163   unordered_map_test(multimap2, value, value);
164   unordered_copyable_test(multimap2, value, map_value, hash, equal_to);
165 }
166 
UNORDERED_AUTO_TEST(test2)167 UNORDERED_AUTO_TEST (test2) {
168   test::minimal::constructor_param x;
169 
170   test::minimal::assignable assignable(x);
171   test::minimal::copy_constructible copy_constructible(x);
172   test::minimal::hash<test::minimal::assignable> hash(x);
173   test::minimal::equal_to<test::minimal::assignable> equal_to(x);
174 
175   typedef std::pair<test::minimal::assignable const, test::minimal::assignable>
176     map_value_type;
177   map_value_type map_value(assignable, assignable);
178 
179   BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Test unordered_map.\n";
180 
181   boost::unordered_map<test::minimal::assignable, test::minimal::assignable,
182     test::minimal::hash<test::minimal::assignable>,
183     test::minimal::equal_to<test::minimal::assignable>,
184     test::minimal::allocator<map_value_type> >
185     map;
186 
187   unordered_unique_test(map, map_value);
188   unordered_map_test(map, assignable, assignable);
189   unordered_copyable_test(map, assignable, map_value, hash, equal_to);
190   unordered_map_member_test(map, map_value);
191 
192   boost::unordered_map<test::minimal::assignable,
193     test::minimal::default_assignable,
194     test::minimal::hash<test::minimal::assignable>,
195     test::minimal::equal_to<test::minimal::assignable>,
196     test::minimal::allocator<map_value_type> >
197     map2;
198 
199   test::minimal::default_assignable default_assignable;
200 
201   unordered_map_functions(map2, assignable, default_assignable);
202 
203   BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Test unordered_multimap.\n";
204 
205   boost::unordered_multimap<test::minimal::assignable,
206     test::minimal::assignable, test::minimal::hash<test::minimal::assignable>,
207     test::minimal::equal_to<test::minimal::assignable>,
208     test::minimal::allocator<map_value_type> >
209     multimap;
210 
211   unordered_equivalent_test(multimap, map_value);
212   unordered_map_test(multimap, assignable, assignable);
213   unordered_copyable_test(multimap, assignable, map_value, hash, equal_to);
214   unordered_map_member_test(multimap, map_value);
215 }
216 
217 // Test for ambiguity when using key convertible from iterator
218 // See LWG2059
219 
220 struct lwg2059_key
221 {
222   int value;
223 
lwg2059_keylwg2059_key224   template <typename T> lwg2059_key(T v) : value(v) {}
225 };
226 
hash_value(lwg2059_key x)227 std::size_t hash_value(lwg2059_key x)
228 {
229   return static_cast<std::size_t>(x.value);
230 }
231 
operator ==(lwg2059_key x,lwg2059_key y)232 bool operator==(lwg2059_key x, lwg2059_key y) { return x.value == y.value; }
233 
UNORDERED_AUTO_TEST(lwg2059)234 UNORDERED_AUTO_TEST (lwg2059) {
235   {
236     boost::unordered_map<lwg2059_key, int> x;
237     x.emplace(lwg2059_key(10), 5);
238     x.erase(x.begin());
239   }
240 
241   {
242     boost::unordered_multimap<lwg2059_key, int> x;
243     x.emplace(lwg2059_key(10), 5);
244     x.erase(x.begin());
245   }
246 }
247 
248 RUN_TESTS()
249