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