2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 22_locale / codecvt / unicode / 1.cc
blob705f43afd6f1692bc1769cd2c3f3b63675e359a7
1 // 2003-02-06 Petur Runolfsson <peturr02@ru.is>
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.
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 // 22.2.1.5 - Template class codecvt [lib.locale.codecvt]
23 #include <locale>
24 #include <testsuite_hooks.h>
28 #ifdef _GLIBCXX_USE___ENC_TRAITS
30 // Need some char_traits specializations for this to work.
31 typedef unsigned short unicode_t;
33 namespace std
35 template<>
36 struct char_traits<unicode_t>
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;
45 static void
46 assign(char_type& __c1, const char_type& __c2);
48 static bool
49 eq(const char_type& __c1, const char_type& __c2);
51 static bool
52 lt(const char_type& __c1, const char_type& __c2);
54 static int
55 compare(const char_type* __s1, const char_type* __s2, size_t __n)
56 { return memcmp(__s1, __s2, __n); }
58 static size_t
59 length(const char_type* __s);
61 static const char_type*
62 find(const char_type* __s, size_t __n, const char_type& __a);
64 static char_type*
65 move(char_type* __s1, const char_type* __s2, size_t __n);
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)); }
71 static char_type*
72 assign(char_type* __s, size_t __n, char_type __a);
74 static char_type
75 to_char_type(const int_type& __c);
77 static int_type
78 to_int_type(const char_type& __c);
80 static bool
81 eq_int_type(const int_type& __c1, const int_type& __c2);
83 static int_type
84 eof();
86 static int_type
87 not_eof(const int_type& __c);
91 void
92 initialize_state(std::__enc_traits& state)
93 { state._M_init(); }
95 bool length_called = false;
97 class length_codecvt : public std::codecvt<unicode_t, char, std::__enc_traits>
99 typedef std::codecvt<unicode_t, char, std::__enc_traits> unicode_codecvt;
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
106 length_called = true;
107 return unicode_codecvt::do_length(state, from, end, max);
111 // Partial specialization using __enc_traits.
112 // codecvt<unicode_t, char, __enc_traits>
113 // UNICODE - UCS2 (big endian)
114 void test01()
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;
122 bool test __attribute__((unused)) = true;
123 const ext_type* e_lit = "black pearl jasmine tea";
124 int size = strlen(e_lit);
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);
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 );
138 #endif // _GLIBCXX_USE___ENC_TRAITS
140 int main ()
142 #if _GLIBCXX_USE___ENC_TRAITS
143 test01();
144 #endif
146 return 0;