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; // constexpr since C++20
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 template <class S>
22 TEST_CONSTEXPR_CXX20 void
test(S s,test_allocator_statistics & alloc_stats)23 test(S s, test_allocator_statistics& alloc_stats)
24 {
25 alloc_stats.throw_after = 0;
26 #ifndef TEST_HAS_NO_EXCEPTIONS
27 try
28 #endif
29 {
30 while (s.size() < s.capacity())
31 s.push_back(typename S::value_type());
32 assert(s.size() == s.capacity());
33 }
34 #ifndef TEST_HAS_NO_EXCEPTIONS
35 catch (...)
36 {
37 assert(false);
38 }
39 #endif
40 alloc_stats.throw_after = INT_MAX;
41 }
42
test()43 TEST_CONSTEXPR_CXX20 bool test() {
44 {
45 test_allocator_statistics alloc_stats;
46 typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > S;
47 S s((test_allocator<char>(&alloc_stats)));
48 test(s, alloc_stats);
49 s.assign(10, 'a');
50 s.erase(5);
51 test(s, alloc_stats);
52 s.assign(100, 'a');
53 s.erase(50);
54 test(s, alloc_stats);
55 }
56 #if TEST_STD_VER >= 11
57 {
58 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
59 S s;
60 assert(s.capacity() > 0);
61 }
62 #endif
63
64 return true;
65 }
66
main(int,char **)67 int main(int, char**)
68 {
69 test();
70 #if TEST_STD_VER > 17
71 static_assert(test());
72 #endif
73
74 return 0;
75 }
76