📄 locale_test.cpp
字号:
//Smallest string should be before largest one: CPPUNIT_ASSERT( col.compare(str1, str1 + size1 - 2, str2, str2 + size2 - 1) == -1 ); CPPUNIT_ASSERT( col.compare(str1, str1 + size1 - 1, str2, str2 + size2 - 2) == 1 ); }# if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) try { locale loc("fr_FR"); { CPPUNIT_ASSERT( has_facet<collate<char> >(loc) ); collate<char> const& col = use_facet<collate<char> >(loc); 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 ); //Smallest string should be before largest one: CPPUNIT_ASSERT( col.compare(str1, str1 + size1 - 2, str2, str2 + size2 - 1) == -1 ); CPPUNIT_ASSERT( col.compare(str1, str1 + size1 - 1, str2, str2 + size2 - 2) == 1 ); } { CPPUNIT_ASSERT( has_facet<collate<char> >(loc) ); collate<char> const& col = use_facet<collate<char> >(loc); string strs[] = {"abdd", "ab鏳", "abbd", "abcd"}; string transformed[4]; for (size_t i = 0; i < 4; ++i) { transformed[i] = col.transform(strs[i].data(), strs[i].data() + strs[i].size()); } sort(strs, strs + 4, loc); CPPUNIT_ASSERT( strs[0] == "abbd" ); CPPUNIT_ASSERT( strs[1] == "abcd" ); CPPUNIT_ASSERT( strs[2] == "ab鏳" ); CPPUNIT_ASSERT( strs[3] == "abdd" ); sort(transformed, transformed + 4); CPPUNIT_ASSERT( col.transform(strs[0].data(), strs[0].data() + strs[0].size()) == transformed[0] ); CPPUNIT_ASSERT( col.transform(strs[1].data(), strs[1].data() + strs[1].size()) == transformed[1] ); CPPUNIT_ASSERT( col.transform(strs[2].data(), strs[2].data() + strs[2].size()) == transformed[2] ); CPPUNIT_ASSERT( col.transform(strs[3].data(), strs[3].data() + strs[3].size()) == transformed[3] ); // Check empty string result in empty key. CPPUNIT_ASSERT( col.transform(strs[0].data(), strs[0].data()).empty() ); // Check that only characters that matter are taken into accout to build the key. CPPUNIT_ASSERT( col.transform(strs[0].data(), strs[0].data() + 2) == col.transform(strs[1].data(), strs[1].data() + 2) ); }# if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T) { CPPUNIT_ASSERT( has_facet<collate<wchar_t> >(loc) ); collate<wchar_t> const& col = use_facet<collate<wchar_t> >(loc); wchar_t const str1[] = L"abcdef1"; wchar_t const str2[] = L"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 ); //Smallest string should be before largest one: CPPUNIT_ASSERT( col.compare(str1, str1 + size1 - 2, str2, str2 + size2 - 1) == -1 ); CPPUNIT_ASSERT( col.compare(str1, str1 + size1 - 1, str2, str2 + size2 - 2) == 1 ); } { size_t i; CPPUNIT_ASSERT( has_facet<collate<wchar_t> >(loc) ); collate<wchar_t> const& col = use_facet<collate<wchar_t> >(loc); // Here we would like to use L"ab鏳" but it looks like all compilers // do not support storage of unicode characters in exe resulting in // compilation error. We avoid this test for the moment. wstring strs[] = {L"abdd", L"abcd", L"abbd", L"abcd"}; wstring transformed[4]; for (i = 0; i < 4; ++i) { transformed[i] = col.transform(strs[i].data(), strs[i].data() + strs[i].size()); } sort(strs, strs + 4, loc); CPPUNIT_ASSERT( strs[0] == L"abbd" ); CPPUNIT_ASSERT( strs[1] == L"abcd" ); CPPUNIT_ASSERT( strs[2] == L"abcd" ); CPPUNIT_ASSERT( strs[3] == L"abdd" ); sort(transformed, transformed + 4); CPPUNIT_ASSERT( col.transform(strs[0].data(), strs[0].data() + strs[0].size()) == transformed[0] ); CPPUNIT_ASSERT( col.transform(strs[1].data(), strs[1].data() + strs[1].size()) == transformed[1] ); CPPUNIT_ASSERT( col.transform(strs[2].data(), strs[2].data() + strs[2].size()) == transformed[2] ); CPPUNIT_ASSERT( col.transform(strs[3].data(), strs[3].data() + strs[3].size()) == transformed[3] ); CPPUNIT_ASSERT( col.transform(strs[0].data(), strs[0].data()).empty() ); CPPUNIT_ASSERT( col.transform(strs[0].data(), strs[0].data() + 2) == col.transform(strs[1].data(), strs[1].data() + 2) ); }# endif } catch (runtime_error const&) { CPPUNIT_MESSAGE("No french locale to check collate facet"); }# endif}void LocaleTest::ctype_facet(){ test_supported_locale(*this, &LocaleTest::_ctype_facet); }void LocaleTest::locale_init_problem() {# if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES) test_supported_locale(*this, &LocaleTest::_locale_init_problem);# endif}/* * Creation of a locale instance imply initialization of some STLport internal * static objects first. We use a static instance of locale to check that this * initialization is done correctly. */static locale global_loc;static locale other_loc("");# if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES)void LocaleTest::_locale_init_problem( const locale& loc, const ref_locale&){# if !defined (__APPLE__) && !defined (__FreeBSD__) || \ !defined(__GNUC__) || ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__> 3))) typedef codecvt<char,char,mbstate_t> my_facet;# else// std::mbstate_t required for gcc 3.3.2 on FreeBSD...// I am not sure what key here---FreeBSD or 3.3.2...// - ptr 2005-04-04 typedef codecvt<char,char,std::mbstate_t> my_facet;# endif# if !(defined (__DMC__) && defined (_DLL)) locale loc_ref(global_loc); { locale gloc( loc_ref, _CHECK_PTR(new my_facet()) ); CPPUNIT_ASSERT( has_facet<my_facet>( gloc ) ); //The following code is just here to try to confuse the reference counting underlying mecanism: locale::global( locale::classic() ); locale::global( gloc ); }# if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) try {# endif ostringstream os("test") ; locale loc2( loc, _CHECK_PTR(new my_facet()) ); CPPUNIT_ASSERT( has_facet<my_facet>( loc2 ) ); os.imbue( loc2 );# if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) } catch ( runtime_error& ) { CPPUNIT_ASSERT( false ); } catch ( ... ) { CPPUNIT_ASSERT( false ); }# endif# if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) try {# endif ostringstream os2("test2");# if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) } catch ( runtime_error& ) { CPPUNIT_ASSERT( false ); } catch ( ... ) { CPPUNIT_ASSERT( false ); }# endif# endif /* __DMC__ */}# endifvoid LocaleTest::default_locale(){ locale loc( "" );}void LocaleTest::facet_id(){# if defined (STLPORT) locale::id _id_01 = collate<char>::id; CPPUNIT_CHECK( _id_01._M_index == 1 ); locale::id _id_02 = ctype<char>::id; CPPUNIT_CHECK( _id_02._M_index == 2 );# ifndef _STLP_NO_MBSTATE_T locale::id _id_03 = codecvt<char, char, mbstate_t>::id; CPPUNIT_CHECK( _id_03._M_index == 3 );# endif locale::id _id_04 = moneypunct<char, true>::id; CPPUNIT_CHECK( _id_04._M_index == 4 ); locale::id _id_05 = moneypunct<char, false>::id; CPPUNIT_CHECK( _id_05._M_index == 5 ); locale::id _id_06 = numpunct<char>::id; CPPUNIT_CHECK( _id_06._M_index == 6 ); locale::id _id_07 = messages<char>::id; CPPUNIT_CHECK( _id_07._M_index == 7 ); locale::id _id_08 = money_get<char, istreambuf_iterator<char, char_traits<char> > >::id; CPPUNIT_CHECK( _id_08._M_index == 8 ); /* locale::id _id_09 = money_get<char, const char*>::id; CPPUNIT_CHECK( _id_09._M_index == 9 ); */ locale::id _id_10 = money_put<char, ostreambuf_iterator<char, char_traits<char> > >::id; CPPUNIT_CHECK( _id_10._M_index == 10 ); /* locale::id _id_11 = money_put<char, char*>::id; CPPUNIT_CHECK( _id_11._M_index == 11 ); */ locale::id _id_12 = num_get<char, istreambuf_iterator<char, char_traits<char> > >::id; CPPUNIT_CHECK( _id_12._M_index == 12 ); /* locale::id _id_13 = num_get<char, const char*>::id; CPPUNIT_CHECK( _id_13._M_index == 13 ); */ locale::id _id_14 = num_put<char, ostreambuf_iterator<char, char_traits<char> > >::id; CPPUNIT_CHECK( _id_14._M_index == 14 ); /* locale::id _id_15 = num_put<char, char*>::id; CPPUNIT_CHECK( _id_15._M_index == 15 ); */ locale::id _id_16 = time_get<char, istreambuf_iterator<char, char_traits<char> > >::id; CPPUNIT_CHECK( _id_16._M_index == 16 ); /* locale::id _id_17 = time_get<char, const char*>::id; CPPUNIT_CHECK( _id_17._M_index == 17 ); */ locale::id _id_18 = time_put<char, ostreambuf_iterator<char, char_traits<char> > >::id; CPPUNIT_CHECK( _id_18._M_index == 18 ); /* locale::id _id_19 = time_put<char, char*>::id; CPPUNIT_CHECK( _id_19._M_index == 19 ); */# ifndef _STLP_NO_WCHAR_T locale::id _id_20 = collate<wchar_t>::id; CPPUNIT_CHECK( _id_20._M_index == 20 ); locale::id _id_21 = ctype<wchar_t>::id; CPPUNIT_CHECK( _id_21._M_index == 21 );# ifndef _STLP_NO_MBSTATE_T locale::id _id_22 = codecvt<wchar_t, char, mbstate_t>::id; CPPUNIT_CHECK( _id_22._M_index == 22 );# endif locale::id _id_23 = moneypunct<wchar_t, true>::id; CPPUNIT_CHECK( _id_23._M_index == 23 ); locale::id _id_24 = moneypunct<wchar_t, false>::id; CPPUNIT_CHECK( _id_24._M_index == 24 ); locale::id _id_25 = numpunct<wchar_t>::id; CPPUNIT_CHECK( _id_25._M_index == 25 ); locale::id _id_26 = messages<wchar_t>::id; CPPUNIT_CHECK( _id_26._M_index == 26 ); locale::id _id_27 = money_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id; CPPUNIT_CHECK( _id_27._M_index == 27 ); /* locale::id _id_28 = money_get<wchar_t, const wchar_t*>::id; CPPUNIT_CHECK( _id_28._M_index == 28 ); */ locale::id _id_29 = money_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id; CPPUNIT_CHECK( _id_29._M_index == 29 ); /* locale::id _id_30 = money_put<wchar_t, wchar_t*>::id; CPPUNIT_CHECK( _id_30._M_index == 30 ); */ locale::id _id_31 = num_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id; CPPUNIT_CHECK( _id_31._M_index == 31 ); /* locale::id _id_32 = num_get<wchar_t, const wchar_t*>::id; CPPUNIT_CHECK( _id_32._M_index == 32 ); */ locale::id _id_33 = num_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > > ::id; CPPUNIT_CHECK( _id_33._M_index == 33 ); /* locale::id _id_34 = num_put<wchar_t, wchar_t*>::id; CPPUNIT_CHECK( _id_34._M_index == 34 ); */ locale::id _id_35 = time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id; CPPUNIT_CHECK( _id_35._M_index == 35 ); /* locale::id _id_36 = time_get<wchar_t, const wchar_t*>::id; CPPUNIT_CHECK( _id_36._M_index == 36 ); */ locale::id _id_37 = time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id; CPPUNIT_CHECK( _id_37._M_index == 37 ); /* locale::id _id_38 = time_put<wchar_t, wchar_t*>::id; CPPUNIT_CHECK( _id_38._M_index == 38 ); */# endif# endif}void LocaleTest::combine(){# if (!defined (STLPORT) || \ (defined (_STLP_USE_EXCEPTIONS) && !defined (_STLP_NO_MEMBER_TEMPLATES) && !defined (_STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS))) auto_ptr<locale> loc1, loc2; size_t loc1_index = 0; size_t n = sizeof(tested_locales) / sizeof(tested_locales[0]); for (size_t i = 0; i < n; ++i) { try { locale *ploc = _CHECK_PTR(new locale(tested_locales[i].name)); if (loc1.get() == 0) { loc1.reset(ploc); loc1_index = i; continue; } else { loc2.reset(ploc); } //We can start the test ostringstream ostr; ostr << "combining '" << loc2->name() << "' money facets with '" << loc1->name() << "'"; CPPUNIT_MESSAGE( ostr.str().c_str() ); //We are going to combine money facets as all formats are different. { //We check that resulting locale has correctly acquire loc2 facets. locale loc = loc1->combine<moneypunct<char, true> >(*loc2); loc = loc.combine<moneypunct<char, false> >(*loc2); loc = loc.combine<money_put<char> >(*loc2); loc = loc.combine<money_get<char> >(*loc2); //Check loc has the correct facets: _money_put_get2(*loc2, loc, tested_locales[i]); //Check loc1 has not been impacted: _money_put_get2(*loc1, *loc1, tested_locales[loc1_index]); //Check loc2 has not been impacted: _money_put_get2(*loc2, *loc2, tested_locales[i]); } { //We check that resulting locale has not wrongly acquire loc1 facets that hasn't been combine: locale loc = loc2->combine<numpunct<char> >(*loc1); loc = loc.combine<time_put<char> >(*loc1); loc = loc.combine<time_get<char> >(*loc1); //Check loc has the correct facets: _money_put_get2(*loc2, loc, tested_locales[i]); //Check loc1 has not been impacted: _money_put_get2(*loc1, *loc1, tested_locales[loc1_index]); //Check loc2 has not been impacted: _money_put_get2(*loc2, *loc2, tested_locales[i]); } { // Check auto combination do not result in weird reference counting behavior // (might generate a crash). loc1->combine<numpunct<char> >(*loc1); } loc1.reset(loc2.release()); loc1_index = i; } catch (runtime_error const&) { //This locale is not supported. continue; } }# endif}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -