1.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // { dg-do run { target c++11 } }
  2. // Copyright (C) 2015-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  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. // You should have received a copy of the GNU General Public License along
  14. // with this library; see the file COPYING3. If not see
  15. // <http://www.gnu.org/licenses/>.
  16. // 22.3.3.2.3 Buffer conversions
  17. #include <locale>
  18. #include <sstream>
  19. #include <testsuite_hooks.h>
  20. #include <testsuite_common_types.h>
  21. template<typename Elem>
  22. struct cvt : std::codecvt<Elem, char, std::mbstate_t> { };
  23. template<typename Elem>
  24. using buf_conv = std::wbuffer_convert<cvt<Elem>, Elem>;
  25. using std::string;
  26. using std::wstring;
  27. void test01()
  28. {
  29. #ifdef _GLIBCXX_USE_WCHAR_T
  30. buf_conv<wchar_t> buf;
  31. std::stringbuf sbuf;
  32. VERIFY( buf.rdbuf() == nullptr );
  33. VERIFY( buf.rdbuf(&sbuf) == nullptr );
  34. VERIFY( buf.rdbuf() == &sbuf );
  35. VERIFY( buf.rdbuf(nullptr) == &sbuf );
  36. __gnu_test::implicitly_default_constructible test;
  37. test.operator()<buf_conv<wchar_t>>(); // P0935R0
  38. #endif
  39. }
  40. void test02()
  41. {
  42. std::stringbuf sbuf;
  43. buf_conv<char> buf(&sbuf); // noconv
  44. std::stringstream ss;
  45. ss.std::ios::rdbuf(&buf);
  46. string input = "King for a day...";
  47. ss << input << std::flush;
  48. string output = sbuf.str();
  49. VERIFY( input == output );
  50. }
  51. void test03()
  52. {
  53. #ifdef _GLIBCXX_USE_WCHAR_T
  54. std::stringbuf sbuf;
  55. buf_conv<wchar_t> buf(&sbuf);
  56. std::wstringstream ss;
  57. ss.std::wios::rdbuf(&buf);
  58. wstring input = L"Fool for a lifetime";
  59. ss << input << std::flush;
  60. string output = sbuf.str();
  61. VERIFY( output == "Fool for a lifetime" );
  62. #endif
  63. }
  64. int main()
  65. {
  66. test01();
  67. test02();
  68. test03();
  69. }