Imported Upstream version 3.5.1
[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         unsigned suite_##name(unsigned _start_point) \
27         { \
28                 unsigned _test_counter = 0; \
29                 cct_suite_begin(#name); \
30                 { \
31                         // Empty due to macro trickery.
32
33 #define TEST(name) \
34                         cct_test_end(); \
35                 } \
36                 ++_test_counter; \
37                 { static int name = 0; (void)name; /* Verify test name. */ } \
38                 if (_test_counter >= _start_point) { \
39                         cct_test_begin(#name);
40
41 #define TEST_SUITE_END \
42                         cct_test_end(); \
43                 } \
44                 cct_suite_end(); \
45                 return 0; /* We have reached the end. */ \
46         }
47
48 // ============================================================================
49
50 #define CHECKM(assertion, message) \
51         do { \
52                 if ((assertion)) { \
53                         cct_check_passed(__FILE__, __LINE__, #assertion); \
54                 } else { \
55                         cct_check_failed(__FILE__, __LINE__, #assertion, (message), NULL); \
56                         cct_test_end(); \
57                         cct_suite_end(); \
58                         return _test_counter; \
59                 } \
60         } while (false)
61
62 #define CHECK(assertion) \
63         CHECKM(assertion, NULL)
64
65 #define CHECK_POINTER_EQ_BASE(t, e, a, f1, f2) \
66         do { \
67                 if (!cct_check_##t##_eq(__FILE__, __LINE__, #a, (e), (a), (f1), (f2))) { \
68                         cct_test_end(); \
69                         cct_suite_end(); \
70                         return _test_counter; \
71                 } \
72         } while (false)
73
74 // ============================================================================
75
76 #define CHECK_INT_EQ(expected, actual) \
77         do { \
78                 if (!cct_check_int_eq(__FILE__, __LINE__, #actual, (expected), \
79                                       (actual))) { \
80                         cct_test_end(); \
81                         cct_suite_end(); \
82                         return _test_counter; \
83                 } \
84         } while (false)
85
86 // ============================================================================
87
88 #define CHECK_DOUBLE_EQ(expected, actual) \
89         do { \
90                 if (!cct_check_double_eq(__FILE__, __LINE__, #actual, (expected), \
91                                          (actual))) { \
92                         cct_test_end(); \
93                         cct_suite_end(); \
94                         return _test_counter; \
95                 } \
96         } while (false)
97
98 // ============================================================================
99
100 #define CHECK_STR_EQ(expected, actual) \
101         CHECK_POINTER_EQ_BASE(str, expected, actual, false, false)
102
103 #define CHECK_STR_EQ_FREE1(expected, actual) \
104         CHECK_POINTER_EQ_BASE(str, expected, actual, true, false)
105
106 #define CHECK_STR_EQ_FREE2(expected, actual) \
107         CHECK_POINTER_EQ_BASE(str, expected, actual, false, true)
108
109 #define CHECK_STR_EQ_FREE12(expected, actual) \
110         CHECK_POINTER_EQ_BASE(str, expected, actual, true, true)
111
112 // ============================================================================
113
114 #define CHECK_ARGS_EQ(expected, actual) \
115         CHECK_POINTER_EQ_BASE(args, expected, actual, false, false)
116
117 #define CHECK_ARGS_EQ_FREE1(expected, actual) \
118         CHECK_POINTER_EQ_BASE(args, expected, actual, true, false)
119
120 #define CHECK_ARGS_EQ_FREE2(expected, actual) \
121         CHECK_POINTER_EQ_BASE(args, expected, actual, false, true)
122
123 #define CHECK_ARGS_EQ_FREE12(expected, actual) \
124         CHECK_POINTER_EQ_BASE(args, expected, actual, true, true)
125
126 // ============================================================================
127
128 typedef unsigned (*suite_fn)(unsigned);
129 int cct_run(suite_fn *suites, int verbose);
130
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);
151
152 #endif