Skip to content

Commit 6fa4b57

Browse files
committed
review
1 parent 96903d9 commit 6fa4b57

File tree

3 files changed

+280
-19
lines changed

3 files changed

+280
-19
lines changed
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
//
2+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
//===----------------------------------------------------------------------===//
7+
8+
// REQUIRES: has-unix-headers
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10+
// UNSUPPORTED: libcpp-hardening-mode=none
11+
// REQUIRES: libcpp-hardening-mode=debug
12+
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
13+
14+
// <flat_set>
15+
16+
// flat_set(container_type , const key_compare& __comp = key_compare())
17+
// flat_set(const container_type& , const _Allocator& )
18+
// flat_set(const container_type& , const key_compare&, const _Allocator& )
19+
// void replace(container_type&& )
20+
//
21+
22+
#include <flat_set>
23+
#include <functional>
24+
#include <initializer_list>
25+
#include <memory>
26+
#include <utility>
27+
#include <vector>
28+
29+
#include "check_assertion.h"
30+
31+
int main(int, char**) {
32+
using M = std::flat_set<int>;
33+
34+
TEST_LIBCPP_ASSERT_FAILURE(([] { M m(std::sorted_unique, {2, 2, 3}); }()),
35+
"Either the key container is not sorted or it contains duplicates");
36+
37+
TEST_LIBCPP_ASSERT_FAILURE(([] { M m(std::sorted_unique, {4, 2, 3}); }()),
38+
"Either the key container is not sorted or it contains duplicates");
39+
40+
TEST_LIBCPP_ASSERT_FAILURE(([] { M m(std::sorted_unique, {2, 2, 3}, std::less<int>{}); }()),
41+
"Either the key container is not sorted or it contains duplicates");
42+
43+
TEST_LIBCPP_ASSERT_FAILURE(([] { M m(std::sorted_unique, {4, 2, 3}, std::less<int>{}); }()),
44+
"Either the key container is not sorted or it contains duplicates");
45+
46+
TEST_LIBCPP_ASSERT_FAILURE(
47+
([] {
48+
const std::vector keys{2, 2, 3};
49+
const std::allocator<int> alloc{};
50+
M m(std::sorted_unique, keys, alloc);
51+
}()),
52+
"Either the key container is not sorted or it contains duplicates");
53+
54+
TEST_LIBCPP_ASSERT_FAILURE(
55+
([] {
56+
const std::vector keys{4, 2, 3};
57+
const std::allocator<int> alloc{};
58+
M m(std::sorted_unique, keys, alloc);
59+
}()),
60+
"Either the key container is not sorted or it contains duplicates");
61+
62+
TEST_LIBCPP_ASSERT_FAILURE(
63+
([] {
64+
const std::vector keys{2, 2, 3};
65+
const std::allocator<int> alloc{};
66+
const std::less<int> comp{};
67+
M m(std::sorted_unique, keys, comp, alloc);
68+
}()),
69+
"Either the key container is not sorted or it contains duplicates");
70+
71+
TEST_LIBCPP_ASSERT_FAILURE(
72+
([] {
73+
const std::vector keys{4, 2, 3};
74+
const std::allocator<int> alloc{};
75+
const std::less<int> comp{};
76+
M m(std::sorted_unique, keys, comp, alloc);
77+
}()),
78+
"Either the key container is not sorted or it contains duplicates");
79+
80+
TEST_LIBCPP_ASSERT_FAILURE(
81+
([] {
82+
const std::vector<int> v{2, 2, 3};
83+
const std::less<int> comp{};
84+
M m(std::sorted_unique, v.begin(), v.end(), comp);
85+
}()),
86+
"Either the key container is not sorted or it contains duplicates");
87+
88+
TEST_LIBCPP_ASSERT_FAILURE(
89+
([] {
90+
const std::vector<int> v{4, 2, 3};
91+
const std::less<int> comp{};
92+
M m(std::sorted_unique, v.begin(), v.end(), comp);
93+
}()),
94+
"Either the key container is not sorted or it contains duplicates");
95+
96+
TEST_LIBCPP_ASSERT_FAILURE(
97+
([] {
98+
const std::vector<int> v{2, 2, 3};
99+
const std::less<int> comp{};
100+
const std::allocator<int> alloc{};
101+
M m(std::sorted_unique, v.begin(), v.end(), comp, alloc);
102+
}()),
103+
"Either the key container is not sorted or it contains duplicates");
104+
105+
TEST_LIBCPP_ASSERT_FAILURE(
106+
([] {
107+
const std::vector<int> v{4, 2, 3};
108+
const std::less<int> comp{};
109+
const std::allocator<int> alloc{};
110+
M m(std::sorted_unique, v.begin(), v.end(), comp, alloc);
111+
}()),
112+
"Either the key container is not sorted or it contains duplicates");
113+
114+
TEST_LIBCPP_ASSERT_FAILURE(
115+
([] {
116+
const std::vector<int> v{2, 2, 3};
117+
const std::allocator<int> alloc{};
118+
M m(std::sorted_unique, v.begin(), v.end(), alloc);
119+
}()),
120+
"Either the key container is not sorted or it contains duplicates");
121+
122+
TEST_LIBCPP_ASSERT_FAILURE(
123+
([] {
124+
const std::vector<int> v{4, 2, 3};
125+
const std::allocator<int> alloc{};
126+
M m(std::sorted_unique, v.begin(), v.end(), alloc);
127+
}()),
128+
"Either the key container is not sorted or it contains duplicates");
129+
130+
TEST_LIBCPP_ASSERT_FAILURE(
131+
([] {
132+
std::initializer_list<int> v{2, 2, 3};
133+
const std::less<int> comp{};
134+
M m(std::sorted_unique, v, comp);
135+
}()),
136+
"Either the key container is not sorted or it contains duplicates");
137+
138+
TEST_LIBCPP_ASSERT_FAILURE(
139+
([] {
140+
std::initializer_list<int> v{4, 2, 3};
141+
const std::less<int> comp{};
142+
M m(std::sorted_unique, v, comp);
143+
}()),
144+
"Either the key container is not sorted or it contains duplicates");
145+
146+
TEST_LIBCPP_ASSERT_FAILURE(
147+
([] {
148+
std::initializer_list<int> v{2, 2, 3};
149+
const std::less<int> comp{};
150+
const std::allocator<int> alloc{};
151+
M m(std::sorted_unique, v, comp, alloc);
152+
}()),
153+
"Either the key container is not sorted or it contains duplicates");
154+
155+
TEST_LIBCPP_ASSERT_FAILURE(
156+
([] {
157+
std::initializer_list<int> v{4, 2, 3};
158+
const std::less<int> comp{};
159+
const std::allocator<int> alloc{};
160+
M m(std::sorted_unique, v, comp, alloc);
161+
}()),
162+
"Either the key container is not sorted or it contains duplicates");
163+
164+
TEST_LIBCPP_ASSERT_FAILURE(
165+
([] {
166+
std::initializer_list<int> v{2, 2, 3};
167+
const std::allocator<int> alloc{};
168+
M m(std::sorted_unique, v, alloc);
169+
}()),
170+
"Either the key container is not sorted or it contains duplicates");
171+
172+
TEST_LIBCPP_ASSERT_FAILURE(
173+
([] {
174+
std::initializer_list<int> v{4, 2, 3};
175+
const std::allocator<int> alloc{};
176+
M m(std::sorted_unique, v, alloc);
177+
}()),
178+
"Either the key container is not sorted or it contains duplicates");
179+
TEST_LIBCPP_ASSERT_FAILURE(
180+
([] {
181+
const std::vector<int> v{2, 2, 3};
182+
M m;
183+
m.insert(std::sorted_unique, v.begin(), v.end());
184+
}()),
185+
"Either the key container is not sorted or it contains duplicates");
186+
187+
TEST_LIBCPP_ASSERT_FAILURE(
188+
([] {
189+
const std::vector<int> v{4, 2, 3};
190+
M m;
191+
m.insert(std::sorted_unique, v.begin(), v.end());
192+
}()),
193+
"Either the key container is not sorted or it contains duplicates");
194+
TEST_LIBCPP_ASSERT_FAILURE(
195+
([] {
196+
std::initializer_list<int> v{2, 2, 3};
197+
M m;
198+
m.insert(std::sorted_unique, v);
199+
}()),
200+
"Either the key container is not sorted or it contains duplicates");
201+
202+
TEST_LIBCPP_ASSERT_FAILURE(
203+
([] {
204+
std::initializer_list<int> v{4, 2, 3};
205+
M m;
206+
m.insert(std::sorted_unique, v);
207+
}()),
208+
"Either the key container is not sorted or it contains duplicates");
209+
210+
TEST_LIBCPP_ASSERT_FAILURE(
211+
([] {
212+
std::vector keys{1, 1, 3};
213+
M m;
214+
m.replace(std::move(keys));
215+
}()),
216+
"Either the key container is not sorted or it contains duplicates");
217+
TEST_LIBCPP_ASSERT_FAILURE(
218+
([] {
219+
std::vector keys{2, 1, 3};
220+
M m;
221+
m.replace(std::move(keys));
222+
}()),
223+
"Either the key container is not sorted or it contains duplicates");
224+
225+
return 0;
226+
}

libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/assign_initializer_list.pass.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ void test_one() {
3030
{
3131
M m = {8, 10};
3232
assert(m.size() == 2);
33-
m = {3, 1, 2, 2, 3, 4, 3, 5, 6, 5};
33+
std::same_as<M&> decltype(auto) r = m = {3, 1, 2, 2, 3, 4, 3, 5, 6, 5};
34+
assert(&r == &m);
3435
int expected[] = {1, 2, 3, 4, 5, 6};
3536
assert(std::ranges::equal(m, expected));
3637
}
3738
{
3839
M m = {10, 8};
3940
assert(m.size() == 2);
40-
m = {3};
41+
std::same_as<M&> decltype(auto) r = m = {3};
42+
assert(&r == &m);
4143
int expected[] = {3};
4244
assert(std::ranges::equal(m, expected));
4345
}

libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/move_assign.pass.cpp

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ struct MoveClears {
5555
auto operator<=>(const MoveClears&) const = default;
5656
};
5757

58+
#if !defined(TEST_HAS_NO_EXCEPTIONS)
59+
struct MoveAssignThrows : std::vector<int> {
60+
using std::vector<int>::vector;
61+
MoveAssignThrows& operator=(MoveAssignThrows&& other) {
62+
push_back(0);
63+
push_back(0);
64+
other.push_back(0);
65+
other.push_back(0);
66+
throw 42;
67+
}
68+
};
69+
#endif // TEST_HAS_NO_EXCEPTIONS
70+
5871
void test_move_assign_clears() {
5972
// Preserves the class invariant for the moved-from flat_set.
6073
{
@@ -101,6 +114,23 @@ void test_move_assign_clears() {
101114
check_invariant(m1);
102115
LIBCPP_ASSERT(m1.empty());
103116
}
117+
#if !defined(TEST_HAS_NO_EXCEPTIONS)
118+
{
119+
using M = std::flat_set<int, std::less<>, MoveAssignThrows>;
120+
M m1 = {1, 2, 3};
121+
M m2 = {1, 2};
122+
try {
123+
m2 = std::move(m1);
124+
assert(false);
125+
} catch (int e) {
126+
assert(e == 42);
127+
}
128+
check_invariant(m1);
129+
check_invariant(m2);
130+
LIBCPP_ASSERT(m1.empty());
131+
LIBCPP_ASSERT(m2.empty());
132+
}
133+
#endif // TEST_HAS_NO_EXCEPTIONS
104134
}
105135

106136
struct MoveSensitiveComp {
@@ -161,37 +191,40 @@ void test_move_assign_no_except() {
161191

162192
void test() {
163193
{
164-
using C = test_less<int>;
165-
using A1 = test_allocator<int>;
166-
using M = std::flat_set<int, C, std::vector<int, A1>>;
167-
M mo = M({1, 2, 3}, C(5), A1(7));
168-
M m = M({}, C(3), A1(7));
169-
m = std::move(mo);
194+
using C = test_less<int>;
195+
using A1 = test_allocator<int>;
196+
using M = std::flat_set<int, C, std::vector<int, A1>>;
197+
M mo = M({1, 2, 3}, C(5), A1(7));
198+
M m = M({}, C(3), A1(7));
199+
std::same_as<M&> decltype(auto) r = m = std::move(mo);
200+
assert(&r == &m);
170201
assert((m == M{1, 2, 3}));
171202
assert(m.key_comp() == C(5));
172203
auto ks = std::move(m).extract();
173204
assert(ks.get_allocator() == A1(7));
174205
assert(mo.empty());
175206
}
176207
{
177-
using C = test_less<int>;
178-
using A1 = other_allocator<int>;
179-
using M = std::flat_set<int, C, std::deque<int, A1>>;
180-
M mo = M({4, 5}, C(5), A1(7));
181-
M m = M({1, 2, 3, 4}, C(3), A1(7));
182-
m = std::move(mo);
208+
using C = test_less<int>;
209+
using A1 = other_allocator<int>;
210+
using M = std::flat_set<int, C, std::deque<int, A1>>;
211+
M mo = M({4, 5}, C(5), A1(7));
212+
M m = M({1, 2, 3, 4}, C(3), A1(7));
213+
std::same_as<M&> decltype(auto) r = m = std::move(mo);
214+
assert(&r == &m);
183215
assert((m == M{4, 5}));
184216
assert(m.key_comp() == C(5));
185217
auto ks = std::move(m).extract();
186218
assert(ks.get_allocator() == A1(7));
187219
assert(mo.empty());
188220
}
189221
{
190-
using A = min_allocator<int>;
191-
using M = std::flat_set<int, std::greater<int>, std::vector<int, A>>;
192-
M mo = M({5, 4, 3}, A());
193-
M m = M({4, 3, 2, 1}, A());
194-
m = std::move(mo);
222+
using A = min_allocator<int>;
223+
using M = std::flat_set<int, std::greater<int>, std::vector<int, A>>;
224+
M mo = M({5, 4, 3}, A());
225+
M m = M({4, 3, 2, 1}, A());
226+
std::same_as<M&> decltype(auto) r = m = std::move(mo);
227+
assert(&r == &m);
195228
assert((m == M{5, 4, 3}));
196229
auto ks = std::move(m).extract();
197230
assert(ks.get_allocator() == A());

0 commit comments

Comments
 (0)