OSDN Git Service

8f82dc7afb8064bb931303e9310fb5e980248f64
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 22_locale / time_put_members_wchar_t.cc
1 // 2001-10-02 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.5.3.1 time_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   typedef char_traits<wchar_t> traits;
36
37   bool test = true;
38
39   // basic construction and sanity checks.
40   locale loc_c = locale::classic();
41   locale loc_hk("en_HK");
42   locale loc_fr("fr_FR@euro");
43   locale loc_de("de_DE");
44   VERIFY( loc_hk != loc_c );
45   VERIFY( loc_hk != loc_fr );
46   VERIFY( loc_hk != loc_de );
47   VERIFY( loc_de != loc_fr );
48
49   // cache the __timepunct facets, for quicker gdb inspection
50   const __timepunct<wchar_t>& time_c = use_facet<__timepunct<wchar_t> >(loc_c); 
51   const __timepunct<wchar_t>& time_de = use_facet<__timepunct<wchar_t> >(loc_de); 
52   const __timepunct<wchar_t>& time_hk = use_facet<__timepunct<wchar_t> >(loc_hk); 
53   const __timepunct<wchar_t>& time_fr = use_facet<__timepunct<wchar_t> >(loc_fr); 
54
55   // create an ostream-derived object, cache the time_put facet
56   const wstring empty;
57   wostringstream oss;
58   const time_put<wchar_t>& tim_put = use_facet<time_put<wchar_t> >(oss.getloc()); 
59
60   // create "C" time objects
61   tm time1 = { 0, 0, 12, 4, 3, 71 };
62   const wchar_t* all = L"%a %A %b %B %c %d %H %I %j %m %M %p %s %U "
63                        L"%w %W %x %X %y %Y %Z %%";
64   const wchar_t* date = L"%A, the second of %B";
65   const wchar_t* date_ex = L"%Ex";
66
67   // 1
68   // iter_type 
69   // put(iter_type s, ios_base& str, char_type fill, const tm* t,
70   //     char format, char modifier = 0) const;
71   oss.str(empty);
72   oss.imbue(loc_c);
73   iterator_type os_it01 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'a');
74   wstring result1 = oss.str();
75   VERIFY( result1 == L"Sun" );
76
77   oss.str(empty);
78   iterator_type os_it21 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x');
79   wstring result21 = oss.str(); // "04/04/71"
80   oss.str(empty);
81   iterator_type os_it22 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X');
82   wstring result22 = oss.str(); // "12:00:00"
83   oss.str(empty);
84   iterator_type os_it31 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x', 'E');
85   wstring result31 = oss.str(); // "04/04/71"
86   oss.str(empty);
87   iterator_type os_it32 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X', 'E');
88   wstring result32 = oss.str(); // "12:00:00"
89
90   oss.str(empty);
91   oss.imbue(loc_de);
92   iterator_type os_it02 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'a');
93   wstring result2 = oss.str();
94   VERIFY( result2 == L"Son" );
95
96   oss.str(empty); // "%d.%m.%Y"
97   iterator_type os_it23 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x');
98   wstring result23 = oss.str(); // "04.04.1971"
99   oss.str(empty); // "%T"
100   iterator_type os_it24 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X');
101   wstring result24 = oss.str(); // "12:00:00"
102   oss.str(empty);
103   iterator_type os_it33 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x', 'E');
104   wstring result33 = oss.str(); // "04.04.1971"
105   oss.str(empty);
106   iterator_type os_it34 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X', 'E');
107   wstring result34 = oss.str(); // "12:00:00"
108
109   oss.str(empty);
110   oss.imbue(loc_hk);
111   iterator_type os_it03 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'a');
112   wstring result3 = oss.str();
113   VERIFY( result3 == L"Sun" );
114
115   oss.str(empty); // "%A, %B %d, %Y"
116   iterator_type os_it25 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x');
117   wstring result25 = oss.str(); // "Sunday, April 04, 1971"
118   oss.str(empty); // "%I:%M:%S %Z"
119   iterator_type os_it26 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X');
120   wstring result26 = oss.str(); // "12:00:00 PST"
121   oss.str(empty);
122   iterator_type os_it35 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x', 'E');
123   wstring result35 = oss.str(); // "Sunday, April 04, 1971"
124   oss.str(empty);
125   iterator_type os_it36 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X', 'E');
126   wstring result36 = oss.str(); // "12:00:00 PST"
127
128   oss.str(empty);
129   oss.imbue(loc_fr);
130   iterator_type os_it04 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'a');
131   wstring result4 = oss.str();
132   VERIFY( result4 == L"dim" );
133
134   oss.str(empty); // "%d.%m.%Y"
135   iterator_type os_it27 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x');
136   wstring result27 = oss.str(); // "04.04.1971"
137   oss.str(empty); // "%T"
138   iterator_type os_it28 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X');
139   wstring result28 = oss.str(); // "12:00:00"
140   oss.str(empty);
141   iterator_type os_it37 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x', 'E');
142   wstring result37 = oss.str(); // "04.04.1971"
143   oss.str(empty);
144   iterator_type os_it38 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X', 'E');
145   wstring result38 = oss.str(); // "12:00:00"
146
147   // 2
148   oss.str(empty);
149   oss.imbue(loc_c);
150   iterator_type os_it05 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 
151                                       date, date + traits::length(date));
152   wstring result5 = oss.str();
153   VERIFY( result5 == L"Sunday, the second of April");
154   iterator_type os_it06 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 
155                                       date_ex, date_ex + traits::length(date));
156   wstring result6 = oss.str();
157   VERIFY( result6 != result5 );
158
159   oss.str(empty);
160   oss.imbue(loc_de);
161   iterator_type os_it07 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 
162                                       date, date + traits::length(date));
163   wstring result7 = oss.str();
164   VERIFY( result7 == L"Sonntag, the second of April");
165   iterator_type os_it08 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 
166                                       date_ex, date_ex + traits::length(date));
167   wstring result8 = oss.str();
168   VERIFY( result8 != result7 );
169
170   oss.str(empty);
171   oss.imbue(loc_hk);
172   iterator_type os_it09 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 
173                                       date, date + traits::length(date));
174   wstring result9 = oss.str();
175   VERIFY( result9 == L"Sunday, the second of April");
176   iterator_type os_it10 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 
177                                       date_ex, date_ex + traits::length(date));
178   wstring result10 = oss.str();
179   VERIFY( result10 != result9 );
180
181   oss.str(empty);
182   oss.imbue(loc_fr);
183   iterator_type os_it11 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 
184                                       date, date + traits::length(date));
185   wstring result11 = oss.str();
186   VERIFY( result11 == L"dimanche, the second of avril");
187   iterator_type os_it12 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 
188                                       date_ex, date_ex + traits::length(date));
189   wstring result12 = oss.str();
190   VERIFY( result12 != result11 );
191 }
192
193 void test02()
194 {
195   using namespace std;
196   bool test = true;
197
198   // Check time_put works with other iterators besides streambuf
199   // output iterators. (As long as output_iterator requirements are met.)
200   typedef wstring::iterator iter_type;
201   typedef char_traits<wchar_t> traits;
202   typedef time_put<wchar_t, iter_type> time_put_type;
203   const ios_base::iostate goodbit = ios_base::goodbit;
204   const ios_base::iostate eofbit = ios_base::eofbit;
205   ios_base::iostate err = goodbit;
206   const locale loc_c = locale::classic();
207   const wstring x(50, L'x'); // have to have allocated string!
208   wstring res;
209   const tm time_sanity = { 0, 0, 12, 26, 5, 97, 2 };
210   const wchar_t* date = L"%X, %A, the second of %B, %Y";
211
212   ostringstream oss; 
213   oss.imbue(locale(loc_c, new time_put_type));
214
215   // Iterator advanced, state, output.
216   const time_put_type& tp = use_facet<time_put_type>(oss.getloc());
217
218   // 01 date format
219   res = x;
220   iter_type ret1 = tp.put(res.begin(), oss, L' ', &time_sanity, 
221                           date, date + traits::length(date));
222   wstring sanity1(res.begin(), ret1);
223   VERIFY( err == goodbit );
224   VERIFY( res == L"12:00:00, Tuesday, the second of June, 1997xxxxxxx" );
225   VERIFY( sanity1 == L"12:00:00, Tuesday, the second of June, 1997" );
226
227   // 02 char format
228   res = x;
229   iter_type ret2 = tp.put(res.begin(), oss, L' ', &time_sanity, 'A');
230   wstring sanity2(res.begin(), ret2);
231   VERIFY( err == goodbit );
232   VERIFY( res == L"Tuesdayxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" );
233   VERIFY( sanity2 == L"Tuesday" );
234 }
235
236 // libstdc++/5280
237 void test03()
238 {
239 #ifdef _GLIBCPP_HAVE_SETENV 
240   // Set the global locale to non-"C".
241   std::locale loc_de("de_DE");
242   std::locale::global(loc_de);
243
244   // Set LANG environment variable to de_DE.
245   const char* oldLANG = getenv("LANG");
246   if (!setenv("LANG", "de_DE", 1))
247     {
248       test01();
249       test02();
250       setenv("LANG", oldLANG, 1);
251     }
252 #endif
253 }
254 #endif
255
256 int main()
257 {
258 #ifdef _GLIBCPP_USE_WCHAR_T
259   test01();
260   test02();
261   test03();
262 #endif
263   return 0;
264 }