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