wchar_t.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // { dg-require-iconv "UCS-2BE" }
  2. // { dg-require-iconv "UCS-4BE" }
  3. // 2000-08-23 Benjamin Kosnik <bkoz@cygnus.com>
  4. // Copyright (C) 2000-2022 Free Software Foundation, Inc.
  5. //
  6. // This file is part of the GNU ISO C++ Library. This library is free
  7. // software; you can redistribute it and/or modify it under the
  8. // terms of the GNU General Public License as published by the
  9. // Free Software Foundation; either version 3, or (at your option)
  10. // any later version.
  11. // This library is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. // You should have received a copy of the GNU General Public License along
  16. // with this library; see the file COPYING3. If not see
  17. // <http://www.gnu.org/licenses/>.
  18. // 22.2.1.5 - Template class codecvt [lib.locale.codecvt]
  19. #include <locale>
  20. #include <testsuite_hooks.h>
  21. #include <ext/codecvt_specializations.h>
  22. // Partial specialization using encoding_state.
  23. // codecvt<unicode_t, wchar_t, encoding_state>
  24. void test01()
  25. {
  26. using namespace std;
  27. typedef codecvt_base::result result;
  28. typedef unsigned short int_type;
  29. typedef wchar_t ext_type;
  30. typedef __gnu_cxx::encoding_state state_type;
  31. typedef codecvt<int_type, ext_type, state_type> unicode_codecvt;
  32. typedef char_traits<int_type> int_traits;
  33. typedef char_traits<ext_type> ext_traits;
  34. int size = 23;
  35. char e_lit_base[96] __attribute__((aligned(__alignof__(ext_type)))) =
  36. {
  37. char(0x00), char(0x00), char(0x00), char(0x62), char(0x00), char(0x00),
  38. char(0x00), char(0x6c), char(0x00), char(0x00), char(0x00), char(0x61),
  39. char(0x00), char(0x00), char(0x00), char(0x63), char(0x00), char(0x00),
  40. char(0x00), char(0x6b), char(0x00), char(0x00), char(0x00), char(0x20),
  41. char(0x00), char(0x00), char(0x00), char(0x70), char(0x00), char(0x00),
  42. char(0x00), char(0x65), char(0x00), char(0x00), char(0x00), char(0x61),
  43. char(0x00), char(0x00), char(0x00), char(0x72), char(0x00), char(0x00),
  44. char(0x00), char(0x6c), char(0x00), char(0x00), char(0x00), char(0x20),
  45. char(0x00), char(0x00), char(0x00), char(0x6a), char(0x00), char(0x00),
  46. char(0x00), char(0x61), char(0x00), char(0x00), char(0x00), char(0x73),
  47. char(0x00), char(0x00), char(0x00), char(0x6d), char(0x00), char(0x00),
  48. char(0x00), char(0x69), char(0x00), char(0x00), char(0x00), char(0x6e),
  49. char(0x00), char(0x00), char(0x00), char(0x65), char(0x00), char(0x00),
  50. char(0x00), char(0x20), char(0x00), char(0x00), char(0x00), char(0x74),
  51. char(0x00), char(0x00), char(0x00), char(0x65), char(0x00), char(0x00),
  52. char(0x00), char(0x61), char(0x00), char(0x00), char(0x00), char(0xa0)
  53. };
  54. const ext_type* e_lit = reinterpret_cast<ext_type*>(e_lit_base);
  55. char i_lit_base[48] __attribute__((aligned(__alignof__(int_type)))) =
  56. {
  57. char(0x00), char(0x62), char(0x00), char(0x6c), char(0x00), char(0x61),
  58. char(0x00), char(0x63), char(0x00), char(0x6b), char(0x00), char(0x20),
  59. char(0x00), char(0x70), char(0x00), char(0x65), char(0x00), char(0x61),
  60. char(0x00), char(0x72), char(0x00), char(0x6c), char(0x00), char(0x20),
  61. char(0x00), char(0x6a), char(0x00), char(0x61), char(0x00), char(0x73),
  62. char(0x00), char(0x6d), char(0x00), char(0x69), char(0x00), char(0x6e),
  63. char(0x00), char(0x65), char(0x00), char(0x20), char(0x00), char(0x74),
  64. char(0x00), char(0x65), char(0x00), char(0x61), char(0x00), char(0xa0)
  65. };
  66. const int_type* i_lit = reinterpret_cast<int_type*>(i_lit_base);
  67. const ext_type* efrom_next;
  68. const int_type* ifrom_next;
  69. ext_type* e_arr = new ext_type[size + 1];
  70. ext_type* eto_next;
  71. int_type* i_arr = new int_type[size + 1];
  72. int_type* ito_next;
  73. // construct a locale object with the specialized facet.
  74. locale loc(locale::classic(), new unicode_codecvt);
  75. // sanity check the constructed locale has the specialized facet.
  76. VERIFY( has_facet<unicode_codecvt>(loc) );
  77. const unicode_codecvt& cvt = use_facet<unicode_codecvt>(loc);
  78. // in
  79. // unicode_codecvt::state_type state01("UCS-2BE", "UCS-4BE", 0xfeff, 0);
  80. unicode_codecvt::state_type state01("UCS-2BE", "UCS-4BE", 0, 0);
  81. result r1 = cvt.in(state01, e_lit, e_lit + size, efrom_next,
  82. i_arr, i_arr + size + 1, ito_next);
  83. VERIFY( r1 == codecvt_base::ok );
  84. VERIFY( !int_traits::compare(i_arr, i_lit, size) );
  85. VERIFY( efrom_next == e_lit + size );
  86. VERIFY( ito_next == i_arr + size );
  87. // out
  88. unicode_codecvt::state_type state02("UCS-2BE", "UCS-4BE", 0, 0);
  89. result r2 = cvt.out(state02, i_lit, i_lit + size, ifrom_next,
  90. e_arr, e_arr + size, eto_next);
  91. VERIFY( r2 == codecvt_base::ok ); // XXX?
  92. VERIFY( !ext_traits::compare(e_arr, e_lit, size) );
  93. VERIFY( ifrom_next == i_lit + size );
  94. VERIFY( eto_next == e_arr + size );
  95. // unshift
  96. ext_traits::copy(e_arr, e_lit, size);
  97. unicode_codecvt::state_type state03("UCS-2BE", "UCS-4BE", 0, 0);
  98. result r3 = cvt.unshift(state03, e_arr, e_arr + size, eto_next);
  99. VERIFY( r3 == codecvt_base::noconv );
  100. VERIFY( !ext_traits::compare(e_arr, e_lit, size) );
  101. VERIFY( eto_next == e_arr );
  102. int i = cvt.encoding();
  103. VERIFY( i == 0 );
  104. VERIFY( !cvt.always_noconv() );
  105. unicode_codecvt::state_type state04("UCS-2BE", "UCS-4BE", 0, 0);
  106. int j = cvt.length(state03, e_lit, e_lit + size, 5);
  107. VERIFY( j == 5 );
  108. int k = cvt.max_length();
  109. VERIFY( k == 1 );
  110. delete [] e_arr;
  111. delete [] i_arr;
  112. }
  113. int main ()
  114. {
  115. test01();
  116. return 0;
  117. }