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