changeset 3741:808fad5fb593 gsoc2009_unit_tests

Added command line options. Added verbosity levels.
author Edgar Simo <bobbens@gmail.com>
date Sun, 02 Aug 2009 18:58:03 +0000
parents e451d5d288e9
children 2b000dd830e8
files test/automated/Makefile test/automated/SDL_at.c test/automated/SDL_at.h test/automated/platform/platform.c test/automated/render/render.c test/automated/rwops/rwops.c test/automated/surface/surface.c test/automated/testsdl.c
diffstat 8 files changed, 244 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/test/automated/Makefile	Sun Aug 02 16:01:23 2009 +0000
+++ b/test/automated/Makefile	Sun Aug 02 18:58:03 2009 +0000
@@ -15,13 +15,13 @@
 COMMON_SRC     := SDL_at.c common/common.c
 COMMON_INCLUDE := SDL_at.h
 
-TESTS_ALL := rwops/rwops platform/platform surface/surface render/render
+TESTS_ALL := testsdl rwops/rwops platform/platform surface/surface render/render
 
 
 .PHONY: all clean test
 
 
-all: testsdl $(TESTS_ALL)
+all: $(TESTS_ALL)
 
 test: all
 	@./rwops/rwops
@@ -29,7 +29,7 @@
 	@./surface/surface
 	@./render/render
 
-testsdl:
+testsdl: $(SRC) $(COMMON_SRC)
 	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(SRC) $(COMMON_SRC)
 
 rwops/rwops: rwops/rwops.c $(COMMON_INCLUDE) $(COMMON_SRC)
--- a/test/automated/SDL_at.c	Sun Aug 02 16:01:23 2009 +0000
+++ b/test/automated/SDL_at.c	Sun Aug 02 18:58:03 2009 +0000
@@ -22,6 +22,13 @@
 static int at_failure = 0; /**< Number of failed testcases. */
 
 
+/*
+ * Global properties.
+ */
+static int at_verbose = 0; /**< Verbosity. */
+static int at_quiet = 0; /**< Quietness. */
+
+
 /**
  * @brief Cleans up the automated testsuite state.
  */
@@ -41,48 +48,52 @@
 {
    /* Do not open twice. */
    if (at_suite_msg) {
-      SDL_ATprint( "AT suite '%s' not closed before opening suite '%s'\n",
+      SDL_ATprintErr( "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_ATprintErr( "AT testsuite does not have a name.\n");
    }
    SDL_ATcleanup();
    at_suite_msg = suite;
+
+   /* Verbose message. */
+   SDL_ATprintVerbose( 2, "--+---> Started Test Suite '%s'\n", suite );
 }
 
 
 /**
  * @brief Finish testsuite.
  */
-int SDL_ATfinish( int verbose )
+int SDL_ATfinish (void)
 {
    int failed;
 
    /* Make sure initialized. */
    if (at_suite_msg == NULL) {
-      SDL_ATprint("Ended testcase without initializing.\n");
+      SDL_ATprintErr("Ended testcase without initializing.\n");
       return 1;
    }
 
    /* Finished without closing testcase. */
    if (at_test_msg) {
-      SDL_ATprint( "AT suite '%s' finished without closing testcase '%s'\n",
+      SDL_ATprintErr( "AT suite '%s' finished without closing testcase '%s'\n",
             at_suite_msg, at_test_msg );
    }
 
+   /* Verbose message. */
+   SDL_ATprintVerbose( 2, "<-+---- Finished Test Suite '%s'\n", at_suite_msg );
+
    /* 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_failure+at_success );
-      }
-      else {
-         SDL_ATprint( "%s : All tests successful (%d)\n",
-               at_suite_msg, at_success );
-      }
+   if (at_failure > 0) {
+      SDL_ATprintErr( "%s : Failed %d out of %d testcases!\n",
+            at_suite_msg, at_failure, at_failure+at_success );
+   }
+   else {
+      SDL_ATprint( "%s : All tests successful (%d)\n",
+            at_suite_msg, at_success );
    }
 
    /* Clean up. */
@@ -94,20 +105,57 @@
 
 
 /**
+ * @brief Sets a property.
+ */
+void SDL_ATseti( int property, int value )
+{
+   switch (property) {
+      case SDL_AT_VERBOSE:
+         at_verbose = value;
+         break;
+
+      case SDL_AT_QUIET:
+         at_quiet = value;
+         break;
+   }
+}
+
+
+/**
+ * @brief Gets a property.
+ */
+void SDL_ATgeti( int property, int *value )
+{
+   switch (property) {
+      case SDL_AT_VERBOSE:
+         *value = at_verbose;
+         break;
+
+      case SDL_AT_QUIET:
+         *value = at_quiet;
+         break;
+   }
+}
+
+
+/**
  * @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'\n",
+      SDL_ATprintErr( "AT testcase '%s' not closed before opening testcase '%s'\n",
             at_test_msg, testcase );
    }
    /* Must have a name. */
    if (testcase == NULL) {
-      SDL_ATprint( "AT testcase does not have a name.\n");
+      SDL_ATprintErr( "AT testcase does not have a name.\n");
    }
    at_test_msg = testcase;
+
+   /* Verbose message. */
+   SDL_ATprintVerbose( 2, "  +---> StartedTest Case '%s'\n", testcase );
 }
 
 
@@ -118,7 +166,7 @@
 {
    /* Make sure initialized. */
    if (at_test_msg == NULL) {
-      SDL_ATprint("Ended testcase without initializing.\n");
+      SDL_ATprintErr("Ended testcase without initializing.\n");
       return;
    }
 
@@ -128,6 +176,9 @@
    else
       at_failure++;
 
+   /* Verbose message. */
+   SDL_ATprintVerbose( 2, "  +---- Finished Test Case '%s'\n", at_test_msg );
+
    /* Clean up. */
    at_test_msg = NULL;
 }
@@ -141,7 +192,7 @@
    /* Condition failed. */
    if (!condition) {
       /* Print. */
-      SDL_ATprint( "%s [%s] : %s\n", at_suite_msg, at_test_msg, msg );
+      SDL_ATprintErr( "%s [%s] : %s\n", at_suite_msg, at_test_msg, msg );
       /* End. */
       SDL_ATendWith(0);
    }
@@ -164,7 +215,7 @@
       vsnprintf( buf, sizeof(buf), msg, args );
       va_end( args );
       /* Print. */
-      SDL_ATprint( "%s [%s] : %s\n", at_suite_msg, at_test_msg, buf );
+      SDL_ATprintErr( "%s [%s] : %s\n", at_suite_msg, at_test_msg, buf );
       /* End. */
       SDL_ATendWith(0);
    }
@@ -182,9 +233,9 @@
 
 
 /**
- * @brief Displays a message.
+ * @brief Displays an error.
  */
-int SDL_ATprint( const char *msg, ... )
+int SDL_ATprintErr( const char *msg, ... )
 {
    va_list ap;
    int ret;
@@ -194,7 +245,7 @@
       return 0;
    else {
       va_start(ap, msg);
-      ret = vprintf(msg, ap);
+      ret = vfprintf( stderr, msg, ap );
       va_end(ap);
    }
 
@@ -202,3 +253,54 @@
 }
 
 
+/**
+ * @brief Displays a message.
+ */
+int SDL_ATprint( const char *msg, ... )
+{
+   va_list ap;
+   int ret;
+
+   /* Only print if not quiet. */
+   if (at_quiet)
+      return 0;
+
+   /* Make sure there is something to print. */
+   if (msg == NULL)
+      return 0;
+   else {
+      va_start(ap, msg);
+      ret = vfprintf( stdout, msg, ap );
+      va_end(ap);
+   }
+
+   return ret;
+}
+
+
+/**
+ * @brief Displays a verbose message.
+ */
+int SDL_ATprintVerbose( int level, const char *msg, ... )
+{
+   va_list ap;
+   int ret;
+
+   /* Only print if not quiet. */
+   if (at_quiet || (at_verbose < level))
+      return 0;
+
+   /* Make sure there is something to print. */
+   if (msg == NULL)
+      return 0;
+   else {
+      va_start(ap, msg);
+      ret = vfprintf( stdout, msg, ap );
+      va_end(ap);
+   }
+
+   return ret;
+}
+
+
+
--- a/test/automated/SDL_at.h	Sun Aug 02 16:01:23 2009 +0000
+++ b/test/automated/SDL_at.h	Sun Aug 02 18:58:03 2009 +0000
@@ -38,6 +38,13 @@
 #  define _SDL_AT_H
 
 
+
+enum {
+   SDL_AT_VERBOSE,
+   SDL_AT_QUIET
+};
+
+
 /*
  * Suite level actions.
  */
@@ -52,7 +59,21 @@
  *
  *    @param verbose Displays global results.
  */
-int SDL_ATfinish( int verbose );
+int SDL_ATfinish (void);
+/**
+ * @brief Sets a global property value.
+ *
+ *    @param property Property to set.
+ *    @param value Value to set property to.
+ */
+void SDL_ATseti( int property, int value );
+/**
+ * @brief Gets a global property value.
+ *
+ *    @param property Property to get.
+ *    @param[out] value Value of the property.
+ */
+void SDL_ATgeti( int property, int *value );
 
 
 /*
@@ -94,12 +115,27 @@
  * Misc functions.
  */
 /**
+ * @brief Prints an error.
+ *
+ *    @param msg printf formatted string to display.
+ *    @return Number of character printed.
+ */
+int SDL_ATprintErr( const char *msg, ... );
+/**
  * @brief Prints some text.
  *
  *    @param msg printf formatted string to display.
  *    @return Number of character printed.
  */
 int SDL_ATprint( const char *msg, ... );
+/**
+ * @brief Prints some verbose text.
+ *
+ *    @param level Level of verbosity to print at.
+ *    @param msg printf formatted string to display.
+ *    @return Number of character printed.
+ */
+int SDL_ATprintVerbose( int level, const char *msg, ... );
 
 
 #endif /* _SDL_AT_H */
--- a/test/automated/platform/platform.c	Sun Aug 02 16:01:23 2009 +0000
+++ b/test/automated/platform/platform.c	Sun Aug 02 18:58:03 2009 +0000
@@ -149,5 +149,5 @@
    plat_testTypes();
    plat_testEndian();
 
-   return SDL_ATfinish(1);
+   return SDL_ATfinish();
 }
--- a/test/automated/render/render.c	Sun Aug 02 16:01:23 2009 +0000
+++ b/test/automated/render/render.c	Sun Aug 02 18:58:03 2009 +0000
@@ -1056,7 +1056,7 @@
       /*
        * Finish testsuite.
        */
-      SDL_ATfinish(1);
+      SDL_ATfinish();
    }
 
 
--- a/test/automated/rwops/rwops.c	Sun Aug 02 16:01:23 2009 +0000
+++ b/test/automated/rwops/rwops.c	Sun Aug 02 18:58:03 2009 +0000
@@ -267,5 +267,5 @@
    rwops_testFile();
    rwops_testFP();
 
-   return SDL_ATfinish(1);
+   return SDL_ATfinish();
 }
--- a/test/automated/surface/surface.c	Sun Aug 02 16:01:23 2009 +0000
+++ b/test/automated/surface/surface.c	Sun Aug 02 18:58:03 2009 +0000
@@ -588,9 +588,9 @@
    /* Exit SDL. */
    SDL_Quit();
 
-   return SDL_ATfinish(1);
+   return SDL_ATfinish();
 
 err:
-   return SDL_ATfinish(1);
+   return SDL_ATfinish();
 }
 
--- a/test/automated/testsdl.c	Sun Aug 02 16:01:23 2009 +0000
+++ b/test/automated/testsdl.c	Sun Aug 02 18:58:03 2009 +0000
@@ -7,16 +7,89 @@
  */
 
 
+#include "SDL_at.h"
+
 #include "platform/platform.h"
 #include "rwops/rwops.h"
 #include "surface/surface.h"
 #include "render/render.h"
 
+#include <stdio.h> /* printf */
+#include <stdlib.h> /* exit */
+#include <unistd.h> /* getopt */
+#include <getopt.h> /* getopt_long */
 
+
+/*
+ * Prototypes.
+ */
+static void print_usage( const char *name );
+static void parse_options( int argc, char *argv[] );
+
+
+/**
+ * @brief Displays program usage.
+ */
+static void print_usage( const char *name )
+{
+   printf("Usage: %s [OPTIONS]\n", name);
+   printf("Options are:\n");
+   printf("   -v, --verbose   increases verbosity level by 1 for each -v\n");
+   printf("   -q, --quiet     only displays errors\n");
+   printf("   -h, --help      display this message and exit\n");
+}
+
+
+/**
+ * @brief Handles the options.
+ */
+static void parse_options( int argc, char *argv[] )
+{
+   static struct option long_options[] = {
+      { "verbose", no_argument, 0, 'v' },
+      { "quiet", no_argument, 0, 'q' },
+      { "help", no_argument, 0, 'h' },
+      {NULL,0,0,0}
+   };
+   int option_index = 0;
+   int c = 0;
+   int i;
+
+   /* Iterate over options. */
+   while ((c = getopt_long( argc, argv,
+               "vqh",
+               long_options, &option_index)) != -1) {
+
+      /* Handle options. */
+      switch (c) {
+
+         /* Verbosity. */
+         case 'v':
+            SDL_ATgeti( SDL_AT_VERBOSE, &i );
+            SDL_ATseti( SDL_AT_VERBOSE, i+1 );
+            break;
+
+         /* Quiet. */
+         case 'q':
+            SDL_ATseti( SDL_AT_QUIET, 1 );
+            break;
+
+         /* Help. */
+         case 'h':
+            print_usage( argv[0] );
+            exit(EXIT_SUCCESS);
+      }
+   }
+
+}
+
+
+/**
+ * @brief Main entry point.
+ */
 int main( int argc, char *argv[] )
 {
-   (void) argc;
-   (void) argv;
+   parse_options( argc, argv );
 
    test_platform();
    test_rwops();