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_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 // template <class InputIterator>
17 // unordered_set(InputIterator first, InputIterator last);
18
19 #include <unordered_set>
20 #include <cassert>
21 #include <cfloat>
22 #include <cmath>
23 #include <cstddef>
24
25 #include "test_macros.h"
26 #include "test_iterators.h"
27 #include "../../../test_compare.h"
28 #include "../../../test_hash.h"
29 #include "test_allocator.h"
30 #include "min_allocator.h"
31
main()32 int main()
33 {
34 {
35 typedef std::unordered_set<int,
36 test_hash<std::hash<int> >,
37 test_compare<std::equal_to<int> >,
38 test_allocator<int>
39 > C;
40 typedef int P;
41 P a[] =
42 {
43 P(1),
44 P(2),
45 P(3),
46 P(4),
47 P(1),
48 P(2)
49 };
50 C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])));
51 assert(c.bucket_count() >= 5);
52 assert(c.size() == 4);
53 assert(c.count(1) == 1);
54 assert(c.count(2) == 1);
55 assert(c.count(3) == 1);
56 assert(c.count(4) == 1);
57 assert(c.hash_function() == test_hash<std::hash<int> >());
58 assert(c.key_eq() == test_compare<std::equal_to<int> >());
59 assert(c.get_allocator() == test_allocator<int>());
60 assert(!c.empty());
61 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
62 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
63 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
64 assert(c.max_load_factor() == 1);
65 }
66 #if TEST_STD_VER >= 11
67 {
68 typedef std::unordered_set<int,
69 test_hash<std::hash<int> >,
70 test_compare<std::equal_to<int> >,
71 min_allocator<int>
72 > C;
73 typedef int P;
74 P a[] =
75 {
76 P(1),
77 P(2),
78 P(3),
79 P(4),
80 P(1),
81 P(2)
82 };
83 C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])));
84 assert(c.bucket_count() >= 5);
85 assert(c.size() == 4);
86 assert(c.count(1) == 1);
87 assert(c.count(2) == 1);
88 assert(c.count(3) == 1);
89 assert(c.count(4) == 1);
90 assert(c.hash_function() == test_hash<std::hash<int> >());
91 assert(c.key_eq() == test_compare<std::equal_to<int> >());
92 assert(c.get_allocator() == min_allocator<int>());
93 assert(!c.empty());
94 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
95 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
96 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
97 assert(c.max_load_factor() == 1);
98 }
99 #if TEST_STD_VER > 11
100 {
101 typedef int T;
102 typedef test_hash<std::hash<T>> HF;
103 typedef test_compare<std::equal_to<T>> Comp;
104 typedef test_allocator<T> A;
105 typedef std::unordered_set<T, HF, Comp, A> C;
106 T arr[] =
107 {
108 T(1),
109 T(2),
110 T(3),
111 T(4),
112 T(1),
113 T(2)
114 };
115 A a(42);
116 C c(input_iterator<T*>(arr), input_iterator<T*>(arr + sizeof(arr)/sizeof(arr[0])), 12, a);
117 assert(c.bucket_count() >= 12);
118 assert(c.size() == 4);
119 assert(c.count(1) == 1);
120 assert(c.count(2) == 1);
121 assert(c.count(3) == 1);
122 assert(c.count(4) == 1);
123 assert(c.hash_function() == HF());
124 assert(c.key_eq() == Comp());
125 assert(c.get_allocator() == a);
126 assert(!(c.get_allocator() == A()));
127 assert(!c.empty());
128 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
129 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
130 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
131 assert(c.max_load_factor() == 1);
132 }
133 {
134 typedef int T;
135 typedef test_hash<std::hash<T>> HF;
136 typedef test_compare<std::equal_to<T>> Comp;
137 typedef test_allocator<T> A;
138 typedef std::unordered_set<T, HF, Comp, A> C;
139 T arr[] =
140 {
141 T(1),
142 T(2),
143 T(3),
144 T(4),
145 T(1),
146 T(2)
147 };
148 HF hf(43);
149 A a(42);
150 C c(input_iterator<T*>(arr), input_iterator<T*>(arr + sizeof(arr)/sizeof(arr[0])), 16, hf, a);
151 assert(c.bucket_count() >= 16);
152 assert(c.size() == 4);
153 assert(c.count(1) == 1);
154 assert(c.count(2) == 1);
155 assert(c.count(3) == 1);
156 assert(c.count(4) == 1);
157 assert(c.hash_function() == hf);
158 assert(!(c.hash_function() == HF()));
159 assert(c.key_eq() == Comp());
160 assert(c.get_allocator() == a);
161 assert(!(c.get_allocator() == A()));
162 assert(!c.empty());
163 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
164 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
165 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
166 assert(c.max_load_factor() == 1);
167 }
168
169 #endif
170 #endif
171 }
172