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 TEST_CONSTEXPR_CXX20 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 bool test() {
46   {
47     typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > S;
48     S s((test_allocator<char>(&alloc_stats)));
49     test(s);
50     s.assign(10, 'a');
51     s.erase(5);
52     test(s);
53     s.assign(100, 'a');
54     s.erase(50);
55     test(s);
56   }
57 #if TEST_STD_VER >= 11
58   {
59     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
60     S s;
61     assert(s.capacity() > 0);
62   }
63 #endif
64 
65   return true;
66 }
67 
68 int main(int, char**)
69 {
70   test();
71 #if TEST_STD_VER > 17
72   // static_assert(test());
73 #endif
74 
75   return 0;
76 }
77