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) \
27 unsigned _test_counter = 0; \
28 cct_suite_begin(#name); \
30 // Empty due to macro trickery.
36 { static int name = 0; (void)name; /* Verify test name. */ } \
37 if (_test_counter >= _start_point) { \
38 cct_test_begin(#name);
40 #define TEST_SUITE_END \
44 return 0; /* We have reached the end. */ \
47 // ============================================================================
49 #define CHECKM(assertion, message) \
52 cct_check_passed(__FILE__, __LINE__, #assertion); \
54 cct_check_failed(__FILE__, __LINE__, #assertion, (message), NULL); \
57 return _test_counter; \
61 #define CHECK(assertion) \
62 CHECKM(assertion, NULL)
64 #define CHECK_POINTER_EQ_BASE(t, e, a, f1, f2) \
66 if (!cct_check_##t##_eq(__FILE__, __LINE__, #a, (e), (a), (f1), (f2))) { \
69 return _test_counter; \
73 // ============================================================================
75 #define CHECK_INT_EQ(expected, actual) \
77 if (!cct_check_int_eq(__FILE__, __LINE__, #actual, (expected), \
81 return _test_counter; \
85 // ============================================================================
87 #define CHECK_FLOAT_EQ(expected, actual) \
89 if (!cct_check_float_eq(__FILE__, __LINE__, #actual, (expected), \
93 return _test_counter; \
97 // ============================================================================
99 #define CHECK_STR_EQ(expected, actual) \
100 CHECK_POINTER_EQ_BASE(str, expected, actual, false, false)
102 #define CHECK_STR_EQ_FREE1(expected, actual) \
103 CHECK_POINTER_EQ_BASE(str, expected, actual, true, false)
105 #define CHECK_STR_EQ_FREE2(expected, actual) \
106 CHECK_POINTER_EQ_BASE(str, expected, actual, false, true)
108 #define CHECK_STR_EQ_FREE12(expected, actual) \
109 CHECK_POINTER_EQ_BASE(str, expected, actual, true, true)
111 // ============================================================================
113 #define CHECK_ARGS_EQ(expected, actual) \
114 CHECK_POINTER_EQ_BASE(args, expected, actual, false, false)
116 #define CHECK_ARGS_EQ_FREE1(expected, actual) \
117 CHECK_POINTER_EQ_BASE(args, expected, actual, true, false)
119 #define CHECK_ARGS_EQ_FREE2(expected, actual) \
120 CHECK_POINTER_EQ_BASE(args, expected, actual, false, true)
122 #define CHECK_ARGS_EQ_FREE12(expected, actual) \
123 CHECK_POINTER_EQ_BASE(args, expected, actual, true, true)
125 // ============================================================================
127 typedef unsigned (*suite_fn)(unsigned);
128 int cct_run(suite_fn *suites, int verbose);
130 void cct_suite_begin(const char *name);
131 void cct_suite_end(void);
132 void cct_test_begin(const char *name);
133 void cct_test_end(void);
134 void cct_check_passed(const char *file, int line, const char *assertion);
135 void cct_check_failed(const char *file, int line, const char *assertion,
136 const char *expected, const char *actual);
137 bool cct_check_float_eq(const char *file, int line, const char *expression,
138 double expected, double actual);
139 bool cct_check_int_eq(const char *file, int line, const char *expression,
140 int64_t expected, int64_t actual);
141 bool cct_check_str_eq(const char *file, int line, const char *expression,
142 const char *expected, const char *actual, bool free1,
144 bool cct_check_args_eq(const char *file, int line, const char *expression,
145 struct args *expected, struct args *actual,
146 bool free1, bool free2);
147 void cct_chdir(const char *path);
148 void cct_wipe(const char *path);
149 void cct_create_fresh_dir(const char *path);