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
30int 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;
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> >(10)));
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 other_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 other_allocator<std::pair<const int, std::string> >(10));
92 C c = c0;
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() == (other_allocator<std::pair<const int, std::string> >(-2)));
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 std::unordered_map<int,
110 std::string,
111 test_hash<int>,
112 test_equal_to<int>,
113 min_allocator<std::pair<const int, std::string> > >
114 C;
115 typedef std::pair<int, std::string> P;
116 P a[] = {
117 P(1, "one"),
118 P(2, "two"),
119 P(3, "three"),
120 P(4, "four"),
121 P(1, "four"),
122 P(2, "four"),
123 };
124 C c0(a,
125 a + sizeof(a) / sizeof(a[0]),
126 7,
127 test_hash<int>(8),
128 test_equal_to<int>(9),
129 min_allocator<std::pair<const int, std::string> >());
130 C c = c0;
131 LIBCPP_ASSERT(c.bucket_count() == 7);
132 assert(c.size() == 4);
133 assert(c.at(1) == "one");
134 assert(c.at(2) == "two");
135 assert(c.at(3) == "three");
136 assert(c.at(4) == "four");
137 assert(c.hash_function() == test_hash<int>(8));
138 assert(c.key_eq() == test_equal_to<int>(9));
139 assert(c.get_allocator() == (min_allocator<std::pair<const int, std::string> >()));
140 assert(!c.empty());
141 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
142 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
143 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
144 assert(c.max_load_factor() == 1);
145 }
146#endif
147
148 return 0;
149}
150

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