]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/1.cc
3 // Copyright (C) 1999-2022 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 // { dg-options "-Wno-stringop-overflow" }
22 // 21.3.3 string capacity
25 #include <testsuite_hooks.h>
29 // POD types : resize, capacity, reserve
31 typedef std::string::size_type size_type_s
;
33 size_type_s sz01
= str01
.capacity();
35 size_type_s sz02
= str01
.capacity();
36 VERIFY( sz02
>= sz01
);
37 VERIFY( sz02
>= 100 );
38 #if __cplusplus <= 201703L
41 str01
.shrink_to_fit(); // reserve is deprecated in C++20
43 sz01
= str01
.capacity();
44 VERIFY( sz01
< sz02
);
46 // P0966: reserve should not shrink
48 sz01
= str01
.capacity();
49 str01
.reserve(sz01
- 1);
50 VERIFY( str01
.capacity() == sz01
);
52 sz01
= str01
.size() + 5;
55 VERIFY( sz01
== sz02
);
57 sz01
= str01
.size() - 5;
60 VERIFY( sz01
== sz02
);
62 std::string
str05(30, 'q');
63 std::string str06
= str05
;
64 // The following triggers -Wstringop-overflow. See PR 103332.
65 str05
= str06
+ str05
;
66 VERIFY( str05
.capacity() >= str05
.size() );
67 VERIFY( str06
.capacity() >= str06
.size() );
69 // POD types: size, length, max_size, clear(), empty()
73 VERIFY( b01
== true );
75 sz02
= str01
.length();
76 VERIFY( sz01
== sz02
);
79 sz02
= str01
.length();
80 VERIFY( sz01
== sz02
);
82 sz01
= str01
.length();
84 str011
= str01
+ "_addendum_";
86 sz02
= str01
.length();
87 VERIFY( sz01
== sz02
);
88 sz02
= str011
.length();
89 VERIFY( sz02
> sz01
);
91 // trickster allocator issues involved with these:
92 std::string str3
= "8-chars_8-chars_";
93 std::string str4
= str3
+ "7-chars";
96 sz02
= str01
.max_size();
97 VERIFY( sz02
>= sz01
);
102 VERIFY( b01
== true );
104 VERIFY( sz01
>= sz02
);