1 // 2000-09-13 Benjamin Kosnik <bkoz@redhat.com>
3 // Copyright (C) 2000, 2001, 2002, 2003 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>
28 #if _GLIBCXX_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
;
34 class gnu_codecvt
: public c_codecvt
{ };
36 class gnu_facet
: public std::locale::facet
39 static std::locale::id id
;
42 std::locale::id
gnu_facet::id
;
44 // Need some char_traits specializations for this to work.
45 typedef unsigned short unicode_t
;
50 struct char_traits
<unicode_t
>
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
;
60 assign(char_type
& __c1
, const char_type
& __c2
);
63 eq(const char_type
& __c1
, const char_type
& __c2
);
66 lt(const char_type
& __c1
, const char_type
& __c2
);
69 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
70 { return memcmp(__s1
, __s2
, __n
); }
73 length(const char_type
* __s
);
75 static const char_type
*
76 find(const char_type
* __s
, size_t __n
, const char_type
& __a
);
79 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
);
82 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
83 { return static_cast<char_type
*>(memcpy(__s1
, __s2
, __n
)); }
86 assign(char_type
* __s
, size_t __n
, char_type __a
);
89 to_char_type(const int_type
& __c
);
92 to_int_type(const char_type
& __c
);
95 eq_int_type(const int_type
& __c1
, const int_type
& __c2
);
101 not_eof(const int_type
& __c
);
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
;
116 // construct a locale object with the C facet
117 const locale loc01
= locale::classic();
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() == "*");
127 VERIFY (has_facet
<gnu_codecvt
>(loc02
));
128 VERIFY (has_facet
<c_codecvt
>(loc02
));
129 VERIFY (has_facet
<w_codecvt
>(loc02
));
135 { use_facet
<gnu_facet
>(loc02
); }
142 locale
loc13(locale::classic(), new unicode_codecvt
);
143 VERIFY (loc01
!= loc13
);
144 VERIFY (loc13
.name() == "*");
147 VERIFY (has_facet
<c_codecvt
>(loc13
));
148 VERIFY (has_facet
<w_codecvt
>(loc13
));
149 VERIFY (has_facet
<unicode_codecvt
>(loc13
));
155 { use_facet
<gnu_facet
>(loc13
); }
164 VERIFY (loc03
== loc01
);
165 VERIFY (loc03
.name() == "C");
166 locale loc04
= locale::global(loc02
);
168 VERIFY (loc05
!= loc03
);
169 VERIFY (loc05
== loc02
);
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");
178 VERIFY (loc07
!= loc02
);
179 VERIFY (loc07
.name() != "");
181 { locale
loc08(static_cast<const char*>(NULL
)); }
182 catch(runtime_error
& obj
)
188 { locale
loc08("saturn_SUN*RA"); }
189 catch(runtime_error
& obj
)
195 // locale(const locale& other, const char* std_name, category)
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
);
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
);
210 locale
loc11(loc01
, "C", locale::ctype
);
211 VERIFY (loc11
.name() == "C");
212 VERIFY (loc11
== loc01
);
215 { locale
loc12(loc01
, static_cast<const char*>(NULL
), locale::ctype
); }
216 catch(runtime_error
& obj
)
222 { locale
loc13(loc01
, "localized by the wu-tang clan", locale::ctype
); }
223 catch(runtime_error
& obj
)
228 locale
loc14(loc06
, "C", locale::none
);
229 VERIFY (loc14
.name() == "fr_FR");
230 VERIFY (loc14
== loc06
);
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
);
243 // locale(const locale& other, const locale& one, category)
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
);
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
);
258 locale
loc11(loc01
, loc01
, locale::ctype
);
259 VERIFY (loc11
.name() == "C");
260 VERIFY (loc11
== loc01
);
263 { locale
loc12(loc01
, static_cast<const char*>(NULL
), locale::ctype
); }
264 catch(runtime_error
& obj
)
270 { locale
loc13(loc01
, locale("wu-tang clan"), locale::ctype
); }
271 catch(runtime_error
& obj
)
276 locale
loc14(loc06
, loc01
, locale::none
);
277 VERIFY (loc14
.name() == "fr_FR");
278 VERIFY (loc14
== loc06
);
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
);
290 #endif // _GLIBCXX_USE___ENC_TRAITS
294 #if _GLIBCXX_USE___ENC_TRAITS