| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03 |
| 10 | |
| 11 | // <unordered_map> |
| 12 | |
| 13 | // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, |
| 14 | // class Alloc = allocator<pair<const Key, T>>> |
| 15 | // class unordered_multimap |
| 16 | |
| 17 | // unordered_multimap& operator=(unordered_multimap&& u); |
| 18 | |
| 19 | #include <unordered_map> |
| 20 | #include <string> |
| 21 | #include <set> |
| 22 | #include <cassert> |
| 23 | #include <cfloat> |
| 24 | #include <cmath> |
| 25 | #include <cstddef> |
| 26 | |
| 27 | #include "test_macros.h" |
| 28 | #include "../../../check_consecutive.h" |
| 29 | #include "../../../test_compare.h" |
| 30 | #include "../../../test_hash.h" |
| 31 | #include "test_allocator.h" |
| 32 | #include "min_allocator.h" |
| 33 | |
| 34 | int main(int, char**) { |
| 35 | { |
| 36 | typedef test_allocator<std::pair<const int, std::string> > A; |
| 37 | typedef std::unordered_multimap<int, std::string, test_hash<int>, test_equal_to<int>, A > C; |
| 38 | typedef std::pair<int, std::string> P; |
| 39 | P a[] = { |
| 40 | P(1, "one" ), |
| 41 | P(2, "two" ), |
| 42 | P(3, "three" ), |
| 43 | P(4, "four" ), |
| 44 | P(1, "four" ), |
| 45 | P(2, "four" ), |
| 46 | }; |
| 47 | C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A(10)); |
| 48 | C c(a, a + 2, 7, test_hash<int>(2), test_equal_to<int>(3), A(4)); |
| 49 | c = std::move(c0); |
| 50 | LIBCPP_ASSERT(c.bucket_count() == 7); |
| 51 | assert(c.size() == 6); |
| 52 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
| 53 | Eq eq = c.equal_range(x: 1); |
| 54 | assert(std::distance(eq.first, eq.second) == 2); |
| 55 | std::multiset<std::string> s; |
| 56 | s.insert(x: "one" ); |
| 57 | s.insert(x: "four" ); |
| 58 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s); |
| 59 | eq = c.equal_range(x: 2); |
| 60 | assert(std::distance(eq.first, eq.second) == 2); |
| 61 | s.insert(x: "two" ); |
| 62 | s.insert(x: "four" ); |
| 63 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s); |
| 64 | |
| 65 | eq = c.equal_range(x: 3); |
| 66 | assert(std::distance(eq.first, eq.second) == 1); |
| 67 | C::const_iterator i = eq.first; |
| 68 | assert(i->first == 3); |
| 69 | assert(i->second == "three" ); |
| 70 | eq = c.equal_range(x: 4); |
| 71 | assert(std::distance(eq.first, eq.second) == 1); |
| 72 | i = eq.first; |
| 73 | assert(i->first == 4); |
| 74 | assert(i->second == "four" ); |
| 75 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 76 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 77 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 78 | assert(c.max_load_factor() == 1); |
| 79 | } |
| 80 | { |
| 81 | typedef test_allocator<std::pair<const int, std::string> > A; |
| 82 | typedef std::unordered_multimap<int, std::string, test_hash<int>, test_equal_to<int>, A > C; |
| 83 | typedef std::pair<int, std::string> P; |
| 84 | P a[] = { |
| 85 | P(1, "one" ), |
| 86 | P(2, "two" ), |
| 87 | P(3, "three" ), |
| 88 | P(4, "four" ), |
| 89 | P(1, "four" ), |
| 90 | P(2, "four" ), |
| 91 | }; |
| 92 | C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A(10)); |
| 93 | C c(a, a + 2, 7, test_hash<int>(2), test_equal_to<int>(3), A(10)); |
| 94 | C::iterator it0 = c0.begin(); |
| 95 | c = std::move(c0); |
| 96 | assert(it0 == c.begin()); // Iterators remain valid |
| 97 | LIBCPP_ASSERT(c.bucket_count() == 7); |
| 98 | assert(c.size() == 6); |
| 99 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
| 100 | Eq eq = c.equal_range(x: 1); |
| 101 | assert(std::distance(eq.first, eq.second) == 2); |
| 102 | std::multiset<std::string> s; |
| 103 | s.insert(x: "one" ); |
| 104 | s.insert(x: "four" ); |
| 105 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s); |
| 106 | eq = c.equal_range(x: 2); |
| 107 | assert(std::distance(eq.first, eq.second) == 2); |
| 108 | s.insert(x: "two" ); |
| 109 | s.insert(x: "four" ); |
| 110 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s); |
| 111 | |
| 112 | eq = c.equal_range(x: 3); |
| 113 | assert(std::distance(eq.first, eq.second) == 1); |
| 114 | C::const_iterator i = eq.first; |
| 115 | assert(i->first == 3); |
| 116 | assert(i->second == "three" ); |
| 117 | eq = c.equal_range(x: 4); |
| 118 | assert(std::distance(eq.first, eq.second) == 1); |
| 119 | i = eq.first; |
| 120 | assert(i->first == 4); |
| 121 | assert(i->second == "four" ); |
| 122 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 123 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 124 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 125 | assert(c.max_load_factor() == 1); |
| 126 | } |
| 127 | { |
| 128 | typedef other_allocator<std::pair<const int, std::string> > A; |
| 129 | typedef std::unordered_multimap<int, std::string, test_hash<int>, test_equal_to<int>, A > C; |
| 130 | typedef std::pair<int, std::string> P; |
| 131 | P a[] = { |
| 132 | P(1, "one" ), |
| 133 | P(2, "two" ), |
| 134 | P(3, "three" ), |
| 135 | P(4, "four" ), |
| 136 | P(1, "four" ), |
| 137 | P(2, "four" ), |
| 138 | }; |
| 139 | C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A(10)); |
| 140 | C c(a, a + 2, 7, test_hash<int>(2), test_equal_to<int>(3), A(4)); |
| 141 | C::iterator it0 = c0.begin(); |
| 142 | c = std::move(c0); |
| 143 | assert(it0 == c.begin()); // Iterators remain valid |
| 144 | LIBCPP_ASSERT(c.bucket_count() == 7); |
| 145 | assert(c.size() == 6); |
| 146 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
| 147 | Eq eq = c.equal_range(x: 1); |
| 148 | assert(std::distance(eq.first, eq.second) == 2); |
| 149 | std::multiset<std::string> s; |
| 150 | s.insert(x: "one" ); |
| 151 | s.insert(x: "four" ); |
| 152 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s); |
| 153 | eq = c.equal_range(x: 2); |
| 154 | assert(std::distance(eq.first, eq.second) == 2); |
| 155 | s.insert(x: "two" ); |
| 156 | s.insert(x: "four" ); |
| 157 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s); |
| 158 | |
| 159 | eq = c.equal_range(x: 3); |
| 160 | assert(std::distance(eq.first, eq.second) == 1); |
| 161 | C::const_iterator i = eq.first; |
| 162 | assert(i->first == 3); |
| 163 | assert(i->second == "three" ); |
| 164 | eq = c.equal_range(x: 4); |
| 165 | assert(std::distance(eq.first, eq.second) == 1); |
| 166 | i = eq.first; |
| 167 | assert(i->first == 4); |
| 168 | assert(i->second == "four" ); |
| 169 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 170 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 171 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 172 | assert(c.max_load_factor() == 1); |
| 173 | } |
| 174 | { |
| 175 | typedef min_allocator<std::pair<const int, std::string> > A; |
| 176 | typedef std::unordered_multimap<int, std::string, test_hash<int>, test_equal_to<int>, A > C; |
| 177 | typedef std::pair<int, std::string> P; |
| 178 | P a[] = { |
| 179 | P(1, "one" ), |
| 180 | P(2, "two" ), |
| 181 | P(3, "three" ), |
| 182 | P(4, "four" ), |
| 183 | P(1, "four" ), |
| 184 | P(2, "four" ), |
| 185 | }; |
| 186 | C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A()); |
| 187 | C c(a, a + 2, 7, test_hash<int>(2), test_equal_to<int>(3), A()); |
| 188 | C::iterator it0 = c0.begin(); |
| 189 | c = std::move(c0); |
| 190 | assert(it0 == c.begin()); // Iterators remain valid |
| 191 | LIBCPP_ASSERT(c.bucket_count() == 7); |
| 192 | assert(c.size() == 6); |
| 193 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
| 194 | Eq eq = c.equal_range(x: 1); |
| 195 | assert(std::distance(eq.first, eq.second) == 2); |
| 196 | std::multiset<std::string> s; |
| 197 | s.insert(x: "one" ); |
| 198 | s.insert(x: "four" ); |
| 199 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s); |
| 200 | eq = c.equal_range(x: 2); |
| 201 | assert(std::distance(eq.first, eq.second) == 2); |
| 202 | s.insert(x: "two" ); |
| 203 | s.insert(x: "four" ); |
| 204 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s); |
| 205 | |
| 206 | eq = c.equal_range(x: 3); |
| 207 | assert(std::distance(eq.first, eq.second) == 1); |
| 208 | C::const_iterator i = eq.first; |
| 209 | assert(i->first == 3); |
| 210 | assert(i->second == "three" ); |
| 211 | eq = c.equal_range(x: 4); |
| 212 | assert(std::distance(eq.first, eq.second) == 1); |
| 213 | i = eq.first; |
| 214 | assert(i->first == 4); |
| 215 | assert(i->second == "four" ); |
| 216 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 217 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 218 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 219 | assert(c.max_load_factor() == 1); |
| 220 | } |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |