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