| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | // Copyright (C) 2015-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 | // 22.3.3.2.3 Buffer conversions |
| 21 | |
| 22 | #include <locale> |
| 23 | #include <sstream> |
| 24 | #include <testsuite_hooks.h> |
| 25 | #include <testsuite_common_types.h> |
| 26 | |
| 27 | template<typename Elem> |
| 28 | struct cvt : std::codecvt<Elem, char, std::mbstate_t> { }; |
| 29 | |
| 30 | template<typename Elem> |
| 31 | using buf_conv = std::wbuffer_convert<cvt<Elem>, Elem>; |
| 32 | // { dg-warning "deprecated" "" { target c++17 } 31 } |
| 33 | |
| 34 | using std::string; |
| 35 | using std::wstring; |
| 36 | |
| 37 | void test01() |
| 38 | { |
| 39 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 40 | buf_conv<wchar_t> buf; |
| 41 | std::stringbuf sbuf; |
| 42 | VERIFY( buf.rdbuf() == nullptr ); |
| 43 | VERIFY( buf.rdbuf(&sbuf) == nullptr ); |
| 44 | VERIFY( buf.rdbuf() == &sbuf ); |
| 45 | VERIFY( buf.rdbuf(nullptr) == &sbuf ); |
| 46 | |
| 47 | __gnu_test::implicitly_default_constructible test; |
| 48 | test.operator()<buf_conv<wchar_t>>(); // P0935R0 |
| 49 | #endif |
| 50 | } |
| 51 | |
| 52 | void test02() |
| 53 | { |
| 54 | std::stringbuf sbuf; |
| 55 | buf_conv<char> buf(&sbuf); // noconv |
| 56 | |
| 57 | std::stringstream ss; |
| 58 | ss.std::ios::rdbuf(&buf); |
| 59 | string input = "King for a day..."; |
| 60 | ss << input << std::flush; |
| 61 | string output = sbuf.str(); |
| 62 | VERIFY( input == output ); |
| 63 | } |
| 64 | |
| 65 | void test03() |
| 66 | { |
| 67 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 68 | std::stringbuf sbuf; |
| 69 | buf_conv<wchar_t> buf(&sbuf); |
| 70 | |
| 71 | std::wstringstream ss; |
| 72 | ss.std::wios::rdbuf(&buf); |
| 73 | wstring input = L"Fool for a lifetime"; |
| 74 | ss << input << std::flush; |
| 75 | string output = sbuf.str(); |
| 76 | VERIFY( output == "Fool for a lifetime" ); |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | int main() |
| 81 | { |
| 82 | test01(); |
| 83 | test02(); |
| 84 | test03(); |
| 85 | } |