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