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