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
30int 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, test_allocator<std::pair<const int, std::string> >(5));
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> >(5)));
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 min_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 min_allocator<std::pair<const int, std::string> >()
93 );
94 C c(c0, min_allocator<std::pair<const int, std::string> >());
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 (min_allocator<std::pair<const int, std::string> >()));
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 explicit_allocator<std::pair<const int, std::string>> A;
113 typedef std::unordered_map<int, std::string,
114 test_hash<int>,
115 test_equal_to<int>,
116 A
117 > C;
118 typedef std::pair<int, std::string> P;
119 P a[] =
120 {
121 P(1, "one"),
122 P(2, "two"),
123 P(3, "three"),
124 P(4, "four"),
125 P(1, "four"),
126 P(2, "four"),
127 };
128 C c0(a, a + sizeof(a)/sizeof(a[0]),
129 7,
130 test_hash<int>(8),
131 test_equal_to<int>(9),
132 A{}
133 );
134 C c(c0, A{});
135 LIBCPP_ASSERT(c.bucket_count() == 7);
136 assert(c.size() == 4);
137 assert(c.at(1) == "one");
138 assert(c.at(2) == "two");
139 assert(c.at(3) == "three");
140 assert(c.at(4) == "four");
141 assert(c.hash_function() == test_hash<int>(8));
142 assert(c.key_eq() == test_equal_to<int>(9));
143 assert(c.get_allocator() == A{});
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

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