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

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