1/* ----------------------------------------------------------------------------
2Copyright (c) 2018-2020, Microsoft Research, Daan Leijen
3This is free software; you can redistribute it and/or modify it under the
4terms of the MIT license. A copy of the license can be found in the file
5"LICENSE" at the root of this distribution.
6-----------------------------------------------------------------------------*/
7#ifndef TESTHELPER_H_
8#define TESTHELPER_H_
9
10#include <stdbool.h>
11#include <stdio.h>
12#include <errno.h>
13
14// ---------------------------------------------------------------------------
15// Test macros: CHECK(name,predicate) and CHECK_BODY(name,body)
16// ---------------------------------------------------------------------------
17static int ok = 0;
18static int failed = 0;
19
20static bool check_result(bool result, const char* testname, const char* fname, long lineno) {
21 if (!(result)) {
22 failed++;
23 fprintf(stderr,"\n FAILED: %s: %s:%ld\n", testname, fname, lineno);
24 /* exit(1); */
25 }
26 else {
27 ok++;
28 fprintf(stderr, "ok.\n");
29 }
30 return true;
31}
32
33#define CHECK_BODY(name) \
34 fprintf(stderr,"test: %s... ", name ); \
35 errno = 0; \
36 for(bool done = false, result = true; !done; done = check_result(result,name,__FILE__,__LINE__))
37
38#define CHECK(name,expr) CHECK_BODY(name){ result = (expr); }
39
40// Print summary of test. Return value can be directly use as a return value for main().
41static inline int print_test_summary(void)
42{
43 fprintf(stderr,"\n\n---------------------------------------------\n"
44 "succeeded: %i\n"
45 "failed : %i\n\n", ok, failed);
46 return failed;
47}
48
49#endif // TESTHELPER_H_
50