Commit fd5c9e1c authored by Chris Müller's avatar Chris Müller

add commandline interface for all unittests; you can now set the following...

add commandline interface for all unittests; you can now set the following parameters: colors, repeat, filter
parent e8d8420c
......@@ -9,6 +9,7 @@ set(SOURCES
set(HEADER
standard.h
unix_colors.h
matrix.h
unittest.h
)
......
#include "unittest.h"
#include <unistd.h>
#include "unix_colors.h"
#include <sys/signal.h>
#include <sys/wait.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
static int monitor_testprocess(pid_t worker_pid, unsigned int timeout);
static void print_testprocess_status(const char* desc, int return_code, suseconds_t time);
static void print_usage(FILE* stream, const char* programm, int exit_code);
#define COLOR(condition, color, string) (condition ? color string CRY_NORMAL : string)
// Current Errorfile
const char* ErrorFile = 0;
// Options
static int Colors = 0;
static int Repeat = 1;
static const char* Filter = 0;
static struct option option[] = {
{"help", no_argument, 0, 'h'},
{"colors", no_argument, 0, 'c'},
{"repeat", optional_argument, 0, 'r'},
{"filter", optional_argument, 0, 'f'}
};
// Statistics
static int TestCaseNumber = 0;
static int SegFaultNumber = 0;
static int TimeoutNumber = 0;
static int FailureNumber = 0;
void
cry_unittest_initialize(int argc, char** argv)
{
int ch = 0;
while( (ch = getopt_long(argc, argv, "hcr:f:", option, 0)) != -1 ) {
switch(ch) {
case 'h':
print_usage(stdout, argv[0], EXIT_SUCCESS);
case 'c':
Colors = 1;
break;
case 'r':
Repeat = atoi(optarg);
break;
case 'f':
Filter = optarg;
break;
case '?':
print_usage(stderr, argv[0], EXIT_FAILURE);
default:
abort();
}
}
printf(":: colors %s, repeat %d, filter %s ::\n", Colors ? "ON" : "OFF", Repeat, Filter ? Filter : "OFF");
}
void
print_usage(FILE* stream, const char* programm, int exit_code)
{
fprintf(stream, "Usage: %s options\n", programm);
fprintf(stream,
" -h --help Display this usage information for your testcase.\n"
" -c --colors Enable color support for test output.\n"
" -r --repeat INT Set number of repeats for each testcase.\n"
" -f --filter REGEX Set a regular expression filter for all testcases.\n");
exit(exit_code);
}
void
cry_unittest_run(const char* desc, cry_modulechecker handler, unsigned int timeout)
cry_unittest_run(const char* desc, cry_modulechecker handler, const_pointer data, unsigned int timeout)
{
pid_t process_pid = fork();
......@@ -36,7 +103,7 @@ cry_unittest_run(const char* desc, cry_modulechecker handler, unsigned int timeo
} else {
// Process for running test case
handler();
handler(data);
exit(EXIT_SUCCESS);
}
}
......@@ -72,20 +139,20 @@ monitor_testprocess(pid_t worker_pid, unsigned int timeout)
void
print_testprocess_status(const char* desc, int return_code, suseconds_t time)
{
const char buffer[128];
char buffer[128];
snprintf(buffer, 128, "%s (%.3lf ms)", desc, time / 1000.0);
printf("%-50s ", buffer);
if(return_code == SIGINT)
printf("TIMEOUT");
printf(COLOR(Colors, CRY_RED, "TIMEOUT"));
else if(WIFEXITED(return_code))
printf("OK");
printf(COLOR(Colors, CRY_GREEN, "OK"));
else if(WCOREDUMP(return_code))
printf("SEGFAULT");
printf(COLOR(Colors, CRY_RED, "SEGFAULT"));
else
printf("FAIL (%d)", return_code);
printf(COLOR(Colors, CRY_RED, "FAIL (%d)"), return_code);
printf("\n");
}
......
#ifndef CRYSTAL_UNITTEST_H
#define CRYSTAL_UNITTEST_H
#include <sys/types.h>
#include <sys/time.h>
#include <assert.h>
#include "standard.h"
typedef void (*cry_modulechecker)(void);
typedef void (*cry_modulechecker)(const_pointer data);
void cry_unittest_run(const char* desc, cry_modulechecker handler, unsigned int timeout);
void cry_unittest_run(const char* desc, cry_modulechecker handler, const_pointer data, unsigned int timeout);
void cry_unittest_initialize(int argc, char** argv);
#endif // CRYSTAL_UNITTEST_H
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