2.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. template<typename Elem>
  21. struct cvt : std::codecvt<Elem, char, std::mbstate_t> { };
  22. template<typename Elem>
  23. using str_conv = std::wstring_convert<cvt<Elem>, Elem>;
  24. using std::string;
  25. using std::u16string;
  26. using std::u32string;
  27. // test conversion errors, with and without error strings
  28. void test01()
  29. {
  30. typedef str_conv<char> sc;
  31. const sc::byte_string berr = "invalid wide string";
  32. const sc::wide_string werr = "invalid byte string";
  33. sc c(berr, werr);
  34. string input = "Stop";
  35. input += char(0xFF);
  36. string woutput = c.from_bytes(input);
  37. VERIFY( input == woutput ); // noconv case doesn't detect invalid input
  38. string winput = "Stop";
  39. winput += char(0xFF);
  40. string output = c.to_bytes(winput);
  41. VERIFY( winput == output ); // noconv case doesn't detect invalid input
  42. }
  43. void test02()
  44. {
  45. typedef str_conv<char16_t> sc;
  46. const sc::byte_string berr = "invalid wide string";
  47. const sc::wide_string werr = u"invalid byte string";
  48. sc c(berr, werr);
  49. string input = "Stop";
  50. input += char(0xFF);
  51. u16string woutput = c.from_bytes(input);
  52. VERIFY( werr == woutput );
  53. u16string winput = u"Stop";
  54. winput += char16_t(0xDC00);
  55. string output = c.to_bytes(winput);
  56. VERIFY( berr == output );
  57. }
  58. void test03()
  59. {
  60. typedef str_conv<char32_t> sc;
  61. const sc::byte_string berr = "invalid wide string";
  62. const sc::wide_string werr = U"invalid byte string";
  63. sc c(berr, werr);
  64. string input = "Halt";
  65. input += char(0xff);
  66. u32string woutput = c.from_bytes(input);
  67. VERIFY( werr == woutput );
  68. u32string winput = U"Halt";
  69. winput += char32_t(-1);
  70. string output = c.to_bytes(winput);
  71. VERIFY( berr == output );
  72. }
  73. int main()
  74. {
  75. test01();
  76. test02();
  77. test03();
  78. }