| 1 |
// 1999-07-08 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 |
// 21.3.5.2 basic_string::append |
| 21 |
|
| 22 |
#include <string> |
| 23 |
#include <stdexcept> |
| 24 |
#include <testsuite_hooks.h> |
| 25 |
|
| 26 |
void test01(void) |
| 27 |
{ |
| 28 |
typedef std::string::size_type csize_type; |
| 29 |
typedef std::string::const_reference cref; |
| 30 |
typedef std::string::reference ref; |
| 31 |
csize_type csz01; |
| 32 |
|
| 33 |
const char str_lit01[] = "point bolivar, texas"; |
| 34 |
const std::string str01(str_lit01); |
| 35 |
const std::string str02("corpus, "); |
| 36 |
const std::string str03; |
| 37 |
std::string str05; |
| 38 |
|
| 39 |
|
| 40 |
// string& append(const string&) |
| 41 |
str05 = str02; |
| 42 |
str05.append(str05); |
| 43 |
VERIFY( str05 == "corpus, corpus, " ); |
| 44 |
str05.append(str01); |
| 45 |
VERIFY( str05 == "corpus, corpus, point bolivar, texas" ); |
| 46 |
str05.append(str03); |
| 47 |
VERIFY( str05 == "corpus, corpus, point bolivar, texas" ); |
| 48 |
std::string str06; |
| 49 |
str06.append(str05); |
| 50 |
VERIFY( str06 == str05 ); |
| 51 |
|
| 52 |
|
| 53 |
// string& append(const string&, size_type pos, size_type n) |
| 54 |
str05.erase(); |
| 55 |
str06.erase(); |
| 56 |
csz01 = str03.size(); |
| 57 |
try { |
| 58 |
str06.append(str03, csz01 + 1, 0); |
| 59 |
VERIFY( false ); |
| 60 |
} |
| 61 |
catch(std::out_of_range& fail) { |
| 62 |
VERIFY( true ); |
| 63 |
} |
| 64 |
catch(...) { |
| 65 |
VERIFY( false ); |
| 66 |
} |
| 67 |
|
| 68 |
csz01 = str01.size(); |
| 69 |
try { |
| 70 |
str06.append(str01, csz01 + 1, 0); |
| 71 |
VERIFY( false ); |
| 72 |
} |
| 73 |
catch(std::out_of_range& fail) { |
| 74 |
VERIFY( true ); |
| 75 |
} |
| 76 |
catch(...) { |
| 77 |
VERIFY( false ); |
| 78 |
} |
| 79 |
|
| 80 |
str05 = str02; |
| 81 |
str05.append(str01, 0, std::string::npos); |
| 82 |
VERIFY( str05 == "corpus, point bolivar, texas" ); |
| 83 |
VERIFY( str05 != str02 ); |
| 84 |
|
| 85 |
str06 = str02; |
| 86 |
str06.append(str01, 15, std::string::npos); |
| 87 |
VERIFY( str06 == "corpus, texas" ); |
| 88 |
VERIFY( str02 != str06 ); |
| 89 |
|
| 90 |
|
| 91 |
// string& append(const char* s) |
| 92 |
str05.erase(); |
| 93 |
str06.erase(); |
| 94 |
str05.append(""); |
| 95 |
VERIFY( str05 == str03 ); |
| 96 |
|
| 97 |
str05.append(str_lit01); |
| 98 |
VERIFY( str05 == str01 ); |
| 99 |
|
| 100 |
str06 = str02; |
| 101 |
str06.append("corpus, "); |
| 102 |
VERIFY( str06 == "corpus, corpus, " ); |
| 103 |
|
| 104 |
|
| 105 |
// string& append(const char* s, size_type n) |
| 106 |
str05.erase(); |
| 107 |
str06.erase(); |
| 108 |
str05.append("", 0); |
| 109 |
VERIFY( str05.size() == 0 ); |
| 110 |
VERIFY( str05 == str03 ); |
| 111 |
|
| 112 |
str05.append(str_lit01, sizeof(str_lit01) - 1); |
| 113 |
VERIFY( str05 == str01 ); |
| 114 |
|
| 115 |
str06 = str02; |
| 116 |
str06.append("corpus, ", 6); |
| 117 |
VERIFY( str06 == "corpus, corpus" ); |
| 118 |
|
| 119 |
str06 = str02; |
| 120 |
str06.append("corpus, ", 9); // n=9 includes null terminator |
| 121 |
VERIFY( str06 != "corpus, corpus, " ); |
| 122 |
|
| 123 |
|
| 124 |
// string& append(size_type n, char c) |
| 125 |
str05.erase(); |
| 126 |
str06.erase(); |
| 127 |
str05.append(0, 'a'); |
| 128 |
VERIFY( str05 == str03 ); |
| 129 |
str06.append(8, '.'); |
| 130 |
VERIFY( str06 == "........" ); |
| 131 |
|
| 132 |
|
| 133 |
// template<typename InputIter> |
| 134 |
// string& append(InputIter first, InputIter last) |
| 135 |
str05.erase(); |
| 136 |
str06.erase(); |
| 137 |
str05.append(str03.begin(), str03.end()); |
| 138 |
VERIFY( str05 == str03 ); |
| 139 |
|
| 140 |
str06 = str02; |
| 141 |
str06.append(str01.begin(), str01.begin() + str01.find('r')); |
| 142 |
VERIFY( str06 == "corpus, point boliva" ); |
| 143 |
VERIFY( str06 != str01 ); |
| 144 |
VERIFY( str06 != str02 ); |
| 145 |
|
| 146 |
str05 = str01; |
| 147 |
str05.append(str05.begin(), str05.begin() + str05.find('r')); |
| 148 |
VERIFY( str05 == "point bolivar, texaspoint boliva" ); |
| 149 |
VERIFY( str05 != str01 ); |
| 150 |
} |
| 151 |
|
| 152 |
int main() |
| 153 |
{ |
| 154 |
test01(); |
| 155 |
return 0; |
| 156 |
} |