]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/22_locale/ctor_copy_dtor.cc
1 // 2000-09-13 Benjamin Kosnik <bkoz@redhat.com>
3 // Copyright (C) 2000, 2001, 2002 Free Software Foundation
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)
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,
21 // 22.1.1.2 locale constructors and destructors [lib.locale.cons]
23 #include <cwchar> // for mbstate_t
26 #include <testsuite_hooks.h>
30 // Should be able to do this as the first thing that happens in a
31 // file and have it not crash.
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
;
41 class gnu_codecvt
: public c_codecvt
{ };
43 class gnu_facet
: public std::locale::facet
46 static std::locale::id id
;
49 std::locale::id
gnu_facet::id
;
51 // Need some char_traits specializations for this to work.
52 typedef unsigned short unicode_t
;
57 struct char_traits
<unicode_t
>
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
;
67 assign(char_type
& __c1
, const char_type
& __c2
);
70 eq(const char_type
& __c1
, const char_type
& __c2
);
73 lt(const char_type
& __c1
, const char_type
& __c2
);
76 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
77 { return memcmp(__s1
, __s2
, __n
); }
80 length(const char_type
* __s
);
82 static const char_type
*
83 find(const char_type
* __s
, size_t __n
, const char_type
& __a
);
86 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
);
89 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
90 { return static_cast<char_type
*>(memcpy(__s1
, __s2
, __n
)); }
93 assign(char_type
* __s
, size_t __n
, char_type __a
);
96 to_char_type(const int_type
& __c
);
99 to_int_type(const char_type
& __c
);
102 eq_int_type(const int_type
& __c1
, const int_type
& __c2
);
108 not_eof(const int_type
& __c
);
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
;
123 // construct a locale object with the C facet
124 const locale loc01
= locale::classic();
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() == "*");
134 VERIFY (has_facet
<gnu_codecvt
>(loc02
));
135 VERIFY (has_facet
<c_codecvt
>(loc02
));
136 VERIFY (has_facet
<w_codecvt
>(loc02
));
142 { use_facet
<gnu_facet
>(loc02
); }
149 locale
loc13(locale::classic(), new unicode_codecvt
);
150 VERIFY (loc01
!= loc13
);
151 VERIFY (loc13
.name() == "*");
154 VERIFY (has_facet
<c_codecvt
>(loc13
));
155 VERIFY (has_facet
<w_codecvt
>(loc13
));
156 VERIFY (has_facet
<unicode_codecvt
>(loc13
));
162 { use_facet
<gnu_facet
>(loc13
); }
171 VERIFY (loc03
== loc01
);
172 VERIFY (loc03
.name() == "C");
173 locale loc04
= locale::global(loc02
);
175 VERIFY (loc05
!= loc03
);
176 VERIFY (loc05
== loc02
);
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");
185 VERIFY (loc07
!= loc02
);
186 VERIFY (loc07
.name() != "");
188 { locale
loc08(static_cast<const char*>(NULL
)); }
189 catch(runtime_error
& obj
)
195 { locale
loc08("saturn_SUN*RA"); }
196 catch(runtime_error
& obj
)
202 // locale(const locale& other, const char* std_name, category)
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
);
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
);
217 locale
loc11(loc01
, "C", locale::ctype
);
218 VERIFY (loc11
.name() == "C");
219 VERIFY (loc11
== loc01
);
222 { locale
loc12(loc01
, static_cast<const char*>(NULL
), locale::ctype
); }
223 catch(runtime_error
& obj
)
229 { locale
loc13(loc01
, "localized by the wu-tang clan", locale::ctype
); }
230 catch(runtime_error
& obj
)
235 locale
loc14(loc06
, "C", locale::none
);
236 VERIFY (loc14
.name() == "fr_FR");
237 VERIFY (loc14
== loc06
);
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
);
250 // locale(const locale& other, const locale& one, category)
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
);
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
);
265 locale
loc11(loc01
, loc01
, locale::ctype
);
266 VERIFY (loc11
.name() == "C");
267 VERIFY (loc11
== loc01
);
270 { locale
loc12(loc01
, static_cast<const char*>(NULL
), locale::ctype
); }
271 catch(runtime_error
& obj
)
277 { locale
loc13(loc01
, locale("wu-tang clan"), locale::ctype
); }
278 catch(runtime_error
& obj
)
283 locale
loc14(loc06
, loc01
, locale::none
);
284 VERIFY (loc14
.name() == "fr_FR");
285 VERIFY (loc14
== loc06
);
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
);
297 #endif // _GLIBCPP_USE___ENC_TRAITS
303 std::locale
loc_c1("C");
304 std::locale
loc_c2 ("C");
306 std::locale
loc_1("");
307 std::locale
loc_2("");
309 VERIFY( loc_c1
== loc_c2
);
310 VERIFY( loc_1
== loc_2
);
317 #ifdef _GLIBCPP_HAVE_SETENV
318 const char* LC_ALL_orig
= getenv("LC_ALL");
319 if (!setenv("LC_ALL", "it_IT", 1))
322 VERIFY( loc
.name() == "it_IT" );
323 setenv("LC_ALL", LC_ALL_orig
? LC_ALL_orig
: "", 1);
329 // More tests for locale("") == POSIX locale::name.
335 #ifdef _GLIBCPP_HAVE_SETENV
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")) : "";
366 // Check that a "POSIX" LC_ALL is equivalent to "C".
367 if (!setenv("LC_ALL", "POSIX", 1))
370 VERIFY( loc
.name() == "C" );
372 setenv("LC_ALL", "", 1);
374 // Check that a "en_PH" LC_ALL is equivalent to "en_PH".
375 if (!setenv("LC_ALL", "en_PH", 1))
378 VERIFY( loc
.name() == "en_PH" );
380 setenv("LC_ALL", "", 1);
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))
385 if (!setenv("LC_ALL", "en_PH", 1))
388 VERIFY( loc
.name() == "en_PH" );
390 setenv("LC_ALL", "", 1);
391 setenv("LANG", LANG_orig
? LANG_orig
: "", 1);
392 setenv("LC_COLLATE", LC_COLLATE_orig
? LC_COLLATE_orig
: "", 1);
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);
413 // Check the default set by LANG.
414 if (!setenv("LANG", "fr_FR", 1))
417 VERIFY( loc
.name() == "fr_FR" );
420 // Check that a "POSIX" LANG is equivalent to "C".
421 if (!setenv("LANG", "POSIX", 1))
424 VERIFY( loc
.name() == "C" );
427 // Setting a category in the "C" default.
428 if (!setenv("LC_COLLATE", "de_DE", 1))
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" );
438 VERIFY( loc
.name() == "LC_CTYPE=C;LC_NUMERIC=C;LC_TIME=C;"
439 "LC_COLLATE=de_DE;LC_MONETARY=C;LC_MESSAGES=C" );
443 // Changing the LANG default while LC_COLLATE is set.
444 if (!setenv("LANG", "fr_FR", 1))
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" );
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" );
460 // Changing another (C only) category.
461 #if _GLIBCPP_NUM_CATEGORIES
462 if (!setenv("LC_IDENTIFICATION", "it_IT", 1))
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" );
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);
499 #if _GLIBCPP_USE___ENC_TRAITS