xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_erase/pop_back.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 // void pop_back(); // 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,S expected)21 TEST_CONSTEXPR_CXX20 void test(S s, S expected) {
22   s.pop_back();
23   LIBCPP_ASSERT(s.__invariants());
24   assert(s[s.size()] == typename S::value_type());
25   assert(s == expected);
26   LIBCPP_ASSERT(is_string_asan_correct(s));
27 }
28 
29 template <class S>
test_string()30 TEST_CONSTEXPR_CXX20 void test_string() {
31   test(S("abcde"), S("abcd"));
32   test(S("abcdefghij"), S("abcdefghi"));
33   test(S("abcdefghijklmnopqrst"), S("abcdefghijklmnopqrs"));
34   test(S("abcdefghijklmnopqrstabcdefghijklmnopqrstabcdefghijklmnopqrstabcdefghijklmnopqrst"),
35        S("abcdefghijklmnopqrstabcdefghijklmnopqrstabcdefghijklmnopqrstabcdefghijklmnopqrs"));
36 }
37 
test()38 TEST_CONSTEXPR_CXX20 bool test() {
39   test_string<std::string>();
40 #if TEST_STD_VER >= 11
41   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
42   test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>();
43 #endif
44 
45   return true;
46 }
47 
main(int,char **)48 int main(int, char**) {
49   test();
50 #if TEST_STD_VER > 17
51   static_assert(test());
52 #endif
53 
54   return 0;
55 }
56