test/automated/rwops/rwops.c
author David Ludwig <dludwig@pobox.com>
Tue, 12 Feb 2013 12:57:06 -0500
changeset 8424 3cf9501008f1
parent 6708 1fa41febb331
permissions -rw-r--r--
WinRT: fixed bug: SDL_CreateWindow wouldn't work after an initial window was created + destroyed
     1 /**
     2  * Automated SDL_RWops test.
     3  *
     4  * Written by Edgar Simo "bobbens"
     5  *
     6  * Released under Public Domain.
     7  */
     8 
     9 
    10 #include "SDL.h"
    11 #include "../SDL_at.h"
    12 #include "TestSupportRWops.h"
    13 
    14 /* Defined in TestSupportRWops implementation to allow flexibility. */
    15 extern const char* RWOPS_READ;
    16 extern const char* RWOPS_WRITE;
    17 
    18 static const char hello_world[] = "Hello World!";
    19 
    20 
    21 /**
    22  * @brief Makes sure parameters work properly.
    23  */
    24 static void rwops_testParam (void)
    25 {
    26    SDL_RWops *rwops;
    27 
    28    /* Begin testcase. */
    29    SDL_ATbegin( "RWops Parameters" );
    30 
    31    /* These should all fail. */
    32    rwops = SDL_RWFromFile(NULL, NULL);
    33    if (SDL_ATassert( "SDL_RWFromFile(NULL, NULL) worked", rwops == NULL ))
    34       return;
    35    rwops = SDL_RWFromFile(NULL, "ab+");
    36    if (SDL_ATassert( "SDL_RWFromFile(NULL, \"ab+\") worked", rwops == NULL ))
    37       return;
    38    rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
    39    if (SDL_ATassert( "SDL_RWFromFile(NULL, \"sldfkjsldkfj\") worked", rwops == NULL ))
    40       return;
    41    rwops = SDL_RWFromFile("something", "");
    42    if (SDL_ATassert( "SDL_RWFromFile(\"something\", \"\") worked", rwops == NULL ))
    43       return;
    44    rwops = SDL_RWFromFile("something", NULL);
    45    if (SDL_ATassert( "SDL_RWFromFile(\"something\", NULL) worked", rwops == NULL ))
    46       return;
    47 
    48 
    49    /* End testcase. */
    50    SDL_ATend();
    51 }
    52 
    53 
    54 /**
    55  * @brief Does a generic rwops test.
    56  *
    57  * RWops should have "Hello World!" in it already if write is disabled.
    58  *
    59  *    @param write Test writing also.
    60  *    @return 1 if an assert is failed.
    61  */
    62 static int rwops_testGeneric( SDL_RWops *rw, int write )
    63 {
    64    char buf[sizeof(hello_world)];
    65    Sint64 i;
    66 
    67    /* Set to start. */
    68    i = SDL_RWseek( rw, 0, RW_SEEK_SET );
    69    if (SDL_ATvassert( i == 0,
    70             "Seeking with SDL_RWseek (RW_SEEK_SET): got %d, expected %d",
    71             (int)i, 0 ))
    72       return 1;
    73 
    74    /* Test write. */
    75    i = SDL_RWwrite( rw, hello_world, sizeof(hello_world)-1, 1 );
    76    if (write) {
    77       if (SDL_ATassert( "Writing with SDL_RWwrite (failed to write)", i == 1 ))
    78          return 1;
    79    }
    80    else {
    81       if (SDL_ATassert( "Writing with SDL_RWwrite (wrote when shouldn't have)", i <= 0 ))
    82          return 1;
    83    }
    84 
    85    /* Test seek. */
    86    i = SDL_RWseek( rw, 6, RW_SEEK_SET );
    87    if (SDL_ATvassert( i == 6,
    88             "Seeking with SDL_RWseek (RW_SEEK_SET): got %d, expected %d",
    89             (int)i, 0 ))
    90        return 1;
    91 
    92    /* Test seek. */
    93    i = SDL_RWseek( rw, 0, RW_SEEK_SET );
    94    if (SDL_ATvassert( i == 0,
    95             "Seeking with SDL_RWseek (RW_SEEK_SET): got %d, expected %d",
    96             (int)i, 0 ))
    97       return 1;
    98 
    99    /* Test read. */
   100    i = SDL_RWread( rw, buf, 1, sizeof(hello_world)-1 );
   101    if (SDL_ATassert( "Reading with SDL_RWread", i == sizeof(hello_world)-1 ))
   102       return 1;
   103    if (SDL_ATassert( "Memory read does not match memory written",
   104             SDL_memcmp( buf, hello_world, sizeof(hello_world)-1 ) == 0 ))
   105       return 1;
   106 
   107    /* More seek tests. */
   108    i = SDL_RWseek( rw, -4, RW_SEEK_CUR );
   109    if (SDL_ATvassert( i == sizeof(hello_world)-5,
   110             "Seeking with SDL_RWseek (RW_SEEK_CUR): got %d, expected %d",
   111             (int)i, sizeof(hello_world)-5 ))
   112       return 1;
   113    i = SDL_RWseek( rw, -1, RW_SEEK_END );
   114    if (SDL_ATvassert( i == sizeof(hello_world)-2,
   115             "Seeking with SDL_RWseek (RW_SEEK_END): got %d, expected %d",
   116             (int)i, sizeof(hello_world)-2 ))
   117       return 1;
   118 
   119    return 0;
   120 }
   121 
   122 
   123 /**
   124  * @brief Tests opening from memory.
   125  */
   126 static void rwops_testMem (void)
   127 {
   128    char mem[sizeof(hello_world)];
   129    SDL_RWops *rw;
   130 
   131    /* Begin testcase. */
   132    SDL_ATbegin( "SDL_RWFromMem" );
   133 
   134    /* Open. */
   135    rw = SDL_RWFromMem( mem, sizeof(hello_world)-1 );
   136    if (SDL_ATassert( "Opening memory with SDL_RWFromMem", rw != NULL ))
   137       return;
   138 
   139    /* Run generic tests. */
   140    if (rwops_testGeneric( rw, 1 ))
   141       return;
   142 
   143    /* Close. */
   144    SDL_FreeRW( rw );
   145 
   146    /* End testcase. */
   147    SDL_ATend();
   148 }
   149 
   150 
   151 static const char const_mem[] = "Hello World!";
   152 /**
   153  * @brief Tests opening from memory.
   154  */
   155 static void rwops_testConstMem (void)
   156 {
   157    SDL_RWops *rw;
   158 
   159    /* Begin testcase. */
   160    SDL_ATbegin( "SDL_RWFromConstMem" );
   161 
   162    /* Open. */
   163    rw = SDL_RWFromConstMem( const_mem, sizeof(const_mem)-1 );
   164    if (SDL_ATassert( "Opening memory with SDL_RWFromConstMem", rw != NULL ))
   165       return;
   166 
   167    /* Run generic tests. */
   168    if (rwops_testGeneric( rw, 0 ))
   169       return;
   170 
   171    /* Close. */
   172    SDL_FreeRW( rw );
   173 
   174    /* End testcase. */
   175    SDL_ATend();
   176 }
   177 
   178 
   179 /**
   180  * @brief Tests opening from memory.
   181  */
   182 static void rwops_testFile (void)
   183 {
   184    SDL_RWops *rw;
   185 
   186    /* Begin testcase. */
   187    SDL_ATbegin( "SDL_RWFromFile" );
   188 
   189    /* Read test. */
   190    rw = TestSupportRWops_OpenRWopsFromReadDir( RWOPS_READ, "r" );
   191    if (SDL_ATassert( "Opening memory with SDL_RWFromFile RWOPS_READ", rw != NULL ))
   192       return;
   193    if (rwops_testGeneric( rw, 0 ))
   194       return;
   195    SDL_FreeRW( rw );
   196 
   197    /* Write test. */
   198    rw = TestSupportRWops_OpenRWopsFromWriteDir( RWOPS_WRITE, "w+" );
   199    if (SDL_ATassert( "Opening memory with SDL_RWFromFile RWOPS_WRITE", rw != NULL ))
   200       return;
   201    if (rwops_testGeneric( rw, 1 ))
   202       return;
   203    SDL_FreeRW( rw );
   204 
   205    /* End testcase. */
   206    SDL_ATend();
   207 }
   208 
   209 
   210 /**
   211  * @brief Tests opening from memory.
   212  */
   213 static void rwops_testFP (void)
   214 {
   215 #ifdef HAVE_STDIO_H
   216    FILE *fp;
   217    SDL_RWops *rw;
   218 
   219    /* Begin testcase. */
   220    SDL_ATbegin( "SDL_RWFromFP" );
   221 
   222    /* Run read tests. */
   223    fp = TestSupportRWops_OpenFPFromReadDir( RWOPS_READ, "r" );
   224    if (SDL_ATassert( "Failed to open file 'WOPS_READ", fp != NULL))
   225       return;
   226    rw = SDL_RWFromFP( fp, 1 );
   227    if (SDL_ATassert( "Opening memory with SDL_RWFromFP", rw != NULL ))
   228       return;
   229    if (rwops_testGeneric( rw, 0 ))
   230       return;
   231    SDL_FreeRW( rw );
   232 
   233    /* Run write tests. */
   234    fp = TestSupportRWops_OpenFPFromWriteDir( RWOPS_WRITE, "w+" );
   235    if (SDL_ATassert( "Failed to open file RWOPS_WRITE", fp != NULL))
   236       return;
   237    rw = SDL_RWFromFP( fp, 1 );
   238    if (SDL_ATassert( "Opening memory with SDL_RWFromFP", rw != NULL ))
   239       return;
   240    if (rwops_testGeneric( rw, 1 ))
   241       return;
   242    SDL_FreeRW( rw );
   243 
   244    /* End testcase. */
   245    SDL_ATend();
   246 #endif /* HAVE_STDIO_H */
   247 }
   248 
   249 
   250 /**
   251  * @brief Entry point.
   252  */
   253 #ifdef TEST_STANDALONE
   254 int main( int argc, const char *argv[] )
   255 {
   256    (void) argc;
   257    (void) argv;
   258 #else /* TEST_STANDALONE */
   259 int test_rwops (void)
   260 {
   261 #endif /* TEST_STANDALONE */
   262 
   263    SDL_ATinit( "SDL_RWops" );
   264 
   265    rwops_testParam();
   266    rwops_testMem();
   267    rwops_testConstMem();
   268    rwops_testFile();
   269    rwops_testFP();
   270 
   271    return SDL_ATfinish();
   272 }