OSDN Git Service

* java/awt/image/ColorModel.java (getAlpha(Object)): Call
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 22_locale / num_put_members_wchar_t.cc
1 // 2001-11-19 Benjamin Kosnik  <bkoz@redhat.com>
2
3 // Copyright (C) 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.2.2.2.1  num_put members
22
23 #include <locale>
24 #include <sstream>
25 #include <testsuite_hooks.h>
26
27 // XXX This test is not working for non-glibc locale models.
28 // { dg-do run { xfail *-*-* } }
29
30 #ifdef _GLIBCPP_USE_WCHAR_T
31 void test01()
32 {
33   using namespace std;
34   typedef ostreambuf_iterator<wchar_t> iterator_type;
35
36   bool test = true;
37
38   // basic construction
39   locale loc_c = locale::classic();
40   locale loc_hk("en_HK");
41   locale loc_fr("fr_FR@euro");
42   locale loc_de("de_DE");
43   VERIFY( loc_c != loc_de );
44   VERIFY( loc_hk != loc_fr );
45   VERIFY( loc_hk != loc_de );
46   VERIFY( loc_de != loc_fr );
47
48   // cache the numpunct facets
49   const numpunct<wchar_t>& numpunct_c = use_facet<numpunct<wchar_t> >(loc_c); 
50   const numpunct<wchar_t>& numpunct_de = use_facet<numpunct<wchar_t> >(loc_de); 
51   const numpunct<wchar_t>& numpunct_hk = use_facet<numpunct<wchar_t> >(loc_hk); 
52
53   // sanity check the data is correct.
54   const wstring empty;
55   wstring result1;
56   wstring result2;
57   wchar_t c;
58
59   bool b1 = true;
60   bool b0 = false;
61   long l1 = 2147483647;
62   long l2 = -2147483647;
63   unsigned long ul1 = 1294967294;
64   unsigned long ul2 = 0;
65   double d1 =  1.7976931348623157e+308;
66   double d2 = 2.2250738585072014e-308;
67   long double ld1 = 1.7976931348623157e+308;
68   long double ld2 = 2.2250738585072014e-308;
69   const void* cv = &ld1;
70
71   // cache the num_put facet
72   wostringstream oss;
73   oss.imbue(loc_de);
74   const num_put<wchar_t>& np = use_facet<num_put<wchar_t> >(oss.getloc()); 
75
76   // bool, simple
77   iterator_type os_it00 = oss.rdbuf();
78   iterator_type os_it01 = np.put(os_it00, oss, '+', b1);
79   result1 = oss.str();
80   VERIFY( result1 == L"1" );
81   //  VERIFY( os_it00 != os_it01 );
82
83   oss.str(empty);
84   np.put(oss.rdbuf(), oss, L'+', b0);
85   result2 = oss.str();
86   VERIFY( result2 == L"0" );
87
88   // bool, more twisted examples
89   oss.imbue(loc_c);
90   oss.str(empty);
91   oss.width(20);
92   oss.setf(ios_base::right, ios_base::adjustfield);
93   np.put(oss.rdbuf(), oss, L'+', b0);
94   result1 = oss.str();
95   VERIFY( result1 == L"+++++++++++++++++++0" );
96
97   oss.str(empty);
98   oss.width(20);
99   oss.setf(ios_base::left, ios_base::adjustfield);
100   oss.setf(ios_base::boolalpha);
101   np.put(oss.rdbuf(), oss, L'+', b1);
102   result2 = oss.str();
103   VERIFY( result2 == L"true++++++++++++++++" );
104
105   // long, in a locale that expects grouping
106   oss.imbue(loc_hk);
107   oss.str(empty);
108   oss.clear();
109   np.put(oss.rdbuf(), oss, L'+', l1);
110   result1 = oss.str();
111   VERIFY( result1 == L"2,147,483,647" );
112
113   oss.str(empty);
114   oss.clear();
115   oss.width(20);
116   oss.setf(ios_base::left, ios_base::adjustfield);
117   np.put(oss.rdbuf(), oss, L'+', l2);
118   result1 = oss.str();
119   VERIFY( result1 == L"-2,147,483,647++++++" );
120
121   // unsigned long, in a locale that does not group
122   oss.imbue(loc_c);
123   oss.str(empty);
124   oss.clear();
125   np.put(oss.rdbuf(), oss, L'+', ul1);
126   result1 = oss.str();
127   VERIFY( result1 == L"1294967294" );
128
129   oss.str(empty);
130   oss.clear();
131   oss.width(20);
132   oss.setf(ios_base::left, ios_base::adjustfield);
133   np.put(oss.rdbuf(), oss, L'+', ul2);
134   result1 = oss.str();
135   VERIFY( result1 == L"0+++++++++++++++++++" );
136
137   // ... and one that does
138   oss.imbue(loc_de);
139   oss.str(empty);
140   oss.clear();
141   oss.width(20);
142   oss.setf(ios_base::left, ios_base::adjustfield);
143   np.put(oss.rdbuf(), oss, L'+', ul1);
144   result1 = oss.str();
145   VERIFY( result1 == L"1.294.967.294+++++++" );
146
147   // double
148   oss.str(empty);
149   oss.clear();
150   oss.width(20);
151   oss.setf(ios_base::left, ios_base::adjustfield);
152   np.put(oss.rdbuf(), oss, L'+', d1);
153   result1 = oss.str();
154   VERIFY( result1 == L"1,79769e+308++++++++" );
155
156   oss.str(empty);
157   oss.clear();
158   oss.width(20);
159   oss.setf(ios_base::right, ios_base::adjustfield);
160   np.put(oss.rdbuf(), oss, L'+', d2);
161   result1 = oss.str();
162   VERIFY( result1 == L"++++++++2,22507e-308" );
163
164   oss.str(empty);
165   oss.clear();
166   oss.width(20);
167   oss.setf(ios_base::right, ios_base::adjustfield);
168   oss.setf(ios_base::scientific, ios_base::floatfield);
169   np.put(oss.rdbuf(), oss, L'+', d2);
170   result2 = oss.str();
171   VERIFY( result2 == L"+++++++2,225074e-308" );
172
173   oss.str(empty);
174   oss.clear();
175   oss.width(20);
176   oss.precision(10);
177   oss.setf(ios_base::right, ios_base::adjustfield);
178   oss.setf(ios_base::scientific, ios_base::floatfield);
179   oss.setf(ios_base::uppercase);
180   np.put(oss.rdbuf(), oss, L'+', d2);
181   result1 = oss.str();
182   VERIFY( result1 == L"+++2,2250738585E-308" );
183
184   // long double
185   oss.str(empty);
186   oss.clear();
187   np.put(oss.rdbuf(), oss, L'+', ld1);
188   result1 = oss.str();
189   VERIFY( result1 == L"1,7976931349E+308" );
190
191   oss.str(empty);
192   oss.clear();
193   oss.precision(0);
194   oss.setf(ios_base::fixed, ios_base::floatfield);
195   np.put(oss.rdbuf(), oss, L'+', ld2);
196   result1 = oss.str();
197   VERIFY( result1 == L"0" );
198
199   // const void
200   oss.str(empty);
201   oss.clear();
202   np.put(oss.rdbuf(), oss, L'+', cv);
203   result1 = oss.str();
204   // No grouping characters.
205   VERIFY( !char_traits<wchar_t>::find(result1.c_str(), 
206                                    numpunct_de.decimal_point(), 
207                                    result1.size()) );
208   // Should contain an 'x'.
209   VERIFY( result1.find(L'x') == 1 );
210
211 #ifdef _GLIBCPP_USE_LONG_LONG
212   long long ll1 = 9223372036854775807;
213   long long ll2 = -9223372036854775807;
214
215   oss.str(empty);
216   oss.clear();
217   np.put(oss.rdbuf(), oss, '+', ll1);
218   result1 = oss.str();
219   VERIFY( result1 == L"9.223.372.036.854.775.807" );
220 #endif
221 }
222 void test02()
223 {
224   using namespace std;
225   bool test = true;
226
227   // Check num_put works with other iterators besides streambuf
228   // output iterators. (As long as output_iterator requirements are met.)
229   typedef wstring::iterator iter_type;
230   typedef char_traits<wchar_t> traits;
231   typedef num_put<wchar_t, iter_type> num_put_type;
232   const ios_base::iostate goodbit = ios_base::goodbit;
233   const ios_base::iostate eofbit = ios_base::eofbit;
234   const locale loc_c = locale::classic();
235   const wstring str(L"1798 Lady Elgin");
236   const wstring str2(L"0 true 0xbffff74c Mary Nisbet");
237   const wstring x(15, L'x'); // have to have allocated string!
238   wstring res;
239
240   wostringstream oss; 
241   oss.imbue(locale(loc_c, new num_put_type));
242
243   // Iterator advanced, state, output.
244   const num_put_type& tp = use_facet<num_put_type>(oss.getloc());
245
246   // 01 put(long)
247   // 02 put(long double)
248   // 03 put(bool)
249   // 04 put(void*)
250
251   // 01 put(long)
252   const long l = 1798;
253   res = x;
254   iter_type ret1 = tp.put(res.begin(), oss, L' ', l);
255   wstring sanity1(res.begin(), ret1);
256   VERIFY( res == L"1798xxxxxxxxxxx" );
257   VERIFY( sanity1 == L"1798" );
258
259   // 02 put(long double)
260   const long double ld = 1798;
261   res = x;
262   iter_type ret2 = tp.put(res.begin(), oss, L' ', ld);
263   wstring sanity2(res.begin(), ret2);
264   VERIFY( res == L"1798xxxxxxxxxxx" );
265   VERIFY( sanity2 == L"1798" );
266
267   // 03 put(bool)
268   bool b = 1;
269   res = x;
270   iter_type ret3 = tp.put(res.begin(), oss, L' ', b);
271   wstring sanity3(res.begin(), ret3);
272   VERIFY( res == L"1xxxxxxxxxxxxxx" );
273   VERIFY( sanity3 == L"1" );
274
275   b = 0;
276   res = x;
277   oss.setf(ios_base::boolalpha);
278   iter_type ret4 = tp.put(res.begin(), oss, L' ', b);
279   wstring sanity4(res.begin(), ret4);
280   VERIFY( res == L"falsexxxxxxxxxx" );
281   VERIFY( sanity4 == L"false" );
282
283   // 04 put(void*)
284   oss.clear();
285   const void* cv = &ld;
286   res = x;
287   oss.setf(ios_base::fixed, ios_base::floatfield);
288   iter_type ret5 = tp.put(res.begin(), oss, L' ', cv);
289   wstring sanity5(res.begin(), ret5);
290   VERIFY( sanity5.size() );
291   VERIFY( sanity5[1] == L'x' );
292 }
293
294 // libstdc++/5280
295 void test03()
296 {
297 #ifdef _GLIBCPP_HAVE_SETENV 
298   // Set the global locale to non-"C".
299   std::locale loc_de("de_DE");
300   std::locale::global(loc_de);
301
302   // Set LANG environment variable to de_DE.
303   const char* oldLANG = getenv("LANG");
304   if (!setenv("LANG", "de_DE", 1))
305     {
306       test01();
307       test02();
308       setenv("LANG", oldLANG, 1);
309     }
310 #endif
311 }
312 #endif
313
314 int main()
315 {
316 #ifdef _GLIBCPP_USE_WCHAR_T
317   test01();
318   test02();
319   test03();
320 #endif
321   return 0;
322 }
323
324
325 // Diana D. Brooks, former chief executive of Sotheby's
326 // art-thief extraordinaire
327