| 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 |
// <string> |
| 10 |
|
| 11 |
// basic_string<charT,traits,Allocator>& |
| 12 |
// erase(size_type pos = 0, size_type n = npos); // constexpr since C++20 |
| 13 |
|
| 14 |
#include <string> |
| 15 |
#include <stdexcept> |
| 16 |
#include <cassert> |
| 17 |
|
| 18 |
#include "test_macros.h" |
| 19 |
#include "min_allocator.h" |
| 20 |
#include "asan_testing.h" |
| 21 |
|
| 22 |
template <class S> |
| 23 |
TEST_CONSTEXPR_CXX20 void test(S s, typename S::size_type pos, typename S::size_type n, S expected) { |
| 24 |
const typename S::size_type old_size = s.size(); |
| 25 |
S s0 = s; |
| 26 |
if (pos <= old_size) { |
| 27 |
s.erase(pos, n); |
| 28 |
LIBCPP_ASSERT(s.__invariants()); |
| 29 |
assert(s[s.size()] == typename S::value_type()); |
| 30 |
assert(s == expected); |
| 31 |
LIBCPP_ASSERT(is_string_asan_correct(s)); |
| 32 |
} |
| 33 |
#ifndef TEST_HAS_NO_EXCEPTIONS |
| 34 |
else if (!TEST_IS_CONSTANT_EVALUATED) { |
| 35 |
try { |
| 36 |
s.erase(pos, n); |
| 37 |
assert(false); |
| 38 |
} catch (std::out_of_range&) { |
| 39 |
assert(pos > old_size); |
| 40 |
assert(s == s0); |
| 41 |
} |
| 42 |
} |
| 43 |
#endif |
| 44 |
} |
| 45 |
|
| 46 |
template <class S> |
| 47 |
TEST_CONSTEXPR_CXX20 void test(S s, typename S::size_type pos, S expected) { |
| 48 |
const typename S::size_type old_size = s.size(); |
| 49 |
S s0 = s; |
| 50 |
if (pos <= old_size) { |
| 51 |
s.erase(pos); |
| 52 |
LIBCPP_ASSERT(s.__invariants()); |
| 53 |
assert(s[s.size()] == typename S::value_type()); |
| 54 |
assert(s == expected); |
| 55 |
} |
| 56 |
#ifndef TEST_HAS_NO_EXCEPTIONS |
| 57 |
else if (!TEST_IS_CONSTANT_EVALUATED) { |
| 58 |
try { |
| 59 |
s.erase(pos); |
| 60 |
assert(false); |
| 61 |
} catch (std::out_of_range&) { |
| 62 |
assert(pos > old_size); |
| 63 |
assert(s == s0); |
| 64 |
} |
| 65 |
} |
| 66 |
#endif |
| 67 |
} |
| 68 |
|
| 69 |
template <class S> |
| 70 |
TEST_CONSTEXPR_CXX20 void test(S s, S expected) { |
| 71 |
s.erase(); |
| 72 |
LIBCPP_ASSERT(s.__invariants()); |
| 73 |
assert(s[s.size()] == typename S::value_type()); |
| 74 |
assert(s == expected); |
| 75 |
} |
| 76 |
|
| 77 |
template <class S> |
| 78 |
TEST_CONSTEXPR_CXX20 void test_string() { |
| 79 |
test(S(""), 0, 0, S("")); |
| 80 |
test(S(""), 0, 1, S("")); |
| 81 |
test(S(""), 1, 0, S("can't happen")); |
| 82 |
test(S("abcde"), 0, 0, S("abcde")); |
| 83 |
test(S("abcde"), 0, 1, S("bcde")); |
| 84 |
test(S("abcde"), 0, 2, S("cde")); |
| 85 |
test(S("abcde"), 0, 4, S("e")); |
| 86 |
test(S("abcde"), 0, 5, S("")); |
| 87 |
test(S("abcde"), 0, 6, S("")); |
| 88 |
test(S("abcde"), 1, 0, S("abcde")); |
| 89 |
test(S("abcde"), 1, 1, S("acde")); |
| 90 |
test(S("abcde"), 1, 2, S("ade")); |
| 91 |
test(S("abcde"), 1, 3, S("ae")); |
| 92 |
test(S("abcde"), 1, 4, S("a")); |
| 93 |
test(S("abcde"), 1, 5, S("a")); |
| 94 |
test(S("abcde"), 2, 0, S("abcde")); |
| 95 |
test(S("abcde"), 2, 1, S("abde")); |
| 96 |
test(S("abcde"), 2, 2, S("abe")); |
| 97 |
test(S("abcde"), 2, 3, S("ab")); |
| 98 |
test(S("abcde"), 2, 4, S("ab")); |
| 99 |
test(S("abcde"), 4, 0, S("abcde")); |
| 100 |
test(S("abcde"), 4, 1, S("abcd")); |
| 101 |
test(S("abcde"), 4, 2, S("abcd")); |
| 102 |
test(S("abcde"), 5, 0, S("abcde")); |
| 103 |
test(S("abcde"), 5, 1, S("abcde")); |
| 104 |
test(S("abcde"), 6, 0, S("can't happen")); |
| 105 |
test(S("abcdefghij"), 0, 0, S("abcdefghij")); |
| 106 |
test(S("abcdefghij"), 0, 1, S("bcdefghij")); |
| 107 |
test(S("abcdefghij"), 0, 5, S("fghij")); |
| 108 |
test(S("abcdefghij"), 0, 9, S("j")); |
| 109 |
test(S("abcdefghij"), 0, 10, S("")); |
| 110 |
test(S("abcdefghij"), 0, 11, S("")); |
| 111 |
test(S("abcdefghij"), 1, 0, S("abcdefghij")); |
| 112 |
test(S("abcdefghij"), 1, 1, S("acdefghij")); |
| 113 |
test(S("abcdefghij"), 1, 4, S("afghij")); |
| 114 |
test(S("abcdefghij"), 1, 8, S("aj")); |
| 115 |
test(S("abcdefghij"), 1, 9, S("a")); |
| 116 |
test(S("abcdefghij"), 1, 10, S("a")); |
| 117 |
test(S("abcdefghij"), 5, 0, S("abcdefghij")); |
| 118 |
test(S("abcdefghij"), 5, 1, S("abcdeghij")); |
| 119 |
test(S("abcdefghij"), 5, 2, S("abcdehij")); |
| 120 |
test(S("abcdefghij"), 5, 4, S("abcdej")); |
| 121 |
test(S("abcdefghij"), 5, 5, S("abcde")); |
| 122 |
test(S("abcdefghij"), 5, 6, S("abcde")); |
| 123 |
test(S("abcdefghij"), 9, 0, S("abcdefghij")); |
| 124 |
test(S("abcdefghij"), 9, 1, S("abcdefghi")); |
| 125 |
test(S("abcdefghij"), 9, 2, S("abcdefghi")); |
| 126 |
test(S("abcdefghij"), 10, 0, S("abcdefghij")); |
| 127 |
test(S("abcdefghij"), 10, 1, S("abcdefghij")); |
| 128 |
test(S("abcdefghij"), 11, 0, S("can't happen")); |
| 129 |
test(S("abcdefghijklmnopqrst"), 0, 0, S("abcdefghijklmnopqrst")); |
| 130 |
test(S("abcdefghijklmnopqrst"), 0, 1, S("bcdefghijklmnopqrst")); |
| 131 |
test(S("abcdefghijklmnopqrst"), 0, 10, S("klmnopqrst")); |
| 132 |
test(S("abcdefghijklmnopqrst"), 0, 19, S("t")); |
| 133 |
test(S("abcdefghijklmnopqrst"), 0, 20, S("")); |
| 134 |
test(S("abcdefghijklmnopqrst"), 0, 21, S("")); |
| 135 |
test(S("abcdefghijklmnopqrst"), 1, 0, S("abcdefghijklmnopqrst")); |
| 136 |
test(S("abcdefghijklmnopqrst"), 1, 1, S("acdefghijklmnopqrst")); |
| 137 |
test(S("abcdefghijklmnopqrst"), 1, 9, S("aklmnopqrst")); |
| 138 |
test(S("abcdefghijklmnopqrst"), 1, 18, S("at")); |
| 139 |
test(S("abcdefghijklmnopqrst"), 1, 19, S("a")); |
| 140 |
test(S("abcdefghijklmnopqrst"), 1, 20, S("a")); |
| 141 |
test(S("abcdefghijklmnopqrst"), 10, 0, S("abcdefghijklmnopqrst")); |
| 142 |
test(S("abcdefghijklmnopqrst"), 10, 1, S("abcdefghijlmnopqrst")); |
| 143 |
test(S("abcdefghijklmnopqrst"), 10, 5, S("abcdefghijpqrst")); |
| 144 |
test(S("abcdefghijklmnopqrst"), 10, 9, S("abcdefghijt")); |
| 145 |
test(S("abcdefghijklmnopqrst"), 10, 10, S("abcdefghij")); |
| 146 |
test(S("abcdefghijklmnopqrst"), 10, 11, S("abcdefghij")); |
| 147 |
test(S("abcdefghijklmnopqrst"), 19, 0, S("abcdefghijklmnopqrst")); |
| 148 |
test(S("abcdefghijklmnopqrst"), 19, 1, S("abcdefghijklmnopqrs")); |
| 149 |
test(S("abcdefghijklmnopqrst"), 19, 2, S("abcdefghijklmnopqrs")); |
| 150 |
test(S("abcdefghijklmnopqrst"), 20, 0, S("abcdefghijklmnopqrst")); |
| 151 |
test(S("abcdefghijklmnopqrst"), 20, 1, S("abcdefghijklmnopqrst")); |
| 152 |
test(S("abcdefghijklmnopqrst"), 21, 0, S("can't happen")); |
| 153 |
|
| 154 |
test(S(""), 0, S("")); |
| 155 |
test(S(""), 1, S("can't happen")); |
| 156 |
test(S("abcde"), 0, S("")); |
| 157 |
test(S("abcde"), 1, S("a")); |
| 158 |
test(S("abcde"), 2, S("ab")); |
| 159 |
test(S("abcde"), 4, S("abcd")); |
| 160 |
test(S("abcde"), 5, S("abcde")); |
| 161 |
test(S("abcde"), 6, S("can't happen")); |
| 162 |
test(S("abcdefghij"), 0, S("")); |
| 163 |
test(S("abcdefghij"), 1, S("a")); |
| 164 |
test(S("abcdefghij"), 5, S("abcde")); |
| 165 |
test(S("abcdefghij"), 9, S("abcdefghi")); |
| 166 |
test(S("abcdefghij"), 10, S("abcdefghij")); |
| 167 |
test(S("abcdefghij"), 11, S("can't happen")); |
| 168 |
test(S("abcdefghijklmnopqrst"), 0, S("")); |
| 169 |
test(S("abcdefghijklmnopqrst"), 1, S("a")); |
| 170 |
test(S("abcdefghijklmnopqrst"), 10, S("abcdefghij")); |
| 171 |
test(S("abcdefghijklmnopqrst"), 19, S("abcdefghijklmnopqrs")); |
| 172 |
test(S("abcdefghijklmnopqrst"), 20, S("abcdefghijklmnopqrst")); |
| 173 |
test(S("abcdefghijklmnopqrst"), 21, S("can't happen")); |
| 174 |
|
| 175 |
test(S(""), S("")); |
| 176 |
test(S("abcde"), S("")); |
| 177 |
test(S("abcdefghij"), S("")); |
| 178 |
test(S("abcdefghijklmnopqrst"), S("")); |
| 179 |
} |
| 180 |
|
| 181 |
TEST_CONSTEXPR_CXX20 bool test() { |
| 182 |
test_string<std::string>(); |
| 183 |
#if TEST_STD_VER >= 11 |
| 184 |
test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>(); |
| 185 |
test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>(); |
| 186 |
#endif |
| 187 |
|
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
int main(int, char**) { |
| 192 |
test(); |
| 193 |
#if TEST_STD_VER > 17 |
| 194 |
static_assert(test()); |
| 195 |
#endif |
| 196 |
|
| 197 |
return 0; |
| 198 |
} |