OSDN Git Service

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