erase.pass.cpp 2.1 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 U>
  11. // void erase(basic_string<charT, traits, Allocator>& c, const U& value);
  12. #include <string>
  13. #include <optional>
  14. #include "test_macros.h"
  15. #include "test_allocator.h"
  16. #include "min_allocator.h"
  17. template <class S, class U>
  18. void
  19. test0(S s, U val, S expected)
  20. {
  21. ASSERT_SAME_TYPE(void, decltype(std::erase(s, val)));
  22. std::erase(s, val);
  23. LIBCPP_ASSERT(s.__invariants());
  24. assert(s == expected);
  25. }
  26. template <class S>
  27. void test()
  28. {
  29. test0(S(""), 'a', S(""));
  30. test0(S("a"), 'a', S(""));
  31. test0(S("a"), 'b', S("a"));
  32. test0(S("ab"), 'a', S("b"));
  33. test0(S("ab"), 'b', S("a"));
  34. test0(S("ab"), 'c', S("ab"));
  35. test0(S("aa"), 'a', S(""));
  36. test0(S("aa"), 'c', S("aa"));
  37. test0(S("abc"), 'a', S("bc"));
  38. test0(S("abc"), 'b', S("ac"));
  39. test0(S("abc"), 'c', S("ab"));
  40. test0(S("abc"), 'd', S("abc"));
  41. test0(S("aab"), 'a', S("b"));
  42. test0(S("aab"), 'b', S("aa"));
  43. test0(S("aab"), 'c', S("aab"));
  44. test0(S("abb"), 'a', S("bb"));
  45. test0(S("abb"), 'b', S("a"));
  46. test0(S("abb"), 'c', S("abb"));
  47. test0(S("aaa"), 'a', S(""));
  48. test0(S("aaa"), 'b', S("aaa"));
  49. // Test cross-type erasure
  50. using opt = std::optional<typename S::value_type>;
  51. test0(S("aba"), opt(), S("aba"));
  52. test0(S("aba"), opt('a'), S("b"));
  53. test0(S("aba"), opt('b'), S("aa"));
  54. test0(S("aba"), opt('c'), S("aba"));
  55. }
  56. int main(int, char**)
  57. {
  58. test<std::string>();
  59. test<std::basic_string<char, std::char_traits<char>, min_allocator<char>>> ();
  60. test<std::basic_string<char, std::char_traits<char>, test_allocator<char>>> ();
  61. return 0;
  62. }