xref: /freebsd/sys/contrib/openzfs/tests/unit/unit.h (revision d9497217456002b0ddad3cd319570d0b098daa29)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * http://www.illumos.org/license/CDDL.
11  */
12 
13 /*
14  * Copyright (c) 2026, TrueNAS.
15  */
16 
17 #ifndef UNIT_H
18 #define	UNIT_H
19 
20 #include "munit.h"
21 
22 /* test/suite definition helpers */
23 
24 /* single element in a MunitTest array */
25 #define	_UNIT_TEST(name, func, params, ...)				\
26 	{ (name), (func), NULL, NULL, MUNIT_TEST_OPTION_NONE,	\
27 	(MunitParameterEnum*)(params)  }
28 #define	UNIT_TEST(name, func, ...)				\
29 	_UNIT_TEST(name, func, ##__VA_ARGS__, NULL)
30 
31 /* single element in a MunitParameterEnum array */
32 #define	UNIT_PARAM(name, ...)	\
33 	{ (char *)(name), (char **)(const char *[]) { __VA_ARGS__, NULL } }
34 
35 /* shortcut for truthy tests */
36 #define	unit_true(a)	munit_assert_true(a)
37 #define	unit_false(a)	munit_assert_false(a)
38 
39 /* shortcut for zero test */
40 #define	unit_zero(a)	munit_assert_uint64((a), ==, 0)
41 
42 /* shortcuts for integer comparisons */
43 #define	_unit_op(a, op, b)	munit_assert_uint64((a), op, (b))
44 
45 #define	unit_eq(a, b)	_unit_op((a), ==, (b))
46 #define	unit_ne(a, b)	_unit_op((a), !=, (b))
47 #define	unit_le(a, b)	_unit_op((a), <=, (b))
48 #define	unit_ge(a, b)	_unit_op((a), >=, (b))
49 #define	unit_lt(a, b)	_unit_op((a), <,  (b))
50 #define	unit_gt(a, b)	_unit_op((a), >,  (b))
51 
52 /* shortcuts for string comparisons */
53 #define	unit_str_eq(a, b)	munit_assert_string_equal(a, b)
54 #define	unit_str_ne(a, b)	munit_assert_string_not_equal(a, b)
55 
56 /* shortcuts for error-returning function call */
57 #define	unit_ok(a)	munit_assert_int((a), ==, 0)
58 #define	unit_err(a, e)	munit_assert_int((a), ==, (e))
59 
60 /* helpers to generate useful random data */
61 extern uint64_t unit_rand_uint64(void);
62 extern char *unit_rand_str(char *buf, size_t bufsz);
63 
64 #endif /* UNIT_H */
65