xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_erase/iter.pass.cpp (revision 9ed20568e7de53dce85f1631d7d8c1415e7930ae)
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 p); // 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,S expected)21 TEST_CONSTEXPR_CXX20 void test(S s, typename S::difference_type pos, S expected) {
22   typename S::const_iterator p = s.begin() + pos;
23   typename S::iterator i       = s.erase(p);
24   LIBCPP_ASSERT(s.__invariants());
25   assert(s[s.size()] == typename S::value_type());
26   assert(s == expected);
27   assert(i - s.begin() == pos);
28   LIBCPP_ASSERT(is_string_asan_correct(s));
29 }
30 
31 template <class S>
test_string()32 TEST_CONSTEXPR_CXX20 void test_string() {
33   test(S("abcde"), 0, S("bcde"));
34   test(S("abcde"), 1, S("acde"));
35   test(S("abcde"), 2, S("abde"));
36   test(S("abcde"), 4, S("abcd"));
37   test(S("abcdefghij"), 0, S("bcdefghij"));
38   test(S("abcdefghij"), 1, S("acdefghij"));
39   test(S("abcdefghij"), 5, S("abcdeghij"));
40   test(S("abcdefghij"), 9, S("abcdefghi"));
41   test(S("abcdefghijklmnopqrst"), 0, S("bcdefghijklmnopqrst"));
42   test(S("abcdefghijklmnopqrst"), 1, S("acdefghijklmnopqrst"));
43   test(S("abcdefghijklmnopqrst"), 10, S("abcdefghijlmnopqrst"));
44   test(S("abcdefghijklmnopqrst"), 19, S("abcdefghijklmnopqrs"));
45 }
46 
test()47 TEST_CONSTEXPR_CXX20 bool test() {
48   test_string<std::string>();
49 #if TEST_STD_VER >= 11
50   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
51   test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>();
52 #endif
53 
54   return true;
55 }
56 
main(int,char **)57 int main(int, char**) {
58   test();
59 #if TEST_STD_VER > 17
60   static_assert(test());
61 #endif
62 
63   return 0;
64 }
65