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_map(sorted_unique_t, key_container_type key_cont, mapped_container_type mapped_cont,
14// const key_compare& comp = key_compare());
15//
16// template<class Alloc>
17// flat_map(sorted_unique_t, const key_container_type& key_cont,
18// const mapped_container_type& mapped_cont, const Alloc& a);
19// template<class Alloc>
20// flat_map(sorted_unique_t, const key_container_type& key_cont,
21// const mapped_container_type& mapped_cont,
22// const key_compare& comp, const Alloc& a);
23
24#include <deque>
25#include <flat_map>
26#include <functional>
27#include <vector>
28
29#include "min_allocator.h"
30#include "MoveOnly.h"
31#include "test_allocator.h"
32#include "test_iterators.h"
33#include "test_macros.h"
34#include "../../../test_compare.h"
35
36int main(int, char**) {
37 {
38 // The constructors in this subclause shall not participate in overload
39 // resolution unless uses_allocator_v<key_container_type, Alloc> is true
40 // and uses_allocator_v<mapped_container_type, Alloc> is true.
41
42 using C = test_less<int>;
43 using A1 = test_allocator<int>;
44 using A2 = other_allocator<int>;
45 using V1 = std::vector<int, A1>;
46 using V2 = std::vector<int, A2>;
47 using M1 = std::flat_map<int, int, C, V1, V1>;
48 using M2 = std::flat_map<int, int, C, V1, V2>;
49 using M3 = std::flat_map<int, int, C, V2, V1>;
50 static_assert(std::is_constructible_v<M1, std::sorted_unique_t, const V1&, const V1&, const A1&>);
51 static_assert(!std::is_constructible_v<M1, std::sorted_unique_t, const V1&, const V1&, const A2&>);
52 static_assert(!std::is_constructible_v<M2, std::sorted_unique_t, const V1&, const V2&, const A2&>);
53 static_assert(!std::is_constructible_v<M3, std::sorted_unique_t, const V2&, const V1&, const A2&>);
54
55 static_assert(std::is_constructible_v<M1, std::sorted_unique_t, const V1&, const V1&, const C&, const A1&>);
56 static_assert(!std::is_constructible_v<M1, std::sorted_unique_t, const V1&, const V1&, const C&, const A2&>);
57 static_assert(!std::is_constructible_v<M2, std::sorted_unique_t, const V1&, const V2&, const C&, const A2&>);
58 static_assert(!std::is_constructible_v<M3, std::sorted_unique_t, const V2&, const V1&, const C&, const A2&>);
59 }
60 {
61 // flat_map(sorted_unique_t, key_container_type , mapped_container_type)
62 using M = std::flat_map<int, char>;
63 std::vector<int> ks = {1, 2, 4, 10};
64 std::vector<char> vs = {4, 3, 2, 1};
65 auto ks2 = ks;
66 auto vs2 = vs;
67
68 auto m = M(std::sorted_unique, ks, vs);
69 assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}}));
70 m = M(std::sorted_unique, std::move(ks), std::move(vs));
71 assert(ks.empty()); // it was moved-from
72 assert(vs.empty()); // it was moved-from
73 assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}}));
74
75 // explicit(false)
76 M m2 = {std::sorted_unique, std::move(ks2), std::move(vs2)};
77 assert(m == m2);
78 }
79 {
80 // flat_map(sorted_unique_t, key_container_type , mapped_container_type)
81 // non-default container, comparator and allocator type
82 using Ks = std::deque<int, min_allocator<int>>;
83 using Vs = std::deque<char, min_allocator<char>>;
84 using M = std::flat_map<int, char, std::greater<int>, Ks, Vs>;
85 Ks ks = {10, 4, 2, 1};
86 Vs vs = {1, 2, 3, 4};
87 auto m = M(std::sorted_unique, ks, vs);
88 assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}}));
89 m = M(std::sorted_unique, std::move(ks), std::move(vs));
90 assert(ks.empty()); // it was moved-from
91 assert(vs.empty()); // it was moved-from
92 assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}}));
93 }
94 {
95 // flat_map(sorted_unique_t, key_container_type , mapped_container_type)
96 // allocator copied into the containers
97 using A = test_allocator<int>;
98 using M = std::flat_map<int, int, std::less<int>, std::vector<int, A>, std::deque<int, A>>;
99 auto ks = std::vector<int, A>({1, 2, 4, 10}, A(4));
100 auto vs = std::deque<int, A>({4, 3, 2, 1}, A(5));
101 auto m = M(std::sorted_unique, std::move(ks), std::move(vs));
102 assert(ks.empty()); // it was moved-from
103 assert(vs.empty()); // it was moved-from
104 assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}}));
105 assert(m.keys().get_allocator() == A(4));
106 assert(m.values().get_allocator() == A(5));
107 }
108 {
109 // flat_map(sorted_unique_t, key_container_type , mapped_container_type, key_compare)
110 using C = test_less<int>;
111 using M = std::flat_map<int, char, C>;
112 std::vector<int> ks = {1, 2, 4, 10};
113 std::vector<char> vs = {4, 3, 2, 1};
114
115 auto m = M(std::sorted_unique, ks, vs, C(4));
116 assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}}));
117 assert(m.key_comp() == C(4));
118
119 // explicit(false)
120 M m2 = {std::sorted_unique, ks, vs, C(4)};
121 assert(m2 == m);
122 assert(m2.key_comp() == C(4));
123 }
124 {
125 // flat_map(sorted_unique_t, key_container_type , mapped_container_type, key_compare, const Allocator&)
126 using C = test_less<int>;
127 using A = test_allocator<int>;
128 using M = std::flat_map<int, int, C, std::vector<int, A>, std::vector<int, A>>;
129 std::vector<int, A> ks = {1, 2, 4, 10};
130 std::vector<int, A> vs = {4, 3, 2, 1};
131 auto m = M(std::sorted_unique, ks, vs, C(4), A(5));
132 assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}}));
133 assert(m.key_comp() == C(4));
134 assert(m.keys().get_allocator() == A(5));
135 assert(m.values().get_allocator() == A(5));
136
137 // explicit(false)
138 M m2 = {ks, vs, C(4), A(5)};
139 assert(m2 == m);
140 assert(m2.key_comp() == C(4));
141 assert(m2.keys().get_allocator() == A(5));
142 assert(m2.values().get_allocator() == A(5));
143 }
144 {
145 // flat_map(sorted_unique_t, key_container_type , mapped_container_type, const Allocator&)
146 using A = test_allocator<int>;
147 using M = std::flat_map<int, int, std::less<int>, std::vector<int, A>, std::deque<int, A>>;
148 auto ks = std::vector<int, A>({1, 2, 4, 10}, A(4));
149 auto vs = std::deque<int, A>({4, 3, 2, 1}, A(5));
150 auto m = M(std::sorted_unique, ks, vs, A(6)); // replaces the allocators
151 assert(!ks.empty()); // it was an lvalue above
152 assert(!vs.empty()); // it was an lvalue above
153 assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}}));
154 assert(m.keys().get_allocator() == A(6));
155 assert(m.values().get_allocator() == A(6));
156
157 // explicit(false)
158 M m2 = {std::sorted_unique, ks, vs, A(6)};
159 assert(m2 == m);
160 assert(m2.keys().get_allocator() == A(6));
161 assert(m2.values().get_allocator() == A(6));
162 }
163
164 return 0;
165}
166

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