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