Changeset 18571 in vbox
- Timestamp:
- Mar 31, 2009 1:08:16 PM (16 years ago)
- svn:sync-xref-src-repo-rev:
- 45465
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/testcase/tstUtf8.cpp
r14831 r18571 41 41 #include <iprt/assert.h> 42 42 #include <iprt/err.h> 43 #include <iprt/test.h> 43 44 44 45 #include <stdlib.h> 45 46 46 47 /*******************************************************************************48 * Global Variables *49 *******************************************************************************/50 static int g_cErrors = 0;51 47 52 48 … … 68 64 * 69 65 */ 70 static void test1( void)66 static void test1(RTTEST hTest) 71 67 { 72 68 static const char s_szBadString1[] = "Bad \xe0\x13\x0"; … … 78 74 PRTUTF16 pwszRand; 79 75 80 RTPrintf("tstUtf8: TEST 1\n");81 82 76 /* 83 77 * Invalid UTF-8 to UCS-2 test. 84 78 */ 79 RTTestSub(hTest, "Feeding bad UTF-8 to RTStrToUtf16"); 85 80 rc = RTStrToUtf16(s_szBadString1, &pwsz); 86 if (rc != VERR_NO_TRANSLATION && rc != VERR_INVALID_UTF8_ENCODING) 87 { 88 RTPrintf("tstUtf8: FAILURE - %d: Conversion of first bad UTF-8 string to UTF-16 apparantly succeeded. It shouldn't. rc=%Rrc\n", 89 __LINE__, rc); 90 g_cErrors++; 91 } 81 RTTEST_CHECK_MSG(hTest, rc == rc != VERR_NO_TRANSLATION || rc == VERR_INVALID_UTF8_ENCODING, 82 (hTest, RTTESTLVL_FAILURE, "Conversion of first bad UTF-8 string to UTF-16 apparantly succeeded. It shouldn't. rc=%Rrc\n", rc)); 92 83 rc = RTStrToUtf16(s_szBadString2, &pwsz); 93 if (rc != VERR_NO_TRANSLATION && rc != VERR_INVALID_UTF8_ENCODING) 94 { 95 RTPrintf("tstUtf8: FAILURE - %d: Conversion of second bad UTF-8 strings to UTF-16 apparantly succeeded. It shouldn't. rc=%Rrc\n", 96 __LINE__, rc); 97 g_cErrors++; 98 } 84 RTTEST_CHECK_MSG(hTest, rc == rc != VERR_NO_TRANSLATION || rc == VERR_INVALID_UTF8_ENCODING, 85 (hTest, RTTESTLVL_FAILURE, "Conversion of second bad UTF-8 strings to UTF-16 apparantly succeeded. It shouldn't. rc=%Rrc\n", rc)); 99 86 100 87 /* 101 88 * Test current CP convertion. 102 89 */ 90 RTTestSub(hTest, "Rand UTF-16 -> UTF-8 -> CP -> UTF-8"); 103 91 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz)); 104 92 srand((unsigned)RTTimeNanoTS()); … … 115 103 rc = RTStrCurrentCPToUtf8(&pszUtf8, pszCurrent); 116 104 if (rc == VINF_SUCCESS) 117 RT Printf("tstUtf8:Random UTF-16 -> UTF-8 -> Current -> UTF-8 successful.\n");105 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> Current -> UTF-8 successful.\n"); 118 106 else 119 { 120 RTPrintf("tstUtf8: FAILURE - %d: The third part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.\n", 107 RTTestFailed(hTest, "%d: The third part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.", 108 __LINE__, rc); 109 } 110 else if (rc == VERR_NO_TRANSLATION) 111 RTTestPassed(hTest, "The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 returned VERR_NO_TRANSLATION. This is probably as it should be.\n"); 112 else 113 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.", 121 114 __LINE__, rc); 122 g_cErrors++; 123 } 124 } 125 else if (rc == VERR_NO_TRANSLATION) 126 RTPrintf("tstUtf8: The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 returned VERR_NO_TRANSLATION. This is probably as it should be.\n"); 127 else 128 { 129 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.\n", 115 } 116 else 117 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.", 130 118 __LINE__, rc); 131 g_cErrors++;132 }133 }134 else135 {136 RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.\n",137 __LINE__, rc);138 g_cErrors++;139 }140 119 141 120 /* 142 121 * Generate a new random string. 143 122 */ 123 RTTestSub(hTest, "Random UTF-16 -> UTF-8 -> UTF-16"); 144 124 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz)); 145 125 srand((unsigned)RTTimeNanoTS()); … … 157 137 /* nothing */; 158 138 if (pwszRand[i] == pwsz[i] && pwsz[i] == 0) 159 RT Printf("tstUtf8:Random UTF-16 -> UTF-8 -> UTF-16 successful.\n");139 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> UTF-16 successful.\n"); 160 140 else 161 141 { 162 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed.\n", __LINE__); 163 RTPrintf("tstUtf8: First differing character is at position %d and has the value %x.\n", i, pwsz[i]); 164 g_cErrors++; 142 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed.", __LINE__); 143 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz[i]); 165 144 } 166 145 } 167 146 else 168 { 169 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Rrc.\n", 147 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Rrc.", 148 __LINE__, rc); 149 } 150 else 151 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Rrc.", 170 152 __LINE__, rc); 171 g_cErrors++;172 }173 }174 else175 {176 RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Rrc.\n",177 __LINE__, rc);178 g_cErrors++;179 }180 153 181 154 /* 182 155 * Generate yet another random string and convert it to a buffer. 183 156 */ 157 RTTestSub(hTest, "Random RTUtf16ToUtf8Ex + RTStrToUtf16"); 184 158 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz)); 185 159 srand((unsigned)RTTimeNanoTS()); … … 200 174 ; 201 175 if (pwsz[i] == 0 && i >= 8) 202 RT Printf("tstUtf8:Random UTF-16 -> fixed length UTF-8 -> UTF-16 successful.\n");176 RTTestPassed(hTest, "Random UTF-16 -> fixed length UTF-8 -> UTF-16 successful.\n"); 203 177 else 204 178 { 205 RTPrintf("tstUtf8: FAILURE - %d: Incorrect conversion of UTF-16 -> fixed length UTF-8 -> UTF-16.\n", __LINE__); 206 RTPrintf("tstUtf8: First differing character is at position %d and has the value %x.\n", i, pwsz[i]); 207 g_cErrors++; 179 RTTestFailed(hTest, "%d: Incorrect conversion of UTF-16 -> fixed length UTF-8 -> UTF-16.\n", __LINE__); 180 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz[i]); 208 181 } 209 182 } 210 183 else 211 { 212 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", 213 __LINE__, rc); 214 g_cErrors++; 215 } 184 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", __LINE__, rc); 216 185 } 217 186 else 218 { 219 RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", 220 __LINE__, rc); 221 g_cErrors++; 222 } 187 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", __LINE__, rc); 223 188 224 189 /* 225 190 * And again. 226 191 */ 192 RTTestSub(hTest, "Random RTUtf16ToUtf8 + RTStrToUtf16Ex"); 227 193 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz)); 228 194 srand((unsigned)RTTimeNanoTS()); … … 243 209 ; 244 210 if (pwszRand[i] == 0 && pwsz2Buf[i] == 0) 245 RT Printf("tstUtf8:Random UTF-16 -> UTF-8 -> fixed length UTF-16 successful.\n");211 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> fixed length UTF-16 successful.\n"); 246 212 else 247 213 { 248 RTPrintf("tstUtf8: FAILURE - %d: Incorrect conversion of random UTF-16 -> UTF-8 -> fixed length UTF-16.\n", __LINE__); 249 RTPrintf("tstUtf8: First differing character is at position %d and has the value %x.\n", i, pwsz2Buf[i]); 250 g_cErrors++; 214 RTTestFailed(hTest, "%d: Incorrect conversion of random UTF-16 -> UTF-8 -> fixed length UTF-16.\n", __LINE__); 215 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz2Buf[i]); 251 216 } 252 217 } 253 218 else 254 { 255 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n", 219 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n", __LINE__, rc); 220 } 221 else 222 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n", 256 223 __LINE__, rc); 257 g_cErrors++;258 }259 }260 else261 {262 RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",263 __LINE__, rc);264 g_cErrors++;265 }266 224 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz)); 267 225 srand((unsigned)RTTimeNanoTS()); … … 272 230 rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 20, NULL); 273 231 if (rc == VERR_BUFFER_OVERFLOW) 274 RT Printf("tstUtf8:Random UTF-16 -> fixed length UTF-8 with too short buffer successfully rejected.\n");232 RTTestPassed(hTest, "Random UTF-16 -> fixed length UTF-8 with too short buffer successfully rejected.\n"); 275 233 else 276 { 277 RTPrintf("tstUtf8: FAILURE - %d: Random UTF-16 -> fixed length UTF-8 with too small buffer returned value %d instead of VERR_BUFFER_OVERFLOW.\n", 278 __LINE__, rc); 279 g_cErrors++; 280 } 234 RTTestFailed(hTest, "%d: Random UTF-16 -> fixed length UTF-8 with too small buffer returned value %d instead of VERR_BUFFER_OVERFLOW.\n", 235 __LINE__, rc); 281 236 282 237 /* 283 238 * last time... 284 239 */ 240 RTTestSub(hTest, "Random RTUtf16ToUtf8 + RTStrToUtf16Ex"); 285 241 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz)); 286 242 srand((unsigned)RTTimeNanoTS()); … … 294 250 rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 20, NULL); 295 251 if (rc == VERR_BUFFER_OVERFLOW) 296 RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer successfully rejected.\n"); 297 else 298 { 299 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer returned value %Rrc instead of VERR_BUFFER_OVERFLOW.\n", 252 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer successfully rejected.\n"); 253 else 254 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer returned value %Rrc instead of VERR_BUFFER_OVERFLOW.\n", 255 __LINE__, rc); 256 } 257 else 258 RTTestFailed(hTest, "%d:The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n", 300 259 __LINE__, rc); 301 g_cErrors++; 302 } 303 } 304 else 305 { 306 RTPrintf("tstUtf8: FAILURE - %d:The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n", 307 __LINE__, rc); 308 g_cErrors++; 309 } 310 260 261 262 RTTestSubDone(hTest); 311 263 } 312 264 … … 321 273 { 322 274 if (off < 0x7f) 323 RT Printf("UTF-8 U+%#x\n", off + 1);275 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", off + 1); 324 276 else if (off < 0xf7f) 325 RT Printf("UTF-8 U+%#x\n", (off - 0x7f) / 2 + 0x80);277 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x7f) / 2 + 0x80); 326 278 else if (off < 0x27f7f) 327 RT Printf("UTF-8 U+%#x\n", (off - 0xf7f) / 3 + 0x800);279 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0xf7f) / 3 + 0x800); 328 280 else if (off < 0x2df79) 329 RT Printf("UTF-8 U+%#x\n", (off - 0x27f7f) / 3 + 0xe000);281 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x27f7f) / 3 + 0xe000); 330 282 else if (off < 0x42df79) 331 RT Printf("UTF-8 U+%#x\n", (off - 0x2df79) / 4 + 0x10000);332 else 333 RT Printf("UTF-8 ???\n");283 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x2df79) / 4 + 0x10000); 284 else 285 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 ???\n"); 334 286 } 335 287 else if (cBits == 16) 336 288 { 337 289 if (off < 0xd7ff*2) 338 RT Printf("UTF-16 U+%#x\n", off / 2 + 1);290 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", off / 2 + 1); 339 291 else if (off < 0xf7fd*2) 340 RT Printf("UTF-16 U+%#x\n", (off - 0xd7ff*2) / 2 + 0xe000);292 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", (off - 0xd7ff*2) / 2 + 0xe000); 341 293 else if (off < 0x20f7fd) 342 RT Printf("UTF-16 U+%#x\n", (off - 0xf7fd*2) / 4 + 0x10000);343 else 344 RT Printf("UTF-16 ???\n");294 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", (off - 0xf7fd*2) / 4 + 0x10000); 295 else 296 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 ???\n"); 345 297 } 346 298 else 347 299 { 348 300 if (off < (0xd800 - 1) * sizeof(RTUNICP)) 349 RT Printf("RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 1);301 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 1); 350 302 else if (off < (0xfffe - 0x800 - 1) * sizeof(RTUNICP)) 351 RT Printf("RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1);352 else 353 RT Printf("RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1 + 2);303 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1); 304 else 305 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1 + 2); 354 306 } 355 307 } … … 363 315 if (pb1[off] != pb2[off]) 364 316 { 365 RT Printf("mismatch at %#x: ", off);317 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "mismatch at %#x: ", off); 366 318 whereami(cBits, off); 367 RT Printf(" %#x: %02x != %02x!\n", off-1, pb1[off-1], pb2[off-1]);368 RT Printf("*%#x: %02x != %02x!\n", off, pb1[off], pb2[off]);369 RT Printf(" %#x: %02x != %02x!\n", off+1, pb1[off+1], pb2[off+1]);370 RT Printf(" %#x: %02x != %02x!\n", off+2, pb1[off+2], pb2[off+2]);371 RT Printf(" %#x: %02x != %02x!\n", off+3, pb1[off+3], pb2[off+3]);372 RT Printf(" %#x: %02x != %02x!\n", off+4, pb1[off+4], pb2[off+4]);373 RT Printf(" %#x: %02x != %02x!\n", off+5, pb1[off+5], pb2[off+5]);374 RT Printf(" %#x: %02x != %02x!\n", off+6, pb1[off+6], pb2[off+6]);375 RT Printf(" %#x: %02x != %02x!\n", off+7, pb1[off+7], pb2[off+7]);376 RT Printf(" %#x: %02x != %02x!\n", off+8, pb1[off+8], pb2[off+8]);377 RT Printf(" %#x: %02x != %02x!\n", off+9, pb1[off+9], pb2[off+9]);319 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off-1, pb1[off-1], pb2[off-1]); 320 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "*%#x: %02x != %02x!\n", off, pb1[off], pb2[off]); 321 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+1, pb1[off+1], pb2[off+1]); 322 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+2, pb1[off+2], pb2[off+2]); 323 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+3, pb1[off+3], pb2[off+3]); 324 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+4, pb1[off+4], pb2[off+4]); 325 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+5, pb1[off+5], pb2[off+5]); 326 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+6, pb1[off+6], pb2[off+6]); 327 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+7, pb1[off+7], pb2[off+7]); 328 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+8, pb1[off+8], pb2[off+8]); 329 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+9, pb1[off+9], pb2[off+9]); 378 330 return 1; 379 331 } … … 383 335 384 336 385 void InitStrings( void)337 void InitStrings() 386 338 { 387 339 /* … … 476 428 477 429 478 void test2(void) 479 { 480 RTPrintf("tstUtf8: TEST 2\n"); 481 430 void test2(RTTEST hTest) 431 { 482 432 /* 483 433 * Convert to UTF-8 and back. 484 434 */ 485 RT Printf("tstUtf8: #1: UTF-16 -> UTF-8 -> UTF-16...\n");435 RTTestSub(hTest, "UTF-16 -> UTF-8 -> UTF-16"); 486 436 char *pszUtf8; 487 437 int rc = RTUtf16ToUtf8(&g_wszAll[0], &pszUtf8); … … 489 439 { 490 440 if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8)) 491 { 492 RTPrintf("tstUtf8: FAILURE - the full #1: UTF-16 -> UTF-8 mismatch!\n"); 493 g_cErrors++; 494 } 441 RTTestFailed(hTest, "UTF-16 -> UTF-8 mismatch!"); 495 442 496 443 PRTUTF16 pwszUtf16; … … 499 446 { 500 447 if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16)) 501 { 502 RTPrintf("tstUtf8: FAILURE - the full #1: UTF-8 -> UTF-16 failed compare!\n"); 503 g_cErrors++; 504 } 448 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed compare!"); 505 449 RTUtf16Free(pwszUtf16); 506 450 } 507 451 else 508 { 509 RTPrintf("tstUtf8: FAILURE - the full #1: UTF-8 -> UTF-16 failed, rc=%Rrc.\n", rc); 510 g_cErrors++; 511 } 452 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc); 512 453 RTStrFree(pszUtf8); 513 454 } 514 455 else 515 { 516 RTPrintf("tstUtf8: FAILURE - the full #1: UTF-16 -> UTF-8 failed, rc=%Rrc.\n", rc); 517 g_cErrors++; 518 } 456 RTTestFailed(hTest, "UTF-16 -> UTF-8 failed, rc=%Rrc.", rc); 519 457 520 458 … … 522 460 * Convert to UTF-16 and back. (just in case the above test fails) 523 461 */ 524 RT Printf("tstUtf8: #2: UTF-8 -> UTF-16 -> UTF-8...\n");462 RTTestSub(hTest, "UTF-8 -> UTF-16 -> UTF-8"); 525 463 PRTUTF16 pwszUtf16; 526 464 rc = RTStrToUtf16(&g_szAll[0], &pwszUtf16); … … 528 466 { 529 467 if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16)) 530 { 531 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-8 -> UTF-16 failed compare!\n"); 532 g_cErrors++; 533 } 468 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed compare!"); 534 469 535 470 char *pszUtf8; … … 538 473 { 539 474 if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8)) 540 { 541 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-16 -> UTF-8 failed compare!\n"); 542 g_cErrors++; 543 } 475 RTTestFailed(hTest, "UTF-16 -> UTF-8 failed compare!"); 544 476 RTStrFree(pszUtf8); 545 477 } 546 478 else 547 { 548 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-16 -> UTF-8 failed, rc=%Rrc.\n", rc); 549 g_cErrors++; 550 } 479 RTTestFailed(hTest, "UTF-16 -> UTF-8 failed, rc=%Rrc.", rc); 551 480 RTUtf16Free(pwszUtf16); 552 481 } 553 482 else 554 { 555 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-8 -> UTF-16 failed, rc=%Rrc.\n", rc); 556 g_cErrors++; 557 } 483 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc); 558 484 559 485 /* 560 486 * Convert UTF-8 to CPs. 561 487 */ 488 RTTestSub(hTest, "UTF-8 -> UNI -> UTF-8"); 562 489 PRTUNICP paCps; 563 490 rc = RTStrToUni(g_szAll, &paCps); … … 565 492 { 566 493 if (mymemcmp(paCps, g_uszAll, sizeof(g_uszAll), 32)) 567 { 568 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-8 -> UTF-16 failed, rc=%Rrc.\n", rc); 569 g_cErrors++; 570 } 494 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc); 571 495 572 496 size_t cCps; … … 575 499 { 576 500 if (cCps != RT_ELEMENTS(g_uszAll) - 1) 577 { 578 RTPrintf("tstUtf8: FAILURE - the full #3+: wrong Code Point count %zu, expected %zu\n", cCps, RT_ELEMENTS(g_uszAll) - 1); 579 g_cErrors++; 580 } 581 } 582 else 583 { 584 RTPrintf("tstUtf8: FAILURE - the full #3+: UTF-8 -> Code Points failed, rc=%Rrc.\n", rc); 585 g_cErrors++; 586 } 501 RTTestFailed(hTest, "wrong Code Point count %zu, expected %zu\n", cCps, RT_ELEMENTS(g_uszAll) - 1); 502 } 503 else 504 RTTestFailed(hTest, "UTF-8 -> Code Points failed, rc=%Rrc.\n", rc); 587 505 588 506 /** @todo RTCpsToUtf8 or something. */ 589 507 } 590 508 else 591 { 592 RTPrintf("tstUtf8: FAILURE - the full #3a: UTF-8 -> Code Points failed, rc=%Rrc.\n", rc); 593 g_cErrors++; 594 } 509 RTTestFailed(hTest, "UTF-8 -> Code Points failed, rc=%Rrc.\n", rc); 595 510 596 511 /* 597 512 * Check the various string lengths. 598 513 */ 514 RTTestSub(hTest, "Lengths"); 599 515 size_t cuc1 = RTStrCalcUtf16Len(g_szAll); 600 516 size_t cuc2 = RTUtf16Len(g_wszAll); 601 517 if (cuc1 != cuc2) 602 { 603 RTPrintf("tstUtf8: FAILURE - cuc1=%zu != cuc2=%zu\n", cuc1, cuc2); 604 g_cErrors++; 605 } 518 RTTestFailed(hTest, "cuc1=%zu != cuc2=%zu\n", cuc1, cuc2); 606 519 //size_t cuc3 = RTUniLen(g_uszAll); 607 520 … … 610 523 * Enumerate the strings. 611 524 */ 525 RTTestSub(hTest, "Code Point Getters and Putters"); 612 526 char *pszPut1Base = (char *)RTMemAlloc(sizeof(g_szAll)); 613 527 AssertRelease(pszPut1Base); … … 629 543 if (RT_FAILURE(rc)) 630 544 { 631 RT Printf("tstUtf8: FAILURE - RTStrGetCpEx failed with rc=%Rrc at %.10Rhxs\n", rc, psz2);545 RTTestFailed(hTest, "RTStrGetCpEx failed with rc=%Rrc at %.10Rhxs", rc, psz2); 632 546 whereami(8, psz2 - &g_szAll[0]); 633 g_cErrors++;634 547 break; 635 548 } … … 637 550 if (pszPrev1 != psz2) 638 551 { 639 RT Printf("tstUtf8: FAILURE - RTStrPrevCp returned %p expected %p!\n", pszPrev1, psz2);552 RTTestFailed(hTest, "RTStrPrevCp returned %p expected %p!", pszPrev1, psz2); 640 553 whereami(8, psz2 - &g_szAll[0]); 641 g_cErrors++;642 554 break; 643 555 } … … 645 557 if (uc2 != uc1) 646 558 { 647 RT Printf("tstUtf8: FAILURE - RTStrGetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp\n", uc2, uc1);559 RTTestFailed(hTest, "RTStrGetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp", uc2, uc1); 648 560 whereami(8, psz2 - &g_szAll[0]); 649 g_cErrors++;650 561 break; 651 562 } … … 653 564 if (psz2 != psz1) 654 565 { 655 RT Printf("tstUtf8: FAILURE - RTStrGetCpEx and RTStrGetNext returned different next pointer!\n");566 RTTestFailed(hTest, "RTStrGetCpEx and RTStrGetNext returned different next pointer!"); 656 567 whereami(8, psz2 - &g_szAll[0]); 657 g_cErrors++;658 568 break; 659 569 } … … 663 573 if (RT_FAILURE(rc)) 664 574 { 665 RT Printf("tstUtf8: FAILURE - RTUtf16GetCpEx failed with rc=%Rrc at %.10Rhxs\n", rc, pwsz4);575 RTTestFailed(hTest, "RTUtf16GetCpEx failed with rc=%Rrc at %.10Rhxs", rc, pwsz4); 666 576 whereami(16, pwsz4 - &g_wszAll[0]); 667 g_cErrors++;668 577 break; 669 578 } 670 579 if (uc3 != uc2) 671 580 { 672 RT Printf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp\n", uc3, uc2);581 RTTestFailed(hTest, "RTUtf16GetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp", uc3, uc2); 673 582 whereami(16, pwsz4 - &g_wszAll[0]); 674 g_cErrors++;675 583 break; 676 584 } … … 678 586 if (uc3 != uc4) 679 587 { 680 RT Printf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTUtf16GetCp returned different CPs: %RTunicp != %RTunicp\n", uc3, uc4);588 RTTestFailed(hTest, "RTUtf16GetCpEx and RTUtf16GetCp returned different CPs: %RTunicp != %RTunicp", uc3, uc4); 681 589 whereami(16, pwsz4 - &g_wszAll[0]); 682 g_cErrors++;683 590 break; 684 591 } … … 686 593 if (pwsz4 != pwsz3) 687 594 { 688 RT Printf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTUtf16GetNext returned different next pointer!\n");595 RTTestFailed(hTest, "RTUtf16GetCpEx and RTUtf16GetNext returned different next pointer!"); 689 596 whereami(8, pwsz4 - &g_wszAll[0]); 690 g_cErrors++;691 597 break; 692 598 } … … 699 605 if (pszPut1 - pszPut1Base != psz1 - &g_szAll[0]) 700 606 { 701 RT Printf("tstUtf8: FAILURE - RTStrPutCp is not at the same offset! %p != %p\n",702 pszPut1 - pszPut1Base, psz1 - &g_szAll[0]);607 RTTestFailed(hTest, "RTStrPutCp is not at the same offset! %p != %p", 608 pszPut1 - pszPut1Base, psz1 - &g_szAll[0]); 703 609 whereami(8, psz2 - &g_szAll[0]); 704 g_cErrors++;705 610 break; 706 611 } … … 709 614 if (pwszPut2 - pwszPut2Base != pwsz3 - &g_wszAll[0]) 710 615 { 711 RT Printf("tstUtf8: FAILURE - RTStrPutCp is not at the same offset! %p != %p\n",712 pwszPut2 - pwszPut2Base, pwsz3 - &g_wszAll[0]);616 RTTestFailed(hTest, "RTStrPutCp is not at the same offset! %p != %p", 617 pwszPut2 - pwszPut2Base, pwsz3 - &g_wszAll[0]); 713 618 whereami(8, pwsz4 - &g_wszAll[0]); 714 g_cErrors++;715 619 break; 716 620 } … … 726 630 { 727 631 if (mymemcmp(pszPut1Base, g_szAll, sizeof(g_szAll), 8)) 728 { 729 RTPrintf("tstUtf8: FAILURE - RTStrPutCp encoded the string incorrectly.\n"); 730 g_cErrors++; 731 } 632 RTTestFailed(hTest, "RTStrPutCp encoded the string incorrectly."); 732 633 if (mymemcmp(pwszPut2Base, g_wszAll, sizeof(g_wszAll), 16)) 733 { 734 RTPrintf("tstUtf8: FAILURE - RTUtf16PutCp encoded the string incorrectly.\n"); 735 g_cErrors++; 736 } 634 RTTestFailed(hTest, "RTUtf16PutCp encoded the string incorrectly."); 737 635 } 738 636 739 637 RTMemFree(pszPut1Base); 740 638 RTMemFree(pwszPut2Base); 639 640 RTTestSubDone(hTest); 741 641 } 742 642 … … 745 645 * Check case insensitivity. 746 646 */ 747 void test3( void)748 { 749 RT Printf("tstUtf8: TEST 3\n");647 void test3(RTTEST hTest) 648 { 649 RTTestSub(hTest, "Case Sensitivitity"); 750 650 751 651 if ( RTUniCpToLower('a') != 'a' … … 759 659 || RTUniCpToUpper('z') != 'Z' 760 660 || RTUniCpToUpper('Z') != 'Z') 761 { 762 RTPrintf("tstUtf8: FAILURE - RTUniToUpper/Lower failed basic tests.\n"); 763 g_cErrors++; 764 } 661 RTTestFailed(hTest, "RTUniToUpper/Lower failed basic tests.\n"); 765 662 766 663 if (RTUtf16ICmp(g_wszAll, g_wszAll)) 767 { 768 RTPrintf("tstUtf8: FAILURE - RTUtf16ICmp failed the basic test.\n"); 769 g_cErrors++; 770 } 664 RTTestFailed(hTest, "RTUtf16ICmp failed the basic test.\n"); 771 665 772 666 if (RTUtf16Cmp(g_wszAll, g_wszAll)) 773 { 774 RTPrintf("tstUtf8: FAILURE - RTUtf16Cmp failed the basic test.\n"); 775 g_cErrors++; 776 } 667 RTTestFailed(hTest, "RTUtf16Cmp failed the basic test.\n"); 777 668 778 669 static RTUTF16 s_wszTst1a[] = { 'a', 'B', 'c', 'D', 'E', 'f', 'g', 'h', 'i', 'j', 'K', 'L', 'm', 'N', 'o', 'P', 'q', 'r', 'S', 't', 'u', 'V', 'w', 'x', 'Y', 'Z', 0xc5, 0xc6, 0xf8, 0 }; … … 783 674 || RTUtf16ICmp(s_wszTst1b, s_wszTst1a) 784 675 ) 785 { 786 RTPrintf("tstUtf8: FAILURE - RTUtf16ICmp failed the alphabet test.\n"); 787 g_cErrors++; 788 } 676 RTTestFailed(hTest, "RTUtf16ICmp failed the alphabet test.\n"); 789 677 790 678 if ( RTUtf16Cmp(s_wszTst1b, s_wszTst1b) … … 793 681 || !RTUtf16Cmp(s_wszTst1b, s_wszTst1a) 794 682 ) 795 { 796 RTPrintf("tstUtf8: FAILURE - RTUtf16Cmp failed the alphabet test.\n"); 797 g_cErrors++; 798 } 683 RTTestFailed(hTest, "RTUtf16Cmp failed the alphabet test.\n"); 684 685 RTTestSubDone(hTest); 799 686 } 800 687 … … 803 690 * Test the RTStr*Cmp functions. 804 691 */ 805 void TstRTStrXCmp(void) 806 { 807 RTPrintf("tstUtf8: TEST 4 - RTStr*Cmp\n"); 692 void TstRTStrXCmp(RTTEST hTest) 693 { 808 694 #define CHECK_DIFF(expr, op) \ 809 695 do \ … … 811 697 int iDiff = expr; \ 812 698 if (!(iDiff op 0)) \ 813 { \ 814 RTPrintf("tstUtf8(%d): failure - %d " #op " 0: %s\n", __LINE__, iDiff, #expr); \ 815 g_cErrors++; \ 816 } \ 699 RTTestFailed(hTest, "%d: %d " #op " 0: %s\n", __LINE__, iDiff, #expr); \ 817 700 } while (0) 818 701 819 702 /** @todo test the non-ascii bits. */ 820 703 704 RTTestSub(hTest, "RTStrCmp"); 821 705 CHECK_DIFF(RTStrCmp(NULL, NULL), == ); 822 706 CHECK_DIFF(RTStrCmp(NULL, ""), < ); … … 832 716 833 717 718 RTTestSub(hTest, "RTStrNCmp"); 834 719 CHECK_DIFF(RTStrNCmp(NULL, NULL, RTSTR_MAX), == ); 835 720 CHECK_DIFF(RTStrNCmp(NULL, "", RTSTR_MAX), < ); … … 849 734 850 735 736 RTTestSub(hTest, "RTStrICmp"); 851 737 CHECK_DIFF(RTStrICmp(NULL, NULL), == ); 852 738 CHECK_DIFF(RTStrICmp(NULL, ""), < ); … … 869 755 870 756 757 RTTestSub(hTest, "RTStrNICmp"); 871 758 CHECK_DIFF(RTStrNICmp(NULL, NULL, RTSTR_MAX), == ); 872 759 CHECK_DIFF(RTStrNICmp(NULL, "", RTSTR_MAX), < ); … … 901 788 /* We should continue using byte comparison when we hit the invalid CP. Will assert in debug builds. */ 902 789 // CHECK_DIFF(RTStrNICmp("AbCd\xff""eg", "aBcD\xff""eF", 6), ==); 790 791 RTTestSubDone(hTest); 903 792 } 904 793 … … 908 797 * Benchmark stuff. 909 798 */ 910 void Benchmarks(void) 911 { 912 RTPrintf("tstUtf8: BENCHMARKS\n"); 799 void Benchmarks(RTTEST hTest) 800 { 913 801 static union 914 802 { … … 917 805 } s_Buf; 918 806 807 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTStrToUtf16Ex: "); /** @todo figure this stuff into the test framework. */ 919 808 PRTUTF16 pwsz = &s_Buf.wszBuf[0]; 920 809 int rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL); … … 928 817 if (RT_FAILURE(rc)) 929 818 { 930 RT Printf("tstUtf8:UTF-8 -> UTF-16 benchmark failed at i=%d, rc=%Rrc\n", i, rc);819 RTTestFailed(hTest, "UTF-8 -> UTF-16 benchmark failed at i=%d, rc=%Rrc\n", i, rc); 931 820 break; 932 821 } 933 822 } 934 823 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start; 935 RTPrintf("tstUtf8: UTF-8 -> UTF-16: %d in %RI64ns\n", i, u64Elapsed); 936 } 937 824 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %RI64ns\n", i, u64Elapsed); 825 } 826 827 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTUtf16ToUtf8Ex: "); 938 828 char *psz = &s_Buf.szBuf[0]; 939 829 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL); … … 947 837 if (RT_FAILURE(rc)) 948 838 { 949 RT Printf("tstUtf8:UTF-16 -> UTF-8 benchmark failed at i=%d, rc=%Rrc\n", i, rc);839 RTTestFailed(hTest, "UTF-16 -> UTF-8 benchmark failed at i=%d, rc=%Rrc\n", i, rc); 950 840 break; 951 841 } 952 842 } 953 843 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start; 954 RTPrintf("tstUtf8: UTF-16 -> UTF-8: %d in %RI64ns\n", i, u64Elapsed); 955 } 956 844 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %RI64ns\n", i, u64Elapsed); 845 } 846 847 } 848 849 850 /** 851 * Tests RTStrStr and RTStrIStr. 852 */ 853 static void testStrStr(RTTEST hTest) 854 { 855 #define CHECK_NULL(expr) \ 856 do { \ 857 const char *pszRet = expr; \ 858 if (pszRet != NULL) \ 859 RTTestFailed(hTest, "%d: %#x -> %s expected NULL", __LINE__, #expr, pszRet); \ 860 } while (0) 861 862 #define CHECK(expr, expect) \ 863 do { \ 864 const char *pszRet = expr; \ 865 if ( pszRet != expect \ 866 && ( (expect) == NULL \ 867 || pszRet == NULL \ 868 || strcmp(pszRet, (expect)) ) \ 869 ) \ 870 RTTestFailed(hTest, "%d: %#x -> %s expected %s", __LINE__, #expr, pszRet, (expect)); \ 871 } while (0) 872 873 874 RTTestSub(hTest, "RTStrStr"); 875 CHECK(RTStrStr("abcdef", ""), "abcdef"); 876 CHECK_NULL(RTStrStr("abcdef", NULL)); 877 CHECK_NULL(RTStrStr(NULL, "")); 878 CHECK_NULL(RTStrStr(NULL, NULL)); 879 CHECK(RTStrStr("abcdef", "abcdef"), "abcdef"); 880 CHECK(RTStrStr("abcdef", "b"), "bcdef"); 881 CHECK(RTStrStr("abcdef", "bcdef"), "bcdef"); 882 CHECK(RTStrStr("abcdef", "cdef"), "cdef"); 883 CHECK(RTStrStr("abcdef", "cde"), "cdef"); 884 CHECK(RTStrStr("abcdef", "cd"), "cdef"); 885 CHECK(RTStrStr("abcdef", "c"), "cdef"); 886 CHECK(RTStrStr("abcdef", "f"), "f"); 887 CHECK(RTStrStr("abcdef", "ef"), "ef"); 888 CHECK(RTStrStr("abcdef", "e"), "ef"); 889 CHECK_NULL(RTStrStr("abcdef", "z")); 890 CHECK_NULL(RTStrStr("abcdef", "A")); 891 CHECK_NULL(RTStrStr("abcdef", "F")); 892 893 RTTestSub(hTest, "RTStrIStr"); 894 CHECK(RTStrIStr("abcdef", ""), "abcdef"); 895 CHECK_NULL(RTStrIStr("abcdef", NULL)); 896 CHECK_NULL(RTStrIStr(NULL, "")); 897 CHECK_NULL(RTStrIStr(NULL, NULL)); 898 CHECK(RTStrIStr("abcdef", "abcdef"), "abcdef"); 899 CHECK(RTStrIStr("abcdef", "Abcdef"), "abcdef"); 900 CHECK(RTStrIStr("abcdef", "ABcDeF"), "abcdef"); 901 CHECK(RTStrIStr("abcdef", "b"), "bcdef"); 902 CHECK(RTStrIStr("abcdef", "B"), "bcdef"); 903 CHECK(RTStrIStr("abcdef", "bcdef"), "bcdef"); 904 CHECK(RTStrIStr("abcdef", "BCdEf"), "bcdef"); 905 CHECK(RTStrIStr("abcdef", "bCdEf"), "bcdef"); 906 CHECK(RTStrIStr("abcdef", "bcdEf"), "bcdef"); 907 CHECK(RTStrIStr("abcdef", "BcdEf"), "bcdef"); 908 CHECK(RTStrIStr("abcdef", "cdef"), "cdef"); 909 CHECK(RTStrIStr("abcdef", "cde"), "cdef"); 910 CHECK(RTStrIStr("abcdef", "cd"), "cdef"); 911 CHECK(RTStrIStr("abcdef", "c"), "cdef"); 912 CHECK(RTStrIStr("abcdef", "f"), "f"); 913 CHECK(RTStrIStr("abcdeF", "F"), "F"); 914 CHECK(RTStrIStr("abcdef", "F"), "f"); 915 CHECK(RTStrIStr("abcdef", "ef"), "ef"); 916 CHECK(RTStrIStr("EeEef", "e"), "EeEef"); 917 CHECK(RTStrIStr("EeEef", "E"), "EeEef"); 918 CHECK(RTStrIStr("EeEef", "EE"), "EeEef"); 919 CHECK(RTStrIStr("EeEef", "EEE"), "EeEef"); 920 CHECK(RTStrIStr("EeEef", "EEEF"), "eEef"); 921 CHECK_NULL(RTStrIStr("EeEef", "z")); 922 923 #undef CHECK 924 #undef CHECK_NULL 925 RTTestSubDone(hTest); 957 926 } 958 927 … … 960 929 int main() 961 930 { 962 RTR3Init(); 931 /* 932 * Init the runtime and stuff. 933 */ 934 RTTEST hTest; 935 if ( RT_FAILURE(RTR3Init()) 936 || RT_FAILURE(RTTestCreate("tstUtf8", &hTest))) 937 { 938 RTPrintf("tstBitstUtf8: fatal initialization error\n"); 939 return 1; 940 } 941 RTTestBanner(hTest); 963 942 964 943 InitStrings(); 965 test1(); 966 test2(); 967 test3(); 968 TstRTStrXCmp(); 969 Benchmarks(); 944 test1(hTest); 945 test2(hTest); 946 test3(hTest); 947 TstRTStrXCmp(hTest); 948 testStrStr(hTest); 949 Benchmarks(hTest); 970 950 971 951 /* 972 952 * Summary 973 953 */ 974 if (!g_cErrors) 975 RTPrintf("tstUtf8: SUCCESS\n"); 976 else 977 RTPrintf("tstUtf8: FAILURE - %d errors!\n", g_cErrors); 978 979 return !!g_cErrors; 980 } 954 return RTTestSummaryAndDestroy(hTest); 955 }
Note:
See TracChangeset
for help on using the changeset viewer.