1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Base unit test (KUnit) API.
5 * Copyright (C) 2019, Google LLC.
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/kconfig.h>
20 #include <linux/kref.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/string.h>
26 #include <linux/types.h>
28 #include <asm/rwonce.h>
32 /* Size of log associated with test. */
33 #define KUNIT_LOG_SIZE 512
35 /* Maximum size of parameter description string. */
36 #define KUNIT_PARAM_DESC_SIZE 128
38 /* Maximum size of a status comment. */
39 #define KUNIT_STATUS_COMMENT_SIZE 256
42 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
43 * sub-subtest. See the "Subtests" section in
44 * https://node-tap.org/tap-protocol/
46 #define KUNIT_SUBTEST_INDENT " "
47 #define KUNIT_SUBSUBTEST_INDENT " "
50 * enum kunit_status - Type of result for a test or test suite
51 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
52 * @KUNIT_FAILURE: Denotes the test has failed.
53 * @KUNIT_SKIPPED: Denotes the test has been skipped.
62 * struct kunit_case - represents an individual test case.
64 * @run_case: the function representing the actual test case.
65 * @name: the name of the test case.
66 * @generate_params: the generator function for parameterized tests.
68 * A test case is a function with the signature,
69 * ``void (*)(struct kunit *)``
70 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
71 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
72 * with a &struct kunit_suite and will be run after the suite's init
73 * function and followed by the suite's exit function.
75 * A test case should be static and should only be created with the
76 * KUNIT_CASE() macro; additionally, every array of test cases should be
77 * terminated with an empty test case.
83 * void add_test_basic(struct kunit *test)
85 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
86 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
87 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
88 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
89 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
92 * static struct kunit_case example_test_cases[] = {
93 * KUNIT_CASE(add_test_basic),
99 void (*run_case)(struct kunit *test);
101 const void* (*generate_params)(const void *prev, char *desc);
103 /* private: internal use only. */
104 enum kunit_status status;
108 static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
121 * KUNIT_CASE - A helper for creating a &struct kunit_case
123 * @test_name: a reference to a test case function.
125 * Takes a symbol for a function representing a test case and creates a
126 * &struct kunit_case object from it. See the documentation for
127 * &struct kunit_case for an example on how to use it.
129 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
132 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
134 * @test_name: a reference to a test case function.
135 * @gen_params: a reference to a parameter generator function.
137 * The generator function::
139 * const void* gen_params(const void *prev, char *desc)
141 * is used to lazily generate a series of arbitrarily typed values that fit into
142 * a void*. The argument @prev is the previously returned value, which should be
143 * used to derive the next value; @prev is set to NULL on the initial generator
144 * call. When no more values are available, the generator must return NULL.
145 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
146 * describing the parameter.
148 #define KUNIT_CASE_PARAM(test_name, gen_params) \
149 { .run_case = test_name, .name = #test_name, \
150 .generate_params = gen_params }
153 * struct kunit_suite - describes a related collection of &struct kunit_case
155 * @name: the name of the test. Purely informational.
156 * @suite_init: called once per test suite before the test cases.
157 * @suite_exit: called once per test suite after all test cases.
158 * @init: called before every test case.
159 * @exit: called after every test case.
160 * @test_cases: a null terminated array of test cases.
162 * A kunit_suite is a collection of related &struct kunit_case s, such that
163 * @init is called before every test case and @exit is called after every
164 * test case, similar to the notion of a *test fixture* or a *test class*
165 * in other unit testing frameworks like JUnit or Googletest.
167 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
171 const char name[256];
172 int (*suite_init)(struct kunit_suite *suite);
173 void (*suite_exit)(struct kunit_suite *suite);
174 int (*init)(struct kunit *test);
175 void (*exit)(struct kunit *test);
176 struct kunit_case *test_cases;
178 /* private: internal use only */
179 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
180 struct dentry *debugfs;
186 * struct kunit - represents a running instance of a test.
188 * @priv: for user to store arbitrary data. Commonly used to pass data
189 * created in the init function (see &struct kunit_suite).
191 * Used to store information about the current context under which the test
192 * is running. Most of this data is private and should only be accessed
193 * indirectly via public functions; the one exception is @priv which can be
194 * used by the test writer to store arbitrary data.
199 /* private: internal use only. */
200 const char *name; /* Read only after initialization! */
201 char *log; /* Points at case log after initialization */
202 struct kunit_try_catch try_catch;
203 /* param_value is the current parameter value for a test case. */
204 const void *param_value;
205 /* param_index stores the index of the parameter in parameterized tests. */
208 * success starts as true, and may only be set to false during a
209 * test case; thus, it is safe to update this across multiple
210 * threads using WRITE_ONCE; however, as a consequence, it may only
211 * be read after the test case finishes once all threads associated
212 * with the test case have terminated.
214 spinlock_t lock; /* Guards all mutable test state. */
215 enum kunit_status status; /* Read only after test_case finishes! */
217 * Because resources is a list that may be updated multiple times (with
218 * new resources) from any thread associated with a test case, we must
219 * protect it with some type of lock.
221 struct list_head resources; /* Protected by lock. */
223 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
226 static inline void kunit_set_failure(struct kunit *test)
228 WRITE_ONCE(test->status, KUNIT_FAILURE);
231 void kunit_init_test(struct kunit *test, const char *name, char *log);
233 int kunit_run_tests(struct kunit_suite *suite);
235 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
237 unsigned int kunit_test_case_num(struct kunit_suite *suite,
238 struct kunit_case *test_case);
240 int __kunit_test_suites_init(struct kunit_suite * const * const suites);
242 void __kunit_test_suites_exit(struct kunit_suite **suites);
244 #if IS_BUILTIN(CONFIG_KUNIT)
245 int kunit_run_all_tests(void);
247 static inline int kunit_run_all_tests(void)
251 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
255 * kunit_test_suites_for_module() - used to register one or more
256 * &struct kunit_suite with KUnit.
258 * @__suites: a statically allocated list of &struct kunit_suite.
260 * Registers @__suites with the test framework. See &struct kunit_suite for
263 * If a test suite is built-in, module_init() gets translated into
264 * an initcall which we don't want as the idea is that for builtins
265 * the executor will manage execution. So ensure we do not define
266 * module_{init|exit} functions for the builtin case when registering
267 * suites via kunit_test_suites() below.
269 #define kunit_test_suites_for_module(__suites) \
270 static int __init kunit_test_suites_init(void) \
272 return __kunit_test_suites_init(__suites); \
274 module_init(kunit_test_suites_init); \
276 static void __exit kunit_test_suites_exit(void) \
278 return __kunit_test_suites_exit(__suites); \
280 module_exit(kunit_test_suites_exit)
282 #define kunit_test_suites_for_module(__suites)
285 #define __kunit_test_suites(unique_array, unique_suites, ...) \
286 static struct kunit_suite *unique_array[] = { __VA_ARGS__, NULL }; \
287 kunit_test_suites_for_module(unique_array); \
288 static struct kunit_suite **unique_suites \
289 __used __section(".kunit_test_suites") = unique_array
292 * kunit_test_suites() - used to register one or more &struct kunit_suite
295 * @__suites: a statically allocated list of &struct kunit_suite.
297 * Registers @suites with the test framework. See &struct kunit_suite for
300 * When builtin, KUnit tests are all run via executor; this is done
301 * by placing the array of struct kunit_suite * in the .kunit_test_suites
304 * An alternative is to build the tests as a module. Because modules do not
305 * support multiple initcall()s, we need to initialize an array of suites for a
309 #define kunit_test_suites(__suites...) \
310 __kunit_test_suites(__UNIQUE_ID(array), \
311 __UNIQUE_ID(suites), \
314 #define kunit_test_suite(suite) kunit_test_suites(&suite)
317 * kunit_test_init_section_suites() - used to register one or more &struct
318 * kunit_suite containing init functions or
321 * @__suites: a statically allocated list of &struct kunit_suite.
323 * This functions identically as &kunit_test_suites() except that it suppresses
324 * modpost warnings for referencing functions marked __init or data marked
325 * __initdata; this is OK because currently KUnit only runs tests upon boot
326 * during the init phase or upon loading a module during the init phase.
328 * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
329 * tests must be excluded.
331 * The only thing this macro does that's different from kunit_test_suites is
332 * that it suffixes the array and suite declarations it makes with _probe;
333 * modpost suppresses warnings about referencing init data for symbols named in
336 #define kunit_test_init_section_suites(__suites...) \
337 __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
338 CONCATENATE(__UNIQUE_ID(suites), _probe), \
341 #define kunit_test_init_section_suite(suite) \
342 kunit_test_init_section_suites(&suite)
344 #define kunit_suite_for_each_test_case(suite, test_case) \
345 for (test_case = suite->test_cases; test_case->run_case; test_case++)
347 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
350 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
351 * @test: The test context object.
352 * @n: number of elements.
353 * @size: The size in bytes of the desired memory.
354 * @gfp: flags passed to underlying kmalloc().
356 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
357 * and is automatically cleaned up after the test case concludes. See &struct
358 * kunit_resource for more information.
360 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
363 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
364 * @test: The test context object.
365 * @size: The size in bytes of the desired memory.
366 * @gfp: flags passed to underlying kmalloc().
368 * See kmalloc() and kunit_kmalloc_array() for more information.
370 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
372 return kunit_kmalloc_array(test, 1, size, gfp);
376 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
377 * @test: The test case to which the resource belongs.
378 * @ptr: The memory allocation to free.
380 void kunit_kfree(struct kunit *test, const void *ptr);
383 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
384 * @test: The test context object.
385 * @size: The size in bytes of the desired memory.
386 * @gfp: flags passed to underlying kmalloc().
388 * See kzalloc() and kunit_kmalloc_array() for more information.
390 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
392 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
396 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
397 * @test: The test context object.
398 * @n: number of elements.
399 * @size: The size in bytes of the desired memory.
400 * @gfp: flags passed to underlying kmalloc().
402 * See kcalloc() and kunit_kmalloc_array() for more information.
404 static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
406 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
409 void kunit_cleanup(struct kunit *test);
411 void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
414 * kunit_mark_skipped() - Marks @test_or_suite as skipped
416 * @test_or_suite: The test context object.
417 * @fmt: A printk() style format string.
419 * Marks the test as skipped. @fmt is given output as the test status
420 * comment, typically the reason the test was skipped.
422 * Test execution continues after kunit_mark_skipped() is called.
424 #define kunit_mark_skipped(test_or_suite, fmt, ...) \
426 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
427 scnprintf((test_or_suite)->status_comment, \
428 KUNIT_STATUS_COMMENT_SIZE, \
429 fmt, ##__VA_ARGS__); \
433 * kunit_skip() - Marks @test_or_suite as skipped
435 * @test_or_suite: The test context object.
436 * @fmt: A printk() style format string.
438 * Skips the test. @fmt is given output as the test status
439 * comment, typically the reason the test was skipped.
441 * Test execution is halted after kunit_skip() is called.
443 #define kunit_skip(test_or_suite, fmt, ...) \
445 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
446 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
450 * printk and log to per-test or per-suite log buffer. Logging only done
451 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
453 #define kunit_log(lvl, test_or_suite, fmt, ...) \
455 printk(lvl fmt, ##__VA_ARGS__); \
456 kunit_log_append((test_or_suite)->log, fmt "\n", \
460 #define kunit_printk(lvl, test, fmt, ...) \
461 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
462 (test)->name, ##__VA_ARGS__)
465 * kunit_info() - Prints an INFO level message associated with @test.
467 * @test: The test context object.
468 * @fmt: A printk() style format string.
470 * Prints an info level message associated with the test suite being run.
471 * Takes a variable number of format parameters just like printk().
473 #define kunit_info(test, fmt, ...) \
474 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
477 * kunit_warn() - Prints a WARN level message associated with @test.
479 * @test: The test context object.
480 * @fmt: A printk() style format string.
482 * Prints a warning level message.
484 #define kunit_warn(test, fmt, ...) \
485 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
488 * kunit_err() - Prints an ERROR level message associated with @test.
490 * @test: The test context object.
491 * @fmt: A printk() style format string.
493 * Prints an error level message.
495 #define kunit_err(test, fmt, ...) \
496 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
499 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
500 * @test: The test context object.
502 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
503 * words, it does nothing and only exists for code clarity. See
504 * KUNIT_EXPECT_TRUE() for more information.
506 #define KUNIT_SUCCEED(test) do {} while (0)
508 void kunit_do_failed_assertion(struct kunit *test,
509 const struct kunit_loc *loc,
510 enum kunit_assert_type type,
511 const struct kunit_assert *assert,
512 const char *fmt, ...);
514 #define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
515 if (unlikely(!(pass))) { \
516 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
517 struct assert_class __assertion = INITIALIZER; \
518 kunit_do_failed_assertion(test, \
521 &__assertion.assert, \
528 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
529 KUNIT_ASSERTION(test, \
533 KUNIT_INIT_FAIL_ASSERT_STRUCT, \
538 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
539 * @test: The test context object.
540 * @fmt: an informational message to be printed when the assertion is made.
541 * @...: string format arguments.
543 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
544 * other words, it always results in a failed expectation, and consequently
545 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
546 * for more information.
548 #define KUNIT_FAIL(test, fmt, ...) \
549 KUNIT_FAIL_ASSERTION(test, \
554 #define KUNIT_UNARY_ASSERTION(test, \
560 KUNIT_ASSERTION(test, \
562 !!(condition) == !!expected_true, \
563 kunit_unary_assert, \
564 KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
569 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
570 KUNIT_UNARY_ASSERTION(test, \
577 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
578 KUNIT_UNARY_ASSERTION(test, \
586 * A factory macro for defining the assertions and expectations for the basic
587 * comparisons defined for the built in types.
589 * Unfortunately, there is no common type that all types can be promoted to for
590 * which all the binary operators behave the same way as for the actual types
591 * (for example, there is no type that long long and unsigned long long can
592 * both be cast to where the comparison result is preserved for all values). So
593 * the best we can do is do the comparison in the original types and then coerce
594 * everything to long long for printing; this way, the comparison behaves
595 * correctly and the printed out value usually makes sense without
596 * interpretation, but can always be interpreted to figure out the actual
599 #define KUNIT_BASE_BINARY_ASSERTION(test, \
609 const typeof(left) __left = (left); \
610 const typeof(right) __right = (right); \
611 static const struct kunit_binary_assert_text __text = { \
613 .left_text = #left, \
614 .right_text = #right, \
617 KUNIT_ASSERTION(test, \
621 KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \
629 #define KUNIT_BINARY_INT_ASSERTION(test, \
636 KUNIT_BASE_BINARY_ASSERTION(test, \
637 kunit_binary_assert, \
638 kunit_binary_assert_format, \
644 #define KUNIT_BINARY_PTR_ASSERTION(test, \
651 KUNIT_BASE_BINARY_ASSERTION(test, \
652 kunit_binary_ptr_assert, \
653 kunit_binary_ptr_assert_format, \
659 #define KUNIT_BINARY_STR_ASSERTION(test, \
667 const char *__left = (left); \
668 const char *__right = (right); \
669 static const struct kunit_binary_assert_text __text = { \
671 .left_text = #left, \
672 .right_text = #right, \
675 KUNIT_ASSERTION(test, \
677 strcmp(__left, __right) op 0, \
678 kunit_binary_str_assert, \
679 KUNIT_INIT_BINARY_ASSERT_STRUCT(kunit_binary_str_assert_format,\
687 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
693 const typeof(ptr) __ptr = (ptr); \
695 KUNIT_ASSERTION(test, \
697 !IS_ERR_OR_NULL(__ptr), \
698 kunit_ptr_not_err_assert, \
699 KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \
706 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
707 * @test: The test context object.
708 * @condition: an arbitrary boolean expression. The test fails when this does
709 * not evaluate to true.
711 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
712 * to fail when the specified condition is not met; however, it will not prevent
713 * the test case from continuing to run; this is otherwise known as an
714 * *expectation failure*.
716 #define KUNIT_EXPECT_TRUE(test, condition) \
717 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
719 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
720 KUNIT_TRUE_MSG_ASSERTION(test, \
727 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
728 * @test: The test context object.
729 * @condition: an arbitrary boolean expression. The test fails when this does
730 * not evaluate to false.
732 * Sets an expectation that @condition evaluates to false. See
733 * KUNIT_EXPECT_TRUE() for more information.
735 #define KUNIT_EXPECT_FALSE(test, condition) \
736 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
738 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
739 KUNIT_FALSE_MSG_ASSERTION(test, \
746 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
747 * @test: The test context object.
748 * @left: an arbitrary expression that evaluates to a primitive C type.
749 * @right: an arbitrary expression that evaluates to a primitive C type.
751 * Sets an expectation that the values that @left and @right evaluate to are
752 * equal. This is semantically equivalent to
753 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
756 #define KUNIT_EXPECT_EQ(test, left, right) \
757 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
759 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
760 KUNIT_BINARY_INT_ASSERTION(test, \
767 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
768 * @test: The test context object.
769 * @left: an arbitrary expression that evaluates to a pointer.
770 * @right: an arbitrary expression that evaluates to a pointer.
772 * Sets an expectation that the values that @left and @right evaluate to are
773 * equal. This is semantically equivalent to
774 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
777 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
778 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
780 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
781 KUNIT_BINARY_PTR_ASSERTION(test, \
788 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
789 * @test: The test context object.
790 * @left: an arbitrary expression that evaluates to a primitive C type.
791 * @right: an arbitrary expression that evaluates to a primitive C type.
793 * Sets an expectation that the values that @left and @right evaluate to are not
794 * equal. This is semantically equivalent to
795 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
798 #define KUNIT_EXPECT_NE(test, left, right) \
799 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
801 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
802 KUNIT_BINARY_INT_ASSERTION(test, \
809 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
810 * @test: The test context object.
811 * @left: an arbitrary expression that evaluates to a pointer.
812 * @right: an arbitrary expression that evaluates to a pointer.
814 * Sets an expectation that the values that @left and @right evaluate to are not
815 * equal. This is semantically equivalent to
816 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
819 #define KUNIT_EXPECT_PTR_NE(test, left, right) \
820 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
822 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
823 KUNIT_BINARY_PTR_ASSERTION(test, \
830 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
831 * @test: The test context object.
832 * @left: an arbitrary expression that evaluates to a primitive C type.
833 * @right: an arbitrary expression that evaluates to a primitive C type.
835 * Sets an expectation that the value that @left evaluates to is less than the
836 * value that @right evaluates to. This is semantically equivalent to
837 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
840 #define KUNIT_EXPECT_LT(test, left, right) \
841 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
843 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
844 KUNIT_BINARY_INT_ASSERTION(test, \
851 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
852 * @test: The test context object.
853 * @left: an arbitrary expression that evaluates to a primitive C type.
854 * @right: an arbitrary expression that evaluates to a primitive C type.
856 * Sets an expectation that the value that @left evaluates to is less than or
857 * equal to the value that @right evaluates to. Semantically this is equivalent
858 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
861 #define KUNIT_EXPECT_LE(test, left, right) \
862 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
864 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
865 KUNIT_BINARY_INT_ASSERTION(test, \
872 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
873 * @test: The test context object.
874 * @left: an arbitrary expression that evaluates to a primitive C type.
875 * @right: an arbitrary expression that evaluates to a primitive C type.
877 * Sets an expectation that the value that @left evaluates to is greater than
878 * the value that @right evaluates to. This is semantically equivalent to
879 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
882 #define KUNIT_EXPECT_GT(test, left, right) \
883 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
885 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
886 KUNIT_BINARY_INT_ASSERTION(test, \
893 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
894 * @test: The test context object.
895 * @left: an arbitrary expression that evaluates to a primitive C type.
896 * @right: an arbitrary expression that evaluates to a primitive C type.
898 * Sets an expectation that the value that @left evaluates to is greater than
899 * the value that @right evaluates to. This is semantically equivalent to
900 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
903 #define KUNIT_EXPECT_GE(test, left, right) \
904 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
906 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
907 KUNIT_BINARY_INT_ASSERTION(test, \
914 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
915 * @test: The test context object.
916 * @left: an arbitrary expression that evaluates to a null terminated string.
917 * @right: an arbitrary expression that evaluates to a null terminated string.
919 * Sets an expectation that the values that @left and @right evaluate to are
920 * equal. This is semantically equivalent to
921 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
922 * for more information.
924 #define KUNIT_EXPECT_STREQ(test, left, right) \
925 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
927 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
928 KUNIT_BINARY_STR_ASSERTION(test, \
935 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
936 * @test: The test context object.
937 * @left: an arbitrary expression that evaluates to a null terminated string.
938 * @right: an arbitrary expression that evaluates to a null terminated string.
940 * Sets an expectation that the values that @left and @right evaluate to are
941 * not equal. This is semantically equivalent to
942 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
943 * for more information.
945 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
946 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
948 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
949 KUNIT_BINARY_STR_ASSERTION(test, \
956 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
957 * @test: The test context object.
958 * @ptr: an arbitrary pointer.
960 * Sets an expectation that the value that @ptr evaluates to is null. This is
961 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
962 * See KUNIT_EXPECT_TRUE() for more information.
964 #define KUNIT_EXPECT_NULL(test, ptr) \
965 KUNIT_EXPECT_NULL_MSG(test, \
969 #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
970 KUNIT_BINARY_PTR_ASSERTION(test, \
977 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
978 * @test: The test context object.
979 * @ptr: an arbitrary pointer.
981 * Sets an expectation that the value that @ptr evaluates to is not null. This
982 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
983 * See KUNIT_EXPECT_TRUE() for more information.
985 #define KUNIT_EXPECT_NOT_NULL(test, ptr) \
986 KUNIT_EXPECT_NOT_NULL_MSG(test, \
990 #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
991 KUNIT_BINARY_PTR_ASSERTION(test, \
998 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
999 * @test: The test context object.
1000 * @ptr: an arbitrary pointer.
1002 * Sets an expectation that the value that @ptr evaluates to is not null and not
1003 * an errno stored in a pointer. This is semantically equivalent to
1004 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1007 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1008 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1010 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1011 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1012 KUNIT_EXPECTATION, \
1017 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1018 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1021 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1022 * @test: The test context object.
1023 * @condition: an arbitrary boolean expression. The test fails and aborts when
1024 * this does not evaluate to true.
1026 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1027 * fail *and immediately abort* when the specified condition is not met. Unlike
1028 * an expectation failure, it will prevent the test case from continuing to run;
1029 * this is otherwise known as an *assertion failure*.
1031 #define KUNIT_ASSERT_TRUE(test, condition) \
1032 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
1034 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1035 KUNIT_TRUE_MSG_ASSERTION(test, \
1042 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1043 * @test: The test context object.
1044 * @condition: an arbitrary boolean expression.
1046 * Sets an assertion that the value that @condition evaluates to is false. This
1047 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1048 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1050 #define KUNIT_ASSERT_FALSE(test, condition) \
1051 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
1053 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1054 KUNIT_FALSE_MSG_ASSERTION(test, \
1061 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1062 * @test: The test context object.
1063 * @left: an arbitrary expression that evaluates to a primitive C type.
1064 * @right: an arbitrary expression that evaluates to a primitive C type.
1066 * Sets an assertion that the values that @left and @right evaluate to are
1067 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1068 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1070 #define KUNIT_ASSERT_EQ(test, left, right) \
1071 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
1073 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1074 KUNIT_BINARY_INT_ASSERTION(test, \
1081 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1082 * @test: The test context object.
1083 * @left: an arbitrary expression that evaluates to a pointer.
1084 * @right: an arbitrary expression that evaluates to a pointer.
1086 * Sets an assertion that the values that @left and @right evaluate to are
1087 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1088 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1090 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1091 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
1093 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1094 KUNIT_BINARY_PTR_ASSERTION(test, \
1101 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1102 * @test: The test context object.
1103 * @left: an arbitrary expression that evaluates to a primitive C type.
1104 * @right: an arbitrary expression that evaluates to a primitive C type.
1106 * Sets an assertion that the values that @left and @right evaluate to are not
1107 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1108 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1110 #define KUNIT_ASSERT_NE(test, left, right) \
1111 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
1113 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1114 KUNIT_BINARY_INT_ASSERTION(test, \
1121 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1122 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1123 * @test: The test context object.
1124 * @left: an arbitrary expression that evaluates to a pointer.
1125 * @right: an arbitrary expression that evaluates to a pointer.
1127 * Sets an assertion that the values that @left and @right evaluate to are not
1128 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1129 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1131 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1132 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1134 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1135 KUNIT_BINARY_PTR_ASSERTION(test, \
1141 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1142 * @test: The test context object.
1143 * @left: an arbitrary expression that evaluates to a primitive C type.
1144 * @right: an arbitrary expression that evaluates to a primitive C type.
1146 * Sets an assertion that the value that @left evaluates to is less than the
1147 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1148 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1151 #define KUNIT_ASSERT_LT(test, left, right) \
1152 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1154 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1155 KUNIT_BINARY_INT_ASSERTION(test, \
1156 KUNIT_EXPECTATION, \
1161 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1162 * @test: The test context object.
1163 * @left: an arbitrary expression that evaluates to a primitive C type.
1164 * @right: an arbitrary expression that evaluates to a primitive C type.
1166 * Sets an assertion that the value that @left evaluates to is less than or
1167 * equal to the value that @right evaluates to. This is the same as
1168 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1169 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1171 #define KUNIT_ASSERT_LE(test, left, right) \
1172 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1174 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1175 KUNIT_BINARY_INT_ASSERTION(test, \
1182 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1183 * @test: The test context object.
1184 * @left: an arbitrary expression that evaluates to a primitive C type.
1185 * @right: an arbitrary expression that evaluates to a primitive C type.
1187 * Sets an assertion that the value that @left evaluates to is greater than the
1188 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1189 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1192 #define KUNIT_ASSERT_GT(test, left, right) \
1193 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1195 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1196 KUNIT_BINARY_INT_ASSERTION(test, \
1197 KUNIT_EXPECTATION, \
1203 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1204 * @test: The test context object.
1205 * @left: an arbitrary expression that evaluates to a primitive C type.
1206 * @right: an arbitrary expression that evaluates to a primitive C type.
1208 * Sets an assertion that the value that @left evaluates to is greater than the
1209 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1210 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1213 #define KUNIT_ASSERT_GE(test, left, right) \
1214 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1216 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1217 KUNIT_BINARY_INT_ASSERTION(test, \
1224 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1225 * @test: The test context object.
1226 * @left: an arbitrary expression that evaluates to a null terminated string.
1227 * @right: an arbitrary expression that evaluates to a null terminated string.
1229 * Sets an assertion that the values that @left and @right evaluate to are
1230 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1231 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1233 #define KUNIT_ASSERT_STREQ(test, left, right) \
1234 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1236 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1237 KUNIT_BINARY_STR_ASSERTION(test, \
1244 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1245 * @test: The test context object.
1246 * @left: an arbitrary expression that evaluates to a null terminated string.
1247 * @right: an arbitrary expression that evaluates to a null terminated string.
1249 * Sets an expectation that the values that @left and @right evaluate to are
1250 * not equal. This is semantically equivalent to
1251 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1252 * for more information.
1254 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1255 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1257 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1258 KUNIT_BINARY_STR_ASSERTION(test, \
1265 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1266 * @test: The test context object.
1267 * @ptr: an arbitrary pointer.
1269 * Sets an assertion that the values that @ptr evaluates to is null. This is
1270 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1271 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1273 #define KUNIT_ASSERT_NULL(test, ptr) \
1274 KUNIT_ASSERT_NULL_MSG(test, \
1278 #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1279 KUNIT_BINARY_PTR_ASSERTION(test, \
1286 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1287 * @test: The test context object.
1288 * @ptr: an arbitrary pointer.
1290 * Sets an assertion that the values that @ptr evaluates to is not null. This
1291 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1292 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1294 #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1295 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1299 #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1300 KUNIT_BINARY_PTR_ASSERTION(test, \
1307 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1308 * @test: The test context object.
1309 * @ptr: an arbitrary pointer.
1311 * Sets an assertion that the value that @ptr evaluates to is not null and not
1312 * an errno stored in a pointer. This is the same as
1313 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1314 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1316 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1317 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1319 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1320 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1327 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1328 * @name: prefix for the test parameter generator function.
1329 * @array: array of test parameters.
1330 * @get_desc: function to convert param to description; NULL to use default
1332 * Define function @name_gen_params which uses @array to generate parameters.
1334 #define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1335 static const void *name##_gen_params(const void *prev, char *desc) \
1337 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1338 if (__next - (array) < ARRAY_SIZE((array))) { \
1339 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1341 __get_desc(__next, desc); \
1348 // include resource.h themselves if they need it.
1349 #include <kunit/resource.h>
1351 #endif /* _KUNIT_TEST_H */