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_set>
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_set>
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 Set = std::flat_set<int, double>;
35
36static_assert(CanInsertRange<Set, std::ranges::subrange<int*>>);
37static_assert(CanInsertRange<Set, std::ranges::subrange<short*>>);
38static_assert(!CanInsertRange<Set, std::ranges::subrange<std::pair<int, int>*>>);
39static_assert(!CanInsertRange<Set, std::ranges::subrange<std::pair<short, short>*>>);
40
41template <class KeyContainer>
42void test_one() {
43 using Key = typename KeyContainer::value_type;
44
45 {
46 using M = std::flat_set<Key, std::less<Key>, KeyContainer>;
47 using It = forward_iterator<const int*>;
48 M m = {10, 8, 5, 2, 1};
49 int ar[] = {3, 1, 4, 1, 5, 9};
50 std::ranges::subrange r = {It(ar), It(ar + 6)};
51 static_assert(std::ranges::common_range<decltype(r)>);
52 m.insert_range(r);
53 assert((m == M{1, 2, 3, 4, 5, 8, 9, 10}));
54 }
55 {
56 using M = std::flat_set<Key, std::greater<>, KeyContainer>;
57 using It = cpp20_input_iterator<const int*>;
58 M m = {8, 5, 3, 2};
59 int ar[] = {3, 1, 4, 1, 5, 9};
60 std::ranges::subrange r = {It(ar), sentinel_wrapper<It>(It(ar + 6))};
61 static_assert(!std::ranges::common_range<decltype(r)>);
62 m.insert_range(r);
63 assert((m == M{1, 2, 3, 4, 5, 8, 9}));
64 }
65 {
66 // The "uniquing" part uses the comparator, not operator==.
67 struct ModTen {
68 bool operator()(int a, int b) const { return (a % 10) < (b % 10); }
69 };
70 using M = std::flat_set<Key, ModTen, KeyContainer>;
71 M m = {21, 43, 15, 37};
72 int ar[] = {33, 18, 55, 18, 42};
73 m.insert_range(ar);
74 assert((m == M{21, 42, 43, 15, 37, 18}));
75 }
76 {
77 // was empty
78 using M = std::flat_set<Key, std::less<Key>, KeyContainer>;
79 M m;
80 int ar[] = {3, 1, 4, 1, 5, 9};
81 m.insert_range(ar);
82 assert((m == M{1, 3, 4, 5, 9}));
83 }
84}
85
86void test() {
87 test_one<std::vector<int>>();
88 test_one<std::deque<int>>();
89 test_one<MinSequenceContainer<int>>();
90 test_one<std::vector<int, min_allocator<int>>>();
91 {
92 // Items are forwarded correctly from the input range.
93 MoveOnly a[] = {3, 1, 4, 1, 5};
94 std::flat_set<MoveOnly> m;
95 m.insert_range(a | std::views::as_rvalue);
96 MoveOnly expected[] = {1, 3, 4, 5};
97 assert(std::ranges::equal(m, expected));
98 }
99}
100
101void test_exception() {
102 auto insert_func = [](auto& m, const auto& newValues) { m.insert_range(newValues); };
103 test_insert_range_exception_guarantee(insert_function&: insert_func);
104}
105
106int main(int, char**) {
107 test();
108 test_exception();
109
110 return 0;
111}
112

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