OSDN Git Service

2005-08-17 Kelley Cook <kcook@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 22_locale / locale / cons / 2.cc
1 // { dg-require-namedlocale "" }
2
3 // 2000-09-13 Benjamin Kosnik <bkoz@redhat.com>
4
5 // Copyright (C) 2000, 2001, 2002, 2003, 2005 Free Software Foundation
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING.  If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // USA.
22
23 // 22.1.1.2 locale constructors and destructors [lib.locale.cons]
24
25 #include <cwchar> // for mbstate_t
26 #include <locale>
27 #include <stdexcept>
28 #include <testsuite_hooks.h>
29
30 #if _GLIBCXX_USE___ENC_TRAITS
31 typedef std::codecvt<char, char, std::mbstate_t>              c_codecvt;
32 typedef std::codecvt_byname<char, char, std::mbstate_t>       c_codecvt_byname;
33 typedef std::codecvt<wchar_t, char, std::mbstate_t>           w_codecvt;
34 typedef std::codecvt_byname<wchar_t, char, std::mbstate_t>    w_codecvt_byname;
35
36 class gnu_codecvt: public c_codecvt { }; 
37
38 class gnu_facet: public std::locale::facet
39 {
40 public:
41   static std::locale::id id;
42 };
43
44 std::locale::id gnu_facet::id;
45
46 // Need some char_traits specializations for this to work.
47 typedef unsigned short                  unicode_t;
48
49 namespace std
50 {
51   template<>
52     struct char_traits<unicode_t>
53     {
54       typedef unicode_t         char_type;
55       // Unsigned as wint_t is unsigned.
56       typedef unsigned long     int_type;
57       typedef streampos         pos_type;
58       typedef streamoff         off_type;
59       typedef mbstate_t         state_type;
60       
61       static void 
62       assign(char_type& __c1, const char_type& __c2);
63
64       static bool 
65       eq(const char_type& __c1, const char_type& __c2);
66
67       static bool 
68       lt(const char_type& __c1, const char_type& __c2);
69
70       static int 
71       compare(const char_type* __s1, const char_type* __s2, size_t __n)
72       { return memcmp(__s1, __s2, __n); }
73
74       static size_t
75       length(const char_type* __s);
76
77       static const char_type* 
78       find(const char_type* __s, size_t __n, const char_type& __a);
79
80       static char_type* 
81       move(char_type* __s1, const char_type* __s2, size_t __n);
82
83       static char_type* 
84       copy(char_type* __s1, const char_type* __s2, size_t __n)
85       { return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
86
87       static char_type* 
88       assign(char_type* __s, size_t __n, char_type __a);
89
90       static char_type 
91       to_char_type(const int_type& __c);
92
93       static int_type 
94       to_int_type(const char_type& __c);
95
96       static bool 
97       eq_int_type(const int_type& __c1, const int_type& __c2);
98
99       static int_type 
100       eof(); 
101
102       static int_type 
103       not_eof(const int_type& __c);
104     };
105 }
106
107 void test01()
108 {
109   using namespace std;
110   typedef unicode_t                             int_type;
111   typedef char                                  ext_type;
112   typedef __enc_traits                          enc_type;
113   typedef codecvt<int_type, ext_type, enc_type> unicode_codecvt;
114
115   bool test __attribute__((unused)) = true;
116   string str1, str2;
117
118   // construct a locale object with the C facet
119   const locale  loc01 = locale::classic();
120
121   // 1
122   // template <class Facet> locale(const locale& other, Facet* f)
123   // construct a locale object with the specialized facet.
124   locale loc02(locale::classic(), new gnu_codecvt);
125   VERIFY (loc01 != loc02);
126   VERIFY (loc02.name() == "*");
127   try
128     {
129       VERIFY (has_facet<gnu_codecvt>(loc02));
130       VERIFY (has_facet<c_codecvt>(loc02));
131       VERIFY (has_facet<w_codecvt>(loc02));
132     }
133   catch(...)
134     { VERIFY( false ); }
135
136   try 
137     { use_facet<gnu_facet>(loc02); }
138   catch(bad_cast& obj)
139     { VERIFY( true ); }
140   catch(...)
141     { VERIFY( false ); }
142
143   // unicode_codecvt
144   locale loc13(locale::classic(), new unicode_codecvt);  
145   VERIFY (loc01 != loc13);
146   VERIFY (loc13.name() == "*");
147   try 
148     {
149       VERIFY (has_facet<c_codecvt>(loc13));
150       VERIFY (has_facet<w_codecvt>(loc13));
151       VERIFY (has_facet<unicode_codecvt>(loc13));
152     }
153   catch(...)
154     { VERIFY( false ); }
155
156   try 
157     { use_facet<gnu_facet>(loc13); }
158   catch(bad_cast& obj)
159     { VERIFY( true ); }
160   catch(...)
161     { VERIFY( false ); }
162
163   // 2
164   // locale() throw()
165   locale loc03;
166   VERIFY (loc03 == loc01);
167   VERIFY (loc03.name() == "C");
168   locale loc04 = locale::global(loc02);
169   locale loc05;
170   VERIFY (loc05 != loc03);
171   VERIFY (loc05 == loc02);
172
173   // 3
174   // explicit locale(const char* std_name)
175   locale loc06 = locale("fr_FR");
176   VERIFY (loc06 != loc01);  
177   VERIFY (loc06 != loc02);  
178   VERIFY (loc06.name() == "fr_FR");
179   locale loc07("");
180   VERIFY (loc07 != loc02);  
181   VERIFY (loc07.name() != "");
182   try
183     { locale loc08(static_cast<const char*>(NULL)); }
184   catch(runtime_error& obj)
185     { VERIFY (true); }
186   catch(...)
187     { VERIFY (false); }
188
189   try
190     { locale loc08("saturn_SUN*RA"); }
191   catch(runtime_error& obj)
192     { VERIFY (true); }
193   catch(...)
194     { VERIFY (false); }
195
196   // 4
197   // locale(const locale& other, const char* std_name, category)
198   {
199     // This is the same as 5 only use "C" for loc("C")
200     locale loc09(loc06, "C", locale::ctype);
201     VERIFY (loc09.name() != "fr_FR");
202     VERIFY (loc09.name() != "C");
203     VERIFY (loc09.name() != "*");
204     VERIFY (loc09 != loc01);  
205     VERIFY (loc09 != loc06);  
206
207     locale loc10(loc02, "C", locale::ctype);
208     VERIFY (loc10.name() == "*");
209     VERIFY (loc10 != loc01);   // As not named, even tho facets same...
210     VERIFY (loc10 != loc02);  
211
212     locale loc11(loc01, "C", locale::ctype);
213     VERIFY (loc11.name() == "C");
214     VERIFY (loc11 == loc01);  
215
216     try
217       { locale loc12(loc01, static_cast<const char*>(NULL), locale::ctype); }
218     catch(runtime_error& obj)
219       { VERIFY (true); }
220     catch(...)
221       { VERIFY (false); }
222
223     try
224       { locale loc13(loc01, "localized by the wu-tang clan", locale::ctype); }
225     catch(runtime_error& obj)
226       { VERIFY (true); }
227     catch(...)
228       { VERIFY (false); }
229
230     locale loc14(loc06, "C", locale::none);
231     VERIFY (loc14.name() == "fr_FR");
232     VERIFY (loc14 == loc06);  
233
234     locale loc15(loc06, "C", locale::collate);
235     VERIFY (loc15.name() != "fr_FR");
236     VERIFY (loc15.name() != "C");
237     VERIFY (loc15.name() != "*");
238     VERIFY (loc15.name() != loc09.name());
239     VERIFY (loc15 != loc01);  
240     VERIFY (loc15 != loc06);  
241     VERIFY (loc15 != loc09);  
242   }
243
244   // 5
245   // locale(const locale& other, const locale& one, category)
246   {
247     // This is the exact same as 4, with locale("C") for "C"
248     locale loc09(loc06, loc01, locale::ctype);
249     VERIFY (loc09.name() != "fr_FR");
250     VERIFY (loc09.name() != "C");
251     VERIFY (loc09.name() != "*");
252     VERIFY (loc09 != loc01);  
253     VERIFY (loc09 != loc06);  
254
255     locale loc10(loc02, loc01, locale::ctype);
256     VERIFY (loc10.name() == "*");
257     VERIFY (loc10 != loc01);   // As not named, even tho facets same...
258     VERIFY (loc10 != loc02);  
259
260     locale loc11(loc01, loc01, locale::ctype);
261     VERIFY (loc11.name() == "C");
262     VERIFY (loc11 == loc01);  
263
264     try
265       { locale loc12(loc01, static_cast<const char*>(NULL), locale::ctype); }
266     catch(runtime_error& obj)
267       { VERIFY (true); }
268     catch(...)
269       { VERIFY (false); }
270
271     try
272       { locale loc13(loc01, locale("wu-tang clan"), locale::ctype); }
273     catch(runtime_error& obj)
274       { VERIFY (true); }
275     catch(...)
276       { VERIFY (false); }
277
278     locale loc14(loc06, loc01, locale::none);
279     VERIFY (loc14.name() == "fr_FR");
280     VERIFY (loc14 == loc06);  
281
282     locale loc15(loc06, loc01, locale::collate);
283     VERIFY (loc15.name() != "fr_FR");
284     VERIFY (loc15.name() != "C");
285     VERIFY (loc15.name() != "*");
286     VERIFY (loc15.name() != loc09.name());
287     VERIFY (loc15 != loc01);  
288     VERIFY (loc15 != loc06);  
289     VERIFY (loc15 != loc09);  
290   }
291 }
292 #endif // _GLIBCXX_USE___ENC_TRAITS
293
294 int main()
295 {
296 #if _GLIBCXX_USE___ENC_TRAITS
297   test01();
298 #endif 
299   return 0;
300 }