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); |
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 | { |
33 | typedef std::unordered_map<int, 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 | { |
41 | P(1, "one" ), |
42 | P(2, "two" ), |
43 | P(3, "three" ), |
44 | P(4, "four" ), |
45 | P(1, "four" ), |
46 | P(2, "four" ), |
47 | }; |
48 | C c0(a, 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 | ); |
54 | C c = c0; |
55 | LIBCPP_ASSERT(c.bucket_count() == 7); |
56 | assert(c.size() == 4); |
57 | assert(c.at(1) == "one" ); |
58 | assert(c.at(2) == "two" ); |
59 | assert(c.at(3) == "three" ); |
60 | assert(c.at(4) == "four" ); |
61 | assert(c.hash_function() == test_hash<int>(8)); |
62 | assert(c.key_eq() == test_equal_to<int>(9)); |
63 | assert(c.get_allocator() == |
64 | (test_allocator<std::pair<const int, std::string> >(10))); |
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(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
69 | assert(c.max_load_factor() == 1); |
70 | } |
71 | #if TEST_STD_VER >= 11 |
72 | { |
73 | typedef std::unordered_map<int, std::string, |
74 | test_hash<int>, |
75 | test_equal_to<int>, |
76 | other_allocator<std::pair<const int, std::string> > |
77 | > C; |
78 | typedef std::pair<int, std::string> P; |
79 | P a[] = |
80 | { |
81 | P(1, "one" ), |
82 | P(2, "two" ), |
83 | P(3, "three" ), |
84 | P(4, "four" ), |
85 | P(1, "four" ), |
86 | P(2, "four" ), |
87 | }; |
88 | C c0(a, a + sizeof(a)/sizeof(a[0]), |
89 | 7, |
90 | test_hash<int>(8), |
91 | test_equal_to<int>(9), |
92 | other_allocator<std::pair<const int, std::string> >(10) |
93 | ); |
94 | C c = c0; |
95 | LIBCPP_ASSERT(c.bucket_count() == 7); |
96 | assert(c.size() == 4); |
97 | assert(c.at(1) == "one" ); |
98 | assert(c.at(2) == "two" ); |
99 | assert(c.at(3) == "three" ); |
100 | assert(c.at(4) == "four" ); |
101 | assert(c.hash_function() == test_hash<int>(8)); |
102 | assert(c.key_eq() == test_equal_to<int>(9)); |
103 | assert(c.get_allocator() == |
104 | (other_allocator<std::pair<const int, std::string> >(-2))); |
105 | assert(!c.empty()); |
106 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
107 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
108 | assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
109 | assert(c.max_load_factor() == 1); |
110 | } |
111 | { |
112 | typedef std::unordered_map<int, std::string, |
113 | test_hash<int>, |
114 | test_equal_to<int>, |
115 | min_allocator<std::pair<const int, std::string> > |
116 | > C; |
117 | typedef std::pair<int, std::string> P; |
118 | P a[] = |
119 | { |
120 | P(1, "one" ), |
121 | P(2, "two" ), |
122 | P(3, "three" ), |
123 | P(4, "four" ), |
124 | P(1, "four" ), |
125 | P(2, "four" ), |
126 | }; |
127 | C c0(a, a + sizeof(a)/sizeof(a[0]), |
128 | 7, |
129 | test_hash<int>(8), |
130 | test_equal_to<int>(9), |
131 | min_allocator<std::pair<const int, std::string> >() |
132 | ); |
133 | C c = c0; |
134 | LIBCPP_ASSERT(c.bucket_count() == 7); |
135 | assert(c.size() == 4); |
136 | assert(c.at(1) == "one" ); |
137 | assert(c.at(2) == "two" ); |
138 | assert(c.at(3) == "three" ); |
139 | assert(c.at(4) == "four" ); |
140 | assert(c.hash_function() == test_hash<int>(8)); |
141 | assert(c.key_eq() == test_equal_to<int>(9)); |
142 | assert(c.get_allocator() == |
143 | (min_allocator<std::pair<const int, std::string> >())); |
144 | assert(!c.empty()); |
145 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
146 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
147 | assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
148 | assert(c.max_load_factor() == 1); |
149 | } |
150 | #endif |
151 | |
152 | return 0; |
153 | } |
154 | |