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