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

source code of libcxx/test/std/containers/unord/unord.map/unord.map.cnstr/assign_copy.pass.cpp