Better output. gsoc2009_unit_tests
authorEdgar Simo <bobbens@gmail.com>
Sat, 20 Jun 2009 16:46:58 +0000
branchgsoc2009_unit_tests
changeset 3713a34bab848c7e
parent 3712 916469fbdc29
child 3714 1b710c8e4cfb
Better output.
Newline handling.
rwops test handles ConstMem, FP and File.
test/automated/Makefile
test/automated/SDL_at.c
test/automated/rwops.c
test/automated/rwops/read
test/automated/rwops/rwops.c
     1.1 --- a/test/automated/Makefile	Fri Jun 19 18:57:23 2009 +0000
     1.2 +++ b/test/automated/Makefile	Sat Jun 20 16:46:58 2009 +0000
     1.3 @@ -1,12 +1,12 @@
     1.4  
     1.5  
     1.6 -CFLAGS  := `sdl-config --cflags`
     1.7 +CFLAGS  := -I. `sdl-config --cflags`
     1.8  LDFLAGS := `sdl-config --libs`
     1.9  
    1.10  COMMON_SRC     := SDL_at.c
    1.11  COMMON_INCLUDE := SDL_at.h
    1.12  
    1.13 -TESTS_ALL := rwops
    1.14 +TESTS_ALL := rwops/rwops
    1.15  
    1.16  
    1.17  .PHONY: all clean test
    1.18 @@ -15,10 +15,10 @@
    1.19  all: $(TESTS_ALL)
    1.20  
    1.21  test:
    1.22 -	@./rwops
    1.23 +	@./rwops/rwops
    1.24  
    1.25 -rwops: rwops.c $(COMMON_INCLUDE)
    1.26 -	$(CC) $(CFLAGS) $(LDFLAGS) -o rwops rwops.c $(COMMON_SRC)
    1.27 +rwops/rwops: rwops/rwops.c $(COMMON_INCLUDE)
    1.28 +	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ rwops/rwops.c $(COMMON_SRC)
    1.29  
    1.30  clean:
    1.31  	$(RM) $(TESTS_ALL)
     2.1 --- a/test/automated/SDL_at.c	Fri Jun 19 18:57:23 2009 +0000
     2.2 +++ b/test/automated/SDL_at.c	Sat Jun 20 16:46:58 2009 +0000
     2.3 @@ -71,7 +71,7 @@
     2.4     if (verbose) {
     2.5        if (at_failure > 0) {
     2.6           SDL_ATprint( "%s : Failed %d out of %d testcases!\n",
     2.7 -               at_suite_msg, at_failure, at_success );
     2.8 +               at_suite_msg, at_failure, at_failure+at_success );
     2.9        }
    2.10        else {
    2.11           SDL_ATprint( "%s : All tests successful (%d)\n",
    2.12 @@ -94,12 +94,12 @@
    2.13  {
    2.14     /* Do not open twice. */
    2.15     if (at_test_msg) {
    2.16 -      SDL_ATprint( "AT testcase '%s' not closed before opening testcase '%s'",
    2.17 +      SDL_ATprint( "AT testcase '%s' not closed before opening testcase '%s'\n",
    2.18              at_test_msg, testcase );
    2.19     }
    2.20     /* Must have a name. */
    2.21     if (testcase == NULL) {
    2.22 -      SDL_ATprint( "AT testcase does not have a name.");
    2.23 +      SDL_ATprint( "AT testcase does not have a name.\n");
    2.24     }
    2.25     at_test_msg = testcase;
    2.26  }
    2.27 @@ -112,7 +112,7 @@
    2.28  {
    2.29     /* Make sure initialized. */
    2.30     if (at_test_msg == NULL) {
    2.31 -      SDL_ATprint("Ended testcase without initializing.");
    2.32 +      SDL_ATprint("Ended testcase without initializing.\n");
    2.33        return;
    2.34     }
    2.35  
    2.36 @@ -134,7 +134,7 @@
    2.37  {
    2.38     /* Condition failed. */
    2.39     if (!condition) {
    2.40 -      SDL_ATprint( "%s [%s] : %s", at_suite_msg, at_test_msg, msg );
    2.41 +      SDL_ATprint( "%s [%s] : %s\n", at_suite_msg, at_test_msg, msg );
    2.42        SDL_ATendWith(0);
    2.43     }
    2.44     return !condition;
     3.1 --- a/test/automated/rwops.c	Fri Jun 19 18:57:23 2009 +0000
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,70 +0,0 @@
     3.4 -/**
     3.5 - * Automated SDL_RWops test.
     3.6 - *
     3.7 - * Written by Edgar Simo "bobbens"
     3.8 - *
     3.9 - * Released under Public Domain.
    3.10 - */
    3.11 -
    3.12 -
    3.13 -#include "SDL.h"
    3.14 -#include "SDL_at.h"
    3.15 -
    3.16 -
    3.17 -static const char hello_world[] = "Hello World!";
    3.18 -
    3.19 -
    3.20 -/**
    3.21 - * @brief Tests opening from memory.
    3.22 - */
    3.23 -static void rwops_testMem (void)
    3.24 -{
    3.25 -   SDL_RWops *rw;
    3.26 -   char mem[sizeof(hello_world)], buf[sizeof(hello_world)];
    3.27 -   int i;
    3.28 -
    3.29 -   /* Begin testcase. */
    3.30 -   SDL_ATbegin( "SDL_RWFromMem" );
    3.31 -
    3.32 -   /* Open. */
    3.33 -   rw = SDL_RWFromMem( mem, sizeof(mem) );
    3.34 -   if (SDL_ATassert( "Opening memory with SDL_RWFromMem", rw != NULL ))
    3.35 -      return;
    3.36 -
    3.37 -   /* Test write. */
    3.38 -   i = SDL_RWwrite( rw, hello_world, sizeof(hello_world), 1 );
    3.39 -   if (SDL_ATassert( "Writing with SDL_RWwrite", i == 1 ))
    3.40 -      return;
    3.41 -
    3.42 -   /* Test seek. */
    3.43 -   i = SDL_RWseek( rw, 0, RW_SEEK_SET );
    3.44 -   if (SDL_ATassert( "Seeking with SDL_RWseek", i == 0 ))
    3.45 -      return;
    3.46 -
    3.47 -   /* Test read. */
    3.48 -   i = SDL_RWread( rw, buf, sizeof(hello_world), 1 );
    3.49 -   if (SDL_ATassert( "Reading with SDL_RWread", i == 1 ))
    3.50 -      return;
    3.51 -   if (SDL_ATassert( "Memory read does not match memory written",
    3.52 -            memcmp( buf, hello_world, sizeof(hello_world) ) == 0 ))
    3.53 -      return;
    3.54 -
    3.55 -   /* Close. */
    3.56 -   SDL_FreeRW( rw );
    3.57 -
    3.58 -   /* End testcase. */
    3.59 -   SDL_ATend();
    3.60 -}
    3.61 -
    3.62 -
    3.63 -/**
    3.64 - * @brief Entry point.
    3.65 - */
    3.66 -int main( int argc, const char *argv[] )
    3.67 -{
    3.68 -   SDL_ATinit( "SDL_RWops" );
    3.69 -
    3.70 -   rwops_testMem();
    3.71 -
    3.72 -   return SDL_ATfinish(1);
    3.73 -}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/automated/rwops/read	Sat Jun 20 16:46:58 2009 +0000
     4.3 @@ -0,0 +1,1 @@
     4.4 +Hello World!
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/automated/rwops/rwops.c	Sat Jun 20 16:46:58 2009 +0000
     5.3 @@ -0,0 +1,200 @@
     5.4 +/**
     5.5 + * Automated SDL_RWops test.
     5.6 + *
     5.7 + * Written by Edgar Simo "bobbens"
     5.8 + *
     5.9 + * Released under Public Domain.
    5.10 + */
    5.11 +
    5.12 +
    5.13 +#include "SDL.h"
    5.14 +#include "SDL_at.h"
    5.15 +
    5.16 +
    5.17 +static const char hello_world[] = "Hello World!";
    5.18 +
    5.19 +
    5.20 +/**
    5.21 + * @brief Does a generic rwops test.
    5.22 + *
    5.23 + * RWops should have "Hello World!" in it already if write is disabled.
    5.24 + *
    5.25 + *    @param write Test writing also.
    5.26 + *    @return 1 if an assert is failed.
    5.27 + */
    5.28 +static int rwops_testGeneric( SDL_RWops *rw, int write )
    5.29 +{
    5.30 +   char buf[sizeof(hello_world)];
    5.31 +   int i;
    5.32 +
    5.33 +   if (write) {
    5.34 +      /* Test write. */
    5.35 +      i = SDL_RWwrite( rw, hello_world, sizeof(hello_world)-1, 1 );
    5.36 +      if (SDL_ATassert( "Writing with SDL_RWwrite", i == 1 ))
    5.37 +         return 1;
    5.38 +   }
    5.39 +
    5.40 +   /* Test seek. */
    5.41 +   i = SDL_RWseek( rw, 6, RW_SEEK_SET );
    5.42 +   if (SDL_ATassert( "Seeking with SDL_RWseek", i == 6 ))
    5.43 +      return 1;
    5.44 +
    5.45 +   /* Test seek. */
    5.46 +   i = SDL_RWseek( rw, 0, RW_SEEK_SET );
    5.47 +   if (SDL_ATassert( "Seeking with SDL_RWseek", i == 0 ))
    5.48 +      return 1;
    5.49 +
    5.50 +   /* Test read. */
    5.51 +   i = SDL_RWread( rw, buf, 1, sizeof(hello_world)-1 );
    5.52 +   if (i != sizeof(hello_world)-1)
    5.53 +      printf("%s\n", SDL_GetError());
    5.54 +   if (SDL_ATassert( "Reading with SDL_RWread", i == sizeof(hello_world)-1 ))
    5.55 +      return 1;
    5.56 +   if (SDL_ATassert( "Memory read does not match memory written",
    5.57 +            memcmp( buf, hello_world, sizeof(hello_world)-1 ) == 0 ))
    5.58 +      return 1;
    5.59 +
    5.60 +   return 0;
    5.61 +}
    5.62 +
    5.63 +
    5.64 +/**
    5.65 + * @brief Tests opening from memory.
    5.66 + */
    5.67 +static void rwops_testMem (void)
    5.68 +{
    5.69 +   char mem[sizeof(hello_world)];
    5.70 +   SDL_RWops *rw;
    5.71 +
    5.72 +   /* Begin testcase. */
    5.73 +   SDL_ATbegin( "SDL_RWFromMem" );
    5.74 +
    5.75 +   /* Open. */
    5.76 +   rw = SDL_RWFromMem( mem, sizeof(mem) );
    5.77 +   if (SDL_ATassert( "Opening memory with SDL_RWFromMem", rw != NULL ))
    5.78 +      return;
    5.79 +
    5.80 +   /* Run generic tests. */
    5.81 +   if (rwops_testGeneric( rw, 1 ))
    5.82 +      return;
    5.83 +
    5.84 +   /* Close. */
    5.85 +   SDL_FreeRW( rw );
    5.86 +
    5.87 +   /* End testcase. */
    5.88 +   SDL_ATend();
    5.89 +}
    5.90 +
    5.91 +
    5.92 +static const char const_mem[] = "Hello World!";
    5.93 +/**
    5.94 + * @brief Tests opening from memory.
    5.95 + */
    5.96 +static void rwops_testConstMem (void)
    5.97 +{
    5.98 +   SDL_RWops *rw;
    5.99 +
   5.100 +   /* Begin testcase. */
   5.101 +   SDL_ATbegin( "SDL_RWFromConstMem" );
   5.102 +
   5.103 +   /* Open. */
   5.104 +   rw = SDL_RWFromConstMem( const_mem, sizeof(const_mem) );
   5.105 +   if (SDL_ATassert( "Opening memory with SDL_RWFromConstMem", rw != NULL ))
   5.106 +      return;
   5.107 +
   5.108 +   /* Run generic tests. */
   5.109 +   if (rwops_testGeneric( rw, 0 ))
   5.110 +      return;
   5.111 +
   5.112 +   /* Close. */
   5.113 +   SDL_FreeRW( rw );
   5.114 +
   5.115 +   /* End testcase. */
   5.116 +   SDL_ATend();
   5.117 +}
   5.118 +
   5.119 +
   5.120 +/**
   5.121 + * @brief Tests opening from memory.
   5.122 + */
   5.123 +static void rwops_testFile (void)
   5.124 +{
   5.125 +   SDL_RWops *rw;
   5.126 +   int i;
   5.127 +
   5.128 +   /* Begin testcase. */
   5.129 +   SDL_ATbegin( "SDL_RWFromFile" );
   5.130 +
   5.131 +   /* Open. */
   5.132 +   rw = SDL_RWFromFile( "rwops/read", "r" );
   5.133 +   if (SDL_ATassert( "Opening memory with SDL_RWFromFile", rw != NULL ))
   5.134 +      return;
   5.135 +
   5.136 +   /* Test writing. */
   5.137 +   i = SDL_RWwrite( rw, hello_world, sizeof(hello_world), 1 );
   5.138 +   if (SDL_ATassert( "Writing with SDL_RWwrite", i == 0 ))
   5.139 +      return;
   5.140 +
   5.141 +   /* Run generic tests. */
   5.142 +   if (rwops_testGeneric( rw, 0 ))
   5.143 +      return;
   5.144 +
   5.145 +   /* Close. */
   5.146 +   SDL_FreeRW( rw );
   5.147 +
   5.148 +   /* End testcase. */
   5.149 +   SDL_ATend();
   5.150 +}
   5.151 +
   5.152 +
   5.153 +/**
   5.154 + * @brief Tests opening from memory.
   5.155 + */
   5.156 +static void rwops_testFP (void)
   5.157 +{
   5.158 +#ifdef HAVE_STDIO_H
   5.159 +   FILE *fp;
   5.160 +   SDL_RWops *rw;
   5.161 +   int i;
   5.162 +
   5.163 +   /* Begin testcase. */
   5.164 +   SDL_ATbegin( "SDL_RWFromFP" );
   5.165 +
   5.166 +   /* Open. */
   5.167 +   fp = fopen( "rwops/write", "w+" );
   5.168 +   if (fp == NULL) {
   5.169 +      SDL_ATprint("Failed to open file rwops/write");
   5.170 +      SDL_ATend();
   5.171 +      return;
   5.172 +   }
   5.173 +   rw = SDL_RWFromFP( fp, 1 );
   5.174 +   if (SDL_ATassert( "Opening memory with SDL_RWFromFP", rw != NULL ))
   5.175 +      return;
   5.176 +
   5.177 +   /* Run generic tests. */
   5.178 +   if (rwops_testGeneric( rw, 1 ))
   5.179 +      return;
   5.180 +
   5.181 +   /* Close. */
   5.182 +   SDL_FreeRW( rw );
   5.183 +
   5.184 +   /* End testcase. */
   5.185 +   SDL_ATend();
   5.186 +#endif /* HAVE_STDIO_H */
   5.187 +}
   5.188 +
   5.189 +
   5.190 +/**
   5.191 + * @brief Entry point.
   5.192 + */
   5.193 +int main( int argc, const char *argv[] )
   5.194 +{
   5.195 +   SDL_ATinit( "SDL_RWops" );
   5.196 +
   5.197 +   rwops_testMem();
   5.198 +   rwops_testConstMem();
   5.199 +   rwops_testFile();
   5.200 +   rwops_testFP();
   5.201 +
   5.202 +   return SDL_ATfinish(1);
   5.203 +}