| 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(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 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 | |
| 43 | C c0(7, test_hash<int>(8), test_equal_to<int>(9), test_allocator<std::pair<const int, std::string> >(10)); |
| 44 | C c = std::move(c0); |
| 45 | LIBCPP_ASSERT(c.bucket_count() == 7); |
| 46 | assert(c.size() == 0); |
| 47 | assert(c.hash_function() == test_hash<int>(8)); |
| 48 | assert(c.key_eq() == test_equal_to<int>(9)); |
| 49 | assert(c.get_allocator() == (test_allocator<std::pair<const int, std::string> >(10))); |
| 50 | assert(c.empty()); |
| 51 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 52 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 53 | assert(c.load_factor() == 0); |
| 54 | assert(c.max_load_factor() == 1); |
| 55 | |
| 56 | assert(c0.empty()); |
| 57 | } |
| 58 | { |
| 59 | typedef std::unordered_multimap<int, |
| 60 | std::string, |
| 61 | test_hash<int>, |
| 62 | test_equal_to<int>, |
| 63 | test_allocator<std::pair<const int, std::string> > > |
| 64 | C; |
| 65 | typedef std::pair<int, std::string> P; |
| 66 | P a[] = { |
| 67 | P(1, "one" ), |
| 68 | P(2, "two" ), |
| 69 | P(3, "three" ), |
| 70 | P(4, "four" ), |
| 71 | P(1, "four" ), |
| 72 | P(2, "four" ), |
| 73 | }; |
| 74 | C c0(a, |
| 75 | a + sizeof(a) / sizeof(a[0]), |
| 76 | 7, |
| 77 | test_hash<int>(8), |
| 78 | test_equal_to<int>(9), |
| 79 | test_allocator<std::pair<const int, std::string> >(10)); |
| 80 | C::iterator it0 = c0.begin(); |
| 81 | C c = std::move(c0); |
| 82 | assert(it0 == c.begin()); // Iterators remain valid |
| 83 | LIBCPP_ASSERT(c.bucket_count() == 7); |
| 84 | assert(c.size() == 6); |
| 85 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
| 86 | Eq eq = c.equal_range(1); |
| 87 | assert(std::distance(eq.first, eq.second) == 2); |
| 88 | std::multiset<std::string> s; |
| 89 | s.insert(x: "one" ); |
| 90 | s.insert(x: "four" ); |
| 91 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
| 92 | eq = c.equal_range(2); |
| 93 | assert(std::distance(eq.first, eq.second) == 2); |
| 94 | s.insert(x: "two" ); |
| 95 | s.insert(x: "four" ); |
| 96 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
| 97 | |
| 98 | eq = c.equal_range(3); |
| 99 | assert(std::distance(eq.first, eq.second) == 1); |
| 100 | C::const_iterator i = eq.first; |
| 101 | assert(i->first == 3); |
| 102 | assert(i->second == "three" ); |
| 103 | eq = c.equal_range(4); |
| 104 | assert(std::distance(eq.first, eq.second) == 1); |
| 105 | i = eq.first; |
| 106 | assert(i->first == 4); |
| 107 | assert(i->second == "four" ); |
| 108 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 109 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 110 | assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 111 | assert(c.max_load_factor() == 1); |
| 112 | assert(c.hash_function() == test_hash<int>(8)); |
| 113 | assert(c.key_eq() == test_equal_to<int>(9)); |
| 114 | assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >(10))); |
| 115 | |
| 116 | assert(c0.empty()); |
| 117 | } |
| 118 | { |
| 119 | typedef std::unordered_multimap<int, |
| 120 | std::string, |
| 121 | test_hash<int>, |
| 122 | test_equal_to<int>, |
| 123 | min_allocator<std::pair<const int, std::string> > > |
| 124 | C; |
| 125 | C c0(7, test_hash<int>(8), test_equal_to<int>(9), min_allocator<std::pair<const int, std::string> >()); |
| 126 | C c = std::move(c0); |
| 127 | LIBCPP_ASSERT(c.bucket_count() == 7); |
| 128 | assert(c.size() == 0); |
| 129 | assert(c.hash_function() == test_hash<int>(8)); |
| 130 | assert(c.key_eq() == test_equal_to<int>(9)); |
| 131 | assert(c.get_allocator() == (min_allocator<std::pair<const int, std::string> >())); |
| 132 | assert(c.empty()); |
| 133 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 134 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 135 | assert(c.load_factor() == 0); |
| 136 | assert(c.max_load_factor() == 1); |
| 137 | |
| 138 | assert(c0.empty()); |
| 139 | } |
| 140 | { |
| 141 | typedef std::unordered_multimap<int, |
| 142 | std::string, |
| 143 | test_hash<int>, |
| 144 | test_equal_to<int>, |
| 145 | min_allocator<std::pair<const int, std::string> > > |
| 146 | C; |
| 147 | typedef std::pair<int, std::string> P; |
| 148 | P a[] = { |
| 149 | P(1, "one" ), |
| 150 | P(2, "two" ), |
| 151 | P(3, "three" ), |
| 152 | P(4, "four" ), |
| 153 | P(1, "four" ), |
| 154 | P(2, "four" ), |
| 155 | }; |
| 156 | C c0(a, |
| 157 | a + sizeof(a) / sizeof(a[0]), |
| 158 | 7, |
| 159 | test_hash<int>(8), |
| 160 | test_equal_to<int>(9), |
| 161 | min_allocator<std::pair<const int, std::string> >()); |
| 162 | C::iterator it0 = c0.begin(); |
| 163 | C c = std::move(c0); |
| 164 | assert(it0 == c.begin()); // Iterators remain valid |
| 165 | LIBCPP_ASSERT(c.bucket_count() == 7); |
| 166 | assert(c.size() == 6); |
| 167 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
| 168 | Eq eq = c.equal_range(1); |
| 169 | assert(std::distance(eq.first, eq.second) == 2); |
| 170 | std::multiset<std::string> s; |
| 171 | s.insert(x: "one" ); |
| 172 | s.insert(x: "four" ); |
| 173 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
| 174 | eq = c.equal_range(2); |
| 175 | assert(std::distance(eq.first, eq.second) == 2); |
| 176 | s.insert(x: "two" ); |
| 177 | s.insert(x: "four" ); |
| 178 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
| 179 | |
| 180 | eq = c.equal_range(3); |
| 181 | assert(std::distance(eq.first, eq.second) == 1); |
| 182 | C::const_iterator i = eq.first; |
| 183 | assert(i->first == 3); |
| 184 | assert(i->second == "three" ); |
| 185 | eq = c.equal_range(4); |
| 186 | assert(std::distance(eq.first, eq.second) == 1); |
| 187 | i = eq.first; |
| 188 | assert(i->first == 4); |
| 189 | assert(i->second == "four" ); |
| 190 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 191 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 192 | assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 193 | assert(c.max_load_factor() == 1); |
| 194 | assert(c.hash_function() == test_hash<int>(8)); |
| 195 | assert(c.key_eq() == test_equal_to<int>(9)); |
| 196 | assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >())); |
| 197 | |
| 198 | assert(c0.empty()); |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |