aboutsummaryrefslogtreecommitdiff
path: root/test/test.h
blob: cdd2f053c2c808b2db19436c375b9ca6ed6ae865 (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
// SPDX-License-Identifier: MIT
#pragma once

#ifdef UNIT_TEST

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) ||     \
    defined(__NetBSD__) || defined(__OpenBSD__)
#define USE_SYSCTL_FOR_ARGS 1
// clang-format off
#include <sys/types.h>
#include <sys/sysctl.h>
// clang-format on
#include <unistd.h>        // getpid
#endif

struct test_file_metadata;

struct test_failure {
	bool present;
	const char *message;
	const char *file;
	int line;
	bool owned;
};

struct test_case_metadata {
	void (*fn)(struct test_case_metadata *, struct test_file_metadata *);
	struct test_failure failure;
	const char *name;
	struct test_case_metadata *next;
};

struct test_file_metadata {
	bool registered;
	const char *name;
	struct test_file_metadata *next;
	struct test_case_metadata *tests;
};

struct test_file_metadata __attribute__((weak)) * test_file_head;

#define SET_FAILURE(_message, _owned)                                                    \
	metadata->failure = (struct test_failure) {                                      \
		.present = true,                                                         \
		.message = _message,                                                     \
		.file    = __FILE__,                                                     \
		.line    = __LINE__,                                                     \
		.owned   = _owned,                                                       \
	}

#define TEST_EQUAL(a, b)                                                                 \
	do {                                                                             \
		if ((a) != (b)) {                                                        \
			SET_FAILURE(#a " != " #b, false);                                \
			return;                                                          \
		}                                                                        \
	} while (0)

#define TEST_NOTEQUAL(a, b)                                                              \
	do {                                                                             \
		if ((a) == (b)) {                                                        \
			SET_FAILURE(#a " == " #b, false);                                \
			return;                                                          \
		}                                                                        \
	} while (0)

#define TEST_TRUE(a)                                                                     \
	do {                                                                             \
		if (!(a)) {                                                              \
			SET_FAILURE(#a " is not true", false);                           \
			return;                                                          \
		}                                                                        \
	} while (0)

#define TEST_STREQUAL(a, b)                                                              \
	do {                                                                             \
		if (strcmp(a, b) != 0) {                                                 \
			const char *test_strequal__part2 = " != " #b;                    \
			size_t test_strequal__len =                                      \
			    strlen(a) + strlen(test_strequal__part2) + 3;                \
			char *test_strequal__buf = malloc(test_strequal__len);           \
			snprintf(test_strequal__buf, test_strequal__len, "\"%s\"%s", a,  \
			         test_strequal__part2);                                  \
			SET_FAILURE(test_strequal__buf, true);                           \
			return;                                                          \
		}                                                                        \
	} while (0)

#define TEST_STRNEQUAL(a, b, len)                                                        \
	do {                                                                             \
		if (strncmp(a, b, len) != 0) {                                           \
			const char *test_strnequal__part2 = " != " #b;                   \
			size_t test_strnequal__len2 =                                    \
			    len + strlen(test_strnequal__part2) + 3;                     \
			char *test_strnequal__buf = malloc(test_strnequal__len2);        \
			snprintf(test_strnequal__buf, test_strnequal__len2,              \
			         "\"%.*s\"%s", (int)len, a, test_strnequal__part2);      \
			SET_FAILURE(test_strnequal__buf, true);                          \
			return;                                                          \
		}                                                                        \
	} while (0)

#define TEST_STREQUAL3(str, expected, len)                                               \
	do {                                                                             \
		if (len != strlen(expected) || strncmp(str, expected, len) != 0) {       \
			const char *test_strequal3__part2 = " != " #expected;            \
			size_t test_strequal3__len2 =                                    \
			    len + strlen(test_strequal3__part2) + 3;                     \
			char *test_strequal3__buf = malloc(test_strequal3__len2);        \
			snprintf(test_strequal3__buf, test_strequal3__len2,              \
			         "\"%.*s\"%s", (int)len, str, test_strequal3__part2);    \
			SET_FAILURE(test_strequal3__buf, true);                          \
			return;                                                          \
		}                                                                        \
	} while (0)

#define TEST_CASE(_name)                                                                  \
	static void __test_h_##_name(struct test_case_metadata *,                         \
	                             struct test_file_metadata *);                        \
	static struct test_file_metadata __test_h_file##_name;                            \
	static struct test_case_metadata __test_h_meta_##_name = {                        \
	    .fn = __test_h_##_name,                                                       \
	    .failure = {},                                                                \
	    .name = #_name,                                                               \
	    .next = 0,                                                                    \
	};                                                                                \
	static void __attribute__((constructor(101))) __test_h_##_name##_register(void) { \
		__test_h_meta_##_name.next = __test_h_file##_name.tests;                  \
		__test_h_file##_name.tests = &__test_h_meta_##_name;                      \
		if (!__test_h_file##_name.registered) {                                   \
			__test_h_file##_name.name = __FILE__;                             \
			__test_h_file##_name.next = test_file_head;                       \
			test_file_head = &__test_h_file##_name;                           \
			__test_h_file##_name.registered = true;                           \
		}                                                                         \
	}                                                                                 \
	static void __test_h_##_name(                                                     \
	    struct test_case_metadata *metadata __attribute__((unused)),                  \
	    struct test_file_metadata *file_metadata __attribute__((unused)))

extern void __attribute__((weak)) (*test_h_unittest_setup)(void);
/// Run defined tests, return true if all tests succeeds
/// @param[out] tests_run if not NULL, set to whether tests were run
static inline void __attribute__((constructor(102))) run_tests(void) {
	bool should_run = false;
#ifdef USE_SYSCTL_FOR_ARGS
	int mib[] = {
		CTL_KERN,
#if defined(__NetBSD__) || defined(__OpenBSD__)
		KERN_PROC_ARGS,
		getpid(),
		KERN_PROC_ARGV,
#else
		KERN_PROC,
		KERN_PROC_ARGS,
		getpid(),
#endif
	};
	char *arg = NULL;
	size_t arglen;
	sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &arglen, NULL, 0);
	arg = malloc(arglen);
	sysctl(mib, sizeof(mib) / sizeof(mib[0]), arg, &arglen, NULL, 0);
#else
	FILE *cmdlinef = fopen("/proc/self/cmdline", "r");
	char *arg = NULL;
	int arglen;
	fscanf(cmdlinef, "%ms%n", &arg, &arglen);
	fclose(cmdlinef);
#endif
	for (char *pos = arg; pos < arg + arglen; pos += strlen(pos) + 1) {
		if (strcmp(pos, "--unittest") == 0) {
			should_run = true;
			break;
		}
	}
	free(arg);

	if (!should_run) {
		return;
	}

	if (&test_h_unittest_setup) {
		test_h_unittest_setup();
	}

	struct test_file_metadata *i = test_file_head;
	int failed = 0, success = 0;
	while (i) {
		fprintf(stderr, "Running tests from %s:\n", i->name);
		struct test_case_metadata *j = i->tests;
		while (j) {
			fprintf(stderr, "\t%s ... ", j->name);
			j->failure.present = false;
			j->fn(j, i);
			if (j->failure.present) {
				fprintf(stderr, "failed (%s at %s:%d)\n", j->failure.message,
				        j->failure.file, j->failure.line);
				if (j->failure.owned) {
					free((char *)j->failure.message);
					j->failure.message = NULL;
				}
				failed++;
			} else {
				fprintf(stderr, "passed\n");
				success++;
			}
			j = j->next;
		}
		fprintf(stderr, "\n");
		i = i->next;
	}
	int total = failed + success;
	fprintf(stderr, "Test results: passed %d/%d, failed %d/%d\n", success, total,
	        failed, total);
	exit(failed == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}

#else

#include <stdbool.h>

#define TEST_CASE(name) static void __attribute__((unused)) __test_h_##name(void)

#define TEST_EQUAL(a, b)                                                                 \
	(void)(a);                                                                       \
	(void)(b)
#define TEST_NOTEQUAL(a, b)                                                                 \
	(void)(a);                                                                       \
	(void)(b)
#define TEST_TRUE(a) (void)(a)
#define TEST_STREQUAL(a, b)                                                              \
	(void)(a);                                                                       \
	(void)(b)
#define TEST_STRNEQUAL(a, b, len)                                                        \
	(void)(a);                                                                       \
	(void)(b);                                                                       \
	(void)(len)
#define TEST_STREQUAL3(str, expected, len)                                               \
	(void)(str);                                                                     \
	(void)(expected);                                                                \
	(void)(len)
#endif