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(unordered_map&& u);
16
17// UNSUPPORTED: c++03
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::unordered_map<int,
35 std::string,
36 test_hash<int>,
37 test_equal_to<int>,
38 test_allocator<std::pair<const int, std::string> > >
39 C;
40 C c0(7, test_hash<int>(8), test_equal_to<int>(9), test_allocator<std::pair<const int, std::string> >(10));
41 C::iterator it0 = c0.begin();
42 C c = std::move(c0);
43 LIBCPP_ASSERT(c.bucket_count() == 7);
44 assert(c.size() == 0);
45 assert(c.hash_function() == test_hash<int>(8));
46 assert(c.key_eq() == test_equal_to<int>(9));
47 assert(c.get_allocator() == (test_allocator<std::pair<const int, std::string> >(10)));
48 assert(c.empty());
49 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
50 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
51 assert(c.load_factor() == 0);
52 assert(c.max_load_factor() == 1);
53 assert(it0 == c.begin()); // Iterators remain valid
54
55 assert(c0.empty());
56 }
57 {
58 typedef std::unordered_map<int,
59 std::string,
60 test_hash<int>,
61 test_equal_to<int>,
62 test_allocator<std::pair<const int, std::string> > >
63 C;
64 typedef std::pair<int, std::string> P;
65 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 c0(a,
74 a + sizeof(a) / sizeof(a[0]),
75 7,
76 test_hash<int>(8),
77 test_equal_to<int>(9),
78 test_allocator<std::pair<const int, std::string> >(10));
79 C::iterator it0 = c0.begin();
80 C c = std::move(c0);
81 LIBCPP_ASSERT(c.bucket_count() == 7);
82 assert(c.size() == 4);
83 assert(c.at(1) == "one");
84 assert(c.at(2) == "two");
85 assert(c.at(3) == "three");
86 assert(c.at(4) == "four");
87 assert(c.hash_function() == test_hash<int>(8));
88 assert(c.key_eq() == test_equal_to<int>(9));
89 assert(c.get_allocator() == (test_allocator<std::pair<const int, std::string> >(10)));
90 assert(!c.empty());
91 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
92 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
93 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
94 assert(c.max_load_factor() == 1);
95 assert(it0 == c.begin()); // Iterators remain valid
96
97 assert(c0.empty());
98 }
99 {
100 typedef std::unordered_map<int,
101 std::string,
102 test_hash<int>,
103 test_equal_to<int>,
104 min_allocator<std::pair<const int, std::string> > >
105 C;
106 C c0(7, test_hash<int>(8), test_equal_to<int>(9), min_allocator<std::pair<const int, std::string> >());
107 C::iterator it0 = c0.begin();
108 C c = std::move(c0);
109 LIBCPP_ASSERT(c.bucket_count() == 7);
110 assert(c.size() == 0);
111 assert(c.hash_function() == test_hash<int>(8));
112 assert(c.key_eq() == test_equal_to<int>(9));
113 assert(c.get_allocator() == (min_allocator<std::pair<const int, std::string> >()));
114 assert(c.empty());
115 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
116 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
117 assert(c.load_factor() == 0);
118 assert(c.max_load_factor() == 1);
119 assert(it0 == c.begin()); // Iterators remain valid
120
121 assert(c0.empty());
122 }
123 {
124 typedef std::unordered_map<int,
125 std::string,
126 test_hash<int>,
127 test_equal_to<int>,
128 min_allocator<std::pair<const int, std::string> > >
129 C;
130 typedef std::pair<int, std::string> P;
131 P a[] = {
132 P(1, "one"),
133 P(2, "two"),
134 P(3, "three"),
135 P(4, "four"),
136 P(1, "four"),
137 P(2, "four"),
138 };
139 C c0(a,
140 a + sizeof(a) / sizeof(a[0]),
141 7,
142 test_hash<int>(8),
143 test_equal_to<int>(9),
144 min_allocator<std::pair<const int, std::string> >());
145 C::iterator it0 = c0.begin();
146 C c = std::move(c0);
147 LIBCPP_ASSERT(c.bucket_count() == 7);
148 assert(c.size() == 4);
149 assert(c.at(1) == "one");
150 assert(c.at(2) == "two");
151 assert(c.at(3) == "three");
152 assert(c.at(4) == "four");
153 assert(c.hash_function() == test_hash<int>(8));
154 assert(c.key_eq() == test_equal_to<int>(9));
155 assert(c.get_allocator() == (min_allocator<std::pair<const int, std::string> >()));
156 assert(!c.empty());
157 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
158 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
159 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
160 assert(c.max_load_factor() == 1);
161 assert(it0 == c.begin()); // Iterators remain valid
162
163 assert(c0.empty());
164 }
165
166 return 0;
167}
168

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