utf8-char8_t.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (C) 2015-2022 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. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. // You should have received a copy of the GNU General Public License along
  13. // with this library; see the file COPYING3. If not see
  14. // <http://www.gnu.org/licenses/>.
  15. // { dg-do run { target c++11 } }
  16. // { dg-require-cstdint "" }
  17. // { dg-options "-fchar8_t" }
  18. #include <locale>
  19. #include <iterator>
  20. #include <string>
  21. #include <testsuite_hooks.h>
  22. const char8_t expected[] = u8"£¥€";
  23. const std::size_t expected_len = std::char_traits<char8_t>::length(expected);
  24. template<typename C>
  25. void test(const C* from)
  26. {
  27. auto len = std::char_traits<C>::length(from);
  28. std::mbstate_t state{};
  29. char8_t buf[16] = { };
  30. using test_type = std::codecvt<C, char8_t, std::mbstate_t>;
  31. const test_type& cvt = std::use_facet<test_type>(std::locale::classic());
  32. auto from_end = from + len;
  33. auto from_next = from;
  34. auto buf_end = std::end(buf);
  35. auto buf_next = buf;
  36. auto res = cvt.out(state, from, from_end, from_next, buf, buf_end, buf_next);
  37. VERIFY( res == std::codecvt_base::ok );
  38. VERIFY( from_next == from_end );
  39. VERIFY( (buf_next - buf) == expected_len );
  40. VERIFY( 0 == std::char_traits<char8_t>::compare(buf, expected, expected_len) );
  41. C buf2[16];
  42. auto exp_end = expected + expected_len;
  43. auto exp_next = expected;
  44. auto buf2_end = std::end(buf2);
  45. auto buf2_next = buf2;
  46. res = cvt.in(state, expected, exp_end, exp_next, buf2, buf2_end, buf2_next);
  47. VERIFY( res == std::codecvt_base::ok );
  48. VERIFY( exp_next == exp_end );
  49. VERIFY( (buf2_next - buf2) == len );
  50. VERIFY( 0 == std::char_traits<C>::compare(buf2, from, len) );
  51. }
  52. void
  53. test01()
  54. {
  55. test(u"£¥€");
  56. }
  57. void
  58. test02()
  59. {
  60. test(U"£¥€");
  61. }
  62. int
  63. main()
  64. {
  65. test01();
  66. test02();
  67. }