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