Atlas - testautomation_iostream.c
Home / ext / SDL / test Lines: 1 | Size: 34755 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1 2/** 3 * Automated SDL_IOStream test. 4 * 5 * Original code written by Edgar Simo "bobbens" 6 * Ported by Markus Kauppila ([email protected]) 7 * Updated and extended for SDL_test by aschiffler at ferzkopp dot net 8 * 9 * Released under Public Domain. 10 */ 11 12/* quiet windows compiler warnings */ 13#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) 14#define _CRT_SECURE_NO_WARNINGS 15#endif 16 17#include <stdio.h> 18 19#include <SDL3/SDL.h> 20#include <SDL3/SDL_test.h> 21#include "testautomation_suites.h" 22 23/* ================= Test Case Implementation ================== */ 24 25static const char *IOStreamReadTestFilename = "iostrm_read"; 26static const char *IOStreamWriteTestFilename = "iostrm_write"; 27static const char *IOStreamAlphabetFilename = "iostrm_alphabet"; 28 29static const char IOStreamHelloWorldTestString[] = "Hello World!"; 30static const char IOStreamHelloWorldCompString[] = "Hello World!"; 31static const char IOStreamAlphabetString[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 32 33/* Fixture */ 34 35static void SDLCALL IOStreamSetUp(void **arg) 36{ 37 size_t fileLen; 38 SDL_IOStream *handle; 39 size_t writtenLen; 40 bool result; 41 42 /* Clean up from previous runs (if any); ignore errors */ 43 SDL_RemovePath(IOStreamReadTestFilename); 44 SDL_RemovePath(IOStreamWriteTestFilename); 45 SDL_RemovePath(IOStreamAlphabetFilename); 46 47 /* Create a test file */ 48 handle = SDL_IOFromFile(IOStreamReadTestFilename, "w"); 49 SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", IOStreamReadTestFilename); 50 if (handle == NULL) { 51 return; 52 } 53 54 /* Write some known text into it */ 55 fileLen = SDL_strlen(IOStreamHelloWorldTestString); 56 writtenLen = SDL_WriteIO(handle, IOStreamHelloWorldTestString, fileLen); 57 SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", (int)fileLen, (int)writtenLen); 58 result = SDL_CloseIO(handle); 59 SDLTest_AssertCheck(result == true, "Verify result from SDL_CloseIO, expected true, got %s", result ? "true" : "false"); 60 61 /* Create a second test file */ 62 handle = SDL_IOFromFile(IOStreamAlphabetFilename, "w"); 63 SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", IOStreamAlphabetFilename); 64 if (handle == NULL) { 65 return; 66 } 67 68 /* Write alphabet text into it */ 69 fileLen = SDL_strlen(IOStreamAlphabetString); 70 writtenLen = SDL_WriteIO(handle, IOStreamAlphabetString, fileLen); 71 SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", (int)fileLen, (int)writtenLen); 72 result = SDL_CloseIO(handle); 73 SDLTest_AssertCheck(result == true, "Verify result from SDL_CloseIO, expected true, got %s", result ? "true" : "false"); 74 75 SDLTest_AssertPass("Creation of test file completed"); 76} 77 78static void SDLCALL IOStreamTearDown(void *arg) 79{ 80 int result; 81 82 /* Remove the created files to clean up; ignore errors for write filename */ 83 result = remove(IOStreamReadTestFilename); 84 SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", IOStreamReadTestFilename, result); 85 (void)remove(IOStreamWriteTestFilename); 86 result = remove(IOStreamAlphabetFilename); 87 SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", IOStreamAlphabetFilename, result); 88 89 SDLTest_AssertPass("Cleanup of test files completed"); 90} 91 92/** 93 * Makes sure parameters work properly. Local helper function. 94 * 95 * \sa SDL_SeekIO 96 * \sa SDL_ReadIO 97 */ 98static void testGenericIOStreamValidations(SDL_IOStream *rw, bool write) 99{ 100 char buf[sizeof(IOStreamHelloWorldTestString)]; 101 Sint64 i; 102 size_t s; 103 int seekPos = SDLTest_RandomIntegerInRange(4, 8); 104 105 /* Clear buffer */ 106 SDL_zeroa(buf); 107 108 /* Set to start. */ 109 i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET); 110 SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); 111 SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i); 112 113 /* Test write */ 114 s = SDL_WriteIO(rw, IOStreamHelloWorldTestString, sizeof(IOStreamHelloWorldTestString) - 1); 115 SDLTest_AssertPass("Call to SDL_WriteIO succeeded"); 116 if (write) { 117 SDLTest_AssertCheck(s == sizeof(IOStreamHelloWorldTestString) - 1, "Verify result of writing with SDL_WriteIO, expected %i, got %i", (int)sizeof(IOStreamHelloWorldTestString) - 1, (int)s); 118 } else { 119 SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_WriteIO, expected: 0, got %i", (int)s); 120 } 121 122 /* Test seek to random position */ 123 i = SDL_SeekIO(rw, seekPos, SDL_IO_SEEK_SET); 124 SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); 125 SDLTest_AssertCheck(i == (Sint64)seekPos, "Verify seek to %i with SDL_SeekIO (SDL_IO_SEEK_SET), expected %i, got %" SDL_PRIs64, seekPos, seekPos, i); 126 127 /* Test seek back to start */ 128 i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET); 129 SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); 130 SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i); 131 132 /* Test read */ 133 s = SDL_ReadIO(rw, buf, sizeof(IOStreamHelloWorldTestString) - 1); 134 SDLTest_AssertPass("Call to SDL_ReadIO succeeded"); 135 SDLTest_AssertCheck( 136 s == (sizeof(IOStreamHelloWorldTestString) - 1), 137 "Verify result from SDL_ReadIO, expected %i, got %i", 138 (int)(sizeof(IOStreamHelloWorldTestString) - 1), 139 (int)s); 140 SDLTest_AssertCheck( 141 SDL_memcmp(buf, IOStreamHelloWorldTestString, sizeof(IOStreamHelloWorldTestString) - 1) == 0, 142 "Verify read bytes match expected string, expected '%s', got '%s'", IOStreamHelloWorldTestString, buf); 143 144 /* Test seek back to start */ 145 i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET); 146 SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); 147 SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i); 148 149 /* Test printf */ 150 s = SDL_IOprintf(rw, "%s", IOStreamHelloWorldTestString); 151 SDLTest_AssertPass("Call to SDL_IOprintf succeeded"); 152 if (write) { 153 SDLTest_AssertCheck(s == sizeof(IOStreamHelloWorldTestString) - 1, "Verify result of writing with SDL_IOprintf, expected %i, got %i", (int)sizeof(IOStreamHelloWorldTestString) - 1, (int)s); 154 } else { 155 SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_WriteIO, expected: 0, got %i", (int)s); 156 } 157 158 /* Test seek back to start */ 159 i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET); 160 SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); 161 SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i); 162 163 /* Test read */ 164 s = SDL_ReadIO(rw, buf, sizeof(IOStreamHelloWorldTestString) - 1); 165 SDLTest_AssertPass("Call to SDL_ReadIO succeeded"); 166 SDLTest_AssertCheck( 167 s == (sizeof(IOStreamHelloWorldTestString) - 1), 168 "Verify result from SDL_ReadIO, expected %i, got %i", 169 (int)(sizeof(IOStreamHelloWorldTestString) - 1), 170 (int)s); 171 SDLTest_AssertCheck( 172 SDL_memcmp(buf, IOStreamHelloWorldTestString, sizeof(IOStreamHelloWorldTestString) - 1) == 0, 173 "Verify read bytes match expected string, expected '%s', got '%s'", IOStreamHelloWorldTestString, buf); 174 175 /* More seek tests. */ 176 i = SDL_SeekIO(rw, -4, SDL_IO_SEEK_CUR); 177 SDLTest_AssertPass("Call to SDL_SeekIO(...,-4,SDL_IO_SEEK_CUR) succeeded"); 178 SDLTest_AssertCheck( 179 i == (Sint64)(sizeof(IOStreamHelloWorldTestString) - 5), 180 "Verify seek to -4 with SDL_SeekIO (SDL_IO_SEEK_CUR), expected %i, got %i", 181 (int)(sizeof(IOStreamHelloWorldTestString) - 5), 182 (int)i); 183 184 i = SDL_SeekIO(rw, -1, SDL_IO_SEEK_END); 185 SDLTest_AssertPass("Call to SDL_SeekIO(...,-1,SDL_IO_SEEK_END) succeeded"); 186 SDLTest_AssertCheck( 187 i == (Sint64)(sizeof(IOStreamHelloWorldTestString) - 2), 188 "Verify seek to -1 with SDL_SeekIO (SDL_IO_SEEK_END), expected %i, got %i", 189 (int)(sizeof(IOStreamHelloWorldTestString) - 2), 190 (int)i); 191 192 /* Invalid whence seek */ 193 i = SDL_SeekIO(rw, 0, (SDL_IOWhence)999); 194 SDLTest_AssertPass("Call to SDL_SeekIO(...,0,invalid_whence) succeeded"); 195 SDLTest_AssertCheck( 196 i == (Sint64)(-1), 197 "Verify seek with SDL_SeekIO (invalid_whence); expected: -1, got %i", 198 (int)i); 199} 200 201/** 202 * Makes sure parameters work properly. Local helper function. 203 * 204 * \sa SDL_SeekIO 205 * \sa SDL_ReadIO 206 */ 207static void testEmptyIOStreamValidations(SDL_IOStream *rw, bool write) 208{ 209 char con[sizeof(IOStreamHelloWorldTestString)]; 210 char buf[sizeof(IOStreamHelloWorldTestString)]; 211 Sint64 i; 212 size_t s; 213 int seekPos = SDLTest_RandomIntegerInRange(4, 8); 214 215 /* Clear control & buffer */ 216 SDL_zeroa(con); 217 SDL_zeroa(buf); 218 219 /* Set to start. */ 220 i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET); 221 SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); 222 SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i); 223 224 /* Test write */ 225 s = SDL_WriteIO(rw, IOStreamHelloWorldTestString, sizeof(IOStreamHelloWorldTestString) - 1); 226 SDLTest_AssertPass("Call to SDL_WriteIO succeeded"); 227 if (write) { 228 SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_WriteIO, expected 0, got %i", (int)s); 229 } else { 230 SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_WriteIO, expected 0, got %i", (int)s); 231 } 232 233 /* Test seek to random position */ 234 i = SDL_SeekIO(rw, seekPos, SDL_IO_SEEK_SET); 235 SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); 236 SDLTest_AssertCheck(i == 0, "Verify seek to %i with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, seekPos, i); 237 238 /* Test seek back to start */ 239 i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET); 240 SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); 241 SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i); 242 243 /* Test read */ 244 s = SDL_ReadIO(rw, buf, sizeof(IOStreamHelloWorldTestString) - 1); 245 SDLTest_AssertPass("Call to SDL_ReadIO succeeded"); 246 SDLTest_AssertCheck(s == 0, "Verify result from SDL_ReadIO, expected 0, got %i", (int)s); 247 SDLTest_AssertCheck( 248 SDL_memcmp(buf, con, sizeof(IOStreamHelloWorldTestString) - 1) == 0, 249 "Verify that buffer remains unchanged, expected '%s', got '%s'", con, buf); 250 251 /* Test seek back to start */ 252 i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET); 253 SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); 254 SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i); 255 256 /* Test printf */ 257 s = SDL_IOprintf(rw, "%s", IOStreamHelloWorldTestString); 258 SDLTest_AssertPass("Call to SDL_IOprintf succeeded"); 259 if (write) { 260 SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_IOprintf, expected 0, got %i", (int)s); 261 } else { 262 SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_WriteIO, expected 0, got %i", (int)s); 263 } 264 265 /* Test seek back to start */ 266 i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET); 267 SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); 268 SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i); 269 270 /* Test read */ 271 s = SDL_ReadIO(rw, buf, sizeof(IOStreamHelloWorldTestString) - 1); 272 SDLTest_AssertPass("Call to SDL_ReadIO succeeded"); 273 SDLTest_AssertCheck( 274 s == 0, 275 "Verify result from SDL_ReadIO, expected 0, got %i", 276 (int)s); 277 SDLTest_AssertCheck( 278 SDL_memcmp(buf, con, sizeof(IOStreamHelloWorldTestString) - 1) == 0, 279 "Verify that buffer remains unchanged, expected '%s', got '%s'", con, buf); 280 281 /* More seek tests. */ 282 i = SDL_SeekIO(rw, -4, SDL_IO_SEEK_CUR); 283 SDLTest_AssertPass("Call to SDL_SeekIO(...,-4,SDL_IO_SEEK_CUR) succeeded"); 284 SDLTest_AssertCheck( 285 i == 0, 286 "Verify seek to -4 with SDL_SeekIO (SDL_IO_SEEK_CUR), expected 0, got %i", 287 (int)i); 288 289 i = SDL_SeekIO(rw, -1, SDL_IO_SEEK_END); 290 SDLTest_AssertPass("Call to SDL_SeekIO(...,-1,SDL_IO_SEEK_END) succeeded"); 291 SDLTest_AssertCheck( 292 i == 0, 293 "Verify seek to -1 with SDL_SeekIO (SDL_IO_SEEK_END), expected 0, got %i", 294 (int)i); 295 296 /* Invalid whence seek */ 297 i = SDL_SeekIO(rw, 0, (SDL_IOWhence)999); 298 SDLTest_AssertPass("Call to SDL_SeekIO(...,0,invalid_whence) succeeded"); 299 SDLTest_AssertCheck( 300 i == (Sint64)(-1), 301 "Verify seek with SDL_SeekIO (invalid_whence); expected: -1, got %i", 302 (int)i); 303} 304 305/** 306 * Negative test for SDL_IOFromFile parameters 307 * 308 * \sa SDL_IOFromFile 309 * 310 */ 311static int SDLCALL iostrm_testParamNegative(void *arg) 312{ 313 SDL_IOStream *iostrm; 314 315 /* These should all fail. */ 316 iostrm = SDL_IOFromFile(NULL, NULL); 317 SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, NULL) succeeded"); 318 SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, NULL) returns NULL"); 319 320 iostrm = SDL_IOFromFile(NULL, "ab+"); 321 SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, \"ab+\") succeeded"); 322 SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, \"ab+\") returns NULL"); 323 324 iostrm = SDL_IOFromFile(NULL, "sldfkjsldkfj"); 325 SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, \"sldfkjsldkfj\") succeeded"); 326 SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, \"sldfkjsldkfj\") returns NULL"); 327 328 iostrm = SDL_IOFromFile("something", ""); 329 SDLTest_AssertPass("Call to SDL_IOFromFile(\"something\", \"\") succeeded"); 330 SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(\"something\", \"\") returns NULL"); 331 332 iostrm = SDL_IOFromFile("something", NULL); 333 SDLTest_AssertPass("Call to SDL_IOFromFile(\"something\", NULL) succeeded"); 334 SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(\"something\", NULL) returns NULL"); 335 336 iostrm = SDL_IOFromMem(NULL, 10); 337 SDLTest_AssertPass("Call to SDL_IOFromMem(NULL, 10) succeeded"); 338 SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromMem(NULL, 10) returns NULL"); 339 340 return TEST_COMPLETED; 341} 342 343/** 344 * Tests opening from memory. 345 * 346 * \sa SDL_IOFromMem 347 * \sa SDL_CloseIO 348 */ 349static int SDLCALL iostrm_testMem(void *arg) 350{ 351 char mem[sizeof(IOStreamHelloWorldTestString)]; 352 SDL_IOStream *rw; 353 int result; 354 355 /* Clear buffer */ 356 SDL_zeroa(mem); 357 358 /* Open */ 359 rw = SDL_IOFromMem(mem, sizeof(IOStreamHelloWorldTestString) - 1); 360 SDLTest_AssertPass("Call to SDL_IOFromMem() succeeded"); 361 SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromMem does not return NULL"); 362 363 /* Bail out if NULL */ 364 if (rw == NULL) { 365 return TEST_ABORTED; 366 } 367 368 /* Run generic tests */ 369 testGenericIOStreamValidations(rw, true); 370 371 /* Close */ 372 result = SDL_CloseIO(rw); 373 SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); 374 SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); 375 376 return TEST_COMPLETED; 377} 378 379/** 380 * Tests opening from memory. 381 * 382 * \sa SDL_IOFromConstMem 383 * \sa SDL_CloseIO 384 */ 385static int SDLCALL iostrm_testConstMem(void *arg) 386{ 387 SDL_IOStream *rw; 388 int result; 389 390 /* Open handle */ 391 rw = SDL_IOFromConstMem(IOStreamHelloWorldCompString, sizeof(IOStreamHelloWorldCompString) - 1); 392 SDLTest_AssertPass("Call to SDL_IOFromConstMem() succeeded"); 393 SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromConstMem does not return NULL"); 394 395 /* Bail out if NULL */ 396 if (rw == NULL) { 397 return TEST_ABORTED; 398 } 399 400 /* Run generic tests */ 401 testGenericIOStreamValidations(rw, false); 402 403 /* Close handle */ 404 result = SDL_CloseIO(rw); 405 SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); 406 SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); 407 408 return TEST_COMPLETED; 409} 410 411/** 412 * Tests opening nothing. 413 * 414 * \sa SDL_IOFromMem 415 * \sa SDL_CloseIO 416 */ 417static int SDLCALL iostrm_testMemEmpty(void *arg) 418{ 419 char mem[sizeof(IOStreamHelloWorldTestString)]; 420 SDL_IOStream *rw; 421 int result; 422 423 /* Clear buffer */ 424 SDL_zeroa(mem); 425 426 /* Open empty */ 427 rw = SDL_IOFromMem(mem, 0); 428 SDLTest_AssertPass("Call to SDL_IOFromMem() succeeded"); 429 SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromMem does not return NULL"); 430 431 /* Bail out if NULL */ 432 if (rw == NULL) { 433 return TEST_ABORTED; 434 } 435 436 /* Run generic tests */ 437 testEmptyIOStreamValidations(rw, true); 438 439 /* Close */ 440 result = SDL_CloseIO(rw); 441 SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); 442 SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); 443 444 return TEST_COMPLETED; 445} 446 447/** 448 * Tests opening nothing. 449 * 450 * \sa SDL_IOFromMem 451 * \sa SDL_CloseIO 452 */ 453static int SDLCALL iostrm_testConstMemEmpty(void *arg) 454{ 455 SDL_IOStream *rw; 456 int result; 457 458 /* Open handle */ 459 rw = SDL_IOFromConstMem(IOStreamHelloWorldCompString, 0); 460 SDLTest_AssertPass("Call to SDL_IOFromConstMem() succeeded"); 461 SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromConstMem does not return NULL"); 462 463 /* Bail out if NULL */ 464 if (rw == NULL) { 465 return TEST_ABORTED; 466 } 467 468 /* Run generic tests */ 469 testEmptyIOStreamValidations(rw, false); 470 471 /* Close handle */ 472 result = SDL_CloseIO(rw); 473 SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); 474 SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); 475 476 return TEST_COMPLETED; 477} 478 479static int free_call_count; 480void SDLCALL test_free(void* mem) { 481 free_call_count++; 482 SDL_free(mem); 483} 484 485static int SDLCALL iostrm_testMemWithFree(void *arg) 486{ 487 void *mem; 488 SDL_IOStream *rw; 489 int result; 490 491 /* Allocate some memory */ 492 mem = SDL_malloc(sizeof(IOStreamHelloWorldCompString) - 1); 493 if (mem == NULL) { 494 return TEST_ABORTED; 495 } 496 497 /* Open handle */ 498 rw = SDL_IOFromMem(mem, sizeof(IOStreamHelloWorldCompString) - 1); 499 SDLTest_AssertPass("Call to SDL_IOFromMem() succeeded"); 500 SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromMem does not return NULL"); 501 502 /* Bail out if NULL */ 503 if (rw == NULL) { 504 return TEST_ABORTED; 505 } 506 507 /* Set the free function */ 508 free_call_count = 0; 509 result = SDL_SetPointerProperty(SDL_GetIOProperties(rw), SDL_PROP_IOSTREAM_MEMORY_FREE_FUNC_POINTER, test_free); 510 SDLTest_AssertPass("Call to SDL_SetPointerProperty() succeeded"); 511 SDLTest_AssertCheck(result == true, "Verify result value is true; got %d", result); 512 513 /* Run generic tests */ 514 testGenericIOStreamValidations(rw, true); 515 516 /* Close handle */ 517 result = SDL_CloseIO(rw); 518 SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); 519 SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); 520 SDLTest_AssertCheck(free_call_count == 1, "Verify the custom free function was called once; call count: %d", free_call_count); 521 522 return TEST_COMPLETED; 523} 524 525/** 526 * Tests dynamic memory 527 * 528 * \sa SDL_IOFromDynamicMem 529 * \sa SDL_CloseIO 530 */ 531static int SDLCALL iostrm_testDynamicMem(void *arg) 532{ 533 SDL_IOStream *rw; 534 SDL_PropertiesID props; 535 char *mem; 536 int result; 537 538 /* Open */ 539 rw = SDL_IOFromDynamicMem(); 540 SDLTest_AssertPass("Call to SDL_IOFromDynamicMem() succeeded"); 541 SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromDynamicMem does not return NULL"); 542 543 /* Bail out if NULL */ 544 if (rw == NULL) { 545 return TEST_ABORTED; 546 } 547 548 /* Set the chunk size to 1 byte */ 549 props = SDL_GetIOProperties(rw); 550 SDL_SetNumberProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER, 1); 551 552 /* Run generic tests */ 553 testGenericIOStreamValidations(rw, true); 554 555 /* Get the dynamic memory and verify it */ 556 mem = (char *)SDL_GetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL); 557 SDLTest_AssertPass("Call to SDL_GetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL) succeeded"); 558 SDLTest_AssertCheck(mem != NULL, "Verify memory value is not NULL"); 559 mem[SDL_GetIOSize(rw)] = '\0'; 560 SDLTest_AssertCheck(SDL_strcmp(mem, IOStreamHelloWorldTestString) == 0, "Verify memory value is correct"); 561 562 /* Take the memory and free it ourselves */ 563 SDL_SetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL); 564 SDL_free(mem); 565 566 /* Close */ 567 result = SDL_CloseIO(rw); 568 SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); 569 SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); 570 571 return TEST_COMPLETED; 572} 573 574/** 575 * Tests reading from file. 576 * 577 * \sa SDL_IOFromFile 578 * \sa SDL_CloseIO 579 */ 580static int SDLCALL iostrm_testFileRead(void *arg) 581{ 582 SDL_IOStream *rw; 583 int result; 584 585 /* Read test. */ 586 rw = SDL_IOFromFile(IOStreamReadTestFilename, "r"); 587 SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"r\") succeeded"); 588 SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in read mode does not return NULL"); 589 590 /* Bail out if NULL */ 591 if (rw == NULL) { 592 return TEST_ABORTED; 593 } 594 595 /* Run generic tests */ 596 testGenericIOStreamValidations(rw, false); 597 598 /* Close handle */ 599 result = SDL_CloseIO(rw); 600 SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); 601 SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); 602 603 return TEST_COMPLETED; 604} 605 606/** 607 * Tests writing from file. 608 * 609 * \sa SDL_IOFromFile 610 * \sa SDL_CloseIO 611 */ 612static int SDLCALL iostrm_testFileWrite(void *arg) 613{ 614 SDL_IOStream *rw; 615 int result; 616 617 /* Write test. */ 618 rw = SDL_IOFromFile(IOStreamWriteTestFilename, "w+x"); 619 SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"w+x\") succeeded"); 620 SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in exclusive write mode does not return NULL"); 621 622 /* Bail out if NULL */ 623 if (rw == NULL) { 624 return TEST_ABORTED; 625 } 626 627 /* Run generic tests */ 628 testGenericIOStreamValidations(rw, true); 629 630 /* Close handle */ 631 result = SDL_CloseIO(rw); 632 SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); 633 SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); 634 635 /* Exclusively opening an existing file should fail. */ 636 rw = SDL_IOFromFile(IOStreamWriteTestFilename, "wx"); 637 SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"wx\") succeeded"); 638 SDLTest_AssertCheck(rw == NULL, "Verify opening existing file with SDL_IOFromFile in exclusive write mode returns NULL"); 639 640 return TEST_COMPLETED; 641} 642 643/** 644 * Tests alloc and free RW context. 645 * 646 * \sa SDL_OpenIO 647 * \sa SDL_CloseIO 648 */ 649static int SDLCALL iostrm_testAllocFree(void *arg) 650{ 651 /* Allocate context */ 652 SDL_IOStreamInterface iface; 653 SDL_IOStream *rw; 654 655 SDL_INIT_INTERFACE(&iface); 656 rw = SDL_OpenIO(&iface, NULL); 657 SDLTest_AssertPass("Call to SDL_OpenIO() succeeded"); 658 SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_OpenIO() is not NULL"); 659 if (rw == NULL) { 660 return TEST_ABORTED; 661 } 662 663 /* Free context again */ 664 SDL_CloseIO(rw); 665 SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); 666 667 return TEST_COMPLETED; 668} 669 670/** 671 * Compare memory and file reads 672 * 673 * \sa SDL_IOFromMem 674 * \sa SDL_IOFromFile 675 */ 676static int SDLCALL iostrm_testCompareRWFromMemWithRWFromFile(void *arg) 677{ 678 int slen = 26; 679 char buffer_file[27]; 680 char buffer_mem[27]; 681 size_t rv_file; 682 size_t rv_mem; 683 Uint64 sv_file; 684 Uint64 sv_mem; 685 SDL_IOStream *iostrm_file; 686 SDL_IOStream *iostrm_mem; 687 int size; 688 int result; 689 690 for (size = 5; size < 10; size++) { 691 /* Terminate buffer */ 692 buffer_file[slen] = 0; 693 buffer_mem[slen] = 0; 694 695 /* Read/seek from memory */ 696 iostrm_mem = SDL_IOFromMem((void *)IOStreamAlphabetString, slen); 697 SDLTest_AssertPass("Call to SDL_IOFromMem()"); 698 rv_mem = SDL_ReadIO(iostrm_mem, buffer_mem, size * 6); 699 SDLTest_AssertPass("Call to SDL_ReadIO(mem, size=%d)", size * 6); 700 sv_mem = SDL_SeekIO(iostrm_mem, 0, SEEK_END); 701 SDLTest_AssertPass("Call to SDL_SeekIO(mem,SEEK_END)"); 702 result = SDL_CloseIO(iostrm_mem); 703 SDLTest_AssertPass("Call to SDL_CloseIO(mem)"); 704 SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); 705 706 /* Read/see from file */ 707 iostrm_file = SDL_IOFromFile(IOStreamAlphabetFilename, "r"); 708 SDLTest_AssertPass("Call to SDL_IOFromFile()"); 709 rv_file = SDL_ReadIO(iostrm_file, buffer_file, size * 6); 710 SDLTest_AssertPass("Call to SDL_ReadIO(file, size=%d)", size * 6); 711 sv_file = SDL_SeekIO(iostrm_file, 0, SEEK_END); 712 SDLTest_AssertPass("Call to SDL_SeekIO(file,SEEK_END)"); 713 result = SDL_CloseIO(iostrm_file); 714 SDLTest_AssertPass("Call to SDL_CloseIO(file)"); 715 SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); 716 717 /* Compare */ 718 SDLTest_AssertCheck(rv_mem == rv_file, "Verify returned read blocks matches for mem and file reads; got: rv_mem=%d rv_file=%d", (int)rv_mem, (int)rv_file); 719 SDLTest_AssertCheck(sv_mem == sv_file, "Verify SEEK_END position matches for mem and file seeks; got: sv_mem=%d sv_file=%d", (int)sv_mem, (int)sv_file); 720 SDLTest_AssertCheck(buffer_mem[slen] == 0, "Verify mem buffer termination; expected: 0, got: %d", buffer_mem[slen]); 721 SDLTest_AssertCheck(buffer_file[slen] == 0, "Verify file buffer termination; expected: 0, got: %d", buffer_file[slen]); 722 SDLTest_AssertCheck( 723 SDL_strncmp(buffer_mem, IOStreamAlphabetString, slen) == 0, 724 "Verify mem buffer contain alphabet string; expected: %s, got: %s", IOStreamAlphabetString, buffer_mem); 725 SDLTest_AssertCheck( 726 SDL_strncmp(buffer_file, IOStreamAlphabetString, slen) == 0, 727 "Verify file buffer contain alphabet string; expected: %s, got: %s", IOStreamAlphabetString, buffer_file); 728 } 729 730 return TEST_COMPLETED; 731} 732 733/** 734 * Tests writing and reading from file using endian aware functions. 735 * 736 * \sa SDL_IOFromFile 737 * \sa SDL_CloseIO 738 * \sa SDL_ReadU16BE 739 * \sa SDL_WriteU16BE 740 */ 741static int SDLCALL iostrm_testFileWriteReadEndian(void *arg) 742{ 743 SDL_IOStream *rw; 744 Sint64 result; 745 int mode; 746 Uint16 BE16value; 747 Uint32 BE32value; 748 Uint64 BE64value; 749 Uint16 LE16value; 750 Uint32 LE32value; 751 Uint64 LE64value; 752 Uint16 BE16test; 753 Uint32 BE32test; 754 Uint64 BE64test; 755 Uint16 LE16test; 756 Uint32 LE32test; 757 Uint64 LE64test; 758 bool bresult; 759 int cresult; 760 761 for (mode = 0; mode < 3; mode++) { 762 763 /* Create test data */ 764 switch (mode) { 765 default: 766 case 0: 767 SDLTest_Log("All 0 values"); 768 BE16value = 0; 769 BE32value = 0; 770 BE64value = 0; 771 LE16value = 0; 772 LE32value = 0; 773 LE64value = 0; 774 break; 775 case 1: 776 SDLTest_Log("All 1 values"); 777 BE16value = 1; 778 BE32value = 1; 779 BE64value = 1; 780 LE16value = 1; 781 LE32value = 1; 782 LE64value = 1; 783 break; 784 case 2: 785 SDLTest_Log("Random values"); 786 BE16value = SDLTest_RandomUint16(); 787 BE32value = SDLTest_RandomUint32(); 788 BE64value = SDLTest_RandomUint64(); 789 LE16value = SDLTest_RandomUint16(); 790 LE32value = SDLTest_RandomUint32(); 791 LE64value = SDLTest_RandomUint64(); 792 break; 793 } 794 795 /* Write test. */ 796 rw = SDL_IOFromFile(IOStreamWriteTestFilename, "w+"); 797 SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"w+\")"); 798 SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in write mode does not return NULL"); 799 800 /* Bail out if NULL */ 801 if (rw == NULL) { 802 return TEST_ABORTED; 803 } 804 805 /* Write test data */ 806 bresult = SDL_WriteU16BE(rw, BE16value); 807 SDLTest_AssertPass("Call to SDL_WriteU16BE()"); 808 SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false"); 809 bresult = SDL_WriteU32BE(rw, BE32value); 810 SDLTest_AssertPass("Call to SDL_WriteU32BE()"); 811 SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false"); 812 bresult = SDL_WriteU64BE(rw, BE64value); 813 SDLTest_AssertPass("Call to SDL_WriteU64BE()"); 814 SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false"); 815 bresult = SDL_WriteU16LE(rw, LE16value); 816 SDLTest_AssertPass("Call to SDL_WriteU16LE()"); 817 SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false"); 818 bresult = SDL_WriteU32LE(rw, LE32value); 819 SDLTest_AssertPass("Call to SDL_WriteU32LE()"); 820 SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false"); 821 bresult = SDL_WriteU64LE(rw, LE64value); 822 SDLTest_AssertPass("Call to SDL_WriteU64LE()"); 823 SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false"); 824 825 /* Test seek to start */ 826 result = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET); 827 SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); 828 SDLTest_AssertCheck(result == 0, "Verify result from position 0 with SDL_SeekIO, expected 0, got %i", (int)result); 829 830 /* Read test data */ 831 bresult = SDL_ReadU16BE(rw, &BE16test); 832 SDLTest_AssertPass("Call to SDL_ReadU16BE()"); 833 SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false"); 834 SDLTest_AssertCheck(BE16test == BE16value, "Validate object read from SDL_ReadU16BE, expected: %hu, got: %hu", BE16value, BE16test); 835 bresult = SDL_ReadU32BE(rw, &BE32test); 836 SDLTest_AssertPass("Call to SDL_ReadU32BE()"); 837 SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false"); 838 SDLTest_AssertCheck(BE32test == BE32value, "Validate object read from SDL_ReadU32BE, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, BE32value, BE32test); 839 bresult = SDL_ReadU64BE(rw, &BE64test); 840 SDLTest_AssertPass("Call to SDL_ReadU64BE()"); 841 SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false"); 842 SDLTest_AssertCheck(BE64test == BE64value, "Validate object read from SDL_ReadU64BE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, BE64value, BE64test); 843 bresult = SDL_ReadU16LE(rw, &LE16test); 844 SDLTest_AssertPass("Call to SDL_ReadU16LE()"); 845 SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false"); 846 SDLTest_AssertCheck(LE16test == LE16value, "Validate object read from SDL_ReadU16LE, expected: %hu, got: %hu", LE16value, LE16test); 847 bresult = SDL_ReadU32LE(rw, &LE32test); 848 SDLTest_AssertPass("Call to SDL_ReadU32LE()"); 849 SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false"); 850 SDLTest_AssertCheck(LE32test == LE32value, "Validate object read from SDL_ReadU32LE, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, LE32value, LE32test); 851 bresult = SDL_ReadU64LE(rw, &LE64test); 852 SDLTest_AssertPass("Call to SDL_ReadU64LE()"); 853 SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false"); 854 SDLTest_AssertCheck(LE64test == LE64value, "Validate object read from SDL_ReadU64LE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, LE64value, LE64test); 855 856 /* Close handle */ 857 cresult = SDL_CloseIO(rw); 858 SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); 859 SDLTest_AssertCheck(cresult == true, "Verify result value is true; got: %d", cresult); 860 } 861 862 return TEST_COMPLETED; 863} 864 865/* ================= Test References ================== */ 866 867/* IOStream test cases */ 868static const SDLTest_TestCaseReference iostrmTest1 = { 869 iostrm_testParamNegative, "iostrm_testParamNegative", "Negative test for SDL_IOFromFile parameters", TEST_ENABLED 870}; 871 872static const SDLTest_TestCaseReference iostrmTest2 = { 873 iostrm_testMem, "iostrm_testMem", "Tests opening from memory", TEST_ENABLED 874}; 875 876static const SDLTest_TestCaseReference iostrmTest3 = { 877 iostrm_testConstMem, "iostrm_testConstMem", "Tests opening from (const) memory", TEST_ENABLED 878}; 879 880static const SDLTest_TestCaseReference iostrmTest4 = { 881 iostrm_testDynamicMem, "iostrm_testDynamicMem", "Tests opening dynamic memory", TEST_ENABLED 882}; 883 884static const SDLTest_TestCaseReference iostrmTest5 = { 885 iostrm_testFileRead, "iostrm_testFileRead", "Tests reading from a file", TEST_ENABLED 886}; 887 888static const SDLTest_TestCaseReference iostrmTest6 = { 889 iostrm_testFileWrite, "iostrm_testFileWrite", "Test writing to a file", TEST_ENABLED 890}; 891 892static const SDLTest_TestCaseReference iostrmTest7 = { 893 iostrm_testAllocFree, "iostrm_testAllocFree", "Test alloc and free of RW context", TEST_ENABLED 894}; 895 896static const SDLTest_TestCaseReference iostrmTest8 = { 897 iostrm_testFileWriteReadEndian, "iostrm_testFileWriteReadEndian", "Test writing and reading via the Endian aware functions", TEST_ENABLED 898}; 899 900static const SDLTest_TestCaseReference iostrmTest9 = { 901 iostrm_testCompareRWFromMemWithRWFromFile, "iostrm_testCompareRWFromMemWithRWFromFile", "Compare RWFromMem and RWFromFile IOStream for read and seek", TEST_ENABLED 902}; 903 904static const SDLTest_TestCaseReference iostrmTest10 = { 905 iostrm_testMemWithFree, "iostrm_testMemWithFree", "Tests opening from memory with free on close", TEST_ENABLED 906}; 907 908static const SDLTest_TestCaseReference iostrmTest11 = { 909 iostrm_testMemEmpty, "iostrm_testMemEmpty", "Tests opening empty memory stream", TEST_ENABLED 910}; 911 912static const SDLTest_TestCaseReference iostrmTest12 = { 913 iostrm_testConstMemEmpty, "iostrm_testConstMemEmpty", "Tests opening empty (const) memory stream", TEST_ENABLED 914}; 915 916/* Sequence of IOStream test cases */ 917static const SDLTest_TestCaseReference *iostrmTests[] = { 918 &iostrmTest1, &iostrmTest2, &iostrmTest3, &iostrmTest4, &iostrmTest5, &iostrmTest6, 919 &iostrmTest7, &iostrmTest8, &iostrmTest9, &iostrmTest10, &iostrmTest11, &iostrmTest12, NULL 920}; 921 922/* IOStream test suite (global) */ 923SDLTest_TestSuiteReference iostrmTestSuite = { 924 "IOStream", 925 IOStreamSetUp, 926 iostrmTests, 927 IOStreamTearDown 928}; 929[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.