Refactoring the TestCaseInit and TestCaseQuit functions
authorMarkus Kauppila
Sat, 04 Jun 2011 17:50:23 +0300
changeset 567850401a6c3c95
parent 5677 ecb455bdc5a5
child 5679 10a5199c4169
Refactoring the TestCaseInit and TestCaseQuit functions
to be caller from the Runner.
test/test-automation/Makefile.am
test/test-automation/SDL_test.c
test/test-automation/SDL_test.h
test/test-automation/runner.c
test/test-automation/testrect/testrect.c
test/test-automation/tests/Makefile.am
test/test-automation/tests/test.c
     1.1 --- a/test/test-automation/Makefile.am	Wed Jun 01 18:03:09 2011 -0700
     1.2 +++ b/test/test-automation/Makefile.am	Sat Jun 04 17:50:23 2011 +0300
     1.3 @@ -5,7 +5,7 @@
     1.4  bin_PROGRAMS = runner
     1.5  runner_SOURCES = runner.c SDL_test.c
     1.6  runner_CLAGS = -W -Wall -Wextra -g `sdl-config --cflags` -DSDL_NO_COMPAT
     1.7 -runner_LDFLAGS = `sdl-config --libs`
     1.8 +runner_LDFLAGS = `sdl-config --libs` 
     1.9  
    1.10  install: install-tests
    1.11  install-tests:
     2.1 --- a/test/test-automation/SDL_test.c	Wed Jun 01 18:03:09 2011 -0700
     2.2 +++ b/test/test-automation/SDL_test.c	Sat Jun 04 17:50:23 2011 +0300
     2.3 @@ -27,13 +27,16 @@
     2.4  #include "SDL_test.h"
     2.5  
     2.6  /*! \brief return value of test case. Non-zero value means that the test failed */
     2.7 -static int _testReturnValue;
     2.8 +int _testReturnValue;
     2.9  
    2.10 -static int _testAssertsFailed;
    2.11 -static int _testAssertsPassed;
    2.12 +/*! \brief counts the failed asserts */
    2.13 +int _testAssertsFailed;
    2.14 +
    2.15 +/*! \brief counts the passed asserts */
    2.16 +int _testAssertsPassed;
    2.17  
    2.18  void
    2.19 -TestCaseInit()
    2.20 +_TestCaseInit()
    2.21  {
    2.22  	_testReturnValue = 0;
    2.23  	_testAssertsFailed = 0;
    2.24 @@ -41,13 +44,14 @@
    2.25  }
    2.26  
    2.27  int
    2.28 -TestCaseQuit()
    2.29 +_TestCaseQuit()
    2.30  {
    2.31 -        printf("Asserts: passed %d, failed %d\n", _testAssertsPassed, _testAssertsFailed);
    2.32 +	//! \todo make the test fail, if it does not contain any asserts
    2.33 +	printf("Asserts: passed %d, failed %d\n", _testAssertsPassed, _testAssertsFailed);fflush(stdout);
    2.34  	return _testReturnValue;
    2.35  }
    2.36  
    2.37 -void 
    2.38 +void
    2.39  AssertEquals(Uint32 expected, Uint32 actual, char* message, ...)
    2.40  {
    2.41     va_list args;
    2.42 @@ -57,7 +61,7 @@
    2.43        va_start( args, message );
    2.44        SDL_vsnprintf( buf, sizeof(buf), message, args );
    2.45        va_end( args );
    2.46 -      printf("Assert Equals failed: expected %d, got %d; %s\n", expected, actual, buf);
    2.47 +      printf("Assert Equals failed: expected %d, got %d; %s\n", expected, actual, buf); fflush(stdout);
    2.48        _testReturnValue = 1;
    2.49        _testAssertsFailed++;
    2.50     } else {
    2.51 @@ -65,7 +69,7 @@
    2.52     }
    2.53  }
    2.54  
    2.55 -void 
    2.56 +void
    2.57  AssertTrue(int condition, char *message, ...)
    2.58  {
    2.59     va_list args;
     3.1 --- a/test/test-automation/SDL_test.h	Wed Jun 01 18:03:09 2011 -0700
     3.2 +++ b/test/test-automation/SDL_test.h	Sat Jun 04 17:50:23 2011 +0300
     3.3 @@ -23,11 +23,14 @@
     3.4  
     3.5  #include <SDL/SDL.h>
     3.6  
     3.7 +extern int _testReturnValue;
     3.8 +extern int _testAssertsFailed;
     3.9 +extern int _testAssertsPassed;
    3.10 +
    3.11  // \todo Should these be consts?
    3.12  #define TEST_ENABLED  1
    3.13  #define TEST_DISABLED 0
    3.14  
    3.15 -
    3.16  /*!
    3.17   * Holds information about a test case
    3.18   */
    3.19 @@ -38,23 +41,28 @@
    3.20  	long requirements;  /*!< Set to TEST_REQUIRES_OPENGL, TEST_REQUIRES_AUDIO, ... */
    3.21  } TestCaseReference;
    3.22  
    3.23 -/*! \fn TestCaseInit
    3.24 +/*! \fn _TestCaseInit
    3.25   *  Initialized the test case. Must be called at
    3.26   *  the beginning of every test case, before doing
    3.27   *  anything else.
    3.28   */
    3.29 -void TestCaseInit();
    3.30 +void _TestCaseInit();
    3.31  
    3.32 -/*! \fn TestCaseQuit
    3.33 +/*! \fn _TestCaseQuit
    3.34   *  Deinitializes and exits the test case
    3.35   *
    3.36   * \return 0 if test succeeded, otherwise 1
    3.37   */
    3.38 -int TestCaseQuit();
    3.39 +int _TestCaseQuit();
    3.40  
    3.41 -
    3.42 +/*!
    3.43 + * todo add comment
    3.44 + */
    3.45  void AssertEquals(Uint32 expected, Uint32 actual, char *message, ...);
    3.46  
    3.47 +/*!
    3.48 + * todo add comment
    3.49 + */
    3.50  void AssertTrue(int condition, char *message, ...);
    3.51  
    3.52  #endif
     4.1 --- a/test/test-automation/runner.c	Wed Jun 01 18:03:09 2011 -0700
     4.2 +++ b/test/test-automation/runner.c	Sat Jun 04 17:50:23 2011 +0300
     4.3 @@ -28,7 +28,11 @@
     4.4  #include "SDL_test.h"
     4.5  
     4.6  //!< Function pointer to a test case function
     4.7 -typedef int (*TestCase)(void *arg);
     4.8 +typedef void (*TestCase)(void *arg);
     4.9 +//!< Function pointer to a test case init function
    4.10 +typedef void (*TestCaseInit)(void);
    4.11 +//!< Function pointer to a test case quit function
    4.12 +typedef int  (*TestCaseQuit)(void);
    4.13  
    4.14  //!< Flag for executing tests in-process
    4.15  static int execute_inproc = 0;
    4.16 @@ -99,6 +103,7 @@
    4.17  	return tests;
    4.18  }
    4.19  
    4.20 +
    4.21  /*!
    4.22   * Loads test case from a test suite
    4.23   *
    4.24 @@ -110,7 +115,7 @@
    4.25  TestCase
    4.26  LoadTestCase(void *suite, char *testName)
    4.27  {
    4.28 -	TestCase test = (int (*)(void *)) SDL_LoadFunction(suite, testName);
    4.29 +	TestCase test = (TestCase) SDL_LoadFunction(suite, testName);
    4.30  	if(test == NULL) {
    4.31  		fprintf(stderr, "Loading test failed, tests == NULL\n");
    4.32  		fprintf(stderr, "%s\n", SDL_GetError());
    4.33 @@ -119,6 +124,43 @@
    4.34  	return test;
    4.35  }
    4.36  
    4.37 +/*!
    4.38 + * Loads function that initialises the test case from the
    4.39 + * given test suite.
    4.40 + *
    4.41 + * \param suite Used test suite
    4.42 + *
    4.43 + * \return Function pointer (TestCaseInit) which points to loaded init function. NULL if function fails.
    4.44 + */
    4.45 +TestCaseInit
    4.46 +LoadTestCaseInit(void *suite) {
    4.47 +	TestCaseInit testCaseInit = (TestCaseInit) SDL_LoadFunction(suite, "_TestCaseInit");
    4.48 +	if(testCaseInit == NULL) {
    4.49 +		fprintf(stderr, "Loading TestCaseInit function failed, testCaseInit == NULL\n");
    4.50 +		fprintf(stderr, "%s\n", SDL_GetError());
    4.51 +	}
    4.52 +
    4.53 +	return testCaseInit;
    4.54 +}
    4.55 +
    4.56 +/*!
    4.57 + * Loads function that deinitialises the executed test case from the
    4.58 + * given test suite.
    4.59 + *
    4.60 + * \param suite Used test suite
    4.61 + *
    4.62 + * \return Function pointer (TestCaseInit) which points to loaded init function. NULL if function fails.
    4.63 + */
    4.64 +TestCaseQuit
    4.65 +LoadTestCaseQuit(void *suite) {
    4.66 +	TestCaseQuit testCaseQuit = (TestCaseQuit) SDL_LoadFunction(suite, "_TestCaseQuit");
    4.67 +	if(testCaseQuit == NULL) {
    4.68 +		fprintf(stderr, "Loading TestCaseQuit function failed, testCaseQuit == NULL\n");
    4.69 +		fprintf(stderr, "%s\n", SDL_GetError());
    4.70 +	}
    4.71 +
    4.72 +	return testCaseQuit;
    4.73 +}
    4.74  
    4.75  /*!
    4.76   * If using out-of-proc execution of tests. This function
    4.77 @@ -148,6 +190,16 @@
    4.78  }
    4.79  
    4.80  /*!
    4.81 + * Prints usage information
    4.82 + */
    4.83 +void printUsage() {
    4.84 +	  printf("Usage: ./runner [--in-proc] [--help]\n");
    4.85 +	  printf("Options:\n");
    4.86 +	  printf(" --in-proc        Executes tests in-process\n");
    4.87 +	  printf(" --help           Print this help\n");
    4.88 +}
    4.89 +
    4.90 +/*!
    4.91   * Parse command line arguments
    4.92   *
    4.93   * \param argc Count of command line arguments
    4.94 @@ -164,13 +216,13 @@
    4.95           execute_inproc = 1;
    4.96        }
    4.97        else if(SDL_strcmp(arg, "--help") == 0 || SDL_strcmp(arg, "-h") == 0) {
    4.98 -    	  printf("Usage: ./runner [--in-proc] [--help]\n");
    4.99 -    	  printf("Options:\n");
   4.100 -    	  printf(" --in-proc        Executes tests in-process\n");
   4.101 -    	  printf(" --help           Print this help.:\n");
   4.102 +    	  printUsage();
   4.103 +    	  exit(0);
   4.104 +      } else {
   4.105 +    	  printf("runner: unknown command '%s'\n", arg);
   4.106 +    	  printUsage();
   4.107      	  exit(0);
   4.108        }
   4.109 -      // \todo print error for unknown option
   4.110     }
   4.111  }
   4.112  
   4.113 @@ -206,15 +258,25 @@
   4.114  
   4.115  			printf("Running %s (in %s):\n", testname, testSuiteName);
   4.116  
   4.117 +			TestCaseInit testCaseInit = LoadTestCaseInit(suite);
   4.118 +			TestCaseQuit testCaseQuit = LoadTestCaseQuit(suite);
   4.119 +			TestCase test = (TestCase) LoadTestCase(suite, testname);
   4.120 +
   4.121  			int retVal = 1;
   4.122  			if(execute_inproc) {
   4.123 -				TestCase test = (TestCase) LoadTestCase(suite, testname);
   4.124 -				retVal = test(0x0);
   4.125 +				testCaseInit();
   4.126 +
   4.127 +				test(0x0);
   4.128 +
   4.129 +				retVal = testCaseQuit();
   4.130  			} else {
   4.131  				int childpid = fork();
   4.132  				if(childpid == 0) {
   4.133 -					TestCase test = (TestCase) LoadTestCase(suite, testname);
   4.134 -					return test(0x0);
   4.135 +					testCaseInit();
   4.136 +
   4.137 +					test(0x0);
   4.138 +
   4.139 +					return testCaseQuit();
   4.140  				} else {
   4.141  					int stat_lock = -1;
   4.142  					int child = wait(&stat_lock);
     5.1 --- a/test/test-automation/testrect/testrect.c	Wed Jun 01 18:03:09 2011 -0700
     5.2 +++ b/test/test-automation/testrect/testrect.c	Sat Jun 04 17:50:23 2011 +0300
     5.3 @@ -21,8 +21,8 @@
     5.4  	return (TestCaseReference **)testSuite;
     5.5  }
     5.6  
     5.7 -/**
     5.8 - * @brief Tests SDL_IntersectRectAndLine()
     5.9 +/*!
    5.10 + * \brief Tests SDL_IntersectRectAndLine()
    5.11   */
    5.12  int rect_testIntersectRectAndLine (void *arg)
    5.13  {
    5.14 @@ -31,8 +31,6 @@
    5.15      int x2, y2;
    5.16      SDL_bool clipped;
    5.17  
    5.18 -    TestCaseInit();
    5.19 -
    5.20      x1 = -10;
    5.21      y1 = 0;
    5.22      x2 = -10;
    5.23 @@ -132,6 +130,4 @@
    5.24                     x1 == 0 && y1 == 31 && x2 == 31 && y2 == 0,
    5.25          "diagonal line to upper right was incorrectly clipped: %d,%d - %d,%d",
    5.26          x1, y1, x2, y2);
    5.27 -
    5.28 -    return TestCaseQuit();
    5.29  }
     6.1 --- a/test/test-automation/tests/Makefile.am	Wed Jun 01 18:03:09 2011 -0700
     6.2 +++ b/test/test-automation/tests/Makefile.am	Sat Jun 04 17:50:23 2011 +0300
     6.3 @@ -1,6 +1,6 @@
     6.4  lib_LTLIBRARIES = libtest.la
     6.5  libtest_la_SOURCES = test.c ../SDL_test.c
     6.6 -libtest_la_CLAGS = -fPIC -g
     6.7 +libtest_la_CLAGS = -fPIC -g 
     6.8  libtest_la_LDFLAGS = `sdl-config --libs`
     6.9  
    6.10  distclean-local:
     7.1 --- a/test/test-automation/tests/test.c	Wed Jun 01 18:03:09 2011 -0700
     7.2 +++ b/test/test-automation/tests/test.c	Sat Jun 04 17:50:23 2011 +0300
     7.3 @@ -48,36 +48,25 @@
     7.4  }
     7.5  
     7.6  /* Test case functions */
     7.7 -int hello(void *arg)
     7.8 +void hello(void *arg)
     7.9  {
    7.10 -	TestCaseInit();
    7.11 -
    7.12  	const char *revision = SDL_GetRevision();
    7.13  
    7.14  	printf("Revision is %s\n", revision);
    7.15 +
    7.16  	AssertEquals(3, 5, "fails");
    7.17 -
    7.18 -	return TestCaseQuit();
    7.19  }
    7.20  
    7.21 -int hello2(void *arg)
    7.22 +void hello2(void *arg)
    7.23  {
    7.24 -	TestCaseInit();
    7.25 -
    7.26  	char *msg = "eello";
    7.27  	//msg[0] = 'H';
    7.28 -
    7.29 -	return TestCaseQuit();
    7.30 +	AssertTrue(0, "fails");
    7.31  }
    7.32  
    7.33 -int hello3(void *arg)
    7.34 +void hello3(void *arg)
    7.35  {
    7.36 -	TestCaseInit();
    7.37 -	printf("hello3\n");
    7.38 -
    7.39 -	AssertEquals(3, 3, "passes");
    7.40 -
    7.41 -	return TestCaseQuit();
    7.42 +	AssertTrue(1, "passes");
    7.43  }
    7.44  
    7.45  #endif