📄 locale_test.cpp
字号:
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' ); } } CPPUNIT_ASSERT( str_res[index++] == '9' ); ++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 ); } } } } ostr.str(""); // Check value with two 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, ' ', 90); 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() - 1; ++fd ) { CPPUNIT_ASSERT( str_res[index++] == '0' ); } } CPPUNIT_ASSERT( str_res[index++] == '9' ); if (dom_fmp.frac_digits() != 0) { CPPUNIT_ASSERT( str_res[index++] == '0' ); } ++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 ); } } } }}void LocaleTest::_time_put_get( const locale& loc, const ref_locale&){ CPPUNIT_ASSERT( has_facet<time_put<char> >(loc) ); const time_put<char>& tmp = use_facet<time_put<char> >(loc); struct tm xmas = { 0, 0, 12, 25, 11, 93 }; ostringstream ostr; ostr.imbue(loc); string format = "%B %d %Y"; time_put<char>::iter_type ret = tmp.put(ostr, ostr, ' ', &xmas, format.data(), format.data() + format.size()); CPPUNIT_ASSERT( !ret.failed() ); /* * In other words, user conformation is required for reliable parsing * of user-entered dates and times, but machine-generated formats can be * parsed reliably. This allows parsers to be aggressive about interpreting * user variations on standard format. * * ISO/IEC 14882, 22.2.5.1 */ CPPUNIT_ASSERT( has_facet<time_get<char> >(loc) ); const time_get<char>& tmg = use_facet<time_get<char> >(loc); basic_ios<char> io(0); io.imbue(loc); istringstream istr( ostr.str() ); istreambuf_iterator<char, char_traits<char> > i( istr ); istreambuf_iterator<char, char_traits<char> > e; ios_base::iostate err = ios_base::goodbit; struct tm other = { 15, 20, 9, 14, 7, 105 }; i = tmg.get_monthname( i, e, io, err, &other ); CPPUNIT_ASSERT( err == ios_base::goodbit ); CPPUNIT_ASSERT( other.tm_mon == xmas.tm_mon ); ++i; ++i; ++i; ++i; // skip day of month and spaces around it i = tmg.get_year( i, e, io, err, &other ); CPPUNIT_ASSERT( err == ios_base::eofbit ); CPPUNIT_ASSERT( other.tm_year == xmas.tm_year ); ostringstream ostrX; ostrX.imbue(loc); format = "%x %X"; ret = tmp.put(ostrX, ostrX, ' ', &xmas, format.data(), format.data() + format.size()); CPPUNIT_ASSERT( !ret.failed() ); istringstream istrX( ostrX.str() ); istreambuf_iterator<char, char_traits<char> > j( istrX ); err = ios_base::goodbit; struct tm yet_more = { 15, 20, 9, 14, 7, 105 }; j = tmg.get_date( j, e, io, err, &yet_more ); CPPUNIT_ASSERT( err == ios_base::goodbit ); CPPUNIT_ASSERT( yet_more.tm_sec != xmas.tm_sec ); CPPUNIT_ASSERT( yet_more.tm_min != xmas.tm_min ); CPPUNIT_ASSERT( yet_more.tm_hour != xmas.tm_hour ); CPPUNIT_ASSERT( yet_more.tm_mday == xmas.tm_mday ); CPPUNIT_ASSERT( yet_more.tm_mon == xmas.tm_mon ); CPPUNIT_ASSERT( yet_more.tm_year == xmas.tm_year ); ++j; // skip space j = tmg.get_time( j, e, io, err, &yet_more ); CPPUNIT_ASSERT( err == ios_base::eofbit || err == ios_base::goodbit ); CPPUNIT_ASSERT( yet_more.tm_sec == xmas.tm_sec ); CPPUNIT_ASSERT( yet_more.tm_min == xmas.tm_min ); CPPUNIT_ASSERT( yet_more.tm_hour == xmas.tm_hour ); CPPUNIT_ASSERT( yet_more.tm_mday == xmas.tm_mday ); CPPUNIT_ASSERT( yet_more.tm_mon == xmas.tm_mon ); CPPUNIT_ASSERT( yet_more.tm_year == xmas.tm_year );}void LocaleTest::_ctype_facet( const locale& loc, const ref_locale&){# if !(defined (__DMC__) && defined (_DLL)) CPPUNIT_ASSERT( has_facet<ctype<char> >(loc) ); ctype<char> const& ct = use_facet<ctype<char> >(loc); //is { CPPUNIT_ASSERT( ct.is(ctype_base::digit, '0') ); CPPUNIT_ASSERT( ct.is(ctype_base::upper, 'A') ); CPPUNIT_ASSERT( ct.is(ctype_base::lower, 'a') ); CPPUNIT_ASSERT( ct.is(ctype_base::alpha, 'A') ); CPPUNIT_ASSERT( ct.is(ctype_base::space, ' ') ); CPPUNIT_ASSERT( !ct.is(ctype_base::space, '2') ); CPPUNIT_ASSERT( ct.is(ctype_base::punct, '.') ); CPPUNIT_ASSERT( ct.is(ctype_base::xdigit, 'a') ); } //is range { char values[] = "0Aa ."; ctype_base::mask res[sizeof(values)]; ct.is(values, values + sizeof(values), res); // '0' CPPUNIT_ASSERT( (res[0] & ctype_base::print) != 0 ); CPPUNIT_ASSERT( (res[0] & ctype_base::digit) != 0 ); CPPUNIT_ASSERT( (res[0] & ctype_base::xdigit) != 0 ); // 'A' CPPUNIT_ASSERT( (res[1] & ctype_base::print) != 0 ); CPPUNIT_ASSERT( (res[1] & ctype_base::alpha) != 0 ); CPPUNIT_ASSERT( (res[1] & ctype_base::xdigit) != 0 ); CPPUNIT_ASSERT( (res[1] & ctype_base::upper) != 0 ); // 'a' CPPUNIT_ASSERT( (res[2] & ctype_base::print) != 0 ); CPPUNIT_ASSERT( (res[2] & ctype_base::alpha) != 0 ); CPPUNIT_ASSERT( (res[2] & ctype_base::xdigit) != 0 ); CPPUNIT_ASSERT( (res[2] & ctype_base::lower) != 0 ); CPPUNIT_ASSERT( (res[2] & ctype_base::space) == 0 ); // ' ' CPPUNIT_ASSERT( (res[3] & ctype_base::print) != 0 ); CPPUNIT_ASSERT( (res[3] & ctype_base::space) != 0 ); CPPUNIT_ASSERT( (res[3] & ctype_base::digit) == 0 ); // '.' CPPUNIT_ASSERT( (res[4] & ctype_base::print) != 0 ); CPPUNIT_ASSERT( (res[4] & ctype_base::punct) != 0 ); CPPUNIT_ASSERT( (res[4] & ctype_base::digit) == 0 ); } //scan_is { char range[] = "abAc123 ."; const char *rbeg = range; const char *rend = range + sizeof(range); const char *res; res = ct.scan_is((ctype_base::mask)(ctype_base::alpha | ctype_base::lower), rbeg, rend); CPPUNIT_ASSERT( res != rend ); CPPUNIT_ASSERT( *res == 'a' ); res = ct.scan_is(ctype_base::upper, rbeg, rend); CPPUNIT_ASSERT( res != rend ); CPPUNIT_ASSERT( *res == 'A' ); res = ct.scan_is(ctype_base::punct, rbeg, rend); CPPUNIT_ASSERT( res != rend ); CPPUNIT_ASSERT( *res == '.' ); } //scan_not { char range[] = "abAc123 ."; const char *rbeg = range; const char *rend = range + sizeof(range); const char *res; res = ct.scan_not((ctype_base::mask)(ctype_base::alpha | ctype_base::lower), rbeg, rend); CPPUNIT_ASSERT( res != rend ); CPPUNIT_ASSERT( *res == '1' ); res = ct.scan_not(ctype_base::alpha, rbeg, rend); CPPUNIT_ASSERT( res != rend ); CPPUNIT_ASSERT( *res == '1' ); res = ct.scan_not(ctype_base::punct, rbeg, rend); CPPUNIT_ASSERT( res != rend ); CPPUNIT_ASSERT( *res == 'a' ); } //toupper { CPPUNIT_ASSERT( ct.toupper('a') == 'A' ); CPPUNIT_ASSERT( ct.toupper('A') == 'A' ); CPPUNIT_ASSERT( ct.toupper('1') == '1' ); } //toupper range { char range[] = "abAc1"; char expected_range[] = "ABAC1"; ct.toupper(range, range + sizeof(range)); CPPUNIT_ASSERT( equal(range, range + sizeof(range), expected_range) ); } //tolower { CPPUNIT_ASSERT( ct.tolower('A') == 'a' ); CPPUNIT_ASSERT( ct.tolower('a') == 'a' ); CPPUNIT_ASSERT( ct.tolower('1') == '1' ); } //tolower range { char range[] = "ABaC1"; char expected_range[] = "abac1"; ct.tolower(range, range + sizeof(range)); CPPUNIT_ASSERT( equal(range, range + sizeof(range), expected_range) ); } //widen { CPPUNIT_ASSERT( ct.widen('a') == 'a' ); } //widen range { char range[] = "ABaC1"; char res[sizeof(range)]; ct.widen(range, range + sizeof(range), res); CPPUNIT_ASSERT( equal(range, range + sizeof(range), res) ); } //narrow { CPPUNIT_ASSERT( ct.narrow('a', 'b') == 'a' ); } //narrow range { char range[] = "ABaC1"; char res[sizeof(range)]; ct.narrow(range, range + sizeof(range), 'b', res); CPPUNIT_ASSERT( equal(range, range + sizeof(range), res) ); }# endif /* __DMC__ */}template <class _Tp>void test_supported_locale(LocaleTest inst, _Tp __test) { size_t n = sizeof(tested_locales) / sizeof(tested_locales[0]); for (size_t i = 0; i < n; ++i) { auto_ptr<locale> loc;# if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) try { loc.reset(_CHECK_PTR(new locale(tested_locales[i].name))); } catch (runtime_error const&) { //This locale is not supported. continue; }# else //Without exception support we only test C locale. if (tested_locales[i].name[0] != 'C' || tested_locales[i].name[1] != 0) continue; loc.reset(_CHECK_PTR(new locale(tested_locales[i].name)));# endif CPPUNIT_MESSAGE( loc->name().c_str() ); (inst.*__test)(*loc, tested_locales[i]); }}void LocaleTest::locale_by_name() {# if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) /* * Check of the 22.1.1.2.7 standard point. Construction of a locale * instance from a null pointer or an unknown name should result in * a runtime_error exception. */ try { locale loc(static_cast<char const*>(0)); CPPUNIT_ASSERT( false ); } catch (runtime_error const&) { } catch (...) { CPPUNIT_ASSERT( false ); } try { locale loc("yasli_language"); CPPUNIT_ASSERT( false ); } catch (runtime_error const&) { } catch (...) { CPPUNIT_ASSERT( false ); }# endif}void LocaleTest::loc_has_facet() { locale loc("C"); typedef numpunct<char> implemented_facet; CPPUNIT_ASSERT( has_facet<implemented_facet>(loc) ); /* typedef num_put<char, back_insert_iterator<string> > not_implemented_facet; CPPUNIT_ASSERT( !has_facet<not_implemented_facet>(loc) ); */}void LocaleTest::num_put_get(){ test_supported_locale(*this, &LocaleTest::_num_put_get); }void LocaleTest::money_put_get(){ test_supported_locale(*this, &LocaleTest::_money_put_get); }void LocaleTest::money_put_X_bug(){ test_supported_locale(*this, &LocaleTest::_money_put_X_bug); }void LocaleTest::time_put_get(){ test_supported_locale(*this, &LocaleTest::_time_put_get); }void LocaleTest::collate_facet(){ { CPPUNIT_ASSERT( has_facet<collate<char> >(locale::classic()) ); collate<char> const& col = use_facet<collate<char> >(locale::classic()); char const str1[] = "abcdef1"; char const str2[] = "abcdef2"; const size_t size1 = sizeof(str1) / sizeof(str1[0]) - 1; const size_t size2 = sizeof(str2) / sizeof(str2[0]) - 1; CPPUNIT_ASSERT( col.compare(str1, str1 + size1 - 1, str2, str2 + size2 - 1) == 0 ); CPPUNIT_ASSERT( col.compare(str1, str1 + size1, str2, str2 + size2) == -1 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -