1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <unordered_map>
11
12 // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
13 // class Alloc = allocator<pair<const Key, T>>>
14 // class unordered_map
15
16 // unordered_map(unordered_map&& u);
17
18 // UNSUPPORTED: c++98, c++03
19
20 #include <unordered_map>
21 #include <string>
22 #include <cassert>
23 #include <cfloat>
24
25 #include "../../../test_compare.h"
26 #include "../../../test_hash.h"
27 #include "test_allocator.h"
28 #include "min_allocator.h"
29
main()30 int main()
31 {
32 {
33 typedef std::unordered_map<int, std::string,
34 test_hash<std::hash<int> >,
35 test_compare<std::equal_to<int> >,
36 test_allocator<std::pair<const int, std::string> >
37 > C;
38 C c0(7,
39 test_hash<std::hash<int> >(8),
40 test_compare<std::equal_to<int> >(9),
41 test_allocator<std::pair<const int, std::string> >(10)
42 );
43 C c = std::move(c0);
44 assert(c.bucket_count() == 7);
45 assert(c.size() == 0);
46 assert(c.hash_function() == test_hash<std::hash<int> >(8));
47 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
48 assert(c.get_allocator() ==
49 (test_allocator<std::pair<const int, std::string> >(10)));
50 assert(c.empty());
51 assert(std::distance(c.begin(), c.end()) == c.size());
52 assert(std::distance(c.cbegin(), c.cend()) == c.size());
53 assert(c.load_factor() == 0);
54 assert(c.max_load_factor() == 1);
55
56 assert(c0.empty());
57 }
58 {
59 typedef std::unordered_map<int, std::string,
60 test_hash<std::hash<int> >,
61 test_compare<std::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 {
67 P(1, "one"),
68 P(2, "two"),
69 P(3, "three"),
70 P(4, "four"),
71 P(1, "four"),
72 P(2, "four"),
73 };
74 C c0(a, a + sizeof(a)/sizeof(a[0]),
75 7,
76 test_hash<std::hash<int> >(8),
77 test_compare<std::equal_to<int> >(9),
78 test_allocator<std::pair<const int, std::string> >(10)
79 );
80 C c = std::move(c0);
81 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<std::hash<int> >(8));
88 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
89 assert(c.get_allocator() ==
90 (test_allocator<std::pair<const int, std::string> >(10)));
91 assert(!c.empty());
92 assert(std::distance(c.begin(), c.end()) == c.size());
93 assert(std::distance(c.cbegin(), c.cend()) == c.size());
94 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
95 assert(c.max_load_factor() == 1);
96
97 assert(c0.empty());
98 }
99 {
100 typedef std::unordered_map<int, std::string,
101 test_hash<std::hash<int> >,
102 test_compare<std::equal_to<int> >,
103 min_allocator<std::pair<const int, std::string> >
104 > C;
105 C c0(7,
106 test_hash<std::hash<int> >(8),
107 test_compare<std::equal_to<int> >(9),
108 min_allocator<std::pair<const int, std::string> >()
109 );
110 C c = std::move(c0);
111 assert(c.bucket_count() == 7);
112 assert(c.size() == 0);
113 assert(c.hash_function() == test_hash<std::hash<int> >(8));
114 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
115 assert(c.get_allocator() ==
116 (min_allocator<std::pair<const int, std::string> >()));
117 assert(c.empty());
118 assert(std::distance(c.begin(), c.end()) == c.size());
119 assert(std::distance(c.cbegin(), c.cend()) == c.size());
120 assert(c.load_factor() == 0);
121 assert(c.max_load_factor() == 1);
122
123 assert(c0.empty());
124 }
125 {
126 typedef std::unordered_map<int, std::string,
127 test_hash<std::hash<int> >,
128 test_compare<std::equal_to<int> >,
129 min_allocator<std::pair<const int, std::string> >
130 > C;
131 typedef std::pair<int, std::string> P;
132 P a[] =
133 {
134 P(1, "one"),
135 P(2, "two"),
136 P(3, "three"),
137 P(4, "four"),
138 P(1, "four"),
139 P(2, "four"),
140 };
141 C c0(a, a + sizeof(a)/sizeof(a[0]),
142 7,
143 test_hash<std::hash<int> >(8),
144 test_compare<std::equal_to<int> >(9),
145 min_allocator<std::pair<const int, std::string> >()
146 );
147 C c = std::move(c0);
148 assert(c.bucket_count() == 7);
149 assert(c.size() == 4);
150 assert(c.at(1) == "one");
151 assert(c.at(2) == "two");
152 assert(c.at(3) == "three");
153 assert(c.at(4) == "four");
154 assert(c.hash_function() == test_hash<std::hash<int> >(8));
155 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
156 assert(c.get_allocator() ==
157 (min_allocator<std::pair<const int, std::string> >()));
158 assert(!c.empty());
159 assert(std::distance(c.begin(), c.end()) == c.size());
160 assert(std::distance(c.cbegin(), c.cend()) == c.size());
161 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
162 assert(c.max_load_factor() == 1);
163
164 assert(c0.empty());
165 }
166 #if _LIBCPP_DEBUG >= 1
167 {
168 std::unordered_map<int, int> s1 = {{1, 1}, {2, 2}, {3, 3}};
169 std::unordered_map<int, int>::iterator i = s1.begin();
170 std::pair<const int, int> k = *i;
171 std::unordered_map<int, int> s2 = std::move(s1);
172 assert(*i == k);
173 s2.erase(i);
174 assert(s2.size() == 2);
175 }
176 #endif
177 }
178