xref: /llvm-project/libcxx/test/std/strings/basic.string/string.capacity/capacity.pass.cpp (revision 9a140a1586cc4d13ecc97a28065f9ed2f93c99de)
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 // size_type capacity() const;
12 
13 #include <string>
14 #include <cassert>
15 
16 #include "test_allocator.h"
17 #include "min_allocator.h"
18 
19 #include "test_macros.h"
20 
21 test_allocator_statistics alloc_stats;
22 
23 template <class S>
24 void
25 test(S s)
26 {
27     alloc_stats.throw_after = 0;
28 #ifndef TEST_HAS_NO_EXCEPTIONS
29     try
30 #endif
31     {
32         while (s.size() < s.capacity())
33             s.push_back(typename S::value_type());
34         assert(s.size() == s.capacity());
35     }
36 #ifndef TEST_HAS_NO_EXCEPTIONS
37     catch (...)
38     {
39         assert(false);
40     }
41 #endif
42     alloc_stats.throw_after = INT_MAX;
43 }
44 
45 int main(int, char**)
46 {
47     {
48     typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > S;
49     S s((test_allocator<char>(&alloc_stats)));
50     test(s);
51     s.assign(10, 'a');
52     s.erase(5);
53     test(s);
54     s.assign(100, 'a');
55     s.erase(50);
56     test(s);
57     }
58 #if TEST_STD_VER >= 11
59     {
60     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
61     S s;
62     assert(s.capacity() > 0);
63     }
64 #endif
65 
66   return 0;
67 }
68