xref: /llvm-project/libcxx/test/std/containers/unord/unord.multimap/unord.multimap.cnstr/copy_alloc.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, const allocator_type& a);
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, test_allocator<std::pair<const int, std::string> >(5));
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> >(5)));
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                                    min_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             min_allocator<std::pair<const int, std::string> >()
102            );
103         C c(c0, min_allocator<std::pair<const int, std::string> >());
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                (min_allocator<std::pair<const int, std::string> >()));
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 explicit_allocator<std::pair<const int, std::string>> A;
129         typedef std::unordered_multimap<int, std::string,
130                                    test_hash<int>,
131                                    test_equal_to<int>,
132                                    A
133                                    > C;
134         typedef std::pair<int, std::string> P;
135         P a[] =
136         {
137             P(1, "one"),
138             P(2, "two"),
139             P(3, "three"),
140             P(4, "four"),
141             P(1, "four"),
142             P(2, "four"),
143         };
144         C c0(a, a + sizeof(a)/sizeof(a[0]),
145             7,
146             test_hash<int>(8),
147             test_equal_to<int>(9),
148             A{}
149            );
150         C c(c0, A{});
151         LIBCPP_ASSERT(c.bucket_count() == 7);
152         assert(c.size() == 6);
153         std::multiset<std::string> s;
154         s.insert("one");
155         s.insert("four");
156         CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
157         s.insert("two");
158         s.insert("four");
159         CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
160         s.insert("three");
161         CheckConsecutiveKeys<C::const_iterator>(c.find(3), c.end(), 3, s);
162         s.insert("four");
163         CheckConsecutiveKeys<C::const_iterator>(c.find(4), c.end(), 4, s);
164         assert(c.hash_function() == test_hash<int>(8));
165         assert(c.key_eq() == test_equal_to<int>(9));
166         assert(c.get_allocator() == A{});
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