SDL-mirror/test/test-automation/runner.c

210 lines
5.1 KiB
C
Raw Normal View History

/*
Copyright (C) 2011 Markus Kauppila <markus.kauppila@gmail.com>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
2011-05-23 17:49:06 +03:00
#include "SDL/SDL.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "tests/SDL_test.h"
2011-05-23 17:49:06 +03:00
//!< Function pointer to a test case function
typedef int (*TestCase)(void *arg);
2011-05-30 11:53:59 +03:00
/*!
* Loads test suite which is implemented as dynamic library.
*
* \return Loaded test suite
*/
void *LoadTestSuite() {
2011-05-25 09:45:03 -07:00
#if defined(linux) || defined( __linux)
char *libName = "tests/libtest.so";
#else
char *libName = "tests/libtest.dylib";
2011-05-25 09:45:03 -07:00
#endif
void *library = SDL_LoadObject(libName);
if(library == NULL) {
printf("Loading %s failed\n", libName);
printf("%s\n", SDL_GetError());
}
return library;
}
2011-05-30 11:53:59 +03:00
/*!
* Loads the test case references from the given test suite.
* \param library Previously loaded dynamic library AKA test suite
* \return Loaded TestCaseReferences
*/
2011-05-26 19:19:46 +03:00
TestCaseReference **QueryTestCases(void *library) {
TestCaseReference **(*suite)(void);
2011-05-30 11:53:59 +03:00
suite = (TestCaseReference **(*)(void)) SDL_LoadFunction(library, "QueryTestSuite");
if(suite == NULL) {
2011-05-26 19:19:46 +03:00
printf("Loading QueryTestCaseReferences() failed.\n");
printf("%s\n", SDL_GetError());
}
2011-05-26 19:19:46 +03:00
TestCaseReference **tests = suite();
if(tests == NULL) {
2011-05-26 19:19:46 +03:00
printf("Failed to load test references.\n");
printf("%s\n", SDL_GetError());
}
return tests;
}
/*!
* Loads test case from a test suite
*
* \param Test suite
* \testName Name of the test that is going to be loaded
2011-05-30 11:53:59 +03:00
*
* \return loaded test
2011-05-30 11:53:59 +03:00
*/
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;
}
2011-05-30 11:53:59 +03:00
/*!
* If using out-of-proc execution of tests. This function
* will handle the return value of the child process
* and interprets it to the runner. Also prints warnings
* if child was aborted by a signela.
2011-05-30 11:53:59 +03:00
*
* \param stat_lock information about the exited child process
2011-05-30 11:53:59 +03:00
*
* \return 0 if test case succeeded, 1 otherwise
2011-05-30 11:53:59 +03:00
*/
int HandleTestReturnValue(int stat_lock) {
//! \todo rename to: HandleChildReturn Value
int returnValue = -1;
if(WIFEXITED(stat_lock)) {
returnValue = WEXITSTATUS(stat_lock);
} else if(WIFSIGNALED(stat_lock)) {
int signal = WTERMSIG(stat_lock);
printf("FAILURE: test was aborted due to signal nro %d\n", signal);
returnValue = 1;
} else if(WIFSTOPPED(stat_lock)) {
}
return returnValue;
}
//!< Flag for executing tests in-process
int execute_inproc = 0;
/*!
* 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;
}
}
}
int
main(int argc, char *argv[])
{
ParseOptions(argc, argv);
// print: Testing againts SDL version fuu (rev: bar) if verbose == true
int failureCount = 0, passCount = 0;
char *libName = "libtest";
const Uint32 startTicks = SDL_GetTicks();
2011-05-30 11:53:59 +03:00
void *suite = LoadTestSuite();
TestCaseReference **tests = QueryTestCases(suite);
2011-05-26 19:19:46 +03:00
2011-05-26 19:57:03 +03:00
TestCaseReference *reference = NULL;
int counter = 0;
for(reference = tests[counter]; reference; reference = tests[++counter]) {
2011-05-30 11:53:59 +03:00
if(reference->enabled == TEST_DISABLED) {
printf("Test %s (in %s) disabled. Omitting...\n", reference->name, libName);
} else {
2011-05-30 11:53:59 +03:00
char *testname = reference->name;
printf("Running %s (in %s):\n", testname, libName);
int retVal = 1;
if(execute_inproc) {
TestCase test = (TestCase) LoadTestCase(suite, testname);
retVal = test(0x0);
2011-05-30 11:53:59 +03:00
} else {
int childpid = fork();
if(childpid == 0) {
TestCase test = (TestCase) LoadTestCase(suite, testname);
return test(0x0);
2011-05-30 11:53:59 +03:00
} else {
int stat_lock = -1;
int child = wait(&stat_lock);
retVal = HandleTestReturnValue(stat_lock);
2011-05-30 11:53:59 +03:00
}
}
if(retVal) {
failureCount++;
printf("%s (in %s): FAILED\n", testname, libName);
} else {
passCount++;
printf("%s (in %s): ok\n", testname, libName);
}
}
printf("\n");
}
2011-05-30 11:53:59 +03:00
SDL_UnloadObject(suite);
2011-05-23 17:49:06 +03:00
const Uint32 endTicks = SDL_GetTicks();
printf("Ran %d tests in %0.5f seconds.\n", (passCount + failureCount), (endTicks-startTicks)/1000.0f);
2011-05-23 17:49:06 +03:00
printf("%d tests passed\n", passCount);
printf("%d tests failed\n", failureCount);
return 0;
}