test/testautomation_rwops.c
changeset 6769 b49778d885cd
parent 6760 04dcce3081e6
child 6779 c3b579c78059
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testautomation_rwops.c	Sat Dec 22 17:24:42 2012 -0800
     1.3 @@ -0,0 +1,427 @@
     1.4 +/**
     1.5 + * Automated SDL_RWops test.
     1.6 + *
     1.7 + * Original code written by Edgar Simo "bobbens"
     1.8 + * Ported by Markus Kauppila (markus.kauppila@gmail.com)
     1.9 + * Updated for SDL_test by aschiffler at ferzkopp dot net
    1.10 + *
    1.11 + * Released under Public Domain.
    1.12 + */
    1.13 +
    1.14 +/* quiet windows compiler warnings */
    1.15 +#define _CRT_SECURE_NO_WARNINGS
    1.16 +
    1.17 +#include <stdio.h>
    1.18 +
    1.19 +#include "SDL.h"
    1.20 +#include "SDL_test.h"
    1.21 +
    1.22 +/* ================= Test Case Implementation ================== */
    1.23 +
    1.24 +const char* RWopsReadTestFilename = "rwops_read";
    1.25 +const char* RWopsWriteTestFilename = "rwops_write";
    1.26 +
    1.27 +static const char RWopsHelloWorldTestString[] = "Hello World!";
    1.28 +static const char RWopsHelloWorldCompString[] = "Hello World!";
    1.29 +
    1.30 +/* Fixture */
    1.31 +
    1.32 +void
    1.33 +RWopsSetUp(void *arg)
    1.34 +{
    1.35 +	int fileLen = SDL_strlen(RWopsHelloWorldTestString);
    1.36 +	FILE *handle;
    1.37 +	int writtenLen;
    1.38 +	int result;
    1.39 +
    1.40 +	/* Clean up from previous runs (if any); ignore errors */
    1.41 +	remove(RWopsReadTestFilename);
    1.42 +	remove(RWopsWriteTestFilename);
    1.43 +
    1.44 +	/* Create a test file */
    1.45 +	handle = fopen(RWopsReadTestFilename, "w");
    1.46 +	SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", RWopsReadTestFilename);
    1.47 +    if (handle == NULL) return;
    1.48 +
    1.49 +	/* Write some known test into it */
    1.50 +	writtenLen = (int)fwrite(RWopsHelloWorldTestString, 1, fileLen, handle);
    1.51 +	SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", fileLen, writtenLen);
    1.52 +	result = fclose(handle);
    1.53 +	SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
    1.54 +
    1.55 +	SDLTest_AssertPass("Creation of test file completed");
    1.56 +}
    1.57 +
    1.58 +void
    1.59 +RWopsTearDown(void *arg)
    1.60 +{
    1.61 +	int result;
    1.62 +	
    1.63 +	/* Remove the created files to clean up; ignore errors for write filename */
    1.64 +	result = remove(RWopsReadTestFilename);
    1.65 +	SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", RWopsReadTestFilename, result);
    1.66 +	remove(RWopsWriteTestFilename);
    1.67 +
    1.68 +	SDLTest_AssertPass("Cleanup of test files completed");
    1.69 +}
    1.70 +
    1.71 +/**
    1.72 + * @brief Makes sure parameters work properly. Local helper function.
    1.73 + *
    1.74 + * \sa
    1.75 + * http://wiki.libsdl.org/moin.cgi/SDL_RWseek
    1.76 + * http://wiki.libsdl.org/moin.cgi/SDL_RWread
    1.77 + */
    1.78 +void 
    1.79 +_testGenericRWopsValidations(SDL_RWops *rw, int write)
    1.80 +{
    1.81 +   char buf[sizeof(RWopsHelloWorldTestString)];
    1.82 +   int i;
    1.83 +   int seekPos = SDLTest_RandomIntegerInRange(4, 8);
    1.84 +
    1.85 +   /* Clear buffer */
    1.86 +   SDL_zero(buf);
    1.87 +
    1.88 +   /* Set to start. */
    1.89 +   i = SDL_RWseek(rw, 0, RW_SEEK_SET );
    1.90 +   SDLTest_AssertPass("Call to SDL_RWseek succeeded");
    1.91 +   SDLTest_AssertCheck(i == 0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %i", i);
    1.92 +
    1.93 +   /* Test write. */
    1.94 +   i = SDL_RWwrite(rw, RWopsHelloWorldTestString, sizeof(RWopsHelloWorldTestString)-1, 1);
    1.95 +   SDLTest_AssertPass("Call to SDL_RWwrite succeeded");
    1.96 +   if (write) {
    1.97 +		SDLTest_AssertCheck(i == 1, "Verify result of writing one byte with SDL_RWwrite, expected 1, got %i", i);
    1.98 +   }
    1.99 +   else {
   1.100 +		SDLTest_AssertCheck(i <= 0, "Verify result of writing with SDL_RWwrite, expected <=0, got %i", i);
   1.101 +   }
   1.102 +
   1.103 +   /* Test seek to random position */
   1.104 +   i = SDL_RWseek( rw, seekPos, RW_SEEK_SET );
   1.105 +   SDLTest_AssertPass("Call to SDL_RWseek succeeded");
   1.106 +   SDLTest_AssertCheck(i == seekPos, "Verify seek to %i with SDL_RWseek (RW_SEEK_SET), expected %i, got %i", seekPos, seekPos, i);
   1.107 +
   1.108 +   /* Test seek back to start */
   1.109 +   i = SDL_RWseek(rw, 0, RW_SEEK_SET );
   1.110 +   SDLTest_AssertPass("Call to SDL_RWseek succeeded");
   1.111 +   SDLTest_AssertCheck(i == 0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %i", i);
   1.112 +
   1.113 +   /* Test read */
   1.114 +   i = SDL_RWread( rw, buf, 1, sizeof(RWopsHelloWorldTestString)-1 );
   1.115 +   SDLTest_AssertPass("Call to SDL_RWread succeeded");
   1.116 +   SDLTest_AssertCheck(
   1.117 +	   i == (sizeof(RWopsHelloWorldTestString)-1), 
   1.118 +	   "Verify result from SDL_RWread, expected %i, got %i",
   1.119 +	   sizeof(RWopsHelloWorldTestString)-1,
   1.120 +	   i);
   1.121 +   SDLTest_AssertCheck(
   1.122 +	   SDL_memcmp(buf, RWopsHelloWorldTestString, sizeof(RWopsHelloWorldTestString)-1 ) == 0, 
   1.123 +	   "Verify read bytes match expected string, expected '%s', got '%s'", RWopsHelloWorldTestString, buf);
   1.124 +
   1.125 +   /* More seek tests. */
   1.126 +   i = SDL_RWseek( rw, -4, RW_SEEK_CUR );
   1.127 +   SDLTest_AssertPass("Call to SDL_RWseek(...,-4,RW_SEEK_CUR) succeeded");
   1.128 +   SDLTest_AssertCheck(
   1.129 +	   i == (sizeof(RWopsHelloWorldTestString)-5), 
   1.130 +	   "Verify seek to -4 with SDL_RWseek (RW_SEEK_CUR), expected %i, got %i",
   1.131 +	   sizeof(RWopsHelloWorldTestString)-5,
   1.132 +	   i);
   1.133 +
   1.134 +   i = SDL_RWseek( rw, -1, RW_SEEK_END );
   1.135 +   SDLTest_AssertPass("Call to SDL_RWseek(...,-1,RW_SEEK_END) succeeded");
   1.136 +   SDLTest_AssertCheck(
   1.137 +	   i == (sizeof(RWopsHelloWorldTestString)-2), 
   1.138 +	   "Verify seek to -1 with SDL_RWseek (RW_SEEK_END), expected %i, got %i",
   1.139 +	   sizeof(RWopsHelloWorldTestString)-2,
   1.140 +	   i);
   1.141 +}
   1.142 +
   1.143 +/*!
   1.144 + * Negative test for SDL_RWFromFile parameters
   1.145 + *
   1.146 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
   1.147 + *
   1.148 + */
   1.149 +int
   1.150 +rwops_testParamNegative (void)
   1.151 +{
   1.152 +   SDL_RWops *rwops;
   1.153 +
   1.154 +   /* These should all fail. */
   1.155 +   rwops = SDL_RWFromFile(NULL, NULL);
   1.156 +   SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, NULL) succeeded");
   1.157 +   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, NULL) returns NULL");
   1.158 +
   1.159 +   rwops = SDL_RWFromFile(NULL, "ab+");
   1.160 +   SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, \"ab+\") succeeded");
   1.161 +   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, \"ab+\") returns NULL");
   1.162 +
   1.163 +   rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
   1.164 +   SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, \"sldfkjsldkfj\") succeeded");
   1.165 +   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, \"sldfkjsldkfj\") returns NULL");
   1.166 +
   1.167 +   rwops = SDL_RWFromFile("something", "");
   1.168 +   SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", \"\") succeeded");
   1.169 +   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", \"\") returns NULL");
   1.170 +
   1.171 +   rwops = SDL_RWFromFile("something", NULL);
   1.172 +   SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", NULL) succeeded");
   1.173 +   SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", NULL) returns NULL");
   1.174 +
   1.175 +   return TEST_COMPLETED;
   1.176 +}
   1.177 +
   1.178 +/**
   1.179 + * @brief Tests opening from memory.
   1.180 + *
   1.181 + * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromMem
   1.182 + */
   1.183 +int
   1.184 +rwops_testMem (void)
   1.185 +{
   1.186 +   char mem[sizeof(RWopsHelloWorldTestString)];
   1.187 +   SDL_RWops *rw;
   1.188 +
   1.189 +   /* Clear buffer */
   1.190 +   SDL_zero(mem);
   1.191 +
   1.192 +   /* Open */
   1.193 +   rw = SDL_RWFromMem(mem, sizeof(RWopsHelloWorldTestString)-1);
   1.194 +   SDLTest_AssertPass("Call to SDL_RWFromMem() succeeded");
   1.195 +   SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromMem does not return NULL");
   1.196 +
   1.197 +   /* Bail out if NULL */
   1.198 +   if (rw == NULL) return TEST_ABORTED;
   1.199 +
   1.200 +   /* Run generic tests */
   1.201 +   _testGenericRWopsValidations(rw, 1);
   1.202 +
   1.203 +   /* Close */
   1.204 +   SDL_RWclose(rw);
   1.205 +   SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   1.206 +   SDL_FreeRW(rw);
   1.207 +   SDLTest_AssertPass("Call to SDL_FreeRW() succeeded");
   1.208 +
   1.209 +   return TEST_COMPLETED;
   1.210 +}
   1.211 +
   1.212 +
   1.213 +/**
   1.214 + * @brief Tests opening from memory.
   1.215 + *
   1.216 + * \sa
   1.217 + * http://wiki.libsdl.org/moin.cgi/SDL_RWFromConstMem
   1.218 + */
   1.219 +int 
   1.220 +rwops_testConstMem (void)
   1.221 +{
   1.222 +   SDL_RWops *rw;
   1.223 +
   1.224 +   /* Open handle */
   1.225 +   rw = SDL_RWFromConstMem( RWopsHelloWorldCompString, sizeof(RWopsHelloWorldCompString)-1 );
   1.226 +   SDLTest_AssertPass("Call to SDL_RWFromConstMem() succeeded");
   1.227 +   SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromConstMem does not return NULL");
   1.228 +
   1.229 +   /* Bail out if NULL */
   1.230 +   if (rw == NULL) return TEST_ABORTED;
   1.231 +
   1.232 +   /* Run generic tests */
   1.233 +   _testGenericRWopsValidations( rw, 0 );
   1.234 +
   1.235 +   /* Close handle */
   1.236 +   SDL_RWclose(rw);
   1.237 +   SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   1.238 +   SDL_FreeRW( rw );
   1.239 +   SDLTest_AssertPass("Call to SDL_FreeRW() succeeded");
   1.240 +
   1.241 +  return TEST_COMPLETED;
   1.242 +}
   1.243 +
   1.244 +
   1.245 +/**
   1.246 + * @brief Tests reading from file.
   1.247 + *
   1.248 + * \sa
   1.249 + * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
   1.250 + * http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
   1.251 + */
   1.252 +int
   1.253 +rwops_testFileRead(void)
   1.254 +{
   1.255 +   SDL_RWops *rw;
   1.256 +
   1.257 +   /* Read test. */
   1.258 +   rw = SDL_RWFromFile(RWopsReadTestFilename, "r");
   1.259 +   SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"r\") succeeded");
   1.260 +   SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in read mode does not return NULL");
   1.261 +
   1.262 +   // Bail out if NULL
   1.263 +   if (rw == NULL) return TEST_ABORTED;
   1.264 +
   1.265 +   /* Run generic tests */
   1.266 +   _testGenericRWopsValidations( rw, 0 );
   1.267 +
   1.268 +   /* Close handle */
   1.269 +   SDL_RWclose(rw);
   1.270 +   SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   1.271 +   SDL_FreeRW( rw );
   1.272 +   SDLTest_AssertPass("Call to SDL_FreeRW() succeeded");
   1.273 +
   1.274 +   return TEST_COMPLETED;
   1.275 +}
   1.276 +
   1.277 +/**
   1.278 + * @brief Tests writing from memory.
   1.279 + *
   1.280 + * \sa
   1.281 + * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
   1.282 + * http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
   1.283 + */
   1.284 +int
   1.285 +rwops_testFileWrite(void)
   1.286 +{
   1.287 +   SDL_RWops *rw;
   1.288 +
   1.289 +   /* Write test. */
   1.290 +   rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
   1.291 +   SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\") succeeded");
   1.292 +   SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
   1.293 +
   1.294 +   // Bail out if NULL
   1.295 +   if (rw == NULL) return TEST_ABORTED;
   1.296 +
   1.297 +   /* Run generic tests */
   1.298 +   _testGenericRWopsValidations( rw, 1 );
   1.299 +
   1.300 +   /* Close handle */
   1.301 +   SDL_RWclose(rw);
   1.302 +   SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   1.303 +   SDL_FreeRW( rw );
   1.304 +   SDLTest_AssertPass("Call to SDL_FreeRW() succeeded");
   1.305 +
   1.306 +   return TEST_COMPLETED;
   1.307 +}
   1.308 +
   1.309 +
   1.310 +/**
   1.311 + * @brief Tests reading from file handle
   1.312 + *
   1.313 + * \sa
   1.314 + * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFP
   1.315 + * http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
   1.316 + *
   1.317 + */
   1.318 +int
   1.319 +rwops_testFPRead(void)
   1.320 +{
   1.321 +   FILE *fp;
   1.322 +   SDL_RWops *rw;
   1.323 +
   1.324 +   /* Run read tests. */
   1.325 +   fp = fopen(RWopsReadTestFilename, "r");
   1.326 +   SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in read mode is not NULL", RWopsReadTestFilename);
   1.327 +
   1.328 +   /* Bail out if NULL */
   1.329 +   if (fp == NULL) return TEST_ABORTED;
   1.330 +
   1.331 +   /* Open */
   1.332 +   rw = SDL_RWFromFP( fp, 1 );
   1.333 +   SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded");
   1.334 +   SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in read mode does not return NULL");
   1.335 +
   1.336 +   /* Bail out if NULL */
   1.337 +   if (rw == NULL) return TEST_ABORTED;
   1.338 +
   1.339 +   /* Run generic tests */
   1.340 +   _testGenericRWopsValidations( rw, 0 );
   1.341 +
   1.342 +   /* Close handle */
   1.343 +   SDL_RWclose(rw);
   1.344 +   SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   1.345 +   SDL_FreeRW( rw );
   1.346 +   SDLTest_AssertPass("Call to SDL_FreeRW() succeeded");
   1.347 +   fclose(fp);
   1.348 +
   1.349 +   return TEST_COMPLETED;
   1.350 +}
   1.351 +
   1.352 +
   1.353 +/**
   1.354 + * @brief Tests writing to file handle
   1.355 + *
   1.356 + * \sa
   1.357 + * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFP
   1.358 + * http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
   1.359 + *
   1.360 + */
   1.361 +int
   1.362 +rwops_testFPWrite(void)
   1.363 +{
   1.364 +   FILE *fp;
   1.365 +   SDL_RWops *rw;
   1.366 +
   1.367 +   /* Run write tests. */
   1.368 +   fp = fopen(RWopsWriteTestFilename, "w+");
   1.369 +   SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in write mode is not NULL", RWopsWriteTestFilename);
   1.370 +
   1.371 +   /* Bail out if NULL */
   1.372 +   if (fp == NULL) return TEST_ABORTED;
   1.373 +
   1.374 +   /* Open */
   1.375 +   rw = SDL_RWFromFP( fp, 1 );
   1.376 +   SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded");
   1.377 +   SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in write mode does not return NULL");
   1.378 +
   1.379 +   /* Bail out if NULL */
   1.380 +   if (rw == NULL) return TEST_ABORTED;
   1.381 +
   1.382 +   /* Run generic tests */
   1.383 +   _testGenericRWopsValidations( rw, 1 );
   1.384 +
   1.385 +   /* Close handle */
   1.386 +   SDL_RWclose(rw);
   1.387 +   SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   1.388 +   SDL_FreeRW( rw );
   1.389 +   SDLTest_AssertPass("Call to SDL_FreeRW() succeeded");
   1.390 +   fclose(fp);
   1.391 +
   1.392 +   return TEST_COMPLETED;
   1.393 +}
   1.394 +
   1.395 +/* ================= Test References ================== */
   1.396 +
   1.397 +/* RWops test cases */
   1.398 +static const SDLTest_TestCaseReference rwopsTest1 =
   1.399 +		{ (SDLTest_TestCaseFp)rwops_testParamNegative, "rwops_testParamNegative", "Negative test for SDL_RWFromFile parameters", TEST_ENABLED };
   1.400 +
   1.401 +static const SDLTest_TestCaseReference rwopsTest2 =
   1.402 +		{ (SDLTest_TestCaseFp)rwops_testMem, "rwops_testMem", "Tests opening from memory", TEST_ENABLED };
   1.403 +
   1.404 +static const SDLTest_TestCaseReference rwopsTest3 =
   1.405 +		{ (SDLTest_TestCaseFp)rwops_testConstMem, "rwops_testConstMem", "Tests opening from (const) memory", TEST_ENABLED };
   1.406 +
   1.407 +static const SDLTest_TestCaseReference rwopsTest4 =
   1.408 +		{ (SDLTest_TestCaseFp)rwops_testFileRead, "rwops_testFileRead", "Tests reading from a file", TEST_ENABLED };
   1.409 +
   1.410 +static const SDLTest_TestCaseReference rwopsTest5 =
   1.411 +		{ (SDLTest_TestCaseFp)rwops_testFileWrite, "rwops_testFileWrite", "Test writing to a file", TEST_ENABLED };
   1.412 +
   1.413 +static const SDLTest_TestCaseReference rwopsTest6 =
   1.414 +		{ (SDLTest_TestCaseFp)rwops_testFPRead, "rwops_testFPRead", "Test reading from file pointer", TEST_ENABLED };
   1.415 +
   1.416 +static const SDLTest_TestCaseReference rwopsTest7 =
   1.417 +		{ (SDLTest_TestCaseFp)rwops_testFPWrite, "rwops_testFPWrite", "Test writing to file pointer", TEST_ENABLED };
   1.418 +
   1.419 +/* Sequence of RWops test cases */
   1.420 +static const SDLTest_TestCaseReference *rwopsTests[] =  {
   1.421 +	&rwopsTest1, &rwopsTest2, &rwopsTest3, &rwopsTest4, &rwopsTest5, &rwopsTest6, &rwopsTest7, NULL
   1.422 +};
   1.423 +
   1.424 +/* RWops test suite (global) */
   1.425 +SDLTest_TestSuiteReference rwopsTestSuite = {
   1.426 +	"RWops",
   1.427 +	RWopsSetUp,
   1.428 +	rwopsTests,
   1.429 +	RWopsTearDown
   1.430 +};