| 1 | // 1999-06-03 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.4 basic_string::insert |
| 21 | |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | #ifdef _GLIBCXX_DEBUG |
| 25 | #include <debug/string> |
| 26 | using namespace __gnu_debug; |
| 27 | #else |
| 28 | #include <string> |
| 29 | using namespace std; |
| 30 | #endif |
| 31 | |
| 32 | // More |
| 33 | // string& insert(size_type __p, const char* s, size_type n); |
| 34 | // string& insert(size_type __p, const char* s); |
| 35 | // but now s points inside the _Rep |
| 36 | void test02(void) |
| 37 | { |
| 38 | string str01; |
| 39 | const char* title = "Everything was beautiful, and nothing hurt"; |
| 40 | // Increasing size: str01 is reallocated every time. |
| 41 | str01 = title; |
| 42 | str01.insert(0, str01.c_str() + str01.size() - 4, 4); |
| 43 | VERIFY( str01 == "hurtEverything was beautiful, and nothing hurt" ); |
| 44 | str01 = title; |
| 45 | str01.insert(0, str01.c_str(), 5); |
| 46 | VERIFY( str01 == "EveryEverything was beautiful, and nothing hurt" ); |
| 47 | str01 = title; |
| 48 | str01.insert(10, str01.c_str() + 4, 6); |
| 49 | VERIFY( str01 == "Everythingything was beautiful, and nothing hurt" ); |
| 50 | str01 = title; |
| 51 | str01.insert(15, str01.c_str(), 10); |
| 52 | VERIFY( str01 == "Everything was Everythingbeautiful, and nothing hurt" ); |
| 53 | str01 = title; |
| 54 | str01.insert(15, str01.c_str() + 11, 13); |
| 55 | VERIFY( str01 == "Everything was was beautifulbeautiful, and nothing hurt" ); |
| 56 | str01 = title; |
| 57 | str01.insert(0, str01.c_str()); |
| 58 | VERIFY( str01 == "Everything was beautiful, and nothing hurt" |
| 59 | "Everything was beautiful, and nothing hurt"); |
| 60 | // Again: no reallocations. |
| 61 | str01 = title; |
| 62 | str01.insert(0, str01.c_str() + str01.size() - 4, 4); |
| 63 | VERIFY( str01 == "hurtEverything was beautiful, and nothing hurt" ); |
| 64 | str01 = title; |
| 65 | str01.insert(0, str01.c_str(), 5); |
| 66 | VERIFY( str01 == "EveryEverything was beautiful, and nothing hurt" ); |
| 67 | str01 = title; |
| 68 | str01.insert(10, str01.c_str() + 4, 6); |
| 69 | VERIFY( str01 == "Everythingything was beautiful, and nothing hurt" ); |
| 70 | str01 = title; |
| 71 | str01.insert(15, str01.c_str(), 10); |
| 72 | VERIFY( str01 == "Everything was Everythingbeautiful, and nothing hurt" ); |
| 73 | str01 = title; |
| 74 | str01.insert(15, str01.c_str() + 11, 13); |
| 75 | VERIFY( str01 == "Everything was was beautifulbeautiful, and nothing hurt" ); |
| 76 | str01 = title; |
| 77 | str01.insert(0, str01.c_str()); |
| 78 | VERIFY( str01 == "Everything was beautiful, and nothing hurt" |
| 79 | "Everything was beautiful, and nothing hurt"); |
| 80 | } |
| 81 | |
| 82 | int main() |
| 83 | { |
| 84 | test02(); |
| 85 | return 0; |
| 86 | } |