1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
10 
11 // <string>
12 
13 // template <class charT, class traits, class Allocator, class U>
14 //   void erase(basic_string<charT, traits, Allocator>& c, const U& value);
15 
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)26 test0(S s,  U val, S expected)
27 {
28     ASSERT_SAME_TYPE(void, decltype(std::erase(s, val)));
29     std::erase(s, val);
30     LIBCPP_ASSERT(s.__invariants());
31     assert(s == expected);
32 }
33 
34 template <class S>
test()35 void test()
36 {
37 
38     test0(S(""), 'a', S(""));
39 
40     test0(S("a"), 'a', S(""));
41     test0(S("a"), 'b', S("a"));
42 
43     test0(S("ab"), 'a', S("b"));
44     test0(S("ab"), 'b', S("a"));
45     test0(S("ab"), 'c', S("ab"));
46     test0(S("aa"), 'a', S(""));
47     test0(S("aa"), 'c', S("aa"));
48 
49     test0(S("abc"), 'a', S("bc"));
50     test0(S("abc"), 'b', S("ac"));
51     test0(S("abc"), 'c', S("ab"));
52     test0(S("abc"), 'd', S("abc"));
53 
54     test0(S("aab"), 'a', S("b"));
55     test0(S("aab"), 'b', S("aa"));
56     test0(S("aab"), 'c', S("aab"));
57     test0(S("abb"), 'a', S("bb"));
58     test0(S("abb"), 'b', S("a"));
59     test0(S("abb"), 'c', S("abb"));
60     test0(S("aaa"), 'a', S(""));
61     test0(S("aaa"), 'b', S("aaa"));
62 
63 //  Test cross-type erasure
64     using opt = std::optional<typename S::value_type>;
65     test0(S("aba"), opt(),    S("aba"));
66     test0(S("aba"), opt('a'), S("b"));
67     test0(S("aba"), opt('b'), S("aa"));
68     test0(S("aba"), opt('c'), S("aba"));
69 }
70 
main()71 int main()
72 {
73     test<std::string>();
74     test<std::basic_string<char, std::char_traits<char>, min_allocator<char>>> ();
75     test<std::basic_string<char, std::char_traits<char>, test_allocator<char>>> ();
76 }
77