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