OSDN Git Service

2001-08-07 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.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 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 using namespace std;
27
28 #ifdef _GLIBCPP_USE___ENC_TRAITS
29
30 void
31 initialize_state(__enc_traits& state)
32 { state._M_init(); }
33
34 // Partial specialization using __enc_traits.
35 // codecvt<unicode_t, wchar_t, __enc_traits>
36 void test01()
37 {
38   typedef codecvt_base::result                  result;
39   typedef unsigned short                        unicode_t;
40   typedef unicode_t                             int_type;
41   typedef wchar_t                               ext_type;
42   typedef __enc_traits                          enc_type;
43   typedef codecvt<int_type, ext_type, enc_type> unicode_codecvt;
44   typedef char_traits<int_type>                 int_traits;
45   typedef char_traits<ext_type>                 ext_traits;
46
47   bool                  test = true;
48   int                   size = 23;
49   ext_type              e_lit_base[24] = 
50   { 1644167168, 1811939328, 1627389952, 1660944384, 1795162112,  536870912, 
51     1879048192, 1694498816, 1627389952, 1912602624, 1811939328, 536870912, 
52     1778384896, 1627389952, 1929379840, 1828716544, 1761607680, 1845493760, 
53     1694498816, 536870912,  1946157056, 1694498816, 1627389952, 167772160
54   };
55   const ext_type*       e_lit = e_lit_base;
56
57   int_type              i_lit_base[24] = 
58   { 
59     0x6200, 0x6c00, 0x6100, 0x6300, 0x6b00, 0x2000, 0x7000, 0x6500, 0x6100, 
60     0x7200, 0x6c00, 0x2000, 0x6a00, 0x6100, 0x7300, 0x6d00, 0x6900, 0x6e00, 
61     0x6500, 0x2000, 0x7400, 0x6500, 0x6100, 0xa000
62   };
63   const int_type*       i_lit = i_lit_base;
64
65   const ext_type*       efrom_next;
66   const int_type*       ifrom_next;
67   ext_type*             e_arr = new ext_type[size + 1];
68   ext_type*             eto_next;
69   int_type*             i_arr = new int_type[size + 1];
70   int_type*             ito_next;
71
72   // construct a locale object with the specialized facet.
73   locale                loc(locale::classic(), new unicode_codecvt);
74   // sanity check the constructed locale has the specialized facet.
75   VERIFY( has_facet<unicode_codecvt>(loc) );
76   const unicode_codecvt&        cvt = use_facet<unicode_codecvt>(loc); 
77
78   // in
79   unicode_codecvt::state_type state01("UCS-2BE", "UCS4", 0xfeff, 0);
80   initialize_state(state01);
81   result r1 = cvt.in(state01, e_lit, e_lit + size, efrom_next, 
82                      i_arr, i_arr + size + 1, ito_next);
83   VERIFY( r1 == codecvt_base::ok );
84   VERIFY( !int_traits::compare(i_arr, i_lit, size) ); 
85   VERIFY( efrom_next == e_lit + size );
86   VERIFY( ito_next == i_arr + size );
87
88   // out
89   unicode_codecvt::state_type state02("UCS-2BE", "UCS4", 0xfeff, 0);
90   initialize_state(state02);  
91   result r2 = cvt.out(state02, i_lit, i_lit + size, ifrom_next, 
92                        e_arr, e_arr + size, eto_next);
93   // XXX   VERIFY( r2 == codecvt_base::ok );
94   VERIFY( !ext_traits::compare(e_arr, e_lit, size) ); 
95   VERIFY( ifrom_next == i_lit + size );
96   VERIFY( eto_next == e_arr + size );
97
98   // unshift
99   ext_traits::copy(e_arr, e_lit, size);
100   unicode_codecvt::state_type state03("UCS-2BE", "UCS4", 0xfeff, 0);
101   initialize_state(state03);
102   result r3 = cvt.unshift(state03, e_arr, e_arr + size, eto_next);
103   VERIFY( r3 == codecvt_base::noconv );
104   VERIFY( !ext_traits::compare(e_arr, e_lit, size) ); 
105   VERIFY( eto_next == e_arr );
106
107   int i = cvt.encoding();
108   VERIFY( i == 0 );
109
110   VERIFY( !cvt.always_noconv() );
111
112   unicode_codecvt::state_type state04("UCS-2BE", "UCS4", 0xfeff, 0);
113   initialize_state(state04);
114   int j = cvt.length(state03, e_lit, e_lit + size, 5);
115   VERIFY( j == 5 );
116
117   int k = cvt.max_length();
118   VERIFY( k == 1 );
119
120   delete [] e_arr;
121   delete [] i_arr;
122 }
123 #endif // _GLIBCPP_USE___ENC_TRAITS
124
125 int main ()
126 {
127 #if _GLIBCPP_USE___ENC_TRAITS
128   test01();
129 #endif 
130
131   return 0;
132 }
133