]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/22_locale/ctor_copy_dtor.cc
dwarf2out.c (fde_table_in_use): Mark GTY.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 22_locale / ctor_copy_dtor.cc
1 // 2000-09-13 Benjamin Kosnik <bkoz@redhat.com>
2
3 // Copyright (C) 2000, 2001, 2002 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 void test00()
29 {
30 // Should be able to do this as the first thing that happens in a
31 // file and have it not crash.
32 std::locale loc("C");
33 }
34
35 #if _GLIBCPP_USE___ENC_TRAITS
36 typedef std::codecvt<char, char, std::mbstate_t> c_codecvt;
37 typedef std::codecvt_byname<char, char, std::mbstate_t> c_codecvt_byname;
38 typedef std::codecvt<wchar_t, char, std::mbstate_t> w_codecvt;
39 typedef std::codecvt_byname<wchar_t, char, std::mbstate_t> w_codecvt_byname;
40
41 class gnu_codecvt: public c_codecvt { };
42
43 class gnu_facet: public std::locale::facet
44 {
45 public:
46 static std::locale::id id;
47 };
48
49 std::locale::id gnu_facet::id;
50
51 // Need some char_traits specializations for this to work.
52 typedef unsigned short unicode_t;
53
54 namespace std
55 {
56 template<>
57 struct char_traits<unicode_t>
58 {
59 typedef unicode_t char_type;
60 // Unsigned as wint_t is unsigned.
61 typedef unsigned long int_type;
62 typedef streampos pos_type;
63 typedef streamoff off_type;
64 typedef mbstate_t state_type;
65
66 static void
67 assign(char_type& __c1, const char_type& __c2);
68
69 static bool
70 eq(const char_type& __c1, const char_type& __c2);
71
72 static bool
73 lt(const char_type& __c1, const char_type& __c2);
74
75 static int
76 compare(const char_type* __s1, const char_type* __s2, size_t __n)
77 { return memcmp(__s1, __s2, __n); }
78
79 static size_t
80 length(const char_type* __s);
81
82 static const char_type*
83 find(const char_type* __s, size_t __n, const char_type& __a);
84
85 static char_type*
86 move(char_type* __s1, const char_type* __s2, size_t __n);
87
88 static char_type*
89 copy(char_type* __s1, const char_type* __s2, size_t __n)
90 { return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
91
92 static char_type*
93 assign(char_type* __s, size_t __n, char_type __a);
94
95 static char_type
96 to_char_type(const int_type& __c);
97
98 static int_type
99 to_int_type(const char_type& __c);
100
101 static bool
102 eq_int_type(const int_type& __c1, const int_type& __c2);
103
104 static int_type
105 eof();
106
107 static int_type
108 not_eof(const int_type& __c);
109 };
110 }
111
112 void test01()
113 {
114 using namespace std;
115 typedef unicode_t int_type;
116 typedef char ext_type;
117 typedef __enc_traits enc_type;
118 typedef codecvt<int_type, ext_type, enc_type> unicode_codecvt;
119
120 bool test = true;
121 string str1, str2;
122
123 // construct a locale object with the C facet
124 const locale loc01 = locale::classic();
125
126 // 1
127 // template <class Facet> locale(const locale& other, Facet* f)
128 // construct a locale object with the specialized facet.
129 locale loc02(locale::classic(), new gnu_codecvt);
130 VERIFY (loc01 != loc02);
131 VERIFY (loc02.name() == "*");
132 try
133 {
134 VERIFY (has_facet<gnu_codecvt>(loc02));
135 VERIFY (has_facet<c_codecvt>(loc02));
136 VERIFY (has_facet<w_codecvt>(loc02));
137 }
138 catch(...)
139 { VERIFY( false ); }
140
141 try
142 { use_facet<gnu_facet>(loc02); }
143 catch(bad_cast& obj)
144 { VERIFY( true ); }
145 catch(...)
146 { VERIFY( false ); }
147
148 // unicode_codecvt
149 locale loc13(locale::classic(), new unicode_codecvt);
150 VERIFY (loc01 != loc13);
151 VERIFY (loc13.name() == "*");
152 try
153 {
154 VERIFY (has_facet<c_codecvt>(loc13));
155 VERIFY (has_facet<w_codecvt>(loc13));
156 VERIFY (has_facet<unicode_codecvt>(loc13));
157 }
158 catch(...)
159 { VERIFY( false ); }
160
161 try
162 { use_facet<gnu_facet>(loc13); }
163 catch(bad_cast& obj)
164 { VERIFY( true ); }
165 catch(...)
166 { VERIFY( false ); }
167
168 // 2
169 // locale() throw()
170 locale loc03;
171 VERIFY (loc03 == loc01);
172 VERIFY (loc03.name() == "C");
173 locale loc04 = locale::global(loc02);
174 locale loc05;
175 VERIFY (loc05 != loc03);
176 VERIFY (loc05 == loc02);
177
178 // 3
179 // explicit locale(const char* std_name)
180 locale loc06("fr_FR");
181 VERIFY (loc06 != loc01);
182 VERIFY (loc06 != loc02);
183 VERIFY (loc06.name() == "fr_FR");
184 locale loc07("");
185 VERIFY (loc07 != loc02);
186 VERIFY (loc07.name() != "");
187 try
188 { locale loc08(static_cast<const char*>(NULL)); }
189 catch(runtime_error& obj)
190 { VERIFY (true); }
191 catch(...)
192 { VERIFY (false); }
193
194 try
195 { locale loc08("saturn_SUN*RA"); }
196 catch(runtime_error& obj)
197 { VERIFY (true); }
198 catch(...)
199 { VERIFY (false); }
200
201 // 4
202 // locale(const locale& other, const char* std_name, category)
203 {
204 // This is the same as 5 only use "C" for loc("C")
205 locale loc09(loc06, "C", locale::ctype);
206 VERIFY (loc09.name() != "fr_FR");
207 VERIFY (loc09.name() != "C");
208 VERIFY (loc09.name() != "*");
209 VERIFY (loc09 != loc01);
210 VERIFY (loc09 != loc06);
211
212 locale loc10(loc02, "C", locale::ctype);
213 VERIFY (loc10.name() == "*");
214 VERIFY (loc10 != loc01); // As not named, even tho facets same...
215 VERIFY (loc10 != loc02);
216
217 locale loc11(loc01, "C", locale::ctype);
218 VERIFY (loc11.name() == "C");
219 VERIFY (loc11 == loc01);
220
221 try
222 { locale loc12(loc01, static_cast<const char*>(NULL), locale::ctype); }
223 catch(runtime_error& obj)
224 { VERIFY (true); }
225 catch(...)
226 { VERIFY (false); }
227
228 try
229 { locale loc13(loc01, "localized by the wu-tang clan", locale::ctype); }
230 catch(runtime_error& obj)
231 { VERIFY (true); }
232 catch(...)
233 { VERIFY (false); }
234
235 locale loc14(loc06, "C", locale::none);
236 VERIFY (loc14.name() == "fr_FR");
237 VERIFY (loc14 == loc06);
238
239 locale loc15(loc06, "C", locale::collate);
240 VERIFY (loc15.name() != "fr_FR");
241 VERIFY (loc15.name() != "C");
242 VERIFY (loc15.name() != "*");
243 VERIFY (loc15.name() != loc09.name());
244 VERIFY (loc15 != loc01);
245 VERIFY (loc15 != loc06);
246 VERIFY (loc15 != loc09);
247 }
248
249 // 5
250 // locale(const locale& other, const locale& one, category)
251 {
252 // This is the exact same as 4, with locale("C") for "C"
253 locale loc09(loc06, loc01, locale::ctype);
254 VERIFY (loc09.name() != "fr_FR");
255 VERIFY (loc09.name() != "C");
256 VERIFY (loc09.name() != "*");
257 VERIFY (loc09 != loc01);
258 VERIFY (loc09 != loc06);
259
260 locale loc10(loc02, loc01, locale::ctype);
261 VERIFY (loc10.name() == "*");
262 VERIFY (loc10 != loc01); // As not named, even tho facets same...
263 VERIFY (loc10 != loc02);
264
265 locale loc11(loc01, loc01, locale::ctype);
266 VERIFY (loc11.name() == "C");
267 VERIFY (loc11 == loc01);
268
269 try
270 { locale loc12(loc01, static_cast<const char*>(NULL), locale::ctype); }
271 catch(runtime_error& obj)
272 { VERIFY (true); }
273 catch(...)
274 { VERIFY (false); }
275
276 try
277 { locale loc13(loc01, locale("wu-tang clan"), locale::ctype); }
278 catch(runtime_error& obj)
279 { VERIFY (true); }
280 catch(...)
281 { VERIFY (false); }
282
283 locale loc14(loc06, loc01, locale::none);
284 VERIFY (loc14.name() == "fr_FR");
285 VERIFY (loc14 == loc06);
286
287 locale loc15(loc06, loc01, locale::collate);
288 VERIFY (loc15.name() != "fr_FR");
289 VERIFY (loc15.name() != "C");
290 VERIFY (loc15.name() != "*");
291 VERIFY (loc15.name() != loc09.name());
292 VERIFY (loc15 != loc01);
293 VERIFY (loc15 != loc06);
294 VERIFY (loc15 != loc09);
295 }
296 }
297 #endif // _GLIBCPP_USE___ENC_TRAITS
298
299 // libstdc++/7222
300 void test02()
301 {
302 bool test = true;
303 std::locale loc_c1("C");
304 std::locale loc_c2 ("C");
305
306 std::locale loc_1("");
307 std::locale loc_2("");
308
309 VERIFY( loc_c1 == loc_c2 );
310 VERIFY( loc_1 == loc_2 );
311 }
312
313 // libstdc++/7811
314 void test03()
315 {
316 bool test = true;
317 #ifdef _GLIBCPP_HAVE_SETENV
318 const char* LC_ALL_orig = getenv("LC_ALL");
319 if (!setenv("LC_ALL", "it_IT", 1))
320 {
321 std::locale loc("");
322 VERIFY( loc.name() == "it_IT" );
323 setenv("LC_ALL", LC_ALL_orig ? LC_ALL_orig : "", 1);
324 }
325 #endif
326 }
327
328
329 // More tests for locale("") == POSIX locale::name.
330 void test04()
331 {
332 bool test = true;
333 using namespace std;
334
335 #ifdef _GLIBCPP_HAVE_SETENV
336
337 const char* LANG_orig = getenv("LANG") ? strdup(getenv("LANG")) : "";
338 const char* LC_ALL_orig = getenv("LC_ALL") ? strdup(getenv("LC_ALL")) : "";
339 const char* LC_CTYPE_orig =
340 getenv("LC_CTYPE") ? strdup(getenv("LC_CTYPE")) : "";
341 const char* LC_NUMERIC_orig =
342 getenv("LC_NUMERIC") ? strdup(getenv("LC_NUMERIC")) : "";
343 const char* LC_TIME_orig =
344 getenv("LC_TIME") ? strdup(getenv("LC_TIME")) : "";
345 const char* LC_COLLATE_orig =
346 getenv("LC_COLLATE") ? strdup(getenv("LC_COLLATE")) : "";
347 const char* LC_MONETARY_orig =
348 getenv("LC_MONETARY") ? strdup(getenv("LC_MONETARY")) : "";
349 const char* LC_MESSAGES_orig =
350 getenv("LC_MESSAGES") ? strdup(getenv("LC_MESSAGES")) : "";
351 #if _GLIBCPP_NUM_CATEGORIES
352 const char* LC_PAPER_orig =
353 getenv("LC_PAPER") ? strdup(getenv("LC_PAPER")) : "";
354 const char* LC_NAME_orig =
355 getenv("LC_NAME") ? strdup(getenv("LC_NAME")) : "";
356 const char* LC_ADDRESS_orig =
357 getenv("LC_ADDRESS") ? strdup(getenv("LC_ADDRESS")) : "";
358 const char* LC_TELEPHONE_orig =
359 getenv("LC_TELEPHONE") ? strdup(getenv("LC_TELEPHONE")) : "";
360 const char* LC_MEASUREMENT_orig =
361 getenv("LC_MEASUREMENT") ? strdup(getenv("LC_MEASUREMENT")) : "";
362 const char* LC_IDENTIFICATION_orig =
363 getenv("LC_IDENTIFICATION") ? strdup(getenv("LC_IDENTIFICATION")) : "";
364 #endif
365
366 // Check that a "POSIX" LC_ALL is equivalent to "C".
367 if (!setenv("LC_ALL", "POSIX", 1))
368 {
369 locale loc("");
370 VERIFY( loc.name() == "C" );
371 }
372 setenv("LC_ALL", "", 1);
373
374 // Check that a "en_PH" LC_ALL is equivalent to "en_PH".
375 if (!setenv("LC_ALL", "en_PH", 1))
376 {
377 locale loc("");
378 VERIFY( loc.name() == "en_PH" );
379 }
380 setenv("LC_ALL", "", 1);
381
382 // Explicit check that LC_ALL sets regardless of LC_* and LANG.
383 if (!setenv("LANG", "es_MX", 1) && !setenv("LC_COLLATE", "de_DE", 1))
384 {
385 if (!setenv("LC_ALL", "en_PH", 1))
386 {
387 locale loc("");
388 VERIFY( loc.name() == "en_PH" );
389 }
390 setenv("LC_ALL", "", 1);
391 setenv("LANG", LANG_orig ? LANG_orig : "", 1);
392 setenv("LC_COLLATE", LC_COLLATE_orig ? LC_COLLATE_orig : "", 1);
393 }
394
395 // NB: LANG checks all LC_* macro settings. As such, all LC_* macros
396 // must be cleared for these tests, and then restored.
397 setenv("LC_ALL", "", 1);
398 setenv("LC_CTYPE", "", 1);
399 setenv("LC_NUMERIC", "", 1);
400 setenv("LC_TIME", "", 1);
401 setenv("LC_COLLATE", "", 1);
402 setenv("LC_MONETARY", "", 1);
403 setenv("LC_MESSAGES", "", 1);
404 #if _GLIBCPP_NUM_CATEGORIES
405 setenv("LC_PAPER", "", 1);
406 setenv("LC_NAME", "", 1);
407 setenv("LC_ADDRESS", "", 1);
408 setenv("LC_TELEPHONE", "", 1);
409 setenv("LC_MEASUREMENT", "", 1);
410 setenv("LC_IDENTIFICATION", "", 1);
411 #endif
412
413 // Check the default set by LANG.
414 if (!setenv("LANG", "fr_FR", 1))
415 {
416 locale loc("");
417 VERIFY( loc.name() == "fr_FR" );
418 }
419
420 // Check that a "POSIX" LANG is equivalent to "C".
421 if (!setenv("LANG", "POSIX", 1))
422 {
423 locale loc("");
424 VERIFY( loc.name() == "C" );
425 }
426
427 // Setting a category in the "C" default.
428 if (!setenv("LC_COLLATE", "de_DE", 1))
429 {
430 locale loc("");
431
432 #if _GLIBCPP_NUM_CATEGORIES
433 VERIFY( loc.name() == "LC_CTYPE=C;LC_NUMERIC=C;LC_TIME=C;"
434 "LC_COLLATE=de_DE;LC_MONETARY=C;LC_MESSAGES=C;LC_PAPER=C;"
435 "LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;"
436 "LC_IDENTIFICATION=C" );
437 #else
438 VERIFY( loc.name() == "LC_CTYPE=C;LC_NUMERIC=C;LC_TIME=C;"
439 "LC_COLLATE=de_DE;LC_MONETARY=C;LC_MESSAGES=C" );
440 #endif
441 }
442
443 // Changing the LANG default while LC_COLLATE is set.
444 if (!setenv("LANG", "fr_FR", 1))
445 {
446 locale loc("");
447 #if _GLIBCPP_NUM_CATEGORIES
448 VERIFY( loc.name() == "LC_CTYPE=fr_FR;LC_NUMERIC=fr_FR;"
449 "LC_TIME=fr_FR;LC_COLLATE=de_DE;LC_MONETARY=fr_FR;"
450 "LC_MESSAGES=fr_FR;LC_PAPER=fr_FR;LC_NAME=fr_FR;"
451 "LC_ADDRESS=fr_FR;LC_TELEPHONE=fr_FR;LC_MEASUREMENT=fr_FR;"
452 "LC_IDENTIFICATION=fr_FR" );
453 #else
454 VERIFY( loc.name() == "LC_CTYPE=fr_FR;LC_NUMERIC=fr_FR;"
455 "LC_TIME=fr_FR;LC_COLLATE=de_DE;LC_MONETARY=fr_FR;"
456 "LC_MESSAGES=fr_FR" );
457 #endif
458 }
459
460 // Changing another (C only) category.
461 #if _GLIBCPP_NUM_CATEGORIES
462 if (!setenv("LC_IDENTIFICATION", "it_IT", 1))
463 {
464 locale loc("");
465 VERIFY( loc.name() == "LC_CTYPE=fr_FR;LC_NUMERIC=fr_FR;"
466 "LC_TIME=fr_FR;LC_COLLATE=de_DE;LC_MONETARY=fr_FR;"
467 "LC_MESSAGES=fr_FR;LC_PAPER=fr_FR;LC_NAME=fr_FR;"
468 "LC_ADDRESS=fr_FR;LC_TELEPHONE=fr_FR;LC_MEASUREMENT=fr_FR;"
469 "LC_IDENTIFICATION=it_IT" );
470 }
471 #endif
472
473 // Restore the environment.
474 setenv("LANG", LANG_orig ? LANG_orig : "", 1);
475 setenv("LC_ALL", LC_ALL_orig ? LC_ALL_orig : "", 1);
476 setenv("LC_CTYPE", LC_CTYPE_orig ? LC_CTYPE_orig : "", 1);
477 setenv("LC_NUMERIC", LC_NUMERIC_orig ? LC_NUMERIC_orig : "", 1);
478 setenv("LC_TIME", LC_TIME_orig ? LC_TIME_orig : "", 1);
479 setenv("LC_COLLATE", LC_COLLATE_orig ? LC_COLLATE_orig : "", 1);
480 setenv("LC_MONETARY", LC_MONETARY_orig ? LC_MONETARY_orig : "", 1);
481 setenv("LC_MESSAGES", LC_MESSAGES_orig ? LC_MESSAGES_orig : "", 1);
482 #if _GLIBCPP_NUM_CATEGORIES
483 setenv("LC_PAPER", LC_PAPER_orig ? LC_PAPER_orig : "", 1);
484 setenv("LC_NAME", LC_NAME_orig ? LC_NAME_orig : "", 1);
485 setenv("LC_ADDRESS", LC_ADDRESS_orig ? LC_ADDRESS_orig : "", 1);
486 setenv("LC_TELEPHONE", LC_TELEPHONE_orig ? LC_TELEPHONE_orig : "", 1);
487 setenv("LC_MEASUREMENT", LC_MEASUREMENT_orig ? LC_MEASUREMENT_orig : "", 1);
488 setenv("LC_IDENTIFICATION",
489 LC_IDENTIFICATION_orig ? LC_IDENTIFICATION_orig : "", 1);
490 #endif
491
492 #endif
493 }
494
495 int main()
496 {
497 test00();
498
499 #if _GLIBCPP_USE___ENC_TRAITS
500 test01();
501 #endif
502
503 test02();
504 test03();
505 test04();
506
507 return 0;
508 }