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/compiler.h>
16 #include <linux/container_of.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/jump_label.h>
20 #include <linux/kconfig.h>
21 #include <linux/kref.h>
22 #include <linux/list.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/string.h>
27 #include <linux/types.h>
29 #include <asm/rwonce.h>
31 /* Static key: true if any KUnit tests are currently running */
32 DECLARE_STATIC_KEY_FALSE(kunit_running);
36 /* Size of log associated with test. */
37 #define KUNIT_LOG_SIZE 2048
39 /* Maximum size of parameter description string. */
40 #define KUNIT_PARAM_DESC_SIZE 128
42 /* Maximum size of a status comment. */
43 #define KUNIT_STATUS_COMMENT_SIZE 256
46 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
47 * sub-subtest. See the "Subtests" section in
48 * https://node-tap.org/tap-protocol/
50 #define KUNIT_INDENT_LEN 4
51 #define KUNIT_SUBTEST_INDENT " "
52 #define KUNIT_SUBSUBTEST_INDENT " "
55 * enum kunit_status - Type of result for a test or test suite
56 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
57 * @KUNIT_FAILURE: Denotes the test has failed.
58 * @KUNIT_SKIPPED: Denotes the test has been skipped.
66 /* Attribute struct/enum definitions */
69 * Speed Attribute is stored as an enum and separated into categories of
70 * speed: very_slowm, slow, and normal. These speeds are relative to
73 * Note: unset speed attribute acts as default of KUNIT_SPEED_NORMAL.
77 KUNIT_SPEED_VERY_SLOW,
80 KUNIT_SPEED_MAX = KUNIT_SPEED_NORMAL,
83 /* Holds attributes for each test case and suite */
84 struct kunit_attributes {
85 enum kunit_speed speed;
89 * struct kunit_case - represents an individual test case.
91 * @run_case: the function representing the actual test case.
92 * @name: the name of the test case.
93 * @generate_params: the generator function for parameterized tests.
94 * @attr: the attributes associated with the test
96 * A test case is a function with the signature,
97 * ``void (*)(struct kunit *)``
98 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
99 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
100 * with a &struct kunit_suite and will be run after the suite's init
101 * function and followed by the suite's exit function.
103 * A test case should be static and should only be created with the
104 * KUNIT_CASE() macro; additionally, every array of test cases should be
105 * terminated with an empty test case.
111 * void add_test_basic(struct kunit *test)
113 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
114 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
115 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
116 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
117 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
120 * static struct kunit_case example_test_cases[] = {
121 * KUNIT_CASE(add_test_basic),
127 void (*run_case)(struct kunit *test);
129 const void* (*generate_params)(const void *prev, char *desc);
130 struct kunit_attributes attr;
132 /* private: internal use only. */
133 enum kunit_status status;
138 static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
151 * KUNIT_CASE - A helper for creating a &struct kunit_case
153 * @test_name: a reference to a test case function.
155 * Takes a symbol for a function representing a test case and creates a
156 * &struct kunit_case object from it. See the documentation for
157 * &struct kunit_case for an example on how to use it.
159 #define KUNIT_CASE(test_name) \
160 { .run_case = test_name, .name = #test_name, \
161 .module_name = KBUILD_MODNAME}
164 * KUNIT_CASE_ATTR - A helper for creating a &struct kunit_case
167 * @test_name: a reference to a test case function.
168 * @attributes: a reference to a struct kunit_attributes object containing
171 #define KUNIT_CASE_ATTR(test_name, attributes) \
172 { .run_case = test_name, .name = #test_name, \
173 .attr = attributes, .module_name = KBUILD_MODNAME}
176 * KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case
177 * with the slow attribute
179 * @test_name: a reference to a test case function.
182 #define KUNIT_CASE_SLOW(test_name) \
183 { .run_case = test_name, .name = #test_name, \
184 .attr.speed = KUNIT_SPEED_SLOW, .module_name = KBUILD_MODNAME}
187 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
189 * @test_name: a reference to a test case function.
190 * @gen_params: a reference to a parameter generator function.
192 * The generator function::
194 * const void* gen_params(const void *prev, char *desc)
196 * is used to lazily generate a series of arbitrarily typed values that fit into
197 * a void*. The argument @prev is the previously returned value, which should be
198 * used to derive the next value; @prev is set to NULL on the initial generator
199 * call. When no more values are available, the generator must return NULL.
200 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
201 * describing the parameter.
203 #define KUNIT_CASE_PARAM(test_name, gen_params) \
204 { .run_case = test_name, .name = #test_name, \
205 .generate_params = gen_params, .module_name = KBUILD_MODNAME}
208 * KUNIT_CASE_PARAM_ATTR - A helper for creating a parameterized &struct
209 * kunit_case with attributes
211 * @test_name: a reference to a test case function.
212 * @gen_params: a reference to a parameter generator function.
213 * @attributes: a reference to a struct kunit_attributes object containing
216 #define KUNIT_CASE_PARAM_ATTR(test_name, gen_params, attributes) \
217 { .run_case = test_name, .name = #test_name, \
218 .generate_params = gen_params, \
219 .attr = attributes, .module_name = KBUILD_MODNAME}
222 * struct kunit_suite - describes a related collection of &struct kunit_case
224 * @name: the name of the test. Purely informational.
225 * @suite_init: called once per test suite before the test cases.
226 * @suite_exit: called once per test suite after all test cases.
227 * @init: called before every test case.
228 * @exit: called after every test case.
229 * @test_cases: a null terminated array of test cases.
230 * @attr: the attributes associated with the test suite
232 * A kunit_suite is a collection of related &struct kunit_case s, such that
233 * @init is called before every test case and @exit is called after every
234 * test case, similar to the notion of a *test fixture* or a *test class*
235 * in other unit testing frameworks like JUnit or Googletest.
237 * Note that @exit and @suite_exit will run even if @init or @suite_init
238 * fail: make sure they can handle any inconsistent state which may result.
240 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
244 const char name[256];
245 int (*suite_init)(struct kunit_suite *suite);
246 void (*suite_exit)(struct kunit_suite *suite);
247 int (*init)(struct kunit *test);
248 void (*exit)(struct kunit *test);
249 struct kunit_case *test_cases;
250 struct kunit_attributes attr;
252 /* private: internal use only */
253 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
254 struct dentry *debugfs;
259 /* Stores an array of suites, end points one past the end */
260 struct kunit_suite_set {
261 struct kunit_suite * const *start;
262 struct kunit_suite * const *end;
266 * struct kunit - represents a running instance of a test.
268 * @priv: for user to store arbitrary data. Commonly used to pass data
269 * created in the init function (see &struct kunit_suite).
271 * Used to store information about the current context under which the test
272 * is running. Most of this data is private and should only be accessed
273 * indirectly via public functions; the one exception is @priv which can be
274 * used by the test writer to store arbitrary data.
279 /* private: internal use only. */
280 const char *name; /* Read only after initialization! */
281 char *log; /* Points at case log after initialization */
282 struct kunit_try_catch try_catch;
283 /* param_value is the current parameter value for a test case. */
284 const void *param_value;
285 /* param_index stores the index of the parameter in parameterized tests. */
288 * success starts as true, and may only be set to false during a
289 * test case; thus, it is safe to update this across multiple
290 * threads using WRITE_ONCE; however, as a consequence, it may only
291 * be read after the test case finishes once all threads associated
292 * with the test case have terminated.
294 spinlock_t lock; /* Guards all mutable test state. */
295 enum kunit_status status; /* Read only after test_case finishes! */
297 * Because resources is a list that may be updated multiple times (with
298 * new resources) from any thread associated with a test case, we must
299 * protect it with some type of lock.
301 struct list_head resources; /* Protected by lock. */
303 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
306 static inline void kunit_set_failure(struct kunit *test)
308 WRITE_ONCE(test->status, KUNIT_FAILURE);
311 bool kunit_enabled(void);
312 const char *kunit_action(void);
313 const char *kunit_filter_glob(void);
314 char *kunit_filter(void);
315 char *kunit_filter_action(void);
317 void kunit_init_test(struct kunit *test, const char *name, char *log);
319 int kunit_run_tests(struct kunit_suite *suite);
321 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
323 unsigned int kunit_test_case_num(struct kunit_suite *suite,
324 struct kunit_case *test_case);
326 struct kunit_suite_set
327 kunit_filter_suites(const struct kunit_suite_set *suite_set,
328 const char *filter_glob,
332 void kunit_free_suite_set(struct kunit_suite_set suite_set);
334 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
336 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
338 void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin);
339 void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr);
341 #if IS_BUILTIN(CONFIG_KUNIT)
342 int kunit_run_all_tests(void);
344 static inline int kunit_run_all_tests(void)
348 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
350 #define __kunit_test_suites(unique_array, ...) \
351 static struct kunit_suite *unique_array[] \
352 __aligned(sizeof(struct kunit_suite *)) \
353 __used __section(".kunit_test_suites") = { __VA_ARGS__ }
356 * kunit_test_suites() - used to register one or more &struct kunit_suite
359 * @__suites: a statically allocated list of &struct kunit_suite.
361 * Registers @suites with the test framework.
362 * This is done by placing the array of struct kunit_suite * in the
363 * .kunit_test_suites ELF section.
365 * When builtin, KUnit tests are all run via the executor at boot, and when
366 * built as a module, they run on module load.
369 #define kunit_test_suites(__suites...) \
370 __kunit_test_suites(__UNIQUE_ID(array), \
373 #define kunit_test_suite(suite) kunit_test_suites(&suite)
376 * kunit_test_init_section_suites() - used to register one or more &struct
377 * kunit_suite containing init functions or
380 * @__suites: a statically allocated list of &struct kunit_suite.
382 * This functions identically as kunit_test_suites() except that it suppresses
383 * modpost warnings for referencing functions marked __init or data marked
384 * __initdata; this is OK because currently KUnit only runs tests upon boot
385 * during the init phase or upon loading a module during the init phase.
387 * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
388 * tests must be excluded.
390 * The only thing this macro does that's different from kunit_test_suites is
391 * that it suffixes the array and suite declarations it makes with _probe;
392 * modpost suppresses warnings about referencing init data for symbols named in
395 #define kunit_test_init_section_suites(__suites...) \
396 __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
399 #define kunit_test_init_section_suite(suite) \
400 kunit_test_init_section_suites(&suite)
402 #define kunit_suite_for_each_test_case(suite, test_case) \
403 for (test_case = suite->test_cases; test_case->run_case; test_case++)
405 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
408 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
409 * @test: The test context object.
410 * @n: number of elements.
411 * @size: The size in bytes of the desired memory.
412 * @gfp: flags passed to underlying kmalloc().
414 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
415 * and is automatically cleaned up after the test case concludes. See kunit_add_action()
416 * for more information.
418 * Note that some internal context data is also allocated with GFP_KERNEL,
419 * regardless of the gfp passed in.
421 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
424 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
425 * @test: The test context object.
426 * @size: The size in bytes of the desired memory.
427 * @gfp: flags passed to underlying kmalloc().
429 * See kmalloc() and kunit_kmalloc_array() for more information.
431 * Note that some internal context data is also allocated with GFP_KERNEL,
432 * regardless of the gfp passed in.
434 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
436 return kunit_kmalloc_array(test, 1, size, gfp);
440 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
441 * @test: The test case to which the resource belongs.
442 * @ptr: The memory allocation to free.
444 void kunit_kfree(struct kunit *test, const void *ptr);
447 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
448 * @test: The test context object.
449 * @size: The size in bytes of the desired memory.
450 * @gfp: flags passed to underlying kmalloc().
452 * See kzalloc() and kunit_kmalloc_array() for more information.
454 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
456 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
460 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
461 * @test: The test context object.
462 * @n: number of elements.
463 * @size: The size in bytes of the desired memory.
464 * @gfp: flags passed to underlying kmalloc().
466 * See kcalloc() and kunit_kmalloc_array() for more information.
468 static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
470 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
473 void kunit_cleanup(struct kunit *test);
475 void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
478 * kunit_mark_skipped() - Marks @test_or_suite as skipped
480 * @test_or_suite: The test context object.
481 * @fmt: A printk() style format string.
483 * Marks the test as skipped. @fmt is given output as the test status
484 * comment, typically the reason the test was skipped.
486 * Test execution continues after kunit_mark_skipped() is called.
488 #define kunit_mark_skipped(test_or_suite, fmt, ...) \
490 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
491 scnprintf((test_or_suite)->status_comment, \
492 KUNIT_STATUS_COMMENT_SIZE, \
493 fmt, ##__VA_ARGS__); \
497 * kunit_skip() - Marks @test_or_suite as skipped
499 * @test_or_suite: The test context object.
500 * @fmt: A printk() style format string.
502 * Skips the test. @fmt is given output as the test status
503 * comment, typically the reason the test was skipped.
505 * Test execution is halted after kunit_skip() is called.
507 #define kunit_skip(test_or_suite, fmt, ...) \
509 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
510 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
514 * printk and log to per-test or per-suite log buffer. Logging only done
515 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
517 #define kunit_log(lvl, test_or_suite, fmt, ...) \
519 printk(lvl fmt, ##__VA_ARGS__); \
520 kunit_log_append((test_or_suite)->log, fmt, \
524 #define kunit_printk(lvl, test, fmt, ...) \
525 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
526 (test)->name, ##__VA_ARGS__)
529 * kunit_info() - Prints an INFO level message associated with @test.
531 * @test: The test context object.
532 * @fmt: A printk() style format string.
534 * Prints an info level message associated with the test suite being run.
535 * Takes a variable number of format parameters just like printk().
537 #define kunit_info(test, fmt, ...) \
538 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
541 * kunit_warn() - Prints a WARN level message associated with @test.
543 * @test: The test context object.
544 * @fmt: A printk() style format string.
546 * Prints a warning level message.
548 #define kunit_warn(test, fmt, ...) \
549 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
552 * kunit_err() - Prints an ERROR level message associated with @test.
554 * @test: The test context object.
555 * @fmt: A printk() style format string.
557 * Prints an error level message.
559 #define kunit_err(test, fmt, ...) \
560 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
563 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
564 * @test: The test context object.
566 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
567 * words, it does nothing and only exists for code clarity. See
568 * KUNIT_EXPECT_TRUE() for more information.
570 #define KUNIT_SUCCEED(test) do {} while (0)
572 void __noreturn __kunit_abort(struct kunit *test);
574 void __kunit_do_failed_assertion(struct kunit *test,
575 const struct kunit_loc *loc,
576 enum kunit_assert_type type,
577 const struct kunit_assert *assert,
578 assert_format_t assert_format,
579 const char *fmt, ...);
581 #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
582 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
583 const struct assert_class __assertion = INITIALIZER; \
584 __kunit_do_failed_assertion(test, \
587 &__assertion.assert, \
591 if (assert_type == KUNIT_ASSERTION) \
592 __kunit_abort(test); \
596 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
597 _KUNIT_FAILED(test, \
600 kunit_fail_assert_format, \
606 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
607 * @test: The test context object.
608 * @fmt: an informational message to be printed when the assertion is made.
609 * @...: string format arguments.
611 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
612 * other words, it always results in a failed expectation, and consequently
613 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
614 * for more information.
616 #define KUNIT_FAIL(test, fmt, ...) \
617 KUNIT_FAIL_ASSERTION(test, \
622 /* Helper to safely pass around an initializer list to other macros. */
623 #define KUNIT_INIT_ASSERT(initializers...) { initializers }
625 #define KUNIT_UNARY_ASSERTION(test, \
632 if (likely(!!(condition_) == !!expected_true_)) \
635 _KUNIT_FAILED(test, \
637 kunit_unary_assert, \
638 kunit_unary_assert_format, \
639 KUNIT_INIT_ASSERT(.condition = #condition_, \
640 .expected_true = expected_true_), \
645 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
646 KUNIT_UNARY_ASSERTION(test, \
653 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
654 KUNIT_UNARY_ASSERTION(test, \
662 * A factory macro for defining the assertions and expectations for the basic
663 * comparisons defined for the built in types.
665 * Unfortunately, there is no common type that all types can be promoted to for
666 * which all the binary operators behave the same way as for the actual types
667 * (for example, there is no type that long long and unsigned long long can
668 * both be cast to where the comparison result is preserved for all values). So
669 * the best we can do is do the comparison in the original types and then coerce
670 * everything to long long for printing; this way, the comparison behaves
671 * correctly and the printed out value usually makes sense without
672 * interpretation, but can always be interpreted to figure out the actual
675 #define KUNIT_BASE_BINARY_ASSERTION(test, \
685 const typeof(left) __left = (left); \
686 const typeof(right) __right = (right); \
687 static const struct kunit_binary_assert_text __text = { \
689 .left_text = #left, \
690 .right_text = #right, \
693 if (likely(__left op __right)) \
696 _KUNIT_FAILED(test, \
700 KUNIT_INIT_ASSERT(.text = &__text, \
701 .left_value = __left, \
702 .right_value = __right), \
707 #define KUNIT_BINARY_INT_ASSERTION(test, \
714 KUNIT_BASE_BINARY_ASSERTION(test, \
715 kunit_binary_assert, \
716 kunit_binary_assert_format, \
722 #define KUNIT_BINARY_PTR_ASSERTION(test, \
729 KUNIT_BASE_BINARY_ASSERTION(test, \
730 kunit_binary_ptr_assert, \
731 kunit_binary_ptr_assert_format, \
737 #define KUNIT_BINARY_STR_ASSERTION(test, \
745 const char *__left = (left); \
746 const char *__right = (right); \
747 static const struct kunit_binary_assert_text __text = { \
749 .left_text = #left, \
750 .right_text = #right, \
753 if (likely(strcmp(__left, __right) op 0)) \
757 _KUNIT_FAILED(test, \
759 kunit_binary_str_assert, \
760 kunit_binary_str_assert_format, \
761 KUNIT_INIT_ASSERT(.text = &__text, \
762 .left_value = __left, \
763 .right_value = __right), \
768 #define KUNIT_MEM_ASSERTION(test, \
777 const void *__left = (left); \
778 const void *__right = (right); \
779 const size_t __size = (size_); \
780 static const struct kunit_binary_assert_text __text = { \
782 .left_text = #left, \
783 .right_text = #right, \
786 if (likely(__left && __right)) \
787 if (likely(memcmp(__left, __right, __size) op 0)) \
790 _KUNIT_FAILED(test, \
793 kunit_mem_assert_format, \
794 KUNIT_INIT_ASSERT(.text = &__text, \
795 .left_value = __left, \
796 .right_value = __right, \
802 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
808 const typeof(ptr) __ptr = (ptr); \
810 if (!IS_ERR_OR_NULL(__ptr)) \
813 _KUNIT_FAILED(test, \
815 kunit_ptr_not_err_assert, \
816 kunit_ptr_not_err_assert_format, \
817 KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \
823 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
824 * @test: The test context object.
825 * @condition: an arbitrary boolean expression. The test fails when this does
826 * not evaluate to true.
828 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
829 * to fail when the specified condition is not met; however, it will not prevent
830 * the test case from continuing to run; this is otherwise known as an
831 * *expectation failure*.
833 #define KUNIT_EXPECT_TRUE(test, condition) \
834 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
836 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
837 KUNIT_TRUE_MSG_ASSERTION(test, \
844 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
845 * @test: The test context object.
846 * @condition: an arbitrary boolean expression. The test fails when this does
847 * not evaluate to false.
849 * Sets an expectation that @condition evaluates to false. See
850 * KUNIT_EXPECT_TRUE() for more information.
852 #define KUNIT_EXPECT_FALSE(test, condition) \
853 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
855 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
856 KUNIT_FALSE_MSG_ASSERTION(test, \
863 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
864 * @test: The test context object.
865 * @left: an arbitrary expression that evaluates to a primitive C type.
866 * @right: an arbitrary expression that evaluates to a primitive C type.
868 * Sets an expectation that the values that @left and @right evaluate to are
869 * equal. This is semantically equivalent to
870 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
873 #define KUNIT_EXPECT_EQ(test, left, right) \
874 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
876 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
877 KUNIT_BINARY_INT_ASSERTION(test, \
884 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
885 * @test: The test context object.
886 * @left: an arbitrary expression that evaluates to a pointer.
887 * @right: an arbitrary expression that evaluates to a pointer.
889 * Sets an expectation that the values that @left and @right evaluate to are
890 * equal. This is semantically equivalent to
891 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
894 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
895 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
897 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
898 KUNIT_BINARY_PTR_ASSERTION(test, \
905 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
906 * @test: The test context object.
907 * @left: an arbitrary expression that evaluates to a primitive C type.
908 * @right: an arbitrary expression that evaluates to a primitive C type.
910 * Sets an expectation that the values that @left and @right evaluate to are not
911 * equal. This is semantically equivalent to
912 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
915 #define KUNIT_EXPECT_NE(test, left, right) \
916 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
918 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
919 KUNIT_BINARY_INT_ASSERTION(test, \
926 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
927 * @test: The test context object.
928 * @left: an arbitrary expression that evaluates to a pointer.
929 * @right: an arbitrary expression that evaluates to a pointer.
931 * Sets an expectation that the values that @left and @right evaluate to are not
932 * equal. This is semantically equivalent to
933 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
936 #define KUNIT_EXPECT_PTR_NE(test, left, right) \
937 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
939 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
940 KUNIT_BINARY_PTR_ASSERTION(test, \
947 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
948 * @test: The test context object.
949 * @left: an arbitrary expression that evaluates to a primitive C type.
950 * @right: an arbitrary expression that evaluates to a primitive C type.
952 * Sets an expectation that the value that @left evaluates to is less than the
953 * value that @right evaluates to. This is semantically equivalent to
954 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
957 #define KUNIT_EXPECT_LT(test, left, right) \
958 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
960 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
961 KUNIT_BINARY_INT_ASSERTION(test, \
968 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
969 * @test: The test context object.
970 * @left: an arbitrary expression that evaluates to a primitive C type.
971 * @right: an arbitrary expression that evaluates to a primitive C type.
973 * Sets an expectation that the value that @left evaluates to is less than or
974 * equal to the value that @right evaluates to. Semantically this is equivalent
975 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
978 #define KUNIT_EXPECT_LE(test, left, right) \
979 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
981 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
982 KUNIT_BINARY_INT_ASSERTION(test, \
989 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
990 * @test: The test context object.
991 * @left: an arbitrary expression that evaluates to a primitive C type.
992 * @right: an arbitrary expression that evaluates to a primitive C type.
994 * Sets an expectation that the value that @left evaluates to is greater than
995 * the value that @right evaluates to. This is semantically equivalent to
996 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
999 #define KUNIT_EXPECT_GT(test, left, right) \
1000 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
1002 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
1003 KUNIT_BINARY_INT_ASSERTION(test, \
1004 KUNIT_EXPECTATION, \
1010 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1011 * @test: The test context object.
1012 * @left: an arbitrary expression that evaluates to a primitive C type.
1013 * @right: an arbitrary expression that evaluates to a primitive C type.
1015 * Sets an expectation that the value that @left evaluates to is greater than
1016 * the value that @right evaluates to. This is semantically equivalent to
1017 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1020 #define KUNIT_EXPECT_GE(test, left, right) \
1021 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
1023 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
1024 KUNIT_BINARY_INT_ASSERTION(test, \
1025 KUNIT_EXPECTATION, \
1031 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1032 * @test: The test context object.
1033 * @left: an arbitrary expression that evaluates to a null terminated string.
1034 * @right: an arbitrary expression that evaluates to a null terminated string.
1036 * Sets an expectation that the values that @left and @right evaluate to are
1037 * equal. This is semantically equivalent to
1038 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1039 * for more information.
1041 #define KUNIT_EXPECT_STREQ(test, left, right) \
1042 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
1044 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
1045 KUNIT_BINARY_STR_ASSERTION(test, \
1046 KUNIT_EXPECTATION, \
1052 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1053 * @test: The test context object.
1054 * @left: an arbitrary expression that evaluates to a null terminated string.
1055 * @right: an arbitrary expression that evaluates to a null terminated string.
1057 * Sets an expectation that the values that @left and @right evaluate to are
1058 * not equal. This is semantically equivalent to
1059 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1060 * for more information.
1062 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
1063 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
1065 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
1066 KUNIT_BINARY_STR_ASSERTION(test, \
1067 KUNIT_EXPECTATION, \
1073 * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.
1074 * @test: The test context object.
1075 * @left: An arbitrary expression that evaluates to the specified size.
1076 * @right: An arbitrary expression that evaluates to the specified size.
1077 * @size: Number of bytes compared.
1079 * Sets an expectation that the values that @left and @right evaluate to are
1080 * equal. This is semantically equivalent to
1081 * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
1082 * KUNIT_EXPECT_TRUE() for more information.
1084 * Although this expectation works for any memory block, it is not recommended
1085 * for comparing more structured data, such as structs. This expectation is
1086 * recommended for comparing, for example, data arrays.
1088 #define KUNIT_EXPECT_MEMEQ(test, left, right, size) \
1089 KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)
1091 #define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
1092 KUNIT_MEM_ASSERTION(test, \
1093 KUNIT_EXPECTATION, \
1100 * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.
1101 * @test: The test context object.
1102 * @left: An arbitrary expression that evaluates to the specified size.
1103 * @right: An arbitrary expression that evaluates to the specified size.
1104 * @size: Number of bytes compared.
1106 * Sets an expectation that the values that @left and @right evaluate to are
1107 * not equal. This is semantically equivalent to
1108 * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1109 * KUNIT_EXPECT_TRUE() for more information.
1111 * Although this expectation works for any memory block, it is not recommended
1112 * for comparing more structured data, such as structs. This expectation is
1113 * recommended for comparing, for example, data arrays.
1115 #define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \
1116 KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)
1118 #define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1119 KUNIT_MEM_ASSERTION(test, \
1120 KUNIT_EXPECTATION, \
1127 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
1128 * @test: The test context object.
1129 * @ptr: an arbitrary pointer.
1131 * Sets an expectation that the value that @ptr evaluates to is null. This is
1132 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
1133 * See KUNIT_EXPECT_TRUE() for more information.
1135 #define KUNIT_EXPECT_NULL(test, ptr) \
1136 KUNIT_EXPECT_NULL_MSG(test, \
1140 #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
1141 KUNIT_BINARY_PTR_ASSERTION(test, \
1142 KUNIT_EXPECTATION, \
1148 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
1149 * @test: The test context object.
1150 * @ptr: an arbitrary pointer.
1152 * Sets an expectation that the value that @ptr evaluates to is not null. This
1153 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
1154 * See KUNIT_EXPECT_TRUE() for more information.
1156 #define KUNIT_EXPECT_NOT_NULL(test, ptr) \
1157 KUNIT_EXPECT_NOT_NULL_MSG(test, \
1161 #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1162 KUNIT_BINARY_PTR_ASSERTION(test, \
1163 KUNIT_EXPECTATION, \
1169 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1170 * @test: The test context object.
1171 * @ptr: an arbitrary pointer.
1173 * Sets an expectation that the value that @ptr evaluates to is not null and not
1174 * an errno stored in a pointer. This is semantically equivalent to
1175 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1178 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1179 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1181 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1182 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1183 KUNIT_EXPECTATION, \
1188 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1189 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1192 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1193 * @test: The test context object.
1194 * @condition: an arbitrary boolean expression. The test fails and aborts when
1195 * this does not evaluate to true.
1197 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1198 * fail *and immediately abort* when the specified condition is not met. Unlike
1199 * an expectation failure, it will prevent the test case from continuing to run;
1200 * this is otherwise known as an *assertion failure*.
1202 #define KUNIT_ASSERT_TRUE(test, condition) \
1203 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
1205 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1206 KUNIT_TRUE_MSG_ASSERTION(test, \
1213 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1214 * @test: The test context object.
1215 * @condition: an arbitrary boolean expression.
1217 * Sets an assertion that the value that @condition evaluates to is false. This
1218 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1219 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1221 #define KUNIT_ASSERT_FALSE(test, condition) \
1222 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
1224 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1225 KUNIT_FALSE_MSG_ASSERTION(test, \
1232 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1233 * @test: The test context object.
1234 * @left: an arbitrary expression that evaluates to a primitive C type.
1235 * @right: an arbitrary expression that evaluates to a primitive C type.
1237 * Sets an assertion that the values that @left and @right evaluate to are
1238 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1239 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1241 #define KUNIT_ASSERT_EQ(test, left, right) \
1242 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
1244 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1245 KUNIT_BINARY_INT_ASSERTION(test, \
1252 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1253 * @test: The test context object.
1254 * @left: an arbitrary expression that evaluates to a pointer.
1255 * @right: an arbitrary expression that evaluates to a pointer.
1257 * Sets an assertion that the values that @left and @right evaluate to are
1258 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1259 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1261 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1262 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
1264 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1265 KUNIT_BINARY_PTR_ASSERTION(test, \
1272 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1273 * @test: The test context object.
1274 * @left: an arbitrary expression that evaluates to a primitive C type.
1275 * @right: an arbitrary expression that evaluates to a primitive C type.
1277 * Sets an assertion that the values that @left and @right evaluate to are not
1278 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1279 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1281 #define KUNIT_ASSERT_NE(test, left, right) \
1282 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
1284 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1285 KUNIT_BINARY_INT_ASSERTION(test, \
1292 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1293 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1294 * @test: The test context object.
1295 * @left: an arbitrary expression that evaluates to a pointer.
1296 * @right: an arbitrary expression that evaluates to a pointer.
1298 * Sets an assertion that the values that @left and @right evaluate to are not
1299 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1300 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1302 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1303 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1305 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1306 KUNIT_BINARY_PTR_ASSERTION(test, \
1312 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1313 * @test: The test context object.
1314 * @left: an arbitrary expression that evaluates to a primitive C type.
1315 * @right: an arbitrary expression that evaluates to a primitive C type.
1317 * Sets an assertion that the value that @left evaluates to is less than the
1318 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1319 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1322 #define KUNIT_ASSERT_LT(test, left, right) \
1323 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1325 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1326 KUNIT_BINARY_INT_ASSERTION(test, \
1332 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1333 * @test: The test context object.
1334 * @left: an arbitrary expression that evaluates to a primitive C type.
1335 * @right: an arbitrary expression that evaluates to a primitive C type.
1337 * Sets an assertion that the value that @left evaluates to is less than or
1338 * equal to the value that @right evaluates to. This is the same as
1339 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1340 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1342 #define KUNIT_ASSERT_LE(test, left, right) \
1343 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1345 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1346 KUNIT_BINARY_INT_ASSERTION(test, \
1353 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1354 * @test: The test context object.
1355 * @left: an arbitrary expression that evaluates to a primitive C type.
1356 * @right: an arbitrary expression that evaluates to a primitive C type.
1358 * Sets an assertion that the value that @left evaluates to is greater than the
1359 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1360 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1363 #define KUNIT_ASSERT_GT(test, left, right) \
1364 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1366 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1367 KUNIT_BINARY_INT_ASSERTION(test, \
1374 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1375 * @test: The test context object.
1376 * @left: an arbitrary expression that evaluates to a primitive C type.
1377 * @right: an arbitrary expression that evaluates to a primitive C type.
1379 * Sets an assertion that the value that @left evaluates to is greater than the
1380 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1381 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1384 #define KUNIT_ASSERT_GE(test, left, right) \
1385 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1387 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1388 KUNIT_BINARY_INT_ASSERTION(test, \
1395 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1396 * @test: The test context object.
1397 * @left: an arbitrary expression that evaluates to a null terminated string.
1398 * @right: an arbitrary expression that evaluates to a null terminated string.
1400 * Sets an assertion that the values that @left and @right evaluate to are
1401 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1402 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1404 #define KUNIT_ASSERT_STREQ(test, left, right) \
1405 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1407 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1408 KUNIT_BINARY_STR_ASSERTION(test, \
1415 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1416 * @test: The test context object.
1417 * @left: an arbitrary expression that evaluates to a null terminated string.
1418 * @right: an arbitrary expression that evaluates to a null terminated string.
1420 * Sets an expectation that the values that @left and @right evaluate to are
1421 * not equal. This is semantically equivalent to
1422 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1423 * for more information.
1425 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1426 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1428 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1429 KUNIT_BINARY_STR_ASSERTION(test, \
1436 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1437 * @test: The test context object.
1438 * @ptr: an arbitrary pointer.
1440 * Sets an assertion that the values that @ptr evaluates to is null. This is
1441 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1442 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1444 #define KUNIT_ASSERT_NULL(test, ptr) \
1445 KUNIT_ASSERT_NULL_MSG(test, \
1449 #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1450 KUNIT_BINARY_PTR_ASSERTION(test, \
1457 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1458 * @test: The test context object.
1459 * @ptr: an arbitrary pointer.
1461 * Sets an assertion that the values that @ptr evaluates to is not null. This
1462 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1463 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1465 #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1466 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1470 #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1471 KUNIT_BINARY_PTR_ASSERTION(test, \
1478 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1479 * @test: The test context object.
1480 * @ptr: an arbitrary pointer.
1482 * Sets an assertion that the value that @ptr evaluates to is not null and not
1483 * an errno stored in a pointer. This is the same as
1484 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1485 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1487 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1488 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1490 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1491 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1498 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1499 * @name: prefix for the test parameter generator function.
1500 * @array: array of test parameters.
1501 * @get_desc: function to convert param to description; NULL to use default
1503 * Define function @name_gen_params which uses @array to generate parameters.
1505 #define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1506 static const void *name##_gen_params(const void *prev, char *desc) \
1508 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1509 if (__next - (array) < ARRAY_SIZE((array))) { \
1510 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1512 __get_desc(__next, desc); \
1518 // TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1519 // include resource.h themselves if they need it.
1520 #include <kunit/resource.h>
1522 #endif /* _KUNIT_TEST_H */