Debian Codegraph @

test/lib/munit.h

raw
1 /* µnit Testing Framework
2 * Copyright (c) 2013-2017 Evan Nemerson <[email protected]>
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #if !defined(MUNIT_H)
26 #define MUNIT_H
27
28 #include <stdarg.h>
29 #include <stdlib.h>
30
31 #define MUNIT_VERSION(major, minor, revision) \
32 (((major) << 16) | ((minor) << 8) | (revision))
33
34 #define MUNIT_CURRENT_VERSION MUNIT_VERSION(0, 4, 1)
35
36 #if defined(_MSC_VER) && (_MSC_VER < 1600)
37 # define munit_int8_t __int8
38 # define munit_uint8_t unsigned __int8
39 # define munit_int16_t __int16
40 # define munit_uint16_t unsigned __int16
41 # define munit_int32_t __int32
42 # define munit_uint32_t unsigned __int32
43 # define munit_int64_t __int64
44 # define munit_uint64_t unsigned __int64
45 #else
46 # include <stdint.h>
47 # define munit_int8_t int8_t
48 # define munit_uint8_t uint8_t
49 # define munit_int16_t int16_t
50 # define munit_uint16_t uint16_t
51 # define munit_int32_t int32_t
52 # define munit_uint32_t uint32_t
53 # define munit_int64_t int64_t
54 # define munit_uint64_t uint64_t
55 #endif
56
57 #if defined(_MSC_VER) && (_MSC_VER < 1800)
58 # if !defined(PRIi8)
59 # define PRIi8 "i"
60 # endif
61 # if !defined(PRIi16)
62 # define PRIi16 "i"
63 # endif
64 # if !defined(PRIi32)
65 # define PRIi32 "i"
66 # endif
67 # if !defined(PRIi64)
68 # define PRIi64 "I64i"
69 # endif
70 # if !defined(PRId8)
71 # define PRId8 "d"
72 # endif
73 # if !defined(PRId16)
74 # define PRId16 "d"
75 # endif
76 # if !defined(PRId32)
77 # define PRId32 "d"
78 # endif
79 # if !defined(PRId64)
80 # define PRId64 "I64d"
81 # endif
82 # if !defined(PRIx8)
83 # define PRIx8 "x"
84 # endif
85 # if !defined(PRIx16)
86 # define PRIx16 "x"
87 # endif
88 # if !defined(PRIx32)
89 # define PRIx32 "x"
90 # endif
91 # if !defined(PRIx64)
92 # define PRIx64 "I64x"
93 # endif
94 # if !defined(PRIu8)
95 # define PRIu8 "u"
96 # endif
97 # if !defined(PRIu16)
98 # define PRIu16 "u"
99 # endif
100 # if !defined(PRIu32)
101 # define PRIu32 "u"
102 # endif
103 # if !defined(PRIu64)
104 # define PRIu64 "I64u"
105 # endif
106 # if !defined(bool)
107 # define bool int
108 # endif
109 # if !defined(true)
110 # define true (!0)
111 # endif
112 # if !defined(false)
113 # define false (!!0)
114 # endif
115 #else
116 # include <inttypes.h>
117 # include <stdbool.h>
118 #endif
119
120 #if defined(__cplusplus)
121 extern "C" {
122 #endif
123
124 #if defined(__GNUC__)
125 # define MUNIT_LIKELY(expr) (__builtin_expect ((expr), 1))
126 # define MUNIT_UNLIKELY(expr) (__builtin_expect ((expr), 0))
127 # define MUNIT_UNUSED __attribute__((__unused__))
128 #else
129 # define MUNIT_LIKELY(expr) (expr)
130 # define MUNIT_UNLIKELY(expr) (expr)
131 # define MUNIT_UNUSED
132 #endif
133
134 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__PGI)
135 # define MUNIT_ARRAY_PARAM(name) name
136 #else
137 # define MUNIT_ARRAY_PARAM(name)
138 #endif
139
140 #if !defined(_WIN32)
141 # define MUNIT_SIZE_MODIFIER "z"
142 # define MUNIT_CHAR_MODIFIER "hh"
143 # define MUNIT_SHORT_MODIFIER "h"
144 #else
145 # if defined(_M_X64) || defined(__amd64__)
146 # define MUNIT_SIZE_MODIFIER "I64"
147 # else
148 # define MUNIT_SIZE_MODIFIER ""
149 # endif
150 # define MUNIT_CHAR_MODIFIER ""
151 # define MUNIT_SHORT_MODIFIER ""
152 #endif
153
154 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
155 # define MUNIT_NO_RETURN _Noreturn
156 #elif defined(__GNUC__)
157 # define MUNIT_NO_RETURN __attribute__((__noreturn__))
158 #elif defined(_MSC_VER)
159 # define MUNIT_NO_RETURN __declspec(noreturn)
160 #else
161 # define MUNIT_NO_RETURN
162 #endif
163
164 #if defined(_MSC_VER) && (_MSC_VER >= 1500)
165 # define MUNIT__PUSH_DISABLE_MSVC_C4127 __pragma(warning(push)) __pragma(warning(disable:4127))
166 # define MUNIT__POP_DISABLE_MSVC_C4127 __pragma(warning(pop))
167 #else
168 # define MUNIT__PUSH_DISABLE_MSVC_C4127
169 # define MUNIT__POP_DISABLE_MSVC_C4127
170 #endif
171
172 typedef enum {
173 MUNIT_LOG_DEBUG,
174 MUNIT_LOG_INFO,
175 MUNIT_LOG_WARNING,
176 MUNIT_LOG_ERROR
177 } MunitLogLevel;
178
179 #if defined(__GNUC__) && !defined(__MINGW32__)
180 # define MUNIT_PRINTF(string_index, first_to_check) __attribute__((format (printf, string_index, first_to_check)))
181 #else
182 # define MUNIT_PRINTF(string_index, first_to_check)
183 #endif
184
185 MUNIT_PRINTF(4, 5)
186 void munit_logf_ex(MunitLogLevel level, const char* filename, int line, const char* format, ...);
187
188 #define munit_logf(level, format, ...) \
189 munit_logf_ex(level, __FILE__, __LINE__, format, __VA_ARGS__)
190
191 #define munit_log(level, msg) \
192 munit_logf(level, "%s", msg)
193
194 MUNIT_NO_RETURN
195 MUNIT_PRINTF(3, 4)
196 void munit_errorf_ex(const char* filename, int line, const char* format, ...);
197
198 #define munit_errorf(format, ...) \
199 munit_errorf_ex(__FILE__, __LINE__, format, __VA_ARGS__)
200
201 #define munit_error(msg) \
202 munit_errorf("%s", msg)
203
204 #define munit_assert(expr) \
205 do { \
206 if (!MUNIT_LIKELY(expr)) { \
207 munit_error("assertion failed: " #expr); \
208 } \
209 MUNIT__PUSH_DISABLE_MSVC_C4127 \
210 } while (0) \
211 MUNIT__POP_DISABLE_MSVC_C4127
212
213 #define munit_assert_true(expr) \
214 do { \
215 if (!MUNIT_LIKELY(expr)) { \
216 munit_error("assertion failed: " #expr " is not true"); \
217 } \
218 MUNIT__PUSH_DISABLE_MSVC_C4127 \
219 } while (0) \
220 MUNIT__POP_DISABLE_MSVC_C4127
221
222 #define munit_assert_false(expr) \
223 do { \
224 if (!MUNIT_LIKELY(!(expr))) { \
225 munit_error("assertion failed: " #expr " is not false"); \
226 } \
227 MUNIT__PUSH_DISABLE_MSVC_C4127 \
228 } while (0) \
229 MUNIT__POP_DISABLE_MSVC_C4127
230
231 #define munit_assert_type_full(prefix, suffix, T, fmt, a, op, b) \
232 do { \
233 T munit_tmp_a_ = (a); \
234 T munit_tmp_b_ = (b); \
235 if (!(munit_tmp_a_ op munit_tmp_b_)) { \
236 munit_errorf("assertion failed: %s %s %s (" prefix "%" fmt suffix " %s " prefix "%" fmt suffix ")", \
237 #a, #op, #b, munit_tmp_a_, #op, munit_tmp_b_); \
238 } \
239 MUNIT__PUSH_DISABLE_MSVC_C4127 \
240 } while (0) \
241 MUNIT__POP_DISABLE_MSVC_C4127
242
243 #define munit_assert_type(T, fmt, a, op, b) \
244 munit_assert_type_full("", "", T, fmt, a, op, b)
245
246 #define munit_assert_char(a, op, b) \
247 munit_assert_type_full("'\\x", "'", char, "02" MUNIT_CHAR_MODIFIER "x", a, op, b)
248 #define munit_assert_uchar(a, op, b) \
249 munit_assert_type_full("'\\x", "'", unsigned char, "02" MUNIT_CHAR_MODIFIER "x", a, op, b)
250 #define munit_assert_short(a, op, b) \
251 munit_assert_type(short, MUNIT_SHORT_MODIFIER "d", a, op, b)
252 #define munit_assert_ushort(a, op, b) \
253 munit_assert_type(unsigned short, MUNIT_SHORT_MODIFIER "u", a, op, b)
254 #define munit_assert_int(a, op, b) \
255 munit_assert_type(int, "d", a, op, b)
256 #define munit_assert_uint(a, op, b) \
257 munit_assert_type(unsigned int, "u", a, op, b)
258 #define munit_assert_long(a, op, b) \
259 munit_assert_type(long int, "ld", a, op, b)
260 #define munit_assert_ulong(a, op, b) \
261 munit_assert_type(unsigned long int, "lu", a, op, b)
262 #define munit_assert_llong(a, op, b) \
263 munit_assert_type(long long int, "lld", a, op, b)
264 #define munit_assert_ullong(a, op, b) \
265 munit_assert_type(unsigned long long int, "llu", a, op, b)
266
267 #define munit_assert_size(a, op, b) \
268 munit_assert_type(size_t, MUNIT_SIZE_MODIFIER "u", a, op, b)
269
270 #define munit_assert_float(a, op, b) \
271 munit_assert_type(float, "f", a, op, b)
272 #define munit_assert_double(a, op, b) \
273 munit_assert_type(double, "g", a, op, b)
274 #define munit_assert_ptr(a, op, b) \
275 munit_assert_type(const void*, "p", a, op, b)
276
277 #define munit_assert_int8(a, op, b) \
278 munit_assert_type(munit_int8_t, PRIi8, a, op, b)
279 #define munit_assert_uint8(a, op, b) \
280 munit_assert_type(munit_uint8_t, PRIu8, a, op, b)
281 #define munit_assert_int16(a, op, b) \
282 munit_assert_type(munit_int16_t, PRIi16, a, op, b)
283 #define munit_assert_uint16(a, op, b) \
284 munit_assert_type(munit_uint16_t, PRIu16, a, op, b)
285 #define munit_assert_int32(a, op, b) \
286 munit_assert_type(munit_int32_t, PRIi32, a, op, b)
287 #define munit_assert_uint32(a, op, b) \
288 munit_assert_type(munit_uint32_t, PRIu32, a, op, b)
289 #define munit_assert_int64(a, op, b) \
290 munit_assert_type(munit_int64_t, PRIi64, a, op, b)
291 #define munit_assert_uint64(a, op, b) \
292 munit_assert_type(munit_uint64_t, PRIu64, a, op, b)
293
294 #define munit_assert_double_equal(a, b, precision) \
295 do { \
296 const double munit_tmp_a_ = (a); \
297 const double munit_tmp_b_ = (b); \
298 const double munit_tmp_diff_ = ((munit_tmp_a_ - munit_tmp_b_) < 0) ? \
299 -(munit_tmp_a_ - munit_tmp_b_) : \
300 (munit_tmp_a_ - munit_tmp_b_); \
301 if (MUNIT_UNLIKELY(munit_tmp_diff_ > 1e-##precision)) { \
302 munit_errorf("assertion failed: %s == %s (%0." #precision "g == %0." #precision "g)", \
303 #a, #b, munit_tmp_a_, munit_tmp_b_); \
304 } \
305 MUNIT__PUSH_DISABLE_MSVC_C4127 \
306 } while (0) \
307 MUNIT__POP_DISABLE_MSVC_C4127
308
309 #include <string.h>
310 #define munit_assert_string_equal(a, b) \
311 do { \
312 const char* munit_tmp_a_ = a; \
313 const char* munit_tmp_b_ = b; \
314 if (MUNIT_UNLIKELY(strcmp(munit_tmp_a_, munit_tmp_b_) != 0)) { \
315 munit_errorf("assertion failed: string %s == %s (\"%s\" == \"%s\")", \
316 #a, #b, munit_tmp_a_, munit_tmp_b_); \
317 } \
318 MUNIT__PUSH_DISABLE_MSVC_C4127 \
319 } while (0) \
320 MUNIT__POP_DISABLE_MSVC_C4127
321
322 #define munit_assert_string_not_equal(a, b) \
323 do { \
324 const char* munit_tmp_a_ = a; \
325 const char* munit_tmp_b_ = b; \
326 if (MUNIT_UNLIKELY(strcmp(munit_tmp_a_, munit_tmp_b_) == 0)) { \
327 munit_errorf("assertion failed: string %s != %s (\"%s\" == \"%s\")", \
328 #a, #b, munit_tmp_a_, munit_tmp_b_); \
329 } \
330 MUNIT__PUSH_DISABLE_MSVC_C4127 \
331 } while (0) \
332 MUNIT__POP_DISABLE_MSVC_C4127
333
334 #define munit_assert_memory_equal(size, a, b) \
335 do { \
336 const unsigned char* munit_tmp_a_ = (const unsigned char*) (a); \
337 const unsigned char* munit_tmp_b_ = (const unsigned char*) (b); \
338 const size_t munit_tmp_size_ = (size); \
339 if (MUNIT_UNLIKELY(memcmp(munit_tmp_a_, munit_tmp_b_, munit_tmp_size_)) != 0) { \
340 size_t munit_tmp_pos_; \
341 for (munit_tmp_pos_ = 0 ; munit_tmp_pos_ < munit_tmp_size_ ; munit_tmp_pos_++) { \
342 if (munit_tmp_a_[munit_tmp_pos_] != munit_tmp_b_[munit_tmp_pos_]) { \
343 munit_errorf("assertion failed: memory %s == %s, at offset %" MUNIT_SIZE_MODIFIER "u", \
344 #a, #b, munit_tmp_pos_); \
345 break; \
346 } \
347 } \
348 } \
349 MUNIT__PUSH_DISABLE_MSVC_C4127 \
350 } while (0) \
351 MUNIT__POP_DISABLE_MSVC_C4127
352
353 #define munit_assert_memory_not_equal(size, a, b) \
354 do { \
355 const unsigned char* munit_tmp_a_ = (const unsigned char*) (a); \
356 const unsigned char* munit_tmp_b_ = (const unsigned char*) (b); \
357 const size_t munit_tmp_size_ = (size); \
358 if (MUNIT_UNLIKELY(memcmp(munit_tmp_a_, munit_tmp_b_, munit_tmp_size_)) == 0) { \
359 munit_errorf("assertion failed: memory %s != %s (%zu bytes)", \
360 #a, #b, munit_tmp_size_); \
361 } \
362 MUNIT__PUSH_DISABLE_MSVC_C4127 \
363 } while (0) \
364 MUNIT__POP_DISABLE_MSVC_C4127
365
366 #define munit_assert_ptr_equal(a, b) \
367 munit_assert_ptr(a, ==, b)
368 #define munit_assert_ptr_not_equal(a, b) \
369 munit_assert_ptr(a, !=, b)
370 #define munit_assert_null(ptr) \
371 munit_assert_ptr(ptr, ==, NULL)
372 #define munit_assert_not_null(ptr) \
373 munit_assert_ptr(ptr, !=, NULL)
374 #define munit_assert_ptr_null(ptr) \
375 munit_assert_ptr(ptr, ==, NULL)
376 #define munit_assert_ptr_not_null(ptr) \
377 munit_assert_ptr(ptr, !=, NULL)
378
379 /*** Memory allocation ***/
380
381 void* munit_malloc_ex(const char* filename, int line, size_t size);
382
383 #define munit_malloc(size) \
384 munit_malloc_ex(__FILE__, __LINE__, (size))
385
386 #define munit_new(type) \
387 ((type*) munit_malloc(sizeof(type)))
388
389 #define munit_calloc(nmemb, size) \
390 munit_malloc((nmemb) * (size))
391
392 #define munit_newa(type, nmemb) \
393 ((type*) munit_calloc((nmemb), sizeof(type)))
394
395 /*** Random number generation ***/
396
397 void munit_rand_seed(munit_uint32_t seed);
398 munit_uint32_t munit_rand_uint32(void);
399 int munit_rand_int_range(int min, int max);
400 double munit_rand_double(void);
401 void munit_rand_memory(size_t size, munit_uint8_t buffer[MUNIT_ARRAY_PARAM(size)]);
402
403 /*** Tests and Suites ***/
404
405 typedef enum {
406 /* Test successful */
407 MUNIT_OK,
408 /* Test failed */
409 MUNIT_FAIL,
410 /* Test was skipped */
411 MUNIT_SKIP,
412 /* Test failed due to circumstances not intended to be tested
413 * (things like network errors, invalid parameter value, failure to
414 * allocate memory in the test harness, etc.). */
415 MUNIT_ERROR
416 } MunitResult;
417
418 typedef struct {
419 char* name;
420 char** values;
421 } MunitParameterEnum;
422
423 typedef struct {
424 char* name;
425 char* value;
426 } MunitParameter;
427
428 const char* munit_parameters_get(const MunitParameter params[], const char* key);
429
430 typedef enum {
431 MUNIT_TEST_OPTION_NONE = 0,
432 MUNIT_TEST_OPTION_SINGLE_ITERATION = 1 << 0,
433 MUNIT_TEST_OPTION_TODO = 1 << 1
434 } MunitTestOptions;
435
436 typedef MunitResult (* MunitTestFunc)(const MunitParameter params[], void* user_data_or_fixture);
437 typedef void* (* MunitTestSetup)(const MunitParameter params[], void* user_data);
438 typedef void (* MunitTestTearDown)(void* fixture);
439
440 typedef struct {
441 char* name;
442 MunitTestFunc test;
443 MunitTestSetup setup;
444 MunitTestTearDown tear_down;
445 MunitTestOptions options;
446 MunitParameterEnum* parameters;
447 } MunitTest;
448
449 typedef enum {
450 MUNIT_SUITE_OPTION_NONE = 0
451 } MunitSuiteOptions;
452
453 typedef struct MunitSuite_ MunitSuite;
454
455 struct MunitSuite_ {
456 char* prefix;
457 MunitTest* tests;
458 MunitSuite* suites;
459 unsigned int iterations;
460 MunitSuiteOptions options;
461 };
462
463 int munit_suite_main(const MunitSuite* suite, void* user_data, int argc, char* const argv[MUNIT_ARRAY_PARAM(argc)]);
464
465 /* Note: I'm not very happy with this API; it's likely to change if I
466 * figure out something better. Suggestions welcome. */
467
468 typedef struct MunitArgument_ MunitArgument;
469
470 struct MunitArgument_ {
471 char* name;
472 bool (* parse_argument)(const MunitSuite* suite, void* user_data, int* arg, int argc, char* const argv[MUNIT_ARRAY_PARAM(argc)]);
473 void (* write_help)(const MunitArgument* argument, void* user_data);
474 };
475
476 int munit_suite_main_custom(const MunitSuite* suite,
477 void* user_data,
478 int argc, char* const argv[MUNIT_ARRAY_PARAM(argc)],
479 const MunitArgument arguments[]);
480
481 #if defined(MUNIT_ENABLE_ASSERT_ALIASES)
482
483 #define assert_true(expr) munit_assert_true(expr)
484 #define assert_false(expr) munit_assert_false(expr)
485 #define assert_char(a, op, b) munit_assert_char(a, op, b)
486 #define assert_uchar(a, op, b) munit_assert_uchar(a, op, b)
487 #define assert_short(a, op, b) munit_assert_short(a, op, b)
488 #define assert_ushort(a, op, b) munit_assert_ushort(a, op, b)
489 #define assert_int(a, op, b) munit_assert_int(a, op, b)
490 #define assert_uint(a, op, b) munit_assert_uint(a, op, b)
491 #define assert_long(a, op, b) munit_assert_long(a, op, b)
492 #define assert_ulong(a, op, b) munit_assert_ulong(a, op, b)
493 #define assert_llong(a, op, b) munit_assert_llong(a, op, b)
494 #define assert_ullong(a, op, b) munit_assert_ullong(a, op, b)
495 #define assert_size(a, op, b) munit_assert_size(a, op, b)
496 #define assert_float(a, op, b) munit_assert_float(a, op, b)
497 #define assert_double(a, op, b) munit_assert_double(a, op, b)
498 #define assert_ptr(a, op, b) munit_assert_ptr(a, op, b)
499
500 #define assert_int8(a, op, b) munit_assert_int8(a, op, b)
501 #define assert_uint8(a, op, b) munit_assert_uint8(a, op, b)
502 #define assert_int16(a, op, b) munit_assert_int16(a, op, b)
503 #define assert_uint16(a, op, b) munit_assert_uint16(a, op, b)
504 #define assert_int32(a, op, b) munit_assert_int32(a, op, b)
505 #define assert_uint32(a, op, b) munit_assert_uint32(a, op, b)
506 #define assert_int64(a, op, b) munit_assert_int64(a, op, b)
507 #define assert_uint64(a, op, b) munit_assert_uint64(a, op, b)
508
509 #define assert_double_equal(a, b, precision) munit_assert_double_equal(a, b, precision)
510 #define assert_string_equal(a, b) munit_assert_string_equal(a, b)
511 #define assert_string_not_equal(a, b) munit_assert_string_not_equal(a, b)
512 #define assert_memory_equal(size, a, b) munit_assert_memory_equal(size, a, b)
513 #define assert_memory_not_equal(size, a, b) munit_assert_memory_not_equal(size, a, b)
514 #define assert_ptr_equal(a, b) munit_assert_ptr_equal(a, b)
515 #define assert_ptr_not_equal(a, b) munit_assert_ptr_not_equal(a, b)
516 #define assert_ptr_null(ptr) munit_assert_null_equal(ptr)
517 #define assert_ptr_not_null(ptr) munit_assert_not_null(ptr)
518
519 #define assert_null(ptr) munit_assert_null(ptr)
520 #define assert_not_null(ptr) munit_assert_not_null(ptr)
521
522 #endif /* defined(MUNIT_ENABLE_ASSERT_ALIASES) */
523
524 #if defined(__cplusplus)
525 }
526 #endif
527
528 #endif /* !defined(MUNIT_H) */
529
530 #if defined(MUNIT_ENABLE_ASSERT_ALIASES)
531 # if defined(assert)
532 # undef assert
533 # endif
534 # define assert(expr) munit_assert(expr)
535 #endif