OSDN Git Service

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