Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Louis Dionne | 31cbe0f | 2020-06-01 10:38:23 -0400 | [diff] [blame] | 8 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 9 | |
| 10 | // <string> |
| 11 | |
| 12 | // template <class charT, class traits, class Allocator, class Predicate> |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 13 | // typename basic_string<charT, traits, Allocator>::size_type |
| 14 | // erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 15 | |
| 16 | #include <string> |
| 17 | |
| 18 | #include "test_macros.h" |
| 19 | #include "test_allocator.h" |
| 20 | #include "min_allocator.h" |
| 21 | |
| 22 | template <class S, class Pred> |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 23 | void test0(S s, Pred p, S expected, size_t expected_erased_count) { |
| 24 | ASSERT_SAME_TYPE(typename S::size_type, decltype(std::erase_if(s, p))); |
| 25 | assert(expected_erased_count == std::erase_if(s, p)); |
| 26 | LIBCPP_ASSERT(s.__invariants()); |
| 27 | assert(s == expected); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | template <typename S> |
| 31 | void test() |
| 32 | { |
| 33 | auto isA = [](auto ch) { return ch == 'a';}; |
| 34 | auto isB = [](auto ch) { return ch == 'b';}; |
| 35 | auto isC = [](auto ch) { return ch == 'c';}; |
| 36 | auto isD = [](auto ch) { return ch == 'd';}; |
| 37 | auto True = [](auto) { return true; }; |
| 38 | auto False = [](auto) { return false; }; |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 39 | |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 40 | test0(S(""), isA, S(""), 0); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 41 | |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 42 | test0(S("a"), isA, S(""), 1); |
| 43 | test0(S("a"), isB, S("a"), 0); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 44 | |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 45 | test0(S("ab"), isA, S("b"), 1); |
| 46 | test0(S("ab"), isB, S("a"), 1); |
| 47 | test0(S("ab"), isC, S("ab"), 0); |
| 48 | test0(S("aa"), isA, S(""), 2); |
| 49 | test0(S("aa"), isC, S("aa"), 0); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 50 | |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 51 | test0(S("abc"), isA, S("bc"), 1); |
| 52 | test0(S("abc"), isB, S("ac"), 1); |
| 53 | test0(S("abc"), isC, S("ab"), 1); |
| 54 | test0(S("abc"), isD, S("abc"), 0); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 55 | |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 56 | test0(S("aab"), isA, S("b"), 2); |
| 57 | test0(S("aab"), isB, S("aa"), 1); |
| 58 | test0(S("aab"), isC, S("aab"), 0); |
| 59 | test0(S("abb"), isA, S("bb"), 1); |
| 60 | test0(S("abb"), isB, S("a"), 2); |
| 61 | test0(S("abb"), isC, S("abb"), 0); |
| 62 | test0(S("aaa"), isA, S(""), 3); |
| 63 | test0(S("aaa"), isB, S("aaa"), 0); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 64 | |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 65 | test0(S("aba"), False, S("aba"), 0); |
| 66 | test0(S("aba"), True, S(""), 3); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 67 | } |
| 68 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 69 | int main(int, char**) |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 70 | { |
| 71 | test<std::string>(); |
| 72 | test<std::basic_string<char, std::char_traits<char>, min_allocator<char>>> (); |
| 73 | test<std::basic_string<char, std::char_traits<char>, test_allocator<char>>> (); |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 74 | |
| 75 | return 0; |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 76 | } |