| 1 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // 29.8.5.4 basic_stringstream member functions [stringstream.members] |
| 19 | |
| 20 | // { dg-do run { target c++20 } } |
| 21 | // { dg-require-effective-target cxx11_abi } |
| 22 | |
| 23 | #include <sstream> |
| 24 | #include <testsuite_hooks.h> |
| 25 | #include <testsuite_allocator.h> |
| 26 | |
| 27 | void test01() |
| 28 | { |
| 29 | const std::string s0 = "this is not a short string"; |
| 30 | std::stringstream ss; |
| 31 | ss.str(s0); |
| 32 | VERIFY( ss.str() == s0 ); |
| 33 | VERIFY( ss.str() == s0 ); |
| 34 | |
| 35 | using Alloc = __gnu_test::uneq_allocator<char>; |
| 36 | const Alloc a1(1); |
| 37 | std::basic_string<char, std::char_traits<char>, Alloc> s1 = ss.str(a1); |
| 38 | VERIFY( s1.get_allocator() == a1 ); |
| 39 | VERIFY( ss.str(a1).get_allocator() == a1 ); |
| 40 | VERIFY( ss.str(a1) == s1 ); |
| 41 | VERIFY( std::move(ss).str(a1) == s1 ); |
| 42 | VERIFY( std::move(ss).str(a1) == s1 ); |
| 43 | |
| 44 | const Alloc a2(2); |
| 45 | VERIFY( ss.str(a2).get_allocator() == a2 ); |
| 46 | VERIFY( ss.str(a2) == s1 ); |
| 47 | |
| 48 | VERIFY( std::move(ss).str() == s0 ); |
| 49 | VERIFY( std::move(ss).str().empty() ); |
| 50 | VERIFY( ss.str().empty() ); |
| 51 | VERIFY( ss.str(a1).empty() ); |
| 52 | } |
| 53 | |
| 54 | void test02() |
| 55 | { |
| 56 | std::stringstream ss("123"); |
| 57 | std::string str = "ABCDEF"; |
| 58 | ss << str; |
| 59 | VERIFY( ss.str() == str ); |
| 60 | VERIFY( std::move(ss).str() == str ); |
| 61 | VERIFY( std::move(ss).str().empty() ); |
| 62 | } |
| 63 | |
| 64 | void test03() |
| 65 | { |
| 66 | std::stringstream ss; |
| 67 | using Alloc = __gnu_test::tracker_allocator<char>; |
| 68 | using Str = std::basic_string<char, std::char_traits<char>, Alloc>; |
| 69 | Str s1 = "string that is not short, quite long even"; |
| 70 | auto count1 = __gnu_test::tracker_allocator_counter::get_allocation_count(); |
| 71 | ss.str(s1); |
| 72 | auto count2 = __gnu_test::tracker_allocator_counter::get_allocation_count(); |
| 73 | VERIFY( count1 == count2 ); |
| 74 | VERIFY( ss.str() == s1.c_str() ); |
| 75 | } |
| 76 | |
| 77 | void test04() |
| 78 | { |
| 79 | std::stringstream ss; |
| 80 | const std::string str = "Another quite long string, not at all short"; |
| 81 | std::string str2 = str; |
| 82 | ss.str(std::move(str2)); |
| 83 | VERIFY( str2.empty() ); |
| 84 | VERIFY( ss.str() == str ); |
| 85 | } |
| 86 | |
| 87 | int main() |
| 88 | { |
| 89 | test01(); |
| 90 | test02(); |
| 91 | test03(); |
| 92 | test04(); |
| 93 | } |