1 // 2000-08-23 Benjamin Kosnik <bkoz@cygnus.com>
3 // Copyright (C) 2000, 2001, 2002, 2003 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // 22.2.1.5 - Template class codecvt [lib.locale.codecvt]
24 #include <testsuite_hooks.h>
26 #ifdef _GLIBCXX_USE___ENC_TRAITS
28 // Need some char_traits specializations for this to work.
29 typedef unsigned short unicode_t
;
34 struct char_traits
<unicode_t
>
36 typedef unicode_t char_type
;
37 // Unsigned as wint_t is unsigned.
38 typedef unsigned long int_type
;
39 typedef streampos pos_type
;
40 typedef streamoff off_type
;
41 typedef mbstate_t state_type
;
44 assign(char_type
& __c1
, const char_type
& __c2
);
47 eq(const char_type
& __c1
, const char_type
& __c2
);
50 lt(const char_type
& __c1
, const char_type
& __c2
);
53 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
54 { return memcmp(__s1
, __s2
, __n
); }
57 length(const char_type
* __s
);
59 static const char_type
*
60 find(const char_type
* __s
, size_t __n
, const char_type
& __a
);
63 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
);
66 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
67 { return static_cast<char_type
*>(memcpy(__s1
, __s2
, __n
)); }
70 assign(char_type
* __s
, size_t __n
, char_type __a
);
73 to_char_type(const int_type
& __c
);
76 to_int_type(const char_type
& __c
);
79 eq_int_type(const int_type
& __c1
, const int_type
& __c2
);
85 not_eof(const int_type
& __c
);
90 initialize_state(std::__enc_traits
& state
)
93 // Partial specialization using __enc_traits.
94 // codecvt<unicode_t, wchar_t, __enc_traits>
98 typedef codecvt_base::result result
;
99 typedef unicode_t int_type
;
100 typedef wchar_t ext_type
;
101 typedef __enc_traits enc_type
;
102 typedef codecvt
<int_type
, ext_type
, enc_type
> unicode_codecvt
;
103 typedef char_traits
<int_type
> int_traits
;
104 typedef char_traits
<ext_type
> ext_traits
;
106 bool test
__attribute__((unused
)) = true;
108 char e_lit_base
[96] __attribute__((aligned(__alignof__(ext_type
)))) =
110 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x61,
111 0x00, 0x00, 0x00, 0x63, 0x00, 0x00, 0x00, 0x6b, 0x00, 0x00, 0x00, 0x20,
112 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x61,
113 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x20,
114 0x00, 0x00, 0x00, 0x6a, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x73,
115 0x00, 0x00, 0x00, 0x6d, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00, 0x6e,
116 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x74,
117 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0xa0
119 const ext_type
* e_lit
= reinterpret_cast<ext_type
*>(e_lit_base
);
121 char i_lit_base
[48] __attribute__((aligned(__alignof__(int_type
)))) =
123 0x00, 0x62, 0x00, 0x6c, 0x00, 0x61, 0x00, 0x63, 0x00, 0x6b, 0x00, 0x20,
124 0x00, 0x70, 0x00, 0x65, 0x00, 0x61, 0x00, 0x72, 0x00, 0x6c, 0x00, 0x20,
125 0x00, 0x6a, 0x00, 0x61, 0x00, 0x73, 0x00, 0x6d, 0x00, 0x69, 0x00, 0x6e,
126 0x00, 0x65, 0x00, 0x20, 0x00, 0x74, 0x00, 0x65, 0x00, 0x61, 0x00, 0xa0
128 const int_type
* i_lit
= reinterpret_cast<int_type
*>(i_lit_base
);
130 const ext_type
* efrom_next
;
131 const int_type
* ifrom_next
;
132 ext_type
* e_arr
= new ext_type
[size
+ 1];
134 int_type
* i_arr
= new int_type
[size
+ 1];
137 // construct a locale object with the specialized facet.
138 locale
loc(locale::classic(), new unicode_codecvt
);
139 // sanity check the constructed locale has the specialized facet.
140 VERIFY( has_facet
<unicode_codecvt
>(loc
) );
141 const unicode_codecvt
& cvt
= use_facet
<unicode_codecvt
>(loc
);
144 // unicode_codecvt::state_type state01("UCS-2BE", "UCS-4BE", 0xfeff, 0);
145 unicode_codecvt::state_type
state01("UCS-2BE", "UCS-4BE", 0, 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", 0, 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 VERIFY( r2
== codecvt_base::ok
); // XXX?
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", 0, 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", 0, 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 // _GLIBCXX_USE___ENC_TRAITS
193 #ifdef _GLIBCXX_USE___ENC_TRAITS