xref: /openbsd-src/gnu/lib/libstdc++/libstdc++/testsuite/22_locale/facet.cc (revision 03a78d155d6fff5698289342b62759a75b20d130)
1 // 2000-08-31 Benjamin Kosnik <bkoz@redhat.com>
2 
3 // Copyright (C) 2000, 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.1.2 - class locale::facet [lib.locale.facet]
22 
23 #include <cwchar> // for mbstate_t
24 #include <locale>
25 #include <stdexcept>
26 #include <string>
27 #include <iterator>
28 #include <limits>
29 #include <testsuite_hooks.h>
30 
31 // 1 a class if a facet if it is publicly derived from another facet
32 typedef std::istreambuf_iterator<char>		input_iterator;
33 typedef std::ostreambuf_iterator<char>		output_iterator;
34 
35 class gnu_collate: public std::collate<char> { };
36 class gnu_ctype: public std::ctype<char> { };
37 class gnu_codecvt: public std::codecvt<char, char, std::mbstate_t> { };
38 class gnu_moneypunct: public std::moneypunct<char> { };
39 class gnu_moneypunct_true: public std::moneypunct<char, true> { };
40 class gnu_money_get: public std::money_get<char> { };
41 class gnu_money_put: public std::money_put<char> { };
42 class gnu_numpunct: public std::numpunct<char> { };
43 class gnu_num_get: public std::num_get<char> { };
44 class gnu_num_put: public std::num_put<char> { };
45 class gnu_time_get: public std::time_get<char> { };
46 class gnu_time_put: public std::time_put<char> { };
47 class gnu_messages: public std::messages<char> { };
48 
49 class gnu_collate_byname: public std::collate_byname<char>
50 {
51 public:
52   explicit
gnu_collate_byname(const char * c,size_t refs=0)53   gnu_collate_byname(const char* c, size_t refs = 0)
54   : std::collate_byname<char>(c, refs) { }
55 };
56 
57 class gnu_ctype_byname: public std::ctype_byname<char>
58 {
59 public:
60   explicit
gnu_ctype_byname(const char * c,size_t refs=0)61   gnu_ctype_byname(const char* c, size_t refs = 0)
62   : std::ctype_byname<char>(c, refs) { }
63 };
64 
65 class gnu_moneypunct_byname_true: public std::moneypunct_byname<char, true>
66 {
67 public:
68   explicit
gnu_moneypunct_byname_true(const char * c,size_t refs=0)69   gnu_moneypunct_byname_true(const char* c, size_t refs = 0)
70   : std::moneypunct_byname<char, true>(c, refs) { }
71 };
72 
73 class gnu_moneypunct_byname_false: public std::moneypunct_byname<char, false>
74 {
75 public:
76   explicit
gnu_moneypunct_byname_false(const char * c,size_t refs=0)77   gnu_moneypunct_byname_false(const char* c, size_t refs = 0)
78   : std::moneypunct_byname<char, false>(c, refs) { }
79 };
80 
81 
82 class gnu_money_get_in: public std::money_get<char, input_iterator>
83 {
84 public:
85   explicit
gnu_money_get_in(size_t refs=0)86   gnu_money_get_in(size_t refs = 0)
87   : std::money_get<char, input_iterator>(refs) { }
88 };
89 
90 class gnu_money_put_out: public std::money_put<char, output_iterator>
91 {
92 public:
93   explicit
gnu_money_put_out(size_t refs=0)94   gnu_money_put_out(size_t refs = 0)
95   : std::money_put<char, output_iterator>(refs) { }
96 };
97 
98 class gnu_numpunct_byname: public std::numpunct_byname<char>
99 {
100 public:
101   explicit
gnu_numpunct_byname(const char * c,size_t refs=0)102   gnu_numpunct_byname(const char* c, size_t refs = 0)
103   : std::numpunct_byname<char>(c, refs) { }
104 };
105 
106 class gnu_num_get_in: public std::num_get<char, input_iterator>
107 {
108 public:
109   explicit
gnu_num_get_in(size_t refs=0)110   gnu_num_get_in(size_t refs = 0)
111   : std::num_get<char, input_iterator>(refs) { }
112 };
113 
114 class gnu_num_put_out: public std::num_put<char, output_iterator>
115 {
116 public:
117   explicit
gnu_num_put_out(size_t refs=0)118   gnu_num_put_out(size_t refs = 0)
119   : std::num_put<char, output_iterator>(refs) { }
120 };
121 
122 class gnu_time_get_byname: public std::time_get_byname<char>
123 {
124 public:
125   explicit
gnu_time_get_byname(const char * c,size_t refs=0)126   gnu_time_get_byname(const char* c, size_t refs = 0)
127   : std::time_get_byname<char>(c, refs) { }
128 };
129 
130 class gnu_time_get_in: public std::time_get<char, input_iterator>
131 {
132 public:
133   explicit
gnu_time_get_in(size_t refs=0)134   gnu_time_get_in(size_t refs = 0)
135   : std::time_get<char, input_iterator>(refs) { }
136 };
137 
138 class gnu_time_put_byname: public std::time_put_byname<char>
139 {
140 public:
141   explicit
gnu_time_put_byname(const char * c,size_t refs=0)142   gnu_time_put_byname(const char* c, size_t refs = 0)
143   : std::time_put_byname<char>(c, refs) { }
144 };
145 
146 class gnu_time_put_out: public std::time_put<char, output_iterator>
147 {
148 public:
149   explicit
gnu_time_put_out(size_t refs=0)150   gnu_time_put_out(size_t refs = 0)
151   : std::time_put<char, output_iterator>(refs) { }
152 };
153 
154 class gnu_messages_byname: public std::messages_byname<char>
155 {
156 public:
157   explicit
gnu_messages_byname(const char * c,size_t refs=0)158   gnu_messages_byname(const char* c, size_t refs = 0)
159   : std::messages_byname<char>(c, refs) { }
160 };
161 
162 
163 // 2 or if it is a class derived from locale:;facet and containing a
164 // publicly-accessible declaration as follows:
165 class gnu_facet: public std::locale::facet
166 {
167 public:
168   static std::locale::id id;
169 };
170 
171 std::locale::id gnu_facet::id;
172 
test01()173 void test01()
174 {
175   bool test = true;
176   const std::string name_no("*");
177   const std::string name_c("C");
178 
179   try
180     {
181       gnu_collate 			obj01;
182       gnu_ctype 			obj02;
183       gnu_codecvt 			obj03;
184       gnu_moneypunct			obj04;
185       gnu_moneypunct_true		obj05;
186       gnu_money_get			obj06;
187       gnu_money_put			obj07;
188       gnu_numpunct			obj08;
189       gnu_num_get			obj09;
190       gnu_num_put			obj10;
191       gnu_time_get			obj11;
192       gnu_time_put			obj12;
193       gnu_messages			obj13;
194       gnu_time_put_out			obj14(0);
195       gnu_time_put_byname		obj15("C", 0);
196       gnu_time_get_in			obj16(0);
197       gnu_time_get_byname		obj17("C", 0);
198       gnu_num_put_out			obj18(0);
199       gnu_num_get_in			obj19(0);
200       gnu_numpunct_byname		obj20("C", 0);
201       gnu_money_put_out			obj21(0);
202       gnu_money_get_in			obj22(0);
203       gnu_moneypunct_byname_false	obj23("C", 0);
204       gnu_moneypunct_byname_true	obj24("C", 0);
205       gnu_ctype_byname			obj25("C", 0);
206       gnu_collate_byname		obj26("C", 0);
207       gnu_messages_byname		obj27("C", 0);
208     }
209   catch (std::runtime_error& obj)
210     {
211       // named locale not valid
212       VERIFY( false );
213     }
214   catch (std::exception& obj)
215     {
216       // some other error
217       VERIFY( false );
218     }
219 
220   // 2
221   try
222     {
223       gnu_facet 			obj28;
224     }
225   catch (std::runtime_error& obj)
226     {
227       // named locale not valid
228       VERIFY( false );
229     }
230   catch (std::exception& obj)
231     {
232       // some other error
233       VERIFY( false );
234     }
235 }
236 
237 // Static counter for use in checking ctors/dtors.
238 static std::size_t counter;
239 
240 class surf : public std::locale::facet
241 {
242 public:
243   static std::locale::id 	       	id;
surf(size_t refs=0)244   surf(size_t refs = 0): std::locale::facet(refs) { ++counter; }
~surf()245   ~surf() { --counter; }
246 };
247 
248 std::locale::id surf::id;
249 
250 typedef surf facet_type;
251 
test02()252 void test02()
253 {
254   using namespace std;
255   bool test = true;
256 
257   // 1: Destroyed when out of scope.
258   VERIFY( counter == 0 );
259   {
260     locale loc01(locale::classic(), new facet_type);
261     VERIFY( counter == 1 );
262   }
263   VERIFY( counter == 0 );
264 
265   // 2: Not destroyed when out of scope, deliberately leaked.
266   VERIFY( counter == 0 );
267   {
268     // Default refs argument is zero.
269     locale loc02(locale::classic(), new facet_type(1));
270     VERIFY( counter == 1 );
271   }
272   VERIFY( counter == 1 );
273 
274   // 3: Pathological.
275   counter = 0;
276   {
277     // Test bounds.
278     facet_type* f = new facet_type(numeric_limits<size_t>::max());
279     VERIFY( counter == 1 );
280     // Add a reference.
281     locale loc01(locale::classic(), f);
282     {
283       // Add another reference...
284       locale loc02(locale::classic(), f);
285     }
286     VERIFY( counter == 1 );
287   }
288 
289   // 4: Named locale should destroy facets when it goes out of scope.
290   // Not quite sure how to test for this w/o valgrind at the moment.
291   {
292     locale loc03("es_MX");
293   }
294 }
295 
main()296 int main ()
297 {
298   test01();
299 
300   test02();
301   return 0;
302 }
303