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 //               const hasher& hf);
19 
20 #include <unordered_set>
21 #include <cassert>
22 #include <cfloat>
23 #include <cmath>
24 #include <cstddef>
25 
26 #include "test_macros.h"
27 #include "../../../test_compare.h"
28 #include "../../../test_hash.h"
29 #include "test_allocator.h"
30 #include "min_allocator.h"
31 
main(int,char **)32 int main(int, char**)
33 {
34     {
35         typedef std::unordered_set<int,
36                                    test_hash<int>,
37                                    test_equal_to<int>,
38                                    test_allocator<int>
39                                    > C;
40         typedef int P;
41         C c({
42                 P(1),
43                 P(2),
44                 P(3),
45                 P(4),
46                 P(1),
47                 P(2)
48             },
49             7,
50             test_hash<int>(8)
51            );
52         LIBCPP_ASSERT(c.bucket_count() == 7);
53         assert(c.size() == 4);
54         assert(c.count(1) == 1);
55         assert(c.count(2) == 1);
56         assert(c.count(3) == 1);
57         assert(c.count(4) == 1);
58         assert(c.hash_function() == test_hash<int>(8));
59         assert(c.key_eq() == test_equal_to<int>());
60         assert(c.get_allocator() == test_allocator<int>());
61         assert(!c.empty());
62         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
63         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
64         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
65         assert(c.max_load_factor() == 1);
66     }
67     {
68         typedef std::unordered_set<int,
69                                    test_hash<int>,
70                                    test_equal_to<int>,
71                                    min_allocator<int>
72                                    > C;
73         typedef int P;
74         C c({
75                 P(1),
76                 P(2),
77                 P(3),
78                 P(4),
79                 P(1),
80                 P(2)
81             },
82             7,
83             test_hash<int>(8)
84            );
85         LIBCPP_ASSERT(c.bucket_count() == 7);
86         assert(c.size() == 4);
87         assert(c.count(1) == 1);
88         assert(c.count(2) == 1);
89         assert(c.count(3) == 1);
90         assert(c.count(4) == 1);
91         assert(c.hash_function() == test_hash<int>(8));
92         assert(c.key_eq() == test_equal_to<int>());
93         assert(c.get_allocator() == min_allocator<int>());
94         assert(!c.empty());
95         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
96         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
97         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
98         assert(c.max_load_factor() == 1);
99     }
100 
101   return 0;
102 }
103