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// UNSUPPORTED: c++03
10
11// <unordered_map>
12
13// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
14// class Alloc = allocator<pair<const Key, T>>>
15// class unordered_map
16
17// unordered_map(unordered_map&& u, const allocator_type& a);
18
19#include <unordered_map>
20#include <string>
21#include <cassert>
22#include <cfloat>
23#include <cmath>
24#include <cstddef>
25
26#include "test_macros.h"
27#include "../../../test_compare.h"
28#include "../../../test_hash.h"
29#include "test_allocator.h"
30#include "min_allocator.h"
31
32int main(int, char**) {
33 {
34 typedef std::pair<int, std::string> P;
35 typedef test_allocator<std::pair<const int, std::string>> A;
36 typedef std::unordered_map<int, std::string, test_hash<int>, test_equal_to<int>, A > C;
37 P a[] = {
38 P(1, "one"),
39 P(2, "two"),
40 P(3, "three"),
41 P(4, "four"),
42 P(1, "four"),
43 P(2, "four"),
44 };
45 C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A(10));
46 C c(std::move(c0), A(12));
47 assert(c.bucket_count() >= 5);
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(12));
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(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
60 assert(c.max_load_factor() == 1);
61
62 assert(c0.empty());
63 }
64 {
65 typedef std::pair<int, std::string> P;
66 typedef test_allocator<std::pair<const int, std::string>> A;
67 typedef std::unordered_map<int, std::string, test_hash<int>, test_equal_to<int>, A > C;
68 P a[] = {
69 P(1, "one"),
70 P(2, "two"),
71 P(3, "three"),
72 P(4, "four"),
73 P(1, "four"),
74 P(2, "four"),
75 };
76 C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A(10));
77 C c(std::move(c0), A(10));
78 LIBCPP_ASSERT(c.bucket_count() == 7);
79 assert(c.size() == 4);
80 assert(c.at(1) == "one");
81 assert(c.at(2) == "two");
82 assert(c.at(3) == "three");
83 assert(c.at(4) == "four");
84 assert(c.hash_function() == test_hash<int>(8));
85 assert(c.key_eq() == test_equal_to<int>(9));
86 assert(c.get_allocator() == A(10));
87 assert(!c.empty());
88 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
89 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
90 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
91 assert(c.max_load_factor() == 1);
92
93 assert(c0.empty());
94 }
95 {
96 typedef std::pair<int, std::string> P;
97 typedef min_allocator<std::pair<const int, std::string>> A;
98 typedef std::unordered_map<int, std::string, test_hash<int>, test_equal_to<int>, A > C;
99 P a[] = {
100 P(1, "one"),
101 P(2, "two"),
102 P(3, "three"),
103 P(4, "four"),
104 P(1, "four"),
105 P(2, "four"),
106 };
107 C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A());
108 C c(std::move(c0), A());
109 LIBCPP_ASSERT(c.bucket_count() == 7);
110 assert(c.size() == 4);
111 assert(c.at(1) == "one");
112 assert(c.at(2) == "two");
113 assert(c.at(3) == "three");
114 assert(c.at(4) == "four");
115 assert(c.hash_function() == test_hash<int>(8));
116 assert(c.key_eq() == test_equal_to<int>(9));
117 assert(c.get_allocator() == A());
118 assert(!c.empty());
119 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
120 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
121 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
122 assert(c.max_load_factor() == 1);
123
124 assert(c0.empty());
125 }
126 {
127 typedef std::pair<int, std::string> P;
128 typedef explicit_allocator<std::pair<const int, std::string>> A;
129 typedef std::unordered_map<int, std::string, test_hash<int>, test_equal_to<int>, A > C;
130 P a[] = {
131 P(1, "one"),
132 P(2, "two"),
133 P(3, "three"),
134 P(4, "four"),
135 P(1, "four"),
136 P(2, "four"),
137 };
138 C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A{});
139 C c(std::move(c0), A{});
140 LIBCPP_ASSERT(c.bucket_count() == 7);
141 assert(c.size() == 4);
142 assert(c.at(1) == "one");
143 assert(c.at(2) == "two");
144 assert(c.at(3) == "three");
145 assert(c.at(4) == "four");
146 assert(c.hash_function() == test_hash<int>(8));
147 assert(c.key_eq() == test_equal_to<int>(9));
148 assert(c.get_allocator() == A{});
149 assert(!c.empty());
150 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
151 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
152 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
153 assert(c.max_load_factor() == 1);
154
155 assert(c0.empty());
156 }
157
158 return 0;
159}
160

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