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