blob: 26d6b8674e87dec6813dc2f3e562e14d853cbca6 [file] [log] [blame]
Marshall Clowf60c63c2018-12-14 18:49:35 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Marshall Clowf60c63c2018-12-14 18:49:35 +00006//
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);
Louis Dionne6b77ebd2019-10-23 10:40:15 -070014
Marshall Clowf60c63c2018-12-14 18:49:35 +000015
16#include <string>
17#include <optional>
18
19#include "test_macros.h"
20#include "test_allocator.h"
21#include "min_allocator.h"
22
23template <class S, class U>
24void
25test0(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
33template <class S>
34void 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
JF Bastien2df59c52019-02-04 20:31:13 +000070int main(int, char**)
Marshall Clowf60c63c2018-12-14 18:49:35 +000071{
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>>> ();
JF Bastien2df59c52019-02-04 20:31:13 +000075
76 return 0;
Marshall Clowf60c63c2018-12-14 18:49:35 +000077}