test/testautomation_rwops.c
changeset 6999 681820ca0e78
parent 6996 bc0fd5bced25
child 7191 75360622e65f
     1.1 --- a/test/testautomation_rwops.c	Tue Mar 12 18:28:40 2013 -0700
     1.2 +++ b/test/testautomation_rwops.c	Wed Mar 13 08:35:03 2013 -0700
     1.3 @@ -21,16 +21,18 @@
     1.4  
     1.5  const char* RWopsReadTestFilename = "rwops_read";
     1.6  const char* RWopsWriteTestFilename = "rwops_write";
     1.7 +const char* RWopsAlphabetFilename = "rwops_alphabet";
     1.8  
     1.9  static const char RWopsHelloWorldTestString[] = "Hello World!";
    1.10  static const char RWopsHelloWorldCompString[] = "Hello World!";
    1.11 +static const char RWopsAlphabetString[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    1.12  
    1.13  /* Fixture */
    1.14  
    1.15  void
    1.16  RWopsSetUp(void *arg)
    1.17  {
    1.18 -	int fileLen = SDL_strlen(RWopsHelloWorldTestString);
    1.19 +	int fileLen;
    1.20  	FILE *handle;
    1.21  	int writtenLen;
    1.22  	int result;
    1.23 @@ -38,18 +40,32 @@
    1.24  	/* Clean up from previous runs (if any); ignore errors */
    1.25  	remove(RWopsReadTestFilename);
    1.26  	remove(RWopsWriteTestFilename);
    1.27 +	remove(RWopsAlphabetFilename);
    1.28  
    1.29  	/* Create a test file */
    1.30  	handle = fopen(RWopsReadTestFilename, "w");
    1.31  	SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", RWopsReadTestFilename);
    1.32          if (handle == NULL) return;
    1.33  
    1.34 -	/* Write some known test into it */
    1.35 +	/* Write some known text into it */
    1.36 +	fileLen = SDL_strlen(RWopsHelloWorldTestString);
    1.37  	writtenLen = (int)fwrite(RWopsHelloWorldTestString, 1, fileLen, handle);
    1.38  	SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", fileLen, writtenLen);
    1.39  	result = fclose(handle);
    1.40  	SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
    1.41  
    1.42 +	/* Create a second test file */
    1.43 +	handle = fopen(RWopsAlphabetFilename, "w");
    1.44 +	SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", RWopsAlphabetFilename);
    1.45 +        if (handle == NULL) return;
    1.46 +
    1.47 +	/* Write alphabet text into it */
    1.48 +	fileLen = SDL_strlen(RWopsAlphabetString);
    1.49 +	writtenLen = (int)fwrite(RWopsAlphabetString, 1, fileLen, handle);
    1.50 +	SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", fileLen, writtenLen);
    1.51 +	result = fclose(handle);
    1.52 +	SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
    1.53 +
    1.54  	SDLTest_AssertPass("Creation of test file completed");
    1.55  }
    1.56  
    1.57 @@ -62,6 +78,8 @@
    1.58  	result = remove(RWopsReadTestFilename);
    1.59  	SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", RWopsReadTestFilename, result);
    1.60  	remove(RWopsWriteTestFilename);
    1.61 +	result = remove(RWopsAlphabetFilename);
    1.62 +	SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", RWopsAlphabetFilename, result);
    1.63  
    1.64  	SDLTest_AssertPass("Cleanup of test files completed");
    1.65  }
    1.66 @@ -137,6 +155,14 @@
    1.67  	   "Verify seek to -1 with SDL_RWseek (RW_SEEK_END), expected %i, got %i",
    1.68  	   sizeof(RWopsHelloWorldTestString)-2,
    1.69  	   i);
    1.70 +	   
    1.71 +   /* Invalid whence seek */
    1.72 +   i = SDL_RWseek( rw, 0, 999 );
    1.73 +   SDLTest_AssertPass("Call to SDL_RWseek(...,0,invalid_whence) succeeded");
    1.74 +   SDLTest_AssertCheck(
    1.75 +	   i == (Sint64)(-1), 
    1.76 +	   "Verify seek with SDL_RWseek (invalid_whence); expected: -1, got %i",
    1.77 +	   i);
    1.78  }
    1.79  
    1.80  /*!
    1.81 @@ -171,6 +197,18 @@
    1.82     SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", NULL) succeeded");
    1.83     SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", NULL) returns NULL");
    1.84  
    1.85 +   rwops = SDL_RWFromMem((void *)NULL, 10);
    1.86 +   SDLTest_AssertPass("Call to SDL_RWFromMem(NULL, 10) succeeded");
    1.87 +   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromMem(NULL, 10) returns NULL");
    1.88 +
    1.89 +   rwops = SDL_RWFromMem((void *)RWopsAlphabetString, 0);
    1.90 +   SDLTest_AssertPass("Call to SDL_RWFromMem(data, 0) succeeded");
    1.91 +   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromMem(data, 0) returns NULL");
    1.92 +
    1.93 +   rwops = SDL_RWFromConstMem((const void *)RWopsAlphabetString, 0);
    1.94 +   SDLTest_AssertPass("Call to SDL_RWFromConstMem(data, 0) succeeded");
    1.95 +   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromConstMem(data, 0) returns NULL");
    1.96 +
    1.97     return TEST_COMPLETED;
    1.98  }
    1.99  
   1.100 @@ -178,13 +216,14 @@
   1.101   * @brief Tests opening from memory.
   1.102   *
   1.103   * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromMem
   1.104 - * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
   1.105 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWClose
   1.106   */
   1.107  int
   1.108  rwops_testMem (void)
   1.109  {
   1.110     char mem[sizeof(RWopsHelloWorldTestString)];
   1.111     SDL_RWops *rw;
   1.112 +   int result;
   1.113  
   1.114     /* Clear buffer */
   1.115     SDL_zero(mem);
   1.116 @@ -197,12 +236,16 @@
   1.117     /* Bail out if NULL */
   1.118     if (rw == NULL) return TEST_ABORTED;
   1.119  
   1.120 +   /* Check type */
   1.121 +   SDLTest_AssertCheck(rw->type == SDL_RWOPS_MEMORY, "Verify RWops type is SDL_RWOPS_MEMORY; expected: %d, got: %d", SDL_RWOPS_MEMORY, rw->type);
   1.122 +
   1.123     /* Run generic tests */
   1.124     _testGenericRWopsValidations(rw, 1);
   1.125  
   1.126     /* Close */
   1.127 -   SDL_RWclose(rw);
   1.128 +   result = SDL_RWclose(rw);
   1.129     SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   1.130 +   SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
   1.131  
   1.132     return TEST_COMPLETED;
   1.133  }
   1.134 @@ -219,6 +262,7 @@
   1.135  rwops_testConstMem (void)
   1.136  {
   1.137     SDL_RWops *rw;
   1.138 +   int result;
   1.139  
   1.140     /* Open handle */
   1.141     rw = SDL_RWFromConstMem( RWopsHelloWorldCompString, sizeof(RWopsHelloWorldCompString)-1 );
   1.142 @@ -228,12 +272,16 @@
   1.143     /* Bail out if NULL */
   1.144     if (rw == NULL) return TEST_ABORTED;
   1.145  
   1.146 +   /* Check type */
   1.147 +   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);
   1.148 +
   1.149     /* Run generic tests */
   1.150     _testGenericRWopsValidations( rw, 0 );
   1.151  
   1.152     /* Close handle */
   1.153 -   SDL_RWclose(rw);
   1.154 +   result = SDL_RWclose(rw);
   1.155     SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   1.156 +   SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
   1.157  
   1.158    return TEST_COMPLETED;
   1.159  }
   1.160 @@ -250,6 +298,7 @@
   1.161  rwops_testFileRead(void)
   1.162  {
   1.163     SDL_RWops *rw;
   1.164 +   int result;
   1.165  
   1.166     /* Read test. */
   1.167     rw = SDL_RWFromFile(RWopsReadTestFilename, "r");
   1.168 @@ -259,12 +308,28 @@
   1.169     // Bail out if NULL
   1.170     if (rw == NULL) return TEST_ABORTED;
   1.171  
   1.172 +   /* Check type */
   1.173 +#if defined(ANDROID)
   1.174 +   SDLTest_AssertCheck(
   1.175 +      rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE, 
   1.176 +      "Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
   1.177 +#elif defined(__WIN32__)
   1.178 +   SDLTest_AssertCheck(
   1.179 +      rw->type == SDL_RWOPS_WINFILE, 
   1.180 +      "Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
   1.181 +#else
   1.182 +   SDLTest_AssertCheck(
   1.183 +      rw->type == SDL_RWOPS_STDFILE, 
   1.184 +      "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
   1.185 +#endif
   1.186 +
   1.187     /* Run generic tests */
   1.188     _testGenericRWopsValidations( rw, 0 );
   1.189  
   1.190     /* Close handle */
   1.191 -   SDL_RWclose(rw);
   1.192 +   result = SDL_RWclose(rw);
   1.193     SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   1.194 +   SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
   1.195  
   1.196     return TEST_COMPLETED;
   1.197  }
   1.198 @@ -280,6 +345,7 @@
   1.199  rwops_testFileWrite(void)
   1.200  {
   1.201     SDL_RWops *rw;
   1.202 +   int result;
   1.203  
   1.204     /* Write test. */
   1.205     rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
   1.206 @@ -289,12 +355,28 @@
   1.207     // Bail out if NULL
   1.208     if (rw == NULL) return TEST_ABORTED;
   1.209  
   1.210 +   /* Check type */
   1.211 +#if defined(ANDROID)
   1.212 +   SDLTest_AssertCheck(
   1.213 +      rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE, 
   1.214 +      "Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
   1.215 +#elif defined(__WIN32__)
   1.216 +   SDLTest_AssertCheck(
   1.217 +      rw->type == SDL_RWOPS_WINFILE, 
   1.218 +      "Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
   1.219 +#else
   1.220 +   SDLTest_AssertCheck(
   1.221 +      rw->type == SDL_RWOPS_STDFILE, 
   1.222 +      "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
   1.223 +#endif
   1.224 +
   1.225     /* Run generic tests */
   1.226     _testGenericRWopsValidations( rw, 1 );
   1.227  
   1.228     /* Close handle */
   1.229 -   SDL_RWclose(rw);
   1.230 +   result = SDL_RWclose(rw);
   1.231     SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   1.232 +   SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
   1.233  
   1.234     return TEST_COMPLETED;
   1.235  }
   1.236 @@ -313,6 +395,7 @@
   1.237  {
   1.238     FILE *fp;
   1.239     SDL_RWops *rw;
   1.240 +   int result;
   1.241  
   1.242     /* Run read tests. */
   1.243     fp = fopen(RWopsReadTestFilename, "r");
   1.244 @@ -332,12 +415,18 @@
   1.245       return TEST_ABORTED;
   1.246     }
   1.247  
   1.248 +   /* Check type */
   1.249 +   SDLTest_AssertCheck(
   1.250 +      rw->type == SDL_RWOPS_STDFILE, 
   1.251 +      "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
   1.252 +
   1.253     /* Run generic tests */
   1.254     _testGenericRWopsValidations( rw, 0 );
   1.255  
   1.256     /* Close handle - does fclose() */
   1.257 -   SDL_RWclose(rw);
   1.258 +   result = SDL_RWclose(rw);
   1.259     SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   1.260 +   SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
   1.261  
   1.262     return TEST_COMPLETED;
   1.263  }
   1.264 @@ -356,6 +445,7 @@
   1.265  {
   1.266     FILE *fp;
   1.267     SDL_RWops *rw;
   1.268 +   int result;
   1.269  
   1.270     /* Run write tests. */
   1.271     fp = fopen(RWopsWriteTestFilename, "w+");
   1.272 @@ -375,12 +465,18 @@
   1.273       return TEST_ABORTED;
   1.274     }
   1.275  
   1.276 +   /* Check type */
   1.277 +   SDLTest_AssertCheck(
   1.278 +      rw->type == SDL_RWOPS_STDFILE, 
   1.279 +      "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
   1.280 +
   1.281     /* Run generic tests */
   1.282     _testGenericRWopsValidations( rw, 1 );
   1.283  
   1.284     /* Close handle - does fclose() */
   1.285 -   SDL_RWclose(rw);
   1.286 +   result = SDL_RWclose(rw);
   1.287     SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   1.288 +   SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
   1.289  
   1.290     return TEST_COMPLETED;
   1.291  }
   1.292 @@ -399,6 +495,11 @@
   1.293     SDLTest_AssertPass("Call to SDL_AllocRW() succeeded");
   1.294     SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_AllocRW() is not NULL");
   1.295     if (rw==NULL) return TEST_ABORTED;
   1.296 +
   1.297 +   /* Check type */
   1.298 +   SDLTest_AssertCheck(
   1.299 +      rw->type == SDL_RWOPS_UNKNOWN, 
   1.300 +      "Verify RWops type is SDL_RWOPS_UNKNOWN; expected: %d, got: %d", SDL_RWOPS_UNKNOWN, rw->type);
   1.301            
   1.302     /* Free context again */
   1.303     SDL_FreeRW(rw);
   1.304 @@ -408,6 +509,72 @@
   1.305  }
   1.306  
   1.307  /**
   1.308 + * @brief Compare memory and file reads
   1.309 + *
   1.310 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromMem
   1.311 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
   1.312 + */
   1.313 +int
   1.314 +rwops_testCompareRWFromMemWithRWFromFile(void)
   1.315 +{
   1.316 +   int slen = 26;
   1.317 +   char buffer_file[27];
   1.318 +   char buffer_mem[27];
   1.319 +   size_t rv_file;
   1.320 +   size_t rv_mem;
   1.321 +   Uint64 sv_file;
   1.322 +   Uint64 sv_mem;
   1.323 +   SDL_RWops* rwops_file;
   1.324 +   SDL_RWops* rwops_mem;
   1.325 +   int size;
   1.326 +   int result;
   1.327 +
   1.328 +   
   1.329 +   for (size=5; size<10; size++)
   1.330 +   {
   1.331 +     /* Terminate buffer */
   1.332 +     buffer_file[slen] = 0;
   1.333 +     buffer_mem[slen] = 0;
   1.334 +     
   1.335 +     /* Read/seek from memory */
   1.336 +     rwops_mem = SDL_RWFromMem((void *)RWopsAlphabetString, slen);
   1.337 +     SDLTest_AssertPass("Call to SDL_RWFromMem()");
   1.338 +     rv_mem = SDL_RWread(rwops_mem, buffer_mem, size, 6);
   1.339 +     SDLTest_AssertPass("Call to SDL_RWread(mem, size=%d)", size);
   1.340 +     sv_mem = SDL_RWseek(rwops_mem, 0, SEEK_END);
   1.341 +     SDLTest_AssertPass("Call to SDL_RWseek(mem,SEEK_END)");
   1.342 +     result = SDL_RWclose(rwops_mem);
   1.343 +     SDLTest_AssertPass("Call to SDL_RWclose(mem)");
   1.344 +     SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
   1.345 +
   1.346 +     /* Read/see from file */
   1.347 +     rwops_file = SDL_RWFromFile(RWopsAlphabetFilename, "r");
   1.348 +     SDLTest_AssertPass("Call to SDL_RWFromFile()");
   1.349 +     rv_file = SDL_RWread(rwops_file, buffer_file, size, 6);
   1.350 +     SDLTest_AssertPass("Call to SDL_RWread(file, size=%d)", size);
   1.351 +     sv_file = SDL_RWseek(rwops_file, 0, SEEK_END);
   1.352 +     SDLTest_AssertPass("Call to SDL_RWseek(file,SEEK_END)");
   1.353 +     result = SDL_RWclose(rwops_file);
   1.354 +     SDLTest_AssertPass("Call to SDL_RWclose(file)");
   1.355 +     SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
   1.356 +   
   1.357 +     /* Compare */
   1.358 +     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);
   1.359 +     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);
   1.360 +     SDLTest_AssertCheck(buffer_mem[slen] == 0, "Verify mem buffer termination; expected: 0, got: %d", buffer_mem[slen]);
   1.361 +     SDLTest_AssertCheck(buffer_file[slen] == 0, "Verify file buffer termination; expected: 0, got: %d", buffer_file[slen]);
   1.362 +     SDLTest_AssertCheck(
   1.363 +       SDL_strncmp(buffer_mem, RWopsAlphabetString, slen) == 0,
   1.364 +       "Verify mem buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_mem);
   1.365 +     SDLTest_AssertCheck(
   1.366 +       SDL_strncmp(buffer_file, RWopsAlphabetString, slen) == 0,
   1.367 +       "Verify file buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_file);
   1.368 +   }
   1.369 +      
   1.370 +   return TEST_COMPLETED;
   1.371 +}
   1.372 +
   1.373 +/**
   1.374   * @brief Tests writing and reading from file using endian aware functions.
   1.375   *
   1.376   * \sa
   1.377 @@ -435,6 +602,7 @@
   1.378     Uint16 LE16test;
   1.379     Uint32 LE32test;
   1.380     Uint64 LE64test;
   1.381 +   int cresult;
   1.382  
   1.383     for (mode = 0; mode < 3; mode++) {
   1.384     
   1.385 @@ -523,9 +691,9 @@
   1.386       SDLTest_AssertCheck(LE64test == LE64value, "Validate return value from SDL_ReadLE64, expected: %llu, got: %llu", LE64value, LE64test);
   1.387  
   1.388       /* Close handle */
   1.389 -     SDL_RWclose(rw);
   1.390 +     cresult = SDL_RWclose(rw);
   1.391       SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   1.392 -   
   1.393 +     SDLTest_AssertCheck(cresult == 0, "Verify result value is 0; got: %d", cresult);   
   1.394     }
   1.395  
   1.396     return TEST_COMPLETED;
   1.397 @@ -562,10 +730,13 @@
   1.398  static const SDLTest_TestCaseReference rwopsTest9 =
   1.399  		{ (SDLTest_TestCaseFp)rwops_testFileWriteReadEndian, "rwops_testFileWriteReadEndian", "Test writing and reading via the Endian aware functions", TEST_ENABLED };
   1.400  
   1.401 +static const SDLTest_TestCaseReference rwopsTest10 =
   1.402 +		{ (SDLTest_TestCaseFp)rwops_testCompareRWFromMemWithRWFromFile, "rwops_testCompareRWFromMemWithRWFromFile", "Compare RWFromMem and RWFromFile RWops for read and seek", TEST_ENABLED };
   1.403 +
   1.404  /* Sequence of RWops test cases */
   1.405  static const SDLTest_TestCaseReference *rwopsTests[] =  {
   1.406  	&rwopsTest1, &rwopsTest2, &rwopsTest3, &rwopsTest4, &rwopsTest5, &rwopsTest6, 
   1.407 -	&rwopsTest7, &rwopsTest8, &rwopsTest9, NULL
   1.408 +	&rwopsTest7, &rwopsTest8, &rwopsTest9, &rwopsTest10, NULL
   1.409  };
   1.410  
   1.411  /* RWops test suite (global) */