summaryrefslogtreecommitdiff
path: root/test/framework.c
blob: 88c9ba48cb8fb99e39ae1f2efe1380cbf2ad3731 (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
/*
 * Copyright (C) 2010 Joel Rosdahl
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "ccache.h"
#include "framework.h"
#include <errno.h>
#include <stdio.h>
#if defined(HAVE_TERMIOS_H)
#define USE_COLOR
#include <termios.h>
#endif

static unsigned passed_asserts;
static unsigned failed_asserts;
static unsigned passed_tests;
static unsigned failed_tests;
static unsigned passed_suites;
static unsigned failed_suites;
static unsigned failed_asserts_before_suite;
static unsigned failed_asserts_before_test;
static const char *current_suite;
static const char *current_test;
static char *dir_before_suite;
static char *dir_before_test;
static int verbose;

static const char COLOR_END[] = "\x1b[m";
static const char COLOR_GREEN[] = "\x1b[32m";
static const char COLOR_RED[] = "\x1b[31m";

#define COLOR(tty, color) ((tty) ? COLOR_##color : "")

static int
is_tty(int fd)
{
#ifdef USE_COLOR
	struct termios t;
	return tcgetattr(fd, &t) == 0;
#else
	(void)fd;
	return 0;
#endif
}

static const char *
plural_s(unsigned n)
{
	return n == 1 ? "" : "s";
}

static void
verify_test_suite_name(const char *name)
{
	const char *p = name;
	while (*p) {
		if ((*p < '0' || *p > '9')
		      && (*p < 'A' || *p > 'Z')
		      && *p != '_'
		      && (*p < 'a' || *p > 'z')) {
			fprintf(stderr, "Bad character ('%c') in suite/test name: %s\n", *p, name);
			exit(1);
		}
		++p;
	}
}

int
cct_run(suite_fn *suites, int verbose_output)
{
	suite_fn *suite;
	int tty = is_tty(1);

	verbose = verbose_output;

	for (suite = suites; *suite; suite++) {
		unsigned test_index = 0;
		while (1) {
			test_index = (*suite)(test_index + 1);
			if (test_index == 0) {
				break;
			}
		}
		--passed_tests; /* Fix false increase in first TEST expansion. */
	}

	if (failed_asserts == 0) {
		printf("%sPASSED%s: %u assertion%s, %u test%s, %u suite%s\n",
		       COLOR(tty, GREEN), COLOR(tty, END),
		       passed_asserts, plural_s(passed_asserts),
		       passed_tests, plural_s(passed_tests),
		       passed_suites, plural_s(passed_suites));
	} else {
		printf("%sFAILED%s: %u assertion%s, %u test%s, %u suite%s\n",
		       COLOR(tty, RED), COLOR(tty, END),
		       failed_asserts, plural_s(failed_asserts),
		       failed_tests, plural_s(failed_tests),
		       failed_suites, plural_s(failed_suites));
	}
	return failed_asserts > 0 ? 1 : 0;
}

void cct_suite_begin(const char *name)
{
	verify_test_suite_name(name);
	if (verbose) {
		printf("=== SUITE: %s ===\n", name);
	}
	dir_before_suite = gnu_getcwd();
	create_dir(name);
	cct_chdir(name);
	current_suite = name;
	failed_asserts_before_suite = failed_asserts;
	failed_asserts_before_test = failed_tests; /* For first cct_test_end(). */
}

void cct_suite_end()
{
	cct_chdir(dir_before_suite);
	free(dir_before_suite);
	dir_before_suite = NULL;
	if (failed_asserts > failed_asserts_before_suite) {
		++failed_suites;
	} else {
		++passed_suites;
	}
}

void cct_test_begin(const char *name)
{
	verify_test_suite_name(name);
	if (verbose) {
		printf("--- TEST: %s ---\n", name);
	}
	dir_before_test = gnu_getcwd();
	create_dir(name);
	cct_chdir(name);
	current_test = name;
	failed_asserts_before_test = failed_asserts;
}

void cct_test_end()
{
	if (dir_before_test) {
		cct_chdir(dir_before_test);
		free(dir_before_test);
		dir_before_test = NULL;
	}
	if (failed_asserts > failed_asserts_before_test) {
		++failed_tests;
	} else {
		++passed_tests;
	}
}

void
cct_check_passed(void)
{
	++passed_asserts;
}

void
cct_check_failed(const char *file, int line, const char *what,
                 const char *expected, const char *actual)
{
	++failed_asserts;
	fprintf(stderr, "%s:%u: Failed assertion:\n", file, line);
	fprintf(stderr, "  Suite:      %s\n", current_suite);
	fprintf(stderr, "  Test:       %s\n", current_test);
	if (expected && actual) {
		fprintf(stderr, "  Expression: %s\n", what);
		fprintf(stderr, "  Expected:   %s\n", expected);
		fprintf(stderr, "  Actual:     %s\n", actual);
	} else {
		fprintf(stderr, "  Assertion:  %s\n", what);
	}
	fprintf(stderr, "\n");
}

int
cct_check_int_eq(const char *file, int line, const char *expression,
                 int expected, int actual)
{
	if (expected == actual) {
		cct_check_passed();
		return 1;
	} else {
		char *exp_str = format("%i", expected);
		char *act_str = format("%i", actual);
		cct_check_failed(file, line, expression, exp_str, act_str);
		free(exp_str);
		free(act_str);
		return 0;
	}
}

int
cct_check_uns_eq(const char *file, int line, const char *expression,
                 unsigned expected, unsigned actual)
{
	if (expected == actual) {
		cct_check_passed();
		return 1;
	} else {
		char *exp_str = format("%i", expected);
		char *act_str = format("%i", actual);
		cct_check_failed(file, line, expression, exp_str, act_str);
		free(exp_str);
		free(act_str);
		return 0;
	}
}

int
cct_check_str_eq(const char *file, int line, const char *expression,
                 char *expected, char *actual, int free1, int free2)
{
	int result;

	if (expected && actual && strcmp(actual, expected) == 0) {
		cct_check_passed();
		result = 1;
	} else {
		char *exp_str = expected ? format("\"%s\"", expected) : x_strdup("(null)");
		char *act_str = actual ? format("\"%s\"", actual) : x_strdup("(null)");
		cct_check_failed(file, line, expression, exp_str, act_str);
		free(exp_str);
		free(act_str);
		result = 0;
	}

	if (free1) {
		free(expected);
	}
	if (free2) {
		free(actual);
	}
	return result;
}

int
cct_check_args_eq(const char *file, int line, const char *expression,
                  struct args *expected, struct args *actual,
                  int free1, int free2)
{
	int result;

	if (expected && actual && args_equal(actual, expected)) {
		cct_check_passed();
		result = 1;
	} else {
		char *exp_str = expected ? args_to_string(expected) : x_strdup("(null)");
		char *act_str = actual ? args_to_string(actual) : x_strdup("(null)");
		cct_check_failed(file, line, expression, exp_str, act_str);
		free(exp_str);
		free(act_str);
		result = 0;
	}

	if (free1) {
		args_free(expected);
	}
	if (free2) {
		args_free(actual);
	}
	return result;
}

void cct_chdir(const char *path)
{
	if (chdir(path) != 0) {
		fprintf(stderr, "chdir: %s: %s", path, strerror(errno));
		abort();
	}
}

void
cct_wipe(const char *path)
{
	/* TODO: rewrite using traverse(). */
	char *command = format("rm -rf %s", path);
	if (system(command) != 0) {
		perror(command);
	}
	free(command);
}

void cct_create_fresh_dir(const char *path)
{
	cct_wipe(path);
	if (mkdir(path, 0777) != 0) {
		fprintf(stderr, "mkdir: %s: %s", path, strerror(errno));;
		abort();
	}
}

void cct_create_file(const char *path, const char *content)
{
	FILE *f = fopen(path, "w");
	if (!f || fputs(content, f) < 0) {
		fprintf(stderr, "cct_create_file: %s: %s", path, strerror(errno));
	}
	if (f) {
		fclose(f);
	}
}