PageRenderTime 113ms CodeModel.GetById 14ms app.highlight 92ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/unordered/test/unordered/constructor_tests.cpp

http://hadesmem.googlecode.com/
C++ | 418 lines | 363 code | 52 blank | 3 comment | 3 complexity | efd73692dcefa9ab3c0df2af9a47eed5 MD5 | raw file
  1
  2// Copyright 2006-2010 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 "../helpers/prefix.hpp"
  7
  8#include <boost/unordered_set.hpp>
  9#include <boost/unordered_map.hpp>
 10#include "../helpers/test.hpp"
 11#include "../objects/test.hpp"
 12#include "../helpers/random_values.hpp"
 13#include "../helpers/tracker.hpp"
 14#include "../helpers/equivalent.hpp"
 15#include "../helpers/input_iterator.hpp"
 16#include "../helpers/invariants.hpp"
 17
 18#include <iostream>
 19
 20namespace constructor_tests {
 21
 22test::seed_t seed(356730);
 23
 24template <class T>
 25void constructor_tests1(T*,
 26    test::random_generator generator = test::default_generator)
 27{
 28    BOOST_DEDUCED_TYPENAME T::hasher hf;
 29    BOOST_DEDUCED_TYPENAME T::key_equal eq;
 30    BOOST_DEDUCED_TYPENAME T::allocator_type al;
 31
 32    std::cerr<<"Construct 1\n";
 33    {
 34        T x(0, hf, eq);
 35        BOOST_TEST(x.empty());
 36        BOOST_TEST(test::equivalent(x.hash_function(), hf));
 37        BOOST_TEST(test::equivalent(x.key_eq(), eq));
 38        BOOST_TEST(test::equivalent(x.get_allocator(), al));
 39        test::check_equivalent_keys(x);
 40    }
 41
 42    std::cerr<<"Construct 2\n";
 43    {
 44        T x(100, hf);
 45        BOOST_TEST(x.empty());
 46        BOOST_TEST(x.bucket_count() >= 100);
 47        BOOST_TEST(test::equivalent(x.hash_function(), hf));
 48        BOOST_TEST(test::equivalent(x.key_eq(), eq));
 49        BOOST_TEST(test::equivalent(x.get_allocator(), al));
 50        test::check_equivalent_keys(x);
 51    }
 52
 53    std::cerr<<"Construct 3\n";
 54    {
 55        T x(2000);
 56        BOOST_TEST(x.empty());
 57        BOOST_TEST(x.bucket_count() >= 2000);
 58        BOOST_TEST(test::equivalent(x.hash_function(), hf));
 59        BOOST_TEST(test::equivalent(x.key_eq(), eq));
 60        BOOST_TEST(test::equivalent(x.get_allocator(), al));
 61        test::check_equivalent_keys(x);
 62    }
 63
 64    std::cerr<<"Construct 4\n";
 65    {
 66        T x;
 67        BOOST_TEST(x.empty());
 68        BOOST_TEST(test::equivalent(x.hash_function(), hf));
 69        BOOST_TEST(test::equivalent(x.key_eq(), eq));
 70        BOOST_TEST(test::equivalent(x.get_allocator(), al));
 71        test::check_equivalent_keys(x);
 72    }
 73
 74    std::cerr<<"Construct 5\n";
 75    {
 76        test::random_values<T> v(1000, generator);
 77        T x(v.begin(), v.end(), 10000, hf, eq);
 78        BOOST_TEST(x.bucket_count() >= 10000);
 79        BOOST_TEST(test::equivalent(x.hash_function(), hf));
 80        BOOST_TEST(test::equivalent(x.key_eq(), eq));
 81        BOOST_TEST(test::equivalent(x.get_allocator(), al));
 82        test::check_container(x, v);
 83        test::check_equivalent_keys(x);
 84    }
 85
 86    std::cerr<<"Construct 6\n";
 87    {
 88        test::random_values<T> v(10, generator);
 89        T x(v.begin(), v.end(), 10000, hf);
 90        BOOST_TEST(x.bucket_count() >= 10000);
 91        BOOST_TEST(test::equivalent(x.hash_function(), hf));
 92        BOOST_TEST(test::equivalent(x.key_eq(), eq));
 93        BOOST_TEST(test::equivalent(x.get_allocator(), al));
 94        test::check_container(x, v);
 95        test::check_equivalent_keys(x);
 96    }
 97
 98    std::cerr<<"Construct 7\n";
 99    {
100        test::random_values<T> v(100, generator);
101        T x(v.begin(), v.end(), 100);
102        BOOST_TEST(x.bucket_count() >= 100);
103        BOOST_TEST(test::equivalent(x.hash_function(), hf));
104        BOOST_TEST(test::equivalent(x.key_eq(), eq));
105        BOOST_TEST(test::equivalent(x.get_allocator(), al));
106        test::check_container(x, v);
107        test::check_equivalent_keys(x);
108    }
109
110    std::cerr<<"Construct 8\n";
111    {
112        test::random_values<T> v(1, generator);
113        T x(v.begin(), v.end());
114        BOOST_TEST(test::equivalent(x.hash_function(), hf));
115        BOOST_TEST(test::equivalent(x.key_eq(), eq));
116        BOOST_TEST(test::equivalent(x.get_allocator(), al));
117        test::check_container(x, v);
118        test::check_equivalent_keys(x);
119    }
120
121    std::cerr<<"Construct 9\n";
122    {
123        T x(0, hf, eq, al);
124        BOOST_TEST(x.empty());
125        BOOST_TEST(test::equivalent(x.hash_function(), hf));
126        BOOST_TEST(test::equivalent(x.key_eq(), eq));
127        BOOST_TEST(test::equivalent(x.get_allocator(), al));
128        test::check_equivalent_keys(x);
129    }
130
131    std::cerr<<"Construct 10\n";
132    {
133        test::random_values<T> v(1000, generator);
134        T x(v.begin(), v.end(), 10000, hf, eq, al);
135        BOOST_TEST(x.bucket_count() >= 10000);
136        BOOST_TEST(test::equivalent(x.hash_function(), hf));
137        BOOST_TEST(test::equivalent(x.key_eq(), eq));
138        BOOST_TEST(test::equivalent(x.get_allocator(), al));
139        test::check_container(x, v);
140        test::check_equivalent_keys(x);
141    }
142
143    std::cerr<<"Construct 11\n";
144    {
145        T x(al);
146        BOOST_TEST(x.empty());
147        BOOST_TEST(test::equivalent(x.hash_function(), hf));
148        BOOST_TEST(test::equivalent(x.key_eq(), eq));
149        BOOST_TEST(test::equivalent(x.get_allocator(), al));
150        test::check_equivalent_keys(x);
151    }
152}
153
154template <class T>
155void constructor_tests2(T*,
156    test::random_generator const& generator = test::default_generator)
157{
158    BOOST_DEDUCED_TYPENAME T::hasher hf;
159    BOOST_DEDUCED_TYPENAME T::hasher hf1(1);
160    BOOST_DEDUCED_TYPENAME T::hasher hf2(2);
161    BOOST_DEDUCED_TYPENAME T::key_equal eq;
162    BOOST_DEDUCED_TYPENAME T::key_equal eq1(1);
163    BOOST_DEDUCED_TYPENAME T::key_equal eq2(2);
164    BOOST_DEDUCED_TYPENAME T::allocator_type al;
165    BOOST_DEDUCED_TYPENAME T::allocator_type al1(1);
166    BOOST_DEDUCED_TYPENAME T::allocator_type al2(2);
167
168    std::cerr<<"Construct 1\n";
169    {
170        T x(10000, hf1, eq1);
171        BOOST_TEST(x.bucket_count() >= 10000);
172        BOOST_TEST(test::equivalent(x.hash_function(), hf1));
173        BOOST_TEST(test::equivalent(x.key_eq(), eq1));
174        BOOST_TEST(test::equivalent(x.get_allocator(), al));
175        test::check_equivalent_keys(x);
176    }
177
178    std::cerr<<"Construct 2\n";
179    {
180        T x(100, hf1);
181        BOOST_TEST(x.empty());
182        BOOST_TEST(x.bucket_count() >= 100);
183        BOOST_TEST(test::equivalent(x.hash_function(), hf1));
184        BOOST_TEST(test::equivalent(x.key_eq(), eq));
185        BOOST_TEST(test::equivalent(x.get_allocator(), al));
186        test::check_equivalent_keys(x);
187    }
188
189    std::cerr<<"Construct 3\n";
190    {
191        test::random_values<T> v(100, generator);
192        T x(v.begin(), v.end(), 0, hf1, eq1);
193        BOOST_TEST(test::equivalent(x.hash_function(), hf1));
194        BOOST_TEST(test::equivalent(x.key_eq(), eq1));
195        BOOST_TEST(test::equivalent(x.get_allocator(), al));
196        test::check_container(x, v);
197        test::check_equivalent_keys(x);
198    }
199
200    std::cerr<<"Construct 4\n";
201    {
202        test::random_values<T> v(5, generator);
203        T x(v.begin(), v.end(), 1000, hf1);
204        BOOST_TEST(x.bucket_count() >= 1000);
205        BOOST_TEST(test::equivalent(x.hash_function(), hf1));
206        BOOST_TEST(test::equivalent(x.key_eq(), eq));
207        BOOST_TEST(test::equivalent(x.get_allocator(), al));
208        test::check_container(x, v);
209        test::check_equivalent_keys(x);
210    }
211
212
213    std::cerr<<"Construct 5\n";
214    {
215        test::random_values<T> v(100, generator);
216        T x(v.begin(), v.end(), 0, hf, eq, al1);
217        T y(x.begin(), x.end(), 0, hf1, eq1, al2);
218        test::check_container(x, v);
219        test::check_container(y, x);
220        test::check_equivalent_keys(x);
221        test::check_equivalent_keys(y);
222    }
223
224    std::cerr<<"Construct 6\n";
225    {
226        test::random_values<T> v(100, generator);
227        T x(v.begin(), v.end(), 0, hf1, eq1);
228        T y(x.begin(), x.end(), 0, hf, eq);
229        test::check_container(x, v);
230        test::check_container(y, x);
231        test::check_equivalent_keys(x);
232        test::check_equivalent_keys(y);
233    }
234
235    std::cerr<<"Construct 7\n";
236    {
237        test::random_values<T> v(100, generator);
238        T x(v.begin(), v.end(), 0, hf1, eq1);
239        T y(x.begin(), x.end(), 0, hf2, eq2);
240        test::check_container(x, v);
241        test::check_container(y, x);
242        test::check_equivalent_keys(x);
243        test::check_equivalent_keys(y);
244    }
245
246    std::cerr<<"Construct 8 - from input iterator\n";
247    {
248        test::random_values<T> v(100, generator);
249        BOOST_DEDUCED_TYPENAME test::random_values<T>::const_iterator
250            v_begin = v.begin(), v_end = v.end();
251        T x(test::input_iterator(v_begin),
252            test::input_iterator(v_end), 0, hf1, eq1);
253        BOOST_DEDUCED_TYPENAME T::const_iterator
254            x_begin = x.begin(), x_end = x.end();
255        T y(test::input_iterator(x_begin),
256            test::input_iterator(x_end), 0, hf2, eq2);
257        test::check_container(x, v);
258        test::check_container(y, x);
259        test::check_equivalent_keys(x);
260        test::check_equivalent_keys(y);
261    }
262    
263    std::cerr<<"Construct 8.5 - from copy iterator\n";
264    {
265        test::random_values<T> v(100, generator);
266        T x(test::copy_iterator(v.begin()),
267            test::copy_iterator(v.end()), 0, hf1, eq1);
268        T y(test::copy_iterator(x.begin()),
269            test::copy_iterator(x.end()), 0, hf2, eq2);
270        test::check_container(x, v);
271        test::check_container(y, x);
272        test::check_equivalent_keys(x);
273        test::check_equivalent_keys(y);
274    }
275    
276    std::cerr<<"Construct 9\n";
277    {
278        test::random_values<T> v(100, generator);
279        T x(50);
280        BOOST_TEST(x.bucket_count() >= 50);
281        x.max_load_factor(10);
282        BOOST_TEST(x.bucket_count() >= 50);
283        x.insert(v.begin(), v.end());
284        BOOST_TEST(x.bucket_count() >= 50);
285        test::check_container(x, v);
286        test::check_equivalent_keys(x);
287    }
288
289#if !defined(BOOST_NO_0X_HDR_INITIALIZER_LIST)
290    std::initializer_list<BOOST_DEDUCED_TYPENAME T::value_type> list;
291    
292    std::cerr<<"Initializer list construct 1\n";
293    {
294        T x(list);
295        BOOST_TEST(x.empty());
296        BOOST_TEST(test::equivalent(x.hash_function(), hf));
297        BOOST_TEST(test::equivalent(x.key_eq(), eq));
298        BOOST_TEST(test::equivalent(x.get_allocator(), al));
299    }
300
301    std::cerr<<"Initializer list construct 2\n";
302    {
303        T x(list, 1000);
304        BOOST_TEST(x.empty());
305        BOOST_TEST(x.bucket_count() >= 1000);
306        BOOST_TEST(test::equivalent(x.hash_function(), hf));
307        BOOST_TEST(test::equivalent(x.key_eq(), eq));
308        BOOST_TEST(test::equivalent(x.get_allocator(), al));
309    }
310
311    std::cerr<<"Initializer list construct 3\n";
312    {
313        T x(list, 10, hf1);
314        BOOST_TEST(x.empty());
315        BOOST_TEST(x.bucket_count() >= 10);
316        BOOST_TEST(test::equivalent(x.hash_function(), hf1));
317        BOOST_TEST(test::equivalent(x.key_eq(), eq));
318        BOOST_TEST(test::equivalent(x.get_allocator(), al));
319    }
320
321    std::cerr<<"Initializer list construct 4\n";
322    {
323        T x(list, 10, hf1, eq1);
324        BOOST_TEST(x.empty());
325        BOOST_TEST(x.bucket_count() >= 10);
326        BOOST_TEST(test::equivalent(x.hash_function(), hf1));
327        BOOST_TEST(test::equivalent(x.key_eq(), eq1));
328        BOOST_TEST(test::equivalent(x.get_allocator(), al));
329    }
330
331    std::cerr<<"Initializer list construct 5\n";
332    {
333        T x(list, 10, hf1, eq1, al1);
334        BOOST_TEST(x.empty());
335        BOOST_TEST(x.bucket_count() >= 10);
336        BOOST_TEST(test::equivalent(x.hash_function(), hf1));
337        BOOST_TEST(test::equivalent(x.key_eq(), eq1));
338        BOOST_TEST(test::equivalent(x.get_allocator(), al1));
339    }
340#endif
341}
342
343template <class T>
344void map_constructor_test(T* = 0,
345    test::random_generator const& generator = test::default_generator)
346{
347    std::cerr<<"map_constructor_test\n";
348
349    typedef test::list<
350        std::pair<
351            BOOST_DEDUCED_TYPENAME T::key_type,
352            BOOST_DEDUCED_TYPENAME T::mapped_type
353        >
354    > list;
355    test::random_values<T> v(1000, generator);
356    list l(v.begin(), v.end());
357    T x(l.begin(), l.end());
358
359    test::check_container(x, v);
360    test::check_equivalent_keys(x);
361}
362
363boost::unordered_set<test::object,
364    test::hash, test::equal_to,
365    test::allocator<test::object> >* test_set;
366boost::unordered_multiset<test::object,
367    test::hash, test::equal_to,
368    test::allocator<test::object> >* test_multiset;
369boost::unordered_map<test::object, test::object,
370    test::hash, test::equal_to,
371    test::allocator<test::object> >* test_map;
372boost::unordered_multimap<test::object, test::object,
373    test::hash, test::equal_to,
374    test::allocator<test::object> >* test_multimap;
375
376using test::default_generator;
377using test::generate_collisions;
378
379UNORDERED_TEST(constructor_tests1,
380    ((test_set)(test_multiset)(test_map)(test_multimap))
381    ((default_generator)(generate_collisions))
382)
383
384UNORDERED_TEST(constructor_tests2,
385    ((test_set)(test_multiset)(test_map)(test_multimap))
386    ((default_generator)(generate_collisions))
387)
388
389UNORDERED_TEST(map_constructor_test,
390    ((test_map)(test_multimap))
391)
392
393#if !defined(BOOST_NO_0X_HDR_INITIALIZER_LIST)
394
395UNORDERED_AUTO_TEST(test_default_initializer_list) {
396    std::cerr<<"Initializer List Tests\n";
397    std::initializer_list<int> init;
398    boost::unordered_set<int> x1 = init;
399    BOOST_TEST(x1.empty());
400}
401
402#endif
403
404#if !defined(BOOST_NO_0X_HDR_INITIALIZER_LIST) && \
405    !defined(BOOST_NO_INITIALIZER_LISTS)
406
407UNORDERED_AUTO_TEST(test_initializer_list) {
408    std::cerr<<"Initializer List Tests\n";
409    boost::unordered_set<int> x1 = { 2, 10, 45, -5 };
410    BOOST_TEST(x1.find(10) != x1.end());
411    BOOST_TEST(x1.find(46) == x1.end());
412}
413
414#endif
415
416}
417
418RUN_TESTS()