]>
Commit | Line | Data |
---|---|---|
b9a2dce8 | 1 | // { dg-do run { target c++20 } } |
f4ab6846 | 2 | // { dg-add-options no_pch } |
188588e4 | 3 | |
a945c346 | 4 | // Copyright (C) 2018-2024 Free Software Foundation, Inc. |
188588e4 ESR |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
9 | // Free Software Foundation; either version 3, or (at your option) | |
10 | // any later version. | |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
17 | // You should have received a copy of the GNU General Public License along | |
18 | // with this library; see the file COPYING3. If not see | |
19 | // <http://www.gnu.org/licenses/>. | |
20 | ||
21 | #include <map> | |
188588e4 | 22 | |
45a8d80f | 23 | #ifndef __cpp_lib_erase_if |
0fe9eaaa | 24 | # error "Feature-test macro for erase_if missing in <map>" |
55b00d14 | 25 | #elif __cpp_lib_erase_if < 202002 |
0fe9eaaa | 26 | # error "Feature-test macro for erase_if has wrong value in <map>" |
45a8d80f JW |
27 | #endif |
28 | ||
f4ab6846 JW |
29 | #include <string> |
30 | #include <testsuite_hooks.h> | |
31 | ||
188588e4 ESR |
32 | auto is_odd_pair = [](const std::pair<const int, std::string>& p) |
33 | { | |
34 | return p.first % 2 != 0; | |
35 | }; | |
36 | ||
37 | void | |
38 | test01() | |
39 | { | |
40 | std::map<int, std::string> m{ { 10, "A" }, { 11, "B" }, | |
41 | { 12, "C" }, { 14, "D" }, | |
42 | { 15, "E" }, { 17, "F" }, | |
43 | { 18, "G" }, { 19, "H" } }; | |
0b44b4b8 | 44 | auto num = std::erase_if(m, is_odd_pair); |
188588e4 ESR |
45 | std::map<int, std::string> t{ { 10, "A" }, { 12, "C" }, |
46 | { 14, "D" }, { 18, "G" } }; | |
47 | VERIFY( m == t ); | |
0b44b4b8 | 48 | VERIFY( num == 4 ); |
188588e4 ESR |
49 | } |
50 | ||
51 | void | |
52 | test02() | |
53 | { | |
54 | std::multimap<int, std::string> mm{ { 20, "S" }, { 21, "T" }, | |
55 | { 22, "U" }, { 22, "V" }, | |
56 | { 23, "W" }, { 23, "X" }, | |
57 | { 24, "Y" }, { 25, "Z" } }; | |
0b44b4b8 | 58 | auto num = std::erase_if(mm, is_odd_pair); |
188588e4 ESR |
59 | std::multimap<int, std::string> t{ { 20, "S" }, { 22, "U" }, |
60 | { 22, "V" }, { 24, "Y" } }; | |
61 | VERIFY( mm == t ); | |
0b44b4b8 | 62 | VERIFY( num == 4 ); |
188588e4 ESR |
63 | } |
64 | ||
f54ceb20 JW |
65 | void |
66 | test_pr107850() | |
67 | { | |
68 | // Predicate only callable as non-const and only accepts non-const argument. | |
69 | struct Pred { bool operator()(std::pair<const int, int>&) { return false; } }; | |
70 | const Pred pred; // erase_if parameter is passed by value, so non-const. | |
71 | std::map<int, int> m; | |
72 | std::erase_if(m, pred); | |
73 | std::multimap<int, int> mm; | |
74 | std::erase_if(mm, pred); | |
75 | } | |
76 | ||
188588e4 ESR |
77 | int |
78 | main() | |
79 | { | |
80 | test01(); | |
81 | test02(); | |
f54ceb20 | 82 | test_pr107850(); |
188588e4 ESR |
83 | |
84 | return 0; | |
85 | } |