| 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 | // unordered_multimap& operator=(const unordered_multimap& u); |
| 16 | |
| 17 | #include <unordered_map> |
| 18 | #include <string> |
| 19 | #include <set> |
| 20 | #include <cassert> |
| 21 | #include <cfloat> |
| 22 | #include <cmath> |
| 23 | #include <algorithm> |
| 24 | #include <cstddef> |
| 25 | |
| 26 | #include "test_macros.h" |
| 27 | #include "../../../check_consecutive.h" |
| 28 | #include "../../../test_compare.h" |
| 29 | #include "../../../test_hash.h" |
| 30 | #include "test_allocator.h" |
| 31 | #include "min_allocator.h" |
| 32 | |
| 33 | int main(int, char**) { |
| 34 | { |
| 35 | typedef test_allocator<std::pair<const int, std::string> > A; |
| 36 | typedef std::unordered_multimap<int, std::string, test_hash<int>, test_equal_to<int>, A > C; |
| 37 | typedef std::pair<int, std::string> P; |
| 38 | P a[] = { |
| 39 | P(1, "one" ), |
| 40 | P(2, "two" ), |
| 41 | P(3, "three" ), |
| 42 | P(4, "four" ), |
| 43 | P(1, "four" ), |
| 44 | P(2, "four" ), |
| 45 | }; |
| 46 | C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A(10)); |
| 47 | C c(a, a + 2, 7, test_hash<int>(2), test_equal_to<int>(3), A(4)); |
| 48 | c = c0; |
| 49 | LIBCPP_ASSERT(c.bucket_count() == 7); |
| 50 | assert(c.size() == 6); |
| 51 | std::multiset<std::string> s; |
| 52 | s.insert(x: "one" ); |
| 53 | s.insert(x: "four" ); |
| 54 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s); |
| 55 | s.insert(x: "two" ); |
| 56 | s.insert(x: "four" ); |
| 57 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s); |
| 58 | s.insert(x: "three" ); |
| 59 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 3), end: c.end(), key: 3, values&: s); |
| 60 | s.insert(x: "four" ); |
| 61 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 4), end: c.end(), key: 4, values&: s); |
| 62 | assert(c.hash_function() == test_hash<int>(8)); |
| 63 | assert(c.key_eq() == test_equal_to<int>(9)); |
| 64 | assert(c.get_allocator() == A(4)); |
| 65 | assert(!c.empty()); |
| 66 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 67 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 68 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 69 | assert(c.max_load_factor() == 1); |
| 70 | } |
| 71 | { |
| 72 | typedef std::unordered_multimap<int, std::string> C; |
| 73 | typedef std::pair<const int, std::string> P; |
| 74 | const P a[] = { |
| 75 | P(1, "one" ), |
| 76 | P(2, "two" ), |
| 77 | P(3, "three" ), |
| 78 | P(4, "four" ), |
| 79 | P(1, "four" ), |
| 80 | P(2, "four" ), |
| 81 | }; |
| 82 | C c(a, a + sizeof(a) / sizeof(a[0])); |
| 83 | C* p = &c; |
| 84 | c = *p; |
| 85 | assert(c.size() == 6); |
| 86 | assert(std::is_permutation(c.begin(), c.end(), a)); |
| 87 | } |
| 88 | { |
| 89 | typedef other_allocator<std::pair<const int, std::string> > A; |
| 90 | typedef std::unordered_multimap<int, std::string, test_hash<int>, test_equal_to<int>, A > C; |
| 91 | typedef std::pair<int, std::string> P; |
| 92 | P a[] = { |
| 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 | C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A(10)); |
| 101 | C c(a, a + 2, 7, test_hash<int>(2), test_equal_to<int>(3), A(4)); |
| 102 | c = c0; |
| 103 | assert(c.bucket_count() >= 7); |
| 104 | assert(c.size() == 6); |
| 105 | std::multiset<std::string> s; |
| 106 | s.insert(x: "one" ); |
| 107 | s.insert(x: "four" ); |
| 108 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s); |
| 109 | s.insert(x: "two" ); |
| 110 | s.insert(x: "four" ); |
| 111 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s); |
| 112 | s.insert(x: "three" ); |
| 113 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 3), end: c.end(), key: 3, values&: s); |
| 114 | s.insert(x: "four" ); |
| 115 | CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 4), end: c.end(), key: 4, values&: s); |
| 116 | assert(c.hash_function() == test_hash<int>(8)); |
| 117 | assert(c.key_eq() == test_equal_to<int>(9)); |
| 118 | assert(c.get_allocator() == A(10)); |
| 119 | assert(!c.empty()); |
| 120 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 121 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 122 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 123 | assert(c.max_load_factor() == 1); |
| 124 | } |
| 125 | #if TEST_STD_VER >= 11 |
| 126 | { |
| 127 | typedef min_allocator<std::pair<const int, std::string> > A; |
| 128 | typedef std::unordered_multimap<int, std::string, test_hash<int>, test_equal_to<int>, A > C; |
| 129 | typedef std::pair<int, std::string> P; |
| 130 | P a[] = { |
| 131 | P(1, "one" ), |
| 132 | P(2, "two" ), |
| 133 | P(3, "three" ), |
| 134 | P(4, "four" ), |
| 135 | P(1, "four" ), |
| 136 | P(2, "four" ), |
| 137 | }; |
| 138 | C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A()); |
| 139 | C c(a, a + 2, 7, test_hash<int>(2), test_equal_to<int>(3), A()); |
| 140 | c = c0; |
| 141 | LIBCPP_ASSERT(c.bucket_count() == 7); |
| 142 | assert(c.size() == 6); |
| 143 | std::multiset<std::string> s; |
| 144 | s.insert("one" ); |
| 145 | s.insert("four" ); |
| 146 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
| 147 | s.insert("two" ); |
| 148 | s.insert("four" ); |
| 149 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
| 150 | s.insert("three" ); |
| 151 | CheckConsecutiveKeys<C::const_iterator>(c.find(3), c.end(), 3, s); |
| 152 | s.insert("four" ); |
| 153 | CheckConsecutiveKeys<C::const_iterator>(c.find(4), c.end(), 4, s); |
| 154 | assert(c.hash_function() == test_hash<int>(8)); |
| 155 | assert(c.key_eq() == test_equal_to<int>(9)); |
| 156 | assert(c.get_allocator() == A()); |
| 157 | assert(!c.empty()); |
| 158 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
| 159 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
| 160 | assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON); |
| 161 | assert(c.max_load_factor() == 1); |
| 162 | } |
| 163 | #endif |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |