Branch
Hash :
a5df4851
Author :
Date :
2023-10-24T16:41:19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
/**
* Standard C library routine test suite
*/
#include <stdio.h>
#include "SDL.h"
#include "SDL_test.h"
/* Test case functions */
/**
* @brief Call to SDL_strlcpy
*/
#undef SDL_strlcpy
int stdlib_strlcpy(void *arg)
{
size_t result;
char text[1024];
const char *expected;
result = SDL_strlcpy(text, "foo", sizeof(text));
expected = "foo";
SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\")");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), (int)result);
result = SDL_strlcpy(text, "foo", 2);
expected = "f";
SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\") with buffer size 2");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", (int)result);
return TEST_COMPLETED;
}
#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
#pragma GCC diagnostic push
#if defined(HAVE_WFORMAT)
#pragma GCC diagnostic ignored "-Wformat"
#endif
#if defined(HAVE_WFORMAT_EXTRA_ARGS)
#pragma GCC diagnostic ignored "-Wformat-extra-args"
#endif
#endif
/**
* @brief Call to SDL_snprintf
*/
#undef SDL_snprintf
int stdlib_snprintf(void *arg)
{
int result;
int predicted;
char text[1024];
const char *expected;
size_t size;
result = SDL_snprintf(text, sizeof(text), "%s", "foo");
expected = "foo";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\")");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
result = SDL_snprintf(text, sizeof(text), "%S", L"foo");
expected = "foo";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%S\", \"foo\")");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
result = SDL_snprintf(text, sizeof(text), "%ls", L"foo");
expected = "foo";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%ls\", \"foo\")");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
result = SDL_snprintf(text, 2, "%s", "foo");
expected = "f";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\") with buffer size 2");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
result = SDL_snprintf(NULL, 0, "%s", "foo");
SDLTest_AssertPass("Call to SDL_snprintf(NULL, 0, \"%%s\", \"foo\")");
SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
result = SDL_snprintf(text, 2, "%s\n", "foo");
expected = "f";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\\n\", \"foo\") with buffer size 2");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
SDLTest_AssertCheck(result == 4, "Check result value, expected: 4, got: %d", result);
result = SDL_snprintf(text, sizeof(text), "%f", 0.0);
predicted = SDL_snprintf(NULL, 0, "%f", 0.0);
expected = "0.000000";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 0.0)");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
result = SDL_snprintf(text, sizeof(text), "%f", 1.0);
predicted = SDL_snprintf(NULL, 0, "%f", 1.0);
expected = "1.000000";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0)");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
result = SDL_snprintf(text, sizeof(text), "%.f", 1.0);
predicted = SDL_snprintf(NULL, 0, "%.f", 1.0);
expected = "1";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%.f\", 1.0)");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
result = SDL_snprintf(text, sizeof(text), "%#.f", 1.0);
predicted = SDL_snprintf(NULL, 0, "%#.f", 1.0);
expected = "1.";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%#.f\", 1.0)");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
result = SDL_snprintf(text, sizeof(text), "%f", 1.0 + 1.0 / 3.0);
predicted = SDL_snprintf(NULL, 0, "%f", 1.0 + 1.0 / 3.0);
expected = "1.333333";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0 + 1.0 / 3.0)");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
result = SDL_snprintf(text, sizeof(text), "%+f", 1.0 + 1.0 / 3.0);
predicted = SDL_snprintf(NULL, 0, "%+f", 1.0 + 1.0 / 3.0);
expected = "+1.333333";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%+f\", 1.0 + 1.0 / 3.0)");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
result = SDL_snprintf(text, sizeof(text), "%.2f", 1.0 + 1.0 / 3.0);
predicted = SDL_snprintf(NULL, 0, "%.2f", 1.0 + 1.0 / 3.0);
expected = "1.33";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%.2f\", 1.0 + 1.0 / 3.0)");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
result = SDL_snprintf(text, sizeof(text), "%6.2f", 1.0 + 1.0 / 3.0);
predicted = SDL_snprintf(NULL, 0, "%6.2f", 1.0 + 1.0 / 3.0);
expected = " 1.33";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%6.2f\", 1.0 + 1.0 / 3.0)");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
result = SDL_snprintf(text, sizeof(text), "%06.2f", 1.0 + 1.0 / 3.0);
predicted = SDL_snprintf(NULL, 0, "%06.2f", 1.0 + 1.0 / 3.0);
expected = "001.33";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0)");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
result = SDL_snprintf(text, 5, "%06.2f", 1.0 + 1.0 / 3.0);
expected = "001.";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0) with buffer size 5");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
size = 64;
result = SDL_snprintf(text, sizeof(text), "%zu %s", size, "test");
expected = "64 test";
SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"%%zu %%s\", size, \"test\")");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
SDLTest_AssertCheck(result == 7, "Check result value, expected: 7, got: %d", result);
return TEST_COMPLETED;
}
#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
#pragma GCC diagnostic pop
#endif
/**
* @brief Call to SDL_getenv and SDL_setenv
*/
int stdlib_getsetenv(void *arg)
{
const int nameLen = 16;
char name[17];
int counter;
int result;
char *value1;
char *value2;
char *expected;
int overwrite;
char *text;
/* Create a random name. This tests SDL_getenv, since we need to */
/* make sure the variable is not set yet (it shouldn't). */
do {
for (counter = 0; counter < nameLen; counter++) {
name[counter] = (char)SDLTest_RandomIntegerInRange(65, 90);
}
name[nameLen] = '\0';
text = SDL_getenv(name);
SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
if (text != NULL) {
SDLTest_Log("Expected: NULL, Got: '%s' (%i)", text, (int)SDL_strlen(text));
}
} while (text != NULL);
/* Create random values to set */
value1 = SDLTest_RandomAsciiStringOfSize(10);
value2 = SDLTest_RandomAsciiStringOfSize(10);
/* Set value 1 without overwrite */
overwrite = 0;
expected = value1;
result = SDL_setenv(name, value1, overwrite);
SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
/* Check value */
text = SDL_getenv(name);
SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
if (text != NULL) {
SDLTest_AssertCheck(
SDL_strcmp(text, expected) == 0,
"Verify returned text, expected: %s, got: %s",
expected,
text);
}
/* Set value 2 with overwrite */
overwrite = 1;
expected = value2;
result = SDL_setenv(name, value2, overwrite);
SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value2, overwrite);
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
/* Check value */
text = SDL_getenv(name);
SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
if (text != NULL) {
SDLTest_AssertCheck(
SDL_strcmp(text, expected) == 0,
"Verify returned text, expected: %s, got: %s",
expected,
text);
}
/* Set value 1 without overwrite */
overwrite = 0;
expected = value2;
result = SDL_setenv(name, value1, overwrite);
SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
/* Check value */
text = SDL_getenv(name);
SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
if (text != NULL) {
SDLTest_AssertCheck(
SDL_strcmp(text, expected) == 0,
"Verify returned text, expected: %s, got: %s",
expected,
text);
}
/* Set value 1 without overwrite */
overwrite = 1;
expected = value1;
result = SDL_setenv(name, value1, overwrite);
SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
/* Check value */
text = SDL_getenv(name);
SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
if (text != NULL) {
SDLTest_AssertCheck(
SDL_strcmp(text, expected) == 0,
"Verify returned text, expected: %s, got: %s",
expected,
text);
}
/* Negative cases */
for (overwrite = 0; overwrite <= 1; overwrite++) {
result = SDL_setenv(NULL, value1, overwrite);
SDLTest_AssertPass("Call to SDL_setenv(NULL,'%s', %i)", value1, overwrite);
SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
result = SDL_setenv("", value1, overwrite);
SDLTest_AssertPass("Call to SDL_setenv('','%s', %i)", value1, overwrite);
SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
result = SDL_setenv("=", value1, overwrite);
SDLTest_AssertPass("Call to SDL_setenv('=','%s', %i)", value1, overwrite);
SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
result = SDL_setenv(name, NULL, overwrite);
SDLTest_AssertPass("Call to SDL_setenv('%s', NULL, %i)", name, overwrite);
SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
}
/* Clean up */
SDL_free(value1);
SDL_free(value2);
return TEST_COMPLETED;
}
#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
#pragma GCC diagnostic push
#if defined(HAVE_WFORMAT)
#pragma GCC diagnostic ignored "-Wformat"
#endif
#if defined(HAVE_WFORMAT_EXTRA_ARGS)
#pragma GCC diagnostic ignored "-Wformat-extra-args"
#endif
#endif
/**
* @brief Call to SDL_sscanf
*/
#undef SDL_sscanf
int stdlib_sscanf(void *arg)
{
int output;
int result;
int expected_output;
int expected_result;
short short_output, expected_short_output;
long long_output, expected_long_output;
long long long_long_output, expected_long_long_output;
size_t size_output, expected_size_output;
char text[128], text2[128];
expected_output = output = 123;
expected_result = -1;
result = SDL_sscanf("", "%i", &output);
SDLTest_AssertPass("Call to SDL_sscanf(\"\", \"%%i\", &output)");
SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
expected_output = output = 123;
expected_result = 0;
result = SDL_sscanf("a", "%i", &output);
SDLTest_AssertPass("Call to SDL_sscanf(\"a\", \"%%i\", &output)");
SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
output = 123;
expected_output = 2;
expected_result = 1;
result = SDL_sscanf("2", "%i", &output);
SDLTest_AssertPass("Call to SDL_sscanf(\"2\", \"%%i\", &output)");
SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
output = 123;
expected_output = 0xa;
expected_result = 1;
result = SDL_sscanf("aa", "%1x", &output);
SDLTest_AssertPass("Call to SDL_sscanf(\"aa\", \"%%1x\", &output)");
SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
#define SIZED_TEST_CASE(type, var, format_specifier) \
var##_output = 123; \
expected_##var##_output = (type)(((unsigned type)(~0)) >> 1); \
expected_result = 1; \
result = SDL_snprintf(text, sizeof(text), format_specifier, expected_##var##_output); \
result = SDL_sscanf(text, format_specifier, &var##_output); \
SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%s\", &output)", text, #format_specifier); \
SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " format_specifier ", got: " format_specifier, expected_##var##_output, var##_output); \
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); \
\
var##_output = 123; \
expected_##var##_output = ~(type)(((unsigned type)(~0)) >> 1); \
expected_result = 1; \
result = SDL_snprintf(text, sizeof(text), format_specifier, expected_##var##_output); \
result = SDL_sscanf(text, format_specifier, &var##_output); \
SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%s\", &output)", text, #format_specifier); \
SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " format_specifier ", got: " format_specifier, expected_##var##_output, var##_output); \
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
SIZED_TEST_CASE(short, short, "%hd")
SIZED_TEST_CASE(long, long, "%ld")
SIZED_TEST_CASE(long long, long_long, "%lld")
size_output = 123;
expected_size_output = ~((size_t)0);
expected_result = 1;
result = SDL_snprintf(text, sizeof(text), "%zu", expected_size_output);
result = SDL_sscanf(text, "%zu", &size_output);
SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%%zu\", &output)", text);
SDLTest_AssertCheck(expected_size_output == size_output, "Check output, expected: %zu, got: %zu", expected_size_output, size_output);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
expected_result = 1;
text[0] = '\0';
result = SDL_sscanf("abc def", "%s", text);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc def\", \"%%s\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
expected_result = 1;
text[0] = '\0';
result = SDL_sscanf("abc,def", "%s", text);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%s\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc,def") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
expected_result = 1;
text[0] = '\0';
result = SDL_sscanf("abc,def", "%[cba]", text);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[cba]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
expected_result = 1;
text[0] = '\0';
result = SDL_sscanf("abc,def", "%[a-z]", text);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[z-a]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
expected_result = 1;
text[0] = '\0';
result = SDL_sscanf("abc,def", "%[^,]", text);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[^,]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
expected_result = 0;
text[0] = '\0';
result = SDL_sscanf("abc,def", "%[A-Z]", text);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[A-Z]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "") == 0, "Check output, expected: \"\", got: \"%s\"", text);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
expected_result = 2;
text[0] = '\0';
text2[0] = '\0';
result = SDL_sscanf("abc,def", "%[abc],%[def]", text, text2);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[abc],%%[def]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
expected_result = 2;
text[0] = '\0';
text2[0] = '\0';
result = SDL_sscanf("abc,def", "%[abc]%*[,]%[def]", text, text2);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[abc]%%*[,]%%[def]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
expected_result = 2;
text[0] = '\0';
text2[0] = '\0';
result = SDL_sscanf("abc def", "%[abc] %[def]", text, text2);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc def\", \"%%[abc] %%[def]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
expected_result = 1;
text[0] = '\0';
result = SDL_sscanf("abc123XYZ", "%[a-zA-Z0-9]", text);
SDLTest_AssertPass("Call to SDL_sscanf(\"abc123XYZ\", \"%%[a-zA-Z0-9]\", text)");
SDLTest_AssertCheck(SDL_strcmp(text, "abc123XYZ") == 0, "Check output, expected: \"abc123XYZ\", got: \"%s\"", text);
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
return TEST_COMPLETED;
}
#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
#pragma GCC diagnostic pop
#endif
#if defined(_WIN64)
#define SIZE_FORMAT "I64u"
#elif defined(__WIN32__)
#define SIZE_FORMAT "I32u"
#else
#define SIZE_FORMAT "zu"
#endif
typedef struct
{
size_t a;
size_t b;
size_t result;
int status;
} overflow_test;
static const overflow_test multiplications[] = {
{ 1, 1, 1, 0 },
{ 0, 0, 0, 0 },
{ SDL_SIZE_MAX, 0, 0, 0 },
{ SDL_SIZE_MAX, 1, SDL_SIZE_MAX, 0 },
{ SDL_SIZE_MAX / 2, 2, SDL_SIZE_MAX - (SDL_SIZE_MAX % 2), 0 },
{ SDL_SIZE_MAX / 23, 23, SDL_SIZE_MAX - (SDL_SIZE_MAX % 23), 0 },
{ (SDL_SIZE_MAX / 2) + 1, 2, 0, -1 },
{ (SDL_SIZE_MAX / 23) + 42, 23, 0, -1 },
{ SDL_SIZE_MAX, SDL_SIZE_MAX, 0, -1 },
};
static const overflow_test additions[] = {
{ 1, 1, 2, 0 },
{ 0, 0, 0, 0 },
{ SDL_SIZE_MAX, 0, SDL_SIZE_MAX, 0 },
{ SDL_SIZE_MAX - 1, 1, SDL_SIZE_MAX, 0 },
{ SDL_SIZE_MAX - 42, 23, SDL_SIZE_MAX - (42 - 23), 0 },
{ SDL_SIZE_MAX, 1, 0, -1 },
{ SDL_SIZE_MAX, 23, 0, -1 },
{ SDL_SIZE_MAX, SDL_SIZE_MAX, 0, -1 },
};
static int
stdlib_overflow(void *arg)
{
size_t i;
size_t useBuiltin;
for (useBuiltin = 0; useBuiltin < 2; useBuiltin++) {
if (useBuiltin) {
SDLTest_Log("Using gcc/clang builtins if possible");
} else {
SDLTest_Log("Not using gcc/clang builtins");
}
for (i = 0; i < SDL_arraysize(multiplications); i++) {
const overflow_test *t = &multiplications[i];
int status;
size_t result = ~t->result;
if (useBuiltin) {
status = SDL_size_mul_overflow(t->a, t->b, &result);
} else {
/* This disables the macro that tries to use a gcc/clang
* builtin, so we test the fallback implementation instead. */
status = (SDL_size_mul_overflow)(t->a, t->b, &result);
}
if (t->status == 0) {
SDLTest_AssertCheck(status == 0,
"(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should succeed",
t->a, t->b);
SDLTest_AssertCheck(result == t->result,
"(%" SIZE_FORMAT " * %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
t->a, t->b, t->result, result);
} else {
SDLTest_AssertCheck(status == -1,
"(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should fail",
t->a, t->b);
}
if (t->a == t->b) {
continue;
}
result = ~t->result;
if (useBuiltin) {
status = SDL_size_mul_overflow(t->b, t->a, &result);
} else {
status = (SDL_size_mul_overflow)(t->b, t->a, &result);
}
if (t->status == 0) {
SDLTest_AssertCheck(status == 0,
"(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should succeed",
t->b, t->a);
SDLTest_AssertCheck(result == t->result,
"(%" SIZE_FORMAT " * %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
t->b, t->a, t->result, result);
} else {
SDLTest_AssertCheck(status == -1,
"(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should fail",
t->b, t->a);
}
}
for (i = 0; i < SDL_arraysize(additions); i++) {
const overflow_test *t = &additions[i];
int status;
size_t result = ~t->result;
if (useBuiltin) {
status = SDL_size_add_overflow(t->a, t->b, &result);
} else {
status = (SDL_size_add_overflow)(t->a, t->b, &result);
}
if (t->status == 0) {
SDLTest_AssertCheck(status == 0,
"(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should succeed",
t->a, t->b);
SDLTest_AssertCheck(result == t->result,
"(%" SIZE_FORMAT " + %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
t->a, t->b, t->result, result);
} else {
SDLTest_AssertCheck(status == -1,
"(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should fail",
t->a, t->b);
}
if (t->a == t->b) {
continue;
}
result = ~t->result;
if (useBuiltin) {
status = SDL_size_add_overflow(t->b, t->a, &result);
} else {
status = (SDL_size_add_overflow)(t->b, t->a, &result);
}
if (t->status == 0) {
SDLTest_AssertCheck(status == 0,
"(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should succeed",
t->b, t->a);
SDLTest_AssertCheck(result == t->result,
"(%" SIZE_FORMAT " + %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
t->b, t->a, t->result, result);
} else {
SDLTest_AssertCheck(status == -1,
"(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should fail",
t->b, t->a);
}
}
}
return TEST_COMPLETED;
}
/* ================= Test References ================== */
/* Standard C routine test cases */
static const SDLTest_TestCaseReference stdlibTest1 = {
(SDLTest_TestCaseFp)stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED
};
static const SDLTest_TestCaseReference stdlibTest2 = {
(SDLTest_TestCaseFp)stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED
};
static const SDLTest_TestCaseReference stdlibTest3 = {
(SDLTest_TestCaseFp)stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_getenv and SDL_setenv", TEST_ENABLED
};
static const SDLTest_TestCaseReference stdlibTest4 = {
(SDLTest_TestCaseFp)stdlib_sscanf, "stdlib_sscanf", "Call to SDL_sscanf", TEST_ENABLED
};
static const SDLTest_TestCaseReference stdlibTestOverflow = {
stdlib_overflow, "stdlib_overflow", "Overflow detection", TEST_ENABLED
};
/* Sequence of Standard C routine test cases */
static const SDLTest_TestCaseReference *stdlibTests[] = {
&stdlibTest1,
&stdlibTest2,
&stdlibTest3,
&stdlibTest4,
&stdlibTestOverflow,
NULL
};
/* Standard C routine test suite (global) */
SDLTest_TestSuiteReference stdlibTestSuite = {
"Stdlib",
NULL,
stdlibTests,
NULL
};