cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

testautomation_rwops.c (26636B)


      1
      2/**
      3 * Automated SDL_RWops test.
      4 *
      5 * Original code written by Edgar Simo "bobbens"
      6 * Ported by Markus Kauppila (markus.kauppila@gmail.com)
      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#define _CRT_SECURE_NO_WARNINGS
     14
     15#include <stdio.h>
     16
     17#include "SDL.h"
     18#include "SDL_test.h"
     19
     20/* ================= Test Case Implementation ================== */
     21
     22const char* RWopsReadTestFilename = "rwops_read";
     23const char* RWopsWriteTestFilename = "rwops_write";
     24const char* RWopsAlphabetFilename = "rwops_alphabet";
     25
     26static const char RWopsHelloWorldTestString[] = "Hello World!";
     27static const char RWopsHelloWorldCompString[] = "Hello World!";
     28static const char RWopsAlphabetString[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     29
     30/* Fixture */
     31
     32void
     33RWopsSetUp(void *arg)
     34{
     35    int fileLen;
     36    FILE *handle;
     37    int writtenLen;
     38    int result;
     39
     40    /* Clean up from previous runs (if any); ignore errors */
     41    remove(RWopsReadTestFilename);
     42    remove(RWopsWriteTestFilename);
     43    remove(RWopsAlphabetFilename);
     44
     45    /* Create a test file */
     46    handle = fopen(RWopsReadTestFilename, "w");
     47    SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", RWopsReadTestFilename);
     48        if (handle == NULL) return;
     49
     50    /* Write some known text into it */
     51    fileLen = SDL_strlen(RWopsHelloWorldTestString);
     52    writtenLen = (int)fwrite(RWopsHelloWorldTestString, 1, fileLen, handle);
     53    SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", fileLen, writtenLen);
     54    result = fclose(handle);
     55    SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
     56
     57    /* Create a second test file */
     58    handle = fopen(RWopsAlphabetFilename, "w");
     59    SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", RWopsAlphabetFilename);
     60        if (handle == NULL) return;
     61
     62    /* Write alphabet text into it */
     63    fileLen = SDL_strlen(RWopsAlphabetString);
     64    writtenLen = (int)fwrite(RWopsAlphabetString, 1, fileLen, handle);
     65    SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", fileLen, writtenLen);
     66    result = fclose(handle);
     67    SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
     68
     69    SDLTest_AssertPass("Creation of test file completed");
     70}
     71
     72void
     73RWopsTearDown(void *arg)
     74{
     75    int result;
     76
     77    /* Remove the created files to clean up; ignore errors for write filename */
     78    result = remove(RWopsReadTestFilename);
     79    SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", RWopsReadTestFilename, result);
     80    remove(RWopsWriteTestFilename);
     81    result = remove(RWopsAlphabetFilename);
     82    SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", RWopsAlphabetFilename, result);
     83
     84    SDLTest_AssertPass("Cleanup of test files completed");
     85}
     86
     87/**
     88 * @brief Makes sure parameters work properly. Local helper function.
     89 *
     90 * \sa
     91 * http://wiki.libsdl.org/moin.cgi/SDL_RWseek
     92 * http://wiki.libsdl.org/moin.cgi/SDL_RWread
     93 */
     94void
     95_testGenericRWopsValidations(SDL_RWops *rw, int write)
     96{
     97   char buf[sizeof(RWopsHelloWorldTestString)];
     98   Sint64 i;
     99   size_t s;
    100   int seekPos = SDLTest_RandomIntegerInRange(4, 8);
    101
    102   /* Clear buffer */
    103   SDL_zero(buf);
    104
    105   /* Set to start. */
    106   i = SDL_RWseek(rw, 0, RW_SEEK_SET );
    107   SDLTest_AssertPass("Call to SDL_RWseek succeeded");
    108   SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %i", i);
    109
    110   /* Test write. */
    111   s = SDL_RWwrite(rw, RWopsHelloWorldTestString, sizeof(RWopsHelloWorldTestString)-1, 1);
    112   SDLTest_AssertPass("Call to SDL_RWwrite succeeded");
    113   if (write) {
    114        SDLTest_AssertCheck(s == (size_t)1, "Verify result of writing one byte with SDL_RWwrite, expected 1, got %i", s);
    115   }
    116   else {
    117        SDLTest_AssertCheck(s == (size_t)0, "Verify result of writing with SDL_RWwrite, expected: 0, got %i", s);
    118   }
    119
    120   /* Test seek to random position */
    121   i = SDL_RWseek( rw, seekPos, RW_SEEK_SET );
    122   SDLTest_AssertPass("Call to SDL_RWseek succeeded");
    123   SDLTest_AssertCheck(i == (Sint64)seekPos, "Verify seek to %i with SDL_RWseek (RW_SEEK_SET), expected %i, got %i", seekPos, seekPos, i);
    124
    125   /* Test seek back to start */
    126   i = SDL_RWseek(rw, 0, RW_SEEK_SET );
    127   SDLTest_AssertPass("Call to SDL_RWseek succeeded");
    128   SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %i", i);
    129
    130   /* Test read */
    131   s = SDL_RWread( rw, buf, 1, sizeof(RWopsHelloWorldTestString)-1 );
    132   SDLTest_AssertPass("Call to SDL_RWread succeeded");
    133   SDLTest_AssertCheck(
    134       s == (size_t)(sizeof(RWopsHelloWorldTestString)-1),
    135       "Verify result from SDL_RWread, expected %i, got %i",
    136       sizeof(RWopsHelloWorldTestString)-1,
    137       s);
    138   SDLTest_AssertCheck(
    139       SDL_memcmp(buf, RWopsHelloWorldTestString, sizeof(RWopsHelloWorldTestString)-1 ) == 0,
    140       "Verify read bytes match expected string, expected '%s', got '%s'", RWopsHelloWorldTestString, buf);
    141
    142   /* More seek tests. */
    143   i = SDL_RWseek( rw, -4, RW_SEEK_CUR );
    144   SDLTest_AssertPass("Call to SDL_RWseek(...,-4,RW_SEEK_CUR) succeeded");
    145   SDLTest_AssertCheck(
    146       i == (Sint64)(sizeof(RWopsHelloWorldTestString)-5),
    147       "Verify seek to -4 with SDL_RWseek (RW_SEEK_CUR), expected %i, got %i",
    148       sizeof(RWopsHelloWorldTestString)-5,
    149       i);
    150
    151   i = SDL_RWseek( rw, -1, RW_SEEK_END );
    152   SDLTest_AssertPass("Call to SDL_RWseek(...,-1,RW_SEEK_END) succeeded");
    153   SDLTest_AssertCheck(
    154       i == (Sint64)(sizeof(RWopsHelloWorldTestString)-2),
    155       "Verify seek to -1 with SDL_RWseek (RW_SEEK_END), expected %i, got %i",
    156       sizeof(RWopsHelloWorldTestString)-2,
    157       i);
    158
    159   /* Invalid whence seek */
    160   i = SDL_RWseek( rw, 0, 999 );
    161   SDLTest_AssertPass("Call to SDL_RWseek(...,0,invalid_whence) succeeded");
    162   SDLTest_AssertCheck(
    163       i == (Sint64)(-1),
    164       "Verify seek with SDL_RWseek (invalid_whence); expected: -1, got %i",
    165       i);
    166}
    167
    168/* !
    169 * Negative test for SDL_RWFromFile parameters
    170 *
    171 * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
    172 *
    173 */
    174int
    175rwops_testParamNegative (void)
    176{
    177   SDL_RWops *rwops;
    178
    179   /* These should all fail. */
    180   rwops = SDL_RWFromFile(NULL, NULL);
    181   SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, NULL) succeeded");
    182   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, NULL) returns NULL");
    183
    184   rwops = SDL_RWFromFile(NULL, "ab+");
    185   SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, \"ab+\") succeeded");
    186   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, \"ab+\") returns NULL");
    187
    188   rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
    189   SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, \"sldfkjsldkfj\") succeeded");
    190   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, \"sldfkjsldkfj\") returns NULL");
    191
    192   rwops = SDL_RWFromFile("something", "");
    193   SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", \"\") succeeded");
    194   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", \"\") returns NULL");
    195
    196   rwops = SDL_RWFromFile("something", NULL);
    197   SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", NULL) succeeded");
    198   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", NULL) returns NULL");
    199
    200   rwops = SDL_RWFromMem((void *)NULL, 10);
    201   SDLTest_AssertPass("Call to SDL_RWFromMem(NULL, 10) succeeded");
    202   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromMem(NULL, 10) returns NULL");
    203
    204   rwops = SDL_RWFromMem((void *)RWopsAlphabetString, 0);
    205   SDLTest_AssertPass("Call to SDL_RWFromMem(data, 0) succeeded");
    206   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromMem(data, 0) returns NULL");
    207
    208   rwops = SDL_RWFromConstMem((const void *)RWopsAlphabetString, 0);
    209   SDLTest_AssertPass("Call to SDL_RWFromConstMem(data, 0) succeeded");
    210   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromConstMem(data, 0) returns NULL");
    211
    212   return TEST_COMPLETED;
    213}
    214
    215/**
    216 * @brief Tests opening from memory.
    217 *
    218 * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromMem
    219 * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWClose
    220 */
    221int
    222rwops_testMem (void)
    223{
    224   char mem[sizeof(RWopsHelloWorldTestString)];
    225   SDL_RWops *rw;
    226   int result;
    227
    228   /* Clear buffer */
    229   SDL_zero(mem);
    230
    231   /* Open */
    232   rw = SDL_RWFromMem(mem, sizeof(RWopsHelloWorldTestString)-1);
    233   SDLTest_AssertPass("Call to SDL_RWFromMem() succeeded");
    234   SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromMem does not return NULL");
    235
    236   /* Bail out if NULL */
    237   if (rw == NULL) return TEST_ABORTED;
    238
    239   /* Check type */
    240   SDLTest_AssertCheck(rw->type == SDL_RWOPS_MEMORY, "Verify RWops type is SDL_RWOPS_MEMORY; expected: %d, got: %d", SDL_RWOPS_MEMORY, rw->type);
    241
    242   /* Run generic tests */
    243   _testGenericRWopsValidations(rw, 1);
    244
    245   /* Close */
    246   result = SDL_RWclose(rw);
    247   SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
    248   SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
    249
    250   return TEST_COMPLETED;
    251}
    252
    253
    254/**
    255 * @brief Tests opening from memory.
    256 *
    257 * \sa
    258 * http://wiki.libsdl.org/moin.cgi/SDL_RWFromConstMem
    259 * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
    260 */
    261int
    262rwops_testConstMem (void)
    263{
    264   SDL_RWops *rw;
    265   int result;
    266
    267   /* Open handle */
    268   rw = SDL_RWFromConstMem( RWopsHelloWorldCompString, sizeof(RWopsHelloWorldCompString)-1 );
    269   SDLTest_AssertPass("Call to SDL_RWFromConstMem() succeeded");
    270   SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromConstMem does not return NULL");
    271
    272   /* Bail out if NULL */
    273   if (rw == NULL) return TEST_ABORTED;
    274
    275   /* Check type */
    276   SDLTest_AssertCheck(rw->type == SDL_RWOPS_MEMORY_RO, "Verify RWops type is SDL_RWOPS_MEMORY_RO; expected: %d, got: %d", SDL_RWOPS_MEMORY_RO, rw->type);
    277
    278   /* Run generic tests */
    279   _testGenericRWopsValidations( rw, 0 );
    280
    281   /* Close handle */
    282   result = SDL_RWclose(rw);
    283   SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
    284   SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
    285
    286  return TEST_COMPLETED;
    287}
    288
    289
    290/**
    291 * @brief Tests reading from file.
    292 *
    293 * \sa
    294 * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
    295 * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
    296 */
    297int
    298rwops_testFileRead(void)
    299{
    300   SDL_RWops *rw;
    301   int result;
    302
    303   /* Read test. */
    304   rw = SDL_RWFromFile(RWopsReadTestFilename, "r");
    305   SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"r\") succeeded");
    306   SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in read mode does not return NULL");
    307
    308   /* Bail out if NULL */
    309   if (rw == NULL) return TEST_ABORTED;
    310
    311   /* Check type */
    312#if defined(__ANDROID__)
    313   SDLTest_AssertCheck(
    314      rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE,
    315      "Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
    316#elif defined(__WIN32__)
    317   SDLTest_AssertCheck(
    318      rw->type == SDL_RWOPS_WINFILE,
    319      "Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
    320#else
    321   SDLTest_AssertCheck(
    322      rw->type == SDL_RWOPS_STDFILE,
    323      "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
    324#endif
    325
    326   /* Run generic tests */
    327   _testGenericRWopsValidations( rw, 0 );
    328
    329   /* Close handle */
    330   result = SDL_RWclose(rw);
    331   SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
    332   SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
    333
    334   return TEST_COMPLETED;
    335}
    336
    337/**
    338 * @brief Tests writing from file.
    339 *
    340 * \sa
    341 * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
    342 * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
    343 */
    344int
    345rwops_testFileWrite(void)
    346{
    347   SDL_RWops *rw;
    348   int result;
    349
    350   /* Write test. */
    351   rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
    352   SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\") succeeded");
    353   SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
    354
    355   /* Bail out if NULL */
    356   if (rw == NULL) return TEST_ABORTED;
    357
    358   /* Check type */
    359#if defined(__ANDROID__)
    360   SDLTest_AssertCheck(
    361      rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE,
    362      "Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
    363#elif defined(__WIN32__)
    364   SDLTest_AssertCheck(
    365      rw->type == SDL_RWOPS_WINFILE,
    366      "Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
    367#else
    368   SDLTest_AssertCheck(
    369      rw->type == SDL_RWOPS_STDFILE,
    370      "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
    371#endif
    372
    373   /* Run generic tests */
    374   _testGenericRWopsValidations( rw, 1 );
    375
    376   /* Close handle */
    377   result = SDL_RWclose(rw);
    378   SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
    379   SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
    380
    381   return TEST_COMPLETED;
    382}
    383
    384
    385/**
    386 * @brief Tests reading from file handle
    387 *
    388 * \sa
    389 * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFP
    390 * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
    391 *
    392 */
    393int
    394rwops_testFPRead(void)
    395{
    396   FILE *fp;
    397   SDL_RWops *rw;
    398   int result;
    399
    400   /* Run read tests. */
    401   fp = fopen(RWopsReadTestFilename, "r");
    402   SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in read mode is not NULL", RWopsReadTestFilename);
    403
    404   /* Bail out if NULL */
    405   if (fp == NULL) return TEST_ABORTED;
    406
    407   /* Open */
    408   rw = SDL_RWFromFP( fp, SDL_TRUE );
    409   SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded");
    410   SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in read mode does not return NULL");
    411
    412   /* Bail out if NULL */
    413   if (rw == NULL) {
    414     fclose(fp);
    415     return TEST_ABORTED;
    416   }
    417
    418   /* Check type */
    419   SDLTest_AssertCheck(
    420      rw->type == SDL_RWOPS_STDFILE,
    421      "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
    422
    423   /* Run generic tests */
    424   _testGenericRWopsValidations( rw, 0 );
    425
    426   /* Close handle - does fclose() */
    427   result = SDL_RWclose(rw);
    428   SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
    429   SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
    430
    431   return TEST_COMPLETED;
    432}
    433
    434
    435/**
    436 * @brief Tests writing to file handle
    437 *
    438 * \sa
    439 * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFP
    440 * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
    441 *
    442 */
    443int
    444rwops_testFPWrite(void)
    445{
    446   FILE *fp;
    447   SDL_RWops *rw;
    448   int result;
    449
    450   /* Run write tests. */
    451   fp = fopen(RWopsWriteTestFilename, "w+");
    452   SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in write mode is not NULL", RWopsWriteTestFilename);
    453
    454   /* Bail out if NULL */
    455   if (fp == NULL) return TEST_ABORTED;
    456
    457   /* Open */
    458   rw = SDL_RWFromFP( fp, SDL_TRUE );
    459   SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded");
    460   SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in write mode does not return NULL");
    461
    462   /* Bail out if NULL */
    463   if (rw == NULL) {
    464     fclose(fp);
    465     return TEST_ABORTED;
    466   }
    467
    468   /* Check type */
    469   SDLTest_AssertCheck(
    470      rw->type == SDL_RWOPS_STDFILE,
    471      "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
    472
    473   /* Run generic tests */
    474   _testGenericRWopsValidations( rw, 1 );
    475
    476   /* Close handle - does fclose() */
    477   result = SDL_RWclose(rw);
    478   SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
    479   SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
    480
    481   return TEST_COMPLETED;
    482}
    483
    484/**
    485 * @brief Tests alloc and free RW context.
    486 *
    487 * \sa http://wiki.libsdl.org/moin.cgi/SDL_AllocRW
    488 * \sa http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
    489 */
    490int
    491rwops_testAllocFree (void)
    492{
    493   /* Allocate context */
    494   SDL_RWops *rw = SDL_AllocRW();
    495   SDLTest_AssertPass("Call to SDL_AllocRW() succeeded");
    496   SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_AllocRW() is not NULL");
    497   if (rw==NULL) return TEST_ABORTED;
    498
    499   /* Check type */
    500   SDLTest_AssertCheck(
    501      rw->type == SDL_RWOPS_UNKNOWN,
    502      "Verify RWops type is SDL_RWOPS_UNKNOWN; expected: %d, got: %d", SDL_RWOPS_UNKNOWN, rw->type);
    503
    504   /* Free context again */
    505   SDL_FreeRW(rw);
    506   SDLTest_AssertPass("Call to SDL_FreeRW() succeeded");
    507
    508   return TEST_COMPLETED;
    509}
    510
    511/**
    512 * @brief Compare memory and file reads
    513 *
    514 * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromMem
    515 * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
    516 */
    517int
    518rwops_testCompareRWFromMemWithRWFromFile(void)
    519{
    520   int slen = 26;
    521   char buffer_file[27];
    522   char buffer_mem[27];
    523   size_t rv_file;
    524   size_t rv_mem;
    525   Uint64 sv_file;
    526   Uint64 sv_mem;
    527   SDL_RWops* rwops_file;
    528   SDL_RWops* rwops_mem;
    529   int size;
    530   int result;
    531
    532
    533   for (size=5; size<10; size++)
    534   {
    535     /* Terminate buffer */
    536     buffer_file[slen] = 0;
    537     buffer_mem[slen] = 0;
    538
    539     /* Read/seek from memory */
    540     rwops_mem = SDL_RWFromMem((void *)RWopsAlphabetString, slen);
    541     SDLTest_AssertPass("Call to SDL_RWFromMem()");
    542     rv_mem = SDL_RWread(rwops_mem, buffer_mem, size, 6);
    543     SDLTest_AssertPass("Call to SDL_RWread(mem, size=%d)", size);
    544     sv_mem = SDL_RWseek(rwops_mem, 0, SEEK_END);
    545     SDLTest_AssertPass("Call to SDL_RWseek(mem,SEEK_END)");
    546     result = SDL_RWclose(rwops_mem);
    547     SDLTest_AssertPass("Call to SDL_RWclose(mem)");
    548     SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
    549
    550     /* Read/see from file */
    551     rwops_file = SDL_RWFromFile(RWopsAlphabetFilename, "r");
    552     SDLTest_AssertPass("Call to SDL_RWFromFile()");
    553     rv_file = SDL_RWread(rwops_file, buffer_file, size, 6);
    554     SDLTest_AssertPass("Call to SDL_RWread(file, size=%d)", size);
    555     sv_file = SDL_RWseek(rwops_file, 0, SEEK_END);
    556     SDLTest_AssertPass("Call to SDL_RWseek(file,SEEK_END)");
    557     result = SDL_RWclose(rwops_file);
    558     SDLTest_AssertPass("Call to SDL_RWclose(file)");
    559     SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
    560
    561     /* Compare */
    562     SDLTest_AssertCheck(rv_mem == rv_file, "Verify returned read blocks matches for mem and file reads; got: rv_mem=%d rv_file=%d", rv_mem, rv_file);
    563     SDLTest_AssertCheck(sv_mem == sv_file, "Verify SEEK_END position matches for mem and file seeks; got: sv_mem=%llu sv_file=%llu", sv_mem, sv_file);
    564     SDLTest_AssertCheck(buffer_mem[slen] == 0, "Verify mem buffer termination; expected: 0, got: %d", buffer_mem[slen]);
    565     SDLTest_AssertCheck(buffer_file[slen] == 0, "Verify file buffer termination; expected: 0, got: %d", buffer_file[slen]);
    566     SDLTest_AssertCheck(
    567       SDL_strncmp(buffer_mem, RWopsAlphabetString, slen) == 0,
    568       "Verify mem buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_mem);
    569     SDLTest_AssertCheck(
    570       SDL_strncmp(buffer_file, RWopsAlphabetString, slen) == 0,
    571       "Verify file buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_file);
    572   }
    573
    574   return TEST_COMPLETED;
    575}
    576
    577/**
    578 * @brief Tests writing and reading from file using endian aware functions.
    579 *
    580 * \sa
    581 * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
    582 * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
    583 * http://wiki.libsdl.org/moin.cgi/SDL_ReadBE16
    584 * http://wiki.libsdl.org/moin.cgi/SDL_WriteBE16
    585 */
    586int
    587rwops_testFileWriteReadEndian(void)
    588{
    589   SDL_RWops *rw;
    590   Sint64 result;
    591   int mode;
    592   size_t objectsWritten;
    593   Uint16 BE16value;
    594   Uint32 BE32value;
    595   Uint64 BE64value;
    596   Uint16 LE16value;
    597   Uint32 LE32value;
    598   Uint64 LE64value;
    599   Uint16 BE16test;
    600   Uint32 BE32test;
    601   Uint64 BE64test;
    602   Uint16 LE16test;
    603   Uint32 LE32test;
    604   Uint64 LE64test;
    605   int cresult;
    606
    607   for (mode = 0; mode < 3; mode++) {
    608
    609     /* Create test data */
    610     switch (mode) {
    611       case 0:
    612        SDLTest_Log("All 0 values");
    613        BE16value = 0;
    614        BE32value = 0;
    615        BE64value = 0;
    616        LE16value = 0;
    617        LE32value = 0;
    618        LE64value = 0;
    619        break;
    620       case 1:
    621        SDLTest_Log("All 1 values");
    622        BE16value = 1;
    623        BE32value = 1;
    624        BE64value = 1;
    625        LE16value = 1;
    626        LE32value = 1;
    627        LE64value = 1;
    628        break;
    629       case 2:
    630        SDLTest_Log("Random values");
    631        BE16value = SDLTest_RandomUint16();
    632        BE32value = SDLTest_RandomUint32();
    633        BE64value = SDLTest_RandomUint64();
    634        LE16value = SDLTest_RandomUint16();
    635        LE32value = SDLTest_RandomUint32();
    636        LE64value = SDLTest_RandomUint64();
    637        break;
    638     }
    639
    640     /* Write test. */
    641     rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
    642     SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\")");
    643     SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
    644
    645     /* Bail out if NULL */
    646     if (rw == NULL) return TEST_ABORTED;
    647
    648     /* Write test data */
    649     objectsWritten = SDL_WriteBE16(rw, BE16value);
    650     SDLTest_AssertPass("Call to SDL_WriteBE16");
    651     SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);
    652     objectsWritten = SDL_WriteBE32(rw, BE32value);
    653     SDLTest_AssertPass("Call to SDL_WriteBE32");
    654     SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);
    655     objectsWritten = SDL_WriteBE64(rw, BE64value);
    656     SDLTest_AssertPass("Call to SDL_WriteBE64");
    657     SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);
    658     objectsWritten = SDL_WriteLE16(rw, LE16value);
    659     SDLTest_AssertPass("Call to SDL_WriteLE16");
    660     SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);
    661     objectsWritten = SDL_WriteLE32(rw, LE32value);
    662     SDLTest_AssertPass("Call to SDL_WriteLE32");
    663     SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);
    664     objectsWritten = SDL_WriteLE64(rw, LE64value);
    665     SDLTest_AssertPass("Call to SDL_WriteLE64");
    666     SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);
    667
    668     /* Test seek to start */
    669     result = SDL_RWseek( rw, 0, RW_SEEK_SET );
    670     SDLTest_AssertPass("Call to SDL_RWseek succeeded");
    671     SDLTest_AssertCheck(result == 0, "Verify result from position 0 with SDL_RWseek, expected 0, got %i", result);
    672
    673     /* Read test data */
    674     BE16test = SDL_ReadBE16(rw);
    675     SDLTest_AssertPass("Call to SDL_ReadBE16");
    676     SDLTest_AssertCheck(BE16test == BE16value, "Validate return value from SDL_ReadBE16, expected: %hu, got: %hu", BE16value, BE16test);
    677     BE32test = SDL_ReadBE32(rw);
    678     SDLTest_AssertPass("Call to SDL_ReadBE32");
    679     SDLTest_AssertCheck(BE32test == BE32value, "Validate return value from SDL_ReadBE32, expected: %u, got: %u", BE32value, BE32test);
    680     BE64test = SDL_ReadBE64(rw);
    681     SDLTest_AssertPass("Call to SDL_ReadBE64");
    682     SDLTest_AssertCheck(BE64test == BE64value, "Validate return value from SDL_ReadBE64, expected: %llu, got: %llu", BE64value, BE64test);
    683     LE16test = SDL_ReadLE16(rw);
    684     SDLTest_AssertPass("Call to SDL_ReadLE16");
    685     SDLTest_AssertCheck(LE16test == LE16value, "Validate return value from SDL_ReadLE16, expected: %hu, got: %hu", LE16value, LE16test);
    686     LE32test = SDL_ReadLE32(rw);
    687     SDLTest_AssertPass("Call to SDL_ReadLE32");
    688     SDLTest_AssertCheck(LE32test == LE32value, "Validate return value from SDL_ReadLE32, expected: %u, got: %u", LE32value, LE32test);
    689     LE64test = SDL_ReadLE64(rw);
    690     SDLTest_AssertPass("Call to SDL_ReadLE64");
    691     SDLTest_AssertCheck(LE64test == LE64value, "Validate return value from SDL_ReadLE64, expected: %llu, got: %llu", LE64value, LE64test);
    692
    693     /* Close handle */
    694     cresult = SDL_RWclose(rw);
    695     SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
    696     SDLTest_AssertCheck(cresult == 0, "Verify result value is 0; got: %d", cresult);
    697   }
    698
    699   return TEST_COMPLETED;
    700}
    701
    702
    703/* ================= Test References ================== */
    704
    705/* RWops test cases */
    706static const SDLTest_TestCaseReference rwopsTest1 =
    707        { (SDLTest_TestCaseFp)rwops_testParamNegative, "rwops_testParamNegative", "Negative test for SDL_RWFromFile parameters", TEST_ENABLED };
    708
    709static const SDLTest_TestCaseReference rwopsTest2 =
    710        { (SDLTest_TestCaseFp)rwops_testMem, "rwops_testMem", "Tests opening from memory", TEST_ENABLED };
    711
    712static const SDLTest_TestCaseReference rwopsTest3 =
    713        { (SDLTest_TestCaseFp)rwops_testConstMem, "rwops_testConstMem", "Tests opening from (const) memory", TEST_ENABLED };
    714
    715static const SDLTest_TestCaseReference rwopsTest4 =
    716        { (SDLTest_TestCaseFp)rwops_testFileRead, "rwops_testFileRead", "Tests reading from a file", TEST_ENABLED };
    717
    718static const SDLTest_TestCaseReference rwopsTest5 =
    719        { (SDLTest_TestCaseFp)rwops_testFileWrite, "rwops_testFileWrite", "Test writing to a file", TEST_ENABLED };
    720
    721static const SDLTest_TestCaseReference rwopsTest6 =
    722        { (SDLTest_TestCaseFp)rwops_testFPRead, "rwops_testFPRead", "Test reading from file pointer", TEST_ENABLED };
    723
    724static const SDLTest_TestCaseReference rwopsTest7 =
    725        { (SDLTest_TestCaseFp)rwops_testFPWrite, "rwops_testFPWrite", "Test writing to file pointer", TEST_ENABLED };
    726
    727static const SDLTest_TestCaseReference rwopsTest8 =
    728        { (SDLTest_TestCaseFp)rwops_testAllocFree, "rwops_testAllocFree", "Test alloc and free of RW context", TEST_ENABLED };
    729
    730static const SDLTest_TestCaseReference rwopsTest9 =
    731        { (SDLTest_TestCaseFp)rwops_testFileWriteReadEndian, "rwops_testFileWriteReadEndian", "Test writing and reading via the Endian aware functions", TEST_ENABLED };
    732
    733static const SDLTest_TestCaseReference rwopsTest10 =
    734        { (SDLTest_TestCaseFp)rwops_testCompareRWFromMemWithRWFromFile, "rwops_testCompareRWFromMemWithRWFromFile", "Compare RWFromMem and RWFromFile RWops for read and seek", TEST_ENABLED };
    735
    736/* Sequence of RWops test cases */
    737static const SDLTest_TestCaseReference *rwopsTests[] =  {
    738    &rwopsTest1, &rwopsTest2, &rwopsTest3, &rwopsTest4, &rwopsTest5, &rwopsTest6,
    739    &rwopsTest7, &rwopsTest8, &rwopsTest9, &rwopsTest10, NULL
    740};
    741
    742/* RWops test suite (global) */
    743SDLTest_TestSuiteReference rwopsTestSuite = {
    744    "RWops",
    745    RWopsSetUp,
    746    rwopsTests,
    747    RWopsTearDown
    748};