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 <class... Args>
14// pair<iterator, bool> emplace(Args&&... args);
15
16#include <flat_set>
17#include <cassert>
18#include <deque>
19#include <tuple>
20#include <functional>
21#include <vector>
22
23#include "MinSequenceContainer.h"
24#include "../helpers.h"
25#include "test_macros.h"
26#include "../../../Emplaceable.h"
27#include "DefaultOnly.h"
28#include "min_allocator.h"
29
30template <class KeyContainer>
31void test_one() {
32 using Key = typename KeyContainer::value_type;
33 using M = std::flat_set<Key, std::less<Key>, KeyContainer>;
34 using R = std::pair<typename M::iterator, bool>;
35 {
36 // was empty
37 M m;
38 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));
39 assert(r.second);
40 assert(r.first == m.begin());
41 assert(m.size() == 1);
42 assert(*r.first == 2);
43 }
44 {
45 // key does not exist and inserted at the begin
46 M m = {3, 5, 6, 7};
47 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));
48 assert(r.second);
49 assert(r.first == m.begin());
50 assert(m.size() == 5);
51 assert(*r.first == 2);
52 }
53 {
54 // key does not exist and inserted in the middle
55 M m = {0, 1, 3, 4};
56 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));
57 assert(r.second);
58 assert(r.first == m.begin() + 2);
59 assert(m.size() == 5);
60 assert(*r.first == 2);
61 }
62 {
63 // key does not exist and inserted at the end
64 M m = {0, 1};
65 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));
66 assert(r.second);
67 assert(r.first == m.begin() + 2);
68 assert(m.size() == 3);
69 assert(*r.first == 2);
70 }
71 {
72 // key already exists and original at the begin
73 M m = {2, 3, 5, 6};
74 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));
75 assert(!r.second);
76 assert(r.first == m.begin());
77 assert(m.size() == 4);
78 assert(*r.first == 2);
79 }
80 {
81 // key already exists and original in the middle
82 M m = {0, 2, 3, 4};
83 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));
84 assert(!r.second);
85 assert(r.first == m.begin() + 1);
86 assert(m.size() == 4);
87 assert(*r.first == 2);
88 }
89 {
90 // key already exists and original at the end
91 M m = {0, 1, 2};
92 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));
93 assert(!r.second);
94 assert(r.first == m.begin() + 2);
95 assert(m.size() == 3);
96 assert(*r.first == 2);
97 }
98}
99
100template <class KeyContainer>
101void test_emplaceable() {
102 using M = std::flat_set<Emplaceable, std::less<Emplaceable>, KeyContainer>;
103 using R = std::pair<typename M::iterator, bool>;
104
105 M m;
106 ASSERT_SAME_TYPE(decltype(m.emplace()), R);
107 R r = m.emplace(2, 0.0);
108 assert(r.second);
109 assert(r.first == m.begin());
110 assert(m.size() == 1);
111 assert(*m.begin() == Emplaceable(2, 0.0));
112 r = m.emplace(1, 3.5);
113 assert(r.second);
114 assert(r.first == m.begin());
115 assert(m.size() == 2);
116 assert(*m.begin() == Emplaceable(1, 3.5));
117 r = m.emplace(1, 3.5);
118 assert(!r.second);
119 assert(r.first == m.begin());
120 assert(m.size() == 2);
121 assert(*m.begin() == Emplaceable(1, 3.5));
122}
123
124void test() {
125 test_one<std::vector<int>>();
126 test_one<std::deque<int>>();
127 test_one<MinSequenceContainer<int>>();
128 test_one<std::vector<int, min_allocator<int>>>();
129
130 test_emplaceable<std::vector<Emplaceable>>();
131 test_emplaceable<std::deque<Emplaceable>>();
132 test_emplaceable<MinSequenceContainer<Emplaceable>>();
133 test_emplaceable<std::vector<Emplaceable, min_allocator<Emplaceable>>>();
134}
135
136void test_exception() {
137 auto emplace_func = [](auto& m, auto key_arg) { m.emplace(key_arg); };
138 test_emplace_exception_guarantee(emplace_function&: emplace_func);
139}
140
141int main(int, char**) {
142 test();
143 test_exception();
144
145 return 0;
146}
147

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