diff test/automated/SDL_at.c @ 3711:80839fc6b8e1 gsoc2009_unit_tests

First revision of the automated test suite.
author Edgar Simo <bobbens@gmail.com>
date Fri, 19 Jun 2009 18:53:58 +0000
parents
children a34bab848c7e
line wrap: on
line diff
--- /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;
+}
+
+