aboutsummaryrefslogtreecommitdiffhomepage
path: root/src-common/unittest/unittest.h
blob: aad9a3f19fa3c660e1bfdee78bf143c86feae6e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296

/*

  unittest.h -- Simple "library" for unit tests

  Copyright © 2016-2024 Samuel Lidén Borell <samuel@kodafritt.se>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/
#ifndef UNITTEST_H
#define UNITTEST_H

#include <stddef.h>
#include <stdio.h> /* several tests print error details using fprintf */

/*
  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

  Simple unit testing "library". Please see README.md for an overview

  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*/


#if defined(__GNUC__) || defined(__clang__) || defined(__cppcheck__)
#    define UNITTEST_NORETURN __attribute__((__noreturn__))
#else
#    define UNITTEST_NORETURN
#endif


/* =======================================================================
   ||||||||||  Internal Definitions. Should be used via macros  ||||||||||
   ======================================================================= */

/** Function type for test cases, and for before/after functions */
typedef void TestFunction(void);

/**
 * Information entry of a given test case.
 * Each test file should have an array of this, named test_xyz where the
 * filename should be test_xyz.c (the "test_" part is required).
 */
typedef struct {
    /** Test/function name. It may also point to either before_test_ptrval or
        after_test_ptrval to indicate that the function should be run before
        or after each test case, respectively. */
    const char *name;
    /** Pointer to test function */
    TestFunction *function;
} TestFunctionInfo;

/** Special sentinel value for the name in TestFunctionInfo.
    Use the TEST_BEFORE macro instead. */
extern char before_test_ptrval[];
/** Special sentinel value for the name in TestFunctionInfo.
    Use the TEST_AFTER macro instead. */
extern char after_test_ptrval[];

/** Called from test_all(). Runs an array of test cases, that should be
    terminated with TEST_END */
void test_file(const char *filename, const TestFunctionInfo *tests);


/* =======================================================================
   ||||||||||||||||||||||  Definition of Test Cases  |||||||||||||||||||||
   ======================================================================= */

/** Test function info in test source files */
#define TEST_INFO(func) { #func, &(func) },
/** Function to run before each test case */
#define TEST_BEFORE(func) { before_test_ptrval, &(func) },
/** Function to run after each test case */
#define TEST_AFTER(func) { after_test_ptrval, &(func) },
/** Sentinel value at end of list of tests */
#define TEST_END { NULL, NULL }


/* =======================================================================
   ||||||||||||||||||  Assertion and Printing Functions  |||||||||||||||||
   ======================================================================= */

/** Like printf, but respects the "quiet" variable */
void tprintf(const char *format, ...);

/** Reports error at the given filename/line, and aborts the test.
    Use the tfail() macro instead to report at the current line. */
UNITTEST_NORETURN void tfail_(const char *filename, int line, const char *msg);
/** Reports error at the given filename/line, but continues.
    Use the tsoftfail() macro instead to report at the current line. */
int tsoftfail_(const char *filename, int line, const char *msg, int ok);

/** Evaluates the expression, and aborts the test with an error if false */
#define tassert(expr) do { \
        if (!(expr)) { tfail_(__FILE__, __LINE__, #expr); } \
    } while (0)
/** Evaluates the expression, and reports an error (but continues) if false */
#define tsoftassert(expr) \
    ((expr) || (tsoftfail_(__FILE__, __LINE__, #expr, 0), 0))

/** Like tassert(), but allows the source line to be overridden */
#define tassert_line(line, expr) do { \
        if (!(expr)) { tfail_(__FILE__, line, #expr); } \
    } while (0)
/** Like tsoftassert(), but allows the source line to be overridden */
#define tsoftassert_line(line, expr) \
    ((expr) || (tsoftfail_(__FILE__, line, #expr, 0), 0))

/** Aborts the test with an error message */
#define tfail(message) tfail_(__FILE__, __LINE__, message)
/** Reports an error, but allows the test to continue */
#define tsoftfail(message) tsoftfail_(__FILE__, __LINE__, message, 0)


/* =======================================================================
   |||||||||||||||||||||||||||  Configuration  |||||||||||||||||||||||||||
   ======================================================================= */

/**
 * Whether to disable printing of error messages / tprintf.
 *
 * This is set by the -q option.
 */
extern int quiet;

/**
 * Whether to enable printing of the name of the running test case.
 *
 * This is set by the -v option.
 */
extern int verbose;

/**
 * Ignore test results, just check that there is no crash.
 * If 1: Don't print errors, don't run TEST_AFTER functions.
 *
 * This is useful for testing out-of-memory handling (which will certainly
 * generate error, but should still not crash).
 */
extern int crash_test_only;


/* =======================================================================
   |||||||||||||||||||||  Project-Defined Functions  |||||||||||||||||||||
   ======================================================================= */

/**
 * This function MUST be defined, and is run once per test case.
 *
 * In the most simple case it can be a one-liner that just calls run_test().
 *
 * Optionally, it can iterate through test configurations and perform
 * multiple calls to run_test(), and/or perform additional before-test or
 * after-test code.
 */
void test_handler(void);

/**
 * This function MUST be defined, and is run when printing an error.
 *
 * In the most simple case it can be a one-liner that just returns an
 * empty string.
 *
 * Optionally, it can return some form of identifier for the current
 * test configuration.
 */
const char *error_extra_text(void);

/**
 * This function MUST be defined, and is run when printing the command
 * usage / help text.
 *
 * It should return NULL if there is no extra help text.
 *
 * If non-NULL, it should have lines shorter than 80 characters and
 * a newline "\n" at the end of each line.
 */
const char *help_extra_text(void);

/**
 * This function MUST be defined, and is run when printing test names
 * in verbose mode.
 *
 * It should return NULL if there is no extra text.
 */
const char *verbose_extra_text(void);


/* =======================================================================
   ||||||||||||  Functions that should be called from project  |||||||||||
   ======================================================================= */

/**
 * Must be called by test_handler() to run the test (possibly multiple times).
 * (If not called, the test will appear to run, while it in fact gets skipped)
 */
void run_test(void);

/**
 * Processes command-line arguments, and then runs specified tests.
 * Should be called from main().
 */
int test_main(int argc, char **argv);

/* =======================================================================
   ||||||||||||||  Information about the Current Test Case  ||||||||||||||
   ======================================================================= */

/**
 * File name of the current test file (without path)
 */
extern const char *current_file;

/**
 * Name of the current test case/function.
 */
extern const char *current_function;

/**
 * Number of errors that have occurred in the current test case.
 */
extern int errors_testcase;

/**
 * Whether the last test case was aborted (1) or ran to the end (0).
 */
extern int testcase_aborted;

/* =======================================================================
   |||||||||||||||||||||  Listing of All Test Cases  |||||||||||||||||||||
   ======================================================================= */

/*

    You should have a file called "alltests.h" which should contain a
    sequence of TEST_D macro calls, for each test file, for example:

        TEST_D(wheels)
        TEST_D(pedals)
        TEST_D(chain)

    Each test file should be named test_xyz.c, for example test_wheels.c.
    In each test file, please include unittest.h like this:

        #include "unittest.h"
        #include "alltests.h"

    In the main file, please include unittest.h like this:

        #define TEST_MAIN
        #include "unittest.h"

        #include "alltests.h"
        TESTCALLS_START
        #undef TEST_D
        #define TEST_D TEST_D2
        #include "alltests.h"
        TESTCALLS_END

*/

void test_all(void);
#ifndef TEST_MAIN
#    define TEST_D(name) extern const TestFunctionInfo tests_##name[];
#else
#    define TESTCALLS_START void test_all(void) {
/* This is nicer, but gives warnings */
/*#    define TEST_D(name) { \
            extern const TestFunctionInfo tests_##name[]; \
            test_file(#name ".c", tests_##name); \
        }*/
#    define TEST_D1(name) extern const TestFunctionInfo tests_##name[];
#    define TEST_D2(name) test_file(#name ".c", tests_##name);
#    define TESTCALLS_END }
#    define TEST_D TEST_D1
#endif


#endif