1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
/* $Id: testsuite.inc.c,v 1.5 2005/01/21 15:13:39 pekberg Exp $
******************************************************************************
common.c - framework for c based regression tests
Copyright (C) 2004 Christoph Egger
This software is placed in the public domain and can be used freely
for any purpose. It comes without any kind of warranty, either
expressed or implied, including, but not limited to the implied
warranties of merchantability or fitness for a particular purpose.
Use it at your own risk. the author is not responsible for any damage
or consequences raised by use or inability to use this program.
******************************************************************************
*/
#include "config.h"
#include <stdio.h>
#include <stdarg.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <ggi/internal/gg_replace.h>
/* EXPECTED2PASS is used for testcases that are expected to pass.
* That is for most testcases.
*/
#define EXPECTED2PASS 0
/* EXPECTED2FAIL is used for testcases that are expcted to fail.
* That is almost useful to test error handling.
*/
#define EXPECTED2FAIL 1
/* global variables */
static int num_tests = 0;
static int num_expected_successfultests = 0;
static int num_unexpected_successfultests = 0;
static int num_expected_failedtests = 0;
static int num_unexpected_failedtests = 0;
static int num_asserterrors = 0;
static int expected_current_testresult = EXPECTED2PASS;
static int verbose = 0;
static int dontrun = 0;
static void printdesc(const char *desc)
{
if (!verbose) return;
printf("%s", desc);
fflush(stdout);
return;
}
static void printteststart(const char *file, const char *funcname,
int expected, const char *description)
{
const char *fp;
int upcount = 0;
while (strncmp("../", file, 3) == 0) {
file += 3;
++upcount;
}
while (--upcount > 0) {
fp = strchr(file, '/');
if (!fp)
break;
file = fp + 1;
}
if (verbose) {
printf("%s: %s: %s\n", file, funcname, description);
}
if (dontrun) return;
printf("%s: Running %s...", file, funcname);
fflush(stdout);
num_tests++;
expected_current_testresult = expected;
return;
}
#ifdef _MSC_VER
#define printassert()
#else
#define printassert(x, fmt...) \
if (!(x)) { \
printf(fmt); \
fflush(stdout); \
num_asserterrors++; \
}
#endif
static void printsuccess(void)
{
switch (expected_current_testresult) {
case EXPECTED2PASS:
printf("passed, as expected\n");
num_expected_successfultests++;
break;
case EXPECTED2FAIL:
printf("passed - UNEXPECTED!\n");
num_unexpected_successfultests++;
break;
}
fflush(stdout);
return;
}
static void printfailure(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
switch (expected_current_testresult) {
case EXPECTED2PASS:
printf("failed - UNEXPECTED!\n");
num_unexpected_failedtests++;
break;
case EXPECTED2FAIL:
printf("failed, as expected\n");
num_expected_failedtests++;
break;
}
vprintf(fmt, ap);
fflush(stdout);
va_end(ap);
return;
}
static void printsummary(void)
{
printf("\nSummary:\n");
printf("%2d tests run\n", num_tests);
printf("%2d tests successful, as expected\n",
num_expected_successfultests);
printf("%2d tests successful - UNEXPECTED\n",
num_unexpected_successfultests);
printf("%2d tests failed, as expected\n",
num_expected_failedtests);
printf("%2d tests failed - UNEXPECTED\n",
num_unexpected_failedtests);
printf("%2d assert failures\n", num_asserterrors);
fflush(stdout);
}
static void printhelp(const char *name)
{
printf("Usage: %s [OPTION]\n", name);
printf("Options:\n");
printf(" -h Shows this help\n");
printf(" -n Don't run testcases. Just show what would they do. Implies -v\n");
printf(" -v Print testcase description\n");
fflush(stdout);
exit(0);
}
static void parseopts(int argc, char * const argv[])
{
int ch;
while ((ch = getopt(argc, argv, "hnv")) != -1) {
switch (ch) {
case 'n':
dontrun = 1;
verbose = 1;
break;
case 'v':
verbose = 1;
break;
case 'h':
default:
printhelp(argv[0]);
}
}
argc -= optind;
argv += optind;
return;
}
|