xref: /llvm-project/libcxx/test/std/containers/unord/unord.multiset/unord.multiset.cnstr/copy.pass.cpp (revision bbd717b9a3b22c6cba1535018dae2ebcee95fc9b)
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_multiset
14 
15 // unordered_multiset(const unordered_multiset& u);
16 
17 #include <unordered_set>
18 #include <cassert>
19 #include <cfloat>
20 #include <cmath>
21 #include <cstddef>
22 
23 #include "test_macros.h"
24 #include "../../../check_consecutive.h"
25 #include "../../../test_compare.h"
26 #include "../../../test_hash.h"
27 #include "test_allocator.h"
28 #include "min_allocator.h"
29 
main(int,char **)30 int main(int, char**)
31 {
32     {
33         typedef std::unordered_multiset<int,
34                                    test_hash<int>,
35                                    test_equal_to<int>,
36                                    test_allocator<int>
37                                    > C;
38         typedef int P;
39         P a[] =
40         {
41             P(1),
42             P(2),
43             P(3),
44             P(4),
45             P(1),
46             P(2)
47         };
48         C c0(a, a + sizeof(a)/sizeof(a[0]),
49             7,
50             test_hash<int>(8),
51             test_equal_to<int>(9),
52             test_allocator<int>(10)
53            );
54         C c = c0;
55         LIBCPP_ASSERT(c.bucket_count() == 7);
56         assert(c.size() == 6);
57         CheckConsecutiveValues<C::const_iterator>(c.find(1), c.end(), 1, 2);
58         CheckConsecutiveValues<C::const_iterator>(c.find(2), c.end(), 2, 2);
59         CheckConsecutiveValues<C::const_iterator>(c.find(3), c.end(), 3, 1);
60         CheckConsecutiveValues<C::const_iterator>(c.find(4), c.end(), 4, 1);
61         assert(c.hash_function() == test_hash<int>(8));
62         assert(c.key_eq() == test_equal_to<int>(9));
63         assert(c.get_allocator() == test_allocator<int>(10));
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(std::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 std::unordered_multiset<int,
73                                    test_hash<int>,
74                                    test_equal_to<int>,
75                                    other_allocator<int>
76                                    > C;
77         typedef int P;
78         P a[] =
79         {
80             P(1),
81             P(2),
82             P(3),
83             P(4),
84             P(1),
85             P(2)
86         };
87         C c0(a, a + sizeof(a)/sizeof(a[0]),
88             7,
89             test_hash<int>(8),
90             test_equal_to<int>(9),
91             other_allocator<int>(10)
92            );
93         C c = c0;
94         LIBCPP_ASSERT(c.bucket_count() == 7);
95         assert(c.size() == 6);
96         CheckConsecutiveValues<C::const_iterator>(c.find(1), c.end(), 1, 2);
97         CheckConsecutiveValues<C::const_iterator>(c.find(2), c.end(), 2, 2);
98         CheckConsecutiveValues<C::const_iterator>(c.find(3), c.end(), 3, 1);
99         CheckConsecutiveValues<C::const_iterator>(c.find(4), c.end(), 4, 1);
100         assert(c.hash_function() == test_hash<int>(8));
101         assert(c.key_eq() == test_equal_to<int>(9));
102         assert(c.get_allocator() == other_allocator<int>(-2));
103         assert(!c.empty());
104         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
105         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
106         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
107         assert(c.max_load_factor() == 1);
108     }
109     {
110         typedef std::unordered_multiset<int,
111                                    test_hash<int>,
112                                    test_equal_to<int>,
113                                    min_allocator<int>
114                                    > C;
115         typedef int P;
116         P a[] =
117         {
118             P(1),
119             P(2),
120             P(3),
121             P(4),
122             P(1),
123             P(2)
124         };
125         C c0(a, a + sizeof(a)/sizeof(a[0]),
126             7,
127             test_hash<int>(8),
128             test_equal_to<int>(9),
129             min_allocator<int>()
130            );
131         C c = c0;
132         LIBCPP_ASSERT(c.bucket_count() == 7);
133         assert(c.size() == 6);
134         CheckConsecutiveValues<C::const_iterator>(c.find(1), c.end(), 1, 2);
135         CheckConsecutiveValues<C::const_iterator>(c.find(2), c.end(), 2, 2);
136         CheckConsecutiveValues<C::const_iterator>(c.find(3), c.end(), 3, 1);
137         CheckConsecutiveValues<C::const_iterator>(c.find(4), c.end(), 4, 1);
138         assert(c.hash_function() == test_hash<int>(8));
139         assert(c.key_eq() == test_equal_to<int>(9));
140         assert(c.get_allocator() == min_allocator<int>());
141         assert(!c.empty());
142         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
143         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
144         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
145         assert(c.max_load_factor() == 1);
146     }
147 #endif
148 
149   return 0;
150 }
151