aboutsummaryrefslogtreecommitdiffstats
path: root/testhelp.c
blob: 74997ebfe956a79999f695b7126bc18bb9e42e74 (plain) (blame)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* See LICENSE file for copyright and license details. */
#define TEST
#include "common.h"


#if defined(__GNUC__)
# pragma GCC diagnostic ignored "-Wswitch"
# pragma GCC diagnostic ignored "-Wswitch-enum"
#endif


static char *
quote(const char *s, char **out)
{
	char *ret, *p;
	int safe = 1;
	*out = p = ret = malloc(strlen(s) * 4 + 3);
	if (!ret) {
		fprintf(stderr, "ran out of memory during printing of assertion failure\n");
		exit(1);
	}
	*p++ = '"';
	for (; *s; s++) {
		if (*s == '\r')
			p = stpcpy(p, "\\r"), safe = 1;
		else if (*s == '\t')
			p = stpcpy(p, "\\t"), safe = 1;
		else if (*s == '\a')
			p = stpcpy(p, "\\a"), safe = 1;
		else if (*s == '\f')
			p = stpcpy(p, "\\f"), safe = 1;
		else if (*s == '\v')
			p = stpcpy(p, "\\v"), safe = 1;
		else if (*s == '\b')
			p = stpcpy(p, "\\b"), safe = 1;
		else if (*s == '\n')
			p = stpcpy(p, "\\n"), safe = 1;
		else if (*s == '\"')
			p = stpcpy(p, "\\\""), safe = 1;
		else if (*s == '\\')
			p = stpcpy(p, "\\\\"), safe = 1;
		else if (*s < ' ' || *s >= 127)
			p += sprintf(p, "\\x%02X", (unsigned char)*s), safe = 0;
		else if (*s == '?' && p[-1] == '?')
			p += sprintf(p, "\"\"?"), safe = 1;
		else if (isxdigit(*s) && !safe)
			p += sprintf(p, "\"\"%c", *s), safe = 1;
		else
			*p++ = *s, safe = 1;
	}
	*p++ = '"';
	*p++ = '\0';
	return ret;
}


#define CASE(HOW, OP)\
	case HOW:\
		if ((*have) OP (*expect))\
			return;\
		symbol = #OP;\
		break


#define CASE_LOGIC(HOW, OP1, OP2, OPERAND3_IS_EXPECT)\
	case HOW:\
		if (((*have) OP1 (*expect)) OP2 (OPERAND3_IS_EXPECT ? (*expect) : 0))\
			return;\
		symbol = #OP1;\
		symbol2 = #OP2;\
		operand3_is_expect = (OPERAND3_IS_EXPECT);\
		break


static void
test_assert_uint(const char *file, int line, enum assert_how how, const char *have_string,
                 const char *expect_string, const uintmax_t *have, const uintmax_t *expect)
{
	const char *symbol;
	const char *symbol2 = NULL;
	int operand3_is_expect;
	switch (how) {
	case ASSERT_NOT_LT: how = ASSERT_NOT_GE; break;
	case ASSERT_NOT_LE: how = ASSERT_NOT_GT; break;
	case ASSERT_NOT_GT: how = ASSERT_NOT_LE; break;
	case ASSERT_NOT_GE: how = ASSERT_NOT_LT; break;
	}
	switch (how) {
		CASE(ASSERT_EQ, ==);
		CASE(ASSERT_NE, !=);
		CASE(ASSERT_LT, <);
		CASE(ASSERT_LE, <=);
		CASE(ASSERT_GT, >);
		CASE(ASSERT_GE, >=);
		CASE_LOGIC(ASSERT_CONTAINS_ALL, &, ==, 1);
		CASE_LOGIC(ASSERT_CONTAINS_ANY, &, !=, 0);
		CASE_LOGIC(ASSERT_CONTAINS_NOT_ALL, &, !=, 1);
		CASE_LOGIC(ASSERT_CONTAINS_NOT_ANY, &, ==, 0);
		CASE_LOGIC(ASSERT_IS_IN, &~, ==, 0);
		CASE_LOGIC(ASSERT_NOT_IN, &~, !=, 0);
	default:
		abort();
	}
	if (!symbol2)
		fprintf(stderr, "TEST FAILED at %s:%i: asserting (%s) %s (%s), actualised as %ju %s %ju\n",
		        file, line, have_string, symbol, expect_string, *have, symbol, *expect);
	else if (!operand3_is_expect)
		fprintf(stderr, "TEST FAILED at %s:%i: asserting ((%s) %s (%s)) %s 0, actualised as (%#jx %s %#jx) %s 0\n",
		        file, line, have_string, symbol, expect_string, symbol2, *have, symbol, *expect, symbol2);
	else
		fprintf(stderr, "TEST FAILED at %s:%i: asserting ((%s) %s (%s)) %s (%s), actualised as (%#jx %s %#jx) %s %#jx\n",
		        file, line, have_string, symbol, expect_string, symbol2, expect_string,
		        *have, symbol, *expect, symbol2, *expect);
	exit(1);
}


static void
test_assert_int(const char *file, int line, enum assert_how how, const char *have_string,
                const char *expect_string, const intmax_t *have, const intmax_t *expect)
{
	const char *symbol;
	switch (how) {
	case ASSERT_NOT_LT: how = ASSERT_NOT_GE; break;
	case ASSERT_NOT_LE: how = ASSERT_NOT_GT; break;
	case ASSERT_NOT_GT: how = ASSERT_NOT_LE; break;
	case ASSERT_NOT_GE: how = ASSERT_NOT_LT; break;
	}
	switch (how) {
		CASE(ASSERT_EQ, ==);
		CASE(ASSERT_NE, !=);
		CASE(ASSERT_LT, <);
		CASE(ASSERT_LE, <=);
		CASE(ASSERT_GT, >);
		CASE(ASSERT_GE, >=);
	default:
		test_assert_uint(file, line, how, have_string, expect_string,
		                 (const uintmax_t *)have, (const uintmax_t *)expect);
		return;
	}
	fprintf(stderr, "TEST FAILED at %s:%i: asserting (%s) %s (%s), actualised as %ji %s %ji\n",
	        file, line, have_string, symbol, expect_string, *have, symbol, *expect);
	exit(1);
}


#define PTR_CASE(HOW, OP)\
	case HOW:\
		if ((have) OP (expect))\
			return;\
		symbol = #OP;\
		break


static void
test_assert_ptr(const char *file, int line, enum assert_how how, const char *have_string,
                const char *expect_string, const char *have, const char *expect)
{
	const char *symbol;
	switch (how) {
	case ASSERT_NOT_LT: how = ASSERT_NOT_GE; break;
	case ASSERT_NOT_LE: how = ASSERT_NOT_GT; break;
	case ASSERT_NOT_GT: how = ASSERT_NOT_LE; break;
	case ASSERT_NOT_GE: how = ASSERT_NOT_LT; break;
	}
	switch (how) {
		PTR_CASE(ASSERT_EQ, ==);
		PTR_CASE(ASSERT_NE, !=);
		PTR_CASE(ASSERT_LT, <);
		PTR_CASE(ASSERT_LE, <=);
		PTR_CASE(ASSERT_GT, >);
		PTR_CASE(ASSERT_GE, >=);
	default:
		abort();
	}
	fprintf(stderr, "TEST FAILED at %s:%i: asserting (%s) %s (%s), actualised as %p %s %p\n",
	        file, line, have_string, symbol, expect_string, have, symbol, expect);
	exit(1);
}


#define STR_CASE(HOW, FUNC)\
	case HOW:\
		if (FUNC(have, expect))\
			return;\
		function = #FUNC;\
		break

#define SWAP_STR_CASE(HOW, NEW_HOW)\
	case HOW:\
		test_assert_str(file, line, how, expect_string, have_string, expect, have, rev);\
		return


static void
test_assert_str(const char *file, int line, enum assert_how how, const char *have_string,
                const char *expect_string, const char *have, const char *expect, int rev)
{
	const char *function;
	size_t have_len;
	size_t expect_len;
	char *have_quote = NULL;
	char *expect_quote = NULL;

	if (!have && !expect)
		fprintf(stderr, "TEST FAILED at %s:%i: asserting failed because (%s) and (%s) are NULL\n",
		        file, line, have_string, expect_string);
	else if (!have)
		fprintf(stderr, "TEST FAILED at %s:%i: asserting failed because (%s) was NULL\n",
		        file, line, have_string);
	else if (!expect)
		fprintf(stderr, "TEST FAILED at %s:%i: asserting failed because (%s) was NULL\n",
		        file, line, expect_string);
	if (!have || !expect)
		exit(1);

	switch (how) {
		STR_CASE(ASSERT_EQ, !strcmp);
		STR_CASE(ASSERT_NE, strcmp);
		STR_CASE(ASSERT_IS_IN, strstr);
		STR_CASE(ASSERT_NOT_IN, !strstr);
		SWAP_STR_CASE(ASSERT_CONTAINS_ALL, ASSERT_IS_IN);
		SWAP_STR_CASE(ASSERT_CONTAINS_NOT_ALL, ASSERT_NOT_INT);
		SWAP_STR_CASE(ASSERT_LE, ASSERT_GE);
		SWAP_STR_CASE(ASSERT_LT, ASSERT_GT);
		SWAP_STR_CASE(ASSERT_NOT_LE, ASSERT_NOT_GE);
		SWAP_STR_CASE(ASSERT_NOT_LT, ASSERT_NOT_GT);
	default:
		have_len = strlen(have);
		expect_len = strlen(expect);
		if (rev)
			goto assert_end;
		else
			goto assert_start;
	}
	fprintf(stderr, "TEST FAILED at %s:%i: asserting %s(%s, %s), actualised as %s(%s, %s)\n",
	        file, line, function, have_string, expect_string,
	        function, quote(have, &have_quote), quote(expect, &expect_quote));
free_and_exit:
	free(have_quote);
	free(expect_quote);
	exit(1);

assert_start:
	switch (how) {
	case ASSERT_GE:
		if (have_len >= expect_len && !strncmp(have, expect, expect_len))
			return;
		fprintf(stderr, "TEST FAILED at %s:%i: "
		        "asserting strlen(%s) >= strlen(%s) && !strncmp(%s, %s, strlen(%s)), "
		        "actualised as %zu >= %zu && !strncmp(%s, %s, %zu)\n",
		        file, line, have_string, expect_string, have_string, expect_string, expect_string,
		        have_len, expect_len, quote(have, &have_quote), quote(expect, &expect_quote), expect_len);
		break;
	case ASSERT_NOT_GE:
		if (have_len < expect_len || strncmp(have, expect, expect_len))
			return;
		fprintf(stderr, "TEST FAILED at %s:%i: "
		        "asserting strlen(%s) < strlen(%s) || strncmp(%s, %s, strlen(%s)), "
		        "actualised as %zu < %zu && !strncmp(%s, %s, %zu)\n",
		        file, line, have_string, expect_string, have_string, expect_string, expect_string,
		        have_len, expect_len, quote(have, &have_quote), quote(expect, &expect_quote), expect_len);
		break;
	case ASSERT_GT:
		if (have_len > expect_len && !strncmp(have, expect, expect_len))
			return;
		fprintf(stderr, "TEST FAILED at %s:%i: "
		        "asserting strlen(%s) > strlen(%s) && !strncmp(%s, %s, strlen(%s)), "
		        "actualised as %zu > %zu && !strncmp(%s, %s, %zu)\n",
		        file, line, have_string, expect_string, have_string, expect_string, expect_string,
		        have_len, expect_len, quote(have, &have_quote), quote(expect, &expect_quote), expect_len);
		break;
	case ASSERT_NOT_GT:
		if (have_len <= expect_len || strncmp(have, expect, expect_len))
			return;
		fprintf(stderr, "TEST FAILED at %s:%i: "
		        "asserting strlen(%s) <= strlen(%s) || strncmp(%s, %s, strlen(%s)), "
		        "actualised as %zu <= %zu || strncmp(%s, %s, %zu)\n",
		        file, line, have_string, expect_string, have_string, expect_string, expect_string,
		        have_len, expect_len, quote(have, &have_quote), quote(expect, &expect_quote), expect_len);
		break;
	default:
		abort();
	}
	goto free_and_exit;

assert_end:
	switch (how) {
	case ASSERT_GE:
		if (have_len >= expect_len && !strcmp(&have[have_len - expect_len], expect))
			return;
		fprintf(stderr, "TEST FAILED at %s:%i: "
		        "asserting strlen(%s) >= strlen(%s) && !strcmp(&(%s)[strlen(%s) - strlen(%s)], %s), "
		        "actualised as %zu >= %zu && !strcmp(&%s[%ti], %s)\n",
		        file, line, have_string, expect_string, have_string, have_string, expect_string,
		        expect_string, have_len, expect_len, quote(have, &have_quote),
		        (ptrdiff_t)have_len - (ptrdiff_t)expect_len, quote(expect, &expect_quote));
		break;
	case ASSERT_NOT_GE:
		if (have_len > expect_len || strcmp(&have[have_len - expect_len], expect))
			return;
		fprintf(stderr, "TEST FAILED at %s:%i: "
		        "asserting strlen(%s) < strlen(%s) || strcmp(&(%s)[strlen(%s) - strlen(%s)], %s), "
		        "actualised as %zu < %zu && !strcmp(&%s[%ti], %s)\n",
		        file, line, have_string, expect_string, have_string, have_string, expect_string,
		        expect_string, have_len, expect_len, quote(have, &have_quote),
		        (ptrdiff_t)have_len - (ptrdiff_t)expect_len, quote(expect, &expect_quote));
		break;
	case ASSERT_GT:
		if (have_len > expect_len && !strcmp(&have[have_len - expect_len], expect))
			return;
		fprintf(stderr, "TEST FAILED at %s:%i: "
		        "asserting strlen(%s) > strlen(%s) && !strcmp(&(%s)[strlen(%s) - strlen(%s)], %s), "
		        "actualised as %zu > %zu && !strcmp(&%s[%ti], %s)\n",
		        file, line, have_string, expect_string, have_string, have_string, expect_string,
		        expect_string, have_len, expect_len, quote(have, &have_quote),
		        (ptrdiff_t)have_len - (ptrdiff_t)expect_len, quote(expect, &expect_quote));
		break;
	case ASSERT_NOT_GT:
		if (have_len <= expect_len || strcmp(&have[have_len - expect_len], expect))
			return;
		fprintf(stderr, "TEST FAILED at %s:%i: "
		        "asserting strlen(%s) <= strlen(%s) || strcmp(&(%s)[strlen(%s) - strlen(%s)], %s), "
		        "actualised as %zu <= %zu || strcmp(&%s[%ti], %s)\n",
		        file, line, have_string, expect_string, have_string, have_string, expect_string,
		        expect_string, have_len, expect_len, quote(have, &have_quote),
		        (ptrdiff_t)have_len - (ptrdiff_t)expect_len, quote(expect, &expect_quote));
		break;
	default:
		abort();
	}
	goto free_and_exit;
}


void
test_assert(const char *file, int line, enum assert_type type, enum assert_how how,
            const char *have_string, const char *expect_string, const void *have, const void *expect)
{
	switch (type) {
	case ASSERT_ENUM:
	case ASSERT_UINT:
		test_assert_uint(file, line, how, have_string, expect_string, have, expect);
		break;

	case ASSERT_INT:
		test_assert_int(file, line, how, have_string, expect_string, have, expect);
		break;

	case ASSERT_NULL:
	case ASSERT_PTR:
		test_assert_ptr(file, line, how, have_string, expect_string, have, expect);
		break;

	case ASSERT_STR:
		test_assert_str(file, line, how, have_string, expect_string, have, expect, 0);
		break;

	case ASSERT_STR_REV:
		test_assert_str(file, line, how, have_string, expect_string, have, expect, 1);
		break;

	default:
		abort();
	}
}