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 bool kunit_enabled(void);
233 void kunit_init_test(struct kunit *test, const char *name, char *log);
235 int kunit_run_tests(struct kunit_suite *suite);
237 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
239 unsigned int kunit_test_case_num(struct kunit_suite *suite,
240 struct kunit_case *test_case);
242 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
244 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
246 #if IS_BUILTIN(CONFIG_KUNIT)
247 int kunit_run_all_tests(void);
249 static inline int kunit_run_all_tests(void)
253 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
255 #define __kunit_test_suites(unique_array, ...) \
256 static struct kunit_suite *unique_array[] \
257 __aligned(sizeof(struct kunit_suite *)) \
258 __used __section(".kunit_test_suites") = { __VA_ARGS__ }
261 * kunit_test_suites() - used to register one or more &struct kunit_suite
264 * @__suites: a statically allocated list of &struct kunit_suite.
266 * Registers @suites with the test framework.
267 * This is done by placing the array of struct kunit_suite * in the
268 * .kunit_test_suites ELF section.
270 * When builtin, KUnit tests are all run via the executor at boot, and when
271 * built as a module, they run on module load.
274 #define kunit_test_suites(__suites...) \
275 __kunit_test_suites(__UNIQUE_ID(array), \
278 #define kunit_test_suite(suite) kunit_test_suites(&suite)
281 * kunit_test_init_section_suites() - used to register one or more &struct
282 * kunit_suite containing init functions or
285 * @__suites: a statically allocated list of &struct kunit_suite.
287 * This functions identically as kunit_test_suites() except that it suppresses
288 * modpost warnings for referencing functions marked __init or data marked
289 * __initdata; this is OK because currently KUnit only runs tests upon boot
290 * during the init phase or upon loading a module during the init phase.
292 * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
293 * tests must be excluded.
295 * The only thing this macro does that's different from kunit_test_suites is
296 * that it suffixes the array and suite declarations it makes with _probe;
297 * modpost suppresses warnings about referencing init data for symbols named in
300 #define kunit_test_init_section_suites(__suites...) \
301 __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
302 CONCATENATE(__UNIQUE_ID(suites), _probe), \
305 #define kunit_test_init_section_suite(suite) \
306 kunit_test_init_section_suites(&suite)
308 #define kunit_suite_for_each_test_case(suite, test_case) \
309 for (test_case = suite->test_cases; test_case->run_case; test_case++)
311 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
314 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
315 * @test: The test context object.
316 * @n: number of elements.
317 * @size: The size in bytes of the desired memory.
318 * @gfp: flags passed to underlying kmalloc().
320 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
321 * and is automatically cleaned up after the test case concludes. See &struct
322 * kunit_resource for more information.
324 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
327 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
328 * @test: The test context object.
329 * @size: The size in bytes of the desired memory.
330 * @gfp: flags passed to underlying kmalloc().
332 * See kmalloc() and kunit_kmalloc_array() for more information.
334 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
336 return kunit_kmalloc_array(test, 1, size, gfp);
340 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
341 * @test: The test case to which the resource belongs.
342 * @ptr: The memory allocation to free.
344 void kunit_kfree(struct kunit *test, const void *ptr);
347 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
348 * @test: The test context object.
349 * @size: The size in bytes of the desired memory.
350 * @gfp: flags passed to underlying kmalloc().
352 * See kzalloc() and kunit_kmalloc_array() for more information.
354 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
356 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
360 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
361 * @test: The test context object.
362 * @n: number of elements.
363 * @size: The size in bytes of the desired memory.
364 * @gfp: flags passed to underlying kmalloc().
366 * See kcalloc() and kunit_kmalloc_array() for more information.
368 static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
370 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
373 void kunit_cleanup(struct kunit *test);
375 void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
378 * kunit_mark_skipped() - Marks @test_or_suite as skipped
380 * @test_or_suite: The test context object.
381 * @fmt: A printk() style format string.
383 * Marks the test as skipped. @fmt is given output as the test status
384 * comment, typically the reason the test was skipped.
386 * Test execution continues after kunit_mark_skipped() is called.
388 #define kunit_mark_skipped(test_or_suite, fmt, ...) \
390 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
391 scnprintf((test_or_suite)->status_comment, \
392 KUNIT_STATUS_COMMENT_SIZE, \
393 fmt, ##__VA_ARGS__); \
397 * kunit_skip() - Marks @test_or_suite as skipped
399 * @test_or_suite: The test context object.
400 * @fmt: A printk() style format string.
402 * Skips the test. @fmt is given output as the test status
403 * comment, typically the reason the test was skipped.
405 * Test execution is halted after kunit_skip() is called.
407 #define kunit_skip(test_or_suite, fmt, ...) \
409 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
410 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
414 * printk and log to per-test or per-suite log buffer. Logging only done
415 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
417 #define kunit_log(lvl, test_or_suite, fmt, ...) \
419 printk(lvl fmt, ##__VA_ARGS__); \
420 kunit_log_append((test_or_suite)->log, fmt "\n", \
424 #define kunit_printk(lvl, test, fmt, ...) \
425 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
426 (test)->name, ##__VA_ARGS__)
429 * kunit_info() - Prints an INFO level message associated with @test.
431 * @test: The test context object.
432 * @fmt: A printk() style format string.
434 * Prints an info level message associated with the test suite being run.
435 * Takes a variable number of format parameters just like printk().
437 #define kunit_info(test, fmt, ...) \
438 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
441 * kunit_warn() - Prints a WARN level message associated with @test.
443 * @test: The test context object.
444 * @fmt: A printk() style format string.
446 * Prints a warning level message.
448 #define kunit_warn(test, fmt, ...) \
449 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
452 * kunit_err() - Prints an ERROR level message associated with @test.
454 * @test: The test context object.
455 * @fmt: A printk() style format string.
457 * Prints an error level message.
459 #define kunit_err(test, fmt, ...) \
460 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
463 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
464 * @test: The test context object.
466 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
467 * words, it does nothing and only exists for code clarity. See
468 * KUNIT_EXPECT_TRUE() for more information.
470 #define KUNIT_SUCCEED(test) do {} while (0)
472 void kunit_do_failed_assertion(struct kunit *test,
473 const struct kunit_loc *loc,
474 enum kunit_assert_type type,
475 const struct kunit_assert *assert,
476 assert_format_t assert_format,
477 const char *fmt, ...);
479 #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
480 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
481 const struct assert_class __assertion = INITIALIZER; \
482 kunit_do_failed_assertion(test, \
485 &__assertion.assert, \
492 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
493 _KUNIT_FAILED(test, \
496 kunit_fail_assert_format, \
502 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
503 * @test: The test context object.
504 * @fmt: an informational message to be printed when the assertion is made.
505 * @...: string format arguments.
507 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
508 * other words, it always results in a failed expectation, and consequently
509 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
510 * for more information.
512 #define KUNIT_FAIL(test, fmt, ...) \
513 KUNIT_FAIL_ASSERTION(test, \
518 #define KUNIT_UNARY_ASSERTION(test, \
525 if (likely(!!(condition) == !!expected_true)) \
528 _KUNIT_FAILED(test, \
530 kunit_unary_assert, \
531 kunit_unary_assert_format, \
532 KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
538 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
539 KUNIT_UNARY_ASSERTION(test, \
546 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
547 KUNIT_UNARY_ASSERTION(test, \
555 * A factory macro for defining the assertions and expectations for the basic
556 * comparisons defined for the built in types.
558 * Unfortunately, there is no common type that all types can be promoted to for
559 * which all the binary operators behave the same way as for the actual types
560 * (for example, there is no type that long long and unsigned long long can
561 * both be cast to where the comparison result is preserved for all values). So
562 * the best we can do is do the comparison in the original types and then coerce
563 * everything to long long for printing; this way, the comparison behaves
564 * correctly and the printed out value usually makes sense without
565 * interpretation, but can always be interpreted to figure out the actual
568 #define KUNIT_BASE_BINARY_ASSERTION(test, \
578 const typeof(left) __left = (left); \
579 const typeof(right) __right = (right); \
580 static const struct kunit_binary_assert_text __text = { \
582 .left_text = #left, \
583 .right_text = #right, \
586 if (likely(__left op __right)) \
589 _KUNIT_FAILED(test, \
593 KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \
600 #define KUNIT_BINARY_INT_ASSERTION(test, \
607 KUNIT_BASE_BINARY_ASSERTION(test, \
608 kunit_binary_assert, \
609 kunit_binary_assert_format, \
615 #define KUNIT_BINARY_PTR_ASSERTION(test, \
622 KUNIT_BASE_BINARY_ASSERTION(test, \
623 kunit_binary_ptr_assert, \
624 kunit_binary_ptr_assert_format, \
630 #define KUNIT_BINARY_STR_ASSERTION(test, \
638 const char *__left = (left); \
639 const char *__right = (right); \
640 static const struct kunit_binary_assert_text __text = { \
642 .left_text = #left, \
643 .right_text = #right, \
646 if (likely(strcmp(__left, __right) op 0)) \
650 _KUNIT_FAILED(test, \
652 kunit_binary_str_assert, \
653 kunit_binary_str_assert_format, \
654 KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \
661 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
667 const typeof(ptr) __ptr = (ptr); \
669 if (!IS_ERR_OR_NULL(__ptr)) \
672 _KUNIT_FAILED(test, \
674 kunit_ptr_not_err_assert, \
675 kunit_ptr_not_err_assert_format, \
676 KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr), \
682 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
683 * @test: The test context object.
684 * @condition: an arbitrary boolean expression. The test fails when this does
685 * not evaluate to true.
687 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
688 * to fail when the specified condition is not met; however, it will not prevent
689 * the test case from continuing to run; this is otherwise known as an
690 * *expectation failure*.
692 #define KUNIT_EXPECT_TRUE(test, condition) \
693 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
695 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
696 KUNIT_TRUE_MSG_ASSERTION(test, \
703 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
704 * @test: The test context object.
705 * @condition: an arbitrary boolean expression. The test fails when this does
706 * not evaluate to false.
708 * Sets an expectation that @condition evaluates to false. See
709 * KUNIT_EXPECT_TRUE() for more information.
711 #define KUNIT_EXPECT_FALSE(test, condition) \
712 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
714 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
715 KUNIT_FALSE_MSG_ASSERTION(test, \
722 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
723 * @test: The test context object.
724 * @left: an arbitrary expression that evaluates to a primitive C type.
725 * @right: an arbitrary expression that evaluates to a primitive C type.
727 * Sets an expectation that the values that @left and @right evaluate to are
728 * equal. This is semantically equivalent to
729 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
732 #define KUNIT_EXPECT_EQ(test, left, right) \
733 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
735 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
736 KUNIT_BINARY_INT_ASSERTION(test, \
743 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
744 * @test: The test context object.
745 * @left: an arbitrary expression that evaluates to a pointer.
746 * @right: an arbitrary expression that evaluates to a pointer.
748 * Sets an expectation that the values that @left and @right evaluate to are
749 * equal. This is semantically equivalent to
750 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
753 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
754 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
756 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
757 KUNIT_BINARY_PTR_ASSERTION(test, \
764 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
765 * @test: The test context object.
766 * @left: an arbitrary expression that evaluates to a primitive C type.
767 * @right: an arbitrary expression that evaluates to a primitive C type.
769 * Sets an expectation that the values that @left and @right evaluate to are not
770 * equal. This is semantically equivalent to
771 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
774 #define KUNIT_EXPECT_NE(test, left, right) \
775 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
777 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
778 KUNIT_BINARY_INT_ASSERTION(test, \
785 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
786 * @test: The test context object.
787 * @left: an arbitrary expression that evaluates to a pointer.
788 * @right: an arbitrary expression that evaluates to a pointer.
790 * Sets an expectation that the values that @left and @right evaluate to are not
791 * equal. This is semantically equivalent to
792 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
795 #define KUNIT_EXPECT_PTR_NE(test, left, right) \
796 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
798 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
799 KUNIT_BINARY_PTR_ASSERTION(test, \
806 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
807 * @test: The test context object.
808 * @left: an arbitrary expression that evaluates to a primitive C type.
809 * @right: an arbitrary expression that evaluates to a primitive C type.
811 * Sets an expectation that the value that @left evaluates to is less than the
812 * value that @right evaluates to. This is semantically equivalent to
813 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
816 #define KUNIT_EXPECT_LT(test, left, right) \
817 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
819 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
820 KUNIT_BINARY_INT_ASSERTION(test, \
827 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
828 * @test: The test context object.
829 * @left: an arbitrary expression that evaluates to a primitive C type.
830 * @right: an arbitrary expression that evaluates to a primitive C type.
832 * Sets an expectation that the value that @left evaluates to is less than or
833 * equal to the value that @right evaluates to. Semantically this is equivalent
834 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
837 #define KUNIT_EXPECT_LE(test, left, right) \
838 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
840 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
841 KUNIT_BINARY_INT_ASSERTION(test, \
848 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
849 * @test: The test context object.
850 * @left: an arbitrary expression that evaluates to a primitive C type.
851 * @right: an arbitrary expression that evaluates to a primitive C type.
853 * Sets an expectation that the value that @left evaluates to is greater than
854 * the value that @right evaluates to. This is semantically equivalent to
855 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
858 #define KUNIT_EXPECT_GT(test, left, right) \
859 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
861 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
862 KUNIT_BINARY_INT_ASSERTION(test, \
869 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
870 * @test: The test context object.
871 * @left: an arbitrary expression that evaluates to a primitive C type.
872 * @right: an arbitrary expression that evaluates to a primitive C type.
874 * Sets an expectation that the value that @left evaluates to is greater than
875 * the value that @right evaluates to. This is semantically equivalent to
876 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
879 #define KUNIT_EXPECT_GE(test, left, right) \
880 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
882 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
883 KUNIT_BINARY_INT_ASSERTION(test, \
890 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
891 * @test: The test context object.
892 * @left: an arbitrary expression that evaluates to a null terminated string.
893 * @right: an arbitrary expression that evaluates to a null terminated string.
895 * Sets an expectation that the values that @left and @right evaluate to are
896 * equal. This is semantically equivalent to
897 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
898 * for more information.
900 #define KUNIT_EXPECT_STREQ(test, left, right) \
901 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
903 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
904 KUNIT_BINARY_STR_ASSERTION(test, \
911 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
912 * @test: The test context object.
913 * @left: an arbitrary expression that evaluates to a null terminated string.
914 * @right: an arbitrary expression that evaluates to a null terminated string.
916 * Sets an expectation that the values that @left and @right evaluate to are
917 * not equal. This is semantically equivalent to
918 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
919 * for more information.
921 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
922 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
924 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
925 KUNIT_BINARY_STR_ASSERTION(test, \
932 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
933 * @test: The test context object.
934 * @ptr: an arbitrary pointer.
936 * Sets an expectation that the value that @ptr evaluates to is null. This is
937 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
938 * See KUNIT_EXPECT_TRUE() for more information.
940 #define KUNIT_EXPECT_NULL(test, ptr) \
941 KUNIT_EXPECT_NULL_MSG(test, \
945 #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
946 KUNIT_BINARY_PTR_ASSERTION(test, \
953 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
954 * @test: The test context object.
955 * @ptr: an arbitrary pointer.
957 * Sets an expectation that the value that @ptr evaluates to is not null. This
958 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
959 * See KUNIT_EXPECT_TRUE() for more information.
961 #define KUNIT_EXPECT_NOT_NULL(test, ptr) \
962 KUNIT_EXPECT_NOT_NULL_MSG(test, \
966 #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
967 KUNIT_BINARY_PTR_ASSERTION(test, \
974 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
975 * @test: The test context object.
976 * @ptr: an arbitrary pointer.
978 * Sets an expectation that the value that @ptr evaluates to is not null and not
979 * an errno stored in a pointer. This is semantically equivalent to
980 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
983 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
984 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
986 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
987 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
993 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
994 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
997 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
998 * @test: The test context object.
999 * @condition: an arbitrary boolean expression. The test fails and aborts when
1000 * this does not evaluate to true.
1002 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1003 * fail *and immediately abort* when the specified condition is not met. Unlike
1004 * an expectation failure, it will prevent the test case from continuing to run;
1005 * this is otherwise known as an *assertion failure*.
1007 #define KUNIT_ASSERT_TRUE(test, condition) \
1008 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
1010 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1011 KUNIT_TRUE_MSG_ASSERTION(test, \
1018 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1019 * @test: The test context object.
1020 * @condition: an arbitrary boolean expression.
1022 * Sets an assertion that the value that @condition evaluates to is false. This
1023 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1024 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1026 #define KUNIT_ASSERT_FALSE(test, condition) \
1027 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
1029 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1030 KUNIT_FALSE_MSG_ASSERTION(test, \
1037 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1038 * @test: The test context object.
1039 * @left: an arbitrary expression that evaluates to a primitive C type.
1040 * @right: an arbitrary expression that evaluates to a primitive C type.
1042 * Sets an assertion that the values that @left and @right evaluate to are
1043 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1044 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1046 #define KUNIT_ASSERT_EQ(test, left, right) \
1047 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
1049 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1050 KUNIT_BINARY_INT_ASSERTION(test, \
1057 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1058 * @test: The test context object.
1059 * @left: an arbitrary expression that evaluates to a pointer.
1060 * @right: an arbitrary expression that evaluates to a pointer.
1062 * Sets an assertion that the values that @left and @right evaluate to are
1063 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1064 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1066 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1067 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
1069 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1070 KUNIT_BINARY_PTR_ASSERTION(test, \
1077 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1078 * @test: The test context object.
1079 * @left: an arbitrary expression that evaluates to a primitive C type.
1080 * @right: an arbitrary expression that evaluates to a primitive C type.
1082 * Sets an assertion that the values that @left and @right evaluate to are not
1083 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1084 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1086 #define KUNIT_ASSERT_NE(test, left, right) \
1087 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
1089 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1090 KUNIT_BINARY_INT_ASSERTION(test, \
1097 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1098 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1099 * @test: The test context object.
1100 * @left: an arbitrary expression that evaluates to a pointer.
1101 * @right: an arbitrary expression that evaluates to a pointer.
1103 * Sets an assertion that the values that @left and @right evaluate to are not
1104 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1105 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1107 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1108 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1110 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1111 KUNIT_BINARY_PTR_ASSERTION(test, \
1117 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1118 * @test: The test context object.
1119 * @left: an arbitrary expression that evaluates to a primitive C type.
1120 * @right: an arbitrary expression that evaluates to a primitive C type.
1122 * Sets an assertion that the value that @left evaluates to is less than the
1123 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1124 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1127 #define KUNIT_ASSERT_LT(test, left, right) \
1128 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1130 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1131 KUNIT_BINARY_INT_ASSERTION(test, \
1137 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1138 * @test: The test context object.
1139 * @left: an arbitrary expression that evaluates to a primitive C type.
1140 * @right: an arbitrary expression that evaluates to a primitive C type.
1142 * Sets an assertion that the value that @left evaluates to is less than or
1143 * equal to the value that @right evaluates to. This is the same as
1144 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1145 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1147 #define KUNIT_ASSERT_LE(test, left, right) \
1148 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1150 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1151 KUNIT_BINARY_INT_ASSERTION(test, \
1158 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1159 * @test: The test context object.
1160 * @left: an arbitrary expression that evaluates to a primitive C type.
1161 * @right: an arbitrary expression that evaluates to a primitive C type.
1163 * Sets an assertion that the value that @left evaluates to is greater than the
1164 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1165 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1168 #define KUNIT_ASSERT_GT(test, left, right) \
1169 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1171 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1172 KUNIT_BINARY_INT_ASSERTION(test, \
1179 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1180 * @test: The test context object.
1181 * @left: an arbitrary expression that evaluates to a primitive C type.
1182 * @right: an arbitrary expression that evaluates to a primitive C type.
1184 * Sets an assertion that the value that @left evaluates to is greater than the
1185 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1186 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1189 #define KUNIT_ASSERT_GE(test, left, right) \
1190 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1192 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1193 KUNIT_BINARY_INT_ASSERTION(test, \
1200 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1201 * @test: The test context object.
1202 * @left: an arbitrary expression that evaluates to a null terminated string.
1203 * @right: an arbitrary expression that evaluates to a null terminated string.
1205 * Sets an assertion that the values that @left and @right evaluate to are
1206 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1207 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1209 #define KUNIT_ASSERT_STREQ(test, left, right) \
1210 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1212 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1213 KUNIT_BINARY_STR_ASSERTION(test, \
1220 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1221 * @test: The test context object.
1222 * @left: an arbitrary expression that evaluates to a null terminated string.
1223 * @right: an arbitrary expression that evaluates to a null terminated string.
1225 * Sets an expectation that the values that @left and @right evaluate to are
1226 * not equal. This is semantically equivalent to
1227 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1228 * for more information.
1230 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1231 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1233 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1234 KUNIT_BINARY_STR_ASSERTION(test, \
1241 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1242 * @test: The test context object.
1243 * @ptr: an arbitrary pointer.
1245 * Sets an assertion that the values that @ptr evaluates to is null. This is
1246 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1247 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1249 #define KUNIT_ASSERT_NULL(test, ptr) \
1250 KUNIT_ASSERT_NULL_MSG(test, \
1254 #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1255 KUNIT_BINARY_PTR_ASSERTION(test, \
1262 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1263 * @test: The test context object.
1264 * @ptr: an arbitrary pointer.
1266 * Sets an assertion that the values that @ptr evaluates to is not null. This
1267 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1268 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1270 #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1271 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1275 #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1276 KUNIT_BINARY_PTR_ASSERTION(test, \
1283 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1284 * @test: The test context object.
1285 * @ptr: an arbitrary pointer.
1287 * Sets an assertion that the value that @ptr evaluates to is not null and not
1288 * an errno stored in a pointer. This is the same as
1289 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1290 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1292 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1293 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1295 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1296 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1303 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1304 * @name: prefix for the test parameter generator function.
1305 * @array: array of test parameters.
1306 * @get_desc: function to convert param to description; NULL to use default
1308 * Define function @name_gen_params which uses @array to generate parameters.
1310 #define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1311 static const void *name##_gen_params(const void *prev, char *desc) \
1313 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1314 if (__next - (array) < ARRAY_SIZE((array))) { \
1315 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1317 __get_desc(__next, desc); \
1323 // TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1324 // include resource.h themselves if they need it.
1325 #include <kunit/resource.h>
1327 #endif /* _KUNIT_TEST_H */