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_multiset<Key, Compare, KeyContainer>::size_type |
16 | // erase_if(flat_multiset<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 | |
31 | struct 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 | }; |
42 | Counter g_counter = {.c1: 0, .c2: 0, .throws: 0}; |
43 | |
44 | struct 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 | |
57 | struct ThrowingComparator { |
58 | bool operator()(const ThrowingAssignment& a, const ThrowingAssignment& b) const { |
59 | g_counter.tick(); |
60 | return a.i_ < b.i_; |
61 | } |
62 | }; |
63 | |
64 | struct ErasurePredicate { |
65 | bool operator()(const auto& x) const { return (3 <= x && x <= 5); } |
66 | }; |
67 | |
68 | void test() { |
69 | { |
70 | using M = std::flat_multiset<ThrowingAssignment, ThrowingComparator>; |
71 | for (int first_throw = 1; first_throw < 99; ++first_throw) { |
72 | for (int second_throw = 1; second_throw < 99; ++second_throw) { |
73 | g_counter = {.c1: 0, .c2: 0, .throws: 0}; |
74 | M m = M({1, 1, 2, 3, 3, 3, 4, 5, 6, 7, 8}); |
75 | try { |
76 | g_counter = {.c1: first_throw, .c2: second_throw, .throws: 0}; |
77 | auto n = std::erase_if(m, ErasurePredicate()); |
78 | assert(n == 5); |
79 | // If it didn't throw at all, we're done. |
80 | g_counter = {.c1: 0, .c2: 0, .throws: 0}; |
81 | assert((m == M{1, 1, 2, 6, 7, 8})); |
82 | first_throw = 99; // "done" |
83 | break; |
84 | } catch (int ex) { |
85 | assert(ex == 42); |
86 | check_invariant(m); |
87 | LIBCPP_ASSERT(m.empty()); |
88 | if (g_counter.throws == 1) { |
89 | // We reached the first throw but not the second throw. |
90 | break; |
91 | } |
92 | } |
93 | } |
94 | } |
95 | } |
96 | |
97 | { |
98 | using M = std::flat_multiset<ThrowingAssignment, ThrowingComparator, std::deque<ThrowingAssignment>>; |
99 | for (int first_throw = 1; first_throw < 99; ++first_throw) { |
100 | for (int second_throw = 1; second_throw < 99; ++second_throw) { |
101 | g_counter = {.c1: 0, .c2: 0, .throws: 0}; |
102 | std::deque<ThrowingAssignment> container = {5, 6, 7, 8}; |
103 | container.insert(p: container.begin(), l: {1, 2, 3, 4}); |
104 | M m = M(std::move(container)); |
105 | try { |
106 | g_counter = {.c1: first_throw, .c2: second_throw, .throws: 0}; |
107 | auto n = std::erase_if(m, ErasurePredicate()); |
108 | assert(n == 3); |
109 | // If it didn't throw at all, we're done. |
110 | g_counter = {.c1: 0, .c2: 0, .throws: 0}; |
111 | assert((m == M{1, 2, 6, 7, 8})); |
112 | first_throw = 99; // "done" |
113 | break; |
114 | } catch (int ex) { |
115 | assert(ex == 42); |
116 | check_invariant(m); |
117 | LIBCPP_ASSERT(m.empty()); |
118 | if (g_counter.throws == 1) { |
119 | // We reached the first throw but not the second throw. |
120 | break; |
121 | } |
122 | } |
123 | } |
124 | } |
125 | } |
126 | } |
127 | |
128 | int main(int, char**) { |
129 | test(); |
130 | |
131 | return 0; |
132 | } |
133 | |