| 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 | // C++20 29.8.3.2 basic_istringstream constructors [istringstream.cons] |
| 19 | |
| 20 | // { dg-do run { target c++20 } } |
| 21 | // { dg-require-effective-target cxx11_abi } |
| 22 | |
| 23 | #include <sstream> |
| 24 | #include <string> |
| 25 | #include <testsuite_allocator.h> |
| 26 | #include <testsuite_hooks.h> |
| 27 | |
| 28 | void |
| 29 | test01() |
| 30 | { |
| 31 | std::istringstream::allocator_type a; |
| 32 | std::istringstream stm(std::ios_base::in, a); |
| 33 | } |
| 34 | |
| 35 | auto const cstr = "This is a test string"; |
| 36 | |
| 37 | void |
| 38 | test02() |
| 39 | { |
| 40 | std::string s1(cstr); |
| 41 | std::istringstream stm(std::move(s1)); |
| 42 | VERIFY( s1.empty() ); |
| 43 | |
| 44 | std::string s2(cstr); |
| 45 | VERIFY( stm.str() == s2 ); |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | test03() |
| 50 | { |
| 51 | using C = char; |
| 52 | using alloc_type = __gnu_test::uneq_allocator<C>; |
| 53 | using traits_type = std::char_traits<C>; |
| 54 | using string = std::basic_string<C, traits_type, alloc_type>; |
| 55 | using istringstream = std::basic_istringstream<C, traits_type, alloc_type>; |
| 56 | |
| 57 | auto const mode = std::ios_base::in; |
| 58 | alloc_type a1(1); |
| 59 | const string s1(cstr, a1); |
| 60 | |
| 61 | // basic_istringstream() |
| 62 | { |
| 63 | alloc_type a0; |
| 64 | istringstream ss; |
| 65 | VERIFY( ss.str().empty() ); |
| 66 | VERIFY( ss.rdbuf()->get_allocator() == a0 ); |
| 67 | VERIFY( ss.str().get_allocator() == a0 ); |
| 68 | } |
| 69 | |
| 70 | // basic_istringstream(openmode) |
| 71 | { |
| 72 | alloc_type a0; |
| 73 | istringstream ss(mode); |
| 74 | VERIFY( ss.str().empty() ); |
| 75 | VERIFY( ss.rdbuf()->get_allocator() == a0 ); |
| 76 | VERIFY( ss.str().get_allocator() == a0 ); |
| 77 | } |
| 78 | |
| 79 | // basic_istringstream(const basic_string<C,T,A>&, openmode = in) |
| 80 | { |
| 81 | istringstream ss(s1); |
| 82 | VERIFY( ss.str() == cstr ); |
| 83 | VERIFY( ss.rdbuf()->get_allocator() == a1 ); |
| 84 | VERIFY( ss.str().get_allocator() == a1 ); |
| 85 | } |
| 86 | |
| 87 | // basic_istringstream(const basic_string<C,T,A>&, openmode = in) |
| 88 | { |
| 89 | istringstream ss(s1, mode); |
| 90 | VERIFY( ss.str() == cstr ); |
| 91 | VERIFY( ss.rdbuf()->get_allocator() == a1 ); |
| 92 | VERIFY( ss.str().get_allocator() == a1 ); |
| 93 | } |
| 94 | |
| 95 | // basic_istringstream(openmode, const A&) |
| 96 | { |
| 97 | istringstream ss(mode, a1); |
| 98 | VERIFY( ss.str().empty() ); |
| 99 | VERIFY( ss.rdbuf()->get_allocator() == a1 ); |
| 100 | VERIFY( ss.str().get_allocator() == a1 ); |
| 101 | } |
| 102 | |
| 103 | // basic_istringstream(basic_string<C,T,A>&&, openmode = in) |
| 104 | { |
| 105 | istringstream ss(string{s1}); |
| 106 | VERIFY( ss.str() == s1 ); |
| 107 | VERIFY( ss.rdbuf()->get_allocator() == a1 ); |
| 108 | VERIFY( ss.str().get_allocator() == a1 ); |
| 109 | } |
| 110 | |
| 111 | // basic_istringstream(basic_string<C,T,A>&&, openmode = in) |
| 112 | { |
| 113 | istringstream ss(string(s1), mode); |
| 114 | VERIFY( ss.str() == s1 ); |
| 115 | VERIFY( ss.rdbuf()->get_allocator() == a1 ); |
| 116 | VERIFY( ss.str().get_allocator() == a1 ); |
| 117 | } |
| 118 | |
| 119 | // basic_istringstream(const basic_string<C,T,SA>&, const A&) |
| 120 | { |
| 121 | alloc_type a2(2); |
| 122 | istringstream ss(s1, a2); |
| 123 | VERIFY( ss.str() == s1 ); |
| 124 | VERIFY( ss.rdbuf()->get_allocator() == a2 ); |
| 125 | VERIFY( ss.str().get_allocator() == a2 ); |
| 126 | } |
| 127 | |
| 128 | // basic_istringstream(const basic_string<C,T,SA>&, const A&) |
| 129 | { |
| 130 | alloc_type a2(2); |
| 131 | const std::string s2 = cstr; |
| 132 | istringstream ss(s2, a2); |
| 133 | VERIFY( ss.str() == cstr ); |
| 134 | VERIFY( ss.rdbuf()->get_allocator() == a2 ); |
| 135 | VERIFY( ss.str().get_allocator() == a2 ); |
| 136 | } |
| 137 | |
| 138 | // basic_istringstream(const basic_string<C,T,SA>&, openmode, const A&) |
| 139 | { |
| 140 | alloc_type a2(2); |
| 141 | istringstream ss(s1, mode, a2); |
| 142 | VERIFY( ss.str() == s1 ); |
| 143 | VERIFY( ss.rdbuf()->get_allocator() == a2 ); |
| 144 | VERIFY( ss.str().get_allocator() == a2 ); |
| 145 | } |
| 146 | |
| 147 | // basic_istringstream(const basic_string<C,T,SA>&, openmode, const A&) |
| 148 | { |
| 149 | alloc_type a2(2); |
| 150 | const std::string s2 = cstr; |
| 151 | istringstream ss(s2, mode, a2); |
| 152 | VERIFY( ss.str() == cstr ); |
| 153 | VERIFY( ss.rdbuf()->get_allocator() == a2 ); |
| 154 | VERIFY( ss.str().get_allocator() == a2 ); |
| 155 | } |
| 156 | |
| 157 | // basic_istringstream(const basic_string<C,T,SA>&, openmode = in) |
| 158 | { |
| 159 | alloc_type a0; |
| 160 | const std::string s2 = cstr; |
| 161 | istringstream ss(s2, mode); |
| 162 | VERIFY( ss.str() == cstr ); |
| 163 | VERIFY( ss.rdbuf()->get_allocator() == a0 ); |
| 164 | VERIFY( ss.str().get_allocator() == a0 ); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // A minimal allocator with no default constructor |
| 169 | template<typename T> |
| 170 | struct NoDefaultCons : __gnu_test::SimpleAllocator<T> |
| 171 | { |
| 172 | using __gnu_test::SimpleAllocator<T>::SimpleAllocator; |
| 173 | |
| 174 | NoDefaultCons() = delete; |
| 175 | |
| 176 | NoDefaultCons(int) { } |
| 177 | }; |
| 178 | |
| 179 | void |
| 180 | test04() |
| 181 | { |
| 182 | using sstream = std::basic_istringstream<char, std::char_traits<char>, |
| 183 | NoDefaultCons<char>>; |
| 184 | |
| 185 | NoDefaultCons<char> a(1); |
| 186 | const std::string str(cstr); |
| 187 | |
| 188 | sstream ss1(str, a); |
| 189 | VERIFY( ss1.str() == cstr ); |
| 190 | VERIFY( ss1.get() == cstr[0] ); |
| 191 | |
| 192 | sstream ss2(str, std::ios::out, a); |
| 193 | VERIFY( ss2.str() == cstr ); |
| 194 | VERIFY( ss2.get() == cstr[0] ); |
| 195 | |
| 196 | sstream ss3(std::string(str), std::ios::out, a); |
| 197 | VERIFY( ss3.str() == cstr ); |
| 198 | VERIFY( ss3.get() == cstr[0] ); |
| 199 | } |
| 200 | |
| 201 | int |
| 202 | main() |
| 203 | { |
| 204 | test01(); |
| 205 | test02(); |
| 206 | test03(); |
| 207 | test04(); |
| 208 | } |