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