Path: blob/main/sys/contrib/openzfs/tests/unit/munit.h
289174 views
// SPDX-License-Identifier: MIT1/* µnit Testing Framework2* Copyright (c) 2013-2017 Evan Nemerson <[email protected]>3*4* Permission is hereby granted, free of charge, to any person5* obtaining a copy of this software and associated documentation6* files (the "Software"), to deal in the Software without7* restriction, including without limitation the rights to use, copy,8* modify, merge, publish, distribute, sublicense, and/or sell copies9* of the Software, and to permit persons to whom the Software is10* furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be13* 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 OF17* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND18* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS19* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN20* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN21* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22* SOFTWARE.23*/2425#ifndef MUNIT_H26#define MUNIT_H2728#include <stdarg.h>29#include <stdlib.h>30#include <stdio.h>31#include <stddef.h>3233#define MUNIT_VERSION(major, minor, revision) \34(((major) << 16) | ((minor) << 8) | (revision))3536#define MUNIT_CURRENT_VERSION MUNIT_VERSION(0, 4, 1)3738#if defined(_MSC_VER) && (_MSC_VER < 1600)39# define munit_int8_t __int840# define munit_uint8_t unsigned __int841# define munit_int16_t __int1642# define munit_uint16_t unsigned __int1643# define munit_int32_t __int3244# define munit_uint32_t unsigned __int3245# define munit_int64_t __int6446# define munit_uint64_t unsigned __int6447#else48# include <stdint.h>49# define munit_int8_t int8_t50# define munit_uint8_t uint8_t51# define munit_int16_t int16_t52# define munit_uint16_t uint16_t53# define munit_int32_t int32_t54# define munit_uint32_t uint32_t55# define munit_int64_t int64_t56# define munit_uint64_t uint64_t57#endif5859#if defined(_MSC_VER) && (_MSC_VER < 1800)60# if !defined(PRIi8)61# define PRIi8 "i"62# endif63# if !defined(PRIi16)64# define PRIi16 "i"65# endif66# if !defined(PRIi32)67# define PRIi32 "i"68# endif69# if !defined(PRIi64)70# define PRIi64 "I64i"71# endif72# if !defined(PRId8)73# define PRId8 "d"74# endif75# if !defined(PRId16)76# define PRId16 "d"77# endif78# if !defined(PRId32)79# define PRId32 "d"80# endif81# if !defined(PRId64)82# define PRId64 "I64d"83# endif84# if !defined(PRIx8)85# define PRIx8 "x"86# endif87# if !defined(PRIx16)88# define PRIx16 "x"89# endif90# if !defined(PRIx32)91# define PRIx32 "x"92# endif93# if !defined(PRIx64)94# define PRIx64 "I64x"95# endif96# if !defined(PRIu8)97# define PRIu8 "u"98# endif99# if !defined(PRIu16)100# define PRIu16 "u"101# endif102# if !defined(PRIu32)103# define PRIu32 "u"104# endif105# if !defined(PRIu64)106# define PRIu64 "I64u"107# endif108#else109# include <inttypes.h>110#endif111112#if !defined(munit_bool)113# if defined(bool)114# define munit_bool bool115# elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)116# define munit_bool _Bool117# else118# define munit_bool int119# endif120#endif121122#if defined(__cplusplus)123extern "C" {124#endif125126#if defined(__GNUC__)127# define MUNIT_LIKELY(expr) (__builtin_expect((expr), 1))128# define MUNIT_UNLIKELY(expr) (__builtin_expect((expr), 0))129# define MUNIT_UNUSED __attribute__((__unused__))130#else131# define MUNIT_LIKELY(expr) (expr)132# define MUNIT_UNLIKELY(expr) (expr)133# define MUNIT_UNUSED134#endif135136#if !defined(_WIN32)137# define MUNIT_SIZE_MODIFIER "z"138# define MUNIT_CHAR_MODIFIER "hh"139# define MUNIT_SHORT_MODIFIER "h"140#else141# if defined(_M_X64) || defined(__amd64__)142# define MUNIT_SIZE_MODIFIER "I64"143# else144# define MUNIT_SIZE_MODIFIER ""145# endif146# define MUNIT_CHAR_MODIFIER ""147# define MUNIT_SHORT_MODIFIER ""148#endif149150#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L151# define MUNIT_NO_RETURN _Noreturn152#elif defined(__GNUC__)153# define MUNIT_NO_RETURN __attribute__((__noreturn__))154#elif defined(_MSC_VER)155# define MUNIT_NO_RETURN __declspec(noreturn)156#else157# define MUNIT_NO_RETURN158#endif159160#if defined(_MSC_VER) && (_MSC_VER >= 1500)161# define MUNIT_PUSH_DISABLE_MSVC_C4127_ \162__pragma(warning(push)) __pragma(warning(disable : 4127))163# define MUNIT_POP_DISABLE_MSVC_C4127_ __pragma(warning(pop))164#else165# define MUNIT_PUSH_DISABLE_MSVC_C4127_166# define MUNIT_POP_DISABLE_MSVC_C4127_167#endif168169typedef enum {170MUNIT_LOG_DEBUG,171MUNIT_LOG_INFO,172MUNIT_LOG_WARNING,173MUNIT_LOG_ERROR174} MunitLogLevel;175176#if defined(__GNUC__) && !defined(__MINGW32__)177# define MUNIT_PRINTF(string_index, first_to_check) \178__attribute__((format(printf, string_index, first_to_check)))179#else180# define MUNIT_PRINTF(string_index, first_to_check)181#endif182183MUNIT_PRINTF(4, 5)184void munit_logf_ex(MunitLogLevel level, const char *filename, int line,185const char *format, ...);186187#define munit_logf(level, format, ...) \188munit_logf_ex(level, __FILE__, __LINE__, format, __VA_ARGS__)189190#define munit_log(level, msg) munit_logf(level, "%s", msg)191192MUNIT_NO_RETURN193MUNIT_PRINTF(3, 4)194void munit_errorf_ex(const char *filename, int line, const char *format, ...);195196#define munit_errorf(format, ...) \197munit_errorf_ex(__FILE__, __LINE__, format, __VA_ARGS__)198199#define munit_error(msg) munit_errorf("%s", msg)200201#define munit_assert(expr) \202do { \203if (!MUNIT_LIKELY(expr)) { \204munit_error("assertion failed: " #expr); \205} \206MUNIT_PUSH_DISABLE_MSVC_C4127_ \207} while (0) MUNIT_POP_DISABLE_MSVC_C4127_208209#define munit_assert_true(expr) \210do { \211if (!MUNIT_LIKELY(expr)) { \212munit_error("assertion failed: " #expr " is not true"); \213} \214MUNIT_PUSH_DISABLE_MSVC_C4127_ \215} while (0) MUNIT_POP_DISABLE_MSVC_C4127_216217#define munit_assert_false(expr) \218do { \219if (!MUNIT_LIKELY(!(expr))) { \220munit_error("assertion failed: " #expr " is not false"); \221} \222MUNIT_PUSH_DISABLE_MSVC_C4127_ \223} while (0) MUNIT_POP_DISABLE_MSVC_C4127_224225#define munit_assert_type_full(prefix, suffix, T, fmt, a, op, b) \226do { \227T munit_tmp_a_ = (a); \228T munit_tmp_b_ = (b); \229if (!(munit_tmp_a_ op munit_tmp_b_)) { \230munit_errorf("assertion failed: %s %s %s (" prefix "%" fmt suffix \231" %s " prefix "%" fmt suffix ")", \232#a, #op, #b, munit_tmp_a_, #op, munit_tmp_b_); \233} \234MUNIT_PUSH_DISABLE_MSVC_C4127_ \235} while (0) MUNIT_POP_DISABLE_MSVC_C4127_236237#define munit_assert_type(T, fmt, a, op, b) \238munit_assert_type_full("", "", T, fmt, a, op, b)239240#define munit_assert_char(a, op, b) \241munit_assert_type_full("'\\x", "'", char, "02" MUNIT_CHAR_MODIFIER "x", a, \242op, b)243#define munit_assert_uchar(a, op, b) \244munit_assert_type_full("'\\x", "'", unsigned char, \245"02" MUNIT_CHAR_MODIFIER "x", a, op, b)246#define munit_assert_short(a, op, b) \247munit_assert_type(short, MUNIT_SHORT_MODIFIER "d", a, op, b)248#define munit_assert_ushort(a, op, b) \249munit_assert_type(unsigned short, MUNIT_SHORT_MODIFIER "u", a, op, b)250#define munit_assert_int(a, op, b) munit_assert_type(int, "d", a, op, b)251#define munit_assert_uint(a, op, b) \252munit_assert_type(unsigned int, "u", a, op, b)253#define munit_assert_long(a, op, b) munit_assert_type(long int, "ld", a, op, b)254#define munit_assert_ulong(a, op, b) \255munit_assert_type(unsigned long int, "lu", a, op, b)256#define munit_assert_llong(a, op, b) \257munit_assert_type(long long int, "lld", a, op, b)258#define munit_assert_ullong(a, op, b) \259munit_assert_type(unsigned long long int, "llu", a, op, b)260261#define munit_assert_size(a, op, b) \262munit_assert_type(size_t, MUNIT_SIZE_MODIFIER "u", a, op, b)263#define munit_assert_ssize(a, op, b) \264munit_assert_type(ssize_t, MUNIT_SIZE_MODIFIER "d", a, op, b)265266#define munit_assert_float(a, op, b) munit_assert_type(float, "f", a, op, b)267#define munit_assert_double(a, op, b) munit_assert_type(double, "g", a, op, b)268#define munit_assert_ptr(a, op, b) \269munit_assert_type(const void *, "p", a, op, b)270271#define munit_assert_int8(a, op, b) \272munit_assert_type(munit_int8_t, PRIi8, a, op, b)273#define munit_assert_uint8(a, op, b) \274munit_assert_type(munit_uint8_t, PRIu8, a, op, b)275#define munit_assert_int16(a, op, b) \276munit_assert_type(munit_int16_t, PRIi16, a, op, b)277#define munit_assert_uint16(a, op, b) \278munit_assert_type(munit_uint16_t, PRIu16, a, op, b)279#define munit_assert_int32(a, op, b) \280munit_assert_type(munit_int32_t, PRIi32, a, op, b)281#define munit_assert_uint32(a, op, b) \282munit_assert_type(munit_uint32_t, PRIu32, a, op, b)283#define munit_assert_int64(a, op, b) \284munit_assert_type(munit_int64_t, PRIi64, a, op, b)285#define munit_assert_uint64(a, op, b) \286munit_assert_type(munit_uint64_t, PRIu64, a, op, b)287288#define munit_assert_ptrdiff(a, op, b) \289munit_assert_type(ptrdiff_t, "td", a, op, b)290291#define munit_assert_enum(T, a, op, b) munit_assert_type(T, "d", a, op, b)292293#define munit_assert_double_equal(a, b, precision) \294do { \295const double munit_tmp_a_ = (a); \296const double munit_tmp_b_ = (b); \297const double munit_tmp_diff_ = ((munit_tmp_a_ - munit_tmp_b_) < 0) \298? -(munit_tmp_a_ - munit_tmp_b_) \299: (munit_tmp_a_ - munit_tmp_b_); \300if (MUNIT_UNLIKELY(munit_tmp_diff_ > 1e-##precision)) { \301munit_errorf("assertion failed: %s == %s (%0." #precision \302"g == %0." #precision "g)", \303#a, #b, munit_tmp_a_, munit_tmp_b_); \304} \305MUNIT_PUSH_DISABLE_MSVC_C4127_ \306} while (0) MUNIT_POP_DISABLE_MSVC_C4127_307308#include <string.h>309#define munit_assert_string_equal(a, b) \310do { \311const char *munit_tmp_a_ = (a); \312const char *munit_tmp_b_ = (b); \313if (MUNIT_UNLIKELY(strcmp(munit_tmp_a_, munit_tmp_b_) != 0)) { \314munit_hexdump_diff(stderr, munit_tmp_a_, strlen(munit_tmp_a_), \315munit_tmp_b_, strlen(munit_tmp_b_)); \316munit_errorf("assertion failed: string %s == %s (\"%s\" == \"%s\")", #a, \317#b, munit_tmp_a_, munit_tmp_b_); \318} \319MUNIT_PUSH_DISABLE_MSVC_C4127_ \320} while (0) MUNIT_POP_DISABLE_MSVC_C4127_321322#define munit_assert_string_not_equal(a, b) \323do { \324const char *munit_tmp_a_ = (a); \325const char *munit_tmp_b_ = (b); \326if (MUNIT_UNLIKELY(strcmp(munit_tmp_a_, munit_tmp_b_) == 0)) { \327munit_errorf("assertion failed: string %s != %s (\"%s\" == \"%s\")", #a, \328#b, munit_tmp_a_, munit_tmp_b_); \329} \330MUNIT_PUSH_DISABLE_MSVC_C4127_ \331} while (0) MUNIT_POP_DISABLE_MSVC_C4127_332333#define munit_assert_memory_equal(size, a, b) \334do { \335const unsigned char *munit_tmp_a_ = (const unsigned char *)(a); \336const unsigned char *munit_tmp_b_ = (const unsigned char *)(b); \337const size_t munit_tmp_size_ = (size); \338if (MUNIT_UNLIKELY(memcmp(munit_tmp_a_, munit_tmp_b_, munit_tmp_size_)) != \3390) { \340size_t munit_tmp_pos_; \341for (munit_tmp_pos_ = 0; munit_tmp_pos_ < munit_tmp_size_; \342munit_tmp_pos_++) { \343if (munit_tmp_a_[munit_tmp_pos_] != munit_tmp_b_[munit_tmp_pos_]) { \344munit_hexdump_diff(stderr, munit_tmp_a_, size, munit_tmp_b_, size); \345munit_errorf("assertion failed: memory %s == %s, at offset " \346"%" MUNIT_SIZE_MODIFIER "u", \347#a, #b, munit_tmp_pos_); \348break; \349} \350} \351} \352MUNIT_PUSH_DISABLE_MSVC_C4127_ \353} while (0) MUNIT_POP_DISABLE_MSVC_C4127_354355#define munit_assert_memn_equal(a, a_size, b, b_size) \356do { \357const unsigned char *munit_tmp_a_ = (const unsigned char *)(a); \358const unsigned char *munit_tmp_b_ = (const unsigned char *)(b); \359const size_t munit_tmp_a_size_ = (a_size); \360const size_t munit_tmp_b_size_ = (b_size); \361if (MUNIT_UNLIKELY(munit_tmp_a_size_ != munit_tmp_b_size_) || \362MUNIT_UNLIKELY(munit_tmp_a_size_ && memcmp(munit_tmp_a_, munit_tmp_b_, \363munit_tmp_a_size_)) != 0) { \364munit_hexdump_diff(stderr, munit_tmp_a_, munit_tmp_a_size_, \365munit_tmp_b_, munit_tmp_b_size_); \366munit_errorf("assertion failed: memory %s == %s", #a, #b); \367} \368MUNIT_PUSH_DISABLE_MSVC_C4127_ \369} while (0) MUNIT_POP_DISABLE_MSVC_C4127_370371#define munit_assert_memory_not_equal(size, a, b) \372do { \373const unsigned char *munit_tmp_a_ = (const unsigned char *)(a); \374const unsigned char *munit_tmp_b_ = (const unsigned char *)(b); \375const size_t munit_tmp_size_ = (size); \376if (MUNIT_UNLIKELY(memcmp(munit_tmp_a_, munit_tmp_b_, munit_tmp_size_)) == \3770) { \378munit_errorf("assertion failed: memory %s != %s (%zu bytes)", #a, #b, \379munit_tmp_size_); \380} \381MUNIT_PUSH_DISABLE_MSVC_C4127_ \382} while (0) MUNIT_POP_DISABLE_MSVC_C4127_383384#define munit_assert_ptr_equal(a, b) munit_assert_ptr(a, ==, b)385#define munit_assert_ptr_not_equal(a, b) munit_assert_ptr(a, !=, b)386#define munit_assert_null(ptr) munit_assert_ptr(ptr, ==, NULL)387#define munit_assert_not_null(ptr) munit_assert_ptr(ptr, !=, NULL)388#define munit_assert_ptr_null(ptr) munit_assert_ptr(ptr, ==, NULL)389#define munit_assert_ptr_not_null(ptr) munit_assert_ptr(ptr, !=, NULL)390391/*** Memory allocation ***/392393void *munit_malloc_ex(const char *filename, int line, size_t size);394395#define munit_malloc(size) munit_malloc_ex(__FILE__, __LINE__, (size))396397#define munit_new(type) ((type *)munit_malloc(sizeof(type)))398399#define munit_calloc(nmemb, size) munit_malloc((nmemb) * (size))400401#define munit_newa(type, nmemb) ((type *)munit_calloc((nmemb), sizeof(type)))402403/*** Random number generation ***/404405void munit_rand_seed(munit_uint32_t seed);406munit_uint32_t munit_rand_uint32(void);407int munit_rand_int_range(int min, int max);408double munit_rand_double(void);409void munit_rand_memory(size_t size, munit_uint8_t *buffer);410411/*** Tests and Suites ***/412413typedef enum {414/* Test successful */415MUNIT_OK,416/* Test failed */417MUNIT_FAIL,418/* Test was skipped */419MUNIT_SKIP,420/* Test failed due to circumstances not intended to be tested421* (things like network errors, invalid parameter value, failure to422* allocate memory in the test harness, etc.). */423MUNIT_ERROR424} MunitResult;425426typedef struct {427char *name;428char **values;429} MunitParameterEnum;430431typedef struct {432char *name;433char *value;434} MunitParameter;435436const char *munit_parameters_get(const MunitParameter params[],437const char *key);438439typedef enum {440MUNIT_TEST_OPTION_NONE = 0,441MUNIT_TEST_OPTION_SINGLE_ITERATION = 1 << 0,442MUNIT_TEST_OPTION_TODO = 1 << 1443} MunitTestOptions;444445typedef MunitResult (*MunitTestFunc)(const MunitParameter params[],446void *user_data_or_fixture);447typedef void *(*MunitTestSetup)(const MunitParameter params[], void *user_data);448typedef void (*MunitTestTearDown)(void *fixture);449450typedef struct {451const char *name;452MunitTestFunc test;453MunitTestSetup setup;454MunitTestTearDown tear_down;455MunitTestOptions options;456MunitParameterEnum *parameters;457} MunitTest;458459typedef enum { MUNIT_SUITE_OPTION_NONE = 0 } MunitSuiteOptions;460461typedef struct MunitSuite_ MunitSuite;462463struct MunitSuite_ {464const char *prefix;465const MunitTest *tests;466const MunitSuite *suites;467unsigned int iterations;468MunitSuiteOptions options;469};470471int munit_suite_main(const MunitSuite *suite, void *user_data, int argc,472char *const *argv);473474/* Note: I'm not very happy with this API; it's likely to change if I475* figure out something better. Suggestions welcome. */476477typedef struct MunitArgument_ MunitArgument;478479struct MunitArgument_ {480char *name;481munit_bool (*parse_argument)(const MunitSuite *suite, void *user_data,482int *arg, int argc, char *const *argv);483void (*write_help)(const MunitArgument *argument, void *user_data);484};485486int munit_suite_main_custom(const MunitSuite *suite, void *user_data, int argc,487char *const *argv, const MunitArgument arguments[]);488489#if defined(MUNIT_ENABLE_ASSERT_ALIASES)490491# define assert_true(expr) munit_assert_true(expr)492# define assert_false(expr) munit_assert_false(expr)493# define assert_char(a, op, b) munit_assert_char(a, op, b)494# define assert_uchar(a, op, b) munit_assert_uchar(a, op, b)495# define assert_short(a, op, b) munit_assert_short(a, op, b)496# define assert_ushort(a, op, b) munit_assert_ushort(a, op, b)497# define assert_int(a, op, b) munit_assert_int(a, op, b)498# define assert_uint(a, op, b) munit_assert_uint(a, op, b)499# define assert_long(a, op, b) munit_assert_long(a, op, b)500# define assert_ulong(a, op, b) munit_assert_ulong(a, op, b)501# define assert_llong(a, op, b) munit_assert_llong(a, op, b)502# define assert_ullong(a, op, b) munit_assert_ullong(a, op, b)503# define assert_size(a, op, b) munit_assert_size(a, op, b)504# define assert_ssize(a, op, b) munit_assert_ssize(a, op, b)505# define assert_float(a, op, b) munit_assert_float(a, op, b)506# define assert_double(a, op, b) munit_assert_double(a, op, b)507# define assert_ptr(a, op, b) munit_assert_ptr(a, op, b)508509# define assert_int8(a, op, b) munit_assert_int8(a, op, b)510# define assert_uint8(a, op, b) munit_assert_uint8(a, op, b)511# define assert_int16(a, op, b) munit_assert_int16(a, op, b)512# define assert_uint16(a, op, b) munit_assert_uint16(a, op, b)513# define assert_int32(a, op, b) munit_assert_int32(a, op, b)514# define assert_uint32(a, op, b) munit_assert_uint32(a, op, b)515# define assert_int64(a, op, b) munit_assert_int64(a, op, b)516# define assert_uint64(a, op, b) munit_assert_uint64(a, op, b)517518# define assert_ptrdiff(a, op, b) munit_assert_ptrdiff(a, op, b)519520# define assert_enum(T, a, op, b) munit_assert_enum(T, a, op, b)521522# define assert_double_equal(a, b, precision) \523munit_assert_double_equal(a, b, precision)524# define assert_string_equal(a, b) munit_assert_string_equal(a, b)525# define assert_string_not_equal(a, b) munit_assert_string_not_equal(a, b)526# define assert_memory_equal(size, a, b) munit_assert_memory_equal(size, a, b)527# define assert_memn_equal(a, a_size, b, b_size) \528munit_assert_memn_equal(a, a_size, b, b_size)529# define assert_memory_not_equal(size, a, b) \530munit_assert_memory_not_equal(size, a, b)531# define assert_ptr_equal(a, b) munit_assert_ptr_equal(a, b)532# define assert_ptr_not_equal(a, b) munit_assert_ptr_not_equal(a, b)533# define assert_ptr_null(ptr) munit_assert_null_equal(ptr)534# define assert_ptr_not_null(ptr) munit_assert_not_null(ptr)535536# define assert_null(ptr) munit_assert_null(ptr)537# define assert_not_null(ptr) munit_assert_not_null(ptr)538539#endif /* defined(MUNIT_ENABLE_ASSERT_ALIASES) */540541#define munit_void_test_decl(func) \542void func(void); \543\544static inline MunitResult wrap_##func(const MunitParameter params[], \545void *fixture) { \546(void)params; \547(void)fixture; \548\549func(); \550return MUNIT_OK; \551}552553#define munit_void_test(func) \554{"/" #func, wrap_##func, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}555556#define munit_test_end() {NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}557558int munit_hexdump(FILE *fp, const void *data, size_t datalen);559560int munit_hexdump_diff(FILE *fp, const void *a, size_t alen, const void *b,561size_t blen);562563#if defined(__cplusplus)564}565#endif566567#endif /* !defined(MUNIT_H) */568569#if defined(MUNIT_ENABLE_ASSERT_ALIASES)570#if defined(assert)571# undef assert572#endif573#define assert(expr) munit_assert(expr)574#endif575576577