]>
Commit | Line | Data |
---|---|---|
61f1ed59 PC |
1 | // 1999-05-11 bkoz |
2 | ||
a945c346 | 3 | // Copyright (C) 1999-2024 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 | 19 | |
b8f2efae JW |
20 | // { dg-options "-Wno-stringop-overflow" } |
21 | ||
61f1ed59 PC |
22 | // 21.3.3 string capacity |
23 | ||
24 | #include <string> | |
25 | #include <testsuite_hooks.h> | |
26 | ||
27 | void test01() | |
28 | { | |
29 | // POD types : resize, capacity, reserve | |
61f1ed59 PC |
30 | std::string str01; |
31 | typedef std::string::size_type size_type_s; | |
32 | ||
33 | size_type_s sz01 = str01.capacity(); | |
34 | str01.reserve(100); | |
35 | size_type_s sz02 = str01.capacity(); | |
36 | VERIFY( sz02 >= sz01 ); | |
37 | VERIFY( sz02 >= 100 ); | |
140cf935 | 38 | #if __cplusplus <= 201703L |
61f1ed59 | 39 | str01.reserve(); |
34a2b755 | 40 | #else |
140cf935 | 41 | str01.shrink_to_fit(); // reserve is deprecated in C++20 |
34a2b755 | 42 | #endif |
140cf935 AL |
43 | sz01 = str01.capacity(); |
44 | VERIFY( sz01 < sz02 ); | |
45 | ||
46 | // P0966: reserve should not shrink | |
47 | str01.reserve(100); | |
48 | sz01 = str01.capacity(); | |
49 | str01.reserve(sz01 - 1); | |
50 | VERIFY( str01.capacity() == sz01 ); | |
61f1ed59 PC |
51 | |
52 | sz01 = str01.size() + 5; | |
53 | str01.resize(sz01); | |
54 | sz02 = str01.size(); | |
55 | VERIFY( sz01 == sz02 ); | |
56 | ||
57 | sz01 = str01.size() - 5; | |
58 | str01.resize(sz01); | |
59 | sz02 = str01.size(); | |
60 | VERIFY( sz01 == sz02 ); | |
61 | ||
62 | std::string str05(30, 'q'); | |
63 | std::string str06 = str05; | |
b8f2efae | 64 | // The following triggers -Wstringop-overflow. See PR 103332. |
61f1ed59 PC |
65 | str05 = str06 + str05; |
66 | VERIFY( str05.capacity() >= str05.size() ); | |
67 | VERIFY( str06.capacity() >= str06.size() ); | |
68 | ||
69 | // POD types: size, length, max_size, clear(), empty() | |
70 | bool b01; | |
71 | std::string str011; | |
72 | b01 = str01.empty(); | |
73 | VERIFY( b01 == true ); | |
74 | sz01 = str01.size(); | |
75 | sz02 = str01.length(); | |
76 | VERIFY( sz01 == sz02 ); | |
b911ca42 | 77 | (void) str01.c_str(); |
61f1ed59 PC |
78 | sz01 = str01.size(); |
79 | sz02 = str01.length(); | |
80 | VERIFY( sz01 == sz02 ); | |
81 | ||
82 | sz01 = str01.length(); | |
b911ca42 | 83 | (void) str01.c_str(); |
61f1ed59 | 84 | str011 = str01 + "_addendum_"; |
b911ca42 | 85 | (void) str01.c_str(); |
61f1ed59 PC |
86 | sz02 = str01.length(); |
87 | VERIFY( sz01 == sz02 ); | |
88 | sz02 = str011.length(); | |
89 | VERIFY( sz02 > sz01 ); | |
90 | ||
91 | // trickster allocator issues involved with these: | |
92 | std::string str3 = "8-chars_8-chars_"; | |
61f1ed59 | 93 | std::string str4 = str3 + "7-chars"; |
61f1ed59 PC |
94 | |
95 | sz01 = str01.size(); | |
96 | sz02 = str01.max_size(); | |
97 | VERIFY( sz02 >= sz01 ); | |
98 | ||
99 | sz01 = str01.size(); | |
100 | str01.clear(); | |
101 | b01 = str01.empty(); | |
102 | VERIFY( b01 == true ); | |
103 | sz02 = str01.size(); | |
104 | VERIFY( sz01 >= sz02 ); | |
105 | } | |
106 | ||
107 | int main() | |
108 | { | |
109 | test01(); | |
110 | return 0; | |
111 | } |