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 // UNSUPPORTED: c++03
10 
11 // <unordered_set>
12 
13 // template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
14 //           class Alloc = allocator<Value>>
15 // class unordered_set
16 
17 // unordered_set(initializer_list<value_type> il);
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_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         C c = {
41                 P(1),
42                 P(2),
43                 P(3),
44                 P(4),
45                 P(1),
46                 P(2)
47             };
48         assert(c.bucket_count() >= 5);
49         assert(c.size() == 4);
50         assert(c.count(1) == 1);
51         assert(c.count(2) == 1);
52         assert(c.count(3) == 1);
53         assert(c.count(4) == 1);
54         assert(c.hash_function() == test_hash<int>());
55         assert(c.key_eq() == test_equal_to<int>());
56         assert(c.get_allocator() == test_allocator<int>());
57         assert(!c.empty());
58         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
59         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
60         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
61         assert(c.max_load_factor() == 1);
62     }
63     {
64         typedef std::unordered_set<int,
65                                    test_hash<int>,
66                                    test_equal_to<int>,
67                                    min_allocator<int>
68                                    > C;
69         typedef int P;
70         C c = {
71                 P(1),
72                 P(2),
73                 P(3),
74                 P(4),
75                 P(1),
76                 P(2)
77             };
78         assert(c.bucket_count() >= 5);
79         assert(c.size() == 4);
80         assert(c.count(1) == 1);
81         assert(c.count(2) == 1);
82         assert(c.count(3) == 1);
83         assert(c.count(4) == 1);
84         assert(c.hash_function() == test_hash<int>());
85         assert(c.key_eq() == test_equal_to<int>());
86         assert(c.get_allocator() == min_allocator<int>());
87         assert(!c.empty());
88         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
89         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
90         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
91         assert(c.max_load_factor() == 1);
92     }
93 #if TEST_STD_VER > 11
94     {
95         typedef int T;
96         typedef test_hash<T> HF;
97         typedef test_equal_to<T> Comp;
98         typedef test_allocator<T> A;
99         typedef std::unordered_set<T, HF, Comp, A> C;
100 
101         A a(42);
102         C c({
103                 T(1),
104                 T(2),
105                 T(3),
106                 T(4),
107                 T(1),
108                 T(2)
109             }, 12, a);
110 
111         assert(c.bucket_count() >= 12);
112         assert(c.size() == 4);
113         assert(c.count(1) == 1);
114         assert(c.count(2) == 1);
115         assert(c.count(3) == 1);
116         assert(c.count(4) == 1);
117         assert(c.hash_function() == HF());
118         assert(c.key_eq() == Comp());
119         assert(c.get_allocator() == a);
120         assert(!(c.get_allocator() == A()));
121         assert(!c.empty());
122         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
123         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
124         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
125         assert(c.max_load_factor() == 1);
126     }
127     {
128         typedef int T;
129         typedef test_hash<T> HF;
130         typedef test_equal_to<T> Comp;
131         typedef test_allocator<T> A;
132         typedef std::unordered_set<T, HF, Comp, A> C;
133 
134         A a(42);
135         HF hf(43);
136         C c({
137                 T(1),
138                 T(2),
139                 T(3),
140                 T(4),
141                 T(1),
142                 T(2)
143             }, 12, hf, a);
144 
145         assert(c.bucket_count() >= 12);
146         assert(c.size() == 4);
147         assert(c.count(1) == 1);
148         assert(c.count(2) == 1);
149         assert(c.count(3) == 1);
150         assert(c.count(4) == 1);
151         assert(c.hash_function() == hf);
152         assert(!(c.hash_function() == HF()));
153         assert(c.key_eq() == Comp());
154         assert(c.get_allocator() == a);
155         assert(!(c.get_allocator() == A()));
156         assert(!c.empty());
157         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
158         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
159         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
160         assert(c.max_load_factor() == 1);
161     }
162 #endif
163 
164   return 0;
165 }
166