| 1 | // 1999-05-11 bkoz |
| 2 | |
| 3 | // Copyright (C) 1999-2025 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 -Wno-stringop-overread" } |
| 21 | |
| 22 | // 21.3.3 string capacity |
| 23 | |
| 24 | #include <string> |
| 25 | #include <cstring> |
| 26 | #include <testsuite_hooks.h> |
| 27 | |
| 28 | template<typename T> |
| 29 | struct A { }; |
| 30 | |
| 31 | template<typename T> |
| 32 | bool |
| 33 | operator==(const A<T>&, const A<T>&) { return true; } |
| 34 | |
| 35 | template<typename T> |
| 36 | bool |
| 37 | operator<(const A<T>&, const A<T>&) { return true; } |
| 38 | |
| 39 | struct B { }; |
| 40 | |
| 41 | // char_traits specialization |
| 42 | namespace 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 |
| 109 | to_char_type(const int_type&) |
| 110 | { return char_type(); } |
| 111 | |
| 112 | static int_type |
| 113 | to_int_type(const char_type&) { return int_type(); } |
| 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 | |
| 128 | void test01() |
| 129 | { |
| 130 | // non POD types : resize, capacity, reserve |
| 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(); |
| 139 | VERIFY( sz04 >= sz03 ); |
| 140 | VERIFY( sz04 >= 100 ); |
| 141 | #if __cplusplus <= 201703L |
| 142 | str02.reserve(); |
| 143 | #else |
| 144 | str02.shrink_to_fit(); // reserve is deprecated in C++20 |
| 145 | #endif |
| 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 ); |
| 154 | |
| 155 | sz03 = str02.size() + 5; |
| 156 | str02.resize(sz03); |
| 157 | sz04 = str02.size(); |
| 158 | VERIFY( sz03 == sz04 ); |
| 159 | |
| 160 | sz03 = str02.size() - 5; |
| 161 | str02.resize(sz03); |
| 162 | sz04 = str02.size(); |
| 163 | VERIFY( sz03 == sz04 ); |
| 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; |
| 169 | VERIFY( str07.capacity() >= str07.size() ); |
| 170 | VERIFY( str08.capacity() >= str08.size() ); |
| 171 | |
| 172 | // non-POD types: size, length, max_size, clear(), empty() |
| 173 | bool b01 = str02.empty(); |
| 174 | VERIFY( b01 == true ); |
| 175 | sz03 = str02.size(); |
| 176 | sz04 = str02.length(); |
| 177 | VERIFY( sz03 == sz04 ); |
| 178 | (void) str02.c_str(); |
| 179 | sz03 = str02.size(); |
| 180 | sz04 = str02.length(); |
| 181 | VERIFY( sz03 == sz04 ); |
| 182 | |
| 183 | sz03 = str02.max_size(); |
| 184 | VERIFY( sz03 >= sz04 ); |
| 185 | |
| 186 | sz03 = str02.size(); |
| 187 | str02.clear(); |
| 188 | b01 = str02.empty(); |
| 189 | VERIFY( b01 == true ); |
| 190 | sz04 = str02.size(); |
| 191 | VERIFY( sz03 >= sz04 ); |
| 192 | } |
| 193 | |
| 194 | #if !__GXX_WEAK__ |
| 195 | // Explicitly instantiate for systems with no COMDAT or weak support. |
| 196 | template |
| 197 | const std::basic_string< A<B> >::size_type |
| 198 | std::basic_string< A<B> >::_Rep::_S_max_size; |
| 199 | |
| 200 | template |
| 201 | const A<B> |
| 202 | std::basic_string< A<B> >::_Rep::_S_terminal; |
| 203 | #endif |
| 204 | |
| 205 | int main() |
| 206 | { |
| 207 | test01(); |
| 208 | return 0; |
| 209 | } |