1 // 2000-08-23 Benjamin Kosnik <bkoz@cygnus.com>
3 // Copyright (C) 2000, 2001, 2002 Free Software Foundation
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // 22.2.1.5 - Template class codecvt [lib.locale.codecvt]
24 #include <testsuite_hooks.h>
26 #ifdef _GLIBCPP_USE___ENC_TRAITS
27 #ifdef _GLIBCPP_USE_WCHAR_T
29 // Need some char_traits specializations for this to work.
30 typedef unsigned short unicode_t;
35 struct char_traits<unicode_t>
37 typedef unicode_t char_type;
38 // Unsigned as wint_t is unsigned.
39 typedef unsigned long int_type;
40 typedef streampos pos_type;
41 typedef streamoff off_type;
42 typedef mbstate_t state_type;
45 assign(char_type& __c1, const char_type& __c2);
48 eq(const char_type& __c1, const char_type& __c2);
51 lt(const char_type& __c1, const char_type& __c2);
54 compare(const char_type* __s1, const char_type* __s2, size_t __n)
55 { return memcmp(__s1, __s2, __n); }
58 length(const char_type* __s);
60 static const char_type*
61 find(const char_type* __s, size_t __n, const char_type& __a);
64 move(char_type* __s1, const char_type* __s2, size_t __n);
67 copy(char_type* __s1, const char_type* __s2, size_t __n)
68 { return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
71 assign(char_type* __s, size_t __n, char_type __a);
74 to_char_type(const int_type& __c);
77 to_int_type(const char_type& __c);
80 eq_int_type(const int_type& __c1, const int_type& __c2);
86 not_eof(const int_type& __c);
91 initialize_state(std::__enc_traits& state)
94 // Partial specialization using __enc_traits.
95 // codecvt<unicode_t, wchar_t, __enc_traits>
99 typedef codecvt_base::result result;
100 typedef unicode_t int_type;
101 typedef wchar_t ext_type;
102 typedef __enc_traits enc_type;
103 typedef codecvt<int_type, ext_type, enc_type> unicode_codecvt;
104 typedef char_traits<int_type> int_traits;
105 typedef char_traits<ext_type> ext_traits;
109 char e_lit_base[96] __attribute__((aligned(__alignof__(ext_type)))) =
111 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x61,
112 0x00, 0x00, 0x00, 0x63, 0x00, 0x00, 0x00, 0x6b, 0x00, 0x00, 0x00, 0x20,
113 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x61,
114 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x20,
115 0x00, 0x00, 0x00, 0x6a, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x73,
116 0x00, 0x00, 0x00, 0x6d, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00, 0x6e,
117 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x74,
118 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0xa0
120 const ext_type* e_lit = reinterpret_cast<ext_type*>(e_lit_base);
122 char i_lit_base[48] __attribute__((aligned(__alignof__(int_type)))) =
124 0x00, 0x62, 0x00, 0x6c, 0x00, 0x61, 0x00, 0x63, 0x00, 0x6b, 0x00, 0x20,
125 0x00, 0x70, 0x00, 0x65, 0x00, 0x61, 0x00, 0x72, 0x00, 0x6c, 0x00, 0x20,
126 0x00, 0x6a, 0x00, 0x61, 0x00, 0x73, 0x00, 0x6d, 0x00, 0x69, 0x00, 0x6e,
127 0x00, 0x65, 0x00, 0x20, 0x00, 0x74, 0x00, 0x65, 0x00, 0x61, 0x00, 0xa0
129 const int_type* i_lit = reinterpret_cast<int_type*>(i_lit_base);
131 const ext_type* efrom_next;
132 const int_type* ifrom_next;
133 ext_type* e_arr = new ext_type[size + 1];
135 int_type* i_arr = new int_type[size + 1];
138 // construct a locale object with the specialized facet.
139 locale loc(locale::classic(), new unicode_codecvt);
140 // sanity check the constructed locale has the specialized facet.
141 VERIFY( has_facet<unicode_codecvt>(loc) );
142 const unicode_codecvt& cvt = use_facet<unicode_codecvt>(loc);
145 unicode_codecvt::state_type state01("UCS-2BE", "UCS-4BE", 0xfeff, 0);
146 initialize_state(state01);
147 result r1 = cvt.in(state01, e_lit, e_lit + size, efrom_next,
148 i_arr, i_arr + size + 1, ito_next);
149 VERIFY( r1 == codecvt_base::ok );
150 VERIFY( !int_traits::compare(i_arr, i_lit, size) );
151 VERIFY( efrom_next == e_lit + size );
152 VERIFY( ito_next == i_arr + size );
155 unicode_codecvt::state_type state02("UCS-2BE", "UCS-4BE", 0xfeff, 0);
156 initialize_state(state02);
157 result r2 = cvt.out(state02, i_lit, i_lit + size, ifrom_next,
158 e_arr, e_arr + size, eto_next);
159 // XXX VERIFY( r2 == codecvt_base::ok );
160 VERIFY( !ext_traits::compare(e_arr, e_lit, size) );
161 VERIFY( ifrom_next == i_lit + size );
162 VERIFY( eto_next == e_arr + size );
165 ext_traits::copy(e_arr, e_lit, size);
166 unicode_codecvt::state_type state03("UCS-2BE", "UCS-4BE", 0xfeff, 0);
167 initialize_state(state03);
168 result r3 = cvt.unshift(state03, e_arr, e_arr + size, eto_next);
169 VERIFY( r3 == codecvt_base::noconv );
170 VERIFY( !ext_traits::compare(e_arr, e_lit, size) );
171 VERIFY( eto_next == e_arr );
173 int i = cvt.encoding();
176 VERIFY( !cvt.always_noconv() );
178 unicode_codecvt::state_type state04("UCS-2BE", "UCS-4BE", 0xfeff, 0);
179 initialize_state(state04);
180 int j = cvt.length(state03, e_lit, e_lit + size, 5);
183 int k = cvt.max_length();
189 #endif // _GLIBCPP_USE_WCHAR_T
190 #endif // _GLIBCPP_USE___ENC_TRAITS
194 #ifdef _GLIBCPP_USE___ENC_TRAITS
195 #ifdef _GLIBCPP_USE_WCHAR_T