summaryrefslogtreecommitdiff
path: root/t/unit-tests/test-lib.c
blob: 66d6980ffbefd83ed350c323bf946b29cb54dbf2 (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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "test-lib.h"
 
enum result {
	RESULT_NONE,
	RESULT_FAILURE,
	RESULT_SKIP,
	RESULT_SUCCESS,
	RESULT_TODO
};
 
static struct {
	enum result result;
	int count;
	unsigned failed :1;
	unsigned lazy_plan :1;
	unsigned running :1;
	unsigned skip_all :1;
	unsigned todo :1;
} ctx = {
	.lazy_plan = 1,
	.result = RESULT_NONE,
};
 
/*
 * Visual C interpolates the absolute Windows path for `__FILE__`,
 * but we want to see relative paths, as verified by t0080.
 * There are other compilers that do the same, and are not for
 * Windows.
 */
#include "dir.h"
 
static const char *make_relative(const char *location)
{
	static char prefix[] = __FILE__, buf[PATH_MAX], *p;
	static size_t prefix_len;
	static int need_bs_to_fs = -1;
 
	/* one-time preparation */
	if (need_bs_to_fs < 0) {
		size_t len = strlen(prefix);
		char needle[] = "t\\unit-tests\\test-lib.c";
		size_t needle_len = strlen(needle);
 
		if (len < needle_len)
			die("unexpected prefix '%s'", prefix);
 
		/*
		 * The path could be relative (t/unit-tests/test-lib.c)
		 * or full (/home/user/git/t/unit-tests/test-lib.c).
		 * Check the slash between "t" and "unit-tests".
		 */
		prefix_len = len - needle_len;
		if (prefix[prefix_len + 1] == '/') {
			/* Oh, we're not Windows */
			for (size_t i = 0; i < needle_len; i++)
				if (needle[i] == '\\')
					needle[i] = '/';
			need_bs_to_fs = 0;
		} else {
			need_bs_to_fs = 1;
		}
 
		/*
		 * prefix_len == 0 if the compiler gives paths relative
		 * to the root of the working tree.  Otherwise, we want
		 * to see that we did find the needle[] at a directory
		 * boundary.  Again we rely on that needle[] begins with
		 * "t" followed by the directory separator.
		 */
		if (fspathcmp(needle, prefix + prefix_len) ||
		    (prefix_len && prefix[prefix_len - 1] != needle[1]))
			die("unexpected suffix of '%s'", prefix);
	}
 
	/*
	 * Does it not start with the expected prefix?
	 * Return it as-is without making it worse.
	 */
	if (prefix_len && fspathncmp(location, prefix, prefix_len))
		return location;
 
	/*
	 * If we do not need to munge directory separator, we can return
	 * the substring at the tail of the location.
	 */
	if (!need_bs_to_fs)
		return location + prefix_len;
 
	/* convert backslashes to forward slashes */
	strlcpy(buf, location + prefix_len, sizeof(buf));
	for (p = buf; *p; p++)
		if (*p == '\\')
			*p = '/';
	return buf;
}
 
static void msg_with_prefix(const char *prefix, const char *format, va_list ap)
{
	fflush(stderr);
	if (prefix)
		fprintf(stdout, "%s", prefix);
	vprintf(format, ap); /* TODO: handle newlines */
	putc('\n', stdout);
	fflush(stdout);
}
 
void test_msg(const char *format, ...)
{
	va_list ap;
 
	va_start(ap, format);
	msg_with_prefix("# ", format, ap);
	va_end(ap);
}
 
void test_plan(int count)
{
	assert(!ctx.running);
 
	fflush(stderr);
	printf("1..%d\n", count);
	fflush(stdout);
	ctx.lazy_plan = 0;
}
 
int test_done(void)
{
	assert(!ctx.running);
 
	if (ctx.lazy_plan)
		test_plan(ctx.count);
 
	return ctx.failed;
}
 
void test_skip(const char *format, ...)
{
	va_list ap;
 
	assert(ctx.running);
 
	ctx.result = RESULT_SKIP;
	va_start(ap, format);
	if (format)
		msg_with_prefix("# skipping test - ", format, ap);
	va_end(ap);
}
 
void test_skip_all(const char *format, ...)
{
	va_list ap;
	const char *prefix;
 
	if (!ctx.count && ctx.lazy_plan) {
		/* We have not printed a test plan yet */
		prefix = "1..0 # SKIP ";
		ctx.lazy_plan = 0;
	} else {
		/* We have already printed a test plan */
		prefix = "Bail out! # ";
		ctx.failed = 1;
	}
	ctx.skip_all = 1;
	ctx.result = RESULT_SKIP;
	va_start(ap, format);
	msg_with_prefix(prefix, format, ap);
	va_end(ap);
}
 
int test__run_begin(void)
{
	assert(!ctx.running);
 
	ctx.count++;
	ctx.result = RESULT_NONE;
	ctx.running = 1;
 
	return ctx.skip_all;
}
 
static void print_description(const char *format, va_list ap)
{
	if (format) {
		fputs(" - ", stdout);
		vprintf(format, ap);
	}
}
 
int test__run_end(int was_run UNUSED, const char *location, const char *format, ...)
{
	va_list ap;
 
	assert(ctx.running);
	assert(!ctx.todo);
 
	fflush(stderr);
	va_start(ap, format);
	if (!ctx.skip_all) {
		switch (ctx.result) {
		case RESULT_SUCCESS:
			printf("ok %d", ctx.count);
			print_description(format, ap);
			break;
 
		case RESULT_FAILURE:
			printf("not ok %d", ctx.count);
			print_description(format, ap);
			break;
 
		case RESULT_TODO:
			printf("not ok %d", ctx.count);
			print_description(format, ap);
			printf(" # TODO");
			break;
 
		case RESULT_SKIP:
			printf("ok %d", ctx.count);
			print_description(format, ap);
			printf(" # SKIP");
			break;
 
		case RESULT_NONE:
			test_msg("BUG: test has no checks at %s",
				 make_relative(location));
			printf("not ok %d", ctx.count);
			print_description(format, ap);
			ctx.result = RESULT_FAILURE;
			break;
		}
	}
	va_end(ap);
	ctx.running = 0;
	if (ctx.skip_all)
		return 1;
	putc('\n', stdout);
	fflush(stdout);
	ctx.failed |= ctx.result == RESULT_FAILURE;
 
	return ctx.result != RESULT_FAILURE;
}
 
static void test_fail(void)
{
	assert(ctx.result != RESULT_SKIP);
 
	ctx.result = RESULT_FAILURE;
}
 
static void test_pass(void)
{
	assert(ctx.result != RESULT_SKIP);
 
	if (ctx.result == RESULT_NONE)
		ctx.result = RESULT_SUCCESS;
}
 
static void test_todo(void)
{
	assert(ctx.result != RESULT_SKIP);
 
	if (ctx.result != RESULT_FAILURE)
		ctx.result = RESULT_TODO;
}
 
int test_assert(const char *location, const char *check, int ok)
{
	assert(ctx.running);
 
	if (ctx.result == RESULT_SKIP) {
		test_msg("skipping check '%s' at %s", check,
			 make_relative(location));
		return 1;
	}
	if (!ctx.todo) {
		if (ok) {
			test_pass();
		} else {
			test_msg("check \"%s\" failed at %s", check,
				 make_relative(location));
			test_fail();
		}
	}
 
	return !!ok;
}
 
void test__todo_begin(void)
{
	assert(ctx.running);
	assert(!ctx.todo);
 
	ctx.todo = 1;
}
 
int test__todo_end(const char *location, const char *check, int res)
{
	assert(ctx.running);
	assert(ctx.todo);
 
	ctx.todo = 0;
	if (ctx.result == RESULT_SKIP)
		return 1;
	if (res) {
		test_msg("todo check '%s' succeeded at %s", check,
			 make_relative(location));
		test_fail();
	} else {
		test_todo();
	}
 
	return !res;
}
 
int check_bool_loc(const char *loc, const char *check, int ok)
{
	return test_assert(loc, check, ok);
}
 
union test__tmp test__tmp[2];
 
int check_int_loc(const char *loc, const char *check, int ok,
		  intmax_t a, intmax_t b)
{
	int ret = test_assert(loc, check, ok);
 
	if (!ret) {
		test_msg("   left: %"PRIdMAX, a);
		test_msg("  right: %"PRIdMAX, b);
	}
 
	return ret;
}
 
int check_uint_loc(const char *loc, const char *check, int ok,
		   uintmax_t a, uintmax_t b)
{
	int ret = test_assert(loc, check, ok);
 
	if (!ret) {
		test_msg("   left: %"PRIuMAX, a);
		test_msg("  right: %"PRIuMAX, b);
	}
 
	return ret;
}
 
static void print_one_char(char ch, char quote)
{
	if ((unsigned char)ch < 0x20u || ch == 0x7f) {
		/* TODO: improve handling of \a, \b, \f ... */
		printf("\\%03o", (unsigned char)ch);
	} else {
		if (ch == '\\' || ch == quote)
			putc('\\', stdout);
		putc(ch, stdout);
	}
}
 
static void print_char(const char *prefix, char ch)
{
	printf("# %s: '", prefix);
	print_one_char(ch, '\'');
	fputs("'\n", stdout);
}
 
int check_char_loc(const char *loc, const char *check, int ok, char a, char b)
{
	int ret = test_assert(loc, check, ok);
 
	if (!ret) {
		fflush(stderr);
		print_char("   left", a);
		print_char("  right", b);
		fflush(stdout);
	}
 
	return ret;
}
 
static void print_str(const char *prefix, const char *str)
{
	printf("# %s: ", prefix);
	if (!str) {
		fputs("NULL\n", stdout);
	} else {
		putc('"', stdout);
		while (*str)
			print_one_char(*str++, '"');
		fputs("\"\n", stdout);
	}
}
 
int check_str_loc(const char *loc, const char *check,
		  const char *a, const char *b)
{
	int ok = (!a && !b) || (a && b && !strcmp(a, b));
	int ret = test_assert(loc, check, ok);
 
	if (!ret) {
		fflush(stderr);
		print_str("   left", a);
		print_str("  right", b);
		fflush(stdout);
	}
 
	return ret;
}