2003-07-04 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / testsuite / 22_locale / locale / cons / 2.cc
blob15ea686141e345904bc222b165f8a355350d95ba
1 // 2000-09-13 Benjamin Kosnik <bkoz@redhat.com>
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.
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,
19 // USA.
21 // 22.1.1.2 locale constructors and destructors [lib.locale.cons]
23 #include <cwchar> // for mbstate_t
24 #include <locale>
25 #include <stdexcept>
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
38 public:
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;
47 namespace std
49 template<>
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;
59 static void
60 assign(char_type& __c1, const char_type& __c2);
62 static bool
63 eq(const char_type& __c1, const char_type& __c2);
65 static bool
66 lt(const char_type& __c1, const char_type& __c2);
68 static int
69 compare(const char_type* __s1, const char_type* __s2, size_t __n)
70 { return memcmp(__s1, __s2, __n); }
72 static size_t
73 length(const char_type* __s);
75 static const char_type*
76 find(const char_type* __s, size_t __n, const char_type& __a);
78 static char_type*
79 move(char_type* __s1, const char_type* __s2, size_t __n);
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)); }
85 static char_type*
86 assign(char_type* __s, size_t __n, char_type __a);
88 static char_type
89 to_char_type(const int_type& __c);
91 static int_type
92 to_int_type(const char_type& __c);
94 static bool
95 eq_int_type(const int_type& __c1, const int_type& __c2);
97 static int_type
98 eof();
100 static int_type
101 not_eof(const int_type& __c);
105 void test01()
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;
113 bool test = true;
114 string str1, str2;
116 // construct a locale object with the C facet
117 const locale loc01 = locale::classic();
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() == "*");
127 VERIFY (has_facet<gnu_codecvt>(loc02));
128 VERIFY (has_facet<c_codecvt>(loc02));
129 VERIFY (has_facet<w_codecvt>(loc02));
131 catch(...)
132 { VERIFY( false ); }
134 try
135 { use_facet<gnu_facet>(loc02); }
136 catch(bad_cast& obj)
137 { VERIFY( true ); }
138 catch(...)
139 { VERIFY( false ); }
141 // unicode_codecvt
142 locale loc13(locale::classic(), new unicode_codecvt);
143 VERIFY (loc01 != loc13);
144 VERIFY (loc13.name() == "*");
145 try
147 VERIFY (has_facet<c_codecvt>(loc13));
148 VERIFY (has_facet<w_codecvt>(loc13));
149 VERIFY (has_facet<unicode_codecvt>(loc13));
151 catch(...)
152 { VERIFY( false ); }
154 try
155 { use_facet<gnu_facet>(loc13); }
156 catch(bad_cast& obj)
157 { VERIFY( true ); }
158 catch(...)
159 { VERIFY( false ); }
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);
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() != "");
181 { locale loc08(static_cast<const char*>(NULL)); }
182 catch(runtime_error& obj)
183 { VERIFY (true); }
184 catch(...)
185 { VERIFY (false); }
188 { locale loc08("saturn_SUN*RA"); }
189 catch(runtime_error& obj)
190 { VERIFY (true); }
191 catch(...)
192 { VERIFY (false); }
194 // 4
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)
217 { VERIFY (true); }
218 catch(...)
219 { VERIFY (false); }
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); }
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);
242 // 5
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)
265 { VERIFY (true); }
266 catch(...)
267 { VERIFY (false); }
270 { locale loc13(loc01, locale("wu-tang clan"), locale::ctype); }
271 catch(runtime_error& obj)
272 { VERIFY (true); }
273 catch(...)
274 { VERIFY (false); }
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
292 int main()
294 #if _GLIBCXX_USE___ENC_TRAITS
295 test01();
296 #endif
297 return 0;