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
|
// test.h - Minimal test assertion macros
//
// Not a library for distribution. Intended for the test suite only.
// Provides ASSERT, ASSERT_FLOAT_EQ, and ASSERT_STR_EQ macros with
// failure tracking via a global test_failures counter.
#ifndef TEST_H
#define TEST_H
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "type.h"
#ifndef TEST_EPS
#define TEST_EPS 1e-9
#endif
#ifndef TEST_FAILURES_VAR
#define TEST_FAILURES_VAR int test_failures = 0
#endif
#define ASSERT(cond, msg) \
do { \
if (!(cond)) { \
fprintf(stderr, "FAIL: %s (%s:%d)\n", msg, __FILE__, __LINE__); \
test_failures++; \
} \
} while (0)
#define ASSERT_FLOAT_EQ(a, b, msg) \
do { \
f64 _a = (a); \
f64 _b = (b); \
if (fabs(_a - _b) > TEST_EPS) { \
fprintf(stderr, "FAIL: %s (got %f, expected %f) (%s:%d)\n", \
msg, (double)_a, (double)_b, __FILE__, __LINE__); \
test_failures++; \
} \
} while (0)
#define ASSERT_STR_EQ(a, b, msg) \
do { \
char const *_a = (a); \
char const *_b = (b); \
if (_a == NULL || _b == NULL || strcmp(_a, _b) != 0) { \
fprintf(stderr, "FAIL: %s (got '%s', expected '%s') (%s:%d)\n", \
msg, _a ? _a : "NULL", _b ? _b : "NULL", __FILE__, __LINE__); \
test_failures++; \
} \
} while (0)
#define TEST_PRINT_RESULTS(name) \
printf("%s: %d failures\n", name, test_failures)
#endif /* TEST_H */
|