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 // iterator erase(const_iterator first, const_iterator last); // constexpr since C++20
12
13 #include <string>
14 #include <cassert>
15
16 #include "test_macros.h"
17 #include "min_allocator.h"
18 #include "asan_testing.h"
19
20 template <class S>
test(S s,typename S::difference_type pos,typename S::difference_type n,S expected)21 TEST_CONSTEXPR_CXX20 void test(S s, typename S::difference_type pos, typename S::difference_type n, S expected) {
22 typename S::const_iterator first = s.cbegin() + pos;
23 typename S::const_iterator last = s.cbegin() + pos + n;
24 typename S::iterator i = s.erase(first, last);
25 LIBCPP_ASSERT(s.__invariants());
26 assert(s[s.size()] == typename S::value_type());
27 assert(s == expected);
28 assert(i - s.begin() == pos);
29 LIBCPP_ASSERT(is_string_asan_correct(s));
30 }
31
32 template <class S>
test_string()33 TEST_CONSTEXPR_CXX20 void test_string() {
34 test(S(""), 0, 0, S(""));
35 test(S("abcde"), 0, 0, S("abcde"));
36 test(S("abcde"), 0, 1, S("bcde"));
37 test(S("abcde"), 0, 2, S("cde"));
38 test(S("abcde"), 0, 4, S("e"));
39 test(S("abcde"), 0, 5, S(""));
40 test(S("abcde"), 1, 0, S("abcde"));
41 test(S("abcde"), 1, 1, S("acde"));
42 test(S("abcde"), 1, 2, S("ade"));
43 test(S("abcde"), 1, 3, S("ae"));
44 test(S("abcde"), 1, 4, S("a"));
45 test(S("abcde"), 2, 0, S("abcde"));
46 test(S("abcde"), 2, 1, S("abde"));
47 test(S("abcde"), 2, 2, S("abe"));
48 test(S("abcde"), 2, 3, S("ab"));
49 test(S("abcde"), 4, 0, S("abcde"));
50 test(S("abcde"), 4, 1, S("abcd"));
51 test(S("abcde"), 5, 0, S("abcde"));
52 test(S("abcdefghij"), 0, 0, S("abcdefghij"));
53 test(S("abcdefghij"), 0, 1, S("bcdefghij"));
54 test(S("abcdefghij"), 0, 5, S("fghij"));
55 test(S("abcdefghij"), 0, 9, S("j"));
56 test(S("abcdefghij"), 0, 10, S(""));
57 test(S("abcdefghij"), 1, 0, S("abcdefghij"));
58 test(S("abcdefghij"), 1, 1, S("acdefghij"));
59 test(S("abcdefghij"), 1, 4, S("afghij"));
60 test(S("abcdefghij"), 1, 8, S("aj"));
61 test(S("abcdefghij"), 1, 9, S("a"));
62 test(S("abcdefghij"), 5, 0, S("abcdefghij"));
63 test(S("abcdefghij"), 5, 1, S("abcdeghij"));
64 test(S("abcdefghij"), 5, 2, S("abcdehij"));
65 test(S("abcdefghij"), 5, 4, S("abcdej"));
66 test(S("abcdefghij"), 5, 5, S("abcde"));
67 test(S("abcdefghij"), 9, 0, S("abcdefghij"));
68 test(S("abcdefghij"), 9, 1, S("abcdefghi"));
69 test(S("abcdefghij"), 10, 0, S("abcdefghij"));
70 test(S("abcdefghijklmnopqrst"), 0, 0, S("abcdefghijklmnopqrst"));
71 test(S("abcdefghijklmnopqrst"), 0, 1, S("bcdefghijklmnopqrst"));
72 test(S("abcdefghijklmnopqrst"), 0, 10, S("klmnopqrst"));
73 test(S("abcdefghijklmnopqrst"), 0, 19, S("t"));
74 test(S("abcdefghijklmnopqrst"), 0, 20, S(""));
75 test(S("abcdefghijklmnopqrst"), 1, 0, S("abcdefghijklmnopqrst"));
76 test(S("abcdefghijklmnopqrst"), 1, 1, S("acdefghijklmnopqrst"));
77 test(S("abcdefghijklmnopqrst"), 1, 9, S("aklmnopqrst"));
78 test(S("abcdefghijklmnopqrst"), 1, 18, S("at"));
79 test(S("abcdefghijklmnopqrst"), 1, 19, S("a"));
80 test(S("abcdefghijklmnopqrst"), 10, 0, S("abcdefghijklmnopqrst"));
81 test(S("abcdefghijklmnopqrst"), 10, 1, S("abcdefghijlmnopqrst"));
82 test(S("abcdefghijklmnopqrst"), 10, 5, S("abcdefghijpqrst"));
83 test(S("abcdefghijklmnopqrst"), 10, 9, S("abcdefghijt"));
84 test(S("abcdefghijklmnopqrst"), 10, 10, S("abcdefghij"));
85 test(S("abcdefghijklmnopqrst"), 19, 0, S("abcdefghijklmnopqrst"));
86 test(S("abcdefghijklmnopqrst"), 19, 1, S("abcdefghijklmnopqrs"));
87 test(S("abcdefghijklmnopqrst"), 20, 0, S("abcdefghijklmnopqrst"));
88 }
89
test()90 TEST_CONSTEXPR_CXX20 bool test() {
91 test_string<std::string>();
92 #if TEST_STD_VER >= 11
93 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
94 test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>();
95 #endif
96
97 return true;
98 }
99
main(int,char **)100 int main(int, char**) {
101 test();
102 #if TEST_STD_VER > 17
103 static_assert(test());
104 #endif
105
106 return 0;
107 }
108