char16_t.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // [locale.codecvt], C++11 22.4.1.4. specialization.
  17. #include <locale>
  18. #include <cstring>
  19. #include <testsuite_hooks.h>
  20. void
  21. test01()
  22. {
  23. using namespace std;
  24. typedef codecvt<char16_t, char, mbstate_t> codecvt_c16;
  25. locale loc_c = locale::classic();
  26. VERIFY(has_facet<codecvt_c16>(loc_c));
  27. const codecvt_c16* const cvt = &use_facet<codecvt_c16>(loc_c);
  28. VERIFY(!cvt->always_noconv());
  29. VERIFY(cvt->max_length() == 4);
  30. VERIFY(cvt->encoding() == 0);
  31. #ifndef _GLIBCXX_USE_CHAR8_T
  32. using char8_t = char;
  33. #endif
  34. const char8_t u8dat_[] = u8"H\U000000E4ll\U000000F6 \U0001F63F \U000056FD "
  35. u8"\U0000222B f(\U000003BA) exp(-2\U000003C0\U000003C9) d\U000003BA "
  36. u8"\U0001F6BF \U0001F6BF \U0001F648 \U00000413\U00000435\U0000043E"
  37. u8"\U00000433\U00000440\U00000430\U00000444\U00000438\U0000044F \U0000FB05";
  38. const char* const u8dat = (const char*)u8dat_;
  39. const char* const u8dat_end = (const char*)std::end(u8dat_);
  40. const char16_t u16dat[] = u"H\U000000E4ll\U000000F6 \U0001F63F \U000056FD "
  41. u"\U0000222B f(\U000003BA) exp(-2\U000003C0\U000003C9) d\U000003BA "
  42. u"\U0001F6BF \U0001F6BF \U0001F648 \U00000413\U00000435\U0000043E"
  43. u"\U00000433\U00000440\U00000430\U00000444\U00000438\U0000044F \U0000FB05";
  44. const char16_t* const u16dat_end = std::end(u16dat);
  45. {
  46. const size_t len = u16dat_end - u16dat + 1;
  47. char16_t* const buffer = new char16_t[len];
  48. char16_t* const buffer_end = buffer + len;
  49. const char* from_next;
  50. char16_t* to_next;
  51. codecvt_c16::state_type state01;
  52. state01 = {};
  53. codecvt_base::result res = cvt->in(state01, u8dat, u8dat_end, from_next,
  54. buffer, buffer_end, to_next);
  55. VERIFY(res == codecvt_base::ok);
  56. VERIFY(from_next == u8dat_end);
  57. VERIFY(std::memcmp((void*)buffer, (void*)u16dat, sizeof(u16dat)) == 0);
  58. delete[] buffer;
  59. }
  60. {
  61. const size_t len = u8dat_end - u8dat + 1;
  62. char* const buffer = new char[len];
  63. char* const buffer_end = buffer + len;
  64. const char16_t* from_next;
  65. char* to_next;
  66. codecvt_c16::state_type state01;
  67. state01 = {};
  68. codecvt_base::result res = cvt->out(state01, u16dat, u16dat_end, from_next,
  69. buffer, buffer_end, to_next);
  70. VERIFY(res == codecvt_base::ok);
  71. VERIFY(from_next == u16dat_end);
  72. VERIFY(std::memcmp((void*)buffer, (void*)u8dat_, sizeof(u8dat_)) == 0);
  73. delete[] buffer;
  74. }
  75. }
  76. int
  77. main()
  78. {
  79. test01();
  80. }