xref: /llvm-project/libcxx/test/std/containers/unord/unord.multimap/unord.multimap.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_map>
10 
11 // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
12 //           class Alloc = allocator<pair<const Key, T>>>
13 // class unordered_multimap
14 
15 // unordered_multimap(const unordered_multimap& u);
16 
17 #include <unordered_map>
18 #include <string>
19 #include <set>
20 #include <cassert>
21 #include <cfloat>
22 #include <cmath>
23 #include <cstddef>
24 
25 #include "test_macros.h"
26 #include "../../../check_consecutive.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_multimap<int, std::string,
36                                    test_hash<int>,
37                                    test_equal_to<int>,
38                                    test_allocator<std::pair<const int, std::string> >
39                                    > C;
40         typedef std::pair<int, std::string> P;
41         P a[] =
42         {
43             P(1, "one"),
44             P(2, "two"),
45             P(3, "three"),
46             P(4, "four"),
47             P(1, "four"),
48             P(2, "four"),
49         };
50         C c0(a, a + sizeof(a)/sizeof(a[0]),
51             7,
52             test_hash<int>(8),
53             test_equal_to<int>(9),
54             test_allocator<std::pair<const int, std::string> >(10)
55            );
56         C c = c0;
57         LIBCPP_ASSERT(c.bucket_count() == 7);
58         assert(c.size() == 6);
59         std::multiset<std::string> s;
60         s.insert("one");
61         s.insert("four");
62         CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
63         s.insert("two");
64         s.insert("four");
65         CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
66         s.insert("three");
67         CheckConsecutiveKeys<C::const_iterator>(c.find(3), c.end(), 3, s);
68         s.insert("four");
69         CheckConsecutiveKeys<C::const_iterator>(c.find(4), c.end(), 4, s);
70         assert(c.hash_function() == test_hash<int>(8));
71         assert(c.key_eq() == test_equal_to<int>(9));
72         assert(c.get_allocator() ==
73                (test_allocator<std::pair<const int, std::string> >(10)));
74         assert(!c.empty());
75         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
76         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
77         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
78         assert(c.max_load_factor() == 1);
79     }
80 #if TEST_STD_VER >= 11
81     {
82         typedef std::unordered_multimap<int, std::string,
83                                    test_hash<int>,
84                                    test_equal_to<int>,
85                                    other_allocator<std::pair<const int, std::string> >
86                                    > C;
87         typedef std::pair<int, std::string> P;
88         P a[] =
89         {
90             P(1, "one"),
91             P(2, "two"),
92             P(3, "three"),
93             P(4, "four"),
94             P(1, "four"),
95             P(2, "four"),
96         };
97         C c0(a, a + sizeof(a)/sizeof(a[0]),
98             7,
99             test_hash<int>(8),
100             test_equal_to<int>(9),
101             other_allocator<std::pair<const int, std::string> >(10)
102            );
103         C c = c0;
104         LIBCPP_ASSERT(c.bucket_count() == 7);
105         assert(c.size() == 6);
106         std::multiset<std::string> s;
107         s.insert("one");
108         s.insert("four");
109         CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
110         s.insert("two");
111         s.insert("four");
112         CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
113         s.insert("three");
114         CheckConsecutiveKeys<C::const_iterator>(c.find(3), c.end(), 3, s);
115         s.insert("four");
116         CheckConsecutiveKeys<C::const_iterator>(c.find(4), c.end(), 4, s);
117         assert(c.hash_function() == test_hash<int>(8));
118         assert(c.key_eq() == test_equal_to<int>(9));
119         assert(c.get_allocator() ==
120                (other_allocator<std::pair<const int, std::string> >(-2)));
121         assert(!c.empty());
122         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
123         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
124         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
125         assert(c.max_load_factor() == 1);
126     }
127     {
128         typedef std::unordered_multimap<int, std::string,
129                                    test_hash<int>,
130                                    test_equal_to<int>,
131                                    min_allocator<std::pair<const int, std::string> >
132                                    > C;
133         typedef std::pair<int, std::string> P;
134         P a[] =
135         {
136             P(1, "one"),
137             P(2, "two"),
138             P(3, "three"),
139             P(4, "four"),
140             P(1, "four"),
141             P(2, "four"),
142         };
143         C c0(a, a + sizeof(a)/sizeof(a[0]),
144             7,
145             test_hash<int>(8),
146             test_equal_to<int>(9),
147             min_allocator<std::pair<const int, std::string> >()
148            );
149         C c = c0;
150         LIBCPP_ASSERT(c.bucket_count() == 7);
151         assert(c.size() == 6);
152         std::multiset<std::string> s;
153         s.insert("one");
154         s.insert("four");
155         CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
156         s.insert("two");
157         s.insert("four");
158         CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
159         s.insert("three");
160         CheckConsecutiveKeys<C::const_iterator>(c.find(3), c.end(), 3, s);
161         s.insert("four");
162         CheckConsecutiveKeys<C::const_iterator>(c.find(4), c.end(), 4, s);
163         assert(c.hash_function() == test_hash<int>(8));
164         assert(c.key_eq() == test_equal_to<int>(9));
165         assert(c.get_allocator() ==
166                (min_allocator<std::pair<const int, std::string> >()));
167         assert(!c.empty());
168         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
169         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
170         assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
171         assert(c.max_load_factor() == 1);
172     }
173 #endif
174 
175   return 0;
176 }
177