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// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10
11// <flat_map>
12
13// flat_multimap(key_container_type key_cont, mapped_container_type mapped_cont,
14// const key_compare& comp = key_compare());
15// template<class Allocator>
16// flat_multimap(const key_container_type& key_cont, const mapped_container_type& mapped_cont,
17// const Allocator& a);
18// template<class Alloc>
19// flat_multimap(const key_container_type& key_cont, const mapped_container_type& mapped_cont,
20// const key_compare& comp, const Alloc& a);
21
22#include <algorithm>
23#include <deque>
24#include <flat_map>
25#include <functional>
26#include <vector>
27
28#include "min_allocator.h"
29#include "MoveOnly.h"
30#include "test_allocator.h"
31#include "test_iterators.h"
32#include "test_macros.h"
33#include "../../../test_compare.h"
34
35struct P {
36 int first;
37 int second;
38 template <class T, class U>
39 bool operator==(const std::pair<T, U>& rhs) const {
40 return MoveOnly(first) == rhs.first && MoveOnly(second) == rhs.second;
41 }
42};
43
44int main(int, char**) {
45 {
46 // The constructors in this subclause shall not participate in overload
47 // resolution unless uses_allocator_v<key_container_type, Alloc> is true
48 // and uses_allocator_v<mapped_container_type, Alloc> is true.
49
50 using C = test_less<int>;
51 using A1 = test_allocator<int>;
52 using A2 = other_allocator<int>;
53 using V1 = std::vector<int, A1>;
54 using V2 = std::vector<int, A2>;
55 using M1 = std::flat_multimap<int, int, C, V1, V1>;
56 using M2 = std::flat_multimap<int, int, C, V1, V2>;
57 using M3 = std::flat_multimap<int, int, C, V2, V1>;
58 static_assert(std::is_constructible_v<M1, const V1&, const V1&, const A1&>);
59 static_assert(!std::is_constructible_v<M1, const V1&, const V1&, const A2&>);
60 static_assert(!std::is_constructible_v<M2, const V1&, const V2&, const A2&>);
61 static_assert(!std::is_constructible_v<M3, const V2&, const V1&, const A2&>);
62
63 static_assert(std::is_constructible_v<M1, const V1&, const V1&, const C&, const A1&>);
64 static_assert(!std::is_constructible_v<M1, const V1&, const V1&, const C&, const A2&>);
65 static_assert(!std::is_constructible_v<M2, const V1&, const V2&, const C&, const A2&>);
66 static_assert(!std::is_constructible_v<M3, const V2&, const V1&, const C&, const A2&>);
67 }
68 {
69 // flat_multimap(key_container_type , mapped_container_type)
70 using M = std::flat_multimap<int, char>;
71 std::vector<int> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};
72 std::vector<char> vs = {1, 2, 3, 4, 5, 6, 7, 8, 9};
73 auto m = M(ks, vs);
74 std::pair<int, char> expected[] = {{1, 1}, {1, 2}, {1, 3}, {2, 4}, {2, 5}, {2, 7}, {3, 6}, {3, 8}, {3, 9}};
75 assert(std::ranges::equal(m, expected));
76
77 // explicit(false)
78 M m2 = {ks, vs};
79 assert(m2 == m);
80
81 m = M(std::move(ks), std::move(vs));
82 assert(ks.empty()); // it was moved-from
83 assert(vs.empty()); // it was moved-from
84 assert(std::ranges::equal(m, expected));
85 }
86 {
87 // flat_multimap(key_container_type , mapped_container_type)
88 // move-only
89 P expected[] = {{.first: 3, .second: 3}, {.first: 3, .second: 2}, {.first: 2, .second: 1}, {.first: 1, .second: 4}};
90 using Ks = std::deque<int, min_allocator<int>>;
91 using Vs = std::vector<MoveOnly, min_allocator<MoveOnly>>;
92 using M = std::flat_multimap<int, MoveOnly, std::greater<int>, Ks, Vs>;
93 Ks ks = {1, 3, 3, 2};
94 Vs vs;
95 vs.push_back(4);
96 vs.push_back(3);
97 vs.push_back(2);
98 vs.push_back(1);
99 auto m = M(std::move(ks), std::move(vs));
100 assert(ks.empty()); // it was moved-from
101 assert(vs.empty()); // it was moved-from
102 assert(std::ranges::equal(m, expected, std::equal_to<>()));
103 }
104 {
105 // flat_multimap(key_container_type , mapped_container_type)
106 // container's allocators are used
107 using A = test_allocator<int>;
108 using M = std::flat_multimap<int, int, std::less<int>, std::vector<int, A>, std::deque<int, A>>;
109 auto ks = std::vector<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5));
110 auto vs = std::deque<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(6));
111 auto m = M(std::move(ks), std::move(vs));
112 assert(ks.empty()); // it was moved-from
113 assert(vs.empty()); // it was moved-from
114 std::pair<int, int> expected[] = {{1, 1}, {1, 1}, {1, 1}, {2, 2}, {2, 2}, {2, 2}, {3, 3}, {3, 3}, {3, 3}};
115 assert(std::ranges::equal(m, expected));
116 assert(m.keys().get_allocator() == A(5));
117 assert(m.values().get_allocator() == A(6));
118 }
119 {
120 // flat_multimap(key_container_type , mapped_container_type, key_compare)
121 using C = test_less<int>;
122 using M = std::flat_multimap<int, char, C>;
123 std::vector<int> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};
124 std::vector<char> vs = {1, 2, 3, 4, 5, 6, 7, 8, 9};
125 auto m = M(ks, vs, C(4));
126 std::pair<int, char> expected[] = {{1, 1}, {1, 2}, {1, 3}, {2, 4}, {2, 5}, {2, 7}, {3, 6}, {3, 8}, {3, 9}};
127 assert(std::ranges::equal(m, expected));
128 assert(m.key_comp() == C(4));
129
130 // explicit(false)
131 M m2 = {ks, vs, C(4)};
132 assert(m2 == m);
133 assert(m2.key_comp() == C(4));
134 }
135 {
136 // flat_multimap(key_container_type , mapped_container_type, const Allocator&)
137 using A = test_allocator<int>;
138 using M = std::flat_multimap<int, int, std::less<int>, std::vector<int, A>, std::deque<int, A>>;
139 auto ks = std::vector<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5));
140 auto vs = std::deque<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(6));
141 auto m = M(ks, vs, A(4)); // replaces the allocators
142 assert(!ks.empty()); // it was an lvalue above
143 assert(!vs.empty()); // it was an lvalue above
144 std::pair<int, int> expected[] = {{1, 1}, {1, 1}, {1, 1}, {2, 2}, {2, 2}, {2, 2}, {3, 3}, {3, 3}, {3, 3}};
145 assert(std::ranges::equal(m, expected));
146 assert(m.keys().get_allocator() == A(4));
147 assert(m.values().get_allocator() == A(4));
148 }
149 {
150 // flat_multimap(key_container_type , mapped_container_type, const Allocator&)
151 // explicit(false)
152 using A = test_allocator<int>;
153 using M = std::flat_multimap<int, int, std::less<int>, std::vector<int, A>, std::deque<int, A>>;
154 auto ks = std::vector<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5));
155 auto vs = std::deque<int, A>({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(6));
156 M m = {ks, vs, A(4)}; // implicit ctor
157 assert(!ks.empty()); // it was an lvalue above
158 assert(!vs.empty()); // it was an lvalue above
159 std::pair<int, int> expected[] = {{1, 1}, {1, 1}, {1, 1}, {2, 2}, {2, 2}, {2, 2}, {3, 3}, {3, 3}, {3, 3}};
160 assert(std::ranges::equal(m, expected));
161 assert(m.keys().get_allocator() == A(4));
162 assert(m.values().get_allocator() == A(4));
163 }
164 {
165 // flat_multimap(key_container_type , mapped_container_type, key_compare, const Allocator&)
166 using C = test_less<int>;
167 using A = test_allocator<int>;
168 using M = std::flat_multimap<int, int, C, std::vector<int, A>, std::vector<int, A>>;
169 std::vector<int, A> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};
170 std::vector<int, A> vs = {1, 2, 3, 4, 5, 6, 7, 8, 9};
171 auto m = M(ks, vs, C(4), A(5));
172 std::pair<int, char> expected[] = {{1, 1}, {1, 2}, {1, 3}, {2, 4}, {2, 5}, {2, 7}, {3, 6}, {3, 8}, {3, 9}};
173 assert(std::ranges::equal(m, expected));
174 assert(m.key_comp() == C(4));
175 assert(m.keys().get_allocator() == A(5));
176 assert(m.values().get_allocator() == A(5));
177
178 // explicit(false)
179 M m2 = {ks, vs, C(4), A(5)};
180 assert(m2 == m);
181 assert(m2.key_comp() == C(4));
182 assert(m2.keys().get_allocator() == A(5));
183 assert(m2.values().get_allocator() == A(5));
184 }
185
186 return 0;
187}
188

source code of libcxx/test/std/containers/container.adaptors/flat.multimap/flat.multimap.cons/containers.pass.cpp