blob: 1420204c5f768259a10702d5897a1b5cee71b838 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
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
Eric Fiselierfd9bbf52015-07-19 03:16:47 +000018// UNSUPPORTED: c++98, c++03
19
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000020#include <unordered_map>
21#include <string>
22#include <cassert>
Howard Hinnantf836d532011-12-02 21:23:14 +000023#include <cfloat>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000024
25#include "../../../test_compare.h"
26#include "../../../test_hash.h"
Marshall Clow1b921882013-12-03 00:18:10 +000027#include "test_allocator.h"
Marshall Clow061d0cc2013-11-26 20:58:02 +000028#include "min_allocator.h"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000029
30int main()
31{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032 {
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;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000038 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());
Dan Albert1d4a1ed2016-05-25 22:36:09 -070094 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000095 assert(c.max_load_factor() == 1);
96
97 assert(c0.empty());
98 }
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +000099 {
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;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000105 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());
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700161 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000162 assert(c.max_load_factor() == 1);
163
164 assert(c0.empty());
165 }
Howard Hinnant5e571422013-08-23 20:10:18 +0000166#if _LIBCPP_DEBUG >= 1
Howard Hinnant824c1992013-08-02 17:50:49 +0000167 {
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
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000177}