Commit 1b21c654 authored by Markus Kauppila's avatar Markus Kauppila

Refactoring the TestCaseInit and TestCaseQuit functions

to be caller from the Runner.
parent 450610b7
...@@ -5,7 +5,7 @@ SUBDIRS = tests testrect ...@@ -5,7 +5,7 @@ SUBDIRS = tests testrect
bin_PROGRAMS = runner bin_PROGRAMS = runner
runner_SOURCES = runner.c SDL_test.c runner_SOURCES = runner.c SDL_test.c
runner_CLAGS = -W -Wall -Wextra -g `sdl-config --cflags` -DSDL_NO_COMPAT runner_CLAGS = -W -Wall -Wextra -g `sdl-config --cflags` -DSDL_NO_COMPAT
runner_LDFLAGS = `sdl-config --libs` runner_LDFLAGS = `sdl-config --libs`
install: install-tests install: install-tests
install-tests: install-tests:
......
...@@ -27,13 +27,16 @@ ...@@ -27,13 +27,16 @@
#include "SDL_test.h" #include "SDL_test.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; int _testReturnValue;
static int _testAssertsFailed; /*! \brief counts the failed asserts */
static int _testAssertsPassed; int _testAssertsFailed;
/*! \brief counts the passed asserts */
int _testAssertsPassed;
void void
TestCaseInit() _TestCaseInit()
{ {
_testReturnValue = 0; _testReturnValue = 0;
_testAssertsFailed = 0; _testAssertsFailed = 0;
...@@ -41,13 +44,14 @@ TestCaseInit() ...@@ -41,13 +44,14 @@ TestCaseInit()
} }
int int
TestCaseQuit() _TestCaseQuit()
{ {
printf("Asserts: passed %d, failed %d\n", _testAssertsPassed, _testAssertsFailed); //! \todo make the test fail, if it does not contain any asserts
printf("Asserts: passed %d, failed %d\n", _testAssertsPassed, _testAssertsFailed);fflush(stdout);
return _testReturnValue; return _testReturnValue;
} }
void void
AssertEquals(Uint32 expected, Uint32 actual, char* message, ...) AssertEquals(Uint32 expected, Uint32 actual, char* message, ...)
{ {
va_list args; va_list args;
...@@ -57,7 +61,7 @@ AssertEquals(Uint32 expected, Uint32 actual, char* message, ...) ...@@ -57,7 +61,7 @@ AssertEquals(Uint32 expected, Uint32 actual, char* message, ...)
va_start( args, message ); va_start( args, message );
SDL_vsnprintf( buf, sizeof(buf), message, args ); SDL_vsnprintf( buf, sizeof(buf), message, args );
va_end( args ); va_end( args );
printf("Assert Equals failed: expected %d, got %d; %s\n", expected, actual, buf); printf("Assert Equals failed: expected %d, got %d; %s\n", expected, actual, buf); fflush(stdout);
_testReturnValue = 1; _testReturnValue = 1;
_testAssertsFailed++; _testAssertsFailed++;
} else { } else {
...@@ -65,7 +69,7 @@ AssertEquals(Uint32 expected, Uint32 actual, char* message, ...) ...@@ -65,7 +69,7 @@ AssertEquals(Uint32 expected, Uint32 actual, char* message, ...)
} }
} }
void void
AssertTrue(int condition, char *message, ...) AssertTrue(int condition, char *message, ...)
{ {
va_list args; va_list args;
......
...@@ -23,11 +23,14 @@ ...@@ -23,11 +23,14 @@
#include <SDL/SDL.h> #include <SDL/SDL.h>
extern int _testReturnValue;
extern int _testAssertsFailed;
extern int _testAssertsPassed;
// \todo Should these be consts? // \todo Should these be consts?
#define TEST_ENABLED 1 #define TEST_ENABLED 1
#define TEST_DISABLED 0 #define TEST_DISABLED 0
/*! /*!
* Holds information about a test case * Holds information about a test case
*/ */
...@@ -38,23 +41,28 @@ typedef struct TestCaseReference { ...@@ -38,23 +41,28 @@ typedef struct TestCaseReference {
long requirements; /*!< Set to TEST_REQUIRES_OPENGL, TEST_REQUIRES_AUDIO, ... */ long requirements; /*!< Set to TEST_REQUIRES_OPENGL, TEST_REQUIRES_AUDIO, ... */
} TestCaseReference; } TestCaseReference;
/*! \fn TestCaseInit /*! \fn _TestCaseInit
* Initialized the test case. Must be called at * Initialized the test case. Must be called at
* the beginning of every test case, before doing * the beginning of every test case, before doing
* anything else. * anything else.
*/ */
void TestCaseInit(); 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 * \return 0 if test succeeded, otherwise 1
*/ */
int TestCaseQuit(); int _TestCaseQuit();
/*!
* todo add comment
*/
void AssertEquals(Uint32 expected, Uint32 actual, char *message, ...); void AssertEquals(Uint32 expected, Uint32 actual, char *message, ...);
/*!
* todo add comment
*/
void AssertTrue(int condition, char *message, ...); void AssertTrue(int condition, char *message, ...);
#endif #endif
...@@ -28,7 +28,11 @@ ...@@ -28,7 +28,11 @@
#include "SDL_test.h" #include "SDL_test.h"
//!< Function pointer to a test case function //!< Function pointer to a test case function
typedef int (*TestCase)(void *arg); typedef void (*TestCase)(void *arg);
//!< Function pointer to a test case init function
typedef void (*TestCaseInit)(void);
//!< Function pointer to a test case quit function
typedef int (*TestCaseQuit)(void);
//!< Flag for executing tests in-process //!< Flag for executing tests in-process
static int execute_inproc = 0; static int execute_inproc = 0;
...@@ -99,6 +103,7 @@ QueryTestCases(void *library) ...@@ -99,6 +103,7 @@ QueryTestCases(void *library)
return tests; return tests;
} }
/*! /*!
* Loads test case from a test suite * Loads test case from a test suite
* *
...@@ -110,7 +115,7 @@ QueryTestCases(void *library) ...@@ -110,7 +115,7 @@ QueryTestCases(void *library)
TestCase TestCase
LoadTestCase(void *suite, char *testName) LoadTestCase(void *suite, char *testName)
{ {
TestCase test = (int (*)(void *)) SDL_LoadFunction(suite, testName); TestCase test = (TestCase) SDL_LoadFunction(suite, testName);
if(test == NULL) { if(test == NULL) {
fprintf(stderr, "Loading test failed, tests == NULL\n"); fprintf(stderr, "Loading test failed, tests == NULL\n");
fprintf(stderr, "%s\n", SDL_GetError()); fprintf(stderr, "%s\n", SDL_GetError());
...@@ -119,6 +124,43 @@ LoadTestCase(void *suite, char *testName) ...@@ -119,6 +124,43 @@ LoadTestCase(void *suite, char *testName)
return test; return test;
} }
/*!
* Loads function that initialises the test case from the
* given test suite.
*
* \param suite Used test suite
*
* \return Function pointer (TestCaseInit) which points to loaded init function. NULL if function fails.
*/
TestCaseInit
LoadTestCaseInit(void *suite) {
TestCaseInit testCaseInit = (TestCaseInit) SDL_LoadFunction(suite, "_TestCaseInit");
if(testCaseInit == NULL) {
fprintf(stderr, "Loading TestCaseInit function failed, testCaseInit == NULL\n");
fprintf(stderr, "%s\n", SDL_GetError());
}
return testCaseInit;
}
/*!
* Loads function that deinitialises the executed test case from the
* given test suite.
*
* \param suite Used test suite
*
* \return Function pointer (TestCaseInit) which points to loaded init function. NULL if function fails.
*/
TestCaseQuit
LoadTestCaseQuit(void *suite) {
TestCaseQuit testCaseQuit = (TestCaseQuit) SDL_LoadFunction(suite, "_TestCaseQuit");
if(testCaseQuit == NULL) {
fprintf(stderr, "Loading TestCaseQuit function failed, testCaseQuit == NULL\n");
fprintf(stderr, "%s\n", SDL_GetError());
}
return testCaseQuit;
}
/*! /*!
* If using out-of-proc execution of tests. This function * If using out-of-proc execution of tests. This function
...@@ -147,6 +189,16 @@ HandleTestReturnValue(int stat_lock) ...@@ -147,6 +189,16 @@ HandleTestReturnValue(int stat_lock)
return returnValue; return returnValue;
} }
/*!
* Prints usage information
*/
void printUsage() {
printf("Usage: ./runner [--in-proc] [--help]\n");
printf("Options:\n");
printf(" --in-proc Executes tests in-process\n");
printf(" --help Print this help\n");
}
/*! /*!
* Parse command line arguments * Parse command line arguments
* *
...@@ -164,13 +216,13 @@ ParseOptions(int argc, char *argv[]) ...@@ -164,13 +216,13 @@ ParseOptions(int argc, char *argv[])
execute_inproc = 1; execute_inproc = 1;
} }
else if(SDL_strcmp(arg, "--help") == 0 || SDL_strcmp(arg, "-h") == 0) { else if(SDL_strcmp(arg, "--help") == 0 || SDL_strcmp(arg, "-h") == 0) {
printf("Usage: ./runner [--in-proc] [--help]\n"); printUsage();
printf("Options:\n"); exit(0);
printf(" --in-proc Executes tests in-process\n"); } else {
printf(" --help Print this help.:\n"); printf("runner: unknown command '%s'\n", arg);
printUsage();
exit(0); exit(0);
} }
// \todo print error for unknown option
} }
} }
...@@ -206,15 +258,25 @@ main(int argc, char *argv[]) ...@@ -206,15 +258,25 @@ main(int argc, char *argv[])
printf("Running %s (in %s):\n", testname, testSuiteName); printf("Running %s (in %s):\n", testname, testSuiteName);
TestCaseInit testCaseInit = LoadTestCaseInit(suite);
TestCaseQuit testCaseQuit = LoadTestCaseQuit(suite);
TestCase test = (TestCase) LoadTestCase(suite, testname);
int retVal = 1; int retVal = 1;
if(execute_inproc) { if(execute_inproc) {
TestCase test = (TestCase) LoadTestCase(suite, testname); testCaseInit();
retVal = test(0x0);
test(0x0);
retVal = testCaseQuit();
} else { } else {
int childpid = fork(); int childpid = fork();
if(childpid == 0) { if(childpid == 0) {
TestCase test = (TestCase) LoadTestCase(suite, testname); testCaseInit();
return test(0x0);
test(0x0);
return testCaseQuit();
} else { } else {
int stat_lock = -1; int stat_lock = -1;
int child = wait(&stat_lock); int child = wait(&stat_lock);
......
...@@ -21,8 +21,8 @@ TestCaseReference **QueryTestSuite() { ...@@ -21,8 +21,8 @@ TestCaseReference **QueryTestSuite() {
return (TestCaseReference **)testSuite; return (TestCaseReference **)testSuite;
} }
/** /*!
* @brief Tests SDL_IntersectRectAndLine() * \brief Tests SDL_IntersectRectAndLine()
*/ */
int rect_testIntersectRectAndLine (void *arg) int rect_testIntersectRectAndLine (void *arg)
{ {
...@@ -31,8 +31,6 @@ int rect_testIntersectRectAndLine (void *arg) ...@@ -31,8 +31,6 @@ int rect_testIntersectRectAndLine (void *arg)
int x2, y2; int x2, y2;
SDL_bool clipped; SDL_bool clipped;
TestCaseInit();
x1 = -10; x1 = -10;
y1 = 0; y1 = 0;
x2 = -10; x2 = -10;
...@@ -132,6 +130,4 @@ int rect_testIntersectRectAndLine (void *arg) ...@@ -132,6 +130,4 @@ int rect_testIntersectRectAndLine (void *arg)
x1 == 0 && y1 == 31 && x2 == 31 && y2 == 0, x1 == 0 && y1 == 31 && x2 == 31 && y2 == 0,
"diagonal line to upper right was incorrectly clipped: %d,%d - %d,%d", "diagonal line to upper right was incorrectly clipped: %d,%d - %d,%d",
x1, y1, x2, y2); x1, y1, x2, y2);
return TestCaseQuit();
} }
lib_LTLIBRARIES = libtest.la lib_LTLIBRARIES = libtest.la
libtest_la_SOURCES = test.c ../SDL_test.c libtest_la_SOURCES = test.c ../SDL_test.c
libtest_la_CLAGS = -fPIC -g libtest_la_CLAGS = -fPIC -g
libtest_la_LDFLAGS = `sdl-config --libs` libtest_la_LDFLAGS = `sdl-config --libs`
distclean-local: distclean-local:
......
...@@ -48,36 +48,25 @@ TestCaseReference **QueryTestSuite() { ...@@ -48,36 +48,25 @@ TestCaseReference **QueryTestSuite() {
} }
/* Test case functions */ /* Test case functions */
int hello(void *arg) void hello(void *arg)
{ {
TestCaseInit();
const char *revision = SDL_GetRevision(); const char *revision = SDL_GetRevision();
printf("Revision is %s\n", revision); printf("Revision is %s\n", revision);
AssertEquals(3, 5, "fails");
return TestCaseQuit(); AssertEquals(3, 5, "fails");
} }
int hello2(void *arg) void hello2(void *arg)
{ {
TestCaseInit();
char *msg = "eello"; char *msg = "eello";
//msg[0] = 'H'; //msg[0] = 'H';
AssertTrue(0, "fails");
return TestCaseQuit();
} }
int hello3(void *arg) void hello3(void *arg)
{ {
TestCaseInit(); AssertTrue(1, "passes");
printf("hello3\n");
AssertEquals(3, 3, "passes");
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