From 3c25c265c35c4f096ce5cf9e9df5253e482be9c1 Mon Sep 17 00:00:00 2001 From: Edgar Simo Date: Sun, 2 Aug 2009 18:58:03 +0000 Subject: [PATCH] Added command line options. Added verbosity levels. --- test/automated/Makefile | 6 +- test/automated/SDL_at.c | 142 +++++++++++++++++++++++++---- test/automated/SDL_at.h | 38 +++++++- test/automated/platform/platform.c | 2 +- test/automated/render/render.c | 2 +- test/automated/rwops/rwops.c | 2 +- test/automated/surface/surface.c | 4 +- test/automated/testsdl.c | 77 +++++++++++++++- 8 files changed, 242 insertions(+), 31 deletions(-) diff --git a/test/automated/Makefile b/test/automated/Makefile index 306715376..83baa36e9 100644 --- a/test/automated/Makefile +++ b/test/automated/Makefile @@ -15,13 +15,13 @@ SRC := testsdl.c \ 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 @@ test: all @./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) diff --git a/test/automated/SDL_at.c b/test/automated/SDL_at.c index 9f4b0f270..54837a7b3 100644 --- a/test/automated/SDL_at.c +++ b/test/automated/SDL_at.c @@ -22,6 +22,13 @@ static int at_success = 0; /**< Number of successful testcases. */ 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 @@ 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", + 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. */ @@ -93,6 +104,40 @@ int SDL_ATfinish( int verbose ) } +/** + * @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. */ @@ -100,14 +145,17 @@ 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 @@ static void SDL_ATendWith( int success ) { /* 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 @@ static void SDL_ATendWith( int success ) 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 @@ int SDL_ATassert( const char *msg, int condition ) /* 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 @@ int SDL_ATvassert( int condition, const char *msg, ... ) 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); } @@ -181,6 +232,27 @@ void SDL_ATend (void) } +/** + * @brief Displays an error. + */ +int SDL_ATprintErr( const char *msg, ... ) +{ + va_list ap; + int ret; + + /* Make sure there is something to print. */ + if (msg == NULL) + return 0; + else { + va_start(ap, msg); + ret = vfprintf( stderr, msg, ap ); + va_end(ap); + } + + return ret; +} + + /** * @brief Displays a message. */ @@ -189,12 +261,41 @@ 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 = vprintf(msg, ap); + ret = vfprintf( stdout, msg, ap ); va_end(ap); } @@ -202,3 +303,4 @@ int SDL_ATprint( const char *msg, ... ) } + diff --git a/test/automated/SDL_at.h b/test/automated/SDL_at.h index dc5736567..bee07995d 100644 --- a/test/automated/SDL_at.h +++ b/test/automated/SDL_at.h @@ -38,6 +38,13 @@ # define _SDL_AT_H + +enum { + SDL_AT_VERBOSE, + SDL_AT_QUIET +}; + + /* * Suite level actions. */ @@ -52,7 +59,21 @@ void SDL_ATinit( const char *suite ); * * @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 ); /* @@ -93,6 +114,13 @@ void SDL_ATend (void); /* * 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. * @@ -100,6 +128,14 @@ void SDL_ATend (void); * @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 */ diff --git a/test/automated/platform/platform.c b/test/automated/platform/platform.c index d7c0cb9bb..a13f7eb37 100644 --- a/test/automated/platform/platform.c +++ b/test/automated/platform/platform.c @@ -149,5 +149,5 @@ int test_platform (void) plat_testTypes(); plat_testEndian(); - return SDL_ATfinish(1); + return SDL_ATfinish(); } diff --git a/test/automated/render/render.c b/test/automated/render/render.c index ae6859dbe..60a197bcd 100644 --- a/test/automated/render/render.c +++ b/test/automated/render/render.c @@ -1056,7 +1056,7 @@ int test_render (void) /* * Finish testsuite. */ - SDL_ATfinish(1); + SDL_ATfinish(); } diff --git a/test/automated/rwops/rwops.c b/test/automated/rwops/rwops.c index b7e3fa132..84eba1605 100644 --- a/test/automated/rwops/rwops.c +++ b/test/automated/rwops/rwops.c @@ -267,5 +267,5 @@ int test_rwops (void) rwops_testFile(); rwops_testFP(); - return SDL_ATfinish(1); + return SDL_ATfinish(); } diff --git a/test/automated/surface/surface.c b/test/automated/surface/surface.c index 06ddec105..f626c71eb 100644 --- a/test/automated/surface/surface.c +++ b/test/automated/surface/surface.c @@ -588,9 +588,9 @@ int test_surface (void) /* Exit SDL. */ SDL_Quit(); - return SDL_ATfinish(1); + return SDL_ATfinish(); err: - return SDL_ATfinish(1); + return SDL_ATfinish(); } diff --git a/test/automated/testsdl.c b/test/automated/testsdl.c index 215d5aec9..f9123f0bb 100644 --- a/test/automated/testsdl.c +++ b/test/automated/testsdl.c @@ -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 /* printf */ +#include /* exit */ +#include /* getopt */ +#include /* 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();