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<container-compatible-range<value_type> R>
14// void insert_range(R&& rg);
15
16#include <algorithm>
17#include <deque>
18#include <flat_map>
19#include <functional>
20#include <ranges>
21#include <vector>
22
23#include "MinSequenceContainer.h"
24#include "../helpers.h"
25#include "MoveOnly.h"
26#include "test_macros.h"
27#include "test_iterators.h"
28#include "min_allocator.h"
29
30// test constraint container-compatible-range
31template <class M, class R>
32concept CanInsertRange = requires(M m, R&& r) { m.insert_range(std::forward<R>(r)); };
33
34using Map = std::flat_map<int, double>;
35
36static_assert(CanInsertRange<Map, std::ranges::subrange<std::pair<int, double>*>>);
37static_assert(CanInsertRange<Map, std::ranges::subrange<std::pair<short, double>*>>);
38static_assert(!CanInsertRange<Map, std::ranges::subrange<int*>>);
39static_assert(!CanInsertRange<Map, std::ranges::subrange<double*>>);
40
41template <class KeyContainer, class ValueContainer>
42void test() {
43 using Key = typename KeyContainer::value_type;
44 using Value = typename ValueContainer::value_type;
45
46 {
47 using P = std::pair<int, int>;
48 using M = std::flat_map<Key, Value, std::less<Key>, KeyContainer, ValueContainer>;
49 using It = forward_iterator<const P*>;
50 M m = {{10, 1}, {8, 2}, {5, 3}, {2, 4}, {1, 5}};
51 P ar[] = {{3, 1}, {1, 2}, {4, 3}, {1, 4}, {5, 5}, {9, 6}};
52 std::ranges::subrange r = {It(ar), It(ar + 6)};
53 static_assert(std::ranges::common_range<decltype(r)>);
54 m.insert_range(r);
55 assert((m == M{{1, 5}, {2, 4}, {3, 1}, {4, 3}, {5, 3}, {8, 2}, {9, 6}, {10, 1}}));
56 }
57 {
58 using P = std::pair<int, int>;
59 using M = std::flat_map<Key, Value, std::greater<>, KeyContainer, ValueContainer>;
60 using It = cpp20_input_iterator<const P*>;
61 M m = {{8, 1}, {5, 2}, {3, 3}, {2, 4}};
62 P ar[] = {{3, 1}, {1, 2}, {4, 3}, {1, 4}, {5, 5}, {9, 6}};
63 std::ranges::subrange r = {It(ar), sentinel_wrapper<It>(It(ar + 6))};
64 static_assert(!std::ranges::common_range<decltype(r)>);
65 m.insert_range(r);
66 assert((m == M{{1, 2}, {2, 4}, {3, 3}, {4, 3}, {5, 2}, {8, 1}, {9, 6}}));
67 }
68 {
69 // The "uniquing" part uses the comparator, not operator==.
70 struct ModTen {
71 bool operator()(int a, int b) const { return (a % 10) < (b % 10); }
72 };
73 using P = std::pair<int, int>;
74 using M = std::flat_map<Key, Value, ModTen, KeyContainer, ValueContainer>;
75 M m = {{21, 0}, {43, 0}, {15, 0}, {37, 0}};
76 P ar[] = {{33, 1}, {18, 1}, {55, 1}, {18, 1}, {42, 1}};
77 m.insert_range(ar);
78 assert((m == M{{21, 0}, {42, 1}, {43, 0}, {15, 0}, {37, 0}, {18, 1}}));
79 }
80}
81
82int main(int, char**) {
83 test<std::vector<int>, std::vector<int>>();
84 test<std::deque<int>, std::vector<int>>();
85 test<MinSequenceContainer<int>, MinSequenceContainer<int>>();
86 test<std::vector<int, min_allocator<int>>, std::vector<int, min_allocator<int>>>();
87 {
88 // Items are forwarded correctly from the input range (P2767).
89 std::pair<MoveOnly, MoveOnly> a[] = {{3, 3}, {1, 1}, {4, 4}, {1, 1}, {5, 5}};
90 std::flat_map<MoveOnly, MoveOnly> m;
91 m.insert_range(a | std::views::as_rvalue);
92 std::pair<MoveOnly, MoveOnly> expected[] = {{1, 1}, {3, 3}, {4, 4}, {5, 5}};
93 assert(std::ranges::equal(m, expected));
94 }
95 {
96 // The element type of the range doesn't need to be std::pair (P2767).
97 std::pair<int, int> pa[] = {{3, 3}, {1, 1}, {4, 4}, {1, 1}, {5, 5}};
98 std::deque<std::reference_wrapper<std::pair<int, int>>> a(pa, pa + 5);
99 std::flat_map<int, int> m;
100 m.insert_range(a);
101 std::pair<int, int> expected[] = {{1, 1}, {3, 3}, {4, 4}, {5, 5}};
102 assert(std::ranges::equal(m, expected));
103 }
104 {
105 auto insert_func = [](auto& m, const auto& newValues) { m.insert_range(newValues); };
106 test_insert_range_exception_guarantee(insert_function&: insert_func);
107 }
108 return 0;
109}
110

source code of libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_range.pass.cpp