xref: /llvm-project/libcxx/test/std/strings/basic.string/string.capacity/clear.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 clear(); // 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)21 TEST_CONSTEXPR_CXX20 void test(S s) {
22   s.clear();
23   assert(s.size() == 0);
24   LIBCPP_ASSERT(is_string_asan_correct(s));
25 }
26 
27 template <class S>
test_string()28 TEST_CONSTEXPR_CXX20 void test_string() {
29   S s;
30   test(s);
31   LIBCPP_ASSERT(is_string_asan_correct(s));
32 
33   s.assign(10, 'a');
34   s.erase(5);
35   LIBCPP_ASSERT(is_string_asan_correct(s));
36   test(s);
37   LIBCPP_ASSERT(is_string_asan_correct(s));
38 
39   s.assign(100, 'a');
40   s.erase(50);
41   LIBCPP_ASSERT(is_string_asan_correct(s));
42   test(s);
43   LIBCPP_ASSERT(is_string_asan_correct(s));
44 }
45 
test()46 TEST_CONSTEXPR_CXX20 bool test() {
47   test_string<std::string>();
48 #if TEST_STD_VER >= 11
49   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
50   test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>();
51 #endif
52 
53   return true;
54 }
55 
main(int,char **)56 int main(int, char**) {
57   test();
58 #if TEST_STD_VER > 17
59   static_assert(test());
60 #endif
61 
62   return 0;
63 }
64