vbox的更動 31222 路徑 trunk/src/VBox/Runtime/testcase
- 時間撮記:
- 2010-7-29 下午04:04:21 (14 年 以前)
- 檔案:
-
- 修改 1 筆資料
圖例:
- 未更動
- 新增
- 刪除
-
trunk/src/VBox/Runtime/testcase/tstUtf8.cpp
r30859 r31222 1094 1094 1095 1095 1096 void testLatin1(RTTEST hTest) 1097 { 1098 RTTestSub(hTest, "Latin1 conversion functions"); 1096 void testUtf8Latin1(RTTEST hTest) 1097 { 1098 RTTestSub(hTest, "Latin-1 <-> Utf-8 conversion functions"); 1099 1100 /* Test Utf8 -> Latin1 */ 1101 size_t cch_szAll = 0; 1102 size_t cbShort = RTStrCalcLatin1Len(g_szAll); 1103 RTTEST_CHECK(hTest, cbShort == 0); 1104 int rc = RTStrCalcLatin1LenEx(g_szAll, 383, &cch_szAll); 1105 RTTEST_CHECK(hTest, (cch_szAll == 255)); 1106 rc = RTStrCalcLatin1LenEx(g_szAll, RTSTR_MAX, &cch_szAll); 1107 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION); 1108 char *psz = NULL; 1109 char szShort[256] = { 0 }; 1110 memcpy(szShort, g_szAll, 255); 1111 cbShort = RTStrCalcLatin1Len(szShort); 1112 RTTEST_CHECK(hTest, cbShort == 191); 1113 rc = RTStrToLatin1(szShort, &psz); 1114 RTTEST_CHECK_RC_OK(hTest, rc); 1115 if (RT_SUCCESS(rc)) 1116 { 1117 RTTEST_CHECK(hTest, (strlen(psz) == 191)); 1118 for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j) 1119 if (psz[i] != (char) j) 1120 { 1121 RTTestFailed(hTest, "conversion of g_szAll to Latin1 failed at position %u\n", i); 1122 break; 1123 } 1124 } 1125 RTStrFree(psz); 1126 rc = RTStrToLatin1(g_szAll, &psz); 1127 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION); 1128 char sz[512]; 1129 char *psz2 = &sz[0]; 1130 size_t cchActual = 0; 1131 rc = RTStrToLatin1Ex(g_szAll, sizeof(sz) - 1, &psz2, sizeof(sz), 1132 &cchActual); 1133 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION); 1134 RTTEST_CHECK_MSG(hTest, cchActual == 0, 1135 (hTest, "cchActual=%lu\n", cchActual)); 1136 rc = RTStrToLatin1Ex(g_szAll, 383, &psz2, sizeof(sz), 1137 &cchActual); 1138 RTTEST_CHECK_RC_OK(hTest, rc); 1139 if (RT_SUCCESS(rc)) 1140 { 1141 RTTEST_CHECK(hTest, (cchActual == 255)); 1142 RTTEST_CHECK(hTest, (cchActual == strlen(sz))); 1143 for (unsigned i = 0, j = 1; psz2[i] != '\0'; ++i, ++j) 1144 if (psz2[i] != (char) j) 1145 { 1146 RTTestFailed(hTest, "second conversion of g_szAll to Latin1 failed at position %u\n", i); 1147 break; 1148 } 1149 } 1150 rc = RTStrToLatin1Ex(g_szAll, 129, &psz2, 128, &cchActual); 1151 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW); 1152 RTTEST_CHECK_MSG(hTest, cchActual == 128, 1153 (hTest, "cchActual=%lu\n", cchActual)); 1154 rc = RTStrToLatin1Ex(g_szAll, 383, &psz, 0, &cchActual); 1155 RTTEST_CHECK_RC_OK(hTest, rc); 1156 if (RT_SUCCESS(rc)) 1157 { 1158 RTTEST_CHECK(hTest, (cchActual == 255)); 1159 RTTEST_CHECK(hTest, (cchActual == strlen(psz))); 1160 for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j) 1161 if ( ((j < 0x100) && (psz[i] != (char) j)) 1162 || ((j > 0xff) && psz[i] != '?')) 1163 { 1164 RTTestFailed(hTest, "third conversion of g_szAll to Latin1 failed at position %u\n", i); 1165 break; 1166 } 1167 } 1168 const char *pszBad = "Hello\xDC\xD8"; 1169 rc = RTStrToLatin1Ex(pszBad, RTSTR_MAX, &psz2, sizeof(sz), 1170 &cchActual); 1171 RTTEST_CHECK_RC(hTest, rc, VERR_INVALID_UTF8_ENCODING); 1172 RTStrFree(psz); 1173 1174 /* Test Latin1 -> Utf8 */ 1175 const char *pszLat1 = "\x01\x20\x40\x80\x81"; 1176 RTTEST_CHECK(hTest, RTLatin1CalcUtf8Len(pszLat1) == 7); 1177 rc = RTLatin1CalcUtf8LenEx(pszLat1, 3, &cchActual); 1178 RTTEST_CHECK_RC_OK(hTest, rc); 1179 if (RT_SUCCESS(rc)) 1180 RTTEST_CHECK(hTest, cchActual == 3); 1181 rc = RTLatin1CalcUtf8LenEx(pszLat1, RTSTR_MAX, &cchActual); 1182 RTTEST_CHECK_RC_OK(hTest, rc); 1183 if (RT_SUCCESS(rc)) 1184 RTTEST_CHECK(hTest, cchActual == 7); 1185 char *pch = NULL; 1186 char ch[8]; 1187 char *pch2 = &ch[0]; 1188 cchActual = 0; 1189 rc = RTLatin1ToUtf8(pszLat1, &pch); 1190 RTTEST_CHECK_RC_OK(hTest, rc); 1191 if (RT_SUCCESS(rc)) 1192 RTTEST_CHECK(hTest, !strcmp(pch, "\x01\x20\x40\xC2\x80\xC2\x81")); 1193 RTStrFree(pch); 1194 rc = RTLatin1ToUtf8Ex(pszLat1, RTSTR_MAX, &pch, 0, &cchActual); 1195 RTTEST_CHECK_RC_OK(hTest, rc); 1196 if (RT_SUCCESS(rc)) 1197 { 1198 RTTEST_CHECK(hTest, (cchActual == 7)); 1199 RTTEST_CHECK(hTest, !strcmp(pch, "\x01\x20\x40\xC2\x80\xC2\x81")); 1200 } 1201 RTStrFree(pch); 1202 rc = RTLatin1ToUtf8Ex(pszLat1, RTSTR_MAX, &pch, 0, NULL); 1203 RTTEST_CHECK_RC_OK(hTest, rc); 1204 if (RT_SUCCESS(rc)) 1205 RTTEST_CHECK(hTest, !strcmp(pch, "\x01\x20\x40\xC2\x80\xC2\x81")); 1206 RTStrFree(pch); 1207 rc = RTLatin1ToUtf8Ex(pszLat1, RTSTR_MAX, &pch2, RT_ELEMENTS(ch), 1208 &cchActual); 1209 RTTEST_CHECK_RC_OK(hTest, rc); 1210 if (RT_SUCCESS(rc)) 1211 { 1212 RTTEST_CHECK(hTest, (cchActual == 7)); 1213 RTTEST_CHECK(hTest, !strcmp(pch2, "\x01\x20\x40\xC2\x80\xC2\x81")); 1214 } 1215 rc = RTLatin1ToUtf8Ex(pszLat1, 3, &pch2, RT_ELEMENTS(ch), 1216 &cchActual); 1217 RTTEST_CHECK_RC_OK(hTest, rc); 1218 if (RT_SUCCESS(rc)) 1219 { 1220 RTTEST_CHECK(hTest, (cchActual == 3)); 1221 RTTEST_CHECK(hTest, !strcmp(pch2, "\x01\x20\x40")); 1222 } 1223 rc = RTLatin1ToUtf8Ex(pszLat1, RTSTR_MAX, &pch2, RT_ELEMENTS(ch) - 1, 1224 &cchActual); 1225 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW); 1226 RTTEST_CHECK(hTest, (cchActual == 7)); 1227 RTTestSubDone(hTest); 1228 } 1229 1230 1231 void testUtf16Latin1(RTTEST hTest) 1232 { 1233 RTTestSub(hTest, "Latin-1 <-> Utf-16 conversion functions"); 1099 1234 1100 1235 /* Test Utf16 -> Latin1 */ … … 1211 1346 && (pwc[2] == 0x40) && (pwc[3] == 0x80) 1212 1347 && (pwc[4] == 0x81) && (pwc[5] == '\0')); 1348 RTUtf16Free(pwc); 1213 1349 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc2, RT_ELEMENTS(wc), 1214 1350 &cwActual); … … 1233 1369 &cwActual); 1234 1370 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW); 1235 /** @todo Either fix the documentation or fix the code - cchActual is1236 * set to the number of bytes actually encoded. */1237 1371 RTTEST_CHECK(hTest, (cwActual == 5)); 1238 1372 RTTestSubDone(hTest); … … 1299 1433 testStrStr(hTest); 1300 1434 testMinistring(hTest); 1301 testLatin1(hTest); 1435 testUtf8Latin1(hTest); 1436 testUtf16Latin1(hTest); 1302 1437 testNoTransation(hTest); 1303 1438
注意:
瀏覽 TracChangeset
來幫助您使用更動檢視器