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
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, test_allocator<std::pair<const int, std::string> >(5));
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> >(5)));
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 min_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 min_allocator<std::pair<const int, std::string> >());
101 C c(c0, min_allocator<std::pair<const int, std::string> >());
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() == (min_allocator<std::pair<const int, std::string> >()));
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 explicit_allocator<std::pair<const int, std::string>> A;
126 typedef std::unordered_multimap<int, std::string, test_hash<int>, test_equal_to<int>, A > C;
127 typedef std::pair<int, std::string> P;
128 P a[] = {
129 P(1, "one"),
130 P(2, "two"),
131 P(3, "three"),
132 P(4, "four"),
133 P(1, "four"),
134 P(2, "four"),
135 };
136 C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), A{});
137 C c(c0, A{});
138 LIBCPP_ASSERT(c.bucket_count() == 7);
139 assert(c.size() == 6);
140 std::multiset<std::string> s;
141 s.insert("one");
142 s.insert("four");
143 CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
144 s.insert("two");
145 s.insert("four");
146 CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
147 s.insert("three");
148 CheckConsecutiveKeys<C::const_iterator>(c.find(3), c.end(), 3, s);
149 s.insert("four");
150 CheckConsecutiveKeys<C::const_iterator>(c.find(4), c.end(), 4, s);
151 assert(c.hash_function() == test_hash<int>(8));
152 assert(c.key_eq() == test_equal_to<int>(9));
153 assert(c.get_allocator() == A{});
154 assert(!c.empty());
155 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
156 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
157 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
158 assert(c.max_load_factor() == 1);
159 }
160#endif
161
162 return 0;
163}
164

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