summaryrefslogtreecommitdiffstats
path: root/tests/main.c
blob: d118bd73599bca0eefe3dce22f90a7fa5d10542f (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD

/**
 * Entry point for unit tests.
 */

#include "tests.h"

#include "alloc.h"
#include "bfstd.h"
#include "color.h"
#include "list.h"

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

/** Result of the current test. */
static bool pass;

bool bfs_check_impl(bool result) {
	pass &= result;
	return result;
}

/**
 * A running test.
 */
struct test_proc {
	/** Linked list links. */
	struct test_proc *prev, *next;

	/** The PID of this test. */
	pid_t pid;
	/** The name of this test. */
	const char *name;
};

/**
 * Global test context.
 */
struct test_ctx {
	/** Number of command line arguments. */
	int argc;
	/** The arguments themselves. */
	char **argv;

	/** Maximum jobs (-j). */
	int jobs;
	/** Current jobs. */
	int running;
	/** Completed jobs. */
	int done;
	/** List of running tests. */
	struct {
		struct test_proc *head, *tail;
	} procs;

	/** Parsed colors. */
	struct colors *colors;
	/** Colorized output stream. */
	CFILE *cout;

	/** Eventual exit status. */
	int ret;
};

/** Initialize the test context. */
static int test_init(struct test_ctx *ctx, int jobs, int argc, char **argv) {
	ctx->argc = argc;
	ctx->argv = argv;

	ctx->jobs = jobs;
	ctx->running = 0;
	ctx->done = 0;
	LIST_INIT(&ctx->procs);

	ctx->colors = parse_colors();
	ctx->cout = cfwrap(stdout, ctx->colors, false);
	if (!ctx->cout) {
		ctx->ret = EXIT_FAILURE;
		return -1;
	}

	ctx->ret = EXIT_SUCCESS;
	return 0;
}

/** Check if a test case is enabled for this run. */
static bool should_run(const struct test_ctx *ctx, const char *test) {
	// Run all tests by default
	if (ctx->argc == 0) {
		return true;
	}

	// With args, run only specified tests
	for (int i = 0; i < ctx->argc; ++i) {
		if (strcmp(test, ctx->argv[i]) == 0) {
			return true;
		}
	}

	return false;
}

/** Wait for a test to finish. */
static void wait_test(struct test_ctx *ctx) {
	int wstatus;
	pid_t pid = xwaitpid(0, &wstatus, 0);
	bfs_everify(pid > 0, "xwaitpid()");

	struct test_proc *proc = NULL;
	for_list (struct test_proc, i, &ctx->procs) {
		if (i->pid == pid) {
			proc = i;
			break;
		}
	}

	bfs_verify(proc, "No test_proc for PID %ju", (intmax_t)pid);

	bool passed = false;

	if (WIFEXITED(wstatus)) {
		int status = WEXITSTATUS(wstatus);
		if (status == EXIT_SUCCESS) {
			cfprintf(ctx->cout, "${grn}[PASS]${rs} ${bld}%s${rs}\n", proc->name);
			passed = true;
		} else if (status == EXIT_FAILURE) {
			cfprintf(ctx->cout, "${red}[FAIL]${rs} ${bld}%s${rs}\n", proc->name);
		} else {
			cfprintf(ctx->cout, "${red}[FAIL]${rs} ${bld}%s${rs} (Exit %d)\n", proc->name, status);
		}
	} else {
		const char *str = NULL;
		if (WIFSIGNALED(wstatus)) {
			str = strsignal(WTERMSIG(wstatus));
		}
		if (!str) {
			str = "Unknown";
		}
		cfprintf(ctx->cout, "${red}[FAIL]${rs} ${bld}%s${rs} (%s)\n", proc->name, str);
	}

	if (!passed) {
		ctx->ret = EXIT_FAILURE;
	}

	--ctx->running;
	++ctx->done;
	LIST_REMOVE(&ctx->procs, proc);
	free(proc);
}

/** Unit test function type. */
typedef void test_fn(void);

/** Run a test if it's enabled. */
static void run_test(struct test_ctx *ctx, const char *test, test_fn *fn) {
	if (!should_run(ctx, test)) {
		return;
	}

	while (ctx->running >= ctx->jobs) {
		wait_test(ctx);
	}

	struct test_proc *proc = ALLOC(struct test_proc);
	bfs_everify(proc, "alloc()");

	LIST_ITEM_INIT(proc);
	proc->name = test;

	fflush(NULL);
	proc->pid = fork();
	bfs_everify(proc->pid >= 0, "fork()");

	if (proc->pid > 0) {
		// Parent
		++ctx->running;
		LIST_APPEND(&ctx->procs, proc);
		return;
	}

	// Child
	pass = true;
	fn();
	exit(pass ? EXIT_SUCCESS : EXIT_FAILURE);
}

/** Finalize the test context. */
static int test_fini(struct test_ctx *ctx) {
	while (ctx->running > 0) {
		wait_test(ctx);
	}

	if (ctx->cout) {
		cfclose(ctx->cout);
	}

	free_colors(ctx->colors);

	return ctx->ret;
}

int main(int argc, char *argv[]) {
	// Try to set a UTF-8 locale
	if (!setlocale(LC_ALL, "C.UTF-8")) {
		setlocale(LC_ALL, "");
	}

	// Run tests in UTC
	if (setenv("TZ", "UTC0", true) != 0) {
		perror("setenv()");
		return EXIT_FAILURE;
	}
	tzset();

	long jobs = 0;

	const char *cmd = argc > 0 ? argv[0] : "units";
	int c;
	while (c = getopt(argc, argv, ":j:"), c != -1) {
		switch (c) {
		case 'j':
			if (xstrtol(optarg, NULL, 10, &jobs) != 0 || jobs <= 0) {
				fprintf(stderr, "%s: Bad job count '%s'\n", cmd, optarg);
				return EXIT_FAILURE;
			}
			break;
		case ':':
			fprintf(stderr, "%s: Missing argument to -%c\n", cmd, optopt);
			return EXIT_FAILURE;
		case '?':
			fprintf(stderr, "%s: Unrecognized option -%c\n", cmd, optopt);
			return EXIT_FAILURE;
		}
	}

	if (jobs == 0) {
		jobs = nproc();
	}

	if (optind > argc) {
		optind = argc;
	}

	struct test_ctx ctx;
	if (test_init(&ctx, jobs, argc - optind, argv + optind) != 0) {
		goto done;
	}

	run_test(&ctx, "alloc", check_alloc);
	run_test(&ctx, "bfstd", check_bfstd);
	run_test(&ctx, "bit", check_bit);
	run_test(&ctx, "ioq", check_ioq);
	run_test(&ctx, "list", check_list);
	run_test(&ctx, "sighook", check_sighook);
	run_test(&ctx, "trie", check_trie);
	run_test(&ctx, "xspawn", check_xspawn);
	run_test(&ctx, "xtime", check_xtime);

done:
	return test_fini(&ctx);
}