83 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710 11// <string>12 13// template <class charT, class traits, class Allocator, class Predicate>14// constexpr typename basic_string<charT, traits, Allocator>::size_type15// erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred);16 17#include <string>18 19#include "test_macros.h"20#include "test_allocator.h"21#include "min_allocator.h"22 23template <class S, class Pred>24constexpr void test0(S s, Pred p, S expected, std::size_t expected_erased_count) {25 ASSERT_SAME_TYPE(typename S::size_type, decltype(std::erase_if(s, p)));26 assert(expected_erased_count == std::erase_if(s, p));27 LIBCPP_ASSERT(s.__invariants());28 assert(s == expected);29}30 31template <typename S>32constexpr void test() {33 auto isA = [](auto ch) { return ch == 'a'; };34 auto isB = [](auto ch) { return ch == 'b'; };35 auto isC = [](auto ch) { return ch == 'c'; };36 auto isD = [](auto ch) { return ch == 'd'; };37 auto True = [](auto) { return true; };38 auto False = [](auto) { return false; };39 40 test0(S(""), isA, S(""), 0);41 42 test0(S("a"), isA, S(""), 1);43 test0(S("a"), isB, S("a"), 0);44 45 test0(S("ab"), isA, S("b"), 1);46 test0(S("ab"), isB, S("a"), 1);47 test0(S("ab"), isC, S("ab"), 0);48 test0(S("aa"), isA, S(""), 2);49 test0(S("aa"), isC, S("aa"), 0);50 51 test0(S("abc"), isA, S("bc"), 1);52 test0(S("abc"), isB, S("ac"), 1);53 test0(S("abc"), isC, S("ab"), 1);54 test0(S("abc"), isD, S("abc"), 0);55 56 test0(S("aab"), isA, S("b"), 2);57 test0(S("aab"), isB, S("aa"), 1);58 test0(S("aab"), isC, S("aab"), 0);59 test0(S("abb"), isA, S("bb"), 1);60 test0(S("abb"), isB, S("a"), 2);61 test0(S("abb"), isC, S("abb"), 0);62 test0(S("aaa"), isA, S(""), 3);63 test0(S("aaa"), isB, S("aaa"), 0);64 65 test0(S("aba"), False, S("aba"), 0);66 test0(S("aba"), True, S(""), 3);67}68 69constexpr bool test() {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>>>();73 74 return true;75}76 77int main(int, char**) {78 test();79 static_assert(test());80 81 return 0;82}83