blob: 6bda4afc99cf27bae556a3765056ee427f082037 [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_set>
11
12// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
13// class Alloc = allocator<Value>>
14// class unordered_set
15
16// unordered_set(unordered_set&& u);
17
18#include <unordered_set>
19#include <cassert>
Howard Hinnantf836d532011-12-02 21:23:14 +000020#include <cfloat>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021
22#include "../../../test_compare.h"
23#include "../../../test_hash.h"
Marshall Clow1b921882013-12-03 00:18:10 +000024#include "test_allocator.h"
Marshall Clow061d0cc2013-11-26 20:58:02 +000025#include "min_allocator.h"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000026
27int main()
28{
Howard Hinnant73d21a42010-09-04 23:28:19 +000029#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000030 {
31 typedef std::unordered_set<int,
32 test_hash<std::hash<int> >,
33 test_compare<std::equal_to<int> >,
34 test_allocator<int>
35 > C;
36 typedef int P;
37 P a[] =
38 {
39 P(1),
40 P(2),
41 P(3),
42 P(4),
43 P(1),
44 P(2)
45 };
46 C c0(7,
47 test_hash<std::hash<int> >(8),
48 test_compare<std::equal_to<int> >(9),
49 test_allocator<int>(10)
50 );
51 C c = std::move(c0);
52 assert(c.bucket_count() == 7);
53 assert(c.size() == 0);
54 assert(c.hash_function() == test_hash<std::hash<int> >(8));
55 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
56 assert(c.get_allocator() == test_allocator<int>(10));
57 assert(c.empty());
58 assert(std::distance(c.begin(), c.end()) == c.size());
59 assert(std::distance(c.cbegin(), c.cend()) == c.size());
60 assert(c.load_factor() == 0);
61 assert(c.max_load_factor() == 1);
62
63 assert(c0.empty());
64 }
65 {
66 typedef std::unordered_set<int,
67 test_hash<std::hash<int> >,
68 test_compare<std::equal_to<int> >,
69 test_allocator<int>
70 > C;
71 typedef int P;
72 P a[] =
73 {
74 P(1),
75 P(2),
76 P(3),
77 P(4),
78 P(1),
79 P(2)
80 };
81 C c0(a, a + sizeof(a)/sizeof(a[0]),
82 7,
83 test_hash<std::hash<int> >(8),
84 test_compare<std::equal_to<int> >(9),
85 test_allocator<int>(10)
86 );
87 C c = std::move(c0);
88 assert(c.bucket_count() == 7);
89 assert(c.size() == 4);
90 assert(c.count(1) == 1);
91 assert(c.count(2) == 1);
92 assert(c.count(3) == 1);
93 assert(c.count(4) == 1);
94 assert(c.hash_function() == test_hash<std::hash<int> >(8));
95 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
96 assert(c.get_allocator() == test_allocator<int>(10));
97 assert(!c.empty());
98 assert(std::distance(c.begin(), c.end()) == c.size());
99 assert(std::distance(c.cbegin(), c.cend()) == c.size());
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700100 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000101 assert(c.max_load_factor() == 1);
102
103 assert(c0.empty());
104 }
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000105#if __cplusplus >= 201103L
106 {
107 typedef std::unordered_set<int,
108 test_hash<std::hash<int> >,
109 test_compare<std::equal_to<int> >,
110 min_allocator<int>
111 > C;
112 typedef int P;
113 P a[] =
114 {
115 P(1),
116 P(2),
117 P(3),
118 P(4),
119 P(1),
120 P(2)
121 };
122 C c0(7,
123 test_hash<std::hash<int> >(8),
124 test_compare<std::equal_to<int> >(9),
125 min_allocator<int>()
126 );
127 C c = std::move(c0);
128 assert(c.bucket_count() == 7);
129 assert(c.size() == 0);
130 assert(c.hash_function() == test_hash<std::hash<int> >(8));
131 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
132 assert(c.get_allocator() == min_allocator<int>());
133 assert(c.empty());
134 assert(std::distance(c.begin(), c.end()) == c.size());
135 assert(std::distance(c.cbegin(), c.cend()) == c.size());
136 assert(c.load_factor() == 0);
137 assert(c.max_load_factor() == 1);
138
139 assert(c0.empty());
140 }
141 {
142 typedef std::unordered_set<int,
143 test_hash<std::hash<int> >,
144 test_compare<std::equal_to<int> >,
145 min_allocator<int>
146 > C;
147 typedef int P;
148 P a[] =
149 {
150 P(1),
151 P(2),
152 P(3),
153 P(4),
154 P(1),
155 P(2)
156 };
157 C c0(a, a + sizeof(a)/sizeof(a[0]),
158 7,
159 test_hash<std::hash<int> >(8),
160 test_compare<std::equal_to<int> >(9),
161 min_allocator<int>()
162 );
163 C c = std::move(c0);
164 assert(c.bucket_count() == 7);
165 assert(c.size() == 4);
166 assert(c.count(1) == 1);
167 assert(c.count(2) == 1);
168 assert(c.count(3) == 1);
169 assert(c.count(4) == 1);
170 assert(c.hash_function() == test_hash<std::hash<int> >(8));
171 assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
172 assert(c.get_allocator() == min_allocator<int>());
173 assert(!c.empty());
174 assert(std::distance(c.begin(), c.end()) == c.size());
175 assert(std::distance(c.cbegin(), c.cend()) == c.size());
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700176 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000177 assert(c.max_load_factor() == 1);
178
179 assert(c0.empty());
180 }
181#endif
Howard Hinnant5e571422013-08-23 20:10:18 +0000182#if _LIBCPP_DEBUG >= 1
Howard Hinnant39213642013-07-23 22:01:58 +0000183 {
184 std::unordered_set<int> s1 = {1, 2, 3};
185 std::unordered_set<int>::iterator i = s1.begin();
186 int k = *i;
187 std::unordered_set<int> s2 = std::move(s1);
188 assert(*i == k);
189 s2.erase(i);
190 assert(s2.size() == 2);
191 }
192#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +0000193#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000194}