blob: b53d2b71bb7eb5ecafe9cece8ea4513e5c60271c [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//===----------------------------------------------------------------------===//
Louis Dionne31cbe0f2020-06-01 10:38:23 -04008// UNSUPPORTED: c++03, c++11, c++14, c++17
Marshall Clowf60c63c2018-12-14 18:49:35 +00009
10// <string>
11
12// template <class charT, class traits, class Allocator, class U>
Marek Kurdej3e895082020-05-02 13:58:03 +020013// typename basic_string<charT, traits, Allocator>::size_type
14// erase(basic_string<charT, traits, Allocator>& c, const U& value);
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>
Marek Kurdej3e895082020-05-02 13:58:03 +020024void 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);
Marshall Clowf60c63c2018-12-14 18:49:35 +000029}
30
31template <class S>
32void test()
33{
34
Marek Kurdej3e895082020-05-02 13:58:03 +020035 test0(S(""), 'a', S(""), 0);
Marshall Clowf60c63c2018-12-14 18:49:35 +000036
Marek Kurdej3e895082020-05-02 13:58:03 +020037 test0(S("a"), 'a', S(""), 1);
38 test0(S("a"), 'b', S("a"), 0);
Marshall Clowf60c63c2018-12-14 18:49:35 +000039
Marek Kurdej3e895082020-05-02 13:58:03 +020040 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);
Marshall Clowf60c63c2018-12-14 18:49:35 +000045
Marek Kurdej3e895082020-05-02 13:58:03 +020046 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);
Marshall Clowf60c63c2018-12-14 18:49:35 +000050
Marek Kurdej3e895082020-05-02 13:58:03 +020051 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);
Marshall Clowf60c63c2018-12-14 18:49:35 +000059
Marek Kurdej3e895082020-05-02 13:58:03 +020060 // 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);
Marshall Clowf60c63c2018-12-14 18:49:35 +000066}
67
JF Bastien2df59c52019-02-04 20:31:13 +000068int main(int, char**)
Marshall Clowf60c63c2018-12-14 18:49:35 +000069{
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>>> ();
JF Bastien2df59c52019-02-04 20:31:13 +000073
74 return 0;
Marshall Clowf60c63c2018-12-14 18:49:35 +000075}