2 // Copyright 2016 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)
7 #include "../helpers/prefix.hpp"
8 #include <boost/unordered_map.hpp>
9 #include <boost/unordered_set.hpp>
10 #include "../helpers/postfix.hpp"
13 #include "../helpers/equivalent.hpp"
14 #include "../helpers/helpers.hpp"
15 #include "../helpers/invariants.hpp"
16 #include "../helpers/random_values.hpp"
17 #include "../helpers/test.hpp"
18 #include "../helpers/tracker.hpp"
19 #include "../objects/test.hpp"
20 #include <boost/next_prior.hpp>
24 namespace extract_tests {
26 test::seed_t initialize_seed(85638);
28 template <class Container>
29 void extract_tests1(Container*, test::random_generator generator)
31 std::cerr << "Extract by key.\n";
33 test::check_instances check_;
35 test::random_values<Container> v(1000, generator);
36 Container x(v.begin(), v.end());
38 for (BOOST_DEDUCED_TYPENAME test::random_values<Container>::iterator
40 it != v.end(); ++it) {
41 std::size_t count = x.count(test::get_key<Container>(*it));
42 std::size_t old_size = x.size();
43 std::size_t new_count = count ? count - 1 : count;
44 std::size_t new_size = count ? old_size - 1 : old_size;
45 typename Container::node_type n =
46 x.extract(test::get_key<Container>(*it));
47 BOOST_TEST((n ? true : false) == (count ? true : false));
48 BOOST_TEST(x.size() == new_size);
49 BOOST_TEST(x.count(test::get_key<Container>(*it)) == new_count);
51 BOOST_TEST(x.find(test::get_key<Container>(*it)) == x.end());
53 BOOST_TEST(x.find(test::get_key<Container>(*it)) != x.end());
55 if (++iterations % 20 == 0)
56 test::check_equivalent_keys(x);
58 BOOST_TEST(x.empty());
61 std::cerr << "extract(begin()).\n";
63 test::check_instances check_;
65 test::random_values<Container> v(1000, generator);
66 Container x(v.begin(), v.end());
67 std::size_t size = x.size();
69 while (size > 0 && !x.empty()) {
70 BOOST_DEDUCED_TYPENAME Container::key_type key =
71 test::get_key<Container>(*x.begin());
72 std::size_t count = x.count(key);
73 typename Container::node_type n = x.extract(x.begin());
76 BOOST_TEST(x.count(key) == count - 1);
77 BOOST_TEST(x.size() == size);
78 if (++iterations % 20 == 0)
79 test::check_equivalent_keys(x);
81 BOOST_TEST(x.empty());
84 std::cerr << "extract(random position).\n";
86 test::check_instances check_;
88 test::random_values<Container> v(1000, generator);
89 Container x(v.begin(), v.end());
90 std::size_t size = x.size();
92 while (size > 0 && !x.empty()) {
94 int index = rand() % (int)x.size();
95 BOOST_DEDUCED_TYPENAME Container::const_iterator prev, pos, next;
97 prev = pos = x.begin();
99 prev = boost::next(x.begin(), index - 1);
100 pos = boost::next(prev);
102 next = boost::next(pos);
103 BOOST_DEDUCED_TYPENAME Container::key_type key =
104 test::get_key<Container>(*pos);
105 std::size_t count = x.count(key);
106 typename Container::node_type n = x.extract(pos);
111 index == 0 ? next == x.begin() : next == boost::next(prev));
112 BOOST_TEST(x.count(key) == count - 1);
113 BOOST_TEST(x.size() == size);
114 if (++iterations % 20 == 0)
115 test::check_equivalent_keys(x);
117 BOOST_TEST(x.empty());
123 boost::unordered_set<test::object, test::hash, test::equal_to,
124 test::allocator1<test::object> >* test_set;
125 boost::unordered_multiset<test::object, test::hash, test::equal_to,
126 test::allocator2<test::object> >* test_multiset;
127 boost::unordered_map<test::object, test::object, test::hash, test::equal_to,
128 test::allocator1<test::object> >* test_map;
129 boost::unordered_multimap<test::object, test::object, test::hash,
130 test::equal_to, test::allocator2<test::object> >* test_multimap;
132 using test::default_generator;
133 using test::generate_collisions;
136 extract_tests1, ((test_set)(test_multiset)(test_map)(test_multimap))(
137 (default_generator)(generate_collisions)))