test/testautomation_rwops.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Sun, 30 Dec 2012 19:05:50 -0800
changeset 6781 17a9b53a5ac3
parent 6779 c3b579c78059
child 6814 fccdef204fcf
permissions -rw-r--r--
Fix incorrect use of SDL_FreeRW in rwops suite; add testcase for SDL_AllocRW/SDL_FreeRW to rwops test suite
     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 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 
    22 const char* RWopsReadTestFilename = "rwops_read";
    23 const char* RWopsWriteTestFilename = "rwops_write";
    24 
    25 static const char RWopsHelloWorldTestString[] = "Hello World!";
    26 static const char RWopsHelloWorldCompString[] = "Hello World!";
    27 
    28 /* Fixture */
    29 
    30 void
    31 RWopsSetUp(void *arg)
    32 {
    33 	int fileLen = SDL_strlen(RWopsHelloWorldTestString);
    34 	FILE *handle;
    35 	int writtenLen;
    36 	int result;
    37 
    38 	/* Clean up from previous runs (if any); ignore errors */
    39 	remove(RWopsReadTestFilename);
    40 	remove(RWopsWriteTestFilename);
    41 
    42 	/* Create a test file */
    43 	handle = fopen(RWopsReadTestFilename, "w");
    44 	SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", RWopsReadTestFilename);
    45     if (handle == NULL) return;
    46 
    47 	/* Write some known test into it */
    48 	writtenLen = (int)fwrite(RWopsHelloWorldTestString, 1, fileLen, handle);
    49 	SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", fileLen, writtenLen);
    50 	result = fclose(handle);
    51 	SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
    52 
    53 	SDLTest_AssertPass("Creation of test file completed");
    54 }
    55 
    56 void
    57 RWopsTearDown(void *arg)
    58 {
    59 	int result;
    60 	
    61 	/* Remove the created files to clean up; ignore errors for write filename */
    62 	result = remove(RWopsReadTestFilename);
    63 	SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", RWopsReadTestFilename, result);
    64 	remove(RWopsWriteTestFilename);
    65 
    66 	SDLTest_AssertPass("Cleanup of test files completed");
    67 }
    68 
    69 /**
    70  * @brief Makes sure parameters work properly. Local helper function.
    71  *
    72  * \sa
    73  * http://wiki.libsdl.org/moin.cgi/SDL_RWseek
    74  * http://wiki.libsdl.org/moin.cgi/SDL_RWread
    75  */
    76 void 
    77 _testGenericRWopsValidations(SDL_RWops *rw, int write)
    78 {
    79    char buf[sizeof(RWopsHelloWorldTestString)];
    80    Sint64 i;
    81    int seekPos = SDLTest_RandomIntegerInRange(4, 8);
    82 
    83    /* Clear buffer */
    84    SDL_zero(buf);
    85 
    86    /* Set to start. */
    87    i = SDL_RWseek(rw, 0, RW_SEEK_SET );
    88    SDLTest_AssertPass("Call to SDL_RWseek succeeded");
    89    SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %i", i);
    90 
    91    /* Test write. */
    92    i = SDL_RWwrite(rw, RWopsHelloWorldTestString, sizeof(RWopsHelloWorldTestString)-1, 1);
    93    SDLTest_AssertPass("Call to SDL_RWwrite succeeded");
    94    if (write) {
    95 		SDLTest_AssertCheck(i == (Sint64)1, "Verify result of writing one byte with SDL_RWwrite, expected 1, got %i", i);
    96    }
    97    else {
    98 		SDLTest_AssertCheck(i != (Sint64)1, "Verify result of writing with SDL_RWwrite, expected !=1, got %i", i);
    99    }
   100 
   101    /* Test seek to random position */
   102    i = SDL_RWseek( rw, seekPos, RW_SEEK_SET );
   103    SDLTest_AssertPass("Call to SDL_RWseek succeeded");
   104    SDLTest_AssertCheck(i == (Sint64)seekPos, "Verify seek to %i with SDL_RWseek (RW_SEEK_SET), expected %i, got %i", seekPos, seekPos, i);
   105 
   106    /* Test seek back to start */
   107    i = SDL_RWseek(rw, 0, RW_SEEK_SET );
   108    SDLTest_AssertPass("Call to SDL_RWseek succeeded");
   109    SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %i", i);
   110 
   111    /* Test read */
   112    i = SDL_RWread( rw, buf, 1, sizeof(RWopsHelloWorldTestString)-1 );
   113    SDLTest_AssertPass("Call to SDL_RWread succeeded");
   114    SDLTest_AssertCheck(
   115 	   i == (Sint64)(sizeof(RWopsHelloWorldTestString)-1), 
   116 	   "Verify result from SDL_RWread, expected %i, got %i",
   117 	   sizeof(RWopsHelloWorldTestString)-1,
   118 	   i);
   119    SDLTest_AssertCheck(
   120 	   SDL_memcmp(buf, RWopsHelloWorldTestString, sizeof(RWopsHelloWorldTestString)-1 ) == 0, 
   121 	   "Verify read bytes match expected string, expected '%s', got '%s'", RWopsHelloWorldTestString, buf);
   122 
   123    /* More seek tests. */
   124    i = SDL_RWseek( rw, -4, RW_SEEK_CUR );
   125    SDLTest_AssertPass("Call to SDL_RWseek(...,-4,RW_SEEK_CUR) succeeded");
   126    SDLTest_AssertCheck(
   127 	   i == (Sint64)(sizeof(RWopsHelloWorldTestString)-5), 
   128 	   "Verify seek to -4 with SDL_RWseek (RW_SEEK_CUR), expected %i, got %i",
   129 	   sizeof(RWopsHelloWorldTestString)-5,
   130 	   i);
   131 
   132    i = SDL_RWseek( rw, -1, RW_SEEK_END );
   133    SDLTest_AssertPass("Call to SDL_RWseek(...,-1,RW_SEEK_END) succeeded");
   134    SDLTest_AssertCheck(
   135 	   i == (Sint64)(sizeof(RWopsHelloWorldTestString)-2), 
   136 	   "Verify seek to -1 with SDL_RWseek (RW_SEEK_END), expected %i, got %i",
   137 	   sizeof(RWopsHelloWorldTestString)-2,
   138 	   i);
   139 }
   140 
   141 /*!
   142  * Negative test for SDL_RWFromFile parameters
   143  *
   144  * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
   145  *
   146  */
   147 int
   148 rwops_testParamNegative (void)
   149 {
   150    SDL_RWops *rwops;
   151 
   152    /* These should all fail. */
   153    rwops = SDL_RWFromFile(NULL, NULL);
   154    SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, NULL) succeeded");
   155    SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, NULL) returns NULL");
   156 
   157    rwops = SDL_RWFromFile(NULL, "ab+");
   158    SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, \"ab+\") succeeded");
   159    SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, \"ab+\") returns NULL");
   160 
   161    rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
   162    SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, \"sldfkjsldkfj\") succeeded");
   163    SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, \"sldfkjsldkfj\") returns NULL");
   164 
   165    rwops = SDL_RWFromFile("something", "");
   166    SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", \"\") succeeded");
   167    SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", \"\") returns NULL");
   168 
   169    rwops = SDL_RWFromFile("something", NULL);
   170    SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", NULL) succeeded");
   171    SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", NULL) returns NULL");
   172 
   173    return TEST_COMPLETED;
   174 }
   175 
   176 /**
   177  * @brief Tests opening from memory.
   178  *
   179  * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromMem
   180  */
   181 int
   182 rwops_testMem (void)
   183 {
   184    char mem[sizeof(RWopsHelloWorldTestString)];
   185    SDL_RWops *rw;
   186 
   187    /* Clear buffer */
   188    SDL_zero(mem);
   189 
   190    /* Open */
   191    rw = SDL_RWFromMem(mem, sizeof(RWopsHelloWorldTestString)-1);
   192    SDLTest_AssertPass("Call to SDL_RWFromMem() succeeded");
   193    SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromMem does not return NULL");
   194 
   195    /* Bail out if NULL */
   196    if (rw == NULL) return TEST_ABORTED;
   197 
   198    /* Run generic tests */
   199    _testGenericRWopsValidations(rw, 1);
   200 
   201    /* Close */
   202    SDL_RWclose(rw);
   203    SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   204 
   205    return TEST_COMPLETED;
   206 }
   207 
   208 
   209 /**
   210  * @brief Tests opening from memory.
   211  *
   212  * \sa
   213  * http://wiki.libsdl.org/moin.cgi/SDL_RWFromConstMem
   214  */
   215 int 
   216 rwops_testConstMem (void)
   217 {
   218    SDL_RWops *rw;
   219 
   220    /* Open handle */
   221    rw = SDL_RWFromConstMem( RWopsHelloWorldCompString, sizeof(RWopsHelloWorldCompString)-1 );
   222    SDLTest_AssertPass("Call to SDL_RWFromConstMem() succeeded");
   223    SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromConstMem does not return NULL");
   224 
   225    /* Bail out if NULL */
   226    if (rw == NULL) return TEST_ABORTED;
   227 
   228    /* Run generic tests */
   229    _testGenericRWopsValidations( rw, 0 );
   230 
   231    /* Close handle */
   232    SDL_RWclose(rw);
   233    SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   234 
   235   return TEST_COMPLETED;
   236 }
   237 
   238 
   239 /**
   240  * @brief Tests reading from file.
   241  *
   242  * \sa
   243  * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
   244  * http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
   245  */
   246 int
   247 rwops_testFileRead(void)
   248 {
   249    SDL_RWops *rw;
   250 
   251    /* Read test. */
   252    rw = SDL_RWFromFile(RWopsReadTestFilename, "r");
   253    SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"r\") succeeded");
   254    SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in read mode does not return NULL");
   255 
   256    // Bail out if NULL
   257    if (rw == NULL) return TEST_ABORTED;
   258 
   259    /* Run generic tests */
   260    _testGenericRWopsValidations( rw, 0 );
   261 
   262    /* Close handle */
   263    SDL_RWclose(rw);
   264    SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   265 
   266    return TEST_COMPLETED;
   267 }
   268 
   269 /**
   270  * @brief Tests writing from memory.
   271  *
   272  * \sa
   273  * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
   274  * http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
   275  */
   276 int
   277 rwops_testFileWrite(void)
   278 {
   279    SDL_RWops *rw;
   280 
   281    /* Write test. */
   282    rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
   283    SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\") succeeded");
   284    SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
   285 
   286    // Bail out if NULL
   287    if (rw == NULL) return TEST_ABORTED;
   288 
   289    /* Run generic tests */
   290    _testGenericRWopsValidations( rw, 1 );
   291 
   292    /* Close handle */
   293    SDL_RWclose(rw);
   294    SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   295 
   296    return TEST_COMPLETED;
   297 }
   298 
   299 
   300 /**
   301  * @brief Tests reading from file handle
   302  *
   303  * \sa
   304  * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFP
   305  * http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
   306  *
   307  */
   308 int
   309 rwops_testFPRead(void)
   310 {
   311    FILE *fp;
   312    SDL_RWops *rw;
   313 
   314    /* Run read tests. */
   315    fp = fopen(RWopsReadTestFilename, "r");
   316    SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in read mode is not NULL", RWopsReadTestFilename);
   317 
   318    /* Bail out if NULL */
   319    if (fp == NULL) return TEST_ABORTED;
   320 
   321    /* Open */
   322    rw = SDL_RWFromFP( fp, SDL_TRUE );
   323    SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded");
   324    SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in read mode does not return NULL");
   325 
   326    /* Bail out if NULL */
   327    if (rw == NULL) {
   328      fclose(fp);
   329      return TEST_ABORTED;
   330    }
   331 
   332    /* Run generic tests */
   333    _testGenericRWopsValidations( rw, 0 );
   334 
   335    /* Close handle */
   336    SDL_RWclose(rw);
   337    SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   338    fclose(fp);
   339 
   340    return TEST_COMPLETED;
   341 }
   342 
   343 
   344 /**
   345  * @brief Tests writing to file handle
   346  *
   347  * \sa
   348  * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFP
   349  * http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
   350  *
   351  */
   352 int
   353 rwops_testFPWrite(void)
   354 {
   355    FILE *fp;
   356    SDL_RWops *rw;
   357 
   358    /* Run write tests. */
   359    fp = fopen(RWopsWriteTestFilename, "w+");
   360    SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in write mode is not NULL", RWopsWriteTestFilename);
   361 
   362    /* Bail out if NULL */
   363    if (fp == NULL) return TEST_ABORTED;
   364 
   365    /* Open */
   366    rw = SDL_RWFromFP( fp, SDL_TRUE );
   367    SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded");
   368    SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in write mode does not return NULL");
   369 
   370    /* Bail out if NULL */
   371    if (rw == NULL) {
   372      fclose(fp);
   373      return TEST_ABORTED;
   374    }
   375 
   376    /* Run generic tests */
   377    _testGenericRWopsValidations( rw, 1 );
   378 
   379    /* Close handle */
   380    SDL_RWclose(rw);
   381    SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
   382    fclose(fp);
   383 
   384    return TEST_COMPLETED;
   385 }
   386 
   387 /**
   388  * @brief Tests alloc and free RW context.
   389  *
   390  * \sa http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
   391  * \sa http://wiki.libsdl.org/moin.cgi/SDL_AllocRW
   392  */
   393 int
   394 rwops_testAllocFree (void)
   395 {
   396    /* Allocate context */
   397    SDL_RWops *rw = SDL_AllocRW();
   398    SDLTest_AssertPass("Call to SDL_AllocRW() succeeded");
   399    SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_AllocRW() is not NULL");
   400    if (rw==NULL) return TEST_ABORTED;
   401           
   402    /* Free context again */
   403    SDL_FreeRW(rw);
   404    SDLTest_AssertPass("Call to SDL_FreeRW() succeeded");
   405 
   406    return TEST_COMPLETED;
   407 }
   408 
   409 
   410 /* ================= Test References ================== */
   411 
   412 /* RWops test cases */
   413 static const SDLTest_TestCaseReference rwopsTest1 =
   414 		{ (SDLTest_TestCaseFp)rwops_testParamNegative, "rwops_testParamNegative", "Negative test for SDL_RWFromFile parameters", TEST_ENABLED };
   415 
   416 static const SDLTest_TestCaseReference rwopsTest2 =
   417 		{ (SDLTest_TestCaseFp)rwops_testMem, "rwops_testMem", "Tests opening from memory", TEST_ENABLED };
   418 
   419 static const SDLTest_TestCaseReference rwopsTest3 =
   420 		{ (SDLTest_TestCaseFp)rwops_testConstMem, "rwops_testConstMem", "Tests opening from (const) memory", TEST_ENABLED };
   421 
   422 static const SDLTest_TestCaseReference rwopsTest4 =
   423 		{ (SDLTest_TestCaseFp)rwops_testFileRead, "rwops_testFileRead", "Tests reading from a file", TEST_ENABLED };
   424 
   425 static const SDLTest_TestCaseReference rwopsTest5 =
   426 		{ (SDLTest_TestCaseFp)rwops_testFileWrite, "rwops_testFileWrite", "Test writing to a file", TEST_ENABLED };
   427 
   428 static const SDLTest_TestCaseReference rwopsTest6 =
   429 		{ (SDLTest_TestCaseFp)rwops_testFPRead, "rwops_testFPRead", "Test reading from file pointer", TEST_ENABLED };
   430 
   431 static const SDLTest_TestCaseReference rwopsTest7 =
   432 		{ (SDLTest_TestCaseFp)rwops_testFPWrite, "rwops_testFPWrite", "Test writing to file pointer", TEST_ENABLED };
   433 
   434 static const SDLTest_TestCaseReference rwopsTest8 =
   435 		{ (SDLTest_TestCaseFp)rwops_testAllocFree, "rwops_testAllocFree", "Test alloc and free of RW context", TEST_ENABLED };
   436 
   437 /* Sequence of RWops test cases */
   438 static const SDLTest_TestCaseReference *rwopsTests[] =  {
   439 	&rwopsTest1, &rwopsTest2, &rwopsTest3, &rwopsTest4, &rwopsTest5, &rwopsTest6, &rwopsTest7, &rwopsTest8, NULL
   440 };
   441 
   442 /* RWops test suite (global) */
   443 SDLTest_TestSuiteReference rwopsTestSuite = {
   444 	"RWops",
   445 	RWopsSetUp,
   446 	rwopsTests,
   447 	RWopsTearDown
   448 };