-
Notifications
You must be signed in to change notification settings - Fork 2
/
nanotest.h
60 lines (50 loc) · 1.61 KB
/
nanotest.h
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
/*
* NanoTest header to include in all unit test files
*
* Author: Kyle Manna <kyle[at]kylemanna[d0t]com>
*/
#pragma once
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
struct nanotest_unit;
typedef void (nanotest_func)(struct nanotest_unit* nanotest_local_ctx);
struct nanotest_fail {
unsigned int line;
const char* func;
char* file;
char* expr;
};
struct nanotest_unit {
char* suite;
char* name;
nanotest_func* func;
};
/*
* Usage: NANOTEST_DISABLED(suite, name) { ... }
*/
#define NANOTEST_DISABLED(s,n) void s##_##n(void)
/*
* Usage: NANOTEST(suite, name) { ... }
*/
#define NANOTEST(s,n) void s##_##n(struct nanotest_unit*); \
struct nanotest_unit test_##s##_##n = { \
.suite = #s, \
.name = #n, \
.func = s##_##n, \
}; \
void s##_##n(__attribute__((unused)) struct nanotest_unit* nanotest_local_ctx)
#define TEST_ASSERT(test) do { if (!(test)) { \
g_nanotest_fail.line = __LINE__; \
g_nanotest_fail.func = __func__; \
g_nanotest_fail.file = __FILE__; \
g_nanotest_fail.expr = #test; \
longjmp(g_nanotest_jmpbuf, 1); \
} } while (0)
/* Shared globals */
extern struct nanotest_fail g_nanotest_fail;
extern jmp_buf g_nanotest_jmpbuf;
#define EXPECT_TRUE(x) TEST_ASSERT( x)
#define EXPECT_FALSE(x) TEST_ASSERT(!x)
#define EXPECT_EQ(x, y) TEST_ASSERT((x) == (y))
#define EXPECT_NE(x, y) TEST_ASSERT((x) != (y))