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 3711 80839fc6b8e1
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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/automated/Makefile	Fri Jun 19 18:53:58 2009 +0000
@@ -0,0 +1,16 @@
+
+
+CFLAGS  := `sdl-config --cflags`
+LDFLAGS := `sdl-config --libs`
+
+COMMON_SRC     := SDL_at.c
+COMMON_INCLUDE := SDL_at.h
+
+
+.PHONY: all
+
+
+all: rwops
+
+rwops: rwops.c $(COMMON_INCLUDE)
+	$(CC) $(CFLAGS) $(LDFLAGS) -o rwops rwops.c $(COMMON_SRC)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/automated/SDL_at.c	Fri Jun 19 18:53:58 2009 +0000
@@ -0,0 +1,173 @@
+/*
+ * Common code for automated test suite.
+ *
+ * Written by Edgar Simo "bobbens"
+ *
+ * Released under Public Domain.
+ */
+
+
+#include "SDL_at.h"
+
+#include <stdio.h>
+#include <stdarg.h>
+
+
+/*
+ * Internal usage SDL_AT variables.
+ */
+static const char *at_suite_msg = NULL; /**< Testsuite message. */
+static const char *at_test_msg = NULL; /**< Testcase message. */
+static int at_success = 0; /**< Number of successful testcases. */
+static int at_failure = 0; /**< Number of failed testcases. */
+
+
+/**
+ * @brief Cleans up the automated testsuite state.
+ */
+static void SDL_ATcleanup (void)
+{
+   at_suite_msg = NULL;
+   at_test_msg = NULL;
+   at_success = 0;
+   at_failure = 0;
+}
+
+
+/**
+ * @brief Begin testsuite.
+ */
+void SDL_ATinit( const char *suite )
+{
+   /* Do not open twice. */
+   if (at_suite_msg) {
+      SDL_ATprint( "AT suite '%s' not closed before opening suite '%s'\n",
+            at_suite_msg, suite );
+   }
+   /* Must have a name. */
+   if (suite == NULL) {
+      SDL_ATprint( "AT testsuite does not have a name.\n");
+   }
+   SDL_ATcleanup();
+   at_suite_msg = suite;
+}
+
+
+/**
+ * @brief Finish testsuite.
+ */
+int SDL_ATfinish( int verbose )
+{
+   int failed;
+
+   /* Make sure initialized. */
+   if (at_suite_msg == NULL) {
+      SDL_ATprint("Ended testcase without initializing.\n");
+      return;
+   }
+
+   /* Display message if verbose on failed. */
+   failed = at_failure;
+   if (verbose) {
+      if (at_failure > 0) {
+         SDL_ATprint( "%s : Failed %d out of %d testcases!\n",
+               at_suite_msg, at_failure, at_success );
+      }
+      else {
+         SDL_ATprint( "%s : All tests successful (%d)\n",
+               at_suite_msg, at_success );
+      }
+   }
+
+   /* Clean up. */
+   SDL_ATcleanup();
+
+   /* Return failed. */
+   return failed;
+}
+
+
+/**
+ * @brief Begin testcase.
+ */
+void SDL_ATbegin( const char *testcase )
+{
+   /* Do not open twice. */
+   if (at_test_msg) {
+      SDL_ATprint( "AT testcase '%s' not closed before opening testcase '%s'",
+            at_test_msg, testcase );
+   }
+   /* Must have a name. */
+   if (testcase == NULL) {
+      SDL_ATprint( "AT testcase does not have a name.");
+   }
+   at_test_msg = testcase;
+}
+
+
+/**
+ * @brief Ends the testcase with a succes or failure.
+ */
+static void SDL_ATendWith( int success )
+{
+   /* Make sure initialized. */
+   if (at_test_msg == NULL) {
+      SDL_ATprint("Ended testcase without initializing.");
+      return;
+   }
+
+   /* Mark as success or failure. */
+   if (success)
+      at_success++;
+   else
+      at_failure++;
+
+   /* Clean up. */
+   at_test_msg = NULL;
+}
+
+
+/**
+ * @brief Testcase test.
+ */
+int SDL_ATassert( const char *msg, int condition )
+{
+   /* Condition failed. */
+   if (!condition) {
+      SDL_ATprint( "%s [%s] : %s", at_suite_msg, at_test_msg, msg );
+      SDL_ATendWith(0);
+   }
+   return !condition;
+}
+
+
+/**
+ * @brief End testcase.
+ */
+void SDL_ATend (void)
+{
+   SDL_ATendWith(1);
+}
+
+
+/**
+ * @brief Displays a message.
+ */
+int SDL_ATprint( const char *msg, ... )
+{
+   va_list ap;
+   int ret;
+
+   /* Make sure there is something to print. */
+   if (msg == NULL)
+      return;
+   else {
+      va_start(ap, msg);
+      ret = vprintf(msg, ap);
+      va_end(ap);
+   }
+
+   return ret;
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/automated/SDL_at.h	Fri Jun 19 18:53:58 2009 +0000
@@ -0,0 +1,97 @@
+/*
+ * Common code for automated test suite.
+ *
+ * Written by Edgar Simo "bobbens"
+ *
+ * Released under Public Domain.
+ */
+
+
+/**
+ * @file SDL_at.h
+ *
+ * @brief Handles automatic testing functionality.
+ *
+ * You create a testsuite and then run multiple testcases within that suite.
+ *  Syntax is similar to OpenGL. An example would be:
+ *
+ * @code
+ * int f;
+ * SDL_ATinit( "My testsuite" );
+ *
+ * SDL_ATbegin( "My first testcase" );
+ * if (!SDL_ATassert( "Trying '1+1=2'.", (1+1)==2))
+ *    SDL_ATend();
+ *
+ * SDL_ATbegin( "My second testcase" );
+ * if (!SDL_ATassert( "Trying '4/2=2'.", (4/2)==2))
+ *    SDL_ATend();
+ *
+ * f = SDL_ATend();
+ * @endcode
+ *
+ * @author Edgar Simo "bobbens"
+ */
+
+
+#ifndef _SDL_AT_H
+#  define _SDL_AT_H
+
+
+/*
+ * Suite level actions.
+ */
+/**
+ * @brief Starts the testsuite.
+ *
+ *    @param suite Name of the suite to start testing.
+ */
+void SDL_ATinit( const char *suite );
+/**
+ * @brief Finishes the testsuite printing out global results if verbose.
+ *
+ *    @param verbose Displays global results.
+ */
+int SDL_ATfinish( int verbose );
+
+
+/*
+ * Testcase level actions.
+ */
+/**
+ * @brief Begins a testcase.
+ *
+ *    @param testcase Name of the testcase to begin.
+ */
+void SDL_ATbegin( const char *testcase );
+/**
+ * @brief Checks a condition in the testcase.
+ *
+ * Will automatically call SDL_ATend if the condition isn't met.
+ *
+ *    @param msg Message to display for failure.
+ *    @param condition Condition to make sure is true.
+ *    @return Returns 1 if the condition isn't met.
+ */
+int SDL_ATassert( const char *msg, int condition );
+/**
+ * @brief Ends a testcase.
+ */
+void SDL_ATend (void);
+
+
+/*
+ * Misc functions.
+ */
+/**
+ * @brief Prints some text.
+ *
+ *    @param msg printf formatted string to display.
+ *    @return Number of character printed.
+ */
+int SDL_ATprint( const char *msg, ... );
+
+
+#endif /* _SDL_AT_H */
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/automated/rwops.c	Fri Jun 19 18:53:58 2009 +0000
@@ -0,0 +1,70 @@
+/**
+ * Automated SDL_RWops test.
+ *
+ * Written by Edgar Simo "bobbens"
+ *
+ * Released under Public Domain.
+ */
+
+
+#include "SDL.h"
+#include "SDL_at.h"
+
+
+static const char hello_world[] = "Hello World!";
+
+
+/**
+ * @brief Tests opening from memory.
+ */
+static void rwops_testMem (void)
+{
+   SDL_RWops *rw;
+   char mem[sizeof(hello_world)], buf[sizeof(hello_world)];
+   int i;
+
+   /* Begin testcase. */
+   SDL_ATbegin( "SDL_RWFromMem" );
+
+   /* Open. */
+   rw = SDL_RWFromMem( mem, sizeof(mem) );
+   if (SDL_ATassert( "Opening memory with SDL_RWFromMem", rw != NULL ))
+      return;
+
+   /* Test write. */
+   i = SDL_RWwrite( rw, hello_world, sizeof(hello_world), 1 );
+   if (SDL_ATassert( "Writing with SDL_RWwrite", i == 1 ))
+      return;
+
+   /* Test seek. */
+   i = SDL_RWseek( rw, 0, RW_SEEK_SET );
+   if (SDL_ATassert( "Seeking with SDL_RWseek", i == 0 ))
+      return;
+
+   /* Test read. */
+   i = SDL_RWread( rw, buf, sizeof(hello_world), 1 );
+   if (SDL_ATassert( "Reading with SDL_RWread", i == 1 ))
+      return;
+   if (SDL_ATassert( "Memory read does not match memory written",
+            memcmp( buf, hello_world, sizeof(hello_world) ) == 0 ))
+      return;
+
+   /* Close. */
+   SDL_FreeRW( rw );
+
+   /* End testcase. */
+   SDL_ATend();
+}
+
+
+/**
+ * @brief Entry point.
+ */
+int main( int argc, const char *argv[] )
+{
+   SDL_ATinit( "SDL_RWops" );
+
+   rwops_testMem();
+
+   return SDL_ATfinish(1);
+}