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