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, const key_equal& eql);
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 test_equal_to<int>(9)
52 );
53 LIBCPP_ASSERT(c.bucket_count() == 7);
54 assert(c.size() == 4);
55 assert(c.count(1) == 1);
56 assert(c.count(2) == 1);
57 assert(c.count(3) == 1);
58 assert(c.count(4) == 1);
59 assert(c.hash_function() == test_hash<int>(8));
60 assert(c.key_eq() == test_equal_to<int>(9));
61 assert(c.get_allocator() == test_allocator<int>());
62 assert(!c.empty());
63 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
64 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
65 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
66 assert(c.max_load_factor() == 1);
67 }
68 {
69 typedef std::unordered_set<int,
70 test_hash<int>,
71 test_equal_to<int>,
72 min_allocator<int>
73 > C;
74 typedef int P;
75 C c({
76 P(1),
77 P(2),
78 P(3),
79 P(4),
80 P(1),
81 P(2)
82 },
83 7,
84 test_hash<int>(8),
85 test_equal_to<int>(9)
86 );
87 LIBCPP_ASSERT(c.bucket_count() == 7);
88 assert(c.size() == 4);
89 assert(c.count(1) == 1);
90 assert(c.count(2) == 1);
91 assert(c.count(3) == 1);
92 assert(c.count(4) == 1);
93 assert(c.hash_function() == test_hash<int>(8));
94 assert(c.key_eq() == test_equal_to<int>(9));
95 assert(c.get_allocator() == min_allocator<int>());
96 assert(!c.empty());
97 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
98 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
99 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
100 assert(c.max_load_factor() == 1);
101 }
102
103 return 0;
104 }
105