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