Added command line options. gsoc2009_unit_tests
authorEdgar Simo <bobbens@gmail.com>
Sun, 02 Aug 2009 18:58:03 +0000
branchgsoc2009_unit_tests
changeset 3741808fad5fb593
parent 3740 e451d5d288e9
child 3742 2b000dd830e8
Added command line options.
Added verbosity levels.
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
     1.1 --- a/test/automated/Makefile	Sun Aug 02 16:01:23 2009 +0000
     1.2 +++ b/test/automated/Makefile	Sun Aug 02 18:58:03 2009 +0000
     1.3 @@ -15,13 +15,13 @@
     1.4  COMMON_SRC     := SDL_at.c common/common.c
     1.5  COMMON_INCLUDE := SDL_at.h
     1.6  
     1.7 -TESTS_ALL := rwops/rwops platform/platform surface/surface render/render
     1.8 +TESTS_ALL := testsdl rwops/rwops platform/platform surface/surface render/render
     1.9  
    1.10  
    1.11  .PHONY: all clean test
    1.12  
    1.13  
    1.14 -all: testsdl $(TESTS_ALL)
    1.15 +all: $(TESTS_ALL)
    1.16  
    1.17  test: all
    1.18  	@./rwops/rwops
    1.19 @@ -29,7 +29,7 @@
    1.20  	@./surface/surface
    1.21  	@./render/render
    1.22  
    1.23 -testsdl:
    1.24 +testsdl: $(SRC) $(COMMON_SRC)
    1.25  	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(SRC) $(COMMON_SRC)
    1.26  
    1.27  rwops/rwops: rwops/rwops.c $(COMMON_INCLUDE) $(COMMON_SRC)
     2.1 --- a/test/automated/SDL_at.c	Sun Aug 02 16:01:23 2009 +0000
     2.2 +++ b/test/automated/SDL_at.c	Sun Aug 02 18:58:03 2009 +0000
     2.3 @@ -22,6 +22,13 @@
     2.4  static int at_failure = 0; /**< Number of failed testcases. */
     2.5  
     2.6  
     2.7 +/*
     2.8 + * Global properties.
     2.9 + */
    2.10 +static int at_verbose = 0; /**< Verbosity. */
    2.11 +static int at_quiet = 0; /**< Quietness. */
    2.12 +
    2.13 +
    2.14  /**
    2.15   * @brief Cleans up the automated testsuite state.
    2.16   */
    2.17 @@ -41,48 +48,52 @@
    2.18  {
    2.19     /* Do not open twice. */
    2.20     if (at_suite_msg) {
    2.21 -      SDL_ATprint( "AT suite '%s' not closed before opening suite '%s'\n",
    2.22 +      SDL_ATprintErr( "AT suite '%s' not closed before opening suite '%s'\n",
    2.23              at_suite_msg, suite );
    2.24     }
    2.25     /* Must have a name. */
    2.26     if (suite == NULL) {
    2.27 -      SDL_ATprint( "AT testsuite does not have a name.\n");
    2.28 +      SDL_ATprintErr( "AT testsuite does not have a name.\n");
    2.29     }
    2.30     SDL_ATcleanup();
    2.31     at_suite_msg = suite;
    2.32 +
    2.33 +   /* Verbose message. */
    2.34 +   SDL_ATprintVerbose( 2, "--+---> Started Test Suite '%s'\n", suite );
    2.35  }
    2.36  
    2.37  
    2.38  /**
    2.39   * @brief Finish testsuite.
    2.40   */
    2.41 -int SDL_ATfinish( int verbose )
    2.42 +int SDL_ATfinish (void)
    2.43  {
    2.44     int failed;
    2.45  
    2.46     /* Make sure initialized. */
    2.47     if (at_suite_msg == NULL) {
    2.48 -      SDL_ATprint("Ended testcase without initializing.\n");
    2.49 +      SDL_ATprintErr("Ended testcase without initializing.\n");
    2.50        return 1;
    2.51     }
    2.52  
    2.53     /* Finished without closing testcase. */
    2.54     if (at_test_msg) {
    2.55 -      SDL_ATprint( "AT suite '%s' finished without closing testcase '%s'\n",
    2.56 +      SDL_ATprintErr( "AT suite '%s' finished without closing testcase '%s'\n",
    2.57              at_suite_msg, at_test_msg );
    2.58     }
    2.59  
    2.60 +   /* Verbose message. */
    2.61 +   SDL_ATprintVerbose( 2, "<-+---- Finished Test Suite '%s'\n", at_suite_msg );
    2.62 +
    2.63     /* Display message if verbose on failed. */
    2.64     failed = at_failure;
    2.65 -   if (verbose) {
    2.66 -      if (at_failure > 0) {
    2.67 -         SDL_ATprint( "%s : Failed %d out of %d testcases!\n",
    2.68 -               at_suite_msg, at_failure, at_failure+at_success );
    2.69 -      }
    2.70 -      else {
    2.71 -         SDL_ATprint( "%s : All tests successful (%d)\n",
    2.72 -               at_suite_msg, at_success );
    2.73 -      }
    2.74 +   if (at_failure > 0) {
    2.75 +      SDL_ATprintErr( "%s : Failed %d out of %d testcases!\n",
    2.76 +            at_suite_msg, at_failure, at_failure+at_success );
    2.77 +   }
    2.78 +   else {
    2.79 +      SDL_ATprint( "%s : All tests successful (%d)\n",
    2.80 +            at_suite_msg, at_success );
    2.81     }
    2.82  
    2.83     /* Clean up. */
    2.84 @@ -94,20 +105,57 @@
    2.85  
    2.86  
    2.87  /**
    2.88 + * @brief Sets a property.
    2.89 + */
    2.90 +void SDL_ATseti( int property, int value )
    2.91 +{
    2.92 +   switch (property) {
    2.93 +      case SDL_AT_VERBOSE:
    2.94 +         at_verbose = value;
    2.95 +         break;
    2.96 +
    2.97 +      case SDL_AT_QUIET:
    2.98 +         at_quiet = value;
    2.99 +         break;
   2.100 +   }
   2.101 +}
   2.102 +
   2.103 +
   2.104 +/**
   2.105 + * @brief Gets a property.
   2.106 + */
   2.107 +void SDL_ATgeti( int property, int *value )
   2.108 +{
   2.109 +   switch (property) {
   2.110 +      case SDL_AT_VERBOSE:
   2.111 +         *value = at_verbose;
   2.112 +         break;
   2.113 +
   2.114 +      case SDL_AT_QUIET:
   2.115 +         *value = at_quiet;
   2.116 +         break;
   2.117 +   }
   2.118 +}
   2.119 +
   2.120 +
   2.121 +/**
   2.122   * @brief Begin testcase.
   2.123   */
   2.124  void SDL_ATbegin( const char *testcase )
   2.125  {
   2.126     /* Do not open twice. */
   2.127     if (at_test_msg) {
   2.128 -      SDL_ATprint( "AT testcase '%s' not closed before opening testcase '%s'\n",
   2.129 +      SDL_ATprintErr( "AT testcase '%s' not closed before opening testcase '%s'\n",
   2.130              at_test_msg, testcase );
   2.131     }
   2.132     /* Must have a name. */
   2.133     if (testcase == NULL) {
   2.134 -      SDL_ATprint( "AT testcase does not have a name.\n");
   2.135 +      SDL_ATprintErr( "AT testcase does not have a name.\n");
   2.136     }
   2.137     at_test_msg = testcase;
   2.138 +
   2.139 +   /* Verbose message. */
   2.140 +   SDL_ATprintVerbose( 2, "  +---> StartedTest Case '%s'\n", testcase );
   2.141  }
   2.142  
   2.143  
   2.144 @@ -118,7 +166,7 @@
   2.145  {
   2.146     /* Make sure initialized. */
   2.147     if (at_test_msg == NULL) {
   2.148 -      SDL_ATprint("Ended testcase without initializing.\n");
   2.149 +      SDL_ATprintErr("Ended testcase without initializing.\n");
   2.150        return;
   2.151     }
   2.152  
   2.153 @@ -128,6 +176,9 @@
   2.154     else
   2.155        at_failure++;
   2.156  
   2.157 +   /* Verbose message. */
   2.158 +   SDL_ATprintVerbose( 2, "  +---- Finished Test Case '%s'\n", at_test_msg );
   2.159 +
   2.160     /* Clean up. */
   2.161     at_test_msg = NULL;
   2.162  }
   2.163 @@ -141,7 +192,7 @@
   2.164     /* Condition failed. */
   2.165     if (!condition) {
   2.166        /* Print. */
   2.167 -      SDL_ATprint( "%s [%s] : %s\n", at_suite_msg, at_test_msg, msg );
   2.168 +      SDL_ATprintErr( "%s [%s] : %s\n", at_suite_msg, at_test_msg, msg );
   2.169        /* End. */
   2.170        SDL_ATendWith(0);
   2.171     }
   2.172 @@ -164,7 +215,7 @@
   2.173        vsnprintf( buf, sizeof(buf), msg, args );
   2.174        va_end( args );
   2.175        /* Print. */
   2.176 -      SDL_ATprint( "%s [%s] : %s\n", at_suite_msg, at_test_msg, buf );
   2.177 +      SDL_ATprintErr( "%s [%s] : %s\n", at_suite_msg, at_test_msg, buf );
   2.178        /* End. */
   2.179        SDL_ATendWith(0);
   2.180     }
   2.181 @@ -182,9 +233,9 @@
   2.182  
   2.183  
   2.184  /**
   2.185 - * @brief Displays a message.
   2.186 + * @brief Displays an error.
   2.187   */
   2.188 -int SDL_ATprint( const char *msg, ... )
   2.189 +int SDL_ATprintErr( const char *msg, ... )
   2.190  {
   2.191     va_list ap;
   2.192     int ret;
   2.193 @@ -194,7 +245,7 @@
   2.194        return 0;
   2.195     else {
   2.196        va_start(ap, msg);
   2.197 -      ret = vprintf(msg, ap);
   2.198 +      ret = vfprintf( stderr, msg, ap );
   2.199        va_end(ap);
   2.200     }
   2.201  
   2.202 @@ -202,3 +253,54 @@
   2.203  }
   2.204  
   2.205  
   2.206 +/**
   2.207 + * @brief Displays a message.
   2.208 + */
   2.209 +int SDL_ATprint( const char *msg, ... )
   2.210 +{
   2.211 +   va_list ap;
   2.212 +   int ret;
   2.213 +
   2.214 +   /* Only print if not quiet. */
   2.215 +   if (at_quiet)
   2.216 +      return 0;
   2.217 +
   2.218 +   /* Make sure there is something to print. */
   2.219 +   if (msg == NULL)
   2.220 +      return 0;
   2.221 +   else {
   2.222 +      va_start(ap, msg);
   2.223 +      ret = vfprintf( stdout, msg, ap );
   2.224 +      va_end(ap);
   2.225 +   }
   2.226 +
   2.227 +   return ret;
   2.228 +}
   2.229 +
   2.230 +
   2.231 +/**
   2.232 + * @brief Displays a verbose message.
   2.233 + */
   2.234 +int SDL_ATprintVerbose( int level, const char *msg, ... )
   2.235 +{
   2.236 +   va_list ap;
   2.237 +   int ret;
   2.238 +
   2.239 +   /* Only print if not quiet. */
   2.240 +   if (at_quiet || (at_verbose < level))
   2.241 +      return 0;
   2.242 +
   2.243 +   /* Make sure there is something to print. */
   2.244 +   if (msg == NULL)
   2.245 +      return 0;
   2.246 +   else {
   2.247 +      va_start(ap, msg);
   2.248 +      ret = vfprintf( stdout, msg, ap );
   2.249 +      va_end(ap);
   2.250 +   }
   2.251 +
   2.252 +   return ret;
   2.253 +}
   2.254 +
   2.255 +
   2.256 +
     3.1 --- a/test/automated/SDL_at.h	Sun Aug 02 16:01:23 2009 +0000
     3.2 +++ b/test/automated/SDL_at.h	Sun Aug 02 18:58:03 2009 +0000
     3.3 @@ -38,6 +38,13 @@
     3.4  #  define _SDL_AT_H
     3.5  
     3.6  
     3.7 +
     3.8 +enum {
     3.9 +   SDL_AT_VERBOSE,
    3.10 +   SDL_AT_QUIET
    3.11 +};
    3.12 +
    3.13 +
    3.14  /*
    3.15   * Suite level actions.
    3.16   */
    3.17 @@ -52,7 +59,21 @@
    3.18   *
    3.19   *    @param verbose Displays global results.
    3.20   */
    3.21 -int SDL_ATfinish( int verbose );
    3.22 +int SDL_ATfinish (void);
    3.23 +/**
    3.24 + * @brief Sets a global property value.
    3.25 + *
    3.26 + *    @param property Property to set.
    3.27 + *    @param value Value to set property to.
    3.28 + */
    3.29 +void SDL_ATseti( int property, int value );
    3.30 +/**
    3.31 + * @brief Gets a global property value.
    3.32 + *
    3.33 + *    @param property Property to get.
    3.34 + *    @param[out] value Value of the property.
    3.35 + */
    3.36 +void SDL_ATgeti( int property, int *value );
    3.37  
    3.38  
    3.39  /*
    3.40 @@ -94,12 +115,27 @@
    3.41   * Misc functions.
    3.42   */
    3.43  /**
    3.44 + * @brief Prints an error.
    3.45 + *
    3.46 + *    @param msg printf formatted string to display.
    3.47 + *    @return Number of character printed.
    3.48 + */
    3.49 +int SDL_ATprintErr( const char *msg, ... );
    3.50 +/**
    3.51   * @brief Prints some text.
    3.52   *
    3.53   *    @param msg printf formatted string to display.
    3.54   *    @return Number of character printed.
    3.55   */
    3.56  int SDL_ATprint( const char *msg, ... );
    3.57 +/**
    3.58 + * @brief Prints some verbose text.
    3.59 + *
    3.60 + *    @param level Level of verbosity to print at.
    3.61 + *    @param msg printf formatted string to display.
    3.62 + *    @return Number of character printed.
    3.63 + */
    3.64 +int SDL_ATprintVerbose( int level, const char *msg, ... );
    3.65  
    3.66  
    3.67  #endif /* _SDL_AT_H */
     4.1 --- a/test/automated/platform/platform.c	Sun Aug 02 16:01:23 2009 +0000
     4.2 +++ b/test/automated/platform/platform.c	Sun Aug 02 18:58:03 2009 +0000
     4.3 @@ -149,5 +149,5 @@
     4.4     plat_testTypes();
     4.5     plat_testEndian();
     4.6  
     4.7 -   return SDL_ATfinish(1);
     4.8 +   return SDL_ATfinish();
     4.9  }
     5.1 --- a/test/automated/render/render.c	Sun Aug 02 16:01:23 2009 +0000
     5.2 +++ b/test/automated/render/render.c	Sun Aug 02 18:58:03 2009 +0000
     5.3 @@ -1056,7 +1056,7 @@
     5.4        /*
     5.5         * Finish testsuite.
     5.6         */
     5.7 -      SDL_ATfinish(1);
     5.8 +      SDL_ATfinish();
     5.9     }
    5.10  
    5.11  
     6.1 --- a/test/automated/rwops/rwops.c	Sun Aug 02 16:01:23 2009 +0000
     6.2 +++ b/test/automated/rwops/rwops.c	Sun Aug 02 18:58:03 2009 +0000
     6.3 @@ -267,5 +267,5 @@
     6.4     rwops_testFile();
     6.5     rwops_testFP();
     6.6  
     6.7 -   return SDL_ATfinish(1);
     6.8 +   return SDL_ATfinish();
     6.9  }
     7.1 --- a/test/automated/surface/surface.c	Sun Aug 02 16:01:23 2009 +0000
     7.2 +++ b/test/automated/surface/surface.c	Sun Aug 02 18:58:03 2009 +0000
     7.3 @@ -588,9 +588,9 @@
     7.4     /* Exit SDL. */
     7.5     SDL_Quit();
     7.6  
     7.7 -   return SDL_ATfinish(1);
     7.8 +   return SDL_ATfinish();
     7.9  
    7.10  err:
    7.11 -   return SDL_ATfinish(1);
    7.12 +   return SDL_ATfinish();
    7.13  }
    7.14  
     8.1 --- a/test/automated/testsdl.c	Sun Aug 02 16:01:23 2009 +0000
     8.2 +++ b/test/automated/testsdl.c	Sun Aug 02 18:58:03 2009 +0000
     8.3 @@ -7,16 +7,89 @@
     8.4   */
     8.5  
     8.6  
     8.7 +#include "SDL_at.h"
     8.8 +
     8.9  #include "platform/platform.h"
    8.10  #include "rwops/rwops.h"
    8.11  #include "surface/surface.h"
    8.12  #include "render/render.h"
    8.13  
    8.14 +#include <stdio.h> /* printf */
    8.15 +#include <stdlib.h> /* exit */
    8.16 +#include <unistd.h> /* getopt */
    8.17 +#include <getopt.h> /* getopt_long */
    8.18  
    8.19 +
    8.20 +/*
    8.21 + * Prototypes.
    8.22 + */
    8.23 +static void print_usage( const char *name );
    8.24 +static void parse_options( int argc, char *argv[] );
    8.25 +
    8.26 +
    8.27 +/**
    8.28 + * @brief Displays program usage.
    8.29 + */
    8.30 +static void print_usage( const char *name )
    8.31 +{
    8.32 +   printf("Usage: %s [OPTIONS]\n", name);
    8.33 +   printf("Options are:\n");
    8.34 +   printf("   -v, --verbose   increases verbosity level by 1 for each -v\n");
    8.35 +   printf("   -q, --quiet     only displays errors\n");
    8.36 +   printf("   -h, --help      display this message and exit\n");
    8.37 +}
    8.38 +
    8.39 +
    8.40 +/**
    8.41 + * @brief Handles the options.
    8.42 + */
    8.43 +static void parse_options( int argc, char *argv[] )
    8.44 +{
    8.45 +   static struct option long_options[] = {
    8.46 +      { "verbose", no_argument, 0, 'v' },
    8.47 +      { "quiet", no_argument, 0, 'q' },
    8.48 +      { "help", no_argument, 0, 'h' },
    8.49 +      {NULL,0,0,0}
    8.50 +   };
    8.51 +   int option_index = 0;
    8.52 +   int c = 0;
    8.53 +   int i;
    8.54 +
    8.55 +   /* Iterate over options. */
    8.56 +   while ((c = getopt_long( argc, argv,
    8.57 +               "vqh",
    8.58 +               long_options, &option_index)) != -1) {
    8.59 +
    8.60 +      /* Handle options. */
    8.61 +      switch (c) {
    8.62 +
    8.63 +         /* Verbosity. */
    8.64 +         case 'v':
    8.65 +            SDL_ATgeti( SDL_AT_VERBOSE, &i );
    8.66 +            SDL_ATseti( SDL_AT_VERBOSE, i+1 );
    8.67 +            break;
    8.68 +
    8.69 +         /* Quiet. */
    8.70 +         case 'q':
    8.71 +            SDL_ATseti( SDL_AT_QUIET, 1 );
    8.72 +            break;
    8.73 +
    8.74 +         /* Help. */
    8.75 +         case 'h':
    8.76 +            print_usage( argv[0] );
    8.77 +            exit(EXIT_SUCCESS);
    8.78 +      }
    8.79 +   }
    8.80 +
    8.81 +}
    8.82 +
    8.83 +
    8.84 +/**
    8.85 + * @brief Main entry point.
    8.86 + */
    8.87  int main( int argc, char *argv[] )
    8.88  {
    8.89 -   (void) argc;
    8.90 -   (void) argv;
    8.91 +   parse_options( argc, argv );
    8.92  
    8.93     test_platform();
    8.94     test_rwops();