summaryrefslogtreecommitdiffstats
path: root/libcxx/test/std/strings/strings.erasure/erase.pass.cpp
blob: 26d6b8674e87dec6813dc2f3e562e14d853cbca6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17

// <string>

// template <class charT, class traits, class Allocator, class U>
//   void erase(basic_string<charT, traits, Allocator>& c, const U& value);


#include <string>
#include <optional>

#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"

template <class S, class U>
void
test0(S s,  U val, S expected)
{
    ASSERT_SAME_TYPE(void, decltype(std::erase(s, val)));
    std::erase(s, val);
    LIBCPP_ASSERT(s.__invariants());
    assert(s == expected);
}

template <class S>
void test()
{

    test0(S(""), 'a', S(""));

    test0(S("a"), 'a', S(""));
    test0(S("a"), 'b', S("a"));

    test0(S("ab"), 'a', S("b"));
    test0(S("ab"), 'b', S("a"));
    test0(S("ab"), 'c', S("ab"));
    test0(S("aa"), 'a', S(""));
    test0(S("aa"), 'c', S("aa"));

    test0(S("abc"), 'a', S("bc"));
    test0(S("abc"), 'b', S("ac"));
    test0(S("abc"), 'c', S("ab"));
    test0(S("abc"), 'd', S("abc"));

    test0(S("aab"), 'a', S("b"));
    test0(S("aab"), 'b', S("aa"));
    test0(S("aab"), 'c', S("aab"));
    test0(S("abb"), 'a', S("bb"));
    test0(S("abb"), 'b', S("a"));
    test0(S("abb"), 'c', S("abb"));
    test0(S("aaa"), 'a', S(""));
    test0(S("aaa"), 'b', S("aaa"));

//  Test cross-type erasure
    using opt = std::optional<typename S::value_type>;
    test0(S("aba"), opt(),    S("aba"));
    test0(S("aba"), opt('a'), S("b"));
    test0(S("aba"), opt('b'), S("aa"));
    test0(S("aba"), opt('c'), S("aba"));
}

int main(int, char**)
{
    test<std::string>();
    test<std::basic_string<char, std::char_traits<char>, min_allocator<char>>> ();
    test<std::basic_string<char, std::char_traits<char>, test_allocator<char>>> ();

  return 0;
}
OpenPOWER on IntegriCloud