32#define MUNIT_VERSION(major, minor, revision) \
33 (((major) << 16) | ((minor) << 8) | (revision))
35#define MUNIT_CURRENT_VERSION MUNIT_VERSION(0, 4, 1)
37#if defined(_MSC_VER) && (_MSC_VER < 1600)
38# define munit_int8_t __int8
39# define munit_uint8_t unsigned __int8
40# define munit_int16_t __int16
41# define munit_uint16_t unsigned __int16
42# define munit_int32_t __int32
43# define munit_uint32_t unsigned __int32
44# define munit_int64_t __int64
45# define munit_uint64_t unsigned __int64
50# define munit_int8_t int8_t
51# define munit_uint8_t uint8_t
52# define munit_int16_t int16_t
53# define munit_uint16_t uint16_t
54# define munit_int32_t int32_t
55# define munit_uint32_t uint32_t
56# define munit_int64_t int64_t
57# define munit_uint64_t uint64_t
60#if defined(_MSC_VER) && (_MSC_VER < 1800)
107# define PRIu64 "I64u"
111# include <inttypes.h>
115#if !defined(munit_bool)
117# define munit_bool bool
118# elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
119# define munit_bool _Bool
121# define munit_bool int
125#if defined(__cplusplus)
130# define MUNIT_LIKELY(expr) (__builtin_expect ((expr), 1))
131# define MUNIT_UNLIKELY(expr) (__builtin_expect ((expr), 0))
132# define MUNIT_UNUSED __attribute__((__unused__))
134# define MUNIT_LIKELY(expr) (expr)
135# define MUNIT_UNLIKELY(expr) (expr)
139#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__PGI)
140# define MUNIT_ARRAY_PARAM(name) name
142# define MUNIT_ARRAY_PARAM(name)
146# define MUNIT_SIZE_MODIFIER "z"
147# define MUNIT_CHAR_MODIFIER "hh"
148# define MUNIT_SHORT_MODIFIER "h"
150# if defined(_M_X64) || defined(__amd64__)
151# define MUNIT_SIZE_MODIFIER "I64"
153# define MUNIT_SIZE_MODIFIER ""
155# define MUNIT_CHAR_MODIFIER ""
156# define MUNIT_SHORT_MODIFIER ""
159#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
160# define MUNIT_NO_RETURN _Noreturn
161#elif defined(__GNUC__)
162# define MUNIT_NO_RETURN __attribute__((__noreturn__))
163#elif defined(_MSC_VER)
164# define MUNIT_NO_RETURN __declspec(noreturn)
166# define MUNIT_NO_RETURN
169#if defined(_MSC_VER) && (_MSC_VER >= 1500)
170# define MUNIT_PUSH_DISABLE_MSVC_C4127_ __pragma(warning(push)) __pragma(warning(disable:4127))
171# define MUNIT_POP_DISABLE_MSVC_C4127_ __pragma(warning(pop))
173# define MUNIT_PUSH_DISABLE_MSVC_C4127_
174# define MUNIT_POP_DISABLE_MSVC_C4127_
184#if defined(__GNUC__) && !defined(__MINGW32__)
185# define MUNIT_PRINTF(string_index, first_to_check) __attribute__((format (printf, string_index, first_to_check)))
187# define MUNIT_PRINTF(string_index, first_to_check)
193#define munit_logf(level, format, ...) \
194 munit_logf_ex(level, __FILE__, __LINE__, format, __VA_ARGS__)
196#define munit_log(level, msg) \
197 munit_logf(level, "%s", msg)
201void
munit_errorf_ex(const
char *filename,
int line, const
char *format, ...);
203#define munit_errorf(format, ...) \
204 munit_errorf_ex(__FILE__, __LINE__, format, __VA_ARGS__)
206#define munit_error(msg) \
207 munit_errorf("%s", msg)
209#define munit_assert(expr) \
211 if (!MUNIT_LIKELY(expr)) { \
212 munit_error("assertion failed: " #expr); \
214 MUNIT_PUSH_DISABLE_MSVC_C4127_ \
216 MUNIT_POP_DISABLE_MSVC_C4127_
218#define munit_assert_true(expr) \
220 if (!MUNIT_LIKELY(expr)) { \
221 munit_error("assertion failed: " #expr " is not true"); \
223 MUNIT_PUSH_DISABLE_MSVC_C4127_ \
225 MUNIT_POP_DISABLE_MSVC_C4127_
227#define munit_assert_false(expr) \
229 if (!MUNIT_LIKELY(!(expr))) { \
230 munit_error("assertion failed: " #expr " is not false"); \
232 MUNIT_PUSH_DISABLE_MSVC_C4127_ \
234 MUNIT_POP_DISABLE_MSVC_C4127_
236#define munit_assert_type_full(prefix, suffix, T, fmt, a, op, b) \
238 T munit_tmp_a_ = (a); \
239 T munit_tmp_b_ = (b); \
240 if (!(munit_tmp_a_ op munit_tmp_b_)) { \
241 munit_errorf("assertion failed: %s %s %s (" prefix "%" fmt suffix " %s " prefix "%" fmt suffix ")", \
242 #a, #op, #b, munit_tmp_a_, #op, munit_tmp_b_); \
244 MUNIT_PUSH_DISABLE_MSVC_C4127_ \
246 MUNIT_POP_DISABLE_MSVC_C4127_
248#define munit_assert_type(T, fmt, a, op, b) \
249 munit_assert_type_full("", "", T, fmt, a, op, b)
251#define munit_assert_char(a, op, b) \
252 munit_assert_type_full("'\\x", "'", char, "02" MUNIT_CHAR_MODIFIER "x", a, op, b)
253#define munit_assert_uchar(a, op, b) \
254 munit_assert_type_full("'\\x", "'", unsigned char, "02" MUNIT_CHAR_MODIFIER "x", a, op, b)
255#define munit_assert_short(a, op, b) \
256 munit_assert_type(short, MUNIT_SHORT_MODIFIER "d", a, op, b)
257#define munit_assert_ushort(a, op, b) \
258 munit_assert_type(unsigned short, MUNIT_SHORT_MODIFIER "u", a, op, b)
259#define munit_assert_int(a, op, b) \
260 munit_assert_type(int, "d", a, op, b)
261#define munit_assert_uint(a, op, b) \
262 munit_assert_type(unsigned int, "u", a, op, b)
263#define munit_assert_long(a, op, b) \
264 munit_assert_type(long int, "ld", a, op, b)
265#define munit_assert_ulong(a, op, b) \
266 munit_assert_type(unsigned long int, "lu", a, op, b)
267#define munit_assert_llong(a, op, b) \
268 munit_assert_type(long long int, "lld", a, op, b)
269#define munit_assert_ullong(a, op, b) \
270 munit_assert_type(unsigned long long int, "llu", a, op, b)
272#define munit_assert_size(a, op, b) \
273 munit_assert_type(size_t, MUNIT_SIZE_MODIFIER "u", a, op, b)
275#define munit_assert_float(a, op, b) \
276 munit_assert_type(float, "f", a, op, b)
277#define munit_assert_double(a, op, b) \
278 munit_assert_type(double, "g", a, op, b)
279#define munit_assert_ptr(a, op, b) \
280 munit_assert_type(const void*, "p", a, op, b)
282#define munit_assert_int8(a, op, b) \
283 munit_assert_type(munit_int8_t, PRIi8, a, op, b)
284#define munit_assert_uint8(a, op, b) \
285 munit_assert_type(munit_uint8_t, PRIu8, a, op, b)
286#define munit_assert_int16(a, op, b) \
287 munit_assert_type(munit_int16_t, PRIi16, a, op, b)
288#define munit_assert_uint16(a, op, b) \
289 munit_assert_type(munit_uint16_t, PRIu16, a, op, b)
290#define munit_assert_int32(a, op, b) \
291 munit_assert_type(munit_int32_t, PRIi32, a, op, b)
292#define munit_assert_uint32(a, op, b) \
293 munit_assert_type(munit_uint32_t, PRIu32, a, op, b)
294#define munit_assert_int64(a, op, b) \
295 munit_assert_type(munit_int64_t, PRIi64, a, op, b)
296#define munit_assert_uint64(a, op, b) \
297 munit_assert_type(munit_uint64_t, PRIu64, a, op, b)
299#define munit_assert_double_equal(a, b, precision) \
301 const double munit_tmp_a_ = (a); \
302 const double munit_tmp_b_ = (b); \
303 const double munit_tmp_diff_ = ((munit_tmp_a_ - munit_tmp_b_) < 0) ? \
304 -(munit_tmp_a_ - munit_tmp_b_) : \
305 (munit_tmp_a_ - munit_tmp_b_); \
306 if (MUNIT_UNLIKELY(munit_tmp_diff_ > 1e-##precision)) { \
307 munit_errorf("assertion failed: %s == %s (%0." #precision "g == %0." #precision "g)", \
308 #a, #b, munit_tmp_a_, munit_tmp_b_); \
310 MUNIT_PUSH_DISABLE_MSVC_C4127_ \
312 MUNIT_POP_DISABLE_MSVC_C4127_
316#define munit_assert_string_equal(a, b) \
318 const char* munit_tmp_a_ = a; \
319 const char* munit_tmp_b_ = b; \
320 if (MUNIT_UNLIKELY(strcmp(munit_tmp_a_, munit_tmp_b_) != 0)) { \
321 munit_errorf("assertion failed: string %s == %s (\"%s\" == \"%s\")", \
322 #a, #b, munit_tmp_a_, munit_tmp_b_); \
324 MUNIT_PUSH_DISABLE_MSVC_C4127_ \
326 MUNIT_POP_DISABLE_MSVC_C4127_
328#define munit_assert_string_not_equal(a, b) \
330 const char* munit_tmp_a_ = a; \
331 const char* munit_tmp_b_ = b; \
332 if (MUNIT_UNLIKELY(strcmp(munit_tmp_a_, munit_tmp_b_) == 0)) { \
333 munit_errorf("assertion failed: string %s != %s (\"%s\" == \"%s\")", \
334 #a, #b, munit_tmp_a_, munit_tmp_b_); \
336 MUNIT_PUSH_DISABLE_MSVC_C4127_ \
338 MUNIT_POP_DISABLE_MSVC_C4127_
340#define munit_assert_memory_equal(size, a, b) \
342 const unsigned char* munit_tmp_a_ = (const unsigned char*) (a); \
343 const unsigned char* munit_tmp_b_ = (const unsigned char*) (b); \
344 const size_t munit_tmp_size_ = (size); \
345 if (MUNIT_UNLIKELY(memcmp(munit_tmp_a_, munit_tmp_b_, munit_tmp_size_)) != 0) { \
346 size_t munit_tmp_pos_; \
347 for (munit_tmp_pos_ = 0 ; munit_tmp_pos_ < munit_tmp_size_ ; munit_tmp_pos_++) { \
348 if (munit_tmp_a_[munit_tmp_pos_] != munit_tmp_b_[munit_tmp_pos_]) { \
349 munit_errorf("assertion failed: memory %s == %s, at offset %" MUNIT_SIZE_MODIFIER "u", \
350 #a, #b, munit_tmp_pos_); \
355 MUNIT_PUSH_DISABLE_MSVC_C4127_ \
357 MUNIT_POP_DISABLE_MSVC_C4127_
359#define munit_assert_memory_not_equal(size, a, b) \
361 const unsigned char* munit_tmp_a_ = (const unsigned char*) (a); \
362 const unsigned char* munit_tmp_b_ = (const unsigned char*) (b); \
363 const size_t munit_tmp_size_ = (size); \
364 if (MUNIT_UNLIKELY(memcmp(munit_tmp_a_, munit_tmp_b_, munit_tmp_size_)) == 0) { \
365 munit_errorf("assertion failed: memory %s != %s (%zu bytes)", \
366 #a, #b, munit_tmp_size_); \
368 MUNIT_PUSH_DISABLE_MSVC_C4127_ \
370 MUNIT_POP_DISABLE_MSVC_C4127_
372#define munit_assert_ptr_equal(a, b) \
373 munit_assert_ptr(a, ==, b)
374#define munit_assert_ptr_not_equal(a, b) \
375 munit_assert_ptr(a, !=, b)
376#define munit_assert_null(ptr) \
377 munit_assert_ptr(ptr, ==, NULL)
378#define munit_assert_not_null(ptr) \
379 munit_assert_ptr(ptr, !=, NULL)
380#define munit_assert_ptr_null(ptr) \
381 munit_assert_ptr(ptr, ==, NULL)
382#define munit_assert_ptr_not_null(ptr) \
383 munit_assert_ptr(ptr, !=, NULL)
389#define munit_malloc(size) \
390 munit_malloc_ex(__FILE__, __LINE__, (size))
392#define munit_new(type) \
393 ((type*) munit_malloc(sizeof(type)))
395#define munit_calloc(nmemb, size) \
396 munit_malloc((nmemb) * (size))
398#define munit_newa(type, nmemb) \
399 ((type*) munit_calloc((nmemb), sizeof(type)))
496#if defined(MUNIT_ENABLE_ASSERT_ALIASES)
498#define assert_true(expr) munit_assert_true(expr)
499#define assert_false(expr) munit_assert_false(expr)
500#define assert_char(a, op, b) munit_assert_char(a, op, b)
501#define assert_uchar(a, op, b) munit_assert_uchar(a, op, b)
502#define assert_short(a, op, b) munit_assert_short(a, op, b)
503#define assert_ushort(a, op, b) munit_assert_ushort(a, op, b)
504#define assert_int(a, op, b) munit_assert_int(a, op, b)
505#define assert_uint(a, op, b) munit_assert_uint(a, op, b)
506#define assert_long(a, op, b) munit_assert_long(a, op, b)
507#define assert_ulong(a, op, b) munit_assert_ulong(a, op, b)
508#define assert_llong(a, op, b) munit_assert_llong(a, op, b)
509#define assert_ullong(a, op, b) munit_assert_ullong(a, op, b)
510#define assert_size(a, op, b) munit_assert_size(a, op, b)
511#define assert_float(a, op, b) munit_assert_float(a, op, b)
512#define assert_double(a, op, b) munit_assert_double(a, op, b)
513#define assert_ptr(a, op, b) munit_assert_ptr(a, op, b)
515#define assert_int8(a, op, b) munit_assert_int8(a, op, b)
516#define assert_uint8(a, op, b) munit_assert_uint8(a, op, b)
517#define assert_int16(a, op, b) munit_assert_int16(a, op, b)
518#define assert_uint16(a, op, b) munit_assert_uint16(a, op, b)
519#define assert_int32(a, op, b) munit_assert_int32(a, op, b)
520#define assert_uint32(a, op, b) munit_assert_uint32(a, op, b)
521#define assert_int64(a, op, b) munit_assert_int64(a, op, b)
522#define assert_uint64(a, op, b) munit_assert_uint64(a, op, b)
524#define assert_double_equal(a, b, precision) munit_assert_double_equal(a, b, precision)
525#define assert_string_equal(a, b) munit_assert_string_equal(a, b)
526#define assert_string_not_equal(a, b) munit_assert_string_not_equal(a, b)
527#define assert_memory_equal(size, a, b) munit_assert_memory_equal(size, a, b)
528#define assert_memory_not_equal(size, a, b) munit_assert_memory_not_equal(size, a, b)
529#define assert_ptr_equal(a, b) munit_assert_ptr_equal(a, b)
530#define assert_ptr_not_equal(a, b) munit_assert_ptr_not_equal(a, b)
531#define assert_ptr_null(ptr) munit_assert_null_equal(ptr)
532#define assert_ptr_not_null(ptr) munit_assert_not_null(ptr)
534#define assert_null(ptr) munit_assert_null(ptr)
535#define assert_not_null(ptr) munit_assert_not_null(ptr)
539#if defined(__cplusplus)
545#if defined(MUNIT_ENABLE_ASSERT_ALIASES)
549# define assert(expr) munit_assert(expr)
void munit_errorf_ex(const char *filename, int line, const char *format,...)
void munit_logf_ex(MunitLogLevel level, const char *filename, int line, const char *format,...)
void(* MunitTestTearDown)(void *fixture)
int munit_rand_int_range(int min, int max)
struct MunitSuite_ MunitSuite
#define MUNIT_ARRAY_PARAM(name)
MunitResult(* MunitTestFunc)(const MunitParameter params[], void *user_data_or_fixture)
int munit_suite_main_custom(const MunitSuite *suite, void *user_data, int argc, char *const argv[MUNIT_ARRAY_PARAM(argc+1)], const MunitArgument arguments[])
void munit_rand_seed(munit_uint32_t seed)
@ MUNIT_TEST_OPTION_SINGLE_ITERATION
void munit_rand_memory(size_t size, munit_uint8_t buffer[MUNIT_ARRAY_PARAM(size)])
int munit_suite_main(const MunitSuite *suite, void *user_data, int argc, char *const argv[MUNIT_ARRAY_PARAM(argc+1)])
struct MunitArgument_ MunitArgument
munit_uint32_t munit_rand_uint32(void)
double munit_rand_double(void)
void * munit_malloc_ex(const char *filename, int line, size_t size)
void *(* MunitTestSetup)(const MunitParameter params[], void *user_data)
#define MUNIT_PRINTF(string_index, first_to_check)
@ MUNIT_SUITE_OPTION_NONE
const char * munit_parameters_get(const MunitParameter params[], const char *key)
munit_bool(* parse_argument)(const MunitSuite *suite, void *user_data, int *arg, int argc, char *const argv[MUNIT_ARRAY_PARAM(argc+1)])
void(* write_help)(const MunitArgument *argument, void *user_data)
MunitSuiteOptions options
MunitTestTearDown tear_down
MunitParameterEnum * parameters