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, const key_equal& eql,
18 // 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_iterators.h"
28 #include "../../../test_compare.h"
29 #include "../../../test_hash.h"
30 #include "test_allocator.h"
31 #include "min_allocator.h"
32
main(int,char **)33 int main(int, char**)
34 {
35 {
36 typedef std::unordered_set<int,
37 test_hash<int>,
38 test_equal_to<int>,
39 test_allocator<int>
40 > C;
41 typedef int P;
42 P a[] =
43 {
44 P(1),
45 P(2),
46 P(3),
47 P(4),
48 P(1),
49 P(2)
50 };
51 C c(cpp17_input_iterator<P*>(a), cpp17_input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
52 7,
53 test_hash<int>(8),
54 test_equal_to<int>(9),
55 test_allocator<int>(10)
56 );
57 LIBCPP_ASSERT(c.bucket_count() == 7);
58 assert(c.size() == 4);
59 assert(c.count(1) == 1);
60 assert(c.count(2) == 1);
61 assert(c.count(3) == 1);
62 assert(c.count(4) == 1);
63 assert(c.hash_function() == test_hash<int>(8));
64 assert(c.key_eq() == test_equal_to<int>(9));
65 assert(c.get_allocator() == test_allocator<int>(10));
66 assert(!c.empty());
67 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
68 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
69 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
70 assert(c.max_load_factor() == 1);
71 }
72 #if TEST_STD_VER >= 11
73 {
74 typedef std::unordered_set<int,
75 test_hash<int>,
76 test_equal_to<int>,
77 min_allocator<int>
78 > C;
79 typedef int P;
80 P a[] =
81 {
82 P(1),
83 P(2),
84 P(3),
85 P(4),
86 P(1),
87 P(2)
88 };
89 C c(cpp17_input_iterator<P*>(a), cpp17_input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
90 7,
91 test_hash<int>(8),
92 test_equal_to<int>(9),
93 min_allocator<int>()
94 );
95 LIBCPP_ASSERT(c.bucket_count() == 7);
96 assert(c.size() == 4);
97 assert(c.count(1) == 1);
98 assert(c.count(2) == 1);
99 assert(c.count(3) == 1);
100 assert(c.count(4) == 1);
101 assert(c.hash_function() == test_hash<int>(8));
102 assert(c.key_eq() == test_equal_to<int>(9));
103 assert(c.get_allocator() == min_allocator<int>());
104 assert(!c.empty());
105 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
106 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
107 assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
108 assert(c.max_load_factor() == 1);
109 }
110 #endif
111
112 return 0;
113 }
114