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
19 #ifndef TEST_FRAMEWORK_H
20 #define TEST_FRAMEWORK_H
24 /*****************************************************************************/
26 #define TEST_SUITE(name) \
27 unsigned suite_##name(unsigned _start_point) \
29 unsigned _test_counter = 0; \
30 cct_suite_begin(#name); \
32 /* Empty due to macro trickery. */
38 { static int name = 0; (void)name; /* Verify test name. */ } \
39 if (_test_counter >= _start_point) { \
40 cct_test_begin(#name);
42 #define TEST_SUITE_END \
46 return 0; /* We have reached the end. */ \
49 /*****************************************************************************/
51 #define CHECK(assertion) \
54 cct_check_passed(__FILE__, __LINE__, #assertion); \
56 cct_check_failed(__FILE__, __LINE__, #assertion, NULL, NULL); \
59 return _test_counter; \
63 #define CHECK_POINTER_EQ_BASE(t, e, a, f1, f2) \
65 if (!cct_check_##t##_eq(__FILE__, __LINE__, #a, (e), (a), (f1), (f2))) { \
68 return _test_counter; \
72 /*****************************************************************************/
74 #define CHECK_INT_EQ(expected, actual) \
76 if (!cct_check_int_eq(__FILE__, __LINE__, #actual, (expected), (actual))) { \
79 return _test_counter; \
83 #define CHECK_UNS_EQ(expected, actual) \
85 if (!cct_check_int_eq(__FILE__, __LINE__, #actual, (expected), (actual))) { \
88 return _test_counter; \
92 /*****************************************************************************/
94 #define CHECK_STR_EQ(expected, actual) \
95 CHECK_POINTER_EQ_BASE(str, expected, actual, 0, 0)
97 #define CHECK_STR_EQ_FREE1(expected, actual) \
98 CHECK_POINTER_EQ_BASE(str, expected, actual, 1, 0)
100 #define CHECK_STR_EQ_FREE2(expected, actual) \
101 CHECK_POINTER_EQ_BASE(str, expected, actual, 0, 1)
103 #define CHECK_STR_EQ_FREE12(expected, actual) \
104 CHECK_POINTER_EQ_BASE(str, expected, actual, 1, 1)
106 /*****************************************************************************/
108 #define CHECK_ARGS_EQ(expected, actual) \
109 CHECK_POINTER_EQ_BASE(args, expected, actual, 0, 0)
111 #define CHECK_ARGS_EQ_FREE1(expected, actual) \
112 CHECK_POINTER_EQ_BASE(args, expected, actual, 1, 0)
114 #define CHECK_ARGS_EQ_FREE2(expected, actual) \
115 CHECK_POINTER_EQ_BASE(args, expected, actual, 0, 1)
117 #define CHECK_ARGS_EQ_FREE12(expected, actual) \
118 CHECK_POINTER_EQ_BASE(args, expected, actual, 1, 1)
120 /*****************************************************************************/
122 typedef unsigned (*suite_fn)(unsigned);
123 int cct_run(suite_fn *suites, int verbose);
125 void cct_suite_begin(const char *name);
126 void cct_suite_end();
127 void cct_test_begin(const char *name);
129 void cct_check_passed(const char *file, int line, const char *assertion);
130 void cct_check_failed(const char *file, int line, const char *assertion,
131 const char *expected, const char *actual);
132 int cct_check_int_eq(const char *file, int line, const char *expression,
133 int expected, int actual);
134 int cct_check_uns_eq(const char *file, int line, const char *expression,
135 unsigned expected, unsigned actual);
136 int cct_check_str_eq(const char *file, int line, const char *expression,
137 const char *expected, const char *actual, int free1,
139 int cct_check_args_eq(const char *file, int line, const char *expression,
140 struct args *expected, struct args *actual,
141 int free1, int free2);
142 void cct_chdir(const char *path);
143 void cct_wipe(const char *path);
144 void cct_create_fresh_dir(const char *path);