]> 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
b9e8095b 1// 1999-05-11 bkoz
2
f1717362 3// Copyright (C) 1999-2016 Free Software Foundation, Inc.
b9e8095b 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
6bc9506f 8// Free Software Foundation; either version 3, or (at your option)
b9e8095b 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
6bc9506f 17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
b9e8095b 19
20// 21.3.3 string capacity
21
22#include <string>
1f3fc4aa 23#include <cstring>
0194306c 24#include <testsuite_hooks.h>
b9e8095b 25
26template<typename T>
27 struct A { };
28
29template<typename T>
30 bool
16171c51 31 operator==(const A<T>&, const A<T>&) { return true; }
b9e8095b 32
33template<typename T>
34 bool
16171c51 35 operator<(const A<T>&, const A<T>&) { return true; }
b9e8095b 36
37struct B { };
38
0cb0a61b 39// char_traits specialization
40namespace std
41{
42 template<>
43 struct char_traits<A<B> >
44 {
45 typedef A<B> char_type;
46 // Unsigned as wint_t in unsigned.
47 typedef unsigned long int_type;
48 typedef streampos pos_type;
49 typedef streamoff off_type;
50 typedef mbstate_t state_type;
51
52 static void
53 assign(char_type& __c1, const char_type& __c2)
54 { __c1 = __c2; }
55
56 static bool
57 eq(const char_type& __c1, const char_type& __c2)
58 { return __c1 == __c2; }
59
60 static bool
61 lt(const char_type& __c1, const char_type& __c2)
62 { return __c1 < __c2; }
63
64 static int
65 compare(const char_type* __s1, const char_type* __s2, size_t __n)
66 {
67 for (size_t __i = 0; __i < __n; ++__i)
68 if (!eq(__s1[__i], __s2[__i]))
69 return lt(__s1[__i], __s2[__i]) ? -1 : 1;
70 return 0;
71 }
72
73 static size_t
74 length(const char_type* __s)
75 {
76 const char_type* __p = __s;
77 while (__p)
78 ++__p;
79 return (__p - __s);
80 }
81
82 static const char_type*
83 find(const char_type* __s, size_t __n, const char_type& __a)
84 {
85 for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
86 if (*__p == __a) return __p;
87 return 0;
88 }
89
90 static char_type*
91 move(char_type* __s1, const char_type* __s2, size_t __n)
92 { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
93
94 static char_type*
95 copy(char_type* __s1, const char_type* __s2, size_t __n)
96 { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
97
98 static char_type*
99 assign(char_type* __s, size_t __n, char_type __a)
100 {
101 for (char_type* __p = __s; __p < __s + __n; ++__p)
102 assign(*__p, __a);
103 return __s;
104 }
105
106 static char_type
16171c51 107 to_char_type(const int_type&)
0cb0a61b 108 { return char_type(); }
109
110 static int_type
16171c51 111 to_int_type(const char_type&) { return int_type(); }
0cb0a61b 112
113 static bool
114 eq_int_type(const int_type& __c1, const int_type& __c2)
115 { return __c1 == __c2; }
116
117 static int_type
118 eof() { return static_cast<int_type>(-1); }
119
120 static int_type
121 not_eof(const int_type& __c)
122 { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
123 };
124} // namespace std
125
86988432 126void test01()
b9e8095b 127{
f8ef786c 128 bool test __attribute__((unused)) = true;
4aca39bb 129
130 // non POD types : resize, capacity, reserve
b9e8095b 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();
43a8c05a 139 VERIFY( sz04 >= sz03 );
140 VERIFY( sz04 >= 100 );
b9e8095b 141 str02.reserve();
142 sz03 = str02.capacity();
63f54259 143#if _GLIBCXX_USE_CXX11_ABI
144 VERIFY( sz03 < 100);
145#else
f633413e 146 VERIFY( sz03 == 0 );
63f54259 147#endif
b9e8095b 148
149 sz03 = str02.size() + 5;
150 str02.resize(sz03);
151 sz04 = str02.size();
43a8c05a 152 VERIFY( sz03 == sz04 );
b9e8095b 153
154 sz03 = str02.size() - 5;
155 str02.resize(sz03);
156 sz04 = str02.size();
43a8c05a 157 VERIFY( sz03 == sz04 );
b9e8095b 158
159 A<B> inst_obj;
160 std::basic_string<A<B> > str07(30, inst_obj);
161 std::basic_string<A<B> > str08 = str07;
162 str07 = str08 + str07;
43a8c05a 163 VERIFY( str07.capacity() >= str07.size() );
164 VERIFY( str08.capacity() >= str08.size() );
b9e8095b 165
4aca39bb 166 // non-POD types: size, length, max_size, clear(), empty()
167 bool b01 = str02.empty();
43a8c05a 168 VERIFY( b01 == true );
b9e8095b 169 sz03 = str02.size();
170 sz04 = str02.length();
43a8c05a 171 VERIFY( sz03 == sz04 );
b9e8095b 172 str02.c_str();
173 sz03 = str02.size();
174 sz04 = str02.length();
43a8c05a 175 VERIFY( sz03 == sz04 );
b9e8095b 176
177 sz03 = str02.max_size();
43a8c05a 178 VERIFY( sz03 >= sz04 );
b9e8095b 179
180 sz03 = str02.size();
181 str02.clear();
182 b01 = str02.empty();
43a8c05a 183 VERIFY( b01 == true );
b9e8095b 184 sz04 = str02.size();
43a8c05a 185 VERIFY( sz03 >= sz04 );
b9e8095b 186}
187
4e61a204 188#if !__GXX_WEAK__
4cfc8a1b 189// Explicitly instantiate for systems with no COMDAT or weak support.
190template
66d1b916 191 const std::basic_string< A<B> >::size_type
4cfc8a1b 192 std::basic_string< A<B> >::_Rep::_S_max_size;
193
194template
66d1b916 195 const A<B>
4cfc8a1b 196 std::basic_string< A<B> >::_Rep::_S_terminal;
4e61a204 197#endif
c6b98516 198
b9e8095b 199int main()
200{
201 test01();
b9e8095b 202 return 0;
203}