-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjp_alloc_test.h
More file actions
63 lines (55 loc) · 1.52 KB
/
Copy pathjp_alloc_test.h
File metadata and controls
63 lines (55 loc) · 1.52 KB
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
61
62
63
/* jp_alloc_test.h - minimal C test framework for jp_alloc
*
* No external dependencies. Provides TEST(), ASSERT_*, RUN_TEST(), DONE().
* Designed to be included by jp_alloc_test.c which is compiled with
* -DJP_ALLOC_TEST (providing main()) and links jp_alloc.c in the same
* translation unit (so static internals are visible for white-box tests).
*
* Usage:
* TEST(test_foo) {
* ASSERT_TRUE(1 == 1);
* }
* int main(void) {
* RUN_TEST(test_foo);
* DONE();
* }
*/
#ifndef JP_ALLOC_TEST_H
#define JP_ALLOC_TEST_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int g_test_count = 0;
static int g_test_fail = 0;
#define TEST(name) \
static void name(void); \
static void name(void)
#define ASSERT_TRUE(cond) do { \
if(!(cond)) { \
fprintf(stderr, " FAIL: %s:%d: %s\n", __FILE__, __LINE__, #cond); \
g_test_fail++; \
return; \
} \
} while(0)
#define ASSERT_EQ(a, b) ASSERT_TRUE((a) == (b))
#define ASSERT_GE(a, b) ASSERT_TRUE((a) >= (b))
#define ASSERT_LE(a, b) ASSERT_TRUE((a) <= (b))
#define ASSERT_NE(a, b) ASSERT_TRUE((a) != (b))
#define RUN_TEST(name) do { \
g_test_count++; \
name(); \
if(g_test_fail) { \
fprintf(stderr, "\nTest %d FAILED, stopping.\n", g_test_count); \
return 1; \
} \
printf(" PASS: %s\n", #name); \
} while(0)
#define DONE() do { \
if(g_test_fail) { \
fprintf(stderr, "\n%d/%d tests FAILED\n", g_test_fail, g_test_count); \
return 1; \
} \
printf("\nAll %d tests passed\n", g_test_count); \
return 0; \
} while(0)
#endif /* JP_ALLOC_TEST_H */