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// iterator insert(const_iterator position, value_type&&);
13
14#include <flat_set>
15#include <cassert>
16#include <deque>
17
18#include "MinSequenceContainer.h"
19#include "MoveOnly.h"
20#include "min_allocator.h"
21#include "../helpers.h"
22#include "test_macros.h"
23
24template <class KeyContainer>
25constexpr void test_one() {
26 using Key = typename KeyContainer::value_type;
27 using M = std::flat_set<Key, std::less<Key>, KeyContainer>;
28 using V = Key;
29 using R = typename M::iterator;
30 M m;
31 std::same_as<R> decltype(auto) r = m.insert(m.end(), V(2));
32 assert(r == m.begin());
33 assert(m.size() == 1);
34 assert(*r == V(2));
35
36 r = m.insert(m.end(), V(1));
37 assert(r == m.begin());
38 assert(m.size() == 2);
39 assert(*r == V(1));
40
41 r = m.insert(m.end(), V(3));
42 assert(r == std::ranges::prev(m.end()));
43 assert(m.size() == 3);
44 assert(*r == V(3));
45
46 r = m.insert(m.end(), V(3));
47 assert(r == std::ranges::prev(m.end()));
48 assert(m.size() == 3);
49 assert(*r == V(3));
50}
51
52constexpr bool test() {
53 test_one<std::vector<int>>();
54 test_one<std::vector<MoveOnly>>();
55#ifndef __cpp_lib_constexpr_deque
56 if (!TEST_IS_CONSTANT_EVALUATED)
57#endif
58 {
59 test_one<std::deque<int>>();
60 test_one<std::deque<MoveOnly>>();
61 }
62 test_one<MinSequenceContainer<int>>();
63 test_one<MinSequenceContainer<MoveOnly>>();
64 test_one<std::vector<int, min_allocator<int>>>();
65 test_one<std::vector<MoveOnly, min_allocator<MoveOnly>>>();
66
67 return true;
68}
69
70void test_exception() {
71 auto insert_func = [](auto& m, auto key_arg) {
72 using FlatSet = std::decay_t<decltype(m)>;
73 using value_type = typename FlatSet::value_type;
74 value_type p(key_arg);
75 m.insert(m.begin(), std::move(p));
76 };
77 test_emplace_exception_guarantee(emplace_function&: insert_func);
78}
79
80int main(int, char**) {
81 test();
82 test_exception();
83#if TEST_STD_VER >= 26
84 static_assert(test());
85#endif
86
87 return 0;
88}
89

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