1 // 2001-10-02 Benjamin Kosnik <bkoz@redhat.com>
3 // Copyright (C) 2001, 2002 Free Software Foundation
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)
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,
21 // 22.2.5.3.1 time_put members
25 #include <testsuite_hooks.h>
27 // XXX This test is not working for non-glibc locale models.
28 // { dg-do run { xfail *-*-* } }
30 #ifdef _GLIBCPP_USE_WCHAR_T
34 typedef ostreambuf_iterator<wchar_t> iterator_type;
35 typedef char_traits<wchar_t> traits;
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 );
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);
55 // create an ostream-derived object, cache the time_put facet
58 const time_put<wchar_t>& tim_put = use_facet<time_put<wchar_t> >(oss.getloc());
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";
69 // put(iter_type s, ios_base& str, char_type fill, const tm* t,
70 // char format, char modifier = 0) const;
73 iterator_type os_it01 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'a');
74 wstring result1 = oss.str();
75 VERIFY( result1 == L"Sun" );
78 iterator_type os_it21 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x');
79 wstring result21 = oss.str(); // "04/04/71"
81 iterator_type os_it22 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X');
82 wstring result22 = oss.str(); // "12:00:00"
84 iterator_type os_it31 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x', 'E');
85 wstring result31 = oss.str(); // "04/04/71"
87 iterator_type os_it32 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X', 'E');
88 wstring result32 = oss.str(); // "12:00:00"
92 iterator_type os_it02 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'a');
93 wstring result2 = oss.str();
94 VERIFY( result2 == L"Son" );
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"
103 iterator_type os_it33 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x', 'E');
104 wstring result33 = oss.str(); // "04.04.1971"
106 iterator_type os_it34 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X', 'E');
107 wstring result34 = oss.str(); // "12:00:00"
111 iterator_type os_it03 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'a');
112 wstring result3 = oss.str();
113 VERIFY( result3 == L"Sun" );
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"
122 iterator_type os_it35 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x', 'E');
123 wstring result35 = oss.str(); // "Sunday, April 04, 1971"
125 iterator_type os_it36 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X', 'E');
126 wstring result36 = oss.str(); // "12:00:00 PST"
130 iterator_type os_it04 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'a');
131 wstring result4 = oss.str();
132 VERIFY( result4 == L"dim" );
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"
141 iterator_type os_it37 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x', 'E');
142 wstring result37 = oss.str(); // "04.04.1971"
144 iterator_type os_it38 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X', 'E');
145 wstring result38 = oss.str(); // "12:00:00"
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 );
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 );
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 );
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 );
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!
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";
213 oss.imbue(locale(loc_c, new time_put_type));
215 // Iterator advanced, state, output.
216 const time_put_type& tp = use_facet<time_put_type>(oss.getloc());
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" );
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" );
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);
244 // Set LANG environment variable to de_DE.
245 const char* oldLANG = getenv("LANG");
246 if (!setenv("LANG", "de_DE", 1))
250 setenv("LANG", oldLANG, 1);
258 #ifdef _GLIBCPP_USE_WCHAR_T