1 // 2003-02-06  Petur Runolfsson  <peturr02@ru.is>
2 
3 // Copyright (C) 2003 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 
27 
28 #ifdef _GLIBCXX_USE___ENC_TRAITS
29 
30 // Need some char_traits specializations for this to work.
31 typedef unsigned short			unicode_t;
32 
33 namespace std
34 {
35   template<>
zero_state(std::mbstate_t & state)36     struct char_traits<unicode_t>
37     {
38       typedef unicode_t 	char_type;
39       // Unsigned as wint_t is unsigned.
40       typedef unsigned long  	int_type;
41       typedef streampos 	pos_type;
42       typedef streamoff 	off_type;
43       typedef mbstate_t 	state_type;
44 
45       static void
46       assign(char_type& __c1, const char_type& __c2);
47 
48       static bool
49       eq(const char_type& __c1, const char_type& __c2);
50 
51       static bool
52       lt(const char_type& __c1, const char_type& __c2);
53 
54       static int
55       compare(const char_type* __s1, const char_type* __s2, size_t __n)
56       { return memcmp(__s1, __s2, __n); }
57 
58       static size_t
59       length(const char_type* __s);
60 
61       static const char_type*
62       find(const char_type* __s, size_t __n, const char_type& __a);
63 
64       static char_type*
65       move(char_type* __s1, const char_type* __s2, size_t __n);
66 
67       static char_type*
68       copy(char_type* __s1, const char_type* __s2, size_t __n)
69       {  return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
70 
71       static char_type*
72       assign(char_type* __s, size_t __n, char_type __a);
73 
74       static char_type
75       to_char_type(const int_type& __c);
76 
77       static int_type
78       to_int_type(const char_type& __c);
79 
80       static bool
81       eq_int_type(const int_type& __c1, const int_type& __c2);
82 
83       static int_type
84       eof();
85 
86       static int_type
87       not_eof(const int_type& __c);
88     };
89 }
90 
91 void
92 initialize_state(std::__enc_traits& state)
93 { state._M_init(); }
94 
95 bool length_called = false;
96 
97 class length_codecvt : public std::codecvt<unicode_t, char, std::__enc_traits>
98 {
99   typedef std::codecvt<unicode_t, char, std::__enc_traits> unicode_codecvt;
100 
101 public:
102   // DR75: type of first argument of do_length is state_type&
103   virtual int do_length(state_type& state, const extern_type* from,
104                         const extern_type* end, std::size_t max) const
105   {
106     length_called = true;
107     return unicode_codecvt::do_length(state, from, end, max);
108   }
109 };
110 
111 // Partial specialization using __enc_traits.
112 // codecvt<unicode_t, char, __enc_traits>
113 // UNICODE - UCS2 (big endian)
114 void test01()
115 {
116   using namespace std;
117   typedef unicode_t				int_type;
118   typedef char					ext_type;
119   typedef __enc_traits				enc_type;
120   typedef codecvt<int_type, ext_type, enc_type>	unicode_codecvt;
121 
122   bool test __attribute__((unused)) = true;
123   const ext_type* 	e_lit = "black pearl jasmine tea";
124   int 			size = strlen(e_lit);
125 
126   // construct a locale object with the specialized facet.
127   locale 		loc(locale::classic(), new length_codecvt);
128   // sanity check the constructed locale has the specialized facet.
129   VERIFY( has_facet<unicode_codecvt>(loc) );
130   const unicode_codecvt&	cvt = use_facet<unicode_codecvt>(loc);
131 
132   unicode_codecvt::state_type state04("UCS-2BE", "ISO-8859-15", 0xfeff, 0);
133   initialize_state(state04);
134   length_called = false;
135   cvt.length(state04, e_lit, e_lit + size, 5);
136   VERIFY( length_called );
137 }
138 #endif // _GLIBCXX_USE___ENC_TRAITS
139 
140 int main ()
141 {
142 #if _GLIBCXX_USE___ENC_TRAITS
143   test01();
144 #endif
145 
146   return 0;
147 }
148