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 // <unordered_set>
10
11 // template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
12 // class Alloc = allocator<Value>>
13 // class unordered_set
14
15 // template <class InputIterator>
16 // unordered_set(InputIterator first, InputIterator last, size_type n,
17 // const hasher& hf);
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_iterators.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 P a[] =
42 {
43 P(1),
44 P(2),
45 P(3),
46 P(4),
47 P(1),
48 P(2)
49 };
50 C c(cpp17_input_iterator<P*>(a), cpp17_input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
51 7,
52 test_hash<int>(8)
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>());
62 assert(c.get_allocator() == test_allocator<int>());
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 #if TEST_STD_VER >= 11
70 {
71 typedef std::unordered_set<int,
72 test_hash<int>,
73 test_equal_to<int>,
74 min_allocator<int>
75 > C;
76 typedef int P;
77 P a[] =
78 {
79 P(1),
80 P(2),
81 P(3),
82 P(4),
83 P(1),
84 P(2)
85 };
86 C c(cpp17_input_iterator<P*>(a), cpp17_input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
87 7,
88 test_hash<int>(8)
89 );
90 LIBCPP_ASSERT(c.bucket_count() == 7);
91 assert(c.size() == 4);
92 assert(c.count(1) == 1);
93 assert(c.count(2) == 1);
94 assert(c.count(3) == 1);
95 assert(c.count(4) == 1);
96 assert(c.hash_function() == test_hash<int>(8));
97 assert(c.key_eq() == test_equal_to<int>());
98 assert(c.get_allocator() == min_allocator<int>());
99 assert(!c.empty());
100 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
101 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
102 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
103 assert(c.max_load_factor() == 1);
104 }
105 #endif
106
107 return 0;
108 }
109