Reintegrated what I needed in test.h. So much for "overengineered".
This commit is contained in:
parent
2ccabe69f2
commit
4f2db68520
24
test/main.c
24
test/main.c
|
@ -36,6 +36,30 @@ static int testcmp(const void *aa, const void *bb)
|
|||
return strcmp(a->name, b->name);
|
||||
}
|
||||
|
||||
static const char *basename(const char *file)
|
||||
{
|
||||
const char *p;
|
||||
|
||||
for (;;) {
|
||||
p = strpbrk(file, "/\\");
|
||||
if (p == NULL)
|
||||
break;
|
||||
file = p + 1;
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
void testingprivLogf(FILE *f, const char *filename, long line, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
fprintf(f, "%s:%ld: ", basename(filename), line);
|
||||
vfprintf(f, fmt, ap);
|
||||
fprintf(f, "\n");
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct test *t;
|
||||
|
|
35
test/test.h
35
test/test.h
|
@ -17,6 +17,10 @@ extern "C" {
|
|||
|
||||
#define testingprivImplName(basename) testingprivImpl ## basename
|
||||
|
||||
#define testingprivScaffoldName(basename) testingprivScaffold ## basename
|
||||
#define testingprivMkScaffold(basename) \
|
||||
static int testingprivScaffoldName(basename)(void) { int ret = 0; testingprivImplName(basename)(&ret); return ret; }
|
||||
|
||||
// references:
|
||||
// - https://gitlab.gnome.org/GNOME/glib/blob/master/glib/gconstructor.h
|
||||
// - https://gitlab.gnome.org/GNOME/glib/blob/master/gio/glib-compile-resources.c
|
||||
|
@ -25,35 +29,52 @@ extern "C" {
|
|||
#define testingprivCtorPtrName(basename) testingprivCtorPtr ## basename
|
||||
#if defined(__GNUC__)
|
||||
#define testingprivMkCtor(basename) \
|
||||
__attribute__((constructor)) static void testingprivCtorName(basename)(void) { testingprivRegisterTest(#basename, testingprivImplName(basename)); }
|
||||
__attribute__((constructor)) static void testingprivCtorName(basename)(void) { testingprivRegisterTest(#basename, testingprivScaffoldName(basename)); }
|
||||
#elif defined(_MSC_VER)
|
||||
#define testingprivMkCtor(basename) \
|
||||
static int testingprivCtorName(basename)(void) { testingprivRegisterTest(#basename, testingprivImplName(basename)); return 0; } \
|
||||
static int testingprivCtorName(basename)(void) { testingprivRegisterTest(#basename, testingprivScaffoldName(basename)); return 0; } \
|
||||
__pragma(section(".CRT$XCU",read)) \
|
||||
__declspec(allocate(".CRT$XCU")) static int (*testingprivCtorPtrName(basename))(void) = testingprivCtorName(basename);
|
||||
#elif defined(__SUNPRO_C)
|
||||
#define testingprivMkCtor(basename) \
|
||||
static void testingprivCtorName(basename)(void) { testingprivRegisterTest(#basename, testingprivImplName(basename)); } \
|
||||
static void testingprivCtorName(basename)(void) { testingprivRegisterTest(#basename, testingprivScaffoldName(basename)); } \
|
||||
_Pragma(init(testingprivCtorName(basename)))
|
||||
#else
|
||||
#error unknown compiler for making constructors in C; cannot continue
|
||||
#endif
|
||||
|
||||
#define Test(basename) \
|
||||
int testingprivImplName(basename)(void); \
|
||||
void testingprivImplName(basename)(int *testingprivRet); \
|
||||
testingprivMkScaffold(basename) \
|
||||
testingprivMkCtor(basename) \
|
||||
int testingprivImplName(basename)(void)
|
||||
void testingprivImplName(basename)(int *testingprivRet)
|
||||
|
||||
#define Test(Name) \
|
||||
testingprivMk(Test ## Name)
|
||||
|
||||
// These can only be called directly from the Test functions.
|
||||
// TODO make it otherwise
|
||||
#define TestFailNow() return 1
|
||||
#define TestFail() (*testingprivRet = 1)
|
||||
#define TestFailNow() {TestFail(); return;}
|
||||
// see https://mesonbuild.com/Unit-tests.html#skipped-tests-and-hard-errors
|
||||
#define TestSkipNow() return 77
|
||||
#define TestSkipNow() {*testingprivRet = 77; return;}
|
||||
|
||||
#define TestLogf(...) \
|
||||
(testingprivLogf(stdout, __FILE__, __LINE__, __VA_ARGS__))
|
||||
#define TestErrorf(...) \
|
||||
{testingprivLogf(stderr, __FILE__, __LINE__, __VA_ARGS__); TestFail();}
|
||||
#define TestFatalf(...) \
|
||||
{testingprivLogf(stderr, __FILE__, __LINE__, __VA_ARGS__); TestFailNow();}
|
||||
// TODO remember if this needs to go to stdout or to stderr
|
||||
#define TestSkipf(...) \
|
||||
{testingprivLogf(stderr, __FILE__, __LINE__, __VA_ARGS__); TestSkipNow();}
|
||||
|
||||
extern void testingprivRegisterTest(const char *name, int (*f)(void));
|
||||
#include "../sharedbits/printfwarn_header.h"
|
||||
sharedbitsPrintfFunc(
|
||||
extern void testingprivLogf(FILE *f, const char *filename, long line, const char *fmt, ...),
|
||||
4, 5);
|
||||
#undef sharedbitsPrintfFunc
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue