1.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.2 String conversions
  17. #include <locale>
  18. #include <string>
  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 str_conv = std::wstring_convert<cvt<Elem>, Elem>;
  25. using std::string;
  26. using std::wstring;
  27. void test01()
  28. {
  29. typedef str_conv<char> sc; // noconv
  30. sc c;
  31. string input = "King for a day...";
  32. string output = c.from_bytes(input);
  33. VERIFY( input == output );
  34. VERIFY( c.converted() == output.length() );
  35. string roundtrip = c.to_bytes(output);
  36. VERIFY( input == roundtrip );
  37. VERIFY( c.converted() == roundtrip.length() );
  38. __gnu_test::implicitly_default_constructible test;
  39. test.operator()<sc>(); // P0935R0
  40. }
  41. void test02()
  42. {
  43. #ifdef _GLIBCXX_USE_WCHAR_T
  44. typedef str_conv<wchar_t> wsc;
  45. wsc c;
  46. string input = "Fool for a lifetime";
  47. wstring output = c.from_bytes(input);
  48. VERIFY( c.converted() == output.length() );
  49. VERIFY( L"Fool for a lifetime" == output );
  50. string roundtrip = c.to_bytes(output);
  51. VERIFY( input == roundtrip );
  52. VERIFY( c.converted() == roundtrip.length() );
  53. VERIFY( c.from_bytes(input[0]) == output.substr(0, 1) );
  54. VERIFY( c.from_bytes(input.c_str()) == output );
  55. VERIFY( c.from_bytes(input.data(), input.data()+input.size()) == output );
  56. VERIFY( c.to_bytes(output[0]) == input.substr(0, 1) );
  57. VERIFY( c.to_bytes(output.c_str()) == input );
  58. VERIFY( c.to_bytes(output.data(), output.data()+output.size()) == input );
  59. __gnu_test::implicitly_default_constructible test;
  60. test.operator()<wsc>(); // P0935R0
  61. #endif
  62. }
  63. int main()
  64. {
  65. test01();
  66. test02();
  67. }