📄 money_facets_test.cpp
字号:
#include "locale_test.h"#if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)# include <locale># include <sstream># include <stdexcept># if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)using namespace std;# endifstruct ref_monetary { const char *name; 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;};static const ref_monetary tested_locales[] = {//{ name, money_int_prefix, money_int_prefix_old, money_prefix, money_suffix, money_decimal_point, money_thousands_sep},# if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) { "fr_FR", "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 ", "", "$", "", ".", "," },# endif { "C", "", "", "", "", " ", " " },};const ref_monetary* LocaleTest::_get_ref_monetary(size_t i){ if (i < sizeof(tested_locales) / sizeof(tested_locales[0])) { return tested_locales + i; } return 0;}const char* LocaleTest::_get_ref_monetary_name(const ref_monetary* _ref){ return _ref->name;}void LocaleTest::_money_put_get( const locale& loc, const ref_monetary* rl ){ _money_put_get2(loc, loc, rl);}void LocaleTest::_money_put_get2( const locale& loc, const locale& streamLoc, const ref_monetary* prl ){ const ref_monetary &rl = *prl; 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(); //CPPUNIT_MESSAGE(str_res.c_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(); //CPPUNIT_MESSAGE(str_res.c_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_monetary* prl ){ const ref_monetary &rl = *prl; 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 ); 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; } if (dom_fmp.frac_digits() != 0) { CPPUNIT_ASSERT( str_res[index++] == '0' ); CPPUNIT_ASSERT( str_res[index++] == dom_fmp.decimal_point() ); for ( int fd = 1; fd < dom_fmp.frac_digits(); ++fd ) { CPPUNIT_ASSERT( str_res[index++] == '0' ); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -