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