Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenlocale_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 <sstream> 00005 # include <locale> 00006 # include <stdexcept> 00007 00008 # if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00009 using namespace std; 00010 # endif 00011 00012 static const char* tested_locales[] = { 00013 //name, 00014 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00015 "fr_FR", 00016 "ru_RU.koi8r", 00017 "en_GB", 00018 "en_US", 00019 # endif 00020 "", 00021 "C" 00022 }; 00023 00024 CPPUNIT_TEST_SUITE_REGISTRATION(LocaleTest); 00025 00026 // 00027 // tests implementation 00028 // 00029 typedef void (LocaleTest::*_Test) (const locale&); 00030 static void test_supported_locale(LocaleTest &inst, _Test __test) { 00031 size_t n = sizeof(tested_locales) / sizeof(tested_locales[0]); 00032 for (size_t i = 0; i < n; ++i) { 00033 locale loc; 00034 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00035 try { 00036 # endif 00037 locale tmp(tested_locales[i]); 00038 loc = tmp; 00039 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00040 } 00041 catch (runtime_error const&) { 00042 //This locale is not supported. 00043 continue; 00044 } 00045 # endif 00046 CPPUNIT_MESSAGE( loc.name().c_str() ); 00047 (inst.*__test)(loc); 00048 } 00049 } 00050 00051 void LocaleTest::locale_by_name() { 00052 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00053 /* 00054 * Check of the 22.1.1.2.7 standard point. Construction of a locale 00055 * instance from a null pointer or an unknown name should result in 00056 * a runtime_error exception. 00057 */ 00058 try { 00059 locale loc(static_cast<char const*>(0)); 00060 CPPUNIT_FAIL; 00061 } 00062 catch (runtime_error const&) { 00063 } 00064 catch (...) { 00065 CPPUNIT_FAIL; 00066 } 00067 00068 try { 00069 locale loc("yasli_language"); 00070 CPPUNIT_FAIL; 00071 } 00072 catch (runtime_error const& /* e */) { 00073 //CPPUNIT_MESSAGE( e.what() ); 00074 } 00075 catch (...) { 00076 CPPUNIT_FAIL; 00077 } 00078 00079 try { 00080 string very_large_locale_name(1024, '?'); 00081 locale loc(very_large_locale_name.c_str()); 00082 CPPUNIT_FAIL; 00083 } 00084 catch (runtime_error const& /* e */) { 00085 //CPPUNIT_MESSAGE( e.what() ); 00086 } 00087 catch (...) { 00088 CPPUNIT_FAIL; 00089 } 00090 00091 #if defined (STLPORT) || !defined (_MSC_VER) || (_MSC_VER > 1400) 00092 try { 00093 string very_large_locale_name("LC_CTYPE="); 00094 very_large_locale_name.append(1024, '?'); 00095 locale loc(very_large_locale_name.c_str()); 00096 CPPUNIT_FAIL; 00097 } 00098 catch (runtime_error const& /* e */) { 00099 //CPPUNIT_MESSAGE( e.what() ); 00100 } 00101 catch (...) { 00102 CPPUNIT_FAIL; 00103 } 00104 00105 try { 00106 string very_large_locale_name("LC_ALL="); 00107 very_large_locale_name.append(1024, '?'); 00108 locale loc(very_large_locale_name.c_str()); 00109 CPPUNIT_FAIL; 00110 } 00111 catch (runtime_error const& /* e */) { 00112 //CPPUNIT_MESSAGE( e.what() ); 00113 } 00114 catch (...) { 00115 CPPUNIT_FAIL; 00116 } 00117 #endif 00118 00119 try { 00120 locale loc("C"); 00121 } 00122 catch (runtime_error const& /* e */) { 00123 /* CPPUNIT_MESSAGE( e.what() ); */ 00124 CPPUNIT_FAIL; 00125 } 00126 catch (...) { 00127 CPPUNIT_FAIL; 00128 } 00129 00130 try { 00131 // On platform without real localization support we should rely on the "C" locale facet. 00132 locale loc(""); 00133 } 00134 catch (runtime_error const& /* e */) { 00135 /* CPPUNIT_MESSAGE( e.what() ); */ 00136 CPPUNIT_FAIL; 00137 } 00138 catch (...) { 00139 CPPUNIT_FAIL; 00140 } 00141 00142 # endif 00143 } 00144 00145 void LocaleTest::loc_has_facet() { 00146 locale loc("C"); 00147 typedef numpunct<char> implemented_facet; 00148 CPPUNIT_ASSERT( has_facet<implemented_facet>(loc) ); 00149 /* 00150 typedef num_put<char, back_insert_iterator<string> > not_implemented_facet; 00151 CPPUNIT_ASSERT( !has_facet<not_implemented_facet>(loc) ); 00152 */ 00153 } 00154 00155 void LocaleTest::locale_init_problem() { 00156 # if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES) 00157 test_supported_locale(*this, &LocaleTest::_locale_init_problem); 00158 # endif 00159 } 00160 00161 /* 00162 * Creation of a locale instance imply initialization of some STLport internal 00163 * static objects first. We use a static instance of locale to check that this 00164 * initialization is done correctly. 00165 */ 00166 static locale global_loc; 00167 static locale other_loc(""); 00168 00169 # if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES) 00170 void LocaleTest::_locale_init_problem( const locale& loc) 00171 { 00172 # if !defined (__APPLE__) && !defined (__FreeBSD__) || \ 00173 !defined(__GNUC__) || ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__> 3))) 00174 typedef codecvt<char,char,mbstate_t> my_facet; 00175 # else 00176 // std::mbstate_t required for gcc 3.3.2 on FreeBSD... 00177 // I am not sure what key here---FreeBSD or 3.3.2... 00178 // - ptr 2005-04-04 00179 typedef codecvt<char,char,std::mbstate_t> my_facet; 00180 # endif 00181 00182 locale loc_ref(global_loc); 00183 { 00184 locale gloc( loc_ref, new my_facet() ); 00185 CPPUNIT_ASSERT( has_facet<my_facet>( gloc ) ); 00186 //The following code is just here to try to confuse the reference counting underlying mecanism: 00187 locale::global( locale::classic() ); 00188 locale::global( gloc ); 00189 } 00190 00191 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00192 try { 00193 # endif 00194 ostringstream os("test") ; 00195 locale loc2( loc, new my_facet() ); 00196 CPPUNIT_ASSERT( has_facet<my_facet>( loc2 ) ); 00197 os.imbue( loc2 ); 00198 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00199 } 00200 catch ( runtime_error& ) { 00201 CPPUNIT_FAIL; 00202 } 00203 catch ( ... ) { 00204 CPPUNIT_FAIL; 00205 } 00206 # endif 00207 00208 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00209 try { 00210 # endif 00211 ostringstream os2("test2"); 00212 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00213 } 00214 catch ( runtime_error& ) { 00215 CPPUNIT_FAIL; 00216 } 00217 catch ( ... ) { 00218 CPPUNIT_FAIL; 00219 } 00220 # endif 00221 } 00222 #endif 00223 00224 void LocaleTest::default_locale() 00225 { 00226 locale loc( "" ); 00227 } 00228 00229 class dummy_facet : public locale::facet { 00230 public: 00231 static locale::id id; 00232 }; 00233 00234 locale::id dummy_facet::id; 00235 00236 void LocaleTest::combine() 00237 { 00238 # if (!defined (STLPORT) || \ 00239 (defined (_STLP_USE_EXCEPTIONS) && !defined (_STLP_NO_MEMBER_TEMPLATES) && !defined (_STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS))) 00240 { 00241 try { 00242 locale loc(""); 00243 if (!has_facet<messages<char> >(loc)) { 00244 loc.combine<messages<char> >(loc); 00245 CPPUNIT_FAIL; 00246 } 00247 } 00248 catch (const runtime_error & /* e */) { 00249 /* CPPUNIT_MESSAGE( e.what() ); */ 00250 } 00251 00252 try { 00253 locale loc; 00254 if (!has_facet<dummy_facet>(loc)) { 00255 loc.combine<dummy_facet>(loc); 00256 CPPUNIT_FAIL; 00257 } 00258 } 00259 catch (const runtime_error & /* e */) { 00260 /* CPPUNIT_MESSAGE( e.what() ); */ 00261 } 00262 } 00263 00264 locale loc1(locale::classic()), loc2; 00265 size_t loc1_index = 0; 00266 for (size_t i = 0; _get_ref_monetary(i) != 0; ++i) { 00267 try { 00268 { 00269 locale loc(_get_ref_monetary_name(_get_ref_monetary(i))); 00270 if (loc1 == locale::classic()) 00271 { 00272 loc1 = loc; 00273 loc1_index = i; 00274 continue; 00275 } 00276 else 00277 { 00278 loc2 = loc; 00279 } 00280 } 00281 00282 //We can start the test 00283 ostringstream ostr; 00284 ostr << "combining '" << loc2.name() << "' money facets with '" << loc1.name() << "'"; 00285 CPPUNIT_MESSAGE( ostr.str().c_str() ); 00286 00287 //We are going to combine money facets as all formats are different. 00288 { 00289 //We check that resulting locale has correctly acquire loc2 facets. 00290 locale loc = loc1.combine<moneypunct<char, true> >(loc2); 00291 loc = loc.combine<moneypunct<char, false> >(loc2); 00292 loc = loc.combine<money_put<char> >(loc2); 00293 loc = loc.combine<money_get<char> >(loc2); 00294 00295 //Check loc has the correct facets: 00296 _money_put_get2(loc2, loc, _get_ref_monetary(i)); 00297 00298 //Check loc1 has not been impacted: 00299 _money_put_get2(loc1, loc1, _get_ref_monetary(loc1_index)); 00300 00301 //Check loc2 has not been impacted: 00302 _money_put_get2(loc2, loc2, _get_ref_monetary(i)); 00303 } 00304 { 00305 //We check that resulting locale has not wrongly acquire loc1 facets that hasn't been combine: 00306 locale loc = loc2.combine<numpunct<char> >(loc1); 00307 loc = loc.combine<time_put<char> >(loc1); 00308 loc = loc.combine<time_get<char> >(loc1); 00309 00310 //Check loc has the correct facets: 00311 _money_put_get2(loc2, loc, _get_ref_monetary(i)); 00312 00313 //Check loc1 has not been impacted: 00314 _money_put_get2(loc1, loc1, _get_ref_monetary(loc1_index)); 00315 00316 //Check loc2 has not been impacted: 00317 _money_put_get2(loc2, loc2, _get_ref_monetary(i)); 00318 } 00319 00320 { 00321 // Check auto combination do not result in weird reference counting behavior 00322 // (might generate a crash). 00323 loc1.combine<numpunct<char> >(loc1); 00324 } 00325 00326 loc1 = loc2; 00327 loc1_index = i; 00328 } 00329 catch (runtime_error const&) { 00330 //This locale is not supported. 00331 continue; 00332 } 00333 } 00334 # endif 00335 } 00336 00337 #endif Generated on Sun May 27 2012 04:35:29 for ReactOS by
1.7.6.1
|