]>
Commit | Line | Data |
---|---|---|
985287b8 | 1 | // 2000-08-23 Benjamin Kosnik <bkoz@cygnus.com> |
ae658d47 | 2 | |
af779169 | 3 | // Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation |
ae658d47 BK |
4 | // |
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) | |
9 | // any later version. | |
10 | ||
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 | ||
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 | |
83f51799 | 18 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
ae658d47 BK |
19 | // USA. |
20 | ||
21 | // 22.2.1.5 - Template class codecvt [lib.locale.codecvt] | |
22 | ||
23 | #include <locale> | |
fe413112 | 24 | #include <testsuite_hooks.h> |
ae658d47 | 25 | |
3d7c150e | 26 | #ifdef _GLIBCXX_USE___ENC_TRAITS |
ce881b7f | 27 | |
f13a69ec BK |
28 | // Need some char_traits specializations for this to work. |
29 | typedef unsigned short unicode_t; | |
30 | ||
31 | namespace std | |
32 | { | |
33 | template<> | |
34 | struct char_traits<unicode_t> | |
35 | { | |
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; | |
42 | ||
43 | static void | |
44 | assign(char_type& __c1, const char_type& __c2); | |
45 | ||
46 | static bool | |
47 | eq(const char_type& __c1, const char_type& __c2); | |
48 | ||
49 | static bool | |
50 | lt(const char_type& __c1, const char_type& __c2); | |
51 | ||
52 | static int | |
53 | compare(const char_type* __s1, const char_type* __s2, size_t __n) | |
54 | { return memcmp(__s1, __s2, __n); } | |
55 | ||
56 | static size_t | |
57 | length(const char_type* __s); | |
58 | ||
59 | static const char_type* | |
60 | find(const char_type* __s, size_t __n, const char_type& __a); | |
61 | ||
62 | static char_type* | |
63 | move(char_type* __s1, const char_type* __s2, size_t __n); | |
64 | ||
65 | static char_type* | |
66 | copy(char_type* __s1, const char_type* __s2, size_t __n) | |
67 | { return static_cast<char_type*>(memcpy(__s1, __s2, __n)); } | |
68 | ||
69 | static char_type* | |
70 | assign(char_type* __s, size_t __n, char_type __a); | |
71 | ||
72 | static char_type | |
73 | to_char_type(const int_type& __c); | |
74 | ||
75 | static int_type | |
76 | to_int_type(const char_type& __c); | |
77 | ||
78 | static bool | |
79 | eq_int_type(const int_type& __c1, const int_type& __c2); | |
80 | ||
81 | static int_type | |
82 | eof(); | |
83 | ||
84 | static int_type | |
85 | not_eof(const int_type& __c); | |
86 | }; | |
87 | } | |
88 | ||
ae658d47 | 89 | void |
f13a69ec | 90 | initialize_state(std::__enc_traits& state) |
ae658d47 BK |
91 | { state._M_init(); } |
92 | ||
93 | // Partial specialization using __enc_traits. | |
94 | // codecvt<unicode_t, wchar_t, __enc_traits> | |
95 | void test01() | |
96 | { | |
f13a69ec | 97 | using namespace std; |
ae658d47 | 98 | typedef codecvt_base::result result; |
ae658d47 BK |
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; | |
105 | ||
11f10e6b | 106 | bool test __attribute__((unused)) = true; |
985287b8 | 107 | int size = 23; |
10cbfb51 UW |
108 | char e_lit_base[96] __attribute__((aligned(__alignof__(ext_type)))) = |
109 | { | |
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 | |
985287b8 | 118 | }; |
10cbfb51 | 119 | const ext_type* e_lit = reinterpret_cast<ext_type*>(e_lit_base); |
ae658d47 | 120 | |
10cbfb51 | 121 | char i_lit_base[48] __attribute__((aligned(__alignof__(int_type)))) = |
7dc08a20 | 122 | { |
10cbfb51 UW |
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 | |
ae658d47 | 127 | }; |
10cbfb51 | 128 | const int_type* i_lit = reinterpret_cast<int_type*>(i_lit_base); |
ae658d47 BK |
129 | |
130 | const ext_type* efrom_next; | |
131 | const int_type* ifrom_next; | |
132 | ext_type* e_arr = new ext_type[size + 1]; | |
133 | ext_type* eto_next; | |
134 | int_type* i_arr = new int_type[size + 1]; | |
135 | int_type* ito_next; | |
136 | ||
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); | |
142 | ||
143 | // in | |
af779169 BK |
144 | // unicode_codecvt::state_type state01("UCS-2BE", "UCS-4BE", 0xfeff, 0); |
145 | unicode_codecvt::state_type state01("UCS-2BE", "UCS-4BE", 0, 0); | |
ae658d47 BK |
146 | initialize_state(state01); |
147 | result r1 = cvt.in(state01, e_lit, e_lit + size, efrom_next, | |
7dc08a20 | 148 | i_arr, i_arr + size + 1, ito_next); |
ae658d47 BK |
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 ); | |
153 | ||
154 | // out | |
af779169 | 155 | unicode_codecvt::state_type state02("UCS-2BE", "UCS-4BE", 0, 0); |
ae658d47 BK |
156 | initialize_state(state02); |
157 | result r2 = cvt.out(state02, i_lit, i_lit + size, ifrom_next, | |
11f10e6b BK |
158 | e_arr, e_arr + size, eto_next); |
159 | VERIFY( r2 == codecvt_base::ok ); // XXX? | |
ae658d47 BK |
160 | VERIFY( !ext_traits::compare(e_arr, e_lit, size) ); |
161 | VERIFY( ifrom_next == i_lit + size ); | |
162 | VERIFY( eto_next == e_arr + size ); | |
163 | ||
164 | // unshift | |
165 | ext_traits::copy(e_arr, e_lit, size); | |
af779169 | 166 | unicode_codecvt::state_type state03("UCS-2BE", "UCS-4BE", 0, 0); |
ae658d47 BK |
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 ); | |
172 | ||
173 | int i = cvt.encoding(); | |
174 | VERIFY( i == 0 ); | |
175 | ||
176 | VERIFY( !cvt.always_noconv() ); | |
177 | ||
af779169 | 178 | unicode_codecvt::state_type state04("UCS-2BE", "UCS-4BE", 0, 0); |
ae658d47 BK |
179 | initialize_state(state04); |
180 | int j = cvt.length(state03, e_lit, e_lit + size, 5); | |
181 | VERIFY( j == 5 ); | |
182 | ||
183 | int k = cvt.max_length(); | |
184 | VERIFY( k == 1 ); | |
185 | ||
186 | delete [] e_arr; | |
187 | delete [] i_arr; | |
188 | } | |
3d7c150e | 189 | #endif // _GLIBCXX_USE___ENC_TRAITS |
ae658d47 BK |
190 | |
191 | int main () | |
192 | { | |
3d7c150e | 193 | #ifdef _GLIBCXX_USE___ENC_TRAITS |
ae658d47 | 194 | test01(); |
33590f13 | 195 | #endif |
ae658d47 BK |
196 | return 0; |
197 | } | |
33590f13 | 198 |