2 * Copyright (C) 2010 Joel Rosdahl
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 3 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "test/framework.h"
22 #if defined(HAVE_TERMIOS_H)
27 static unsigned total_asserts;
28 static unsigned total_tests;
29 static unsigned total_suites;
30 static unsigned failed_tests;
31 static const char *current_suite;
32 static const char *current_test;
33 static char *dir_before_suite;
34 static char *dir_before_test;
37 static const char COLOR_END[] = "\x1b[m";
38 static const char COLOR_GREEN[] = "\x1b[32m";
39 static const char COLOR_RED[] = "\x1b[31m";
41 #define COLOR(tty, color) ((tty) ? COLOR_##color : "")
48 return tcgetattr(fd, &t) == 0;
58 return n == 1 ? "" : "s";
62 cct_run(suite_fn *suites, int verbose_output)
67 verbose = verbose_output;
69 for (suite = suites; *suite; suite++) {
70 unsigned test_index = 0;
72 test_index = (*suite)(test_index + 1);
73 if (test_index == 0) {
74 /* We have reached the end of the suite. */
80 if (failed_tests == 0) {
81 printf("%sPASSED%s: %u assertion%s, %u test%s, %u suite%s\n",
82 COLOR(tty, GREEN), COLOR(tty, END),
83 total_asserts, plural_s(total_asserts),
84 total_tests, plural_s(total_tests),
85 total_suites, plural_s(total_suites));
87 printf("%sFAILED%s: %u test%s\n",
88 COLOR(tty, RED), COLOR(tty, END),
89 failed_tests, plural_s(failed_tests));
91 return failed_tests > 0 ? 1 : 0;
95 cct_suite_begin(const char *name)
99 printf("=== SUITE: %s ===\n", name);
101 dir_before_suite = gnu_getcwd();
104 current_suite = name;
110 cct_chdir(dir_before_suite);
111 free(dir_before_suite);
112 dir_before_suite = NULL;
116 cct_test_begin(const char *name)
118 extern char *cache_logfile;
122 printf("--- TEST: %s ---\n", name);
124 dir_before_test = gnu_getcwd();
130 cache_logfile = getenv("CCACHE_LOGFILE");
136 if (dir_before_test) {
137 cct_chdir(dir_before_test);
138 free(dir_before_test);
139 dir_before_test = NULL;
144 cct_check_passed(const char *file, int line, const char *what)
148 printf("%s:%d: Passed assertion: %s\n", file, line, what);
153 cct_check_failed(const char *file, int line, const char *what,
154 const char *expected, const char *actual)
158 fprintf(stderr, "%s:%d: Failed assertion:\n", file, line);
159 fprintf(stderr, " Suite: %s\n", current_suite);
160 fprintf(stderr, " Test: %s\n", current_test);
161 if (expected && actual) {
162 fprintf(stderr, " Expression: %s\n", what);
163 fprintf(stderr, " Expected: %s\n", expected);
164 fprintf(stderr, " Actual: %s\n", actual);
166 fprintf(stderr, " Assertion: %s\n", what);
168 fprintf(stderr, "\n");
172 cct_check_int_eq(const char *file, int line, const char *expression,
173 int expected, int actual)
175 if (expected == actual) {
176 cct_check_passed(file, line, expression);
179 char *exp_str = format("%i", expected);
180 char *act_str = format("%i", actual);
181 cct_check_failed(file, line, expression, exp_str, act_str);
189 cct_check_uns_eq(const char *file, int line, const char *expression,
190 unsigned expected, unsigned actual)
192 if (expected == actual) {
193 cct_check_passed(file, line, expression);
196 char *exp_str = format("%i", expected);
197 char *act_str = format("%i", actual);
198 cct_check_failed(file, line, expression, exp_str, act_str);
206 cct_check_str_eq(const char *file, int line, const char *expression,
207 const char *expected, const char *actual, int free1,
212 if (expected && actual && str_eq(actual, expected)) {
213 cct_check_passed(file, line, expression);
216 char *exp_str = expected ? format("\"%s\"", expected) : x_strdup("(null)");
217 char *act_str = actual ? format("\"%s\"", actual) : x_strdup("(null)");
218 cct_check_failed(file, line, expression, exp_str, act_str);
225 free((char *)expected);
228 free((char *)actual);
234 cct_check_args_eq(const char *file, int line, const char *expression,
235 struct args *expected, struct args *actual,
236 int free1, int free2)
240 if (expected && actual && args_equal(actual, expected)) {
241 cct_check_passed(file, line, expression);
244 char *exp_str = expected ? args_to_string(expected) : x_strdup("(null)");
245 char *act_str = actual ? args_to_string(actual) : x_strdup("(null)");
246 cct_check_failed(file, line, expression, exp_str, act_str);
262 cct_chdir(const char *path)
264 if (chdir(path) != 0) {
265 fprintf(stderr, "chdir: %s: %s", path, strerror(errno));
271 cct_wipe(const char *path)
273 /* TODO: rewrite using traverse(). */
274 char *command = format("rm -rf %s", path);
275 if (system(command) != 0) {
282 cct_create_fresh_dir(const char *path)
285 if (mkdir(path, 0777) != 0) {
286 fprintf(stderr, "mkdir: %s: %s", path, strerror(errno));;