]>
Commit | Line | Data |
---|---|---|
61f1ed59 PC |
1 | // 1999-05-11 bkoz |
2 | ||
8d9254fc | 3 | // Copyright (C) 1999-2020 Free Software Foundation, Inc. |
61f1ed59 PC |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
61f1ed59 PC |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
16 | // You should have received a copy of the GNU General Public License along | |
748086b7 JJ |
17 | // with this library; see the file COPYING3. If not see |
18 | // <http://www.gnu.org/licenses/>. | |
61f1ed59 PC |
19 | |
20 | // 21.3.3 string capacity | |
21 | ||
22 | #include <string> | |
23 | #include <testsuite_hooks.h> | |
24 | ||
25 | void test01() | |
26 | { | |
27 | // POD types : resize, capacity, reserve | |
61f1ed59 PC |
28 | std::string str01; |
29 | typedef std::string::size_type size_type_s; | |
30 | ||
31 | size_type_s sz01 = str01.capacity(); | |
32 | str01.reserve(100); | |
33 | size_type_s sz02 = str01.capacity(); | |
34 | VERIFY( sz02 >= sz01 ); | |
35 | VERIFY( sz02 >= 100 ); | |
140cf935 | 36 | #if __cplusplus <= 201703L |
61f1ed59 | 37 | str01.reserve(); |
34a2b755 | 38 | #else |
140cf935 | 39 | str01.shrink_to_fit(); // reserve is deprecated in C++20 |
34a2b755 | 40 | #endif |
140cf935 AL |
41 | sz01 = str01.capacity(); |
42 | VERIFY( sz01 < sz02 ); | |
43 | ||
44 | // P0966: reserve should not shrink | |
45 | str01.reserve(100); | |
46 | sz01 = str01.capacity(); | |
47 | str01.reserve(sz01 - 1); | |
48 | VERIFY( str01.capacity() == sz01 ); | |
61f1ed59 PC |
49 | |
50 | sz01 = str01.size() + 5; | |
51 | str01.resize(sz01); | |
52 | sz02 = str01.size(); | |
53 | VERIFY( sz01 == sz02 ); | |
54 | ||
55 | sz01 = str01.size() - 5; | |
56 | str01.resize(sz01); | |
57 | sz02 = str01.size(); | |
58 | VERIFY( sz01 == sz02 ); | |
59 | ||
60 | std::string str05(30, 'q'); | |
61 | std::string str06 = str05; | |
62 | str05 = str06 + str05; | |
63 | VERIFY( str05.capacity() >= str05.size() ); | |
64 | VERIFY( str06.capacity() >= str06.size() ); | |
65 | ||
66 | // POD types: size, length, max_size, clear(), empty() | |
67 | bool b01; | |
68 | std::string str011; | |
69 | b01 = str01.empty(); | |
70 | VERIFY( b01 == true ); | |
71 | sz01 = str01.size(); | |
72 | sz02 = str01.length(); | |
73 | VERIFY( sz01 == sz02 ); | |
74 | str01.c_str(); | |
75 | sz01 = str01.size(); | |
76 | sz02 = str01.length(); | |
77 | VERIFY( sz01 == sz02 ); | |
78 | ||
79 | sz01 = str01.length(); | |
80 | str01.c_str(); | |
81 | str011 = str01 + "_addendum_"; | |
82 | str01.c_str(); | |
83 | sz02 = str01.length(); | |
84 | VERIFY( sz01 == sz02 ); | |
85 | sz02 = str011.length(); | |
86 | VERIFY( sz02 > sz01 ); | |
87 | ||
88 | // trickster allocator issues involved with these: | |
89 | std::string str3 = "8-chars_8-chars_"; | |
61f1ed59 | 90 | std::string str4 = str3 + "7-chars"; |
61f1ed59 PC |
91 | |
92 | sz01 = str01.size(); | |
93 | sz02 = str01.max_size(); | |
94 | VERIFY( sz02 >= sz01 ); | |
95 | ||
96 | sz01 = str01.size(); | |
97 | str01.clear(); | |
98 | b01 = str01.empty(); | |
99 | VERIFY( b01 == true ); | |
100 | sz02 = str01.size(); | |
101 | VERIFY( sz01 >= sz02 ); | |
102 | } | |
103 | ||
104 | int main() | |
105 | { | |
106 | test01(); | |
107 | return 0; | |
108 | } |