| 1 | |
| 2 | // Copyright 2006-2009 Daniel James. |
| 3 | // Copyright 2022-2023 Christian Mazakas. |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | // This test checks the runtime requirements of containers. |
| 8 | |
| 9 | #include "../helpers/unordered.hpp" |
| 10 | |
| 11 | #include "../helpers/equivalent.hpp" |
| 12 | #include "../helpers/generators.hpp" |
| 13 | #include "../helpers/test.hpp" |
| 14 | #include <algorithm> |
| 15 | #include <cstdlib> |
| 16 | |
| 17 | test::seed_t initialize_seed(14878); |
| 18 | |
| 19 | template <class X> 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() == static_cast<typename X::size_type>( |
| 76 | std::distance(a.begin(), a.end()))); |
| 77 | } |
| 78 | |
| 79 | { |
| 80 | BOOST_TEST(a.empty() == (a.size() == 0)); |
| 81 | } |
| 82 | |
| 83 | { |
| 84 | BOOST_TEST(a.empty() == (a.begin() == a.end())); |
| 85 | X u; |
| 86 | BOOST_TEST(u.begin() == u.end()); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | template <class X> static void simple_set_tests(X*) |
| 91 | { |
| 92 | X x; |
| 93 | |
| 94 | simple_test(x); |
| 95 | |
| 96 | x.insert(1); |
| 97 | x.insert(2); |
| 98 | x.insert(1456); |
| 99 | simple_test(x); |
| 100 | } |
| 101 | |
| 102 | #ifndef BOOST_UNORDERED_FOA_TESTS |
| 103 | template <class X> static void simple_multiset_tests(X*) |
| 104 | { |
| 105 | X x; |
| 106 | simple_test(x); |
| 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 | x.insert(index); |
| 112 | } |
| 113 | simple_test(x); |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | template <class X> static void simple_map_tests(X*) |
| 118 | { |
| 119 | X x; |
| 120 | |
| 121 | for (int i2 = 0; i2 < 1000; ++i2) { |
| 122 | x.insert(std::pair<const int, int>(rand(), rand())); |
| 123 | } |
| 124 | simple_test(x); |
| 125 | } |
| 126 | |
| 127 | #ifndef BOOST_UNORDERED_FOA_TESTS |
| 128 | template <class X> static void simple_multimap_tests(X*) |
| 129 | { |
| 130 | X x; |
| 131 | |
| 132 | for (int i3 = 0; i3 < 1000; ++i3) { |
| 133 | int count = rand() % 10, index = rand(); |
| 134 | for (int j = 0; j < count; ++j) |
| 135 | x.insert(std::pair<const int, int>(index, rand())); |
| 136 | } |
| 137 | simple_test(x); |
| 138 | } |
| 139 | #endif |
| 140 | |
| 141 | #ifdef BOOST_UNORDERED_FOA_TESTS |
| 142 | static boost::unordered_flat_set<int>* flat_set; |
| 143 | static boost::unordered_flat_map<int, int>* flat_map; |
| 144 | static boost::unordered_node_set<int>* node_set; |
| 145 | static boost::unordered_node_map<int, int>* node_map; |
| 146 | |
| 147 | UNORDERED_TEST(simple_map_tests, ((flat_map)(node_map))) |
| 148 | UNORDERED_TEST(simple_set_tests, ((flat_set)(node_set))) |
| 149 | #else |
| 150 | static boost::unordered_set<int>* set; |
| 151 | static boost::unordered_map<int, int>* map; |
| 152 | static boost::unordered_multiset<int>* multiset; |
| 153 | static boost::unordered_multimap<int, int>* multimap; |
| 154 | |
| 155 | UNORDERED_TEST(simple_set_tests, ((set))) |
| 156 | UNORDERED_TEST(simple_map_tests, ((map))) |
| 157 | UNORDERED_TEST(simple_multiset_tests, ((multiset))) |
| 158 | UNORDERED_TEST(simple_multimap_tests, ((multimap))) |
| 159 | |
| 160 | #endif |
| 161 | |
| 162 | RUN_TESTS() |
| 163 | |