Imported Upstream version 3.4
[platform/upstream/ccache.git] / unittest / framework.h
1 // Copyright (C) 2010-2018 Joel Rosdahl
2 //
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)
6 // any later version.
7 //
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
11 // more details.
12 //
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
16
17 #ifndef TEST_FRAMEWORK_H
18 #define TEST_FRAMEWORK_H
19
20 #include "../src/ccache.h"
21
22 // ============================================================================
23
24 #define TEST_SUITE(name) \
25         unsigned suite_##name(unsigned _start_point) \
26         { \
27                 unsigned _test_counter = 0; \
28                 cct_suite_begin(#name); \
29                 { \
30                         // Empty due to macro trickery.
31
32 #define TEST(name) \
33                         cct_test_end(); \
34                 } \
35                 ++_test_counter; \
36                 { static int name = 0; (void)name; /* Verify test name. */ } \
37                 if (_test_counter >= _start_point) { \
38                         cct_test_begin(#name);
39
40 #define TEST_SUITE_END \
41                         cct_test_end(); \
42                 } \
43                 cct_suite_end(); \
44                 return 0; /* We have reached the end. */ \
45         }
46
47 // ============================================================================
48
49 #define CHECKM(assertion, message) \
50         do { \
51                 if ((assertion)) { \
52                         cct_check_passed(__FILE__, __LINE__, #assertion); \
53                 } else { \
54                         cct_check_failed(__FILE__, __LINE__, #assertion, (message), NULL); \
55                         cct_test_end(); \
56                         cct_suite_end(); \
57                         return _test_counter; \
58                 } \
59         } while (false)
60
61 #define CHECK(assertion) \
62         CHECKM(assertion, NULL)
63
64 #define CHECK_POINTER_EQ_BASE(t, e, a, f1, f2) \
65         do { \
66                 if (!cct_check_##t##_eq(__FILE__, __LINE__, #a, (e), (a), (f1), (f2))) { \
67                         cct_test_end(); \
68                         cct_suite_end(); \
69                         return _test_counter; \
70                 } \
71         } while (false)
72
73 // ============================================================================
74
75 #define CHECK_INT_EQ(expected, actual) \
76         do { \
77                 if (!cct_check_int_eq(__FILE__, __LINE__, #actual, (expected), \
78                                       (actual))) { \
79                         cct_test_end(); \
80                         cct_suite_end(); \
81                         return _test_counter; \
82                 } \
83         } while (false)
84
85 // ============================================================================
86
87 #define CHECK_FLOAT_EQ(expected, actual) \
88         do { \
89                 if (!cct_check_float_eq(__FILE__, __LINE__, #actual, (expected), \
90                                       (actual))) { \
91                         cct_test_end(); \
92                         cct_suite_end(); \
93                         return _test_counter; \
94                 } \
95         } while (false)
96
97 // ============================================================================
98
99 #define CHECK_STR_EQ(expected, actual) \
100         CHECK_POINTER_EQ_BASE(str, expected, actual, false, false)
101
102 #define CHECK_STR_EQ_FREE1(expected, actual) \
103         CHECK_POINTER_EQ_BASE(str, expected, actual, true, false)
104
105 #define CHECK_STR_EQ_FREE2(expected, actual) \
106         CHECK_POINTER_EQ_BASE(str, expected, actual, false, true)
107
108 #define CHECK_STR_EQ_FREE12(expected, actual) \
109         CHECK_POINTER_EQ_BASE(str, expected, actual, true, true)
110
111 // ============================================================================
112
113 #define CHECK_ARGS_EQ(expected, actual) \
114         CHECK_POINTER_EQ_BASE(args, expected, actual, false, false)
115
116 #define CHECK_ARGS_EQ_FREE1(expected, actual) \
117         CHECK_POINTER_EQ_BASE(args, expected, actual, true, false)
118
119 #define CHECK_ARGS_EQ_FREE2(expected, actual) \
120         CHECK_POINTER_EQ_BASE(args, expected, actual, false, true)
121
122 #define CHECK_ARGS_EQ_FREE12(expected, actual) \
123         CHECK_POINTER_EQ_BASE(args, expected, actual, true, true)
124
125 // ============================================================================
126
127 typedef unsigned (*suite_fn)(unsigned);
128 int cct_run(suite_fn *suites, int verbose);
129
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,
143                       bool free2);
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);
150
151 #endif