utf8.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include <locale>
  17. #include <iterator>
  18. #include <string>
  19. #include <testsuite_hooks.h>
  20. const auto expected = (const char*)u8"£¥€";
  21. const std::size_t expected_len = std::char_traits<char>::length(expected);
  22. template<typename C>
  23. void test(const C* from)
  24. {
  25. auto len = std::char_traits<C>::length(from);
  26. std::mbstate_t state{};
  27. char buf[16] = { };
  28. using test_type = std::codecvt<C, char, std::mbstate_t>;
  29. const test_type& cvt = std::use_facet<test_type>(std::locale::classic());
  30. auto from_end = from + len;
  31. auto from_next = from;
  32. auto buf_end = std::end(buf);
  33. auto buf_next = buf;
  34. auto res = cvt.out(state, from, from_end, from_next, buf, buf_end, buf_next);
  35. VERIFY( res == std::codecvt_base::ok );
  36. VERIFY( from_next == from_end );
  37. VERIFY( (buf_next - buf) == expected_len );
  38. VERIFY( 0 == std::char_traits<char>::compare(buf, expected, expected_len) );
  39. C buf2[16];
  40. auto exp_end = expected + expected_len;
  41. auto exp_next = expected;
  42. auto buf2_end = std::end(buf2);
  43. auto buf2_next = buf2;
  44. res = cvt.in(state, expected, exp_end, exp_next, buf2, buf2_end, buf2_next);
  45. VERIFY( res == std::codecvt_base::ok );
  46. VERIFY( exp_next == exp_end );
  47. VERIFY( (buf2_next - buf2) == len );
  48. VERIFY( 0 == std::char_traits<C>::compare(buf2, from, len) );
  49. }
  50. void
  51. test01()
  52. {
  53. test(u"£¥€");
  54. }
  55. void
  56. test02()
  57. {
  58. test(U"£¥€");
  59. }
  60. int
  61. main()
  62. {
  63. test01();
  64. test02();
  65. }