Commit bc147958 authored by Markus Kauppila's avatar Markus Kauppila

Refactoring the code (runner.c).

Adding support for executing tests in-proc.
parent 73fa28c7
...@@ -27,6 +27,9 @@ ...@@ -27,6 +27,9 @@
#include "tests/SDL_test.h" #include "tests/SDL_test.h"
//!< Function pointer to a test case function
typedef int (*TestCase)(void *arg);
/*! /*!
* Loads test suite which is implemented as dynamic library. * Loads test suite which is implemented as dynamic library.
* *
...@@ -72,44 +75,77 @@ TestCaseReference **QueryTestCases(void *library) { ...@@ -72,44 +75,77 @@ TestCaseReference **QueryTestCases(void *library) {
return tests; return tests;
} }
/* /*!
* Loads test case from a test suite
*
* \param Test suite
* \testName Name of the test that is going to be loaded
* *
* \return loaded test
*/ */
TestCase LoadTestCase(void *suite, char *testName) {
TestCase test = (int (*)(void *)) SDL_LoadFunction(suite, testName);
if(test == NULL) {
printf("Loading test failed, tests == NULL\n");
printf("%s\n", SDL_GetError());
}
return test;
}
/*! /*!
* Success or failure of test case is determined by * If using out-of-proc execution of tests. This function
* it's return value. If test case succeeds, it'll * will handle the return value of the child process
* return 0, if not it will return a positive integer. * and interprets it to the runner. Also prints warnings
* if child was aborted by a signela.
* *
* The function checks the return value and returns value * \param stat_lock information about the exited child process
* based on it. If the test is aborted due to a signal
* function warn about it.
* *
* \return 1 if test case succeeded, 0 otherwise * \return 0 if test case succeeded, 1 otherwise
*/ */
int HandleTestReturnValue(int stat_lock) { int HandleTestReturnValue(int stat_lock) {
if(WIFEXITED(stat_lock)) { //! \todo rename to: HandleChildReturn Value
int returnValue = WEXITSTATUS(stat_lock); int returnValue = -1;
if(returnValue == 0) { if(WIFEXITED(stat_lock)) {
return 1; returnValue = WEXITSTATUS(stat_lock);
}
} else if(WIFSIGNALED(stat_lock)) { } else if(WIFSIGNALED(stat_lock)) {
int signal = WTERMSIG(stat_lock); int signal = WTERMSIG(stat_lock);
printf("FAILURE: test was aborted due to signal nro %d\n", signal); printf("FAILURE: test was aborted due to signal nro %d\n", signal);
returnValue = 1;
} else if(WIFSTOPPED(stat_lock)) { } else if(WIFSTOPPED(stat_lock)) {
} }
return 0; return returnValue;
} }
//!< Flag for executing tests in-process
int execute_inproc = 0;
int main(int argc, char *argv[]) { /*!
* Parse command line arguments
*/
void
ParseOptions(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; ++i) {
const char *arg = argv[i];
if (SDL_strcmp(arg, "--in-proc") == 0) {
execute_inproc = 1;
}
}
}
//! \todo Handle command line arguments int
main(int argc, char *argv[])
{
ParseOptions(argc, argv);
// print: Testing againts SDL version fuu (rev: bar) // print: Testing againts SDL version fuu (rev: bar) if verbose == true
int failureCount = 0, passCount = 0; int failureCount = 0, passCount = 0;
...@@ -131,34 +167,30 @@ int main(int argc, char *argv[]) { ...@@ -131,34 +167,30 @@ int main(int argc, char *argv[]) {
printf("Running %s (in %s):\n", testname, libName); printf("Running %s (in %s):\n", testname, libName);
int childpid = fork(); int retVal = 1;
if(childpid == 0) { if(execute_inproc) {
void (*test)(void *arg); TestCase test = (TestCase) LoadTestCase(suite, testname);
retVal = test(0x0);
test = (void (*)(void *)) SDL_LoadFunction(suite, testname);
if(test == NULL) {
printf("Loading test failed, tests == NULL\n");
printf("%s\n", SDL_GetError());
} else {
test(0x0);
}
return 0; // exit the child if the test didn't exit
} else { } else {
int stat_lock = -1; int childpid = fork();
int child = wait(&stat_lock); if(childpid == 0) {
TestCase test = (TestCase) LoadTestCase(suite, testname);
int passed = -1; return test(0x0);
passed = HandleTestReturnValue(stat_lock);
if(passed) {
passCount++;
printf("%s (in %s): ok\n", testname, libName);
} else { } else {
failureCount++; int stat_lock = -1;
printf("%s (in %s): failed\n", testname, libName); int child = wait(&stat_lock);
retVal = HandleTestReturnValue(stat_lock);
} }
} }
if(retVal) {
failureCount++;
printf("%s (in %s): FAILED\n", testname, libName);
} else {
passCount++;
printf("%s (in %s): ok\n", testname, libName);
}
} }
printf("\n"); printf("\n");
...@@ -168,7 +200,7 @@ int main(int argc, char *argv[]) { ...@@ -168,7 +200,7 @@ int main(int argc, char *argv[]) {
const Uint32 endTicks = SDL_GetTicks(); const Uint32 endTicks = SDL_GetTicks();
printf("Ran %d tests in %0.3f seconds.\n", (passCount + failureCount), (endTicks-startTicks)/1000.0f); printf("Ran %d tests in %0.5f seconds.\n", (passCount + failureCount), (endTicks-startTicks)/1000.0f);
printf("%d tests passed\n", passCount); printf("%d tests passed\n", passCount);
printf("%d tests failed\n", failureCount); printf("%d tests failed\n", failureCount);
......
...@@ -23,8 +23,6 @@ ...@@ -23,8 +23,6 @@
#include "SDL_test.h" #include "SDL_test.h"
#include <stdlib.h>
/*! \brief return value of test case. Non-zero value means that the test failed */ /*! \brief return value of test case. Non-zero value means that the test failed */
static int _testReturnValue; static int _testReturnValue;
...@@ -34,10 +32,10 @@ TestCaseInit() ...@@ -34,10 +32,10 @@ TestCaseInit()
_testReturnValue = 0; _testReturnValue = 0;
} }
void int
TestCaseQuit() TestCaseQuit()
{ {
exit(_testReturnValue); return _testReturnValue;
} }
void void
......
...@@ -48,8 +48,9 @@ void TestCaseInit(); ...@@ -48,8 +48,9 @@ void TestCaseInit();
/*! \fn TestCaseQuit /*! \fn TestCaseQuit
* Deinitializes and exits the test case * Deinitializes and exits the test case
* *
* \return 0 if test succeeded, otherwise 1
*/ */
void TestCaseQuit(); int TestCaseQuit();
void AssertEquals(char *message, Uint32 expected, Uint32 actual); void AssertEquals(char *message, Uint32 expected, Uint32 actual);
......
...@@ -32,7 +32,7 @@ static const TestCaseReference test1 = ...@@ -32,7 +32,7 @@ static const TestCaseReference test1 =
(TestCaseReference){ "hello", "description", TEST_ENABLED, 0 }; (TestCaseReference){ "hello", "description", TEST_ENABLED, 0 };
static const TestCaseReference test2 = static const TestCaseReference test2 =
(TestCaseReference){ "hello2", "description", TEST_DISABLED, 0 }; (TestCaseReference){ "hello2", "description", TEST_ENABLED, 0 };
static const TestCaseReference test3 = static const TestCaseReference test3 =
(TestCaseReference){ "hello3", "description", TEST_ENABLED, 0 }; (TestCaseReference){ "hello3", "description", TEST_ENABLED, 0 };
...@@ -48,7 +48,7 @@ TestCaseReference **QueryTestSuite() { ...@@ -48,7 +48,7 @@ TestCaseReference **QueryTestSuite() {
} }
/* Test case functions */ /* Test case functions */
void hello(void *arg) int hello(void *arg)
{ {
TestCaseInit(); TestCaseInit();
...@@ -57,27 +57,27 @@ void hello(void *arg) ...@@ -57,27 +57,27 @@ void hello(void *arg)
printf("Revision is %s\n", revision); printf("Revision is %s\n", revision);
AssertEquals("will fail", 3, 5); AssertEquals("will fail", 3, 5);
TestCaseQuit(); return TestCaseQuit();
} }
void hello2(void *arg) int hello2(void *arg)
{ {
TestCaseInit(); TestCaseInit();
char *msg = "eello"; char *msg = "eello";
msg[0] = 'H'; //msg[0] = 'H';
TestCaseQuit(); return TestCaseQuit();
} }
void hello3(void *arg) int hello3(void *arg)
{ {
TestCaseInit(); TestCaseInit();
printf("hello3\n"); printf("hello3\n");
AssertEquals("passes", 3, 3); AssertEquals("passes", 3, 3);
TestCaseQuit(); return TestCaseQuit();
} }
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment