]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / capacity / char / 1.cc
1 // 1999-05-11 bkoz
2
3 // Copyright (C) 1999-2022 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
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
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // { dg-options "-Wno-stringop-overflow" }
21
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
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 );
38 #if __cplusplus <= 201703L
39 str01.reserve();
40 #else
41 str01.shrink_to_fit(); // reserve is deprecated in C++20
42 #endif
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 );
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;
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() );
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 );
77 str01.c_str();
78 sz01 = str01.size();
79 sz02 = str01.length();
80 VERIFY( sz01 == sz02 );
81
82 sz01 = str01.length();
83 str01.c_str();
84 str011 = str01 + "_addendum_";
85 str01.c_str();
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_";
93 std::string str4 = str3 + "7-chars";
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 }