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
32int main(int, char**) {
33 {
34 typedef std::unordered_multimap<int,
35 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 P(1, "one"),
43 P(2, "two"),
44 P(3, "three"),
45 P(4, "four"),
46 P(1, "four"),
47 P(2, "four"),
48 };
49 C c0(a,
50 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 C c = c0;
56 LIBCPP_ASSERT(c.bucket_count() == 7);
57 assert(c.size() == 6);
58 std::multiset<std::string> s;
59 s.insert(x: "one");
60 s.insert(x: "four");
61 CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
62 s.insert(x: "two");
63 s.insert(x: "four");
64 CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
65 s.insert(x: "three");
66 CheckConsecutiveKeys<C::const_iterator>(c.find(3), c.end(), 3, s);
67 s.insert(x: "four");
68 CheckConsecutiveKeys<C::const_iterator>(c.find(4), c.end(), 4, s);
69 assert(c.hash_function() == test_hash<int>(8));
70 assert(c.key_eq() == test_equal_to<int>(9));
71 assert(c.get_allocator() == (test_allocator<std::pair<const int, std::string> >(10)));
72 assert(!c.empty());
73 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
74 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
75 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
76 assert(c.max_load_factor() == 1);
77 }
78#if TEST_STD_VER >= 11
79 {
80 typedef std::unordered_multimap<int,
81 std::string,
82 test_hash<int>,
83 test_equal_to<int>,
84 other_allocator<std::pair<const int, std::string> > >
85 C;
86 typedef std::pair<int, std::string> P;
87 P a[] = {
88 P(1, "one"),
89 P(2, "two"),
90 P(3, "three"),
91 P(4, "four"),
92 P(1, "four"),
93 P(2, "four"),
94 };
95 C c0(a,
96 a + sizeof(a) / sizeof(a[0]),
97 7,
98 test_hash<int>(8),
99 test_equal_to<int>(9),
100 other_allocator<std::pair<const int, std::string> >(10));
101 C c = c0;
102 LIBCPP_ASSERT(c.bucket_count() == 7);
103 assert(c.size() == 6);
104 std::multiset<std::string> s;
105 s.insert("one");
106 s.insert("four");
107 CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
108 s.insert("two");
109 s.insert("four");
110 CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
111 s.insert("three");
112 CheckConsecutiveKeys<C::const_iterator>(c.find(3), c.end(), 3, s);
113 s.insert("four");
114 CheckConsecutiveKeys<C::const_iterator>(c.find(4), c.end(), 4, s);
115 assert(c.hash_function() == test_hash<int>(8));
116 assert(c.key_eq() == test_equal_to<int>(9));
117 assert(c.get_allocator() == (other_allocator<std::pair<const int, std::string> >(-2)));
118 assert(!c.empty());
119 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
120 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
121 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
122 assert(c.max_load_factor() == 1);
123 }
124 {
125 typedef std::unordered_multimap<int,
126 std::string,
127 test_hash<int>,
128 test_equal_to<int>,
129 min_allocator<std::pair<const int, std::string> > >
130 C;
131 typedef std::pair<int, std::string> P;
132 P a[] = {
133 P(1, "one"),
134 P(2, "two"),
135 P(3, "three"),
136 P(4, "four"),
137 P(1, "four"),
138 P(2, "four"),
139 };
140 C c0(a,
141 a + sizeof(a) / sizeof(a[0]),
142 7,
143 test_hash<int>(8),
144 test_equal_to<int>(9),
145 min_allocator<std::pair<const int, std::string> >());
146 C c = c0;
147 LIBCPP_ASSERT(c.bucket_count() == 7);
148 assert(c.size() == 6);
149 std::multiset<std::string> s;
150 s.insert("one");
151 s.insert("four");
152 CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
153 s.insert("two");
154 s.insert("four");
155 CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
156 s.insert("three");
157 CheckConsecutiveKeys<C::const_iterator>(c.find(3), c.end(), 3, s);
158 s.insert("four");
159 CheckConsecutiveKeys<C::const_iterator>(c.find(4), c.end(), 4, s);
160 assert(c.hash_function() == test_hash<int>(8));
161 assert(c.key_eq() == test_equal_to<int>(9));
162 assert(c.get_allocator() == (min_allocator<std::pair<const int, std::string> >()));
163 assert(!c.empty());
164 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
165 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
166 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
167 assert(c.max_load_factor() == 1);
168 }
169#endif
170
171 return 0;
172}
173

source code of libcxx/test/std/containers/unord/unord.multimap/unord.multimap.cnstr/copy.pass.cpp