1 // Copyright (C) 2010-2018 Joel Rosdahl
3 // This program is free software; you can redistribute it and/or modify it
4 // under the terms of the GNU General Public License as published by the Free
5 // Software Foundation; either version 3 of the License, or (at your option)
8 // This program is distributed in the hope that it will be useful, but WITHOUT
9 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 // You should have received a copy of the GNU General Public License along with
14 // this program; if not, write to the Free Software Foundation, Inc., 51
15 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 #ifndef TEST_FRAMEWORK_H
18 #define TEST_FRAMEWORK_H
20 #include "../src/ccache.h"
22 // ============================================================================
24 #define TEST_SUITE(name) \
25 unsigned suite_##name(unsigned _start_point); \
26 unsigned suite_##name(unsigned _start_point) \
28 unsigned _test_counter = 0; \
29 cct_suite_begin(#name); \
31 // Empty due to macro trickery.
37 { static int name = 0; (void)name; /* Verify test name. */ } \
38 if (_test_counter >= _start_point) { \
39 cct_test_begin(#name);
41 #define TEST_SUITE_END \
45 return 0; /* We have reached the end. */ \
48 // ============================================================================
50 #define CHECKM(assertion, message) \
53 cct_check_passed(__FILE__, __LINE__, #assertion); \
55 cct_check_failed(__FILE__, __LINE__, #assertion, (message), NULL); \
58 return _test_counter; \
62 #define CHECK(assertion) \
63 CHECKM(assertion, NULL)
65 #define CHECK_POINTER_EQ_BASE(t, e, a, f1, f2) \
67 if (!cct_check_##t##_eq(__FILE__, __LINE__, #a, (e), (a), (f1), (f2))) { \
70 return _test_counter; \
74 // ============================================================================
76 #define CHECK_INT_EQ(expected, actual) \
78 if (!cct_check_int_eq(__FILE__, __LINE__, #actual, (expected), \
82 return _test_counter; \
86 // ============================================================================
88 #define CHECK_DOUBLE_EQ(expected, actual) \
90 if (!cct_check_double_eq(__FILE__, __LINE__, #actual, (expected), \
94 return _test_counter; \
98 // ============================================================================
100 #define CHECK_STR_EQ(expected, actual) \
101 CHECK_POINTER_EQ_BASE(str, expected, actual, false, false)
103 #define CHECK_STR_EQ_FREE1(expected, actual) \
104 CHECK_POINTER_EQ_BASE(str, expected, actual, true, false)
106 #define CHECK_STR_EQ_FREE2(expected, actual) \
107 CHECK_POINTER_EQ_BASE(str, expected, actual, false, true)
109 #define CHECK_STR_EQ_FREE12(expected, actual) \
110 CHECK_POINTER_EQ_BASE(str, expected, actual, true, true)
112 // ============================================================================
114 #define CHECK_ARGS_EQ(expected, actual) \
115 CHECK_POINTER_EQ_BASE(args, expected, actual, false, false)
117 #define CHECK_ARGS_EQ_FREE1(expected, actual) \
118 CHECK_POINTER_EQ_BASE(args, expected, actual, true, false)
120 #define CHECK_ARGS_EQ_FREE2(expected, actual) \
121 CHECK_POINTER_EQ_BASE(args, expected, actual, false, true)
123 #define CHECK_ARGS_EQ_FREE12(expected, actual) \
124 CHECK_POINTER_EQ_BASE(args, expected, actual, true, true)
126 // ============================================================================
128 typedef unsigned (*suite_fn)(unsigned);
129 int cct_run(suite_fn *suites, int verbose);
131 void cct_suite_begin(const char *name);
132 void cct_suite_end(void);
133 void cct_test_begin(const char *name);
134 void cct_test_end(void);
135 void cct_check_passed(const char *file, int line, const char *assertion);
136 void cct_check_failed(const char *file, int line, const char *assertion,
137 const char *expected, const char *actual);
138 bool cct_check_double_eq(const char *file, int line, const char *expression,
139 double expected, double actual);
140 bool cct_check_int_eq(const char *file, int line, const char *expression,
141 int64_t expected, int64_t actual);
142 bool cct_check_str_eq(const char *file, int line, const char *expression,
143 char *expected, char *actual,
144 bool free1, bool free2);
145 bool cct_check_args_eq(const char *file, int line, const char *expression,
146 struct args *expected, struct args *actual,
147 bool free1, bool free2);
148 void cct_chdir(const char *path);
149 void cct_wipe(const char *path);
150 void cct_create_fresh_dir(const char *path);