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, size_type n);
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             7
49            );
50         LIBCPP_ASSERT(c.bucket_count() == 7);
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(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
63         assert(c.max_load_factor() == 1);
64     }
65     {
66         typedef std::unordered_set<int,
67                                    test_hash<int>,
68                                    test_equal_to<int>,
69                                    min_allocator<int>
70                                    > C;
71         typedef int P;
72         C c({
73                 P(1),
74                 P(2),
75                 P(3),
76                 P(4),
77                 P(1),
78                 P(2)
79             },
80             7
81            );
82         LIBCPP_ASSERT(c.bucket_count() == 7);
83         assert(c.size() == 4);
84         assert(c.count(1) == 1);
85         assert(c.count(2) == 1);
86         assert(c.count(3) == 1);
87         assert(c.count(4) == 1);
88         assert(c.hash_function() == test_hash<int>());
89         assert(c.key_eq() == test_equal_to<int>());
90         assert(c.get_allocator() == min_allocator<int>());
91         assert(!c.empty());
92         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
93         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
94         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
95         assert(c.max_load_factor() == 1);
96     }
97 
98   return 0;
99 }
100