]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | |
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) | |
5 | ||
b32b8144 | 6 | // clang-format off |
7c673cae FG |
7 | #include "../helpers/prefix.hpp" |
8 | #include <boost/unordered_set.hpp> | |
9 | #include <boost/unordered_map.hpp> | |
10 | #include "../helpers/postfix.hpp" | |
b32b8144 | 11 | // clang-format on |
7c673cae FG |
12 | |
13 | #include "../helpers/test.hpp" | |
14 | #include "../helpers/invariants.hpp" | |
15 | ||
16 | #include <map> | |
17 | #include <set> | |
7c673cae | 18 | |
b32b8144 FG |
19 | namespace insert_hint { |
20 | UNORDERED_AUTO_TEST (insert_hint_empty) { | |
7c673cae FG |
21 | typedef boost::unordered_multiset<int> container; |
22 | container x; | |
23 | x.insert(x.cbegin(), 10); | |
24 | BOOST_TEST_EQ(x.size(), 1u); | |
25 | BOOST_TEST_EQ(x.count(10), 1u); | |
26 | test::check_equivalent_keys(x); | |
b32b8144 | 27 | } |
7c673cae | 28 | |
b32b8144 | 29 | UNORDERED_AUTO_TEST (insert_hint_empty2) { |
7c673cae FG |
30 | typedef boost::unordered_multimap<std::string, int> container; |
31 | container x; | |
32 | x.emplace_hint(x.cbegin(), "hello", 50); | |
33 | BOOST_TEST_EQ(x.size(), 1u); | |
34 | BOOST_TEST_EQ(x.count("hello"), 1u); | |
35 | BOOST_TEST_EQ(x.find("hello")->second, 50); | |
36 | test::check_equivalent_keys(x); | |
b32b8144 | 37 | } |
7c673cae | 38 | |
b32b8144 | 39 | UNORDERED_AUTO_TEST (insert_hint_single) { |
7c673cae FG |
40 | typedef boost::unordered_multiset<std::string> container; |
41 | container x; | |
42 | x.insert("equal"); | |
43 | x.insert(x.cbegin(), "equal"); | |
44 | BOOST_TEST_EQ(x.size(), 2u); | |
45 | BOOST_TEST_EQ(x.count("equal"), 2u); | |
46 | test::check_equivalent_keys(x); | |
b32b8144 | 47 | } |
7c673cae | 48 | |
b32b8144 | 49 | UNORDERED_AUTO_TEST (insert_hint_single2) { |
7c673cae FG |
50 | typedef boost::unordered_multimap<int, std::string> container; |
51 | container x; | |
52 | x.emplace(10, "one"); | |
53 | x.emplace_hint(x.cbegin(), 10, "two"); | |
54 | BOOST_TEST_EQ(x.size(), 2u); | |
55 | BOOST_TEST_EQ(x.count(10), 2u); | |
56 | ||
57 | container::iterator it = x.find(10); | |
58 | std::string v0 = (it++)->second; | |
59 | std::string v1 = (it++)->second; | |
60 | ||
61 | BOOST_TEST(v0 == "one" || v0 == "two"); | |
62 | BOOST_TEST(v1 == "one" || v1 == "two"); | |
63 | BOOST_TEST(v0 != v1); | |
64 | ||
65 | test::check_equivalent_keys(x); | |
b32b8144 | 66 | } |
7c673cae | 67 | |
b32b8144 | 68 | UNORDERED_AUTO_TEST (insert_hint_multiple) { |
7c673cae | 69 | for (unsigned int size = 0; size < 10; ++size) { |
b32b8144 FG |
70 | for (unsigned int offset = 0; offset <= size; ++offset) { |
71 | typedef boost::unordered_multiset<std::string> container; | |
72 | container x; | |
7c673cae | 73 | |
b32b8144 FG |
74 | for (unsigned int i = 0; i < size; ++i) { |
75 | x.insert("multiple"); | |
76 | } | |
7c673cae | 77 | |
b32b8144 | 78 | BOOST_TEST_EQ(x.size(), size); |
7c673cae | 79 | |
b32b8144 FG |
80 | container::const_iterator position = x.cbegin(); |
81 | for (unsigned int i = 0; i < offset; ++i) { | |
82 | ++position; | |
83 | } | |
7c673cae | 84 | |
b32b8144 | 85 | x.insert(position, "multiple"); |
7c673cae | 86 | |
b32b8144 FG |
87 | BOOST_TEST_EQ(x.size(), size + 1u); |
88 | BOOST_TEST_EQ(x.count("multiple"), size + 1u); | |
89 | test::check_equivalent_keys(x); | |
90 | } | |
7c673cae | 91 | } |
b32b8144 | 92 | } |
7c673cae | 93 | |
b32b8144 | 94 | UNORDERED_AUTO_TEST (insert_hint_unique) { |
7c673cae FG |
95 | typedef boost::unordered_set<int> container; |
96 | container x; | |
97 | x.insert(x.cbegin(), 10); | |
98 | BOOST_TEST_EQ(x.size(), 1u); | |
99 | BOOST_TEST_EQ(x.count(10), 1u); | |
100 | test::check_equivalent_keys(x); | |
b32b8144 | 101 | } |
7c673cae | 102 | |
b32b8144 | 103 | UNORDERED_AUTO_TEST (insert_hint_unique_single) { |
7c673cae FG |
104 | typedef boost::unordered_set<int> container; |
105 | container x; | |
106 | x.insert(10); | |
107 | ||
108 | x.insert(x.cbegin(), 10); | |
109 | BOOST_TEST_EQ(x.size(), 1u); | |
110 | BOOST_TEST_EQ(x.count(10), 1u); | |
111 | test::check_equivalent_keys(x); | |
112 | ||
113 | x.insert(x.cbegin(), 20); | |
114 | BOOST_TEST_EQ(x.size(), 2u); | |
115 | BOOST_TEST_EQ(x.count(10), 1u); | |
116 | BOOST_TEST_EQ(x.count(20), 1u); | |
117 | test::check_equivalent_keys(x); | |
b32b8144 | 118 | } |
7c673cae FG |
119 | } |
120 | ||
121 | RUN_TESTS() |