1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
10
11 // <string>
12
13 // template <class charT, class traits, class Allocator, class Predicate>
14 // void erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred);
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>
23 void
test0(S s,Pred p,S expected)24 test0(S s, Pred p, S expected)
25 {
26 ASSERT_SAME_TYPE(void, decltype(std::erase_if(s, p)));
27 std::erase_if(s, p);
28 LIBCPP_ASSERT(s.__invariants());
29 assert(s == expected);
30 }
31
32 template <typename S>
test()33 void test()
34 {
35 auto isA = [](auto ch) { return ch == 'a';};
36 auto isB = [](auto ch) { return ch == 'b';};
37 auto isC = [](auto ch) { return ch == 'c';};
38 auto isD = [](auto ch) { return ch == 'd';};
39 auto True = [](auto) { return true; };
40 auto False = [](auto) { return false; };
41
42 test0(S(""), isA, S(""));
43
44 test0(S("a"), isA, S(""));
45 test0(S("a"), isB, S("a"));
46
47 test0(S("ab"), isA, S("b"));
48 test0(S("ab"), isB, S("a"));
49 test0(S("ab"), isC, S("ab"));
50 test0(S("aa"), isA, S(""));
51 test0(S("aa"), isC, S("aa"));
52
53 test0(S("abc"), isA, S("bc"));
54 test0(S("abc"), isB, S("ac"));
55 test0(S("abc"), isC, S("ab"));
56 test0(S("abc"), isD, S("abc"));
57
58 test0(S("aab"), isA, S("b"));
59 test0(S("aab"), isB, S("aa"));
60 test0(S("aab"), isC, S("aab"));
61 test0(S("abb"), isA, S("bb"));
62 test0(S("abb"), isB, S("a"));
63 test0(S("abb"), isC, S("abb"));
64 test0(S("aaa"), isA, S(""));
65 test0(S("aaa"), isB, S("aaa"));
66
67 test0(S("aba"), False, S("aba"));
68 test0(S("aba"), True, S(""));
69 }
70
main()71 int main()
72 {
73 test<std::string>();
74 test<std::basic_string<char, std::char_traits<char>, min_allocator<char>>> ();
75 test<std::basic_string<char, std::char_traits<char>, test_allocator<char>>> ();
76 }
77