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_map> |
13 | |
14 | // class flat_multimap |
15 | |
16 | // template<class Key, class T, class Compare, class KeyContainer, class MappedContainer, class Predicate> |
17 | // typename flat_multimap<Key, T, Compare, KeyContainer, MappedContainer>::size_type |
18 | // erase_if(flat_multimap<Key, T, Compare, KeyContainer, MappedContainer>& c, Predicate pred); |
19 | // If any member function in [flat.set.defn] exits via an exception, the invariant is restored. |
20 | // (This is not a member function, but let's respect the invariant anyway.) |
21 | |
22 | #include <algorithm> |
23 | #include <cassert> |
24 | #include <deque> |
25 | #include <flat_map> |
26 | #include <functional> |
27 | #include <utility> |
28 | #include <vector> |
29 | |
30 | #include "../helpers.h" |
31 | #include "test_macros.h" |
32 | |
33 | struct Counter { |
34 | int c1, c2, throws; |
35 | void tick() { |
36 | c1 -= 1; |
37 | if (c1 == 0) { |
38 | c1 = c2; |
39 | throws += 1; |
40 | throw 42; |
41 | } |
42 | } |
43 | }; |
44 | Counter g_counter = {.c1: 0, .c2: 0, .throws: 0}; |
45 | |
46 | struct ThrowingAssignment { |
47 | ThrowingAssignment(int i) : i_(i) {} |
48 | ThrowingAssignment(const ThrowingAssignment&) = default; |
49 | ThrowingAssignment& operator=(const ThrowingAssignment& rhs) { |
50 | g_counter.tick(); |
51 | i_ = rhs.i_; |
52 | g_counter.tick(); |
53 | return *this; |
54 | } |
55 | operator int() const { return i_; } |
56 | int i_; |
57 | }; |
58 | |
59 | struct ThrowingComparator { |
60 | bool operator()(const ThrowingAssignment& a, const ThrowingAssignment& b) const { |
61 | g_counter.tick(); |
62 | return a.i_ < b.i_; |
63 | } |
64 | }; |
65 | |
66 | struct ErasurePredicate { |
67 | bool operator()(const auto& x) const { return (3 <= x.first && x.first <= 5); } |
68 | }; |
69 | |
70 | int main(int, char**) { |
71 | const std::pair<int, int> expected[] = {{1, 1}, {2, 2}, {3, 3}, {3, 3}, {5, 5}, {6, 6}, {7, 7}, {8, 8}}; |
72 | { |
73 | using M = std::flat_multimap<ThrowingAssignment, int, ThrowingComparator>; |
74 | for (int first_throw = 1; first_throw < 99; ++first_throw) { |
75 | for (int second_throw = 1; second_throw < 99; ++second_throw) { |
76 | g_counter = {.c1: 0, .c2: 0, .throws: 0}; |
77 | M m = M({1, 2, 3, 3, 5, 6, 7, 8}, {1, 2, 3, 3, 5, 6, 7, 8}); |
78 | try { |
79 | g_counter = {.c1: first_throw, .c2: second_throw, .throws: 0}; |
80 | auto n = std::erase_if(m, ErasurePredicate()); |
81 | assert(n == 3); |
82 | // If it didn't throw at all, we're done. |
83 | g_counter = {.c1: 0, .c2: 0, .throws: 0}; |
84 | assert((m == M{{1, 1}, {2, 2}, {6, 6}, {7, 7}, {8, 8}})); |
85 | first_throw = 99; // "done" |
86 | break; |
87 | } catch (int ex) { |
88 | assert(ex == 42); |
89 | check_invariant(m); |
90 | LIBCPP_ASSERT(m.empty() || std::equal(m.begin(), m.end(), expected, expected + 8)); |
91 | if (g_counter.throws == 1) { |
92 | // We reached the first throw but not the second throw. |
93 | break; |
94 | } |
95 | } |
96 | } |
97 | } |
98 | } |
99 | { |
100 | using M = std::flat_multimap<int, ThrowingAssignment, ThrowingComparator>; |
101 | for (int first_throw = 1; first_throw < 99; ++first_throw) { |
102 | for (int second_throw = 1; second_throw < 99; ++second_throw) { |
103 | g_counter = {.c1: 0, .c2: 0, .throws: 0}; |
104 | M m = M({1, 2, 3, 3, 5, 6, 7, 8}, {1, 2, 3, 3, 5, 6, 7, 8}); |
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, 1}, {2, 2}, {6, 6}, {7, 7}, {8, 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() || std::equal(m.begin(), m.end(), expected, expected + 8)); |
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 | using M = std:: |
128 | flat_multimap<ThrowingAssignment, int, ThrowingComparator, std::deque<ThrowingAssignment>, std::deque<int>>; |
129 | for (int first_throw = 1; first_throw < 99; ++first_throw) { |
130 | for (int second_throw = 1; second_throw < 99; ++second_throw) { |
131 | g_counter = {.c1: 0, .c2: 0, .throws: 0}; |
132 | std::deque<ThrowingAssignment> container = {5, 6, 7, 8}; |
133 | container.insert(p: container.begin(), l: {1, 2, 3, 3}); |
134 | M m = M(std::move(container), {1, 2, 3, 3, 5, 6, 7, 8}); |
135 | try { |
136 | g_counter = {.c1: first_throw, .c2: second_throw, .throws: 0}; |
137 | auto n = std::erase_if(m, ErasurePredicate()); |
138 | assert(n == 3); |
139 | // If it didn't throw at all, we're done. |
140 | g_counter = {.c1: 0, .c2: 0, .throws: 0}; |
141 | assert((m == M{{1, 1}, {2, 2}, {6, 6}, {7, 7}, {8, 8}})); |
142 | first_throw = 99; // "done" |
143 | break; |
144 | } catch (int ex) { |
145 | assert(ex == 42); |
146 | check_invariant(m); |
147 | LIBCPP_ASSERT(m.empty() || std::equal(m.begin(), m.end(), expected, expected + 8)); |
148 | if (g_counter.throws == 1) { |
149 | // We reached the first throw but not the second throw. |
150 | break; |
151 | } |
152 | } |
153 | } |
154 | } |
155 | } |
156 | return 0; |
157 | } |
158 | |