]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/21_strings/basic_string/capacity/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / capacity / 1.cc
CommitLineData
b2dad0e3
BK
1// 1999-05-11 bkoz
2
a945c346 3// Copyright (C) 1999-2024 Free Software Foundation, Inc.
b2dad0e3
BK
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)
b2dad0e3
BK
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/>.
b2dad0e3 19
9a27acc3 20// { dg-options "-Wno-stringop-overflow -Wno-stringop-overread" }
e864d395 21
b2dad0e3
BK
22// 21.3.3 string capacity
23
24#include <string>
1b716e90 25#include <cstring>
fe413112 26#include <testsuite_hooks.h>
b2dad0e3
BK
27
28template<typename T>
29 struct A { };
30
31template<typename T>
32 bool
8b5bc374 33 operator==(const A<T>&, const A<T>&) { return true; }
b2dad0e3
BK
34
35template<typename T>
36 bool
8b5bc374 37 operator<(const A<T>&, const A<T>&) { return true; }
b2dad0e3
BK
38
39struct B { };
40
f13a69ec
BK
41// char_traits specialization
42namespace std
43{
44 template<>
45 struct char_traits<A<B> >
46 {
47 typedef A<B> char_type;
48 // Unsigned as wint_t in unsigned.
49 typedef unsigned long int_type;
50 typedef streampos pos_type;
51 typedef streamoff off_type;
52 typedef mbstate_t state_type;
53
54 static void
55 assign(char_type& __c1, const char_type& __c2)
56 { __c1 = __c2; }
57
58 static bool
59 eq(const char_type& __c1, const char_type& __c2)
60 { return __c1 == __c2; }
61
62 static bool
63 lt(const char_type& __c1, const char_type& __c2)
64 { return __c1 < __c2; }
65
66 static int
67 compare(const char_type* __s1, const char_type* __s2, size_t __n)
68 {
69 for (size_t __i = 0; __i < __n; ++__i)
70 if (!eq(__s1[__i], __s2[__i]))
71 return lt(__s1[__i], __s2[__i]) ? -1 : 1;
72 return 0;
73 }
74
75 static size_t
76 length(const char_type* __s)
77 {
78 const char_type* __p = __s;
79 while (__p)
80 ++__p;
81 return (__p - __s);
82 }
83
84 static const char_type*
85 find(const char_type* __s, size_t __n, const char_type& __a)
86 {
87 for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
88 if (*__p == __a) return __p;
89 return 0;
90 }
91
92 static char_type*
93 move(char_type* __s1, const char_type* __s2, size_t __n)
94 { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
95
96 static char_type*
97 copy(char_type* __s1, const char_type* __s2, size_t __n)
98 { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
99
100 static char_type*
101 assign(char_type* __s, size_t __n, char_type __a)
102 {
103 for (char_type* __p = __s; __p < __s + __n; ++__p)
104 assign(*__p, __a);
105 return __s;
106 }
107
108 static char_type
8b5bc374 109 to_char_type(const int_type&)
f13a69ec
BK
110 { return char_type(); }
111
112 static int_type
8b5bc374 113 to_int_type(const char_type&) { return int_type(); }
f13a69ec
BK
114
115 static bool
116 eq_int_type(const int_type& __c1, const int_type& __c2)
117 { return __c1 == __c2; }
118
119 static int_type
120 eof() { return static_cast<int_type>(-1); }
121
122 static int_type
123 not_eof(const int_type& __c)
124 { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
125 };
126} // namespace std
127
36ce7daa 128void test01()
b2dad0e3 129{
61f1ed59 130 // non POD types : resize, capacity, reserve
b2dad0e3
BK
131 std::basic_string< A<B> > str02;
132 typedef std::basic_string< A<B> >::size_type size_type_o;
133 size_type_o sz03;
134 size_type_o sz04;
135
136 sz03 = str02.capacity();
137 str02.reserve(100);
138 sz04 = str02.capacity();
aa1b2f7d
BV
139 VERIFY( sz04 >= sz03 );
140 VERIFY( sz04 >= 100 );
140cf935 141#if __cplusplus <= 201703L
b2dad0e3 142 str02.reserve();
34a2b755 143#else
140cf935 144 str02.shrink_to_fit(); // reserve is deprecated in C++20
34a2b755 145#endif
140cf935
AL
146 sz03 = str02.capacity();
147 VERIFY( sz03 < sz04 );
148
149 // P0966: reserve should not shrink
150 str02.reserve(100);
151 sz03 = str02.capacity();
152 str02.reserve(sz03 - 1);
153 VERIFY( str02.capacity() == sz03 );
b2dad0e3
BK
154
155 sz03 = str02.size() + 5;
156 str02.resize(sz03);
157 sz04 = str02.size();
aa1b2f7d 158 VERIFY( sz03 == sz04 );
b2dad0e3
BK
159
160 sz03 = str02.size() - 5;
161 str02.resize(sz03);
162 sz04 = str02.size();
aa1b2f7d 163 VERIFY( sz03 == sz04 );
b2dad0e3
BK
164
165 A<B> inst_obj;
166 std::basic_string<A<B> > str07(30, inst_obj);
167 std::basic_string<A<B> > str08 = str07;
168 str07 = str08 + str07;
aa1b2f7d
BV
169 VERIFY( str07.capacity() >= str07.size() );
170 VERIFY( str08.capacity() >= str08.size() );
b2dad0e3 171
61f1ed59
PC
172 // non-POD types: size, length, max_size, clear(), empty()
173 bool b01 = str02.empty();
aa1b2f7d 174 VERIFY( b01 == true );
b2dad0e3
BK
175 sz03 = str02.size();
176 sz04 = str02.length();
aa1b2f7d 177 VERIFY( sz03 == sz04 );
b911ca42 178 (void) str02.c_str();
b2dad0e3
BK
179 sz03 = str02.size();
180 sz04 = str02.length();
aa1b2f7d 181 VERIFY( sz03 == sz04 );
b2dad0e3
BK
182
183 sz03 = str02.max_size();
aa1b2f7d 184 VERIFY( sz03 >= sz04 );
b2dad0e3
BK
185
186 sz03 = str02.size();
187 str02.clear();
188 b01 = str02.empty();
aa1b2f7d 189 VERIFY( b01 == true );
b2dad0e3 190 sz04 = str02.size();
aa1b2f7d 191 VERIFY( sz03 >= sz04 );
b2dad0e3
BK
192}
193
72c9b062 194#if !__GXX_WEAK__
0d223e3a
BK
195// Explicitly instantiate for systems with no COMDAT or weak support.
196template
1631cbcf 197 const std::basic_string< A<B> >::size_type
0d223e3a
BK
198 std::basic_string< A<B> >::_Rep::_S_max_size;
199
200template
1631cbcf 201 const A<B>
0d223e3a 202 std::basic_string< A<B> >::_Rep::_S_terminal;
72c9b062 203#endif
dcc61724 204
b2dad0e3
BK
205int main()
206{
207 test01();
b2dad0e3
BK
208 return 0;
209}