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// template <class InputIterator>
14// flat_map(InputIterator first, InputIterator last, const key_compare& comp = key_compare());
15// template<class InputIterator, class Allocator>
16// flat_map(InputIterator first, InputIterator last, const Allocator& a);
17// template<class InputIterator, class Allocator>
18// flat_map(InputIterator first, InputIterator last, const key_compare& comp, const Allocator& a);
19
20#include <algorithm>
21#include <deque>
22#include <flat_map>
23#include <functional>
24#include <vector>
25
26#include "min_allocator.h"
27#include "test_allocator.h"
28#include "test_iterators.h"
29#include "test_macros.h"
30#include "../../../test_compare.h"
31
32int main(int, char**) {
33 {
34 // The constructors in this subclause shall not participate in overload
35 // resolution unless uses_allocator_v<key_container_type, Alloc> is true
36 // and uses_allocator_v<mapped_container_type, Alloc> is true.
37
38 using C = test_less<int>;
39 using A1 = test_allocator<int>;
40 using A2 = other_allocator<int>;
41 using V1 = std::vector<int, A1>;
42 using V2 = std::vector<int, A2>;
43 using M1 = std::flat_map<int, int, C, V1, V1>;
44 using M2 = std::flat_map<int, int, C, V1, V2>;
45 using M3 = std::flat_map<int, int, C, V2, V1>;
46 using Iter1 = typename M1::iterator;
47 using Iter2 = typename M2::iterator;
48 using Iter3 = typename M3::iterator;
49 static_assert(std::is_constructible_v<M1, Iter1, Iter1, const A1&>);
50 static_assert(!std::is_constructible_v<M1, Iter1, Iter1, const A2&>);
51 static_assert(!std::is_constructible_v<M2, Iter2, Iter2, const A2&>);
52 static_assert(!std::is_constructible_v<M3, Iter3, Iter3, const A2&>);
53
54 static_assert(std::is_constructible_v<M1, Iter1, Iter1, const C&, const A1&>);
55 static_assert(!std::is_constructible_v<M1, Iter1, Iter1, const C&, const A2&>);
56 static_assert(!std::is_constructible_v<M2, Iter2, Iter2, const C&, const A2&>);
57 static_assert(!std::is_constructible_v<M3, Iter3, Iter3, const C&, const A2&>);
58 }
59
60 using P = std::pair<int, short>;
61 P ar[] = {{1, 1}, {1, 2}, {1, 3}, {2, 4}, {2, 5}, {3, 6}, {2, 7}, {3, 8}, {3, 9}};
62 P expected[] = {{1, 1}, {2, 4}, {3, 6}};
63 {
64 // flat_map(InputIterator , InputIterator)
65 // cpp17_input_iterator
66 using M = std::flat_map<int, short>;
67 auto m = M(cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(ar + 9));
68 assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>));
69 LIBCPP_ASSERT(std::ranges::equal(m, expected));
70
71 // explicit(false)
72 M m2 = {cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(ar + 9)};
73 assert(m2 == m);
74 }
75 {
76 // flat_map(InputIterator , InputIterator)
77 // greater
78 using M = std::flat_map<int, short, std::greater<int>, std::deque<int, min_allocator<int>>, std::deque<short>>;
79 auto m = M(cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(ar + 9));
80 assert((m.keys() == std::deque<int, min_allocator<int>>{3, 2, 1}));
81 LIBCPP_ASSERT((m.values() == std::deque<short>{6, 4, 1}));
82 }
83 {
84 // flat_map(InputIterator , InputIterator)
85 // Test when the operands are of array type (also contiguous iterator type)
86 using M = std::flat_map<int, short, std::greater<int>, std::vector<int, min_allocator<int>>>;
87 auto m = M(ar, ar);
88 assert(m.empty());
89 }
90 {
91 // flat_map(InputIterator , InputIterator, const key_compare&)
92 using C = test_less<int>;
93 using M = std::flat_map<int, short, C, std::vector<int>, std::deque<short>>;
94 auto m = M(ar, ar + 9, C(3));
95 assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>));
96 LIBCPP_ASSERT(std::ranges::equal(m, expected));
97 assert(m.key_comp() == C(3));
98
99 // explicit(false)
100 M m2 = {ar, ar + 9, C(3)};
101 assert(m2 == m);
102 assert(m2.key_comp() == C(3));
103 }
104 {
105 // flat_map(InputIterator , InputIterator, const Allocator&)
106 using A1 = test_allocator<int>;
107 using A2 = test_allocator<short>;
108 using M = std::flat_map<int, short, std::less<int>, std::vector<int, A1>, std::deque<short, A2>>;
109 auto m = M(ar, ar + 9, A1(5));
110 assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>));
111 LIBCPP_ASSERT(std::ranges::equal(m, expected));
112 assert(m.keys().get_allocator() == A1(5));
113 assert(m.values().get_allocator() == A2(5));
114 }
115 {
116 // flat_map(InputIterator , InputIterator, const Allocator&)
117 // explicit(false)
118 using A1 = test_allocator<int>;
119 using A2 = test_allocator<short>;
120 using M = std::flat_map<int, short, std::less<int>, std::vector<int, A1>, std::deque<short, A2>>;
121 M m = {ar, ar + 9, A1(5)}; // implicit ctor
122 assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>));
123 LIBCPP_ASSERT(std::ranges::equal(m, expected));
124 assert(m.keys().get_allocator() == A1(5));
125 assert(m.values().get_allocator() == A2(5));
126 }
127 {
128 // flat_map(InputIterator , InputIterator, const key_compare&, const Allocator&)
129 using C = test_less<int>;
130 using A1 = test_allocator<int>;
131 using A2 = test_allocator<short>;
132 using M = std::flat_map<int, short, C, std::vector<int, A1>, std::deque<short, A2>>;
133 auto m = M(ar, ar + 9, C(3), A1(5));
134 assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>));
135 LIBCPP_ASSERT(std::ranges::equal(m, expected));
136 assert(m.key_comp() == C(3));
137 assert(m.keys().get_allocator() == A1(5));
138 assert(m.values().get_allocator() == A2(5));
139 }
140 {
141 // flat_map(InputIterator , InputIterator, const key_compare&, const Allocator&)
142 // explicit(false)
143 using A1 = test_allocator<int>;
144 using A2 = test_allocator<short>;
145 using M = std::flat_map<int, short, std::less<int>, std::deque<int, A1>, std::vector<short, A2>>;
146 M m = {ar, ar + 9, {}, A2(5)}; // implicit ctor
147 assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>));
148 LIBCPP_ASSERT(std::ranges::equal(m, expected));
149 assert(m.keys().get_allocator() == A1(5));
150 assert(m.values().get_allocator() == A2(5));
151 }
152
153 return 0;
154}
155

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