Refactoring the code (runner.c).
authorMarkus Kauppila <markus.kauppila@gmail.com>
Mon, 30 May 2011 12:55:40 +0300
changeset 5672d2df9dbce0ea
parent 5671 5b0e288921eb
child 5673 07b920bcff70
Refactoring the code (runner.c).
Adding support for executing tests in-proc.
test/test-automation/runner.c
test/test-automation/tests/SDL_test.c
test/test-automation/tests/SDL_test.h
test/test-automation/tests/test.c
     1.1 --- a/test/test-automation/runner.c	Mon May 30 11:53:59 2011 +0300
     1.2 +++ b/test/test-automation/runner.c	Mon May 30 12:55:40 2011 +0300
     1.3 @@ -27,6 +27,9 @@
     1.4  
     1.5  #include "tests/SDL_test.h"
     1.6  
     1.7 +//!< Function pointer to a test case function
     1.8 +typedef int (*TestCase)(void *arg);
     1.9 +
    1.10  /*!
    1.11   * Loads test suite which is implemented as dynamic library.
    1.12   *
    1.13 @@ -72,44 +75,77 @@
    1.14  	return tests;
    1.15  }
    1.16  
    1.17 -/*
    1.18 +/*!
    1.19 + * Loads test case from a test suite
    1.20 + *
    1.21 + * \param Test suite
    1.22 + * \testName Name of the test that is going to be loaded
    1.23   *
    1.24 + * \return loaded test
    1.25   */
    1.26 +TestCase LoadTestCase(void *suite, char *testName) {
    1.27 +	TestCase test = (int (*)(void *)) SDL_LoadFunction(suite, testName);
    1.28 +	if(test == NULL) {
    1.29 +		printf("Loading test failed, tests == NULL\n");
    1.30 +		printf("%s\n", SDL_GetError());
    1.31 +	}
    1.32 +
    1.33 +	return test;
    1.34 +}
    1.35 +
    1.36 +
    1.37  
    1.38  /*!
    1.39 - * Success or failure of test case is determined by
    1.40 - * it's return value. If test case succeeds, it'll
    1.41 - * return 0, if not it will return a positive integer.
    1.42 + * If using out-of-proc execution of tests. This function
    1.43 + * will handle the return value of the child process
    1.44 + * and interprets it to the runner. Also prints warnings
    1.45 + * if child was aborted by a signela.
    1.46   *
    1.47 - * The function checks the return value and returns value
    1.48 - * based on it. If the test is aborted due to a signal
    1.49 - * function warn about it.
    1.50 + * \param stat_lock information about the exited child process
    1.51   *
    1.52 - * \return 1 if test case succeeded, 0 otherwise
    1.53 + * \return 0 if test case succeeded, 1 otherwise
    1.54   */
    1.55  int HandleTestReturnValue(int stat_lock) {
    1.56 -	if(WIFEXITED(stat_lock)) {
    1.57 -		int returnValue = WEXITSTATUS(stat_lock);
    1.58 +	//! \todo rename to: HandleChildReturn Value
    1.59 +	int returnValue = -1;
    1.60  
    1.61 -		if(returnValue == 0) {
    1.62 -			return  1;
    1.63 -		}
    1.64 +	if(WIFEXITED(stat_lock)) {
    1.65 +		returnValue = WEXITSTATUS(stat_lock);
    1.66  	} else if(WIFSIGNALED(stat_lock)) {
    1.67  		int signal = WTERMSIG(stat_lock);
    1.68  		printf("FAILURE: test was aborted due to signal nro %d\n", signal);
    1.69 -
    1.70 +		returnValue = 1;
    1.71  	} else if(WIFSTOPPED(stat_lock)) {
    1.72  	}
    1.73  
    1.74 -	return 0;
    1.75 +	return returnValue;
    1.76  }
    1.77  
    1.78 +//!< Flag for executing tests in-process
    1.79 +int execute_inproc = 0;
    1.80  
    1.81 -int main(int argc, char *argv[]) {
    1.82 +/*!
    1.83 + * Parse command line arguments
    1.84 + */
    1.85 +void
    1.86 +ParseOptions(int argc, char *argv[])
    1.87 +{
    1.88 +   int i;
    1.89  
    1.90 -	//! \todo Handle command line arguments
    1.91 +   for (i = 1; i < argc; ++i) {
    1.92 +      const char *arg = argv[i];
    1.93 +      if (SDL_strcmp(arg, "--in-proc") == 0) {
    1.94 +         execute_inproc = 1;
    1.95 +      }
    1.96 +   }
    1.97 +}
    1.98  
    1.99 -	// print: Testing againts SDL version fuu (rev: bar)
   1.100 +int
   1.101 +main(int argc, char *argv[])
   1.102 +{
   1.103 +	ParseOptions(argc, argv);
   1.104 +
   1.105 +	// print: Testing againts SDL version fuu (rev: bar) if verbose == true
   1.106  
   1.107  	int failureCount = 0, passCount = 0;
   1.108  
   1.109 @@ -131,34 +167,30 @@
   1.110  
   1.111  			printf("Running %s (in %s):\n", testname, libName);
   1.112  
   1.113 -			int childpid = fork();
   1.114 -			if(childpid == 0) {
   1.115 -				void (*test)(void *arg);
   1.116 -
   1.117 -				test = (void (*)(void *)) SDL_LoadFunction(suite, testname);
   1.118 -				if(test == NULL) {
   1.119 -					printf("Loading test failed, tests == NULL\n");
   1.120 -					printf("%s\n", SDL_GetError());
   1.121 -				} else {
   1.122 -					test(0x0);
   1.123 -				}
   1.124 -				return 0; // exit the child if the test didn't exit
   1.125 +			int retVal = 1;
   1.126 +			if(execute_inproc) {
   1.127 +				TestCase test = (TestCase) LoadTestCase(suite, testname);
   1.128 +				retVal = test(0x0);
   1.129  			} else {
   1.130 -				int stat_lock = -1;
   1.131 -				int child = wait(&stat_lock);
   1.132 -
   1.133 -				int passed = -1;
   1.134 -
   1.135 -				passed = HandleTestReturnValue(stat_lock);
   1.136 +				int childpid = fork();
   1.137 +				if(childpid == 0) {
   1.138 +					TestCase test = (TestCase) LoadTestCase(suite, testname);
   1.139 +					return test(0x0);
   1.140 +				} else {
   1.141 +					int stat_lock = -1;
   1.142 +					int child = wait(&stat_lock);
   1.143  
   1.144 -				if(passed) {
   1.145 -					passCount++;
   1.146 -					printf("%s (in %s): ok\n", testname, libName);
   1.147 -				} else {
   1.148 -					failureCount++;
   1.149 -					printf("%s (in %s): failed\n", testname, libName);
   1.150 +					retVal = HandleTestReturnValue(stat_lock);
   1.151  				}
   1.152  			}
   1.153 +
   1.154 +			if(retVal) {
   1.155 +				failureCount++;
   1.156 +				printf("%s (in %s): FAILED\n", testname, libName);
   1.157 +			} else {
   1.158 +				passCount++;
   1.159 +				printf("%s (in %s): ok\n", testname, libName);
   1.160 +			}
   1.161  		}
   1.162  
   1.163  		printf("\n");
   1.164 @@ -168,7 +200,7 @@
   1.165  
   1.166  	const Uint32 endTicks = SDL_GetTicks();
   1.167  
   1.168 -	printf("Ran %d tests in %0.3f seconds.\n", (passCount + failureCount), (endTicks-startTicks)/1000.0f);
   1.169 +	printf("Ran %d tests in %0.5f seconds.\n", (passCount + failureCount), (endTicks-startTicks)/1000.0f);
   1.170  
   1.171  	printf("%d tests passed\n", passCount);
   1.172  	printf("%d tests failed\n", failureCount);
     2.1 --- a/test/test-automation/tests/SDL_test.c	Mon May 30 11:53:59 2011 +0300
     2.2 +++ b/test/test-automation/tests/SDL_test.c	Mon May 30 12:55:40 2011 +0300
     2.3 @@ -23,8 +23,6 @@
     2.4  
     2.5  #include "SDL_test.h"
     2.6  
     2.7 -#include <stdlib.h>
     2.8 -
     2.9  /*! \brief return value of test case. Non-zero value means that the test failed */
    2.10  static int _testReturnValue;
    2.11  
    2.12 @@ -34,10 +32,10 @@
    2.13  	_testReturnValue = 0;
    2.14  }
    2.15  
    2.16 -void
    2.17 +int
    2.18  TestCaseQuit()
    2.19  {
    2.20 -	exit(_testReturnValue);
    2.21 +	return _testReturnValue;
    2.22  }
    2.23  
    2.24  void
     3.1 --- a/test/test-automation/tests/SDL_test.h	Mon May 30 11:53:59 2011 +0300
     3.2 +++ b/test/test-automation/tests/SDL_test.h	Mon May 30 12:55:40 2011 +0300
     3.3 @@ -48,8 +48,9 @@
     3.4  /*! \fn TestCaseQuit
     3.5   *  Deinitializes and exits the test case
     3.6   *
     3.7 + * \return 0 if test succeeded, otherwise 1
     3.8   */
     3.9 -void TestCaseQuit();
    3.10 +int TestCaseQuit();
    3.11  
    3.12  
    3.13  void AssertEquals(char *message, Uint32 expected, Uint32 actual);
     4.1 --- a/test/test-automation/tests/test.c	Mon May 30 11:53:59 2011 +0300
     4.2 +++ b/test/test-automation/tests/test.c	Mon May 30 12:55:40 2011 +0300
     4.3 @@ -32,7 +32,7 @@
     4.4  		(TestCaseReference){ "hello", "description", TEST_ENABLED, 0 };
     4.5  
     4.6  static const TestCaseReference test2 =
     4.7 -		(TestCaseReference){ "hello2", "description", TEST_DISABLED, 0 };
     4.8 +		(TestCaseReference){ "hello2", "description", TEST_ENABLED, 0 };
     4.9  
    4.10  static const TestCaseReference test3 =
    4.11  		(TestCaseReference){ "hello3", "description", TEST_ENABLED, 0 };
    4.12 @@ -48,7 +48,7 @@
    4.13  }
    4.14  
    4.15  /* Test case functions */
    4.16 -void hello(void *arg)
    4.17 +int hello(void *arg)
    4.18  {
    4.19  	TestCaseInit();
    4.20  
    4.21 @@ -57,27 +57,27 @@
    4.22  	printf("Revision is %s\n", revision);
    4.23  	AssertEquals("will fail", 3, 5);
    4.24  
    4.25 -	TestCaseQuit();
    4.26 +	return TestCaseQuit();
    4.27  }
    4.28  
    4.29 -void hello2(void *arg)
    4.30 +int hello2(void *arg)
    4.31  {
    4.32  	TestCaseInit();
    4.33  
    4.34  	char *msg = "eello";
    4.35 -	msg[0] = 'H';
    4.36 +	//msg[0] = 'H';
    4.37  
    4.38 -	TestCaseQuit();
    4.39 +	return TestCaseQuit();
    4.40  }
    4.41  
    4.42 -void hello3(void *arg)
    4.43 +int hello3(void *arg)
    4.44  {
    4.45  	TestCaseInit();
    4.46  	printf("hello3\n");
    4.47  
    4.48  	AssertEquals("passes", 3, 3);
    4.49  
    4.50 -	TestCaseQuit();
    4.51 +	return TestCaseQuit();
    4.52  }
    4.53  
    4.54  #endif