erase_if.pass.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
  9. // <string>
  10. // template <class charT, class traits, class Allocator, class Predicate>
  11. // void erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred);
  12. #include <string>
  13. #include "test_macros.h"
  14. #include "test_allocator.h"
  15. #include "min_allocator.h"
  16. template <class S, class Pred>
  17. void
  18. test0(S s, Pred p, S expected)
  19. {
  20. ASSERT_SAME_TYPE(void, decltype(std::erase_if(s, p)));
  21. std::erase_if(s, p);
  22. LIBCPP_ASSERT(s.__invariants());
  23. assert(s == expected);
  24. }
  25. template <typename S>
  26. void test()
  27. {
  28. auto isA = [](auto ch) { return ch == 'a';};
  29. auto isB = [](auto ch) { return ch == 'b';};
  30. auto isC = [](auto ch) { return ch == 'c';};
  31. auto isD = [](auto ch) { return ch == 'd';};
  32. auto True = [](auto) { return true; };
  33. auto False = [](auto) { return false; };
  34. test0(S(""), isA, S(""));
  35. test0(S("a"), isA, S(""));
  36. test0(S("a"), isB, S("a"));
  37. test0(S("ab"), isA, S("b"));
  38. test0(S("ab"), isB, S("a"));
  39. test0(S("ab"), isC, S("ab"));
  40. test0(S("aa"), isA, S(""));
  41. test0(S("aa"), isC, S("aa"));
  42. test0(S("abc"), isA, S("bc"));
  43. test0(S("abc"), isB, S("ac"));
  44. test0(S("abc"), isC, S("ab"));
  45. test0(S("abc"), isD, S("abc"));
  46. test0(S("aab"), isA, S("b"));
  47. test0(S("aab"), isB, S("aa"));
  48. test0(S("aab"), isC, S("aab"));
  49. test0(S("abb"), isA, S("bb"));
  50. test0(S("abb"), isB, S("a"));
  51. test0(S("abb"), isC, S("abb"));
  52. test0(S("aaa"), isA, S(""));
  53. test0(S("aaa"), isB, S("aaa"));
  54. test0(S("aba"), False, S("aba"));
  55. test0(S("aba"), True, S(""));
  56. }
  57. int main(int, char**)
  58. {
  59. test<std::string>();
  60. test<std::basic_string<char, std::char_traits<char>, min_allocator<char>>> ();
  61. test<std::basic_string<char, std::char_traits<char>, test_allocator<char>>> ();
  62. return 0;
  63. }