Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygentime_facets_test.cpp
Go to the documentation of this file.
00001 #include "locale_test.h" 00002 00003 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS) 00004 # include <locale> 00005 # include <sstream> 00006 # include <memory> 00007 # include <stdexcept> 00008 00009 # if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00010 using namespace std; 00011 # endif 00012 00013 static const char* tested_locales[] = { 00014 // name, 00015 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00016 "fr_FR", 00017 "ru_RU.koi8r", 00018 "en_GB", 00019 "en_US", 00020 # endif 00021 "", 00022 "C" 00023 }; 00024 00025 void LocaleTest::_time_put_get( const locale& loc ) 00026 { 00027 { 00028 typedef time_put<char, ostreambuf_iterator<char, char_traits<char> > > time_put_facet; 00029 CPPUNIT_ASSERT( has_facet<time_put_facet>(loc) ); 00030 const time_put_facet& tmp = use_facet<time_put_facet>(loc); 00031 00032 struct tm xmas = { 0, 0, 12, 25, 11, 93 }; 00033 ostringstream ostr; 00034 ostr.imbue(loc); 00035 string format = "%B %d %Y"; 00036 00037 time_put_facet::iter_type ret = tmp.put(ostr, ostr, ' ', &xmas, format.data(), format.data() + format.size()); 00038 CPPUNIT_ASSERT( !ret.failed() ); 00039 00040 /* 00041 * In other words, user conformation is required for reliable parsing 00042 * of user-entered dates and times, but machine-generated formats can be 00043 * parsed reliably. This allows parsers to be aggressive about interpreting 00044 * user variations on standard format. 00045 * 00046 * ISO/IEC 14882, 22.2.5.1 00047 */ 00048 typedef time_get<char, istreambuf_iterator<char, char_traits<char> > > time_get_facet; 00049 CPPUNIT_ASSERT( has_facet<time_get_facet>(loc) ); 00050 const time_get_facet& tmg = use_facet<time_get_facet>(loc); 00051 basic_ios<char> io(0); 00052 io.imbue(loc); 00053 00054 istringstream istr( ostr.str() ); 00055 istreambuf_iterator<char, char_traits<char> > i( istr ); 00056 istreambuf_iterator<char, char_traits<char> > e; 00057 ios_base::iostate err = ios_base::goodbit; 00058 struct tm other = { 15, 20, 9, 14, 7, 105 }; 00059 00060 i = tmg.get_monthname( i, e, io, err, &other ); 00061 CPPUNIT_ASSERT( err == ios_base::goodbit ); 00062 CPPUNIT_ASSERT( other.tm_mon == xmas.tm_mon ); 00063 00064 ++i; ++i; ++i; ++i; // skip day of month and spaces around it 00065 i = tmg.get_year( i, e, io, err, &other ); 00066 00067 CPPUNIT_ASSERT( err == ios_base::eofbit ); 00068 CPPUNIT_ASSERT( other.tm_year == xmas.tm_year ); 00069 00070 ostringstream ostrX; 00071 ostrX.imbue(loc); 00072 format = "%x %X"; 00073 00074 ret = tmp.put(ostrX, ostrX, ' ', &xmas, format.data(), format.data() + format.size()); 00075 CPPUNIT_ASSERT( !ret.failed() ); 00076 00077 istringstream istrX( ostrX.str() ); 00078 istreambuf_iterator<char, char_traits<char> > j( istrX ); 00079 00080 err = ios_base::goodbit; 00081 00082 struct tm yet_more = { 15, 20, 9, 14, 7, 105 }; 00083 00084 j = tmg.get_date( j, e, io, err, &yet_more ); 00085 00086 CPPUNIT_ASSERT( err == ios_base::goodbit ); 00087 00088 CPPUNIT_ASSERT( yet_more.tm_sec != xmas.tm_sec ); 00089 CPPUNIT_ASSERT( yet_more.tm_min != xmas.tm_min ); 00090 CPPUNIT_ASSERT( yet_more.tm_hour != xmas.tm_hour ); 00091 CPPUNIT_ASSERT( yet_more.tm_mday == xmas.tm_mday ); 00092 CPPUNIT_ASSERT( yet_more.tm_mon == xmas.tm_mon ); 00093 CPPUNIT_ASSERT( yet_more.tm_year == xmas.tm_year ); 00094 00095 ++j; // skip space 00096 00097 j = tmg.get_time( j, e, io, err, &yet_more ); 00098 00099 CPPUNIT_ASSERT( err == ios_base::eofbit || err == ios_base::goodbit ); 00100 00101 CPPUNIT_ASSERT( yet_more.tm_sec == xmas.tm_sec ); 00102 CPPUNIT_ASSERT( yet_more.tm_min == xmas.tm_min ); 00103 CPPUNIT_ASSERT( yet_more.tm_hour == xmas.tm_hour ); 00104 CPPUNIT_ASSERT( yet_more.tm_mday == xmas.tm_mday ); 00105 CPPUNIT_ASSERT( yet_more.tm_mon == xmas.tm_mon ); 00106 CPPUNIT_ASSERT( yet_more.tm_year == xmas.tm_year ); 00107 } 00108 # if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T) 00109 { 00110 typedef time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > > time_put_facet; 00111 CPPUNIT_ASSERT( has_facet<time_put_facet>(loc) ); 00112 const time_put_facet& tmp = use_facet<time_put_facet>(loc); 00113 00114 struct tm xmas = { 0, 0, 12, 25, 11, 93 }; 00115 wostringstream ostr; 00116 ostr.imbue(loc); 00117 wstring format = L"%B %d %Y"; 00118 00119 time_put_facet::iter_type ret = tmp.put(ostr, ostr, ' ', &xmas, format.data(), format.data() + format.size()); 00120 CPPUNIT_ASSERT( !ret.failed() ); 00121 00122 /* 00123 * In other words, user conformation is required for reliable parsing 00124 * of user-entered dates and times, but machine-generated formats can be 00125 * parsed reliably. This allows parsers to be aggressive about interpreting 00126 * user variations on standard format. 00127 * 00128 * ISO/IEC 14882, 22.2.5.1 00129 */ 00130 typedef time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > > time_get_facet; 00131 CPPUNIT_ASSERT( has_facet<time_get_facet>(loc) ); 00132 const time_get_facet& tmg = use_facet<time_get_facet>(loc); 00133 // Intentional instantiation with char to show a bug in a previous STLport version. 00134 basic_ios<char> io(0); 00135 io.imbue(loc); 00136 00137 wistringstream istr( ostr.str() ); 00138 istreambuf_iterator<wchar_t, char_traits<wchar_t> > i( istr ); 00139 istreambuf_iterator<wchar_t, char_traits<wchar_t> > e; 00140 ios_base::iostate err = ios_base::goodbit; 00141 struct tm other = { 15, 20, 9, 14, 7, 105 }; 00142 00143 i = tmg.get_monthname( i, e, io, err, &other ); 00144 CPPUNIT_ASSERT( err == ios_base::goodbit ); 00145 CPPUNIT_ASSERT( other.tm_mon == xmas.tm_mon ); 00146 00147 ++i; ++i; ++i; ++i; // skip day of month and spaces around it 00148 i = tmg.get_year( i, e, io, err, &other ); 00149 00150 CPPUNIT_ASSERT( err == ios_base::eofbit ); 00151 CPPUNIT_ASSERT( other.tm_year == xmas.tm_year ); 00152 00153 wostringstream ostrX; 00154 ostrX.imbue(loc); 00155 format = L"%x %X"; 00156 00157 ret = tmp.put(ostrX, ostrX, ' ', &xmas, format.data(), format.data() + format.size()); 00158 CPPUNIT_ASSERT( !ret.failed() ); 00159 00160 wistringstream istrX( ostrX.str() ); 00161 istreambuf_iterator<wchar_t, char_traits<wchar_t> > j( istrX ); 00162 00163 err = ios_base::goodbit; 00164 00165 struct tm yet_more = { 15, 20, 9, 14, 7, 105 }; 00166 00167 j = tmg.get_date( j, e, io, err, &yet_more ); 00168 00169 CPPUNIT_ASSERT( err == ios_base::goodbit ); 00170 00171 CPPUNIT_ASSERT( yet_more.tm_sec != xmas.tm_sec ); 00172 CPPUNIT_ASSERT( yet_more.tm_min != xmas.tm_min ); 00173 CPPUNIT_ASSERT( yet_more.tm_hour != xmas.tm_hour ); 00174 CPPUNIT_ASSERT( yet_more.tm_mday == xmas.tm_mday ); 00175 CPPUNIT_ASSERT( yet_more.tm_mon == xmas.tm_mon ); 00176 CPPUNIT_ASSERT( yet_more.tm_year == xmas.tm_year ); 00177 00178 ++j; // skip space 00179 00180 j = tmg.get_time( j, e, io, err, &yet_more ); 00181 00182 CPPUNIT_ASSERT( err == ios_base::eofbit || err == ios_base::goodbit ); 00183 00184 CPPUNIT_ASSERT( yet_more.tm_sec == xmas.tm_sec ); 00185 CPPUNIT_ASSERT( yet_more.tm_min == xmas.tm_min ); 00186 CPPUNIT_ASSERT( yet_more.tm_hour == xmas.tm_hour ); 00187 CPPUNIT_ASSERT( yet_more.tm_mday == xmas.tm_mday ); 00188 CPPUNIT_ASSERT( yet_more.tm_mon == xmas.tm_mon ); 00189 CPPUNIT_ASSERT( yet_more.tm_year == xmas.tm_year ); 00190 } 00191 # endif 00192 } 00193 00194 typedef void (LocaleTest::*_Test) (const locale&); 00195 static void test_supported_locale(LocaleTest& inst, _Test __test) { 00196 size_t n = sizeof(tested_locales) / sizeof(tested_locales[0]); 00197 for (size_t i = 0; i < n; ++i) { 00198 locale loc; 00199 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00200 try 00201 # endif 00202 { 00203 locale tmp(tested_locales[i]); 00204 loc = tmp; 00205 } 00206 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00207 catch (runtime_error const&) { 00208 //This locale is not supported. 00209 continue; 00210 } 00211 # endif 00212 CPPUNIT_MESSAGE( loc.name().c_str() ); 00213 (inst.*__test)(loc); 00214 00215 { 00216 locale tmp(locale::classic(), tested_locales[i], locale::time); 00217 loc = tmp; 00218 } 00219 (inst.*__test)(loc); 00220 00221 { 00222 typedef time_put_byname<char, ostreambuf_iterator<char, char_traits<char> > > time_put_facet; 00223 locale tmp0(locale::classic(), new time_put_facet(tested_locales[i])); 00224 typedef time_get_byname<char, istreambuf_iterator<char, char_traits<char> > > time_get_facet; 00225 locale tmp1(tmp0, new time_get_facet(tested_locales[i])); 00226 loc = tmp1; 00227 } 00228 (inst.*__test)(loc); 00229 } 00230 } 00231 00232 void LocaleTest::time_put_get() 00233 { test_supported_locale(*this, &LocaleTest::_time_put_get); } 00234 00235 void LocaleTest::time_by_name() 00236 { 00237 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00238 /* 00239 * Check of the 22.1.1.2.7 standard point. Construction of a locale 00240 * instance from a null pointer or an unknown name should result in 00241 * a runtime_error exception. 00242 */ 00243 # if defined (STLPORT) || !defined (_MSC_VER) || (_MSC_VER > 1400) 00244 try { 00245 locale loc(locale::classic(), new time_put_byname<char, ostreambuf_iterator<char, char_traits<char> > >(static_cast<char const*>(0))); 00246 CPPUNIT_FAIL; 00247 } 00248 catch (runtime_error const&) { 00249 } 00250 catch (...) { 00251 CPPUNIT_FAIL; 00252 } 00253 # endif 00254 00255 try { 00256 locale loc(locale::classic(), new time_put_byname<char, ostreambuf_iterator<char, char_traits<char> > >("yasli_language")); 00257 CPPUNIT_FAIL; 00258 } 00259 catch (runtime_error const&) { 00260 } 00261 catch (...) { 00262 CPPUNIT_FAIL; 00263 } 00264 00265 try { 00266 string veryLongFacetName("LC_TIME="); 00267 veryLongFacetName.append(512, '?'); 00268 locale loc(locale::classic(), new time_put_byname<char, ostreambuf_iterator<char, char_traits<char> > >(veryLongFacetName.c_str())); 00269 CPPUNIT_FAIL; 00270 } 00271 catch (runtime_error const& /* e */) { 00272 //CPPUNIT_MESSAGE( e.what() ); 00273 } 00274 catch (...) { 00275 CPPUNIT_FAIL; 00276 } 00277 00278 try { 00279 locale loc(locale::classic(), new time_get_byname<char, istreambuf_iterator<char, char_traits<char> > >(static_cast<char const*>(0))); 00280 CPPUNIT_FAIL; 00281 } 00282 catch (runtime_error const&) { 00283 } 00284 catch (...) { 00285 CPPUNIT_FAIL; 00286 } 00287 00288 try { 00289 locale loc(locale::classic(), new time_get_byname<char, istreambuf_iterator<char, char_traits<char> > >("yasli_language")); 00290 CPPUNIT_FAIL; 00291 } 00292 catch (runtime_error const&) { 00293 } 00294 catch (...) { 00295 CPPUNIT_FAIL; 00296 } 00297 00298 try { 00299 string veryLongFacetName("LC_TIME="); 00300 veryLongFacetName.append(512, '?'); 00301 locale loc(locale::classic(), new time_get_byname<char, istreambuf_iterator<char, char_traits<char> > >(veryLongFacetName.c_str())); 00302 CPPUNIT_FAIL; 00303 } 00304 catch (runtime_error const& /* e */) { 00305 //CPPUNIT_MESSAGE( e.what() ); 00306 } 00307 catch (...) { 00308 CPPUNIT_FAIL; 00309 } 00310 00311 try { 00312 locale loc(locale::classic(), "C", locale::time); 00313 } 00314 catch (runtime_error const& /* e */) { 00315 /* CPPUNIT_MESSAGE( e.what() ); */ 00316 CPPUNIT_FAIL; 00317 } 00318 catch (...) { 00319 CPPUNIT_FAIL; 00320 } 00321 00322 try { 00323 // On platform without real localization support we should rely on the "C" facet. 00324 locale loc(locale::classic(), "", locale::time); 00325 } 00326 catch (runtime_error const& /* e */) { 00327 /* CPPUNIT_MESSAGE( e.what() ); */ 00328 CPPUNIT_FAIL; 00329 } 00330 catch (...) { 00331 CPPUNIT_FAIL; 00332 } 00333 00334 try { 00335 locale loc(locale::classic(), new time_get_byname<char, istreambuf_iterator<char, char_traits<char> > >("C")); 00336 } 00337 catch (runtime_error const& /* e */) { 00338 /* CPPUNIT_MESSAGE( e.what() ); */ 00339 CPPUNIT_FAIL; 00340 } 00341 catch (...) { 00342 CPPUNIT_FAIL; 00343 } 00344 00345 try { 00346 // On platform without real localization support we should rely on the "C" locale facet. 00347 locale loc(locale::classic(), new time_get_byname<char, istreambuf_iterator<char, char_traits<char> > >("")); 00348 } 00349 catch (runtime_error const& /* e */) { 00350 /* CPPUNIT_MESSAGE( e.what() ); */ 00351 CPPUNIT_FAIL; 00352 } 00353 catch (...) { 00354 CPPUNIT_FAIL; 00355 } 00356 00357 # if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T) 00358 try { 00359 locale loc(locale::classic(), new time_put_byname<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >(static_cast<char const*>(0))); 00360 CPPUNIT_FAIL; 00361 } 00362 catch (runtime_error const&) { 00363 } 00364 catch (...) { 00365 CPPUNIT_FAIL; 00366 } 00367 00368 try { 00369 locale loc(locale::classic(), new time_put_byname<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >("yasli_language")); 00370 CPPUNIT_FAIL; 00371 } 00372 catch (runtime_error const&) { 00373 } 00374 catch (...) { 00375 CPPUNIT_FAIL; 00376 } 00377 00378 try { 00379 locale loc(locale::classic(), new time_get_byname<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >(static_cast<char const*>(0))); 00380 CPPUNIT_FAIL; 00381 } 00382 catch (runtime_error const&) { 00383 } 00384 catch (...) { 00385 CPPUNIT_FAIL; 00386 } 00387 00388 try { 00389 locale loc(locale::classic(), new time_get_byname<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >("yasli_language")); 00390 CPPUNIT_FAIL; 00391 } 00392 catch (runtime_error const&) { 00393 } 00394 catch (...) { 00395 CPPUNIT_FAIL; 00396 } 00397 00398 # endif 00399 # endif 00400 } 00401 00402 #endif Generated on Sat May 26 2012 04:34:47 for ReactOS by
1.7.6.1
|