📄 locale_test.cpp
字号:
#include <string>#if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)# include <sstream># include <locale># include <stdexcept># include <memory># include <algorithm>//# include <iostream># include "cppunit/cppunit_proxy.h"# if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)using namespace std;# endifstruct ref_locale { const char *name; const char *decimal_point; const char *thousands_sep; const char *money_int_prefix; const char *money_int_prefix_old; const char *money_prefix; const char *money_suffix; const char *money_decimal_point; const char *money_thousands_sep;};// Pls, don't write #ifdef _STLP_REAL_LOCALE_IMPLEMENTED here!// It undefined in any case!!!!!static const ref_locale tested_locales[] = {//{ name, decimal_point, thousands_sep, money_int_prefix, money_int_prefix_old, money_prefix, money_suffix, money_decimal_point, money_thousands_sep}, { "fr_FR", ",", "\xa0", "EUR ", "FRF ", "", "", ",",# if defined (WIN32) || defined (_WIN32) "\xa0" },# else " " },# endif { "ru_RU.koi8r", ",", ".", "RUB ", "RUR ", "", "\xd2\xd5\xc2", ".", " " }, { "en_GB", ".", ",", "GBP ", "", "\xa3", "", ".", "," }, { "en_US", ".", ",", "USD ", "", "$", "", ".", "," }, { "C", ".", ",", "", "", "", "", " ", " " },};//// TestCase class//class LocaleTest : public CPPUNIT_NS::TestCase{ CPPUNIT_TEST_SUITE(LocaleTest);# if defined (STLPORT) && !defined (_STLP_USE_EXCEPTIONS) CPPUNIT_IGNORE;# endif CPPUNIT_TEST(locale_by_name); CPPUNIT_STOP_IGNORE; CPPUNIT_TEST(loc_has_facet); CPPUNIT_TEST(num_put_get); CPPUNIT_TEST(money_put_get); CPPUNIT_TEST(money_put_X_bug); CPPUNIT_TEST(time_put_get);# if defined (__DMC__) && defined (_DLL) CPPUNIT_IGNORE;# endif CPPUNIT_TEST(collate_facet); CPPUNIT_TEST(ctype_facet);# if defined (STLPORT) && defined (_STLP_NO_MEMBER_TEMPLATES) CPPUNIT_IGNORE;# endif CPPUNIT_TEST(locale_init_problem); CPPUNIT_STOP_IGNORE; CPPUNIT_TEST(default_locale);# if !defined (STLPORT) CPPUNIT_IGNORE;# endif CPPUNIT_TEST(facet_id); CPPUNIT_STOP_IGNORE;# if defined (STLPORT) && \ (!defined (_STLP_USE_EXCEPTIONS) || defined (_STLP_NO_MEMBER_TEMPLATES) || defined (_STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS)) CPPUNIT_IGNORE;# endif CPPUNIT_TEST(combine); CPPUNIT_TEST_SUITE_END();public: void locale_by_name(); void loc_has_facet(); void num_put_get(); void money_put_get(); void time_put_get(); void collate_facet(); void ctype_facet(); void locale_init_problem(); void money_put_X_bug(); void default_locale(); void facet_id(); void combine();private: void _loc_has_facet( const locale&, const ref_locale& ); void _num_put_get( const locale&, const ref_locale& ); void _money_put_get( const locale&, const ref_locale& ); void _money_put_get2( const locale& loc, const locale& streamLoc, const ref_locale& ); void _time_put_get( const locale&, const ref_locale& ); void _ctype_facet( const locale&, const ref_locale& ); void _locale_init_problem( const locale&, const ref_locale& ); void _money_put_X_bug( const locale&, const ref_locale& );};CPPUNIT_TEST_SUITE_REGISTRATION(LocaleTest);//// tests implementation//void LocaleTest::_num_put_get( const locale& loc, const ref_locale& rl ) { CPPUNIT_ASSERT( has_facet<numpunct<char> >(loc) ); numpunct<char> const& npct = use_facet<numpunct<char> >(loc); CPPUNIT_ASSERT( npct.decimal_point() == *rl.decimal_point ); float val = 1234.56f; ostringstream fostr; fostr.imbue(loc); fostr << val; string ref = "1"; if (!npct.grouping().empty()) { ref += npct.thousands_sep(); } ref += "234"; ref += npct.decimal_point(); ref += "56"; //cout << "In " << loc.name() << " 1234.56 is written: " << fostr.str() << endl; CPPUNIT_ASSERT( fostr.str() == ref ); val = 12345678.9f; ref = "1"; ref += npct.decimal_point(); ref += "23457e+07"; fostr.str(""); fostr << val; CPPUNIT_ASSERT( fostr.str() == ref ); val = 1000000000.0f; fostr.str(""); fostr << val; CPPUNIT_ASSERT( fostr.str() == "1e+09" ); val = 1234.0f; ref = "1"; if (!npct.grouping().empty()) { ref += npct.thousands_sep(); } ref += "234"; fostr.str(""); fostr << val; CPPUNIT_ASSERT( fostr.str() == ref ); val = 10000001.0f; fostr.str(""); fostr << val; CPPUNIT_ASSERT( fostr.str() == "1e+07" );}void LocaleTest::_money_put_get( const locale& loc, const ref_locale& rl ){ _money_put_get2(loc, loc, rl);}void LocaleTest::_money_put_get2( const locale& loc, const locale& streamLoc, const ref_locale& rl ){ CPPUNIT_ASSERT( has_facet<money_put<char> >(loc) ); money_put<char> const& fmp = use_facet<money_put<char> >(loc); CPPUNIT_ASSERT( has_facet<money_get<char> >(loc) ); money_get<char> const& fmg = use_facet<money_get<char> >(loc); ostringstream ostr; ostr.imbue(streamLoc); ostr << showbase; //Check a positive value (international format) { string str_res; //money_put { CPPUNIT_ASSERT( (has_facet<moneypunct<char, true> >(loc)) ); moneypunct<char, true> const& intl_fmp = use_facet<moneypunct<char, true> >(loc); ostreambuf_iterator<char, char_traits<char> > res = fmp.put(ostr, true, ostr, ' ', 123456); CPPUNIT_ASSERT( !res.failed() ); str_res = ostr.str(); size_t fieldIndex = 0; size_t index = 0; //On a positive value we skip the sign field if exists: if (intl_fmp.pos_format().field[fieldIndex] == money_base::sign) { ++fieldIndex; } // international currency abbreviation, if it is before value /* * int_curr_symbol * * The international currency symbol. The operand is a four-character * string, with the first three characters containing the alphabetic * international currency symbol in accordance with those specified * in the ISO 4217 specification. The fourth character is the character used * to separate the international currency symbol from the monetary quantity. * * (http://www.opengroup.org/onlinepubs/7990989775/xbd/locale.html) */ string::size_type p = strlen( rl.money_int_prefix ); if (p != 0) { CPPUNIT_ASSERT( intl_fmp.pos_format().field[fieldIndex] == money_base::symbol ); string::size_type p_old = strlen( rl.money_int_prefix_old ); CPPUNIT_ASSERT( (str_res.substr(index, p) == rl.money_int_prefix) || ((p_old != 0) && (str_res.substr(index, p_old) == rl.money_int_prefix_old)) ); if ( str_res.substr(index, p) == rl.money_int_prefix ) { index += p; } else { index += p_old; } ++fieldIndex; } // space after currency if (intl_fmp.pos_format().field[fieldIndex] == money_base::space || intl_fmp.pos_format().field[fieldIndex] == money_base::none) { // iternational currency symobol has four chars, one of these chars // is separator, so if format has space on this place, it should // be skipped. ++fieldIndex; } // sign if (intl_fmp.pos_format().field[fieldIndex] == money_base::sign) { ++fieldIndex; } // value CPPUNIT_ASSERT( str_res[index++] == '1' ); if (!intl_fmp.grouping().empty()) { CPPUNIT_ASSERT( str_res[index++] == /* intl_fmp.thousands_sep() */ *rl.money_thousands_sep ); } CPPUNIT_ASSERT( str_res[index++] == '2' ); CPPUNIT_ASSERT( str_res[index++] == '3' ); CPPUNIT_ASSERT( str_res[index++] == '4' ); if (intl_fmp.frac_digits() != 0) { CPPUNIT_ASSERT( str_res[index++] == /* intl_fmp.decimal_point() */ *rl.money_decimal_point ); } CPPUNIT_ASSERT( str_res[index++] == '5' ); CPPUNIT_ASSERT( str_res[index++] == '6' ); ++fieldIndex; // sign if (intl_fmp.pos_format().field[fieldIndex] == money_base::sign) { ++fieldIndex; } // space if (intl_fmp.pos_format().field[fieldIndex] == money_base::space ) { CPPUNIT_ASSERT( str_res[index++] == ' ' ); ++fieldIndex; } // sign if (intl_fmp.pos_format().field[fieldIndex] == money_base::sign) { ++fieldIndex; } //as space cannot be last the only left format can be none: while ( fieldIndex < 3 ) { CPPUNIT_ASSERT( intl_fmp.pos_format().field[fieldIndex] == money_base::none ); ++fieldIndex; } } //money_get { ios_base::iostate err = ios_base::goodbit; string digits; istringstream istr(str_res); ostr.str( "" ); ostr.clear(); fmg.get(istr, istreambuf_iterator<char, char_traits<char> >(), true, ostr, err, digits); CPPUNIT_ASSERT( (err & (ios_base::failbit | ios_base::badbit)) == 0 ); CPPUNIT_ASSERT( digits == "123456" ); } } ostr.str(""); //Check a negative value (national format) { CPPUNIT_ASSERT( (has_facet<moneypunct<char, false> >(loc)) ); moneypunct<char, false> const& dom_fmp = use_facet<moneypunct<char, false> >(loc); string str_res; //Check money_put { ostreambuf_iterator<char, char_traits<char> > res = fmp.put(ostr, false, ostr, ' ', -123456); CPPUNIT_ASSERT( !res.failed() ); str_res = ostr.str(); size_t fieldIndex = 0; size_t index = 0; if (dom_fmp.neg_format().field[fieldIndex] == money_base::sign) { CPPUNIT_ASSERT( str_res.substr(index, dom_fmp.negative_sign().size()) == dom_fmp.negative_sign() ); index += dom_fmp.negative_sign().size(); ++fieldIndex; } string::size_type p = strlen( rl.money_prefix ); if (p != 0) { CPPUNIT_ASSERT( str_res.substr(index, p) == rl.money_prefix ); index += p; ++fieldIndex; } if (dom_fmp.neg_format().field[fieldIndex] == money_base::space || dom_fmp.neg_format().field[fieldIndex] == money_base::none) { CPPUNIT_ASSERT( str_res[index++] == ' ' ); ++fieldIndex; } CPPUNIT_ASSERT( str_res[index++] == '1' ); if (!dom_fmp.grouping().empty()) { CPPUNIT_ASSERT( str_res[index++] == dom_fmp.thousands_sep() ); } CPPUNIT_ASSERT( str_res[index++] == '2' ); CPPUNIT_ASSERT( str_res[index++] == '3' ); CPPUNIT_ASSERT( str_res[index++] == '4' ); if (dom_fmp.frac_digits() != 0) { CPPUNIT_ASSERT( str_res[index++] == dom_fmp.decimal_point() ); } CPPUNIT_ASSERT( str_res[index++] == '5' ); CPPUNIT_ASSERT( str_res[index++] == '6' ); ++fieldIndex; //space cannot be last: if ((fieldIndex < 3) && dom_fmp.neg_format().field[fieldIndex] == money_base::space) { CPPUNIT_ASSERT( str_res[index++] == ' ' ); ++fieldIndex; } if (fieldIndex == 3) { //If none is last we should not add anything to the resulting string: if (dom_fmp.neg_format().field[fieldIndex] == money_base::none) { CPPUNIT_ASSERT( index == str_res.size() ); } else { CPPUNIT_ASSERT( dom_fmp.neg_format().field[fieldIndex] == money_base::symbol ); CPPUNIT_ASSERT( str_res.substr(index, strlen(rl.money_suffix)) == rl.money_suffix ); } } } //money_get { ios_base::iostate err = ios_base::goodbit;# if defined (STLPORT) _STLP_LONGEST_FLOAT_TYPE val;# else long double val;# endif istringstream istr(str_res); fmg.get(istr, istreambuf_iterator<char, char_traits<char> >(), false, ostr, err, val); CPPUNIT_ASSERT( (err & (ios_base::failbit | ios_base::badbit)) == 0 ); if (dom_fmp.negative_sign().empty()) { //Without negative sign there is no way to guess the resulting amount sign ("C" locale): CPPUNIT_ASSERT( val == 123456 ); } else { CPPUNIT_ASSERT( val == -123456 ); } } }}// Test for bug in case when number of digits in value less then number// of digits in fraction. I.e. '9' should be printed as '0.09',// if x.frac_digits() == 2.void LocaleTest::_money_put_X_bug( const locale& loc, const ref_locale& rl ){ CPPUNIT_ASSERT( has_facet<money_put<char> >(loc) ); money_put<char> const& fmp = use_facet<money_put<char> >(loc); ostringstream ostr; ostr.imbue(loc); ostr << showbase; // ostr.str(""); // Check value with one decimal digit: { CPPUNIT_ASSERT( (has_facet<moneypunct<char, false> >(loc)) ); moneypunct<char, false> const& dom_fmp = use_facet<moneypunct<char, false> >(loc); string str_res; // Check money_put { ostreambuf_iterator<char, char_traits<char> > res = fmp.put(ostr, false, ostr, ' ', 9); CPPUNIT_ASSERT( !res.failed() ); str_res = ostr.str(); size_t fieldIndex = 0; size_t index = 0; if (dom_fmp.pos_format().field[fieldIndex] == money_base::sign) { CPPUNIT_ASSERT( str_res.substr(index, dom_fmp.positive_sign().size()) == dom_fmp.positive_sign() ); index += dom_fmp.positive_sign().size(); ++fieldIndex; } string::size_type p = strlen( rl.money_prefix ); if (p != 0) { CPPUNIT_ASSERT( str_res.substr(index, p) == rl.money_prefix );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -