2021-12-21 12:39:17 +01:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
Copyright (c) 2018-2020, Microsoft Research, Daan Leijen
|
|
|
|
This is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the MIT license. A copy of the license can be found in the file
|
|
|
|
"LICENSE" at the root of this distribution.
|
|
|
|
-----------------------------------------------------------------------------*/
|
|
|
|
#ifndef TESTHELPER_H_
|
|
|
|
#define TESTHELPER_H_
|
|
|
|
|
2022-04-19 10:49:39 -07:00
|
|
|
#include <stdbool.h>
|
2021-12-21 12:39:17 +01:00
|
|
|
#include <stdio.h>
|
2022-04-19 10:49:39 -07:00
|
|
|
#include <errno.h>
|
2021-12-21 12:39:17 +01:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Test macros: CHECK(name,predicate) and CHECK_BODY(name,body)
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
static int ok = 0;
|
|
|
|
static int failed = 0;
|
|
|
|
|
2022-04-19 10:10:10 -07:00
|
|
|
static bool check_result(bool result, const char* testname, const char* fname, long lineno) {
|
|
|
|
if (!(result)) {
|
2022-12-03 00:23:43 +01:00
|
|
|
failed++;
|
2022-04-19 10:49:39 -07:00
|
|
|
fprintf(stderr,"\n FAILED: %s: %s:%ld\n", testname, fname, lineno);
|
2022-12-03 00:23:43 +01:00
|
|
|
/* exit(1); */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ok++;
|
2022-04-19 10:10:10 -07:00
|
|
|
fprintf(stderr, "ok.\n");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-19 10:21:41 -07:00
|
|
|
#define CHECK_BODY(name) \
|
2022-04-19 10:10:10 -07:00
|
|
|
fprintf(stderr,"test: %s... ", name ); \
|
2022-04-19 10:49:39 -07:00
|
|
|
errno = 0; \
|
2022-04-19 10:10:10 -07:00
|
|
|
for(bool done = false, result = true; !done; done = check_result(result,name,__FILE__,__LINE__))
|
|
|
|
|
2022-04-19 10:21:41 -07:00
|
|
|
#define CHECK(name,expr) CHECK_BODY(name){ result = (expr); }
|
2021-12-21 12:39:17 +01:00
|
|
|
|
|
|
|
// Print summary of test. Return value can be directly use as a return value for main().
|
|
|
|
static inline int print_test_summary(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"\n\n---------------------------------------------\n"
|
|
|
|
"succeeded: %i\n"
|
|
|
|
"failed : %i\n\n", ok, failed);
|
|
|
|
return failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TESTHELPER_H_
|