HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Lollipop MR1
|
5.1.0_r3
下载
查看原文件
收藏
根目录
ndk
tests
device
test-stlport
unit
collate_facets_test.cpp
#include "locale_test.h" #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS) # include
# include
# include
# include
# if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) using namespace std; # endif // // tests implementation // void LocaleTest::collate_facet() { { CPPUNIT_ASSERT( has_facet
>(locale::classic()) ); collate
const& col = use_facet
>(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 ); //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
>(loc) ); collate
const& col = use_facet
>(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
>(loc) ); collate
const& col = use_facet
>(loc); string strs[] = {"abdd", "abd", "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] == "abd" ); 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
>(loc) ); collate
const& col = use_facet
>(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
>(loc) ); collate
const& col = use_facet
>(loc); // Here we would like to use L"abd" 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::collate_by_name() { /* * 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. */ # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) # if defined (STLPORT) || !defined (__GNUC__) try { locale loc(locale::classic(), new collate_byname
(static_cast
(0))); CPPUNIT_FAIL; } catch (runtime_error const& /* e */) { //CPPUNIT_MESSAGE( e.what() ); } catch (...) { CPPUNIT_FAIL; } # endif try { locale loc(locale::classic(), new collate_byname
("yasli_language")); CPPUNIT_FAIL; } catch (runtime_error const& /* e */) { //CPPUNIT_MESSAGE( e.what() ); } catch (...) { CPPUNIT_FAIL; } try { string veryLongFacetName("LC_COLLATE="); veryLongFacetName.append(512, '?'); locale loc(locale::classic(), new collate_byname
(veryLongFacetName.c_str())); CPPUNIT_FAIL; } catch (runtime_error const& /* e */) { //CPPUNIT_MESSAGE( e.what() ); } catch (...) { CPPUNIT_FAIL; } try { locale loc(locale::classic(), "C", locale::collate); } catch (runtime_error const& e) { CPPUNIT_MESSAGE( e.what() ); CPPUNIT_FAIL; } catch (...) { CPPUNIT_FAIL; } try { // On platform without real localization support we should rely on the "C" facet. locale loc(locale::classic(), "", locale::collate); } catch (runtime_error const& e) { CPPUNIT_MESSAGE( e.what() ); CPPUNIT_FAIL; } catch (...) { CPPUNIT_FAIL; } try { locale loc(locale::classic(), new collate_byname
("C")); //We check that the C locale gives a lexicographical comparison: collate
const& cfacet_byname = use_facet
>(loc); collate
const& cfacet = use_facet
>(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( cfacet_byname.compare(str1, str1 + size1 - 1, str2, str2 + size2 - 1) == cfacet.compare(str1, str1 + size1 - 1, str2, str2 + size2 - 1) ); CPPUNIT_ASSERT( cfacet_byname.compare(str1, str1 + size1, str2, str2 + size2) == cfacet.compare(str1, str1 + size1, str2, str2 + size2) ); //Smallest string should be before largest one: CPPUNIT_ASSERT( cfacet_byname.compare(str1, str1 + size1 - 2, str2, str2 + size2 - 1) == cfacet.compare(str1, str1 + size1 - 2, str2, str2 + size2 - 1) ); CPPUNIT_ASSERT( cfacet_byname.compare(str1, str1 + size1 - 1, str2, str2 + size2 - 2) == cfacet.compare(str1, str1 + size1 - 1, str2, str2 + size2 - 2) ); // We cannot play with '' char here because doing so would make test result // dependant on char being consider as signed or not... string strs[] = {"abdd", /* "abd",*/ "abbd", "abcd"}; vector
v1(strs, strs + sizeof(strs) / sizeof(strs[0])); sort(v1.begin(), v1.end(), loc); vector
v2(strs, strs + sizeof(strs) / sizeof(strs[0])); sort(v2.begin(), v2.end(), locale::classic()); CPPUNIT_ASSERT( v1 == v2 ); CPPUNIT_ASSERT( (cfacet_byname.transform(v1[0].data(), v1[0].data() + v1[0].size()).compare(cfacet_byname.transform(v1[1].data(), v1[1].data() + v1[1].size())) == v1[0].compare(v1[1])) ); } catch (runtime_error const& /* e */) { /* CPPUNIT_MESSAGE( e.what() ); */ CPPUNIT_FAIL; } catch (...) { CPPUNIT_FAIL; } # if !defined (STLPORT) || !defined (_STLP_NO_WCHAR_T) # if defined (STLPORT) || !defined (__GNUC__) try { locale loc(locale::classic(), new collate_byname
(static_cast
(0))); CPPUNIT_FAIL; } catch (runtime_error const&) { } catch (...) { CPPUNIT_FAIL; } # endif try { locale loc(locale::classic(), new collate_byname
("yasli_language")); CPPUNIT_FAIL; } catch (runtime_error const&) { } catch (...) { CPPUNIT_FAIL; } # endif # endif } #endif
C++程序
|
280行
|
9.69 KB
原始内容
高亮显示
复制内容
×
已收藏
收藏成功,您可以在我收藏的代码页面中查看,其地址为:
https://www.androidos.net.cn/my/collect/code
。
登录后可以享受更多权益
您还没有登录,登录后您可以:
收藏Android系统代码
收藏喜欢的文章
多个平台共享账号
去登录
首次使用?从这里
注册