First revision of the automated test suite. gsoc2009_unit_tests
authorEdgar Simo <bobbens@gmail.com>
Fri, 19 Jun 2009 18:53:58 +0000
branchgsoc2009_unit_tests
changeset 371180839fc6b8e1
parent 3710 8600c345cd77
child 3712 916469fbdc29
First revision of the automated test suite.
test/automated/Makefile
test/automated/SDL_at.c
test/automated/SDL_at.h
test/automated/rwops.c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/automated/Makefile	Fri Jun 19 18:53:58 2009 +0000
     1.3 @@ -0,0 +1,16 @@
     1.4 +
     1.5 +
     1.6 +CFLAGS  := `sdl-config --cflags`
     1.7 +LDFLAGS := `sdl-config --libs`
     1.8 +
     1.9 +COMMON_SRC     := SDL_at.c
    1.10 +COMMON_INCLUDE := SDL_at.h
    1.11 +
    1.12 +
    1.13 +.PHONY: all
    1.14 +
    1.15 +
    1.16 +all: rwops
    1.17 +
    1.18 +rwops: rwops.c $(COMMON_INCLUDE)
    1.19 +	$(CC) $(CFLAGS) $(LDFLAGS) -o rwops rwops.c $(COMMON_SRC)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/automated/SDL_at.c	Fri Jun 19 18:53:58 2009 +0000
     2.3 @@ -0,0 +1,173 @@
     2.4 +/*
     2.5 + * Common code for automated test suite.
     2.6 + *
     2.7 + * Written by Edgar Simo "bobbens"
     2.8 + *
     2.9 + * Released under Public Domain.
    2.10 + */
    2.11 +
    2.12 +
    2.13 +#include "SDL_at.h"
    2.14 +
    2.15 +#include <stdio.h>
    2.16 +#include <stdarg.h>
    2.17 +
    2.18 +
    2.19 +/*
    2.20 + * Internal usage SDL_AT variables.
    2.21 + */
    2.22 +static const char *at_suite_msg = NULL; /**< Testsuite message. */
    2.23 +static const char *at_test_msg = NULL; /**< Testcase message. */
    2.24 +static int at_success = 0; /**< Number of successful testcases. */
    2.25 +static int at_failure = 0; /**< Number of failed testcases. */
    2.26 +
    2.27 +
    2.28 +/**
    2.29 + * @brief Cleans up the automated testsuite state.
    2.30 + */
    2.31 +static void SDL_ATcleanup (void)
    2.32 +{
    2.33 +   at_suite_msg = NULL;
    2.34 +   at_test_msg = NULL;
    2.35 +   at_success = 0;
    2.36 +   at_failure = 0;
    2.37 +}
    2.38 +
    2.39 +
    2.40 +/**
    2.41 + * @brief Begin testsuite.
    2.42 + */
    2.43 +void SDL_ATinit( const char *suite )
    2.44 +{
    2.45 +   /* Do not open twice. */
    2.46 +   if (at_suite_msg) {
    2.47 +      SDL_ATprint( "AT suite '%s' not closed before opening suite '%s'\n",
    2.48 +            at_suite_msg, suite );
    2.49 +   }
    2.50 +   /* Must have a name. */
    2.51 +   if (suite == NULL) {
    2.52 +      SDL_ATprint( "AT testsuite does not have a name.\n");
    2.53 +   }
    2.54 +   SDL_ATcleanup();
    2.55 +   at_suite_msg = suite;
    2.56 +}
    2.57 +
    2.58 +
    2.59 +/**
    2.60 + * @brief Finish testsuite.
    2.61 + */
    2.62 +int SDL_ATfinish( int verbose )
    2.63 +{
    2.64 +   int failed;
    2.65 +
    2.66 +   /* Make sure initialized. */
    2.67 +   if (at_suite_msg == NULL) {
    2.68 +      SDL_ATprint("Ended testcase without initializing.\n");
    2.69 +      return;
    2.70 +   }
    2.71 +
    2.72 +   /* Display message if verbose on failed. */
    2.73 +   failed = at_failure;
    2.74 +   if (verbose) {
    2.75 +      if (at_failure > 0) {
    2.76 +         SDL_ATprint( "%s : Failed %d out of %d testcases!\n",
    2.77 +               at_suite_msg, at_failure, at_success );
    2.78 +      }
    2.79 +      else {
    2.80 +         SDL_ATprint( "%s : All tests successful (%d)\n",
    2.81 +               at_suite_msg, at_success );
    2.82 +      }
    2.83 +   }
    2.84 +
    2.85 +   /* Clean up. */
    2.86 +   SDL_ATcleanup();
    2.87 +
    2.88 +   /* Return failed. */
    2.89 +   return failed;
    2.90 +}
    2.91 +
    2.92 +
    2.93 +/**
    2.94 + * @brief Begin testcase.
    2.95 + */
    2.96 +void SDL_ATbegin( const char *testcase )
    2.97 +{
    2.98 +   /* Do not open twice. */
    2.99 +   if (at_test_msg) {
   2.100 +      SDL_ATprint( "AT testcase '%s' not closed before opening testcase '%s'",
   2.101 +            at_test_msg, testcase );
   2.102 +   }
   2.103 +   /* Must have a name. */
   2.104 +   if (testcase == NULL) {
   2.105 +      SDL_ATprint( "AT testcase does not have a name.");
   2.106 +   }
   2.107 +   at_test_msg = testcase;
   2.108 +}
   2.109 +
   2.110 +
   2.111 +/**
   2.112 + * @brief Ends the testcase with a succes or failure.
   2.113 + */
   2.114 +static void SDL_ATendWith( int success )
   2.115 +{
   2.116 +   /* Make sure initialized. */
   2.117 +   if (at_test_msg == NULL) {
   2.118 +      SDL_ATprint("Ended testcase without initializing.");
   2.119 +      return;
   2.120 +   }
   2.121 +
   2.122 +   /* Mark as success or failure. */
   2.123 +   if (success)
   2.124 +      at_success++;
   2.125 +   else
   2.126 +      at_failure++;
   2.127 +
   2.128 +   /* Clean up. */
   2.129 +   at_test_msg = NULL;
   2.130 +}
   2.131 +
   2.132 +
   2.133 +/**
   2.134 + * @brief Testcase test.
   2.135 + */
   2.136 +int SDL_ATassert( const char *msg, int condition )
   2.137 +{
   2.138 +   /* Condition failed. */
   2.139 +   if (!condition) {
   2.140 +      SDL_ATprint( "%s [%s] : %s", at_suite_msg, at_test_msg, msg );
   2.141 +      SDL_ATendWith(0);
   2.142 +   }
   2.143 +   return !condition;
   2.144 +}
   2.145 +
   2.146 +
   2.147 +/**
   2.148 + * @brief End testcase.
   2.149 + */
   2.150 +void SDL_ATend (void)
   2.151 +{
   2.152 +   SDL_ATendWith(1);
   2.153 +}
   2.154 +
   2.155 +
   2.156 +/**
   2.157 + * @brief Displays a message.
   2.158 + */
   2.159 +int SDL_ATprint( const char *msg, ... )
   2.160 +{
   2.161 +   va_list ap;
   2.162 +   int ret;
   2.163 +
   2.164 +   /* Make sure there is something to print. */
   2.165 +   if (msg == NULL)
   2.166 +      return;
   2.167 +   else {
   2.168 +      va_start(ap, msg);
   2.169 +      ret = vprintf(msg, ap);
   2.170 +      va_end(ap);
   2.171 +   }
   2.172 +
   2.173 +   return ret;
   2.174 +}
   2.175 +
   2.176 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/automated/SDL_at.h	Fri Jun 19 18:53:58 2009 +0000
     3.3 @@ -0,0 +1,97 @@
     3.4 +/*
     3.5 + * Common code for automated test suite.
     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 +/**
    3.14 + * @file SDL_at.h
    3.15 + *
    3.16 + * @brief Handles automatic testing functionality.
    3.17 + *
    3.18 + * You create a testsuite and then run multiple testcases within that suite.
    3.19 + *  Syntax is similar to OpenGL. An example would be:
    3.20 + *
    3.21 + * @code
    3.22 + * int f;
    3.23 + * SDL_ATinit( "My testsuite" );
    3.24 + *
    3.25 + * SDL_ATbegin( "My first testcase" );
    3.26 + * if (!SDL_ATassert( "Trying '1+1=2'.", (1+1)==2))
    3.27 + *    SDL_ATend();
    3.28 + *
    3.29 + * SDL_ATbegin( "My second testcase" );
    3.30 + * if (!SDL_ATassert( "Trying '4/2=2'.", (4/2)==2))
    3.31 + *    SDL_ATend();
    3.32 + *
    3.33 + * f = SDL_ATend();
    3.34 + * @endcode
    3.35 + *
    3.36 + * @author Edgar Simo "bobbens"
    3.37 + */
    3.38 +
    3.39 +
    3.40 +#ifndef _SDL_AT_H
    3.41 +#  define _SDL_AT_H
    3.42 +
    3.43 +
    3.44 +/*
    3.45 + * Suite level actions.
    3.46 + */
    3.47 +/**
    3.48 + * @brief Starts the testsuite.
    3.49 + *
    3.50 + *    @param suite Name of the suite to start testing.
    3.51 + */
    3.52 +void SDL_ATinit( const char *suite );
    3.53 +/**
    3.54 + * @brief Finishes the testsuite printing out global results if verbose.
    3.55 + *
    3.56 + *    @param verbose Displays global results.
    3.57 + */
    3.58 +int SDL_ATfinish( int verbose );
    3.59 +
    3.60 +
    3.61 +/*
    3.62 + * Testcase level actions.
    3.63 + */
    3.64 +/**
    3.65 + * @brief Begins a testcase.
    3.66 + *
    3.67 + *    @param testcase Name of the testcase to begin.
    3.68 + */
    3.69 +void SDL_ATbegin( const char *testcase );
    3.70 +/**
    3.71 + * @brief Checks a condition in the testcase.
    3.72 + *
    3.73 + * Will automatically call SDL_ATend if the condition isn't met.
    3.74 + *
    3.75 + *    @param msg Message to display for failure.
    3.76 + *    @param condition Condition to make sure is true.
    3.77 + *    @return Returns 1 if the condition isn't met.
    3.78 + */
    3.79 +int SDL_ATassert( const char *msg, int condition );
    3.80 +/**
    3.81 + * @brief Ends a testcase.
    3.82 + */
    3.83 +void SDL_ATend (void);
    3.84 +
    3.85 +
    3.86 +/*
    3.87 + * Misc functions.
    3.88 + */
    3.89 +/**
    3.90 + * @brief Prints some text.
    3.91 + *
    3.92 + *    @param msg printf formatted string to display.
    3.93 + *    @return Number of character printed.
    3.94 + */
    3.95 +int SDL_ATprint( const char *msg, ... );
    3.96 +
    3.97 +
    3.98 +#endif /* _SDL_AT_H */
    3.99 +
   3.100 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/automated/rwops.c	Fri Jun 19 18:53:58 2009 +0000
     4.3 @@ -0,0 +1,70 @@
     4.4 +/**
     4.5 + * Automated SDL_RWops test.
     4.6 + *
     4.7 + * Written by Edgar Simo "bobbens"
     4.8 + *
     4.9 + * Released under Public Domain.
    4.10 + */
    4.11 +
    4.12 +
    4.13 +#include "SDL.h"
    4.14 +#include "SDL_at.h"
    4.15 +
    4.16 +
    4.17 +static const char hello_world[] = "Hello World!";
    4.18 +
    4.19 +
    4.20 +/**
    4.21 + * @brief Tests opening from memory.
    4.22 + */
    4.23 +static void rwops_testMem (void)
    4.24 +{
    4.25 +   SDL_RWops *rw;
    4.26 +   char mem[sizeof(hello_world)], buf[sizeof(hello_world)];
    4.27 +   int i;
    4.28 +
    4.29 +   /* Begin testcase. */
    4.30 +   SDL_ATbegin( "SDL_RWFromMem" );
    4.31 +
    4.32 +   /* Open. */
    4.33 +   rw = SDL_RWFromMem( mem, sizeof(mem) );
    4.34 +   if (SDL_ATassert( "Opening memory with SDL_RWFromMem", rw != NULL ))
    4.35 +      return;
    4.36 +
    4.37 +   /* Test write. */
    4.38 +   i = SDL_RWwrite( rw, hello_world, sizeof(hello_world), 1 );
    4.39 +   if (SDL_ATassert( "Writing with SDL_RWwrite", i == 1 ))
    4.40 +      return;
    4.41 +
    4.42 +   /* Test seek. */
    4.43 +   i = SDL_RWseek( rw, 0, RW_SEEK_SET );
    4.44 +   if (SDL_ATassert( "Seeking with SDL_RWseek", i == 0 ))
    4.45 +      return;
    4.46 +
    4.47 +   /* Test read. */
    4.48 +   i = SDL_RWread( rw, buf, sizeof(hello_world), 1 );
    4.49 +   if (SDL_ATassert( "Reading with SDL_RWread", i == 1 ))
    4.50 +      return;
    4.51 +   if (SDL_ATassert( "Memory read does not match memory written",
    4.52 +            memcmp( buf, hello_world, sizeof(hello_world) ) == 0 ))
    4.53 +      return;
    4.54 +
    4.55 +   /* Close. */
    4.56 +   SDL_FreeRW( rw );
    4.57 +
    4.58 +   /* End testcase. */
    4.59 +   SDL_ATend();
    4.60 +}
    4.61 +
    4.62 +
    4.63 +/**
    4.64 + * @brief Entry point.
    4.65 + */
    4.66 +int main( int argc, const char *argv[] )
    4.67 +{
    4.68 +   SDL_ATinit( "SDL_RWops" );
    4.69 +
    4.70 +   rwops_testMem();
    4.71 +
    4.72 +   return SDL_ATfinish(1);
    4.73 +}