| 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 | // <unordered_map> |
| 10 | |
| 11 | // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, |
| 12 | // class Alloc = allocator<pair<const Key, T>>> |
| 13 | // class unordered_multimap |
| 14 | |
| 15 | // template <class InputIterator> |
| 16 | // unordered_multimap(InputIterator first, InputIterator last); |
| 17 | |
| 18 | #include <unordered_map> |
| 19 | #include <string> |
| 20 | #include <set> |
| 21 | #include <cassert> |
| 22 | #include <cfloat> |
| 23 | #include <cmath> |
| 24 | #include <cstddef> |
| 25 | |
| 26 | #include "test_macros.h" |
| 27 | #include "test_iterators.h" |
| 28 | #include "../../../check_consecutive.h" |
| 29 | #include "../../../NotConstructible.h" |
| 30 | #include "../../../test_compare.h" |
| 31 | #include "../../../test_hash.h" |
| 32 | #include "test_allocator.h" |
| 33 | #include "min_allocator.h" |
| 34 | |
| 35 | int main(int, char**) { |
| 36 | { |
| 37 | typedef std::unordered_multimap<int, |
| 38 | std::string, |
| 39 | test_hash<int>, |
| 40 | test_equal_to<int>, |
| 41 | test_allocator<std::pair<const int, std::string> > > |
| 42 | C; |
| 43 | typedef std::pair<int, std::string> P; |
| 44 | P a[] = { |
| 45 | P(1, "one" ), |
| 46 | P(2, "two" ), |
| 47 | P(3, "three" ), |
| 48 | P(4, "four" ), |
| 49 | P(1, "four" ), |
| 50 | P(2, "four" ), |
| 51 | }; |
| 52 | C c(cpp17_input_iterator<P*>(a), cpp17_input_iterator<P*>(a + sizeof(a) / sizeof(a[0]))); |
| 53 | assert(c.bucket_count() >= 7); |
| 54 | assert(c.size() == 6); |
| 55 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
| 56 | Eq eq = c.equal_range(1); |
| 57 | assert(std::distance(eq.first, eq.second) == 2); |
| 58 | std::multiset<std::string> s; |
| 59 | s.insert(x: "one" ); |
| 60 | s.insert(x: "four" ); |
| 61 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
| 62 | eq = c.equal_range(2); |
| 63 | assert(std::distance(eq.first, eq.second) == 2); |
| 64 | s.insert(x: "two" ); |
| 65 | s.insert(x: "four" ); |
| 66 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
| 67 | |
| 68 | eq = c.equal_range(3); |
| 69 | assert(std::distance(eq.first, eq.second) == 1); |
| 70 | C::const_iterator i = eq.first; |
| 71 | assert(i->first == 3); |
| 72 | assert(i->second == "three" ); |
| 73 | eq = c.equal_range(4); |
| 74 | assert(std::distance(eq.first, eq.second) == 1); |
| 75 | i = eq.first; |
| 76 | assert(i->first == 4); |
| 77 | assert(i->second == "four" ); |
| 78 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 79 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 80 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 81 | assert(c.max_load_factor() == 1); |
| 82 | assert(c.hash_function() == test_hash<int>()); |
| 83 | assert(c.key_eq() == test_equal_to<int>()); |
| 84 | assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >())); |
| 85 | } |
| 86 | #if TEST_STD_VER >= 11 |
| 87 | { |
| 88 | typedef std::unordered_multimap<int, |
| 89 | std::string, |
| 90 | test_hash<int>, |
| 91 | test_equal_to<int>, |
| 92 | min_allocator<std::pair<const int, std::string> > > |
| 93 | C; |
| 94 | typedef std::pair<int, std::string> P; |
| 95 | P a[] = { |
| 96 | P(1, "one" ), |
| 97 | P(2, "two" ), |
| 98 | P(3, "three" ), |
| 99 | P(4, "four" ), |
| 100 | P(1, "four" ), |
| 101 | P(2, "four" ), |
| 102 | }; |
| 103 | C c(cpp17_input_iterator<P*>(a), cpp17_input_iterator<P*>(a + sizeof(a) / sizeof(a[0]))); |
| 104 | assert(c.bucket_count() >= 7); |
| 105 | assert(c.size() == 6); |
| 106 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
| 107 | Eq eq = c.equal_range(1); |
| 108 | assert(std::distance(eq.first, eq.second) == 2); |
| 109 | std::multiset<std::string> s; |
| 110 | s.insert("one" ); |
| 111 | s.insert("four" ); |
| 112 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
| 113 | eq = c.equal_range(2); |
| 114 | assert(std::distance(eq.first, eq.second) == 2); |
| 115 | s.insert("two" ); |
| 116 | s.insert("four" ); |
| 117 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
| 118 | |
| 119 | eq = c.equal_range(3); |
| 120 | assert(std::distance(eq.first, eq.second) == 1); |
| 121 | C::const_iterator i = eq.first; |
| 122 | assert(i->first == 3); |
| 123 | assert(i->second == "three" ); |
| 124 | eq = c.equal_range(4); |
| 125 | assert(std::distance(eq.first, eq.second) == 1); |
| 126 | i = eq.first; |
| 127 | assert(i->first == 4); |
| 128 | assert(i->second == "four" ); |
| 129 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 130 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 131 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 132 | assert(c.max_load_factor() == 1); |
| 133 | assert(c.hash_function() == test_hash<int>()); |
| 134 | assert(c.key_eq() == test_equal_to<int>()); |
| 135 | assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >())); |
| 136 | } |
| 137 | # if TEST_STD_VER > 11 |
| 138 | { |
| 139 | typedef std::pair<int, std::string> P; |
| 140 | typedef test_allocator<std::pair<const int, std::string>> A; |
| 141 | typedef test_hash<int> HF; |
| 142 | typedef test_equal_to<int> Comp; |
| 143 | typedef std::unordered_multimap<int, std::string, HF, Comp, A> C; |
| 144 | |
| 145 | P arr[] = { |
| 146 | P(1, "one" ), |
| 147 | P(2, "two" ), |
| 148 | P(3, "three" ), |
| 149 | P(4, "four" ), |
| 150 | P(1, "four" ), |
| 151 | P(2, "four" ), |
| 152 | }; |
| 153 | A a(42); |
| 154 | C c(cpp17_input_iterator<P*>(arr), cpp17_input_iterator<P*>(arr + sizeof(arr) / sizeof(arr[0])), 14, a); |
| 155 | assert(c.bucket_count() >= 14); |
| 156 | assert(c.size() == 6); |
| 157 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
| 158 | Eq eq = c.equal_range(1); |
| 159 | assert(std::distance(eq.first, eq.second) == 2); |
| 160 | std::multiset<std::string> s; |
| 161 | s.insert("one" ); |
| 162 | s.insert("four" ); |
| 163 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
| 164 | eq = c.equal_range(2); |
| 165 | assert(std::distance(eq.first, eq.second) == 2); |
| 166 | s.insert("two" ); |
| 167 | s.insert("four" ); |
| 168 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
| 169 | |
| 170 | eq = c.equal_range(3); |
| 171 | assert(std::distance(eq.first, eq.second) == 1); |
| 172 | C::const_iterator i = eq.first; |
| 173 | assert(i->first == 3); |
| 174 | assert(i->second == "three" ); |
| 175 | eq = c.equal_range(4); |
| 176 | assert(std::distance(eq.first, eq.second) == 1); |
| 177 | i = eq.first; |
| 178 | assert(i->first == 4); |
| 179 | assert(i->second == "four" ); |
| 180 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 181 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 182 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 183 | assert(c.max_load_factor() == 1); |
| 184 | assert(c.hash_function() == HF()); |
| 185 | assert(c.key_eq() == Comp()); |
| 186 | assert(c.get_allocator() == a); |
| 187 | assert(!(c.get_allocator() == A())); |
| 188 | } |
| 189 | { |
| 190 | typedef std::pair<int, std::string> P; |
| 191 | typedef test_allocator<std::pair<const int, std::string>> A; |
| 192 | typedef test_hash<int> HF; |
| 193 | typedef test_equal_to<int> Comp; |
| 194 | typedef std::unordered_multimap<int, std::string, HF, Comp, A> C; |
| 195 | |
| 196 | P arr[] = { |
| 197 | P(1, "one" ), |
| 198 | P(2, "two" ), |
| 199 | P(3, "three" ), |
| 200 | P(4, "four" ), |
| 201 | P(1, "four" ), |
| 202 | P(2, "four" ), |
| 203 | }; |
| 204 | A a(42); |
| 205 | HF hf(43); |
| 206 | C c(cpp17_input_iterator<P*>(arr), cpp17_input_iterator<P*>(arr + sizeof(arr) / sizeof(arr[0])), 12, hf, a); |
| 207 | assert(c.bucket_count() >= 12); |
| 208 | assert(c.size() == 6); |
| 209 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
| 210 | Eq eq = c.equal_range(1); |
| 211 | assert(std::distance(eq.first, eq.second) == 2); |
| 212 | std::multiset<std::string> s; |
| 213 | s.insert("one" ); |
| 214 | s.insert("four" ); |
| 215 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
| 216 | eq = c.equal_range(2); |
| 217 | assert(std::distance(eq.first, eq.second) == 2); |
| 218 | s.insert("two" ); |
| 219 | s.insert("four" ); |
| 220 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
| 221 | |
| 222 | eq = c.equal_range(3); |
| 223 | assert(std::distance(eq.first, eq.second) == 1); |
| 224 | C::const_iterator i = eq.first; |
| 225 | assert(i->first == 3); |
| 226 | assert(i->second == "three" ); |
| 227 | eq = c.equal_range(4); |
| 228 | assert(std::distance(eq.first, eq.second) == 1); |
| 229 | i = eq.first; |
| 230 | assert(i->first == 4); |
| 231 | assert(i->second == "four" ); |
| 232 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 233 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 234 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 235 | assert(c.max_load_factor() == 1); |
| 236 | assert(c.hash_function() == hf); |
| 237 | assert(!(c.hash_function() == HF())); |
| 238 | assert(c.key_eq() == Comp()); |
| 239 | assert(c.get_allocator() == a); |
| 240 | assert(!(c.get_allocator() == A())); |
| 241 | } |
| 242 | # endif |
| 243 | #endif |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |