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/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, int num_suites);
242 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_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) */
253 #define __kunit_test_suites(unique_array, ...) \
254 MODULE_INFO(test, "Y"); \
255 static struct kunit_suite *unique_array[] \
256 __aligned(sizeof(struct kunit_suite *)) \
257 __used __section(".kunit_test_suites") = { __VA_ARGS__ }
260 * kunit_test_suites() - used to register one or more &struct kunit_suite
263 * @__suites: a statically allocated list of &struct kunit_suite.
265 * Registers @suites with the test framework.
266 * This is done by placing the array of struct kunit_suite * in the
267 * .kunit_test_suites ELF section.
269 * When builtin, KUnit tests are all run via the executor at boot, and when
270 * built as a module, they run on module load.
273 #define kunit_test_suites(__suites...) \
274 __kunit_test_suites(__UNIQUE_ID(array), \
277 #define kunit_test_suite(suite) kunit_test_suites(&suite)
280 * kunit_test_init_section_suites() - used to register one or more &struct
281 * kunit_suite containing init functions or
284 * @__suites: a statically allocated list of &struct kunit_suite.
286 * This functions identically as kunit_test_suites() except that it suppresses
287 * modpost warnings for referencing functions marked __init or data marked
288 * __initdata; this is OK because currently KUnit only runs tests upon boot
289 * during the init phase or upon loading a module during the init phase.
291 * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
292 * tests must be excluded.
294 * The only thing this macro does that's different from kunit_test_suites is
295 * that it suffixes the array and suite declarations it makes with _probe;
296 * modpost suppresses warnings about referencing init data for symbols named in
299 #define kunit_test_init_section_suites(__suites...) \
300 __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
301 CONCATENATE(__UNIQUE_ID(suites), _probe), \
304 #define kunit_test_init_section_suite(suite) \
305 kunit_test_init_section_suites(&suite)
307 #define kunit_suite_for_each_test_case(suite, test_case) \
308 for (test_case = suite->test_cases; test_case->run_case; test_case++)
310 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
313 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
314 * @test: The test context object.
315 * @n: number of elements.
316 * @size: The size in bytes of the desired memory.
317 * @gfp: flags passed to underlying kmalloc().
319 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
320 * and is automatically cleaned up after the test case concludes. See &struct
321 * kunit_resource for more information.
323 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
326 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
327 * @test: The test context object.
328 * @size: The size in bytes of the desired memory.
329 * @gfp: flags passed to underlying kmalloc().
331 * See kmalloc() and kunit_kmalloc_array() for more information.
333 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
335 return kunit_kmalloc_array(test, 1, size, gfp);
339 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
340 * @test: The test case to which the resource belongs.
341 * @ptr: The memory allocation to free.
343 void kunit_kfree(struct kunit *test, const void *ptr);
346 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
347 * @test: The test context object.
348 * @size: The size in bytes of the desired memory.
349 * @gfp: flags passed to underlying kmalloc().
351 * See kzalloc() and kunit_kmalloc_array() for more information.
353 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
355 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
359 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
360 * @test: The test context object.
361 * @n: number of elements.
362 * @size: The size in bytes of the desired memory.
363 * @gfp: flags passed to underlying kmalloc().
365 * See kcalloc() and kunit_kmalloc_array() for more information.
367 static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
369 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
372 void kunit_cleanup(struct kunit *test);
374 void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
377 * kunit_mark_skipped() - Marks @test_or_suite as skipped
379 * @test_or_suite: The test context object.
380 * @fmt: A printk() style format string.
382 * Marks the test as skipped. @fmt is given output as the test status
383 * comment, typically the reason the test was skipped.
385 * Test execution continues after kunit_mark_skipped() is called.
387 #define kunit_mark_skipped(test_or_suite, fmt, ...) \
389 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
390 scnprintf((test_or_suite)->status_comment, \
391 KUNIT_STATUS_COMMENT_SIZE, \
392 fmt, ##__VA_ARGS__); \
396 * kunit_skip() - Marks @test_or_suite as skipped
398 * @test_or_suite: The test context object.
399 * @fmt: A printk() style format string.
401 * Skips the test. @fmt is given output as the test status
402 * comment, typically the reason the test was skipped.
404 * Test execution is halted after kunit_skip() is called.
406 #define kunit_skip(test_or_suite, fmt, ...) \
408 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
409 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
413 * printk and log to per-test or per-suite log buffer. Logging only done
414 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
416 #define kunit_log(lvl, test_or_suite, fmt, ...) \
418 printk(lvl fmt, ##__VA_ARGS__); \
419 kunit_log_append((test_or_suite)->log, fmt "\n", \
423 #define kunit_printk(lvl, test, fmt, ...) \
424 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
425 (test)->name, ##__VA_ARGS__)
428 * kunit_info() - Prints an INFO level message associated with @test.
430 * @test: The test context object.
431 * @fmt: A printk() style format string.
433 * Prints an info level message associated with the test suite being run.
434 * Takes a variable number of format parameters just like printk().
436 #define kunit_info(test, fmt, ...) \
437 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
440 * kunit_warn() - Prints a WARN level message associated with @test.
442 * @test: The test context object.
443 * @fmt: A printk() style format string.
445 * Prints a warning level message.
447 #define kunit_warn(test, fmt, ...) \
448 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
451 * kunit_err() - Prints an ERROR level message associated with @test.
453 * @test: The test context object.
454 * @fmt: A printk() style format string.
456 * Prints an error level message.
458 #define kunit_err(test, fmt, ...) \
459 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
462 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
463 * @test: The test context object.
465 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
466 * words, it does nothing and only exists for code clarity. See
467 * KUNIT_EXPECT_TRUE() for more information.
469 #define KUNIT_SUCCEED(test) do {} while (0)
471 void kunit_do_failed_assertion(struct kunit *test,
472 const struct kunit_loc *loc,
473 enum kunit_assert_type type,
474 const struct kunit_assert *assert,
475 const char *fmt, ...);
477 #define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
478 if (unlikely(!(pass))) { \
479 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
480 struct assert_class __assertion = INITIALIZER; \
481 kunit_do_failed_assertion(test, \
484 &__assertion.assert, \
491 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
492 KUNIT_ASSERTION(test, \
496 KUNIT_INIT_FAIL_ASSERT_STRUCT, \
501 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
502 * @test: The test context object.
503 * @fmt: an informational message to be printed when the assertion is made.
504 * @...: string format arguments.
506 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
507 * other words, it always results in a failed expectation, and consequently
508 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
509 * for more information.
511 #define KUNIT_FAIL(test, fmt, ...) \
512 KUNIT_FAIL_ASSERTION(test, \
517 #define KUNIT_UNARY_ASSERTION(test, \
523 KUNIT_ASSERTION(test, \
525 !!(condition) == !!expected_true, \
526 kunit_unary_assert, \
527 KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
532 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
533 KUNIT_UNARY_ASSERTION(test, \
540 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
541 KUNIT_UNARY_ASSERTION(test, \
549 * A factory macro for defining the assertions and expectations for the basic
550 * comparisons defined for the built in types.
552 * Unfortunately, there is no common type that all types can be promoted to for
553 * which all the binary operators behave the same way as for the actual types
554 * (for example, there is no type that long long and unsigned long long can
555 * both be cast to where the comparison result is preserved for all values). So
556 * the best we can do is do the comparison in the original types and then coerce
557 * everything to long long for printing; this way, the comparison behaves
558 * correctly and the printed out value usually makes sense without
559 * interpretation, but can always be interpreted to figure out the actual
562 #define KUNIT_BASE_BINARY_ASSERTION(test, \
572 const typeof(left) __left = (left); \
573 const typeof(right) __right = (right); \
574 static const struct kunit_binary_assert_text __text = { \
576 .left_text = #left, \
577 .right_text = #right, \
580 KUNIT_ASSERTION(test, \
584 KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \
592 #define KUNIT_BINARY_INT_ASSERTION(test, \
599 KUNIT_BASE_BINARY_ASSERTION(test, \
600 kunit_binary_assert, \
601 kunit_binary_assert_format, \
607 #define KUNIT_BINARY_PTR_ASSERTION(test, \
614 KUNIT_BASE_BINARY_ASSERTION(test, \
615 kunit_binary_ptr_assert, \
616 kunit_binary_ptr_assert_format, \
622 #define KUNIT_BINARY_STR_ASSERTION(test, \
630 const char *__left = (left); \
631 const char *__right = (right); \
632 static const struct kunit_binary_assert_text __text = { \
634 .left_text = #left, \
635 .right_text = #right, \
638 KUNIT_ASSERTION(test, \
640 strcmp(__left, __right) op 0, \
641 kunit_binary_str_assert, \
642 KUNIT_INIT_BINARY_ASSERT_STRUCT(kunit_binary_str_assert_format,\
650 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
656 const typeof(ptr) __ptr = (ptr); \
658 KUNIT_ASSERTION(test, \
660 !IS_ERR_OR_NULL(__ptr), \
661 kunit_ptr_not_err_assert, \
662 KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \
669 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
670 * @test: The test context object.
671 * @condition: an arbitrary boolean expression. The test fails when this does
672 * not evaluate to true.
674 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
675 * to fail when the specified condition is not met; however, it will not prevent
676 * the test case from continuing to run; this is otherwise known as an
677 * *expectation failure*.
679 #define KUNIT_EXPECT_TRUE(test, condition) \
680 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
682 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
683 KUNIT_TRUE_MSG_ASSERTION(test, \
690 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
691 * @test: The test context object.
692 * @condition: an arbitrary boolean expression. The test fails when this does
693 * not evaluate to false.
695 * Sets an expectation that @condition evaluates to false. See
696 * KUNIT_EXPECT_TRUE() for more information.
698 #define KUNIT_EXPECT_FALSE(test, condition) \
699 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
701 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
702 KUNIT_FALSE_MSG_ASSERTION(test, \
709 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
710 * @test: The test context object.
711 * @left: an arbitrary expression that evaluates to a primitive C type.
712 * @right: an arbitrary expression that evaluates to a primitive C type.
714 * Sets an expectation that the values that @left and @right evaluate to are
715 * equal. This is semantically equivalent to
716 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
719 #define KUNIT_EXPECT_EQ(test, left, right) \
720 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
722 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
723 KUNIT_BINARY_INT_ASSERTION(test, \
730 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
731 * @test: The test context object.
732 * @left: an arbitrary expression that evaluates to a pointer.
733 * @right: an arbitrary expression that evaluates to a pointer.
735 * Sets an expectation that the values that @left and @right evaluate to are
736 * equal. This is semantically equivalent to
737 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
740 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
741 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
743 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
744 KUNIT_BINARY_PTR_ASSERTION(test, \
751 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
752 * @test: The test context object.
753 * @left: an arbitrary expression that evaluates to a primitive C type.
754 * @right: an arbitrary expression that evaluates to a primitive C type.
756 * Sets an expectation that the values that @left and @right evaluate to are not
757 * equal. This is semantically equivalent to
758 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
761 #define KUNIT_EXPECT_NE(test, left, right) \
762 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
764 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
765 KUNIT_BINARY_INT_ASSERTION(test, \
772 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
773 * @test: The test context object.
774 * @left: an arbitrary expression that evaluates to a pointer.
775 * @right: an arbitrary expression that evaluates to a pointer.
777 * Sets an expectation that the values that @left and @right evaluate to are not
778 * equal. This is semantically equivalent to
779 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
782 #define KUNIT_EXPECT_PTR_NE(test, left, right) \
783 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
785 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
786 KUNIT_BINARY_PTR_ASSERTION(test, \
793 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
794 * @test: The test context object.
795 * @left: an arbitrary expression that evaluates to a primitive C type.
796 * @right: an arbitrary expression that evaluates to a primitive C type.
798 * Sets an expectation that the value that @left evaluates to is less than the
799 * value that @right evaluates to. This is semantically equivalent to
800 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
803 #define KUNIT_EXPECT_LT(test, left, right) \
804 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
806 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
807 KUNIT_BINARY_INT_ASSERTION(test, \
814 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
815 * @test: The test context object.
816 * @left: an arbitrary expression that evaluates to a primitive C type.
817 * @right: an arbitrary expression that evaluates to a primitive C type.
819 * Sets an expectation that the value that @left evaluates to is less than or
820 * equal to the value that @right evaluates to. Semantically this is equivalent
821 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
824 #define KUNIT_EXPECT_LE(test, left, right) \
825 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
827 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
828 KUNIT_BINARY_INT_ASSERTION(test, \
835 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
836 * @test: The test context object.
837 * @left: an arbitrary expression that evaluates to a primitive C type.
838 * @right: an arbitrary expression that evaluates to a primitive C type.
840 * Sets an expectation that the value that @left evaluates to is greater than
841 * the value that @right evaluates to. This is semantically equivalent to
842 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
845 #define KUNIT_EXPECT_GT(test, left, right) \
846 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
848 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
849 KUNIT_BINARY_INT_ASSERTION(test, \
856 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
857 * @test: The test context object.
858 * @left: an arbitrary expression that evaluates to a primitive C type.
859 * @right: an arbitrary expression that evaluates to a primitive C type.
861 * Sets an expectation that the value that @left evaluates to is greater than
862 * the value that @right evaluates to. This is semantically equivalent to
863 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
866 #define KUNIT_EXPECT_GE(test, left, right) \
867 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
869 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
870 KUNIT_BINARY_INT_ASSERTION(test, \
877 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
878 * @test: The test context object.
879 * @left: an arbitrary expression that evaluates to a null terminated string.
880 * @right: an arbitrary expression that evaluates to a null terminated string.
882 * Sets an expectation that the values that @left and @right evaluate to are
883 * equal. This is semantically equivalent to
884 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
885 * for more information.
887 #define KUNIT_EXPECT_STREQ(test, left, right) \
888 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
890 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
891 KUNIT_BINARY_STR_ASSERTION(test, \
898 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
899 * @test: The test context object.
900 * @left: an arbitrary expression that evaluates to a null terminated string.
901 * @right: an arbitrary expression that evaluates to a null terminated string.
903 * Sets an expectation that the values that @left and @right evaluate to are
904 * not equal. This is semantically equivalent to
905 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
906 * for more information.
908 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
909 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
911 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
912 KUNIT_BINARY_STR_ASSERTION(test, \
919 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
920 * @test: The test context object.
921 * @ptr: an arbitrary pointer.
923 * Sets an expectation that the value that @ptr evaluates to is null. This is
924 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
925 * See KUNIT_EXPECT_TRUE() for more information.
927 #define KUNIT_EXPECT_NULL(test, ptr) \
928 KUNIT_EXPECT_NULL_MSG(test, \
932 #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
933 KUNIT_BINARY_PTR_ASSERTION(test, \
940 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
941 * @test: The test context object.
942 * @ptr: an arbitrary pointer.
944 * Sets an expectation that the value that @ptr evaluates to is not null. This
945 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
946 * See KUNIT_EXPECT_TRUE() for more information.
948 #define KUNIT_EXPECT_NOT_NULL(test, ptr) \
949 KUNIT_EXPECT_NOT_NULL_MSG(test, \
953 #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
954 KUNIT_BINARY_PTR_ASSERTION(test, \
961 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
962 * @test: The test context object.
963 * @ptr: an arbitrary pointer.
965 * Sets an expectation that the value that @ptr evaluates to is not null and not
966 * an errno stored in a pointer. This is semantically equivalent to
967 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
970 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
971 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
973 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
974 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
980 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
981 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
984 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
985 * @test: The test context object.
986 * @condition: an arbitrary boolean expression. The test fails and aborts when
987 * this does not evaluate to true.
989 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
990 * fail *and immediately abort* when the specified condition is not met. Unlike
991 * an expectation failure, it will prevent the test case from continuing to run;
992 * this is otherwise known as an *assertion failure*.
994 #define KUNIT_ASSERT_TRUE(test, condition) \
995 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
997 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
998 KUNIT_TRUE_MSG_ASSERTION(test, \
1005 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1006 * @test: The test context object.
1007 * @condition: an arbitrary boolean expression.
1009 * Sets an assertion that the value that @condition evaluates to is false. This
1010 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1011 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1013 #define KUNIT_ASSERT_FALSE(test, condition) \
1014 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
1016 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1017 KUNIT_FALSE_MSG_ASSERTION(test, \
1024 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1025 * @test: The test context object.
1026 * @left: an arbitrary expression that evaluates to a primitive C type.
1027 * @right: an arbitrary expression that evaluates to a primitive C type.
1029 * Sets an assertion that the values that @left and @right evaluate to are
1030 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1031 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1033 #define KUNIT_ASSERT_EQ(test, left, right) \
1034 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
1036 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1037 KUNIT_BINARY_INT_ASSERTION(test, \
1044 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1045 * @test: The test context object.
1046 * @left: an arbitrary expression that evaluates to a pointer.
1047 * @right: an arbitrary expression that evaluates to a pointer.
1049 * Sets an assertion that the values that @left and @right evaluate to are
1050 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1051 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1053 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1054 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
1056 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1057 KUNIT_BINARY_PTR_ASSERTION(test, \
1064 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1065 * @test: The test context object.
1066 * @left: an arbitrary expression that evaluates to a primitive C type.
1067 * @right: an arbitrary expression that evaluates to a primitive C type.
1069 * Sets an assertion that the values that @left and @right evaluate to are not
1070 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1071 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1073 #define KUNIT_ASSERT_NE(test, left, right) \
1074 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
1076 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1077 KUNIT_BINARY_INT_ASSERTION(test, \
1084 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1085 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1086 * @test: The test context object.
1087 * @left: an arbitrary expression that evaluates to a pointer.
1088 * @right: an arbitrary expression that evaluates to a pointer.
1090 * Sets an assertion that the values that @left and @right evaluate to are not
1091 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1092 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1094 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1095 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1097 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1098 KUNIT_BINARY_PTR_ASSERTION(test, \
1104 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1105 * @test: The test context object.
1106 * @left: an arbitrary expression that evaluates to a primitive C type.
1107 * @right: an arbitrary expression that evaluates to a primitive C type.
1109 * Sets an assertion that the value that @left evaluates to is less than the
1110 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1111 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1114 #define KUNIT_ASSERT_LT(test, left, right) \
1115 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1117 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1118 KUNIT_BINARY_INT_ASSERTION(test, \
1119 KUNIT_EXPECTATION, \
1124 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1125 * @test: The test context object.
1126 * @left: an arbitrary expression that evaluates to a primitive C type.
1127 * @right: an arbitrary expression that evaluates to a primitive C type.
1129 * Sets an assertion that the value that @left evaluates to is less than or
1130 * equal to the value that @right evaluates to. This is the same as
1131 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1132 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1134 #define KUNIT_ASSERT_LE(test, left, right) \
1135 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1137 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1138 KUNIT_BINARY_INT_ASSERTION(test, \
1145 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1146 * @test: The test context object.
1147 * @left: an arbitrary expression that evaluates to a primitive C type.
1148 * @right: an arbitrary expression that evaluates to a primitive C type.
1150 * Sets an assertion that the value that @left evaluates to is greater than the
1151 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1152 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1155 #define KUNIT_ASSERT_GT(test, left, right) \
1156 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1158 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1159 KUNIT_BINARY_INT_ASSERTION(test, \
1160 KUNIT_EXPECTATION, \
1166 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1167 * @test: The test context object.
1168 * @left: an arbitrary expression that evaluates to a primitive C type.
1169 * @right: an arbitrary expression that evaluates to a primitive C type.
1171 * Sets an assertion that the value that @left evaluates to is greater than the
1172 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1173 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1176 #define KUNIT_ASSERT_GE(test, left, right) \
1177 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1179 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1180 KUNIT_BINARY_INT_ASSERTION(test, \
1187 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1188 * @test: The test context object.
1189 * @left: an arbitrary expression that evaluates to a null terminated string.
1190 * @right: an arbitrary expression that evaluates to a null terminated string.
1192 * Sets an assertion that the values that @left and @right evaluate to are
1193 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1194 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1196 #define KUNIT_ASSERT_STREQ(test, left, right) \
1197 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1199 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1200 KUNIT_BINARY_STR_ASSERTION(test, \
1207 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1208 * @test: The test context object.
1209 * @left: an arbitrary expression that evaluates to a null terminated string.
1210 * @right: an arbitrary expression that evaluates to a null terminated string.
1212 * Sets an expectation that the values that @left and @right evaluate to are
1213 * not equal. This is semantically equivalent to
1214 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1215 * for more information.
1217 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1218 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1220 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1221 KUNIT_BINARY_STR_ASSERTION(test, \
1228 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1229 * @test: The test context object.
1230 * @ptr: an arbitrary pointer.
1232 * Sets an assertion that the values that @ptr evaluates to is null. This is
1233 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1234 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1236 #define KUNIT_ASSERT_NULL(test, ptr) \
1237 KUNIT_ASSERT_NULL_MSG(test, \
1241 #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1242 KUNIT_BINARY_PTR_ASSERTION(test, \
1249 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1250 * @test: The test context object.
1251 * @ptr: an arbitrary pointer.
1253 * Sets an assertion that the values that @ptr evaluates to is not null. This
1254 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1255 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1257 #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1258 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1262 #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1263 KUNIT_BINARY_PTR_ASSERTION(test, \
1270 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1271 * @test: The test context object.
1272 * @ptr: an arbitrary pointer.
1274 * Sets an assertion that the value that @ptr evaluates to is not null and not
1275 * an errno stored in a pointer. This is the same as
1276 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1277 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1279 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1280 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1282 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1283 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1290 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1291 * @name: prefix for the test parameter generator function.
1292 * @array: array of test parameters.
1293 * @get_desc: function to convert param to description; NULL to use default
1295 * Define function @name_gen_params which uses @array to generate parameters.
1297 #define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1298 static const void *name##_gen_params(const void *prev, char *desc) \
1300 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1301 if (__next - (array) < ARRAY_SIZE((array))) { \
1302 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1304 __get_desc(__next, desc); \
1310 // TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1311 // include resource.h themselves if they need it.
1312 #include <kunit/resource.h>
1314 #endif /* _KUNIT_TEST_H */