1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Base unit test (KUnit) API.
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
12 #include <kunit/assert.h>
13 #include <kunit/try-catch.h>
15 #include <linux/args.h>
16 #include <linux/compiler.h>
17 #include <linux/container_of.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/jump_label.h>
21 #include <linux/kconfig.h>
22 #include <linux/kref.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
27 #include <linux/string.h>
28 #include <linux/types.h>
30 #include <asm/rwonce.h>
32 /* Static key: true if any KUnit tests are currently running */
33 DECLARE_STATIC_KEY_FALSE(kunit_running);
37 /* Size of log associated with test. */
38 #define KUNIT_LOG_SIZE 2048
40 /* Maximum size of parameter description string. */
41 #define KUNIT_PARAM_DESC_SIZE 128
43 /* Maximum size of a status comment. */
44 #define KUNIT_STATUS_COMMENT_SIZE 256
47 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
48 * sub-subtest. See the "Subtests" section in
49 * https://node-tap.org/tap-protocol/
51 #define KUNIT_INDENT_LEN 4
52 #define KUNIT_SUBTEST_INDENT " "
53 #define KUNIT_SUBSUBTEST_INDENT " "
56 * enum kunit_status - Type of result for a test or test suite
57 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
58 * @KUNIT_FAILURE: Denotes the test has failed.
59 * @KUNIT_SKIPPED: Denotes the test has been skipped.
67 /* Attribute struct/enum definitions */
70 * Speed Attribute is stored as an enum and separated into categories of
71 * speed: very_slowm, slow, and normal. These speeds are relative to
74 * Note: unset speed attribute acts as default of KUNIT_SPEED_NORMAL.
78 KUNIT_SPEED_VERY_SLOW,
81 KUNIT_SPEED_MAX = KUNIT_SPEED_NORMAL,
84 /* Holds attributes for each test case and suite */
85 struct kunit_attributes {
86 enum kunit_speed speed;
90 * struct kunit_case - represents an individual test case.
92 * @run_case: the function representing the actual test case.
93 * @name: the name of the test case.
94 * @generate_params: the generator function for parameterized tests.
95 * @attr: the attributes associated with the test
97 * A test case is a function with the signature,
98 * ``void (*)(struct kunit *)``
99 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
100 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
101 * with a &struct kunit_suite and will be run after the suite's init
102 * function and followed by the suite's exit function.
104 * A test case should be static and should only be created with the
105 * KUNIT_CASE() macro; additionally, every array of test cases should be
106 * terminated with an empty test case.
112 * void add_test_basic(struct kunit *test)
114 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
115 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
116 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
117 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
118 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
121 * static struct kunit_case example_test_cases[] = {
122 * KUNIT_CASE(add_test_basic),
128 void (*run_case)(struct kunit *test);
130 const void* (*generate_params)(const void *prev, char *desc);
131 struct kunit_attributes attr;
133 /* private: internal use only. */
134 enum kunit_status status;
139 static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
152 * KUNIT_CASE - A helper for creating a &struct kunit_case
154 * @test_name: a reference to a test case function.
156 * Takes a symbol for a function representing a test case and creates a
157 * &struct kunit_case object from it. See the documentation for
158 * &struct kunit_case for an example on how to use it.
160 #define KUNIT_CASE(test_name) \
161 { .run_case = test_name, .name = #test_name, \
162 .module_name = KBUILD_MODNAME}
165 * KUNIT_CASE_ATTR - A helper for creating a &struct kunit_case
168 * @test_name: a reference to a test case function.
169 * @attributes: a reference to a struct kunit_attributes object containing
172 #define KUNIT_CASE_ATTR(test_name, attributes) \
173 { .run_case = test_name, .name = #test_name, \
174 .attr = attributes, .module_name = KBUILD_MODNAME}
177 * KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case
178 * with the slow attribute
180 * @test_name: a reference to a test case function.
183 #define KUNIT_CASE_SLOW(test_name) \
184 { .run_case = test_name, .name = #test_name, \
185 .attr.speed = KUNIT_SPEED_SLOW, .module_name = KBUILD_MODNAME}
188 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
190 * @test_name: a reference to a test case function.
191 * @gen_params: a reference to a parameter generator function.
193 * The generator function::
195 * const void* gen_params(const void *prev, char *desc)
197 * is used to lazily generate a series of arbitrarily typed values that fit into
198 * a void*. The argument @prev is the previously returned value, which should be
199 * used to derive the next value; @prev is set to NULL on the initial generator
200 * call. When no more values are available, the generator must return NULL.
201 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
202 * describing the parameter.
204 #define KUNIT_CASE_PARAM(test_name, gen_params) \
205 { .run_case = test_name, .name = #test_name, \
206 .generate_params = gen_params, .module_name = KBUILD_MODNAME}
209 * KUNIT_CASE_PARAM_ATTR - A helper for creating a parameterized &struct
210 * kunit_case with attributes
212 * @test_name: a reference to a test case function.
213 * @gen_params: a reference to a parameter generator function.
214 * @attributes: a reference to a struct kunit_attributes object containing
217 #define KUNIT_CASE_PARAM_ATTR(test_name, gen_params, attributes) \
218 { .run_case = test_name, .name = #test_name, \
219 .generate_params = gen_params, \
220 .attr = attributes, .module_name = KBUILD_MODNAME}
223 * struct kunit_suite - describes a related collection of &struct kunit_case
225 * @name: the name of the test. Purely informational.
226 * @suite_init: called once per test suite before the test cases.
227 * @suite_exit: called once per test suite after all test cases.
228 * @init: called before every test case.
229 * @exit: called after every test case.
230 * @test_cases: a null terminated array of test cases.
231 * @attr: the attributes associated with the test suite
233 * A kunit_suite is a collection of related &struct kunit_case s, such that
234 * @init is called before every test case and @exit is called after every
235 * test case, similar to the notion of a *test fixture* or a *test class*
236 * in other unit testing frameworks like JUnit or Googletest.
238 * Note that @exit and @suite_exit will run even if @init or @suite_init
239 * fail: make sure they can handle any inconsistent state which may result.
241 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
245 const char name[256];
246 int (*suite_init)(struct kunit_suite *suite);
247 void (*suite_exit)(struct kunit_suite *suite);
248 int (*init)(struct kunit *test);
249 void (*exit)(struct kunit *test);
250 struct kunit_case *test_cases;
251 struct kunit_attributes attr;
253 /* private: internal use only */
254 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
255 struct dentry *debugfs;
260 /* Stores an array of suites, end points one past the end */
261 struct kunit_suite_set {
262 struct kunit_suite * const *start;
263 struct kunit_suite * const *end;
267 * struct kunit - represents a running instance of a test.
269 * @priv: for user to store arbitrary data. Commonly used to pass data
270 * created in the init function (see &struct kunit_suite).
272 * Used to store information about the current context under which the test
273 * is running. Most of this data is private and should only be accessed
274 * indirectly via public functions; the one exception is @priv which can be
275 * used by the test writer to store arbitrary data.
280 /* private: internal use only. */
281 const char *name; /* Read only after initialization! */
282 char *log; /* Points at case log after initialization */
283 struct kunit_try_catch try_catch;
284 /* param_value is the current parameter value for a test case. */
285 const void *param_value;
286 /* param_index stores the index of the parameter in parameterized tests. */
289 * success starts as true, and may only be set to false during a
290 * test case; thus, it is safe to update this across multiple
291 * threads using WRITE_ONCE; however, as a consequence, it may only
292 * be read after the test case finishes once all threads associated
293 * with the test case have terminated.
295 spinlock_t lock; /* Guards all mutable test state. */
296 enum kunit_status status; /* Read only after test_case finishes! */
298 * Because resources is a list that may be updated multiple times (with
299 * new resources) from any thread associated with a test case, we must
300 * protect it with some type of lock.
302 struct list_head resources; /* Protected by lock. */
304 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
307 static inline void kunit_set_failure(struct kunit *test)
309 WRITE_ONCE(test->status, KUNIT_FAILURE);
312 bool kunit_enabled(void);
313 const char *kunit_action(void);
314 const char *kunit_filter_glob(void);
315 char *kunit_filter(void);
316 char *kunit_filter_action(void);
318 void kunit_init_test(struct kunit *test, const char *name, char *log);
320 int kunit_run_tests(struct kunit_suite *suite);
322 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
324 unsigned int kunit_test_case_num(struct kunit_suite *suite,
325 struct kunit_case *test_case);
327 struct kunit_suite_set
328 kunit_filter_suites(const struct kunit_suite_set *suite_set,
329 const char *filter_glob,
333 void kunit_free_suite_set(struct kunit_suite_set suite_set);
335 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
337 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
339 void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin);
340 void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr);
342 #if IS_BUILTIN(CONFIG_KUNIT)
343 int kunit_run_all_tests(void);
345 static inline int kunit_run_all_tests(void)
349 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
351 #define __kunit_test_suites(unique_array, ...) \
352 static struct kunit_suite *unique_array[] \
353 __aligned(sizeof(struct kunit_suite *)) \
354 __used __section(".kunit_test_suites") = { __VA_ARGS__ }
357 * kunit_test_suites() - used to register one or more &struct kunit_suite
360 * @__suites: a statically allocated list of &struct kunit_suite.
362 * Registers @suites with the test framework.
363 * This is done by placing the array of struct kunit_suite * in the
364 * .kunit_test_suites ELF section.
366 * When builtin, KUnit tests are all run via the executor at boot, and when
367 * built as a module, they run on module load.
370 #define kunit_test_suites(__suites...) \
371 __kunit_test_suites(__UNIQUE_ID(array), \
374 #define kunit_test_suite(suite) kunit_test_suites(&suite)
377 * kunit_test_init_section_suites() - used to register one or more &struct
378 * kunit_suite containing init functions or
381 * @__suites: a statically allocated list of &struct kunit_suite.
383 * This functions identically as kunit_test_suites() except that it suppresses
384 * modpost warnings for referencing functions marked __init or data marked
385 * __initdata; this is OK because currently KUnit only runs tests upon boot
386 * during the init phase or upon loading a module during the init phase.
388 * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
389 * tests must be excluded.
391 * The only thing this macro does that's different from kunit_test_suites is
392 * that it suffixes the array and suite declarations it makes with _probe;
393 * modpost suppresses warnings about referencing init data for symbols named in
396 #define kunit_test_init_section_suites(__suites...) \
397 __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
400 #define kunit_test_init_section_suite(suite) \
401 kunit_test_init_section_suites(&suite)
403 #define kunit_suite_for_each_test_case(suite, test_case) \
404 for (test_case = suite->test_cases; test_case->run_case; test_case++)
406 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
409 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
410 * @test: The test context object.
411 * @n: number of elements.
412 * @size: The size in bytes of the desired memory.
413 * @gfp: flags passed to underlying kmalloc().
415 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
416 * and is automatically cleaned up after the test case concludes. See kunit_add_action()
417 * for more information.
419 * Note that some internal context data is also allocated with GFP_KERNEL,
420 * regardless of the gfp passed in.
422 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
425 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
426 * @test: The test context object.
427 * @size: The size in bytes of the desired memory.
428 * @gfp: flags passed to underlying kmalloc().
430 * See kmalloc() and kunit_kmalloc_array() for more information.
432 * Note that some internal context data is also allocated with GFP_KERNEL,
433 * regardless of the gfp passed in.
435 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
437 return kunit_kmalloc_array(test, 1, size, gfp);
441 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
442 * @test: The test case to which the resource belongs.
443 * @ptr: The memory allocation to free.
445 void kunit_kfree(struct kunit *test, const void *ptr);
448 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
449 * @test: The test context object.
450 * @size: The size in bytes of the desired memory.
451 * @gfp: flags passed to underlying kmalloc().
453 * See kzalloc() and kunit_kmalloc_array() for more information.
455 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
457 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
461 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
462 * @test: The test context object.
463 * @n: number of elements.
464 * @size: The size in bytes of the desired memory.
465 * @gfp: flags passed to underlying kmalloc().
467 * See kcalloc() and kunit_kmalloc_array() for more information.
469 static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
471 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
474 void kunit_cleanup(struct kunit *test);
476 void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
479 * kunit_mark_skipped() - Marks @test_or_suite as skipped
481 * @test_or_suite: The test context object.
482 * @fmt: A printk() style format string.
484 * Marks the test as skipped. @fmt is given output as the test status
485 * comment, typically the reason the test was skipped.
487 * Test execution continues after kunit_mark_skipped() is called.
489 #define kunit_mark_skipped(test_or_suite, fmt, ...) \
491 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
492 scnprintf((test_or_suite)->status_comment, \
493 KUNIT_STATUS_COMMENT_SIZE, \
494 fmt, ##__VA_ARGS__); \
498 * kunit_skip() - Marks @test_or_suite as skipped
500 * @test_or_suite: The test context object.
501 * @fmt: A printk() style format string.
503 * Skips the test. @fmt is given output as the test status
504 * comment, typically the reason the test was skipped.
506 * Test execution is halted after kunit_skip() is called.
508 #define kunit_skip(test_or_suite, fmt, ...) \
510 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
511 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
515 * printk and log to per-test or per-suite log buffer. Logging only done
516 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
518 #define kunit_log(lvl, test_or_suite, fmt, ...) \
520 printk(lvl fmt, ##__VA_ARGS__); \
521 kunit_log_append((test_or_suite)->log, fmt, \
525 #define kunit_printk(lvl, test, fmt, ...) \
526 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
527 (test)->name, ##__VA_ARGS__)
530 * kunit_info() - Prints an INFO level message associated with @test.
532 * @test: The test context object.
533 * @fmt: A printk() style format string.
535 * Prints an info level message associated with the test suite being run.
536 * Takes a variable number of format parameters just like printk().
538 #define kunit_info(test, fmt, ...) \
539 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
542 * kunit_warn() - Prints a WARN level message associated with @test.
544 * @test: The test context object.
545 * @fmt: A printk() style format string.
547 * Prints a warning level message.
549 #define kunit_warn(test, fmt, ...) \
550 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
553 * kunit_err() - Prints an ERROR level message associated with @test.
555 * @test: The test context object.
556 * @fmt: A printk() style format string.
558 * Prints an error level message.
560 #define kunit_err(test, fmt, ...) \
561 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
564 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
565 * @test: The test context object.
567 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
568 * words, it does nothing and only exists for code clarity. See
569 * KUNIT_EXPECT_TRUE() for more information.
571 #define KUNIT_SUCCEED(test) do {} while (0)
573 void __noreturn __kunit_abort(struct kunit *test);
575 void __kunit_do_failed_assertion(struct kunit *test,
576 const struct kunit_loc *loc,
577 enum kunit_assert_type type,
578 const struct kunit_assert *assert,
579 assert_format_t assert_format,
580 const char *fmt, ...);
582 #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
583 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
584 const struct assert_class __assertion = INITIALIZER; \
585 __kunit_do_failed_assertion(test, \
588 &__assertion.assert, \
592 if (assert_type == KUNIT_ASSERTION) \
593 __kunit_abort(test); \
597 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
598 _KUNIT_FAILED(test, \
601 kunit_fail_assert_format, \
607 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
608 * @test: The test context object.
609 * @fmt: an informational message to be printed when the assertion is made.
610 * @...: string format arguments.
612 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
613 * other words, it always results in a failed expectation, and consequently
614 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
615 * for more information.
617 #define KUNIT_FAIL(test, fmt, ...) \
618 KUNIT_FAIL_ASSERTION(test, \
623 /* Helper to safely pass around an initializer list to other macros. */
624 #define KUNIT_INIT_ASSERT(initializers...) { initializers }
626 #define KUNIT_UNARY_ASSERTION(test, \
633 if (likely(!!(condition_) == !!expected_true_)) \
636 _KUNIT_FAILED(test, \
638 kunit_unary_assert, \
639 kunit_unary_assert_format, \
640 KUNIT_INIT_ASSERT(.condition = #condition_, \
641 .expected_true = expected_true_), \
646 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
647 KUNIT_UNARY_ASSERTION(test, \
654 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
655 KUNIT_UNARY_ASSERTION(test, \
663 * A factory macro for defining the assertions and expectations for the basic
664 * comparisons defined for the built in types.
666 * Unfortunately, there is no common type that all types can be promoted to for
667 * which all the binary operators behave the same way as for the actual types
668 * (for example, there is no type that long long and unsigned long long can
669 * both be cast to where the comparison result is preserved for all values). So
670 * the best we can do is do the comparison in the original types and then coerce
671 * everything to long long for printing; this way, the comparison behaves
672 * correctly and the printed out value usually makes sense without
673 * interpretation, but can always be interpreted to figure out the actual
676 #define KUNIT_BASE_BINARY_ASSERTION(test, \
686 const typeof(left) __left = (left); \
687 const typeof(right) __right = (right); \
688 static const struct kunit_binary_assert_text __text = { \
690 .left_text = #left, \
691 .right_text = #right, \
694 if (likely(__left op __right)) \
697 _KUNIT_FAILED(test, \
701 KUNIT_INIT_ASSERT(.text = &__text, \
702 .left_value = __left, \
703 .right_value = __right), \
708 #define KUNIT_BINARY_INT_ASSERTION(test, \
715 KUNIT_BASE_BINARY_ASSERTION(test, \
716 kunit_binary_assert, \
717 kunit_binary_assert_format, \
723 #define KUNIT_BINARY_PTR_ASSERTION(test, \
730 KUNIT_BASE_BINARY_ASSERTION(test, \
731 kunit_binary_ptr_assert, \
732 kunit_binary_ptr_assert_format, \
738 #define KUNIT_BINARY_STR_ASSERTION(test, \
746 const char *__left = (left); \
747 const char *__right = (right); \
748 static const struct kunit_binary_assert_text __text = { \
750 .left_text = #left, \
751 .right_text = #right, \
754 if (likely(strcmp(__left, __right) op 0)) \
758 _KUNIT_FAILED(test, \
760 kunit_binary_str_assert, \
761 kunit_binary_str_assert_format, \
762 KUNIT_INIT_ASSERT(.text = &__text, \
763 .left_value = __left, \
764 .right_value = __right), \
769 #define KUNIT_MEM_ASSERTION(test, \
778 const void *__left = (left); \
779 const void *__right = (right); \
780 const size_t __size = (size_); \
781 static const struct kunit_binary_assert_text __text = { \
783 .left_text = #left, \
784 .right_text = #right, \
787 if (likely(__left && __right)) \
788 if (likely(memcmp(__left, __right, __size) op 0)) \
791 _KUNIT_FAILED(test, \
794 kunit_mem_assert_format, \
795 KUNIT_INIT_ASSERT(.text = &__text, \
796 .left_value = __left, \
797 .right_value = __right, \
803 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
809 const typeof(ptr) __ptr = (ptr); \
811 if (!IS_ERR_OR_NULL(__ptr)) \
814 _KUNIT_FAILED(test, \
816 kunit_ptr_not_err_assert, \
817 kunit_ptr_not_err_assert_format, \
818 KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \
824 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
825 * @test: The test context object.
826 * @condition: an arbitrary boolean expression. The test fails when this does
827 * not evaluate to true.
829 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
830 * to fail when the specified condition is not met; however, it will not prevent
831 * the test case from continuing to run; this is otherwise known as an
832 * *expectation failure*.
834 #define KUNIT_EXPECT_TRUE(test, condition) \
835 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
837 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
838 KUNIT_TRUE_MSG_ASSERTION(test, \
845 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
846 * @test: The test context object.
847 * @condition: an arbitrary boolean expression. The test fails when this does
848 * not evaluate to false.
850 * Sets an expectation that @condition evaluates to false. See
851 * KUNIT_EXPECT_TRUE() for more information.
853 #define KUNIT_EXPECT_FALSE(test, condition) \
854 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
856 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
857 KUNIT_FALSE_MSG_ASSERTION(test, \
864 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
865 * @test: The test context object.
866 * @left: an arbitrary expression that evaluates to a primitive C type.
867 * @right: an arbitrary expression that evaluates to a primitive C type.
869 * Sets an expectation that the values that @left and @right evaluate to are
870 * equal. This is semantically equivalent to
871 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
874 #define KUNIT_EXPECT_EQ(test, left, right) \
875 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
877 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
878 KUNIT_BINARY_INT_ASSERTION(test, \
885 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
886 * @test: The test context object.
887 * @left: an arbitrary expression that evaluates to a pointer.
888 * @right: an arbitrary expression that evaluates to a pointer.
890 * Sets an expectation that the values that @left and @right evaluate to are
891 * equal. This is semantically equivalent to
892 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
895 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
896 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
898 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
899 KUNIT_BINARY_PTR_ASSERTION(test, \
906 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
907 * @test: The test context object.
908 * @left: an arbitrary expression that evaluates to a primitive C type.
909 * @right: an arbitrary expression that evaluates to a primitive C type.
911 * Sets an expectation that the values that @left and @right evaluate to are not
912 * equal. This is semantically equivalent to
913 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
916 #define KUNIT_EXPECT_NE(test, left, right) \
917 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
919 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
920 KUNIT_BINARY_INT_ASSERTION(test, \
927 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
928 * @test: The test context object.
929 * @left: an arbitrary expression that evaluates to a pointer.
930 * @right: an arbitrary expression that evaluates to a pointer.
932 * Sets an expectation that the values that @left and @right evaluate to are not
933 * equal. This is semantically equivalent to
934 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
937 #define KUNIT_EXPECT_PTR_NE(test, left, right) \
938 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
940 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
941 KUNIT_BINARY_PTR_ASSERTION(test, \
948 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
949 * @test: The test context object.
950 * @left: an arbitrary expression that evaluates to a primitive C type.
951 * @right: an arbitrary expression that evaluates to a primitive C type.
953 * Sets an expectation that the value that @left evaluates to is less than the
954 * value that @right evaluates to. This is semantically equivalent to
955 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
958 #define KUNIT_EXPECT_LT(test, left, right) \
959 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
961 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
962 KUNIT_BINARY_INT_ASSERTION(test, \
969 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
970 * @test: The test context object.
971 * @left: an arbitrary expression that evaluates to a primitive C type.
972 * @right: an arbitrary expression that evaluates to a primitive C type.
974 * Sets an expectation that the value that @left evaluates to is less than or
975 * equal to the value that @right evaluates to. Semantically this is equivalent
976 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
979 #define KUNIT_EXPECT_LE(test, left, right) \
980 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
982 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
983 KUNIT_BINARY_INT_ASSERTION(test, \
990 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
991 * @test: The test context object.
992 * @left: an arbitrary expression that evaluates to a primitive C type.
993 * @right: an arbitrary expression that evaluates to a primitive C type.
995 * Sets an expectation that the value that @left evaluates to is greater than
996 * the value that @right evaluates to. This is semantically equivalent to
997 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
1000 #define KUNIT_EXPECT_GT(test, left, right) \
1001 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
1003 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
1004 KUNIT_BINARY_INT_ASSERTION(test, \
1005 KUNIT_EXPECTATION, \
1011 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1012 * @test: The test context object.
1013 * @left: an arbitrary expression that evaluates to a primitive C type.
1014 * @right: an arbitrary expression that evaluates to a primitive C type.
1016 * Sets an expectation that the value that @left evaluates to is greater than
1017 * the value that @right evaluates to. This is semantically equivalent to
1018 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1021 #define KUNIT_EXPECT_GE(test, left, right) \
1022 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
1024 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
1025 KUNIT_BINARY_INT_ASSERTION(test, \
1026 KUNIT_EXPECTATION, \
1032 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1033 * @test: The test context object.
1034 * @left: an arbitrary expression that evaluates to a null terminated string.
1035 * @right: an arbitrary expression that evaluates to a null terminated string.
1037 * Sets an expectation that the values that @left and @right evaluate to are
1038 * equal. This is semantically equivalent to
1039 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1040 * for more information.
1042 #define KUNIT_EXPECT_STREQ(test, left, right) \
1043 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
1045 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
1046 KUNIT_BINARY_STR_ASSERTION(test, \
1047 KUNIT_EXPECTATION, \
1053 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1054 * @test: The test context object.
1055 * @left: an arbitrary expression that evaluates to a null terminated string.
1056 * @right: an arbitrary expression that evaluates to a null terminated string.
1058 * Sets an expectation that the values that @left and @right evaluate to are
1059 * not equal. This is semantically equivalent to
1060 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1061 * for more information.
1063 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
1064 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
1066 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
1067 KUNIT_BINARY_STR_ASSERTION(test, \
1068 KUNIT_EXPECTATION, \
1074 * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.
1075 * @test: The test context object.
1076 * @left: An arbitrary expression that evaluates to the specified size.
1077 * @right: An arbitrary expression that evaluates to the specified size.
1078 * @size: Number of bytes compared.
1080 * Sets an expectation that the values that @left and @right evaluate to are
1081 * equal. This is semantically equivalent to
1082 * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
1083 * KUNIT_EXPECT_TRUE() for more information.
1085 * Although this expectation works for any memory block, it is not recommended
1086 * for comparing more structured data, such as structs. This expectation is
1087 * recommended for comparing, for example, data arrays.
1089 #define KUNIT_EXPECT_MEMEQ(test, left, right, size) \
1090 KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)
1092 #define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
1093 KUNIT_MEM_ASSERTION(test, \
1094 KUNIT_EXPECTATION, \
1101 * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.
1102 * @test: The test context object.
1103 * @left: An arbitrary expression that evaluates to the specified size.
1104 * @right: An arbitrary expression that evaluates to the specified size.
1105 * @size: Number of bytes compared.
1107 * Sets an expectation that the values that @left and @right evaluate to are
1108 * not equal. This is semantically equivalent to
1109 * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1110 * KUNIT_EXPECT_TRUE() for more information.
1112 * Although this expectation works for any memory block, it is not recommended
1113 * for comparing more structured data, such as structs. This expectation is
1114 * recommended for comparing, for example, data arrays.
1116 #define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \
1117 KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)
1119 #define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1120 KUNIT_MEM_ASSERTION(test, \
1121 KUNIT_EXPECTATION, \
1128 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
1129 * @test: The test context object.
1130 * @ptr: an arbitrary pointer.
1132 * Sets an expectation that the value that @ptr evaluates to is null. This is
1133 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
1134 * See KUNIT_EXPECT_TRUE() for more information.
1136 #define KUNIT_EXPECT_NULL(test, ptr) \
1137 KUNIT_EXPECT_NULL_MSG(test, \
1141 #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
1142 KUNIT_BINARY_PTR_ASSERTION(test, \
1143 KUNIT_EXPECTATION, \
1149 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
1150 * @test: The test context object.
1151 * @ptr: an arbitrary pointer.
1153 * Sets an expectation that the value that @ptr evaluates to is not null. This
1154 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
1155 * See KUNIT_EXPECT_TRUE() for more information.
1157 #define KUNIT_EXPECT_NOT_NULL(test, ptr) \
1158 KUNIT_EXPECT_NOT_NULL_MSG(test, \
1162 #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1163 KUNIT_BINARY_PTR_ASSERTION(test, \
1164 KUNIT_EXPECTATION, \
1170 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1171 * @test: The test context object.
1172 * @ptr: an arbitrary pointer.
1174 * Sets an expectation that the value that @ptr evaluates to is not null and not
1175 * an errno stored in a pointer. This is semantically equivalent to
1176 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1179 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1180 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1182 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1183 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1184 KUNIT_EXPECTATION, \
1189 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1190 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1193 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1194 * @test: The test context object.
1195 * @condition: an arbitrary boolean expression. The test fails and aborts when
1196 * this does not evaluate to true.
1198 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1199 * fail *and immediately abort* when the specified condition is not met. Unlike
1200 * an expectation failure, it will prevent the test case from continuing to run;
1201 * this is otherwise known as an *assertion failure*.
1203 #define KUNIT_ASSERT_TRUE(test, condition) \
1204 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
1206 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1207 KUNIT_TRUE_MSG_ASSERTION(test, \
1214 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1215 * @test: The test context object.
1216 * @condition: an arbitrary boolean expression.
1218 * Sets an assertion that the value that @condition evaluates to is false. This
1219 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1220 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1222 #define KUNIT_ASSERT_FALSE(test, condition) \
1223 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
1225 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1226 KUNIT_FALSE_MSG_ASSERTION(test, \
1233 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1234 * @test: The test context object.
1235 * @left: an arbitrary expression that evaluates to a primitive C type.
1236 * @right: an arbitrary expression that evaluates to a primitive C type.
1238 * Sets an assertion that the values that @left and @right evaluate to are
1239 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1240 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1242 #define KUNIT_ASSERT_EQ(test, left, right) \
1243 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
1245 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1246 KUNIT_BINARY_INT_ASSERTION(test, \
1253 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1254 * @test: The test context object.
1255 * @left: an arbitrary expression that evaluates to a pointer.
1256 * @right: an arbitrary expression that evaluates to a pointer.
1258 * Sets an assertion that the values that @left and @right evaluate to are
1259 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1260 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1262 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1263 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
1265 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1266 KUNIT_BINARY_PTR_ASSERTION(test, \
1273 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1274 * @test: The test context object.
1275 * @left: an arbitrary expression that evaluates to a primitive C type.
1276 * @right: an arbitrary expression that evaluates to a primitive C type.
1278 * Sets an assertion that the values that @left and @right evaluate to are not
1279 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1280 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1282 #define KUNIT_ASSERT_NE(test, left, right) \
1283 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
1285 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1286 KUNIT_BINARY_INT_ASSERTION(test, \
1293 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1294 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1295 * @test: The test context object.
1296 * @left: an arbitrary expression that evaluates to a pointer.
1297 * @right: an arbitrary expression that evaluates to a pointer.
1299 * Sets an assertion that the values that @left and @right evaluate to are not
1300 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1301 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1303 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1304 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1306 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1307 KUNIT_BINARY_PTR_ASSERTION(test, \
1313 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1314 * @test: The test context object.
1315 * @left: an arbitrary expression that evaluates to a primitive C type.
1316 * @right: an arbitrary expression that evaluates to a primitive C type.
1318 * Sets an assertion that the value that @left evaluates to is less than the
1319 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1320 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1323 #define KUNIT_ASSERT_LT(test, left, right) \
1324 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1326 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1327 KUNIT_BINARY_INT_ASSERTION(test, \
1333 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1334 * @test: The test context object.
1335 * @left: an arbitrary expression that evaluates to a primitive C type.
1336 * @right: an arbitrary expression that evaluates to a primitive C type.
1338 * Sets an assertion that the value that @left evaluates to is less than or
1339 * equal to the value that @right evaluates to. This is the same as
1340 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1341 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1343 #define KUNIT_ASSERT_LE(test, left, right) \
1344 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1346 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1347 KUNIT_BINARY_INT_ASSERTION(test, \
1354 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1355 * @test: The test context object.
1356 * @left: an arbitrary expression that evaluates to a primitive C type.
1357 * @right: an arbitrary expression that evaluates to a primitive C type.
1359 * Sets an assertion that the value that @left evaluates to is greater than the
1360 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1361 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1364 #define KUNIT_ASSERT_GT(test, left, right) \
1365 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1367 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1368 KUNIT_BINARY_INT_ASSERTION(test, \
1375 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1376 * @test: The test context object.
1377 * @left: an arbitrary expression that evaluates to a primitive C type.
1378 * @right: an arbitrary expression that evaluates to a primitive C type.
1380 * Sets an assertion that the value that @left evaluates to is greater than the
1381 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1382 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1385 #define KUNIT_ASSERT_GE(test, left, right) \
1386 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1388 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1389 KUNIT_BINARY_INT_ASSERTION(test, \
1396 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1397 * @test: The test context object.
1398 * @left: an arbitrary expression that evaluates to a null terminated string.
1399 * @right: an arbitrary expression that evaluates to a null terminated string.
1401 * Sets an assertion that the values that @left and @right evaluate to are
1402 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1403 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1405 #define KUNIT_ASSERT_STREQ(test, left, right) \
1406 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1408 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1409 KUNIT_BINARY_STR_ASSERTION(test, \
1416 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1417 * @test: The test context object.
1418 * @left: an arbitrary expression that evaluates to a null terminated string.
1419 * @right: an arbitrary expression that evaluates to a null terminated string.
1421 * Sets an expectation that the values that @left and @right evaluate to are
1422 * not equal. This is semantically equivalent to
1423 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1424 * for more information.
1426 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1427 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1429 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1430 KUNIT_BINARY_STR_ASSERTION(test, \
1437 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1438 * @test: The test context object.
1439 * @ptr: an arbitrary pointer.
1441 * Sets an assertion that the values that @ptr evaluates to is null. This is
1442 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1443 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1445 #define KUNIT_ASSERT_NULL(test, ptr) \
1446 KUNIT_ASSERT_NULL_MSG(test, \
1450 #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1451 KUNIT_BINARY_PTR_ASSERTION(test, \
1458 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1459 * @test: The test context object.
1460 * @ptr: an arbitrary pointer.
1462 * Sets an assertion that the values that @ptr evaluates to is not null. This
1463 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1464 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1466 #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1467 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1471 #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1472 KUNIT_BINARY_PTR_ASSERTION(test, \
1479 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1480 * @test: The test context object.
1481 * @ptr: an arbitrary pointer.
1483 * Sets an assertion that the value that @ptr evaluates to is not null and not
1484 * an errno stored in a pointer. This is the same as
1485 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1486 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1488 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1489 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1491 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1492 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1499 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1500 * @name: prefix for the test parameter generator function.
1501 * @array: array of test parameters.
1502 * @get_desc: function to convert param to description; NULL to use default
1504 * Define function @name_gen_params which uses @array to generate parameters.
1506 #define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1507 static const void *name##_gen_params(const void *prev, char *desc) \
1509 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1510 if (__next - (array) < ARRAY_SIZE((array))) { \
1511 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1513 __get_desc(__next, desc); \
1519 // TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1520 // include resource.h themselves if they need it.
1521 #include <kunit/resource.h>
1523 #endif /* _KUNIT_TEST_H */