Changeset 55671 in vbox for trunk/src/VBox/Runtime/testcase/tstRTGetOptArgv.cpp
- Timestamp:
- May 5, 2015 4:27:52 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/testcase/tstRTGetOptArgv.cpp
r55499 r55671 43 43 static const struct 44 44 { 45 /** The input string. */ 46 const char *pszInput; 45 /** The input string, bourne shell. */ 46 const char *pszInBourne; 47 /** The input string, MS CRT. */ 48 const char *pszInMsCrt; 47 49 /** Separators, NULL if default. */ 48 50 const char *pszSeparators; … … 53 55 /** Expected quoted string, bourne shell. */ 54 56 const char *pszOutBourneSh; 55 /** Expected quoted string, MS CCRT. */57 /** Expected quoted string, MS CRT. */ 56 58 const char *pszOutMsCrt; 57 } g_a BourneTests[] =59 } g_aTests[] = 58 60 { 59 61 { 60 62 "0 1 \"\"2'' '3' 4 5 '''''6' 7 8 9 10 11", 63 "0 1 \"\"2 3 4 5 \"6\" 7 8 \"\"\"\"\"\"9\"\"\"\" 10 11", 61 64 NULL, 62 65 12, … … 81 84 { 82 85 "\t\" asdf \" '\"'xyz \"\t\" '\n' '\"' \"'\"\n\r ", 86 "\t\" asdf \" \\\"xyz \"\t\" \"\n\" \"\\\"\" '\n\r ", 83 87 NULL, 84 88 6, … … 98 102 { 99 103 ":0::1::::2:3:4:5:", 104 ":0::1::::2:3:4:5:", 100 105 ":", 101 106 6, … … 115 120 { 116 121 "0:1;2:3;4:5", 122 "0:1;2:3;4:5", 117 123 ";;;;;;;;;;;;;;;;;;;;;;:", 118 124 6, … … 132 138 { 133 139 "abcd 'a ' ' b' ' c '", 140 "abcd \"a \" \" b\" \" c \"", 134 141 NULL, 135 142 4, … … 147 154 { 148 155 "'a\n\\b' 'de'\"'\"'fg' h ''\"'\"''", 156 "\"a\n\\b\" de'fg h \"'\" ", 149 157 NULL, 150 158 4, … … 162 170 { 163 171 "arg1 \"arg2=\\\"zyx\\\"\" 'arg3=\\\\\\'", 172 "arg1 arg2=\\\"zyx\\\" arg3=\\\\\\", 164 173 NULL, 165 174 3, … … 176 185 { 177 186 " a\\\\\\\\b d\"e f\"g h ij\t", 187 " a\\\\b d\"e f\"g h ij\t", 178 188 NULL, 179 189 4, … … 253 263 254 264 255 256 static void tst3(void) 257 { 258 /* 259 * Bourne shell round-tripping. 260 */ 261 RTTestISub("Round-trips / BOURNE_SH"); 262 for (unsigned i = 0; i < RT_ELEMENTS(g_aBourneTests); i++) 265 static void tst4(void) 266 { 267 /* 268 * Microsoft CRT round-tripping. 269 */ 270 RTTestISub("Round-trips / MS_CRT"); 271 for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++) 263 272 { 264 273 /* First */ 265 274 char **papszArgs1 = NULL; 266 275 int cArgs1 = -1; 267 int rc = RTGetOptArgvFromString(&papszArgs1, &cArgs1, g_aBourneTests[i].pszInput, g_aBourneTests[i].pszSeparators); 276 int rc = RTGetOptArgvFromString(&papszArgs1, &cArgs1, g_aTests[i].pszInMsCrt, 277 RTGETOPTARGV_CNV_QUOTE_MS_CRT, g_aTests[i].pszSeparators); 268 278 if (rc == VINF_SUCCESS) 269 279 { 270 if (cArgs1 != g_a BourneTests[i].cArgs)271 RTTestIFailed("g_a BourneTests[%i]: #1=%d, expected %d", i, cArgs1, g_aBourneTests[i].cArgs);280 if (cArgs1 != g_aTests[i].cArgs) 281 RTTestIFailed("g_aTests[%i]: #1=%d, expected %d", i, cArgs1, g_aTests[i].cArgs); 272 282 for (int iArg = 0; iArg < cArgs1; iArg++) 273 if (strcmp(papszArgs1[iArg], g_aBourneTests[i].apszArgs[iArg]) != 0) 274 RTTestIFailed("g_aBourneTests[%i]/1: argv[%i] differs: got '%s', expected '%s' (RTGetOptArgvFromString(,,'%s', '%s'))", 275 i, iArg, papszArgs1[iArg], g_aBourneTests[i].apszArgs[iArg], 276 g_aBourneTests[i].pszInput, g_aBourneTests[i].pszSeparators); 283 if (strcmp(papszArgs1[iArg], g_aTests[i].apszArgs[iArg]) != 0) 284 RTTestIFailed("g_aTests[%i]/1: argv[%i] differs: got '%s', expected '%s' (RTGetOptArgvFromString(,,'%s', '%s'))", 285 i, iArg, papszArgs1[iArg], g_aTests[i].apszArgs[iArg], 286 g_aTests[i].pszInMsCrt, g_aTests[i].pszSeparators); 287 RTTESTI_CHECK_RETV(papszArgs1[cArgs1] == NULL); 288 tstCheckNativeMsCrtToArgv(g_aTests[i].pszInMsCrt, g_aTests[i].cArgs, g_aTests[i].apszArgs); 289 290 /* Second */ 291 char *pszArgs2 = NULL; 292 rc = RTGetOptArgvToString(&pszArgs2, papszArgs1, RTGETOPTARGV_CNV_QUOTE_MS_CRT); 293 if (rc == VINF_SUCCESS) 294 { 295 if (strcmp(pszArgs2, g_aTests[i].pszOutMsCrt)) 296 RTTestIFailed("g_aTests[%i]/2: '%s', expected '%s'", i, pszArgs2, g_aTests[i].pszOutMsCrt); 297 298 /* 299 * Third 300 */ 301 char **papszArgs3 = NULL; 302 int cArgs3 = -1; 303 rc = RTGetOptArgvFromString(&papszArgs3, &cArgs3, pszArgs2, RTGETOPTARGV_CNV_QUOTE_MS_CRT, NULL); 304 if (rc == VINF_SUCCESS) 305 { 306 if (cArgs3 != g_aTests[i].cArgs) 307 RTTestIFailed("g_aTests[%i]/3: %d, expected %d", i, cArgs3, g_aTests[i].cArgs); 308 for (int iArg = 0; iArg < cArgs3; iArg++) 309 if (strcmp(papszArgs3[iArg], g_aTests[i].apszArgs[iArg]) != 0) 310 RTTestIFailed("g_aTests[%i]/3: argv[%i] differs: got '%s', expected '%s' (RTGetOptArgvFromString(,,'%s',))", 311 i, iArg, papszArgs3[iArg], g_aTests[i].apszArgs[iArg], pszArgs2); 312 RTTESTI_CHECK_RETV(papszArgs3[cArgs3] == NULL); 313 tstCheckNativeMsCrtToArgv(pszArgs2, g_aTests[i].cArgs, g_aTests[i].apszArgs); 314 315 /* 316 * Fourth 317 */ 318 char *pszArgs4 = NULL; 319 rc = RTGetOptArgvToString(&pszArgs4, papszArgs3, RTGETOPTARGV_CNV_QUOTE_MS_CRT); 320 if (rc == VINF_SUCCESS) 321 { 322 if (strcmp(pszArgs4, pszArgs2)) 323 RTTestIFailed("g_aTests[%i]/4: '%s' does not match #4='%s'", i, pszArgs2, pszArgs4); 324 RTStrFree(pszArgs4); 325 } 326 else 327 RTTestIFailed("g_aTests[%i]/4: RTGetOptArgvToString() -> %Rrc", i, rc); 328 RTGetOptArgvFree(papszArgs3); 329 } 330 else 331 RTTestIFailed("g_aTests[%i]/3: RTGetOptArgvFromString() -> %Rrc", i, rc); 332 RTStrFree(pszArgs2); 333 } 334 else 335 RTTestIFailed("g_aTests[%i]/2: RTGetOptArgvToString() -> %Rrc", i, rc); 336 RTGetOptArgvFree(papszArgs1); 337 } 338 else 339 RTTestIFailed("g_aTests[%i]/1: RTGetOptArgvFromString(,,'%s', '%s') -> %Rrc", 340 i, g_aTests[i].pszInMsCrt, g_aTests[i].pszSeparators, rc); 341 } 342 343 } 344 345 346 347 static void tst3(void) 348 { 349 /* 350 * Bourne shell round-tripping. 351 */ 352 RTTestISub("Round-trips / BOURNE_SH"); 353 for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++) 354 { 355 /* First */ 356 char **papszArgs1 = NULL; 357 int cArgs1 = -1; 358 int rc = RTGetOptArgvFromString(&papszArgs1, &cArgs1, g_aTests[i].pszInBourne, 359 RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, g_aTests[i].pszSeparators); 360 if (rc == VINF_SUCCESS) 361 { 362 if (cArgs1 != g_aTests[i].cArgs) 363 RTTestIFailed("g_aTests[%i]: #1=%d, expected %d", i, cArgs1, g_aTests[i].cArgs); 364 for (int iArg = 0; iArg < cArgs1; iArg++) 365 if (strcmp(papszArgs1[iArg], g_aTests[i].apszArgs[iArg]) != 0) 366 RTTestIFailed("g_aTests[%i]/1: argv[%i] differs: got '%s', expected '%s' (RTGetOptArgvFromString(,,'%s', '%s'))", 367 i, iArg, papszArgs1[iArg], g_aTests[i].apszArgs[iArg], 368 g_aTests[i].pszInBourne, g_aTests[i].pszSeparators); 277 369 RTTESTI_CHECK_RETV(papszArgs1[cArgs1] == NULL); 278 370 … … 282 374 if (rc == VINF_SUCCESS) 283 375 { 284 if (strcmp(pszArgs2, g_a BourneTests[i].pszOutBourneSh))285 RTTestIFailed("g_a BourneTests[%i]/2: '%s', expected '%s'", i, pszArgs2, g_aBourneTests[i].pszOutBourneSh);376 if (strcmp(pszArgs2, g_aTests[i].pszOutBourneSh)) 377 RTTestIFailed("g_aTests[%i]/2: '%s', expected '%s'", i, pszArgs2, g_aTests[i].pszOutBourneSh); 286 378 287 379 /* … … 290 382 char **papszArgs3 = NULL; 291 383 int cArgs3 = -1; 292 rc = RTGetOptArgvFromString(&papszArgs3, &cArgs3, pszArgs2, NULL);384 rc = RTGetOptArgvFromString(&papszArgs3, &cArgs3, pszArgs2, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, NULL); 293 385 if (rc == VINF_SUCCESS) 294 386 { 295 if (cArgs3 != g_a BourneTests[i].cArgs)296 RTTestIFailed("g_a BourneTests[%i]/3: %d, expected %d", i, cArgs3, g_aBourneTests[i].cArgs);387 if (cArgs3 != g_aTests[i].cArgs) 388 RTTestIFailed("g_aTests[%i]/3: %d, expected %d", i, cArgs3, g_aTests[i].cArgs); 297 389 for (int iArg = 0; iArg < cArgs3; iArg++) 298 if (strcmp(papszArgs3[iArg], g_a BourneTests[i].apszArgs[iArg]) != 0)299 RTTestIFailed("g_a BourneTests[%i]/3: argv[%i] differs: got '%s', expected '%s' (RTGetOptArgvFromString(,,'%s',))",300 i, iArg, papszArgs3[iArg], g_a BourneTests[i].apszArgs[iArg], pszArgs2);390 if (strcmp(papszArgs3[iArg], g_aTests[i].apszArgs[iArg]) != 0) 391 RTTestIFailed("g_aTests[%i]/3: argv[%i] differs: got '%s', expected '%s' (RTGetOptArgvFromString(,,'%s',))", 392 i, iArg, papszArgs3[iArg], g_aTests[i].apszArgs[iArg], pszArgs2); 301 393 RTTESTI_CHECK_RETV(papszArgs3[cArgs3] == NULL); 302 394 … … 309 401 { 310 402 if (strcmp(pszArgs4, pszArgs2)) 311 RTTestIFailed("g_a BourneTests[%i]/4: '%s' does not match #4='%s'", i, pszArgs2, pszArgs4);403 RTTestIFailed("g_aTests[%i]/4: '%s' does not match #4='%s'", i, pszArgs2, pszArgs4); 312 404 RTStrFree(pszArgs4); 313 405 } 314 406 else 315 RTTestIFailed("g_a BourneTests[%i]/4: RTGetOptArgvToString() -> %Rrc", i, rc);407 RTTestIFailed("g_aTests[%i]/4: RTGetOptArgvToString() -> %Rrc", i, rc); 316 408 RTGetOptArgvFree(papszArgs3); 317 409 } 318 410 else 319 RTTestIFailed("g_a BourneTests[%i]/3: RTGetOptArgvFromString() -> %Rrc", i, rc);411 RTTestIFailed("g_aTests[%i]/3: RTGetOptArgvFromString() -> %Rrc", i, rc); 320 412 RTStrFree(pszArgs2); 321 413 } 322 414 else 323 RTTestIFailed("g_a BourneTests[%i]/2: RTGetOptArgvToString() -> %Rrc", i, rc);415 RTTestIFailed("g_aTests[%i]/2: RTGetOptArgvToString() -> %Rrc", i, rc); 324 416 RTGetOptArgvFree(papszArgs1); 325 417 } 326 418 else 327 RTTestIFailed("g_aBourneTests[%i]/1: RTGetOptArgvFromString(,,'%s', '%s') -> %Rrc", 328 i, g_aBourneTests[i].pszInput, g_aBourneTests[i].pszSeparators, rc); 329 } 330 419 RTTestIFailed("g_aTests[%i]/1: RTGetOptArgvFromString(,,'%s', '%s') -> %Rrc", 420 i, g_aTests[i].pszInBourne, g_aTests[i].pszSeparators, rc); 421 } 331 422 } 332 423 … … 375 466 } 376 467 377 for (size_t i = 0; i < RT_ELEMENTS(g_a BourneTests); i++)468 for (size_t i = 0; i < RT_ELEMENTS(g_aTests); i++) 378 469 { 379 470 char *pszCmdLine = NULL; 380 int rc = RTGetOptArgvToString(&pszCmdLine, g_a BourneTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_MS_CRT);471 int rc = RTGetOptArgvToString(&pszCmdLine, g_aTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_MS_CRT); 381 472 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS); 382 if (!strcmp(g_a BourneTests[i].pszOutMsCrt, pszCmdLine))383 tstCheckNativeMsCrtToArgv(pszCmdLine, g_a BourneTests[i].cArgs, g_aBourneTests[i].apszArgs);473 if (!strcmp(g_aTests[i].pszOutMsCrt, pszCmdLine)) 474 tstCheckNativeMsCrtToArgv(pszCmdLine, g_aTests[i].cArgs, g_aTests[i].apszArgs); 384 475 else 385 RTTestIFailed("g_a BourneTests[%i] failed:\n"476 RTTestIFailed("g_aTests[%i] failed:\n" 386 477 " got |%s|\n" 387 478 " expected |%s|\n", 388 i, pszCmdLine, g_a BourneTests[i].pszOutMsCrt);479 i, pszCmdLine, g_aTests[i].pszOutMsCrt); 389 480 RTStrFree(pszCmdLine); 390 481 } … … 394 485 RTTestISub("RTGetOptArgvToString / BOURNE_SH"); 395 486 396 for (size_t i = 0; i < RT_ELEMENTS(g_a BourneTests); i++)487 for (size_t i = 0; i < RT_ELEMENTS(g_aTests); i++) 397 488 { 398 489 char *pszCmdLine = NULL; 399 int rc = RTGetOptArgvToString(&pszCmdLine, g_a BourneTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);490 int rc = RTGetOptArgvToString(&pszCmdLine, g_aTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH); 400 491 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS); 401 if (strcmp(g_a BourneTests[i].pszOutBourneSh, pszCmdLine))402 RTTestIFailed("g_a BourneTests[%i] failed:\n"492 if (strcmp(g_aTests[i].pszOutBourneSh, pszCmdLine)) 493 RTTestIFailed("g_aTests[%i] failed:\n" 403 494 " got |%s|\n" 404 495 " expected |%s|\n", 405 i, pszCmdLine, g_a BourneTests[i].pszOutBourneSh);496 i, pszCmdLine, g_aTests[i].pszOutBourneSh); 406 497 RTStrFree(pszCmdLine); 407 498 } … … 413 504 char **papszArgs = NULL; 414 505 int cArgs = -1; 415 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "", NULL), VINF_SUCCESS);506 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "", RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, NULL), VINF_SUCCESS); 416 507 RTTESTI_CHECK_RETV(cArgs == 0); 417 508 RTTESTI_CHECK_RETV(papszArgs); … … 419 510 RTGetOptArgvFree(papszArgs); 420 511 421 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "0 1 \"\"2'' '3' 4 5 '''''6' 7 8 9 10 11", NULL), VINF_SUCCESS); 512 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "0 1 \"\"2'' '3' 4 5 '''''6' 7 8 9 10 11", 513 RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, NULL), VINF_SUCCESS); 422 514 RTTESTI_CHECK_RETV(cArgs == 12); 423 515 RTTESTI_CHECK_RETV(!strcmp(papszArgs[0], "0")); … … 436 528 RTGetOptArgvFree(papszArgs); 437 529 438 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "\t\" asdf \" '\"'xyz \"\t\" '\n' '\"' \"'\"\n\r ", NULL), VINF_SUCCESS); 530 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "\t\" asdf \" '\"'xyz \"\t\" '\n' '\"' \"'\"\n\r ", 531 RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, NULL), VINF_SUCCESS); 439 532 RTTESTI_CHECK_RETV(cArgs == 6); 440 533 RTTESTI_CHECK_RETV(!strcmp(papszArgs[0], " asdf ")); … … 447 540 RTGetOptArgvFree(papszArgs); 448 541 449 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, ":0::1::::2:3:4:5:", ":"), VINF_SUCCESS); 542 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, ":0::1::::2:3:4:5:", 543 RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, ":"), VINF_SUCCESS); 450 544 RTTESTI_CHECK_RETV(cArgs == 6); 451 545 RTTESTI_CHECK_RETV(!strcmp(papszArgs[0], "0")); … … 458 552 RTGetOptArgvFree(papszArgs); 459 553 460 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "0:1;2:3;4:5", ";;;;;;;;;;;;;;;;;;;;;;:"), VINF_SUCCESS); 554 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "0:1;2:3;4:5", RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, 555 ";;;;;;;;;;;;;;;;;;;;;;:"), VINF_SUCCESS); 461 556 RTTESTI_CHECK_RETV(cArgs == 6); 462 557 RTTESTI_CHECK_RETV(!strcmp(papszArgs[0], "0")); … … 472 567 * Tests from the list. 473 568 */ 474 for (unsigned i = 0; i < RT_ELEMENTS(g_a BourneTests); i++)569 for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++) 475 570 { 476 571 papszArgs = NULL; 477 572 cArgs = -1; 478 int rc = RTGetOptArgvFromString(&papszArgs, &cArgs, g_aBourneTests[i].pszInput, g_aBourneTests[i].pszSeparators); 573 int rc = RTGetOptArgvFromString(&papszArgs, &cArgs, g_aTests[i].pszInBourne, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, 574 g_aTests[i].pszSeparators); 479 575 if (rc == VINF_SUCCESS) 480 576 { 481 if (cArgs == g_a BourneTests[i].cArgs)577 if (cArgs == g_aTests[i].cArgs) 482 578 { 483 579 for (int iArg = 0; iArg < cArgs; iArg++) 484 if (strcmp(papszArgs[iArg], g_a BourneTests[i].apszArgs[iArg]) != 0)485 RTTestIFailed("g_a BourneTests[%i]: argv[%i] differs: got '%s', expected '%s' (RTGetOptArgvFromString(,,'%s', '%s'))",486 i, iArg, papszArgs[iArg], g_a BourneTests[i].apszArgs[iArg],487 g_a BourneTests[i].pszInput, g_aBourneTests[i].pszSeparators);580 if (strcmp(papszArgs[iArg], g_aTests[i].apszArgs[iArg]) != 0) 581 RTTestIFailed("g_aTests[%i]: argv[%i] differs: got '%s', expected '%s' (RTGetOptArgvFromString(,,'%s', '%s'))", 582 i, iArg, papszArgs[iArg], g_aTests[i].apszArgs[iArg], 583 g_aTests[i].pszInBourne, g_aTests[i].pszSeparators); 488 584 RTTESTI_CHECK_RETV(papszArgs[cArgs] == NULL); 489 585 } 490 586 else 491 RTTestIFailed("g_a BourneTests[%i]: cArgs=%u, expected %u for RTGetOptArgvFromString(,,'%s', '%s')",492 i, cArgs, g_a BourneTests[i].cArgs, g_aBourneTests[i].pszInput, g_aBourneTests[i].pszSeparators);587 RTTestIFailed("g_aTests[%i]: cArgs=%u, expected %u for RTGetOptArgvFromString(,,'%s', '%s')", 588 i, cArgs, g_aTests[i].cArgs, g_aTests[i].pszInBourne, g_aTests[i].pszSeparators); 493 589 RTGetOptArgvFree(papszArgs); 494 590 } 495 591 else 496 RTTestIFailed("g_a BourneTests[%i]: RTGetOptArgvFromString(,,'%s', '%s') -> %Rrc",497 i, g_a BourneTests[i].pszInput, g_aBourneTests[i].pszSeparators, rc);592 RTTestIFailed("g_aTests[%i]: RTGetOptArgvFromString(,,'%s', '%s') -> %Rrc", 593 i, g_aTests[i].pszInBourne, g_aTests[i].pszSeparators, rc); 498 594 } 499 595 } … … 516 612 tst1(); 517 613 tst2(); 614 tst4(); 518 615 tst3(); 519 616
Note:
See TracChangeset
for help on using the changeset viewer.