Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygennum_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 <stdexcept> 00007 00008 # include "complete_digits.h" 00009 00010 # if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00011 using namespace std; 00012 # endif 00013 00014 struct ref_locale { 00015 const char *name; 00016 const char *decimal_point; 00017 const char *thousands_sep; 00018 }; 00019 00020 static const ref_locale tested_locales[] = { 00021 //{ name, decimal_point, thousands_sepy_thousands_sep}, 00022 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00023 { "fr_FR", ",", "\xa0"}, 00024 { "ru_RU.koi8r", ",", "."}, 00025 { "en_GB", ".", ","}, 00026 { "en_US", ".", ","}, 00027 # endif 00028 { "C", ".", ","}, 00029 }; 00030 00031 // 00032 // tests implementation 00033 // 00034 void LocaleTest::_num_put_get( const locale& loc, const ref_locale* prl ) { 00035 const ref_locale& rl = *prl; 00036 CPPUNIT_ASSERT( has_facet<numpunct<char> >(loc) ); 00037 numpunct<char> const& npct = use_facet<numpunct<char> >(loc); 00038 CPPUNIT_ASSERT( npct.decimal_point() == *rl.decimal_point ); 00039 00040 float val = 1234.56f; 00041 ostringstream fostr; 00042 fostr.imbue(loc); 00043 fostr << val; 00044 00045 string ref = "1"; 00046 if (!npct.grouping().empty()) { 00047 ref += npct.thousands_sep(); 00048 } 00049 ref += "234"; 00050 ref += npct.decimal_point(); 00051 ref += "56"; 00052 //cout << "In " << loc.name() << " 1234.56 is written: " << fostr.str() << endl; 00053 CPPUNIT_ASSERT( fostr.str() == ref ); 00054 00055 val = 12345678.9f; 00056 ref = "1"; 00057 ref += npct.decimal_point(); 00058 ref += "23457e+"; 00059 string digits = "7"; 00060 complete_digits(digits); 00061 ref += digits; 00062 fostr.str(""); 00063 fostr << val; 00064 CPPUNIT_ASSERT( fostr.str() == ref ); 00065 00066 val = 1000000000.0f; 00067 fostr.str(""); 00068 fostr << val; 00069 digits = "9"; 00070 complete_digits(digits); 00071 CPPUNIT_ASSERT( fostr.str() == string("1e+") + digits ); 00072 00073 val = 1234.0f; 00074 ref = "1"; 00075 if (!npct.grouping().empty()) { 00076 ref += npct.thousands_sep(); 00077 } 00078 ref += "234"; 00079 fostr.str(""); 00080 fostr << val; 00081 CPPUNIT_ASSERT( fostr.str() == ref ); 00082 00083 val = 10000001.0f; 00084 fostr.str(""); 00085 fostr << val; 00086 digits = "7"; 00087 complete_digits(digits); 00088 CPPUNIT_ASSERT( fostr.str() == string("1e+") + digits ); 00089 00090 if (npct.grouping().size() == 1 && npct.grouping()[0] == 3) { 00091 int ival = 1234567890; 00092 fostr.str(""); 00093 fostr << ival; 00094 ref = "1"; 00095 ref += npct.thousands_sep(); 00096 ref += "234"; 00097 ref += npct.thousands_sep(); 00098 ref += "567"; 00099 ref += npct.thousands_sep(); 00100 ref += "890"; 00101 CPPUNIT_ASSERT( fostr.str() == ref ); 00102 } 00103 00104 #if defined (__BORLANDC__) 00105 num_put<char> const& nput = use_facet<num_put<char> >(loc); 00106 typedef numeric_limits<double> limd; 00107 fostr.setf(ios_base::uppercase | ios_base::showpos); 00108 00109 if (limd::has_infinity) { 00110 double infinity = limd::infinity(); 00111 fostr.str(""); 00112 nput.put(fostr, fostr, ' ', infinity); 00113 CPPUNIT_ASSERT( fostr.str() == string("+Inf") ); 00114 } 00115 00116 if (limd::has_quiet_NaN) { 00117 /* Ignore FPU exceptions */ 00118 unsigned int _float_control_word = _control87(0, 0); 00119 _control87(EM_INVALID|EM_INEXACT, MCW_EM); 00120 double qnan = limd::quiet_NaN(); 00121 /* Reset floating point control word */ 00122 _clear87(); 00123 _control87(_float_control_word, MCW_EM); 00124 fostr.str(""); 00125 nput.put(fostr, fostr, ' ', qnan); 00126 CPPUNIT_ASSERT( fostr.str() == string("+NaN") ); 00127 } 00128 #endif 00129 } 00130 00131 typedef void (LocaleTest::*_Test) (const locale&, const ref_locale*); 00132 static void test_supported_locale(LocaleTest& inst, _Test __test) { 00133 size_t n = sizeof(tested_locales) / sizeof(tested_locales[0]); 00134 for (size_t i = 0; i < n; ++i) { 00135 locale loc; 00136 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00137 try 00138 # endif 00139 { 00140 locale tmp(tested_locales[i].name); 00141 loc = tmp; 00142 } 00143 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00144 catch (runtime_error const&) { 00145 //This locale is not supported. 00146 continue; 00147 } 00148 # endif 00149 CPPUNIT_MESSAGE( loc.name().c_str() ); 00150 (inst.*__test)(loc, tested_locales + i); 00151 00152 { 00153 locale tmp(locale::classic(), tested_locales[i].name, locale::numeric); 00154 loc = tmp; 00155 } 00156 (inst.*__test)(loc, tested_locales + i); 00157 00158 { 00159 locale tmp(locale::classic(), new numpunct_byname<char>(tested_locales[i].name)); 00160 loc = tmp; 00161 } 00162 (inst.*__test)(loc, tested_locales + i); 00163 } 00164 } 00165 00166 void LocaleTest::num_put_get() 00167 { test_supported_locale(*this, &LocaleTest::_num_put_get); } 00168 00169 void LocaleTest::numpunct_by_name() 00170 { 00171 /* 00172 * Check of the 22.1.1.2.7 standard point. Construction of a locale 00173 * instance from a null pointer or an unknown name should result in 00174 * a runtime_error exception. 00175 */ 00176 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00177 # if defined (STLPORT) || !defined (__GNUC__) 00178 try { 00179 locale loc(locale::classic(), new numpunct_byname<char>(static_cast<char const*>(0))); 00180 CPPUNIT_FAIL; 00181 } 00182 catch (runtime_error const& /* e */) { 00183 //CPPUNIT_MESSAGE( e.what() ); 00184 } 00185 catch (...) { 00186 CPPUNIT_FAIL; 00187 } 00188 # endif 00189 00190 try { 00191 locale loc(locale::classic(), new numpunct_byname<char>("yasli_language")); 00192 CPPUNIT_FAIL; 00193 } 00194 catch (runtime_error const& /* e */) { 00195 //CPPUNIT_MESSAGE( e.what() ); 00196 } 00197 catch (...) { 00198 CPPUNIT_FAIL; 00199 } 00200 00201 try { 00202 string veryLongFacetName("LC_NUMERIC="); 00203 veryLongFacetName.append(512, '?'); 00204 locale loc(locale::classic(), new numpunct_byname<char>(veryLongFacetName.c_str())); 00205 CPPUNIT_FAIL; 00206 } 00207 catch (runtime_error const& /* e */) { 00208 //CPPUNIT_MESSAGE( e.what() ); 00209 } 00210 catch (...) { 00211 CPPUNIT_FAIL; 00212 } 00213 00214 try { 00215 locale loc(locale::classic(), "C", locale::numeric); 00216 } 00217 catch (runtime_error const& e) { 00218 CPPUNIT_MESSAGE( e.what() ); 00219 CPPUNIT_FAIL; 00220 } 00221 catch (...) { 00222 CPPUNIT_FAIL; 00223 } 00224 00225 try { 00226 // On platform without real localization support we should rely on the "C" facet. 00227 locale loc(locale::classic(), "", locale::numeric); 00228 } 00229 catch (runtime_error const& e) { 00230 CPPUNIT_MESSAGE( e.what() ); 00231 CPPUNIT_FAIL; 00232 } 00233 catch (...) { 00234 CPPUNIT_FAIL; 00235 } 00236 00237 try { 00238 locale loc(locale::classic(), new numpunct_byname<char>("C")); 00239 numpunct<char> const& cfacet_byname = use_facet<numpunct<char> >(loc); 00240 numpunct<char> const& cfacet = use_facet<numpunct<char> >(locale::classic()); 00241 00242 CPPUNIT_CHECK( cfacet_byname.decimal_point() == cfacet.decimal_point() ); 00243 CPPUNIT_CHECK( cfacet_byname.grouping() == cfacet.grouping() ); 00244 if (!cfacet.grouping().empty()) 00245 CPPUNIT_CHECK( cfacet_byname.thousands_sep() == cfacet.thousands_sep() ); 00246 # if !defined (STLPORT) || !defined (__GLIBC__) 00247 CPPUNIT_CHECK( cfacet_byname.truename() == cfacet.truename() ); 00248 CPPUNIT_CHECK( cfacet_byname.falsename() == cfacet.falsename() ); 00249 # endif 00250 } 00251 catch (runtime_error const& /* e */) { 00252 //CPPUNIT_MESSAGE( e.what() ); 00253 CPPUNIT_FAIL; 00254 } 00255 catch (...) { 00256 CPPUNIT_FAIL; 00257 } 00258 00259 try { 00260 // On platform without real localization support we should rely on the "C" locale facet. 00261 locale loc(locale::classic(), new numpunct_byname<char>("")); 00262 } 00263 catch (runtime_error const& e) { 00264 CPPUNIT_MESSAGE( e.what() ); 00265 CPPUNIT_FAIL; 00266 } 00267 catch (...) { 00268 CPPUNIT_FAIL; 00269 } 00270 00271 # if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T) 00272 # if defined (STLPORT) || !defined (__GNUC__) 00273 try { 00274 locale loc(locale::classic(), new numpunct_byname<wchar_t>(static_cast<char const*>(0))); 00275 CPPUNIT_FAIL; 00276 } 00277 catch (runtime_error const&) { 00278 } 00279 catch (...) { 00280 CPPUNIT_FAIL; 00281 } 00282 # endif 00283 00284 try { 00285 locale loc(locale::classic(), new numpunct_byname<wchar_t>("yasli_language")); 00286 CPPUNIT_FAIL; 00287 } 00288 catch (runtime_error const&) { 00289 } 00290 catch (...) { 00291 CPPUNIT_FAIL; 00292 } 00293 # endif 00294 # endif 00295 } 00296 00297 #endif Generated on Sat May 26 2012 04:34:36 for ReactOS by
1.7.6.1
|