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

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