Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Louis Dionne | 31cbe0f | 2020-06-01 10:38:23 -0400 | [diff] [blame] | 8 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 9 | |
| 10 | // <string> |
| 11 | |
| 12 | // template <class charT, class traits, class Allocator, class U> |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 13 | // typename basic_string<charT, traits, Allocator>::size_type |
| 14 | // erase(basic_string<charT, traits, Allocator>& c, const U& value); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 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> |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 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); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | template <class S> |
| 32 | void test() |
| 33 | { |
| 34 | |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 35 | test0(S(""), 'a', S(""), 0); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 36 | |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 37 | test0(S("a"), 'a', S(""), 1); |
| 38 | test0(S("a"), 'b', S("a"), 0); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 39 | |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 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); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 45 | |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 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); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 50 | |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 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); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 59 | |
Marek Kurdej | 3e89508 | 2020-05-02 13:58:03 +0200 | [diff] [blame] | 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); |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 66 | } |
| 67 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 68 | int main(int, char**) |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 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>>> (); |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 73 | |
| 74 | return 0; |
Marshall Clow | f60c63c | 2018-12-14 18:49:35 +0000 | [diff] [blame] | 75 | } |