Reshuffle 22_locale testsuite.
[platform/upstream/gcc.git] / libstdc++-v3 / testsuite / 22_locale / codecvt / unicode / wchar_t.cc
1 // 2000-08-23 Benjamin Kosnik <bkoz@cygnus.com>
2
3 // Copyright (C) 2000, 2001, 2002 Free Software Foundation
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
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // 22.2.1.5 - Template class codecvt [lib.locale.codecvt]
22
23 #include <locale>
24 #include <testsuite_hooks.h>
25
26 #ifdef _GLIBCPP_USE___ENC_TRAITS
27 #ifdef _GLIBCPP_USE_WCHAR_T
28
29 // Need some char_traits specializations for this to work.
30 typedef unsigned short                  unicode_t;
31
32 namespace std
33 {
34   template<>
35     struct char_traits<unicode_t>
36     {
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;
43       
44       static void 
45       assign(char_type& __c1, const char_type& __c2);
46
47       static bool 
48       eq(const char_type& __c1, const char_type& __c2);
49
50       static bool 
51       lt(const char_type& __c1, const char_type& __c2);
52
53       static int 
54       compare(const char_type* __s1, const char_type* __s2, size_t __n)
55       { return memcmp(__s1, __s2, __n); }
56
57       static size_t
58       length(const char_type* __s);
59
60       static const char_type* 
61       find(const char_type* __s, size_t __n, const char_type& __a);
62
63       static char_type* 
64       move(char_type* __s1, const char_type* __s2, size_t __n);
65
66       static char_type* 
67       copy(char_type* __s1, const char_type* __s2, size_t __n)
68       {  return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
69
70       static char_type* 
71       assign(char_type* __s, size_t __n, char_type __a);
72
73       static char_type 
74       to_char_type(const int_type& __c);
75
76       static int_type 
77       to_int_type(const char_type& __c);
78
79       static bool 
80       eq_int_type(const int_type& __c1, const int_type& __c2);
81
82       static int_type 
83       eof(); 
84
85       static int_type 
86       not_eof(const int_type& __c);
87     };
88 }
89
90 void
91 initialize_state(std::__enc_traits& state)
92 { state._M_init(); }
93
94 // Partial specialization using __enc_traits.
95 // codecvt<unicode_t, wchar_t, __enc_traits>
96 void test01()
97 {
98   using namespace std;
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;
106
107   bool                  test = true;
108   int                   size = 23;
109   char  e_lit_base[96] __attribute__((aligned(__alignof__(ext_type)))) =
110   {
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     
119   };
120   const ext_type*       e_lit = reinterpret_cast<ext_type*>(e_lit_base);
121
122   char  i_lit_base[48] __attribute__((aligned(__alignof__(int_type)))) = 
123   { 
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 
128   };
129   const int_type*       i_lit = reinterpret_cast<int_type*>(i_lit_base);
130
131   const ext_type*       efrom_next;
132   const int_type*       ifrom_next;
133   ext_type*             e_arr = new ext_type[size + 1];
134   ext_type*             eto_next;
135   int_type*             i_arr = new int_type[size + 1];
136   int_type*             ito_next;
137
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); 
143
144   // in
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 );
153
154   // out
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 );
163
164   // unshift
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 );
172
173   int i = cvt.encoding();
174   VERIFY( i == 0 );
175
176   VERIFY( !cvt.always_noconv() );
177
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);
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 }
189 #endif // _GLIBCPP_USE_WCHAR_T
190 #endif // _GLIBCPP_USE___ENC_TRAITS
191
192 int main ()
193 {
194 #ifdef _GLIBCPP_USE___ENC_TRAITS
195 #ifdef _GLIBCPP_USE_WCHAR_T
196   test01();
197 #endif
198 #endif 
199   return 0;
200 }
201