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
18 #include <unordered_set>
19 #include <cassert>
20 #include <cfloat>
21 #include <cmath>
22 #include <cstddef>
23
24 #include "test_macros.h"
25 #include "test_iterators.h"
26 #include "../../../test_compare.h"
27 #include "../../../test_hash.h"
28 #include "test_allocator.h"
29 #include "min_allocator.h"
30
main(int,char **)31 int main(int, char**)
32 {
33 {
34 typedef std::unordered_set<int,
35 test_hash<int>,
36 test_equal_to<int>,
37 test_allocator<int>
38 > C;
39 typedef int P;
40 P a[] =
41 {
42 P(1),
43 P(2),
44 P(3),
45 P(4),
46 P(1),
47 P(2)
48 };
49 C c(cpp17_input_iterator<P*>(a), cpp17_input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
50 7
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>());
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 #if TEST_STD_VER >= 11
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 P a[] =
76 {
77 P(1),
78 P(2),
79 P(3),
80 P(4),
81 P(1),
82 P(2)
83 };
84 C c(cpp17_input_iterator<P*>(a), cpp17_input_iterator<P*>(a + sizeof(a)/sizeof(a[0])),
85 7
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>());
94 assert(c.key_eq() == test_equal_to<int>());
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 #endif
103
104 return 0;
105 }
106