Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / unordered / test / unordered / simple_tests.cpp
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
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<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
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);
101     set.insert(2);
102     set.insert(1456);
103     simple_test(set);
104
105     std::cout << "Test unordered_multiset.\n";
106     boost::unordered_multiset<int> multiset;
107     simple_test(multiset);
108
109     for (int i1 = 0; i1 < 1000; ++i1) {
110         int count = rand() % 10, index = rand();
111         for (int j = 0; j < count; ++j)
112             multiset.insert(index);
113     }
114     simple_test(multiset);
115
116     std::cout << "Test unordered_map.\n";
117     boost::unordered_map<int, int> map;
118
119     for (int i2 = 0; i2 < 1000; ++i2) {
120         map.insert(std::pair<const int, int>(rand(), rand()));
121     }
122     simple_test(map);
123
124     std::cout << "Test unordered_multimap.\n";
125     boost::unordered_multimap<int, int> multimap;
126
127     for (int i3 = 0; i3 < 1000; ++i3) {
128         int count = rand() % 10, index = rand();
129         for (int j = 0; j < count; ++j)
130             multimap.insert(std::pair<const int, int>(index, rand()));
131     }
132     simple_test(multimap);
133 }
134
135 RUN_TESTS()