Refactoring the massive main() to smaller functions.
--HG-- rename : test/test-automation/tests/asserts.c => test/test-automation/tests/SDL_test.c rename : test/test-automation/tests/asserts.h => test/test-automation/tests/SDL_test.h
This commit is contained in:
parent
4540a8a1b9
commit
d6d8dec05e
7 changed files with 137 additions and 83 deletions
|
@ -3,7 +3,7 @@ ACLOCAL_AMFLAGS = -I acinclude -I build-scripts
|
||||||
SUBDIRS = tests
|
SUBDIRS = tests
|
||||||
|
|
||||||
bin_PROGRAMS = runner
|
bin_PROGRAMS = runner
|
||||||
runner_SOURCES = runner.c
|
runner_SOURCES = runner.c tests/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`
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,9 @@ AC_CHECK_HEADERS([stdlib.h unistd.h])
|
||||||
|
|
||||||
# Checks for typedefs, structures, and compiler characteristics.
|
# Checks for typedefs, structures, and compiler characteristics.
|
||||||
|
|
||||||
|
# without this debugging information will be stripped (at least on OS X)
|
||||||
|
CFLAGS="-g"
|
||||||
|
|
||||||
# Checks for library functions.
|
# Checks for library functions.
|
||||||
AC_FUNC_FORK
|
AC_FUNC_FORK
|
||||||
|
|
||||||
|
|
|
@ -25,21 +25,13 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include "tests/SDL_test.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
void *LoadLibrary() {
|
||||||
|
|
||||||
// Handle command line arguments
|
|
||||||
|
|
||||||
// print: Testing againts SDL version fuu (rev: bar)
|
|
||||||
|
|
||||||
int failureCount = 0, passCount = 0;
|
|
||||||
|
|
||||||
const Uint32 startTicks = SDL_GetTicks();
|
|
||||||
|
|
||||||
#if defined(linux) || defined( __linux)
|
#if defined(linux) || defined( __linux)
|
||||||
char *libName = "tests/libtest.so";
|
char *libName = "tests/libtest.so";
|
||||||
#else
|
#else
|
||||||
char *libName = "tests/libtest.0.dylib";
|
char *libName = "tests/libtest.dylib";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void *library = SDL_LoadObject(libName);
|
void *library = SDL_LoadObject(libName);
|
||||||
|
@ -48,71 +40,100 @@ int main(int argc, char *argv[]) {
|
||||||
printf("%s\n", SDL_GetError());
|
printf("%s\n", SDL_GetError());
|
||||||
}
|
}
|
||||||
|
|
||||||
const char **(*suite)(void);
|
return library;
|
||||||
suite = (const char **(*)(void)) SDL_LoadFunction(library, "suite");
|
}
|
||||||
|
|
||||||
|
char **QueryTestCases(void *library) {
|
||||||
|
char **(*suite)(void);
|
||||||
|
|
||||||
|
suite = (char **(*)(void)) SDL_LoadFunction(library, "queryTestNames");
|
||||||
if(suite == NULL) {
|
if(suite == NULL) {
|
||||||
printf("Retrieving test names failed, suite == NULL\n");
|
printf("Quering test names failed, suite == NULL\n");
|
||||||
printf("%s\n", SDL_GetError());
|
printf("%s\n", SDL_GetError());
|
||||||
} else {
|
}
|
||||||
const char **tests = suite();
|
|
||||||
|
|
||||||
char *testname = NULL;
|
char **tests = suite();
|
||||||
int counter = 0;
|
if(tests == NULL) {
|
||||||
for(; (testname = (char *) tests[counter]); ++counter) {
|
printf("Failed to load test cases. tests == NULL\n");
|
||||||
int childpid = fork();
|
printf("%s\n", SDL_GetError());
|
||||||
|
}
|
||||||
|
|
||||||
if(childpid == 0) {
|
return tests;
|
||||||
void (*test)(void *arg);
|
}
|
||||||
|
|
||||||
test = (void (*)(void *)) SDL_LoadFunction(library, testname);
|
int HandleTestReturnValue(int stat_lock) {
|
||||||
if(test == NULL) {
|
if(WIFEXITED(stat_lock)) {
|
||||||
printf("Loading test failed, tests == NULL\n");
|
int returnValue = WEXITSTATUS(stat_lock);
|
||||||
printf("%s\n", SDL_GetError());
|
|
||||||
} else {
|
if(returnValue == 0) {
|
||||||
test(0x0);
|
return 1;
|
||||||
}
|
}
|
||||||
return 0; // exit the child if the test didn't exit
|
} else if(WIFSIGNALED(stat_lock)) {
|
||||||
|
int signal = WTERMSIG(stat_lock);
|
||||||
|
printf("FAILURE: test was aborted due to signal nro %d\n", signal);
|
||||||
|
//errorMsg =
|
||||||
|
//errorMsg = SDL_malloc(256 * sizeof(char));
|
||||||
|
//sprintf(errorMsg, "was aborted due to signal nro %d", signal);
|
||||||
|
|
||||||
|
} else if(WIFSTOPPED(stat_lock)) {
|
||||||
|
//int signal = WSTOPSIG(stat_lock);
|
||||||
|
//printf("%d: %d was stopped by signal nro %d\n", pid, child, signal);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
// Handle command line arguments
|
||||||
|
|
||||||
|
// print: Testing againts SDL version fuu (rev: bar)
|
||||||
|
|
||||||
|
int failureCount = 0, passCount = 0;
|
||||||
|
char *testname = NULL;
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
char *libName = "libtest";
|
||||||
|
|
||||||
|
const Uint32 startTicks = SDL_GetTicks();
|
||||||
|
|
||||||
|
void *library = LoadLibrary();
|
||||||
|
char **tests = QueryTestCases(library);
|
||||||
|
|
||||||
|
for(testname = tests[counter]; testname; testname = tests[++counter]) {
|
||||||
|
printf("Running %s (in %s):\n", testname, libName);
|
||||||
|
|
||||||
|
int childpid = fork();
|
||||||
|
if(childpid == 0) {
|
||||||
|
void (*test)(void *arg);
|
||||||
|
|
||||||
|
test = (void (*)(void *)) SDL_LoadFunction(library, testname);
|
||||||
|
if(test == NULL) {
|
||||||
|
printf("Loading test failed, tests == NULL\n");
|
||||||
|
printf("%s\n", SDL_GetError());
|
||||||
} else {
|
} else {
|
||||||
int stat_lock = -1;
|
test(0x0);
|
||||||
int child = wait(&stat_lock);
|
}
|
||||||
|
return 0; // exit the child if the test didn't exit
|
||||||
|
} else {
|
||||||
|
int stat_lock = -1;
|
||||||
|
int child = wait(&stat_lock);
|
||||||
|
|
||||||
char *errorMsg = NULL;
|
int passed = -1;
|
||||||
int passed = -1;
|
|
||||||
if(WIFEXITED(stat_lock)) {
|
|
||||||
int returnValue = WEXITSTATUS(stat_lock);
|
|
||||||
|
|
||||||
if(returnValue == 0) {
|
passed = HandleTestReturnValue(stat_lock);
|
||||||
passed = 1;
|
|
||||||
} else {
|
|
||||||
passed = 0;
|
|
||||||
}
|
|
||||||
} else if(WIFSIGNALED(stat_lock)) {
|
|
||||||
int signal = WTERMSIG(stat_lock);
|
|
||||||
//printf("%d: %d was killed by signal nro %d\n", pid, child, signal);
|
|
||||||
//errorMsg =
|
|
||||||
errorMsg = SDL_malloc(256 * sizeof(char));
|
|
||||||
sprintf(errorMsg, "was aborted due to signal nro %d", signal);
|
|
||||||
|
|
||||||
passed = 0;
|
if(passed) {
|
||||||
} else if(WIFSTOPPED(stat_lock)) {
|
passCount++;
|
||||||
//int signal = WSTOPSIG(stat_lock);
|
printf("%s (in %s): ok\n", testname, libName);
|
||||||
//printf("%d: %d was stopped by signal nro %d\n", pid, child, signal);
|
} else {
|
||||||
}
|
failureCount++;
|
||||||
|
printf("%s (in %s): failed\n", testname, libName);
|
||||||
printf("%s (in %s):", testname, libName);
|
|
||||||
if(passed) {
|
|
||||||
passCount++;
|
|
||||||
printf("\tok\n");
|
|
||||||
} else {
|
|
||||||
failureCount++;
|
|
||||||
printf("\tfailed\n");
|
|
||||||
if(errorMsg) {
|
|
||||||
printf("\t%s\n", errorMsg);
|
|
||||||
SDL_free(errorMsg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_UnloadObject(library);
|
SDL_UnloadObject(library);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
lib_LTLIBRARIES = libtest.la
|
lib_LTLIBRARIES = libtest.la
|
||||||
libtest_la_SOURCES = test.c asserts.c
|
libtest_la_SOURCES = test.c sdl_test.c
|
||||||
libtest_la_CLAGS = -fPIC
|
libtest_la_CLAGS = -fPIC -g
|
||||||
libtest_la_LDFLAGS = `sdl-config --libs`
|
libtest_la_LDFLAGS = `sdl-config --libs`
|
||||||
|
|
||||||
distclean-local:
|
distclean-local:
|
||||||
|
|
|
@ -18,21 +18,36 @@
|
||||||
3. This notice may not be removed or altered from any source distribution.
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _ASSERTS_C
|
#ifndef _SDL_TEST_C
|
||||||
#define _ASSERTS_C
|
#define _SDL_TEST_C
|
||||||
|
|
||||||
#include "asserts.h"
|
#include "SDL_test.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static int _testReturnValue;
|
||||||
|
|
||||||
void
|
void
|
||||||
assertEquals(char *message, Uint32 expected, Uint32 actual)
|
TestInit()
|
||||||
|
{
|
||||||
|
_testReturnValue = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
TestQuit()
|
||||||
|
{
|
||||||
|
exit(_testReturnValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
AssertEquals(char *message, Uint32 expected, Uint32 actual)
|
||||||
{
|
{
|
||||||
if(expected != actual) {
|
if(expected != actual) {
|
||||||
printf("===============================\n");
|
printf("\n===============================\n");
|
||||||
printf("Assert failed: %s\n", message);
|
printf("Assert failed: %s\n", message);
|
||||||
printf("Expected %d, got %d\n", expected, actual);
|
printf("Expected %d, got %d\n", expected, actual);
|
||||||
printf("===============================\n");
|
printf("===============================\n");
|
||||||
|
_testReturnValue = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,11 +18,14 @@
|
||||||
3. This notice may not be removed or altered from any source distribution.
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _ASSERTS_H
|
#ifndef _SDL_TEST_H
|
||||||
#define _ASSERTS_H
|
#define _SDL_TEST_H
|
||||||
|
|
||||||
#include <SDL/SDL.h>
|
#include <SDL/SDL.h>
|
||||||
|
|
||||||
void assertEquals(char *message, Uint32 expected, Uint32 actual);
|
void TestInit();
|
||||||
|
void TestQuit();
|
||||||
|
|
||||||
|
void AssertEquals(char *message, Uint32 expected, Uint32 actual);
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -25,30 +25,42 @@
|
||||||
|
|
||||||
#include <SDL/SDL.h>
|
#include <SDL/SDL.h>
|
||||||
|
|
||||||
#include "asserts.h"
|
#include "SDL_test.h"
|
||||||
|
|
||||||
const char *names[] = {"hello", "hello2", "hello3"};
|
char *names[] = {"hello", "hello2", "hello3", NULL};
|
||||||
|
|
||||||
const char **suite() {
|
char **queryTestNames() {
|
||||||
return names;
|
return names;
|
||||||
}
|
}
|
||||||
|
|
||||||
void hello(void *arg){
|
void hello(void *arg){
|
||||||
|
TestInit();
|
||||||
|
|
||||||
const char *revision = SDL_GetRevision();
|
const char *revision = SDL_GetRevision();
|
||||||
|
|
||||||
printf("Revision is %s\n", revision);
|
printf("Revision is %s\n", revision);
|
||||||
assertEquals("will fail", 3, 5);
|
AssertEquals("will fail", 3, 5);
|
||||||
|
|
||||||
|
TestQuit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void hello2(void *arg) {
|
void hello2(void *arg) {
|
||||||
|
TestInit();
|
||||||
|
|
||||||
|
// why this isn't segfaulting?
|
||||||
char *msg = "eello";
|
char *msg = "eello";
|
||||||
msg[0] = 'H';
|
msg[0] = 'H';
|
||||||
|
|
||||||
|
TestQuit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void hello3(void *arg) {
|
void hello3(void *arg) {
|
||||||
printf("hello\n");
|
TestInit();
|
||||||
|
printf("hello3\n");
|
||||||
|
|
||||||
assertEquals("passes", 3, 3);
|
AssertEquals("passes", 3, 3);
|
||||||
|
|
||||||
|
TestQuit();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue