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