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

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