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);
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_compare.h"
27#include "../../../test_hash.h"
28#include "test_allocator.h"
29#include "min_allocator.h"
30
31int main(int, char**) {
32 {
33 typedef std::unordered_set<int, test_hash<int>, test_equal_to<int>, test_allocator<int> > C;
34 typedef int P;
35 C c = {P(1), P(2), P(3), P(4), P(1), P(2)};
36 assert(c.bucket_count() >= 5);
37 assert(c.size() == 4);
38 assert(c.count(1) == 1);
39 assert(c.count(2) == 1);
40 assert(c.count(3) == 1);
41 assert(c.count(4) == 1);
42 assert(c.hash_function() == test_hash<int>());
43 assert(c.key_eq() == test_equal_to<int>());
44 assert(c.get_allocator() == test_allocator<int>());
45 assert(!c.empty());
46 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
47 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
48 assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
49 assert(c.max_load_factor() == 1);
50 }
51 {
52 typedef std::unordered_set<int, test_hash<int>, test_equal_to<int>, min_allocator<int> > C;
53 typedef int P;
54 C c = {P(1), P(2), P(3), P(4), P(1), P(2)};
55 assert(c.bucket_count() >= 5);
56 assert(c.size() == 4);
57 assert(c.count(1) == 1);
58 assert(c.count(2) == 1);
59 assert(c.count(3) == 1);
60 assert(c.count(4) == 1);
61 assert(c.hash_function() == test_hash<int>());
62 assert(c.key_eq() == test_equal_to<int>());
63 assert(c.get_allocator() == min_allocator<int>());
64 assert(!c.empty());
65 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
66 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
67 assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
68 assert(c.max_load_factor() == 1);
69 }
70#if TEST_STD_VER > 11
71 {
72 typedef int T;
73 typedef test_hash<T> HF;
74 typedef test_equal_to<T> Comp;
75 typedef test_allocator<T> A;
76 typedef std::unordered_set<T, HF, Comp, A> C;
77
78 A a(42);
79 C c({T(1), T(2), T(3), T(4), T(1), T(2)}, 12, a);
80
81 assert(c.bucket_count() >= 12);
82 assert(c.size() == 4);
83 assert(c.count(1) == 1);
84 assert(c.count(2) == 1);
85 assert(c.count(3) == 1);
86 assert(c.count(4) == 1);
87 assert(c.hash_function() == HF());
88 assert(c.key_eq() == Comp());
89 assert(c.get_allocator() == a);
90 assert(!(c.get_allocator() == A()));
91 assert(!c.empty());
92 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
93 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
94 assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
95 assert(c.max_load_factor() == 1);
96 }
97 {
98 typedef int T;
99 typedef test_hash<T> HF;
100 typedef test_equal_to<T> Comp;
101 typedef test_allocator<T> A;
102 typedef std::unordered_set<T, HF, Comp, A> C;
103
104 A a(42);
105 HF hf(43);
106 C c({T(1), T(2), T(3), T(4), T(1), T(2)}, 12, hf, a);
107
108 assert(c.bucket_count() >= 12);
109 assert(c.size() == 4);
110 assert(c.count(1) == 1);
111 assert(c.count(2) == 1);
112 assert(c.count(3) == 1);
113 assert(c.count(4) == 1);
114 assert(c.hash_function() == hf);
115 assert(!(c.hash_function() == HF()));
116 assert(c.key_eq() == Comp());
117 assert(c.get_allocator() == a);
118 assert(!(c.get_allocator() == A()));
119 assert(!c.empty());
120 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
121 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
122 assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
123 assert(c.max_load_factor() == 1);
124 }
125#endif
126
127 return 0;
128}
129

source code of libcxx/test/std/containers/unord/unord.set/unord.set.cnstr/init.pass.cpp