-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.h
140 lines (118 loc) · 3.95 KB
/
test.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Frictionless test framework
*
* Copyright (c) 2020 Saul Pwanson <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
* To use, put tests inside .c files like this:
*
* #ifdef TEST
* TEST(testname) {
* CHECK(whatever());
* CHECK_EQ(int, 2+2, 5); // pretty-printing on fail
* }
* #endif
*
* Then call runtests() from main() (a weak main() is included here also).
*
* Output is to stderr by default; define TESTLOG(FMT, ARGS...) for different behaviour.
*/
#ifndef TEST
#include <stdbool.h> // bool
#include <stdio.h> // fprintf
#include <stdlib.h> // abort
#ifndef TESTLOG // predefine TESTLOG for different behaviour
#define TESTLOG(FMT, ARGS...) fprintf(stderr, FMT "\n", ##ARGS)
#endif
typedef struct test_state_t {
int nfail;
int npass;
int ntests;
} test_state_t;
typedef struct testdesc_t {
const char *name;
void (*func)(test_state_t *);
} test_desc_t;
#define TEST(TESTNAME) \
extern void test_##TESTNAME(test_state_t *); \
test_desc_t desc_##TESTNAME \
__attribute__((__section__("tests"))) \
__attribute__((__used__)) \
= { #TESTNAME, test_##TESTNAME }; \
void test_##TESTNAME(test_state_t *__test)
extern test_desc_t __stop_tests;
extern test_desc_t __start_tests;
static void runtests(void) {
test_state_t t = {0};
for (test_desc_t *desc = &__start_tests; desc < &__stop_tests; ++desc) {
TESTLOG("[%s]", desc->name);
(*desc->func)(&t);
}
if (t.nfail > 0) {
TESTLOG("%d/%d checks passed (%d FAILED)", t.npass, t.ntests, t.nfail);
abort();
}
if (t.nfail == 0 && t.npass == t.ntests) {
TESTLOG("ALL %d checks PASS", t.ntests);
}
}
void main(void) { runtests(); }
#define CHECK(EXPR) ({ \
__test->ntests += 1; \
bool passed = (EXPR); \
if (passed) { \
__test->npass += 1; \
} else { \
TESTLOG("FAILED: " #EXPR); \
__test->nfail += 1; \
} \
passed; \
})
//
// fancier checking with typed output on failure
//
typedef void *ptr;
#define FMT_int "%d"
#define FMT_uint "%u"
#define FMT_float "%f"
#define FMT_ptr "%p"
#define FMT_int8_t FMT_int
#define FMT_int16_t FMT_int
#define FMT_int32_t FMT_int
#define FMT_int64_t "%ld"
#define FMT_uint8_t FMT_uint
#define FMT_uint16_t FMT_uint
#define FMT_uint32_t FMT_uint
#define CHECK_CMP(TYPE, OP, A, B) ({ \
typeof(A) _a = (A); \
typeof(B) _b = (B); \
bool passed2 = CHECK(_a OP _b); \
if (!passed2) { \
TESTLOG(#A " " #OP " " #B " (" FMT_##TYPE " " #OP " " FMT_##TYPE " is false)", (TYPE) _a, (TYPE) _b); \
} \
passed2; \
})
#define CHECK_EQ(T, A, B) CHECK_CMP(T, ==, A, B)
#define CHECK_NEQ(T, A, B) CHECK_CMP(T, !=, A, B)
#define CHECK_GTEQ(T, A, B) CHECK_CMP(T, >=, A, B)
TEST(unittest) {
CHECK_EQ(int, 2+2, 4);
CHECK_NEQ(int, 2+2, 5);
}
#endif