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 checks the runtime requirements of containers.
7
8 #include "../helpers/prefix.hpp"
9 #include <boost/unordered_set.hpp>
10 #include <boost/unordered_map.hpp>
11 #include "../helpers/postfix.hpp"
12
13 #include "../helpers/test.hpp"
14 #include <cstdlib>
15 #include <algorithm>
16 #include "../helpers/equivalent.hpp"
17
18 template <class X>
simple_test(X const & a)19 void simple_test(X const& a)
20 {
21 test::unordered_equivalence_tester<X> equivalent(a);
22
23 {
24 X u;
25 BOOST_TEST(u.size() == 0);
26 BOOST_TEST(X().size() == 0);
27 }
28
29 {
30 BOOST_TEST(equivalent(X(a)));
31 }
32
33 {
34 X u(a);
35 BOOST_TEST(equivalent(u));
36 }
37
38 {
39 X u = a;
40 BOOST_TEST(equivalent(u));
41 }
42
43 {
44 X b(a);
45 BOOST_TEST(b.begin() == const_cast<X const&>(b).cbegin());
46 BOOST_TEST(b.end() == const_cast<X const&>(b).cend());
47 }
48
49 {
50 X b(a);
51 X c;
52 BOOST_TEST(equivalent(b));
53 BOOST_TEST(c.empty());
54 b.swap(c);
55 BOOST_TEST(b.empty());
56 BOOST_TEST(equivalent(c));
57 b.swap(c);
58 BOOST_TEST(c.empty());
59 BOOST_TEST(equivalent(b));
60 }
61
62 {
63 X u;
64 X& r = u;
65 BOOST_TEST(&(r = r) == &r);
66
67 BOOST_TEST(r.empty());
68 BOOST_TEST(&(r = a) == &r);
69 BOOST_TEST(equivalent(r));
70 BOOST_TEST(&(r = r) == &r);
71 BOOST_TEST(equivalent(r));
72 }
73
74 {
75 BOOST_TEST(a.size() ==
76 static_cast<BOOST_DEDUCED_TYPENAME X::size_type>(
77 std::distance(a.begin(), a.end())));
78 }
79
80 {
81 BOOST_TEST(a.empty() == (a.size() == 0));
82 }
83
84 {
85 BOOST_TEST(a.empty() == (a.begin() == a.end()));
86 X u;
87 BOOST_TEST(u.begin() == u.end());
88 }
89 }
90
UNORDERED_AUTO_TEST(simple_tests)91 UNORDERED_AUTO_TEST(simple_tests)
92 {
93 using namespace std;
94 srand(14878);
95
96 std::cout<<"Test unordered_set.\n";
97 boost::unordered_set<int> set;
98 simple_test(set);
99
100 set.insert(1); set.insert(2); set.insert(1456);
101 simple_test(set);
102
103 std::cout<<"Test unordered_multiset.\n";
104 boost::unordered_multiset<int> multiset;
105 simple_test(multiset);
106
107 for(int i1 = 0; i1 < 1000; ++i1) {
108 int count = rand() % 10, index = rand();
109 for(int j = 0; j < count; ++j)
110 multiset.insert(index);
111 }
112 simple_test(multiset);
113
114 std::cout<<"Test unordered_map.\n";
115 boost::unordered_map<int, int> map;
116
117 for(int i2 = 0; i2 < 1000; ++i2) {
118 map.insert(std::pair<const int, int>(rand(), rand()));
119 }
120 simple_test(map);
121
122 std::cout<<"Test unordered_multimap.\n";
123 boost::unordered_multimap<int, int> multimap;
124
125 for(int i3 = 0; i3 < 1000; ++i3) {
126 int count = rand() % 10, index = rand();
127 for(int j = 0; j < count; ++j)
128 multimap.insert(std::pair<const int, int>(index, rand()));
129 }
130 simple_test(multimap);
131 }
132
133 RUN_TESTS()
134