-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtest.h
202 lines (177 loc) · 4.53 KB
/
gtest.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#ifndef GTEST_HEADER
#define GTEST_HEADER
#ifndef _GNU_SOURCE
#error "gtest requires _GNU_SOURCE"
#endif
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <err.h>
#include <sys/mman.h>
#include <sys/sendfile.h>
#include <sys/wait.h>
// #define CAT(x, y) CAT_(x, y)
// #define CAT_(x, y) x ## y
/* "✓" character */
#define PASS_STR "\033[32m\u2713 PASS\033[0m"
/* "✗" character */
#define FAIL_STR "\033[1;31m\u2717 FAIL\033[0m"
#define IGN_STR "\033[1;90mIGNORE\033[0m"
#define gassert(test) do { \
if (test) { \
} else { \
errx(1, "\t \033[1;31mAssert failed\033[0m: \"%s\"", #test);\
} \
} while (0)
#define gtest(name) \
for (int gtest__igtest = (!gtest_ignore) ? gtest__inner(name) : gtest__ignore(name); gtest__igtest == 0; exit(0))
struct gtest__failed_test {
int outfd;
int errfd;
const char *name;
};
static int gtest_ignore = 0;
static unsigned int gtest__passcnt = 0;
static unsigned int gtest__igncnt = 0;
static unsigned int gtest__failcnt = 0;
static struct gtest__failed_test *gtest__failures = NULL;
static unsigned int gtest__failures_cap = 0;
static void
gtest__handle_fail(int outfd, int errfd, const char *name)
{
if (gtest__failures_cap == gtest__failcnt) {
if (gtest__failures_cap) {
gtest__failures_cap *= 2;
} else {
gtest__failures_cap = 8;
}
gtest__failures = (struct gtest__failed_test *) realloc(gtest__failures,
gtest__failures_cap *
sizeof(struct gtest__failed_test));
}
gtest__failures[gtest__failcnt] = (struct gtest__failed_test) {
.outfd = outfd,
.errfd = errfd,
.name = name
};
printf(FAIL_STR " %s\n", name);
gtest__failcnt += 1;
}
static int
gtest__ignore(const char *name)
{
printf(IGN_STR " %s\n", name);
gtest__igncnt += 1;
gtest_ignore = 0;
return 1;
}
static int
gtest__inner(const char* name)
{
int ingtest;
int outfd = memfd_create("test_stdout", 0);
int errfd = memfd_create("test_stderr", 0);
if (outfd == -1 || errfd == -1) {
err(1, "could not create files for stdout and stderr");
}
fflush(NULL); // needed so children don't save stdout
int pid = fork();
if (pid == 0) {
/* child */
ingtest = 0;
dup2(outfd, STDOUT_FILENO);
dup2(errfd, STDERR_FILENO);
} else if (pid == -1) {
warn("fork failed");
gtest__ignore(name);
ingtest = 1;
} else {
/* parent */
ingtest = 1;
int wstatus = 0;
do {
waitpid(pid, &wstatus, 0);
} while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
if (WIFEXITED(wstatus)) {
if (WEXITSTATUS(wstatus) == 0) {
printf(PASS_STR " %s\n", name);
gtest__passcnt += 1;
close(outfd);
close(errfd);
} else {
gtest__handle_fail(outfd, errfd, name);
}
} else {
gtest__handle_fail(outfd, errfd, name);
}
}
return ingtest;
}
static int
gtest_prntres(void)
{
unsigned int i;
int has_out, has_err;
ssize_t e;
struct gtest__failed_test fail;
char buf[4096];
printf("\n%u tests were run:\n", gtest__failcnt + gtest__passcnt);
printf(PASS_STR ": %u\n", gtest__passcnt);
if (gtest__failcnt > 0) {
printf(FAIL_STR ": %u\n", gtest__failcnt);
}
if (gtest__igncnt > 0) {
printf(IGN_STR ": %u\n", gtest__igncnt);
}
i = 0;
for (; i < gtest__failcnt; i++) {
fail = gtest__failures[i];
if ((has_out = (lseek(fail.outfd, 0, SEEK_END) > 0))) {
printf("\n\033[1m---------\033[0m stdout of %s \033[1m---------\033[0m\n", fail.name);
fflush(NULL);
lseek(fail.outfd, 0, SEEK_SET);
while ((e = read(fail.outfd, buf, 4096))) {
if (e == -1) {
err(1, "reading test stdout failed");
}
if (write(STDOUT_FILENO, buf, e) != e) {
err(1, "incomplete write");
}
}
puts("");
}
if (close(fail.outfd)) {
warn("could not close test stdout fd %i", fail.outfd);
}
if ((has_err = (lseek(fail.errfd, 0, SEEK_END) > 0))) {
printf("\n\033[1m---------\033[0m stderr of %s \033[1m---------\033[0m\n", fail.name);
fflush(NULL);
lseek(fail.errfd, 0, SEEK_SET);
while ((e = read(fail.errfd, buf, 4096))) {
if (e == -1) {
err(1, "reading test stderr failed");
}
if (write(STDERR_FILENO, buf, e) != e) {
err(1, "incomplete write");
}
}
puts("");
}
if (close(fail.errfd)) {
warn("could not close test stderr fd %i", fail.errfd);
}
if (!has_out && !has_err) {
printf("\n\033[1m[NO OUTPUT]\033[0m %s\n", fail.name);
}
}
if (gtest__failcnt) {
free(gtest__failures);
}
return gtest__failcnt;
}
static int
gtest_issuccess(void)
{
return gtest__failcnt == 0 && gtest__passcnt > 0;
}
#endif