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 #include "../helpers/prefix.hpp"
7 #include <boost/unordered_set.hpp>
8 #include <boost/unordered_map.hpp>
9 #include "../helpers/postfix.hpp"
10
11 #include "../helpers/test.hpp"
12 #include <algorithm>
13 #include "../objects/test.hpp"
14 #include "../helpers/random_values.hpp"
15 #include "../helpers/helpers.hpp"
16
17 #if BOOST_WORKAROUND(BOOST_MSVC, < 1400)
18 #pragma warning(disable:4267) // conversion from 'size_t' to 'unsigned int',
19 // possible loss of data.
20 #endif
21
22 namespace bucket_tests {
23
24 test::seed_t initialize_seed(54635);
25
26 template <class X>
tests(X *,test::random_generator generator)27 void tests(X*, test::random_generator generator)
28 {
29 test::check_instances check_;
30
31 typedef BOOST_DEDUCED_TYPENAME X::size_type size_type;
32 typedef BOOST_DEDUCED_TYPENAME X::const_local_iterator const_local_iterator;
33 test::random_values<X> v(1000, generator);
34
35 X x(v.begin(), v.end());
36
37 BOOST_TEST(x.bucket_count() < x.max_bucket_count());
38 std::cerr<<x.bucket_count()<<"<"<<x.max_bucket_count()<<"\n";
39
40 for(BOOST_DEDUCED_TYPENAME test::random_values<X>::const_iterator
41 it = v.begin(), end = v.end(); it != end; ++it)
42 {
43 size_type bucket = x.bucket(test::get_key<X>(*it));
44
45 BOOST_TEST(bucket < x.bucket_count());
46 if(bucket < x.max_bucket_count()) {
47 // lit? lend?? I need a new naming scheme.
48 const_local_iterator lit = x.begin(bucket), lend = x.end(bucket);
49 while(lit != lend
50 && test::get_key<X>(*it) != test::get_key<X>(*lit))
51 {
52 ++lit;
53 }
54 BOOST_TEST(lit != lend);
55 }
56 }
57
58 for(size_type i = 0; i < x.bucket_count(); ++i) {
59 BOOST_TEST(x.bucket_size(i) == static_cast<size_type>(
60 std::distance(x.begin(i), x.end(i))));
61 BOOST_TEST(x.bucket_size(i) == static_cast<size_type>(
62 std::distance(x.cbegin(i), x.cend(i))));
63 X const& x_ref = x;
64 BOOST_TEST(x.bucket_size(i) == static_cast<size_type>(
65 std::distance(x_ref.begin(i), x_ref.end(i))));
66 BOOST_TEST(x.bucket_size(i) == static_cast<size_type>(
67 std::distance(x_ref.cbegin(i), x_ref.cend(i))));
68 }
69 }
70
71 boost::unordered_multimap<test::object, test::object,
72 test::hash, test::equal_to,
73 std::allocator<test::object> >* test_multimap_std_alloc;
74
75 boost::unordered_set<test::object,
76 test::hash, test::equal_to,
77 test::allocator2<test::object> >* test_set;
78 boost::unordered_multiset<test::object,
79 test::hash, test::equal_to,
80 test::allocator1<test::object> >* test_multiset;
81 boost::unordered_map<test::object, test::object,
82 test::hash, test::equal_to,
83 test::allocator1<test::object> >* test_map;
84 boost::unordered_multimap<test::object, test::object,
85 test::hash, test::equal_to,
86 test::allocator2<test::object> >* test_multimap;
87
88 using test::default_generator;
89 using test::generate_collisions;
90
91 UNORDERED_TEST(tests,
92 ((test_multimap_std_alloc)(test_set)(test_multiset)(test_map)(test_multimap))
93 ((default_generator)(generate_collisions))
94 )
95
96 }
97
98 RUN_TESTS()
99