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// UNSUPPORTED: no-exceptions
11
12// <flat_set>
13
14// template<class Key, class Compare, class KeyContainer, class Predicate>
15// typename flat_set<Key, Compare, KeyContainer>::size_type
16// erase_if(flat_set<Key, Compare, KeyContainer>& c, Predicate pred);
17// If any member function in [flat.set.defn] exits via an exception, the invariant is restored.
18// (This is not a member function, but let's respect the invariant anyway.)
19
20#include <algorithm>
21#include <cassert>
22#include <deque>
23#include <flat_set>
24#include <functional>
25#include <utility>
26#include <vector>
27
28#include "../helpers.h"
29#include "test_macros.h"
30
31struct Counter {
32 int c1, c2, throws;
33 void tick() {
34 c1 -= 1;
35 if (c1 == 0) {
36 c1 = c2;
37 throws += 1;
38 throw 42;
39 }
40 }
41};
42Counter g_counter = {.c1: 0, .c2: 0, .throws: 0};
43
44struct ThrowingAssignment {
45 ThrowingAssignment(int i) : i_(i) {}
46 ThrowingAssignment(const ThrowingAssignment&) = default;
47 ThrowingAssignment& operator=(const ThrowingAssignment& rhs) {
48 g_counter.tick();
49 i_ = rhs.i_;
50 g_counter.tick();
51 return *this;
52 }
53 operator int() const { return i_; }
54 int i_;
55};
56
57struct ThrowingComparator {
58 bool operator()(const ThrowingAssignment& a, const ThrowingAssignment& b) const {
59 g_counter.tick();
60 return a.i_ < b.i_;
61 }
62};
63
64struct ErasurePredicate {
65 bool operator()(const auto& x) const { return (3 <= x && x <= 5); }
66};
67
68void test() {
69 const int expected[] = {1, 2, 3, 4, 5, 6, 7, 8};
70 {
71 using M = std::flat_set<ThrowingAssignment, ThrowingComparator>;
72 for (int first_throw = 1; first_throw < 99; ++first_throw) {
73 for (int second_throw = 1; second_throw < 99; ++second_throw) {
74 g_counter = {.c1: 0, .c2: 0, .throws: 0};
75 M m = M({1, 2, 3, 4, 5, 6, 7, 8});
76 try {
77 g_counter = {.c1: first_throw, .c2: second_throw, .throws: 0};
78 auto n = std::erase_if(m, ErasurePredicate());
79 assert(n == 3);
80 // If it didn't throw at all, we're done.
81 g_counter = {.c1: 0, .c2: 0, .throws: 0};
82 assert((m == M{1, 2, 6, 7, 8}));
83 first_throw = 99; // "done"
84 break;
85 } catch (int ex) {
86 assert(ex == 42);
87 check_invariant(m);
88 LIBCPP_ASSERT(m.empty() || std::equal(m.begin(), m.end(), expected, expected + 8));
89 if (g_counter.throws == 1) {
90 // We reached the first throw but not the second throw.
91 break;
92 }
93 }
94 }
95 }
96 }
97
98 {
99 using M = std::flat_set<ThrowingAssignment, ThrowingComparator, std::deque<ThrowingAssignment>>;
100 for (int first_throw = 1; first_throw < 99; ++first_throw) {
101 for (int second_throw = 1; second_throw < 99; ++second_throw) {
102 g_counter = {.c1: 0, .c2: 0, .throws: 0};
103 std::deque<ThrowingAssignment> container = {5, 6, 7, 8};
104 container.insert(p: container.begin(), l: {1, 2, 3, 4});
105 M m = M(std::move(container));
106 try {
107 g_counter = {.c1: first_throw, .c2: second_throw, .throws: 0};
108 auto n = std::erase_if(m, ErasurePredicate());
109 assert(n == 3);
110 // If it didn't throw at all, we're done.
111 g_counter = {.c1: 0, .c2: 0, .throws: 0};
112 assert((m == M{1, 2, 6, 7, 8}));
113 first_throw = 99; // "done"
114 break;
115 } catch (int ex) {
116 assert(ex == 42);
117 check_invariant(m);
118 LIBCPP_ASSERT(m.empty() || std::equal(m.begin(), m.end(), expected, expected + 8));
119 if (g_counter.throws == 1) {
120 // We reached the first throw but not the second throw.
121 break;
122 }
123 }
124 }
125 }
126 }
127}
128
129int main(int, char**) {
130 test();
131
132 return 0;
133}
134

source code of libcxx/test/std/containers/container.adaptors/flat.set/flat.set.erasure/erase_if_exceptions.pass.cpp