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