Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenctype_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 <stdexcept> 00006 00007 # if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00008 using namespace std; 00009 # endif 00010 00011 static const char* tested_locales[] = { 00012 //name, 00013 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00014 // We need exception support to check support of the following localizations. 00015 "fr_FR", 00016 "ru_RU.koi8r", 00017 "en_GB", 00018 "en_US", 00019 # endif 00020 "", 00021 "C" 00022 }; 00023 00024 // 00025 // tests implementation 00026 // 00027 void LocaleTest::_ctype_facet( const locale& loc) 00028 { 00029 CPPUNIT_ASSERT( has_facet<ctype<char> >(loc) ); 00030 ctype<char> const& ct = use_facet<ctype<char> >(loc); 00031 //is 00032 { 00033 CPPUNIT_ASSERT( ct.is(ctype_base::digit, '0') ); 00034 CPPUNIT_ASSERT( ct.is(ctype_base::upper, 'A') ); 00035 CPPUNIT_ASSERT( ct.is(ctype_base::lower, 'a') ); 00036 CPPUNIT_ASSERT( ct.is(ctype_base::alpha, 'A') ); 00037 CPPUNIT_ASSERT( ct.is(ctype_base::space, ' ') ); 00038 CPPUNIT_ASSERT( !ct.is(ctype_base::space, '2') ); 00039 CPPUNIT_ASSERT( ct.is(ctype_base::punct, '.') ); 00040 CPPUNIT_ASSERT( ct.is(ctype_base::xdigit, 'a') ); 00041 } 00042 00043 //is range 00044 { 00045 char values[] = "0Aa ."; 00046 ctype_base::mask res[sizeof(values)]; 00047 ct.is(values, values + sizeof(values), res); 00048 // '0' 00049 CPPUNIT_ASSERT( (res[0] & ctype_base::print) != 0 ); 00050 CPPUNIT_ASSERT( (res[0] & ctype_base::digit) != 0 ); 00051 CPPUNIT_ASSERT( (res[0] & ctype_base::xdigit) != 0 ); 00052 // 'A' 00053 CPPUNIT_ASSERT( (res[1] & ctype_base::print) != 0 ); 00054 CPPUNIT_ASSERT( (res[1] & ctype_base::alpha) != 0 ); 00055 CPPUNIT_ASSERT( (res[1] & ctype_base::xdigit) != 0 ); 00056 CPPUNIT_ASSERT( (res[1] & ctype_base::upper) != 0 ); 00057 // 'a' 00058 CPPUNIT_ASSERT( (res[2] & ctype_base::print) != 0 ); 00059 CPPUNIT_ASSERT( (res[2] & ctype_base::alpha) != 0 ); 00060 CPPUNIT_ASSERT( (res[2] & ctype_base::xdigit) != 0 ); 00061 CPPUNIT_ASSERT( (res[2] & ctype_base::lower) != 0 ); 00062 CPPUNIT_ASSERT( (res[2] & ctype_base::space) == 0 ); 00063 // ' ' 00064 CPPUNIT_ASSERT( (res[3] & ctype_base::print) != 0 ); 00065 CPPUNIT_ASSERT( (res[3] & ctype_base::space) != 0 ); 00066 CPPUNIT_ASSERT( (res[3] & ctype_base::digit) == 0 ); 00067 // '.' 00068 CPPUNIT_ASSERT( (res[4] & ctype_base::print) != 0 ); 00069 CPPUNIT_ASSERT( (res[4] & ctype_base::punct) != 0 ); 00070 CPPUNIT_ASSERT( (res[4] & ctype_base::digit) == 0 ); 00071 } 00072 00073 //scan_is 00074 { 00075 char range[] = "abAc123 ."; 00076 const char *rbeg = range; 00077 const char *rend = range + sizeof(range); 00078 00079 const char *res; 00080 res = ct.scan_is((ctype_base::mask)(ctype_base::alpha | ctype_base::lower), rbeg, rend); 00081 CPPUNIT_ASSERT( res != rend ); 00082 CPPUNIT_ASSERT( *res == 'a' ); 00083 00084 res = ct.scan_is(ctype_base::upper, rbeg, rend); 00085 CPPUNIT_ASSERT( res != rend ); 00086 CPPUNIT_ASSERT( *res == 'A' ); 00087 00088 res = ct.scan_is(ctype_base::punct, rbeg, rend); 00089 CPPUNIT_ASSERT( res != rend ); 00090 CPPUNIT_ASSERT( *res == '.' ); 00091 } 00092 00093 //scan_not 00094 { 00095 char range[] = "abAc123 ."; 00096 const char *rbeg = range; 00097 const char *rend = range + sizeof(range); 00098 00099 const char *res; 00100 res = ct.scan_not((ctype_base::mask)(ctype_base::alpha | ctype_base::lower), rbeg, rend); 00101 CPPUNIT_ASSERT( res != rend ); 00102 CPPUNIT_ASSERT( *res == '1' ); 00103 00104 res = ct.scan_not(ctype_base::alpha, rbeg, rend); 00105 CPPUNIT_ASSERT( res != rend ); 00106 CPPUNIT_ASSERT( *res == '1' ); 00107 00108 res = ct.scan_not(ctype_base::punct, rbeg, rend); 00109 CPPUNIT_ASSERT( res != rend ); 00110 CPPUNIT_ASSERT( *res == 'a' ); 00111 } 00112 00113 //toupper 00114 { 00115 CPPUNIT_ASSERT( ct.toupper('a') == 'A' ); 00116 CPPUNIT_ASSERT( ct.toupper('A') == 'A' ); 00117 CPPUNIT_ASSERT( ct.toupper('1') == '1' ); 00118 } 00119 00120 //toupper range 00121 { 00122 char range[] = "abAc1"; 00123 char expected_range[] = "ABAC1"; 00124 ct.toupper(range, range + sizeof(range)); 00125 CPPUNIT_ASSERT( equal(range, range + sizeof(range), expected_range) ); 00126 } 00127 00128 //tolower 00129 { 00130 CPPUNIT_ASSERT( ct.tolower('A') == 'a' ); 00131 CPPUNIT_ASSERT( ct.tolower('a') == 'a' ); 00132 CPPUNIT_ASSERT( ct.tolower('1') == '1' ); 00133 } 00134 00135 //tolower range 00136 { 00137 char range[] = "ABaC1"; 00138 char expected_range[] = "abac1"; 00139 ct.tolower(range, range + sizeof(range)); 00140 CPPUNIT_ASSERT( equal(range, range + sizeof(range), expected_range) ); 00141 } 00142 00143 //widen 00144 { 00145 CPPUNIT_ASSERT( ct.widen('a') == 'a' ); 00146 } 00147 00148 //widen range 00149 { 00150 char range[] = "ABaC1"; 00151 char res[sizeof(range)]; 00152 ct.widen(range, range + sizeof(range), res); 00153 CPPUNIT_ASSERT( equal(range, range + sizeof(range), res) ); 00154 } 00155 00156 //narrow 00157 { 00158 CPPUNIT_ASSERT( ct.narrow('a', 'b') == 'a' ); 00159 } 00160 00161 //narrow range 00162 { 00163 char range[] = "ABaC1"; 00164 char res[sizeof(range)]; 00165 ct.narrow(range, range + sizeof(range), 'b', res); 00166 CPPUNIT_ASSERT( equal(range, range + sizeof(range), res) ); 00167 } 00168 } 00169 00170 void LocaleTest::_ctype_facet_w( const locale& loc ) 00171 { 00172 # ifndef _STLP_NO_WCHAR_T 00173 CPPUNIT_ASSERT( has_facet<ctype<wchar_t> >(loc) ); 00174 ctype<wchar_t> const& wct = use_facet<ctype<wchar_t> >(loc); 00175 //is 00176 { 00177 CPPUNIT_CHECK( wct.is(ctype_base::digit, L'0') ); 00178 CPPUNIT_CHECK( wct.is(ctype_base::upper, L'A') ); 00179 CPPUNIT_CHECK( wct.is(ctype_base::lower, L'a') ); 00180 CPPUNIT_CHECK( wct.is(ctype_base::alpha, L'A') ); 00181 CPPUNIT_CHECK( wct.is(ctype_base::space, L' ') ); 00182 CPPUNIT_CHECK( !wct.is(ctype_base::space, L'2') ); 00183 CPPUNIT_CHECK( wct.is(ctype_base::punct, L'.') ); 00184 CPPUNIT_CHECK( wct.is(ctype_base::xdigit, L'a') ); 00185 } 00186 00187 //is range 00188 { 00189 wchar_t values[] = L"0Aa ."; 00190 ctype_base::mask res[sizeof(values) / sizeof(wchar_t)]; 00191 wct.is(values, values + (sizeof(values) / sizeof(wchar_t)), res); 00192 // '0' 00193 CPPUNIT_CHECK( (res[0] & ctype_base::print) != 0 ); 00194 CPPUNIT_CHECK( (res[0] & ctype_base::digit) != 0 ); 00195 CPPUNIT_CHECK( (res[0] & ctype_base::xdigit) != 0 ); 00196 // 'A' 00197 CPPUNIT_CHECK( (res[1] & ctype_base::print) != 0 ); 00198 CPPUNIT_CHECK( (res[1] & ctype_base::alpha) != 0 ); 00199 CPPUNIT_CHECK( (res[1] & ctype_base::xdigit) != 0 ); 00200 CPPUNIT_CHECK( (res[1] & ctype_base::upper) != 0 ); 00201 // 'a' 00202 CPPUNIT_CHECK( (res[2] & ctype_base::print) != 0 ); 00203 CPPUNIT_CHECK( (res[2] & ctype_base::alpha) != 0 ); 00204 CPPUNIT_CHECK( (res[2] & ctype_base::xdigit) != 0 ); 00205 CPPUNIT_CHECK( (res[2] & ctype_base::lower) != 0 ); 00206 CPPUNIT_CHECK( (res[2] & ctype_base::space) == 0 ); 00207 // ' ' 00208 CPPUNIT_CHECK( (res[3] & ctype_base::print) != 0 ); 00209 CPPUNIT_CHECK( (res[3] & ctype_base::space) != 0 ); 00210 CPPUNIT_CHECK( (res[3] & ctype_base::digit) == 0 ); 00211 // '.' 00212 CPPUNIT_CHECK( (res[4] & ctype_base::print) != 0 ); 00213 CPPUNIT_CHECK( (res[4] & ctype_base::punct) != 0 ); 00214 CPPUNIT_CHECK( (res[4] & ctype_base::digit) == 0 ); 00215 } 00216 00217 //scan_is 00218 { 00219 wchar_t range[] = L"abAc123 ."; 00220 const wchar_t *rbeg = range; 00221 const wchar_t *rend = range + (sizeof(range) / sizeof(wchar_t)); 00222 00223 const wchar_t *res; 00224 res = wct.scan_is((ctype_base::mask)(ctype_base::alpha | ctype_base::lower), rbeg, rend); 00225 CPPUNIT_CHECK( res != rend ); 00226 CPPUNIT_CHECK( *res == L'a' ); 00227 00228 res = wct.scan_is(ctype_base::upper, rbeg, rend); 00229 CPPUNIT_CHECK( res != rend ); 00230 CPPUNIT_CHECK( *res == L'A' ); 00231 00232 res = wct.scan_is(ctype_base::punct, rbeg, rend); 00233 CPPUNIT_CHECK( res != rend ); 00234 CPPUNIT_CHECK( *res == L'.' ); 00235 } 00236 00237 //scan_not 00238 { 00239 wchar_t range[] = L"abAc123 ."; 00240 const wchar_t *rbeg = range; 00241 const wchar_t *rend = range + (sizeof(range) / sizeof(wchar_t)); 00242 00243 const wchar_t *res; 00244 res = wct.scan_not((ctype_base::mask)(ctype_base::alpha | ctype_base::lower), rbeg, rend); 00245 CPPUNIT_CHECK( res != rend ); 00246 CPPUNIT_CHECK( *res == L'1' ); 00247 00248 res = wct.scan_not(ctype_base::alpha, rbeg, rend); 00249 CPPUNIT_CHECK( res != rend ); 00250 CPPUNIT_CHECK( *res == L'1' ); 00251 00252 res = wct.scan_not(ctype_base::punct, rbeg, rend); 00253 CPPUNIT_CHECK( res != rend ); 00254 CPPUNIT_CHECK( *res == L'a' ); 00255 } 00256 00257 //toupper 00258 { 00259 CPPUNIT_CHECK( wct.toupper(L'a') == L'A' ); 00260 CPPUNIT_CHECK( wct.toupper(L'A') == L'A' ); 00261 CPPUNIT_CHECK( wct.toupper(L'1') == L'1' ); 00262 } 00263 00264 //toupper range 00265 { 00266 wchar_t range[] = L"abAc1"; 00267 wchar_t expected_range[] = L"ABAC1"; 00268 wct.toupper(range, range + sizeof(range) / sizeof(wchar_t)); 00269 CPPUNIT_CHECK( equal(range, range + sizeof(range) / sizeof(wchar_t), expected_range) ); 00270 } 00271 00272 //tolower 00273 { 00274 CPPUNIT_CHECK( wct.tolower(L'A') == L'a' ); 00275 CPPUNIT_CHECK( wct.tolower(L'a') == L'a' ); 00276 CPPUNIT_CHECK( wct.tolower(L'1') == L'1' ); 00277 } 00278 00279 //tolower range 00280 { 00281 wchar_t range[] = L"ABaC1"; 00282 wchar_t expected_range[] = L"abac1"; 00283 wct.tolower(range, range + sizeof(range) / sizeof(wchar_t)); 00284 CPPUNIT_CHECK( equal(range, range + sizeof(range) / sizeof(wchar_t), expected_range) ); 00285 } 00286 00287 //widen 00288 { 00289 CPPUNIT_CHECK( wct.widen('a') == L'a' ); 00290 } 00291 00292 //widen range 00293 { 00294 char range[] = "ABaC1"; 00295 wchar_t res[sizeof(range)]; 00296 wchar_t expected_res[] = L"ABaC1"; 00297 wct.widen(range, range + sizeof(range), res); 00298 CPPUNIT_CHECK( equal(expected_res, expected_res + sizeof(range), res) ); 00299 } 00300 00301 //narrow 00302 { 00303 CPPUNIT_CHECK( wct.narrow(L'a', 'b') == L'a' ); 00304 } 00305 00306 //narrow range 00307 { 00308 wchar_t range[] = L"ABaC1"; 00309 char res[sizeof(range) / sizeof(wchar_t)]; 00310 char expected_res[] = "ABaC1"; 00311 wct.narrow(range, range + sizeof(range) / sizeof(wchar_t), 'b', res); 00312 CPPUNIT_CHECK( equal(expected_res, expected_res + sizeof(range) / sizeof(wchar_t), res) ); 00313 } 00314 # endif 00315 } 00316 00317 00318 typedef void (LocaleTest::*_Test) (const locale&); 00319 static void test_supported_locale(LocaleTest& inst, _Test __test) { 00320 size_t n = sizeof(tested_locales) / sizeof(tested_locales[0]); 00321 for (size_t i = 0; i < n; ++i) { 00322 locale loc; 00323 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00324 try 00325 # endif 00326 { 00327 locale tmp(tested_locales[i]); 00328 loc = tmp; 00329 } 00330 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00331 catch (runtime_error const&) { 00332 //This locale is not supported. 00333 continue; 00334 } 00335 # endif 00336 00337 CPPUNIT_MESSAGE( loc.name().c_str() ); 00338 (inst.*__test)(loc); 00339 00340 { 00341 locale tmp(locale::classic(), tested_locales[i], locale::ctype); 00342 loc = tmp; 00343 } 00344 (inst.*__test)(loc); 00345 00346 { 00347 locale tmp(locale::classic(), new ctype_byname<char>(tested_locales[i])); 00348 #ifndef _STLP_NO_WCHAR_T 00349 locale tmp0(tmp, new ctype_byname<wchar_t>(tested_locales[i])); 00350 tmp = tmp0; 00351 #endif 00352 loc = tmp; 00353 } 00354 (inst.*__test)(loc); 00355 } 00356 } 00357 00358 void LocaleTest::ctype_facet() 00359 { 00360 test_supported_locale(*this, &LocaleTest::_ctype_facet); 00361 #ifndef _STLP_NO_WCHAR_T 00362 test_supported_locale(*this, &LocaleTest::_ctype_facet_w); 00363 #endif 00364 } 00365 00366 void LocaleTest::ctype_by_name() 00367 { 00368 /* 00369 * Check of the 22.1.1.2.7 standard point. Construction of a locale 00370 * instance from a null pointer or an unknown name should result in 00371 * a runtime_error exception. 00372 */ 00373 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00374 # if defined (STLPORT) || (!defined(__GNUC__) && (!defined (_MSC_VER) || (_MSC_VER > 1400))) 00375 // libstdc++ call freelocate on bad locale 00376 try { 00377 locale loc(locale::classic(), new ctype_byname<char>(static_cast<char const*>(0))); 00378 CPPUNIT_ASSERT( false ); 00379 } 00380 catch (runtime_error const& /* e */) { 00381 //CPPUNIT_MESSAGE( e.what() ); 00382 } 00383 catch (...) { 00384 CPPUNIT_ASSERT( false ); 00385 } 00386 # endif 00387 00388 try { 00389 locale loc(locale::classic(), new ctype_byname<char>("yasli_language")); 00390 CPPUNIT_FAIL; 00391 } 00392 catch (runtime_error const& /* e */) { 00393 //CPPUNIT_MESSAGE( e.what() ); 00394 } 00395 catch (...) { 00396 CPPUNIT_FAIL; 00397 } 00398 00399 # if defined(STLPORT) || !defined(__GNUC__) 00400 try { 00401 locale loc(locale::classic(), new codecvt_byname<char, char, mbstate_t>(static_cast<char const*>(0))); 00402 CPPUNIT_FAIL; 00403 } 00404 catch (runtime_error const& /* e */) { 00405 //CPPUNIT_MESSAGE( e.what() ); 00406 } 00407 catch (...) { 00408 CPPUNIT_FAIL; 00409 } 00410 # endif 00411 00412 try { 00413 locale loc(locale::classic(), new codecvt_byname<char, char, mbstate_t>("yasli_language")); 00414 //STLport implementation do not care about name pass to this facet. 00415 # if !defined (STLPORT) 00416 CPPUNIT_FAIL; 00417 # endif 00418 } 00419 catch (runtime_error const& /* e */) { 00420 //CPPUNIT_MESSAGE( e.what() ); 00421 } 00422 catch (...) { 00423 CPPUNIT_FAIL; 00424 } 00425 00426 try { 00427 locale loc(locale::classic(), new ctype_byname<char>("fr_FR")); 00428 CPPUNIT_ASSERT( has_facet<ctype<char> >(loc) ); 00429 ctype<char> const& ct = use_facet<ctype<char> >(loc); 00430 CPPUNIT_ASSERT( ct.is(ctype_base::mask(ctype_base::print | ctype_base::lower | ctype_base::alpha), 'ç') ); 00431 } 00432 catch (runtime_error const& /* e */) { 00433 //CPPUNIT_MESSAGE( e.what() ); 00434 } 00435 catch (...) { 00436 CPPUNIT_FAIL; 00437 } 00438 00439 try { 00440 locale loc(locale::classic(), new ctype_byname<char>("C")); 00441 ctype<char> const& cfacet_byname = use_facet<ctype<char> >(loc); 00442 ctype<char> const& cfacet = use_facet<ctype<char> >(locale::classic()); 00443 00444 for (char c = 0;; ++c) { 00445 CPPUNIT_CHECK(cfacet_byname.is(ctype_base::space, c) == cfacet.is(ctype_base::space, c)); 00446 if (cfacet_byname.is(ctype_base::print, c) != cfacet.is(ctype_base::print, c)) 00447 { 00448 CPPUNIT_CHECK(cfacet_byname.is(ctype_base::print, c) == cfacet.is(ctype_base::print, c)); 00449 } 00450 CPPUNIT_CHECK(cfacet_byname.is(ctype_base::cntrl, c) == cfacet.is(ctype_base::cntrl, c)); 00451 CPPUNIT_CHECK(cfacet_byname.is(ctype_base::upper, c) == cfacet.is(ctype_base::upper, c)); 00452 CPPUNIT_CHECK(cfacet_byname.is(ctype_base::lower, c) == cfacet.is(ctype_base::lower, c)); 00453 CPPUNIT_CHECK(cfacet_byname.is(ctype_base::alpha, c) == cfacet.is(ctype_base::alpha, c)); 00454 CPPUNIT_CHECK(cfacet_byname.is(ctype_base::digit, c) == cfacet.is(ctype_base::digit, c)); 00455 CPPUNIT_CHECK(cfacet_byname.is(ctype_base::punct, c) == cfacet.is(ctype_base::punct, c)); 00456 CPPUNIT_CHECK(cfacet_byname.is(ctype_base::xdigit, c) == cfacet.is(ctype_base::xdigit, c)); 00457 CPPUNIT_CHECK(cfacet_byname.is(ctype_base::alnum, c) == cfacet.is(ctype_base::alnum, c)); 00458 CPPUNIT_CHECK(cfacet_byname.is(ctype_base::graph, c) == cfacet.is(ctype_base::graph, c)); 00459 if (c == 127) break; 00460 } 00461 } 00462 catch (runtime_error const& /* e */) { 00463 /* CPPUNIT_MESSAGE( e.what() ); */ 00464 CPPUNIT_FAIL; 00465 } 00466 catch (...) { 00467 CPPUNIT_FAIL; 00468 } 00469 00470 # if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T) 00471 # if defined(STLPORT) || !defined(__GNUC__) 00472 try { 00473 locale loc(locale::classic(), new ctype_byname<wchar_t>(static_cast<char const*>(0))); 00474 CPPUNIT_FAIL; 00475 } 00476 catch (runtime_error const&) { 00477 } 00478 catch (...) { 00479 CPPUNIT_FAIL; 00480 } 00481 # endif 00482 00483 try { 00484 locale loc(locale::classic(), new ctype_byname<wchar_t>("yasli_language")); 00485 CPPUNIT_FAIL; 00486 } 00487 catch (runtime_error const&) { 00488 } 00489 catch (...) { 00490 CPPUNIT_FAIL; 00491 } 00492 00493 # if defined(STLPORT) || !defined(__GNUC__) 00494 try { 00495 locale loc(locale::classic(), new codecvt_byname<wchar_t, char, mbstate_t>(static_cast<char const*>(0))); 00496 CPPUNIT_FAIL; 00497 } 00498 catch (runtime_error const& /* e */) { 00499 //CPPUNIT_MESSAGE( e.what() ); 00500 } 00501 catch (...) { 00502 CPPUNIT_FAIL; 00503 } 00504 # endif 00505 00506 try { 00507 locale loc(locale::classic(), new codecvt_byname<wchar_t, char, mbstate_t>("yasli_language")); 00508 CPPUNIT_FAIL; 00509 } 00510 catch (runtime_error const& /* e */) { 00511 //CPPUNIT_MESSAGE( e.what() ); 00512 } 00513 catch (...) { 00514 CPPUNIT_FAIL; 00515 } 00516 # endif 00517 # endif 00518 } 00519 00520 #endif Generated on Sat May 26 2012 04:34:11 for ReactOS by
1.7.6.1
|