1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Base unit test (KUnit) API.
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
12 #include <kunit/assert.h>
13 #include <kunit/try-catch.h>
15 #include <linux/compiler.h>
16 #include <linux/container_of.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/jump_label.h>
20 #include <linux/kconfig.h>
21 #include <linux/kref.h>
22 #include <linux/list.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/string.h>
27 #include <linux/types.h>
29 #include <asm/rwonce.h>
31 /* Static key: true if any KUnit tests are currently running */
32 DECLARE_STATIC_KEY_FALSE(kunit_running);
36 /* Size of log associated with test. */
37 #define KUNIT_LOG_SIZE 2048
39 /* Maximum size of parameter description string. */
40 #define KUNIT_PARAM_DESC_SIZE 128
42 /* Maximum size of a status comment. */
43 #define KUNIT_STATUS_COMMENT_SIZE 256
46 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
47 * sub-subtest. See the "Subtests" section in
48 * https://node-tap.org/tap-protocol/
50 #define KUNIT_INDENT_LEN 4
51 #define KUNIT_SUBTEST_INDENT " "
52 #define KUNIT_SUBSUBTEST_INDENT " "
55 * enum kunit_status - Type of result for a test or test suite
56 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
57 * @KUNIT_FAILURE: Denotes the test has failed.
58 * @KUNIT_SKIPPED: Denotes the test has been skipped.
67 * struct kunit_case - represents an individual test case.
69 * @run_case: the function representing the actual test case.
70 * @name: the name of the test case.
71 * @generate_params: the generator function for parameterized tests.
73 * A test case is a function with the signature,
74 * ``void (*)(struct kunit *)``
75 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
76 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
77 * with a &struct kunit_suite and will be run after the suite's init
78 * function and followed by the suite's exit function.
80 * A test case should be static and should only be created with the
81 * KUNIT_CASE() macro; additionally, every array of test cases should be
82 * terminated with an empty test case.
88 * void add_test_basic(struct kunit *test)
90 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
91 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
92 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
93 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
94 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
97 * static struct kunit_case example_test_cases[] = {
98 * KUNIT_CASE(add_test_basic),
104 void (*run_case)(struct kunit *test);
106 const void* (*generate_params)(const void *prev, char *desc);
108 /* private: internal use only. */
109 enum kunit_status status;
113 static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
126 * KUNIT_CASE - A helper for creating a &struct kunit_case
128 * @test_name: a reference to a test case function.
130 * Takes a symbol for a function representing a test case and creates a
131 * &struct kunit_case object from it. See the documentation for
132 * &struct kunit_case for an example on how to use it.
134 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
137 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
139 * @test_name: a reference to a test case function.
140 * @gen_params: a reference to a parameter generator function.
142 * The generator function::
144 * const void* gen_params(const void *prev, char *desc)
146 * is used to lazily generate a series of arbitrarily typed values that fit into
147 * a void*. The argument @prev is the previously returned value, which should be
148 * used to derive the next value; @prev is set to NULL on the initial generator
149 * call. When no more values are available, the generator must return NULL.
150 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
151 * describing the parameter.
153 #define KUNIT_CASE_PARAM(test_name, gen_params) \
154 { .run_case = test_name, .name = #test_name, \
155 .generate_params = gen_params }
158 * struct kunit_suite - describes a related collection of &struct kunit_case
160 * @name: the name of the test. Purely informational.
161 * @suite_init: called once per test suite before the test cases.
162 * @suite_exit: called once per test suite after all test cases.
163 * @init: called before every test case.
164 * @exit: called after every test case.
165 * @test_cases: a null terminated array of test cases.
167 * A kunit_suite is a collection of related &struct kunit_case s, such that
168 * @init is called before every test case and @exit is called after every
169 * test case, similar to the notion of a *test fixture* or a *test class*
170 * in other unit testing frameworks like JUnit or Googletest.
172 * Note that @exit and @suite_exit will run even if @init or @suite_init
173 * fail: make sure they can handle any inconsistent state which may result.
175 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
179 const char name[256];
180 int (*suite_init)(struct kunit_suite *suite);
181 void (*suite_exit)(struct kunit_suite *suite);
182 int (*init)(struct kunit *test);
183 void (*exit)(struct kunit *test);
184 struct kunit_case *test_cases;
186 /* private: internal use only */
187 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
188 struct dentry *debugfs;
194 * struct kunit - represents a running instance of a test.
196 * @priv: for user to store arbitrary data. Commonly used to pass data
197 * created in the init function (see &struct kunit_suite).
199 * Used to store information about the current context under which the test
200 * is running. Most of this data is private and should only be accessed
201 * indirectly via public functions; the one exception is @priv which can be
202 * used by the test writer to store arbitrary data.
207 /* private: internal use only. */
208 const char *name; /* Read only after initialization! */
209 char *log; /* Points at case log after initialization */
210 struct kunit_try_catch try_catch;
211 /* param_value is the current parameter value for a test case. */
212 const void *param_value;
213 /* param_index stores the index of the parameter in parameterized tests. */
216 * success starts as true, and may only be set to false during a
217 * test case; thus, it is safe to update this across multiple
218 * threads using WRITE_ONCE; however, as a consequence, it may only
219 * be read after the test case finishes once all threads associated
220 * with the test case have terminated.
222 spinlock_t lock; /* Guards all mutable test state. */
223 enum kunit_status status; /* Read only after test_case finishes! */
225 * Because resources is a list that may be updated multiple times (with
226 * new resources) from any thread associated with a test case, we must
227 * protect it with some type of lock.
229 struct list_head resources; /* Protected by lock. */
231 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
234 static inline void kunit_set_failure(struct kunit *test)
236 WRITE_ONCE(test->status, KUNIT_FAILURE);
239 bool kunit_enabled(void);
241 void kunit_init_test(struct kunit *test, const char *name, char *log);
243 int kunit_run_tests(struct kunit_suite *suite);
245 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
247 unsigned int kunit_test_case_num(struct kunit_suite *suite,
248 struct kunit_case *test_case);
250 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
252 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
254 #if IS_BUILTIN(CONFIG_KUNIT)
255 int kunit_run_all_tests(void);
257 static inline int kunit_run_all_tests(void)
261 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
263 #define __kunit_test_suites(unique_array, ...) \
264 static struct kunit_suite *unique_array[] \
265 __aligned(sizeof(struct kunit_suite *)) \
266 __used __section(".kunit_test_suites") = { __VA_ARGS__ }
269 * kunit_test_suites() - used to register one or more &struct kunit_suite
272 * @__suites: a statically allocated list of &struct kunit_suite.
274 * Registers @suites with the test framework.
275 * This is done by placing the array of struct kunit_suite * in the
276 * .kunit_test_suites ELF section.
278 * When builtin, KUnit tests are all run via the executor at boot, and when
279 * built as a module, they run on module load.
282 #define kunit_test_suites(__suites...) \
283 __kunit_test_suites(__UNIQUE_ID(array), \
286 #define kunit_test_suite(suite) kunit_test_suites(&suite)
289 * kunit_test_init_section_suites() - used to register one or more &struct
290 * kunit_suite containing init functions or
293 * @__suites: a statically allocated list of &struct kunit_suite.
295 * This functions identically as kunit_test_suites() except that it suppresses
296 * modpost warnings for referencing functions marked __init or data marked
297 * __initdata; this is OK because currently KUnit only runs tests upon boot
298 * during the init phase or upon loading a module during the init phase.
300 * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
301 * tests must be excluded.
303 * The only thing this macro does that's different from kunit_test_suites is
304 * that it suffixes the array and suite declarations it makes with _probe;
305 * modpost suppresses warnings about referencing init data for symbols named in
308 #define kunit_test_init_section_suites(__suites...) \
309 __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
312 #define kunit_test_init_section_suite(suite) \
313 kunit_test_init_section_suites(&suite)
315 #define kunit_suite_for_each_test_case(suite, test_case) \
316 for (test_case = suite->test_cases; test_case->run_case; test_case++)
318 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
321 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
322 * @test: The test context object.
323 * @n: number of elements.
324 * @size: The size in bytes of the desired memory.
325 * @gfp: flags passed to underlying kmalloc().
327 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
328 * and is automatically cleaned up after the test case concludes. See kunit_add_action()
329 * for more information.
331 * Note that some internal context data is also allocated with GFP_KERNEL,
332 * regardless of the gfp passed in.
334 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
337 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
338 * @test: The test context object.
339 * @size: The size in bytes of the desired memory.
340 * @gfp: flags passed to underlying kmalloc().
342 * See kmalloc() and kunit_kmalloc_array() for more information.
344 * Note that some internal context data is also allocated with GFP_KERNEL,
345 * regardless of the gfp passed in.
347 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
349 return kunit_kmalloc_array(test, 1, size, gfp);
353 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
354 * @test: The test case to which the resource belongs.
355 * @ptr: The memory allocation to free.
357 void kunit_kfree(struct kunit *test, const void *ptr);
360 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
361 * @test: The test context object.
362 * @size: The size in bytes of the desired memory.
363 * @gfp: flags passed to underlying kmalloc().
365 * See kzalloc() and kunit_kmalloc_array() for more information.
367 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
369 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
373 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
374 * @test: The test context object.
375 * @n: number of elements.
376 * @size: The size in bytes of the desired memory.
377 * @gfp: flags passed to underlying kmalloc().
379 * See kcalloc() and kunit_kmalloc_array() for more information.
381 static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
383 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
386 void kunit_cleanup(struct kunit *test);
388 void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
391 * kunit_mark_skipped() - Marks @test_or_suite as skipped
393 * @test_or_suite: The test context object.
394 * @fmt: A printk() style format string.
396 * Marks the test as skipped. @fmt is given output as the test status
397 * comment, typically the reason the test was skipped.
399 * Test execution continues after kunit_mark_skipped() is called.
401 #define kunit_mark_skipped(test_or_suite, fmt, ...) \
403 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
404 scnprintf((test_or_suite)->status_comment, \
405 KUNIT_STATUS_COMMENT_SIZE, \
406 fmt, ##__VA_ARGS__); \
410 * kunit_skip() - Marks @test_or_suite as skipped
412 * @test_or_suite: The test context object.
413 * @fmt: A printk() style format string.
415 * Skips the test. @fmt is given output as the test status
416 * comment, typically the reason the test was skipped.
418 * Test execution is halted after kunit_skip() is called.
420 #define kunit_skip(test_or_suite, fmt, ...) \
422 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
423 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
427 * printk and log to per-test or per-suite log buffer. Logging only done
428 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
430 #define kunit_log(lvl, test_or_suite, fmt, ...) \
432 printk(lvl fmt, ##__VA_ARGS__); \
433 kunit_log_append((test_or_suite)->log, fmt, \
437 #define kunit_printk(lvl, test, fmt, ...) \
438 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
439 (test)->name, ##__VA_ARGS__)
442 * kunit_info() - Prints an INFO level message associated with @test.
444 * @test: The test context object.
445 * @fmt: A printk() style format string.
447 * Prints an info level message associated with the test suite being run.
448 * Takes a variable number of format parameters just like printk().
450 #define kunit_info(test, fmt, ...) \
451 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
454 * kunit_warn() - Prints a WARN level message associated with @test.
456 * @test: The test context object.
457 * @fmt: A printk() style format string.
459 * Prints a warning level message.
461 #define kunit_warn(test, fmt, ...) \
462 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
465 * kunit_err() - Prints an ERROR level message associated with @test.
467 * @test: The test context object.
468 * @fmt: A printk() style format string.
470 * Prints an error level message.
472 #define kunit_err(test, fmt, ...) \
473 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
476 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
477 * @test: The test context object.
479 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
480 * words, it does nothing and only exists for code clarity. See
481 * KUNIT_EXPECT_TRUE() for more information.
483 #define KUNIT_SUCCEED(test) do {} while (0)
485 void __noreturn __kunit_abort(struct kunit *test);
487 void __kunit_do_failed_assertion(struct kunit *test,
488 const struct kunit_loc *loc,
489 enum kunit_assert_type type,
490 const struct kunit_assert *assert,
491 assert_format_t assert_format,
492 const char *fmt, ...);
494 #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
495 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
496 const struct assert_class __assertion = INITIALIZER; \
497 __kunit_do_failed_assertion(test, \
500 &__assertion.assert, \
504 if (assert_type == KUNIT_ASSERTION) \
505 __kunit_abort(test); \
509 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
510 _KUNIT_FAILED(test, \
513 kunit_fail_assert_format, \
519 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
520 * @test: The test context object.
521 * @fmt: an informational message to be printed when the assertion is made.
522 * @...: string format arguments.
524 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
525 * other words, it always results in a failed expectation, and consequently
526 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
527 * for more information.
529 #define KUNIT_FAIL(test, fmt, ...) \
530 KUNIT_FAIL_ASSERTION(test, \
535 /* Helper to safely pass around an initializer list to other macros. */
536 #define KUNIT_INIT_ASSERT(initializers...) { initializers }
538 #define KUNIT_UNARY_ASSERTION(test, \
545 if (likely(!!(condition_) == !!expected_true_)) \
548 _KUNIT_FAILED(test, \
550 kunit_unary_assert, \
551 kunit_unary_assert_format, \
552 KUNIT_INIT_ASSERT(.condition = #condition_, \
553 .expected_true = expected_true_), \
558 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
559 KUNIT_UNARY_ASSERTION(test, \
566 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
567 KUNIT_UNARY_ASSERTION(test, \
575 * A factory macro for defining the assertions and expectations for the basic
576 * comparisons defined for the built in types.
578 * Unfortunately, there is no common type that all types can be promoted to for
579 * which all the binary operators behave the same way as for the actual types
580 * (for example, there is no type that long long and unsigned long long can
581 * both be cast to where the comparison result is preserved for all values). So
582 * the best we can do is do the comparison in the original types and then coerce
583 * everything to long long for printing; this way, the comparison behaves
584 * correctly and the printed out value usually makes sense without
585 * interpretation, but can always be interpreted to figure out the actual
588 #define KUNIT_BASE_BINARY_ASSERTION(test, \
598 const typeof(left) __left = (left); \
599 const typeof(right) __right = (right); \
600 static const struct kunit_binary_assert_text __text = { \
602 .left_text = #left, \
603 .right_text = #right, \
606 if (likely(__left op __right)) \
609 _KUNIT_FAILED(test, \
613 KUNIT_INIT_ASSERT(.text = &__text, \
614 .left_value = __left, \
615 .right_value = __right), \
620 #define KUNIT_BINARY_INT_ASSERTION(test, \
627 KUNIT_BASE_BINARY_ASSERTION(test, \
628 kunit_binary_assert, \
629 kunit_binary_assert_format, \
635 #define KUNIT_BINARY_PTR_ASSERTION(test, \
642 KUNIT_BASE_BINARY_ASSERTION(test, \
643 kunit_binary_ptr_assert, \
644 kunit_binary_ptr_assert_format, \
650 #define KUNIT_BINARY_STR_ASSERTION(test, \
658 const char *__left = (left); \
659 const char *__right = (right); \
660 static const struct kunit_binary_assert_text __text = { \
662 .left_text = #left, \
663 .right_text = #right, \
666 if (likely(strcmp(__left, __right) op 0)) \
670 _KUNIT_FAILED(test, \
672 kunit_binary_str_assert, \
673 kunit_binary_str_assert_format, \
674 KUNIT_INIT_ASSERT(.text = &__text, \
675 .left_value = __left, \
676 .right_value = __right), \
681 #define KUNIT_MEM_ASSERTION(test, \
690 const void *__left = (left); \
691 const void *__right = (right); \
692 const size_t __size = (size_); \
693 static const struct kunit_binary_assert_text __text = { \
695 .left_text = #left, \
696 .right_text = #right, \
699 if (likely(__left && __right)) \
700 if (likely(memcmp(__left, __right, __size) op 0)) \
703 _KUNIT_FAILED(test, \
706 kunit_mem_assert_format, \
707 KUNIT_INIT_ASSERT(.text = &__text, \
708 .left_value = __left, \
709 .right_value = __right, \
715 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
721 const typeof(ptr) __ptr = (ptr); \
723 if (!IS_ERR_OR_NULL(__ptr)) \
726 _KUNIT_FAILED(test, \
728 kunit_ptr_not_err_assert, \
729 kunit_ptr_not_err_assert_format, \
730 KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \
736 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
737 * @test: The test context object.
738 * @condition: an arbitrary boolean expression. The test fails when this does
739 * not evaluate to true.
741 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
742 * to fail when the specified condition is not met; however, it will not prevent
743 * the test case from continuing to run; this is otherwise known as an
744 * *expectation failure*.
746 #define KUNIT_EXPECT_TRUE(test, condition) \
747 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
749 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
750 KUNIT_TRUE_MSG_ASSERTION(test, \
757 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
758 * @test: The test context object.
759 * @condition: an arbitrary boolean expression. The test fails when this does
760 * not evaluate to false.
762 * Sets an expectation that @condition evaluates to false. See
763 * KUNIT_EXPECT_TRUE() for more information.
765 #define KUNIT_EXPECT_FALSE(test, condition) \
766 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
768 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
769 KUNIT_FALSE_MSG_ASSERTION(test, \
776 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
777 * @test: The test context object.
778 * @left: an arbitrary expression that evaluates to a primitive C type.
779 * @right: an arbitrary expression that evaluates to a primitive C type.
781 * Sets an expectation that the values that @left and @right evaluate to are
782 * equal. This is semantically equivalent to
783 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
786 #define KUNIT_EXPECT_EQ(test, left, right) \
787 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
789 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
790 KUNIT_BINARY_INT_ASSERTION(test, \
797 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
798 * @test: The test context object.
799 * @left: an arbitrary expression that evaluates to a pointer.
800 * @right: an arbitrary expression that evaluates to a pointer.
802 * Sets an expectation that the values that @left and @right evaluate to are
803 * equal. This is semantically equivalent to
804 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
807 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
808 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
810 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
811 KUNIT_BINARY_PTR_ASSERTION(test, \
818 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
819 * @test: The test context object.
820 * @left: an arbitrary expression that evaluates to a primitive C type.
821 * @right: an arbitrary expression that evaluates to a primitive C type.
823 * Sets an expectation that the values that @left and @right evaluate to are not
824 * equal. This is semantically equivalent to
825 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
828 #define KUNIT_EXPECT_NE(test, left, right) \
829 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
831 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
832 KUNIT_BINARY_INT_ASSERTION(test, \
839 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
840 * @test: The test context object.
841 * @left: an arbitrary expression that evaluates to a pointer.
842 * @right: an arbitrary expression that evaluates to a pointer.
844 * Sets an expectation that the values that @left and @right evaluate to are not
845 * equal. This is semantically equivalent to
846 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
849 #define KUNIT_EXPECT_PTR_NE(test, left, right) \
850 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
852 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
853 KUNIT_BINARY_PTR_ASSERTION(test, \
860 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
861 * @test: The test context object.
862 * @left: an arbitrary expression that evaluates to a primitive C type.
863 * @right: an arbitrary expression that evaluates to a primitive C type.
865 * Sets an expectation that the value that @left evaluates to is less than the
866 * value that @right evaluates to. This is semantically equivalent to
867 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
870 #define KUNIT_EXPECT_LT(test, left, right) \
871 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
873 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
874 KUNIT_BINARY_INT_ASSERTION(test, \
881 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
882 * @test: The test context object.
883 * @left: an arbitrary expression that evaluates to a primitive C type.
884 * @right: an arbitrary expression that evaluates to a primitive C type.
886 * Sets an expectation that the value that @left evaluates to is less than or
887 * equal to the value that @right evaluates to. Semantically this is equivalent
888 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
891 #define KUNIT_EXPECT_LE(test, left, right) \
892 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
894 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
895 KUNIT_BINARY_INT_ASSERTION(test, \
902 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
903 * @test: The test context object.
904 * @left: an arbitrary expression that evaluates to a primitive C type.
905 * @right: an arbitrary expression that evaluates to a primitive C type.
907 * Sets an expectation that the value that @left evaluates to is greater than
908 * the value that @right evaluates to. This is semantically equivalent to
909 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
912 #define KUNIT_EXPECT_GT(test, left, right) \
913 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
915 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
916 KUNIT_BINARY_INT_ASSERTION(test, \
923 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
924 * @test: The test context object.
925 * @left: an arbitrary expression that evaluates to a primitive C type.
926 * @right: an arbitrary expression that evaluates to a primitive C type.
928 * Sets an expectation that the value that @left evaluates to is greater than
929 * the value that @right evaluates to. This is semantically equivalent to
930 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
933 #define KUNIT_EXPECT_GE(test, left, right) \
934 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
936 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
937 KUNIT_BINARY_INT_ASSERTION(test, \
944 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
945 * @test: The test context object.
946 * @left: an arbitrary expression that evaluates to a null terminated string.
947 * @right: an arbitrary expression that evaluates to a null terminated string.
949 * Sets an expectation that the values that @left and @right evaluate to are
950 * equal. This is semantically equivalent to
951 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
952 * for more information.
954 #define KUNIT_EXPECT_STREQ(test, left, right) \
955 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
957 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
958 KUNIT_BINARY_STR_ASSERTION(test, \
965 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
966 * @test: The test context object.
967 * @left: an arbitrary expression that evaluates to a null terminated string.
968 * @right: an arbitrary expression that evaluates to a null terminated string.
970 * Sets an expectation that the values that @left and @right evaluate to are
971 * not equal. This is semantically equivalent to
972 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
973 * for more information.
975 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
976 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
978 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
979 KUNIT_BINARY_STR_ASSERTION(test, \
986 * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.
987 * @test: The test context object.
988 * @left: An arbitrary expression that evaluates to the specified size.
989 * @right: An arbitrary expression that evaluates to the specified size.
990 * @size: Number of bytes compared.
992 * Sets an expectation that the values that @left and @right evaluate to are
993 * equal. This is semantically equivalent to
994 * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
995 * KUNIT_EXPECT_TRUE() for more information.
997 * Although this expectation works for any memory block, it is not recommended
998 * for comparing more structured data, such as structs. This expectation is
999 * recommended for comparing, for example, data arrays.
1001 #define KUNIT_EXPECT_MEMEQ(test, left, right, size) \
1002 KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)
1004 #define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
1005 KUNIT_MEM_ASSERTION(test, \
1006 KUNIT_EXPECTATION, \
1013 * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.
1014 * @test: The test context object.
1015 * @left: An arbitrary expression that evaluates to the specified size.
1016 * @right: An arbitrary expression that evaluates to the specified size.
1017 * @size: Number of bytes compared.
1019 * Sets an expectation that the values that @left and @right evaluate to are
1020 * not equal. This is semantically equivalent to
1021 * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1022 * KUNIT_EXPECT_TRUE() for more information.
1024 * Although this expectation works for any memory block, it is not recommended
1025 * for comparing more structured data, such as structs. This expectation is
1026 * recommended for comparing, for example, data arrays.
1028 #define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \
1029 KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)
1031 #define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1032 KUNIT_MEM_ASSERTION(test, \
1033 KUNIT_EXPECTATION, \
1040 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
1041 * @test: The test context object.
1042 * @ptr: an arbitrary pointer.
1044 * Sets an expectation that the value that @ptr evaluates to is null. This is
1045 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
1046 * See KUNIT_EXPECT_TRUE() for more information.
1048 #define KUNIT_EXPECT_NULL(test, ptr) \
1049 KUNIT_EXPECT_NULL_MSG(test, \
1053 #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
1054 KUNIT_BINARY_PTR_ASSERTION(test, \
1055 KUNIT_EXPECTATION, \
1061 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
1062 * @test: The test context object.
1063 * @ptr: an arbitrary pointer.
1065 * Sets an expectation that the value that @ptr evaluates to is not null. This
1066 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
1067 * See KUNIT_EXPECT_TRUE() for more information.
1069 #define KUNIT_EXPECT_NOT_NULL(test, ptr) \
1070 KUNIT_EXPECT_NOT_NULL_MSG(test, \
1074 #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1075 KUNIT_BINARY_PTR_ASSERTION(test, \
1076 KUNIT_EXPECTATION, \
1082 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1083 * @test: The test context object.
1084 * @ptr: an arbitrary pointer.
1086 * Sets an expectation that the value that @ptr evaluates to is not null and not
1087 * an errno stored in a pointer. This is semantically equivalent to
1088 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1091 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1092 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1094 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1095 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1096 KUNIT_EXPECTATION, \
1101 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1102 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1105 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1106 * @test: The test context object.
1107 * @condition: an arbitrary boolean expression. The test fails and aborts when
1108 * this does not evaluate to true.
1110 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1111 * fail *and immediately abort* when the specified condition is not met. Unlike
1112 * an expectation failure, it will prevent the test case from continuing to run;
1113 * this is otherwise known as an *assertion failure*.
1115 #define KUNIT_ASSERT_TRUE(test, condition) \
1116 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
1118 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1119 KUNIT_TRUE_MSG_ASSERTION(test, \
1126 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1127 * @test: The test context object.
1128 * @condition: an arbitrary boolean expression.
1130 * Sets an assertion that the value that @condition evaluates to is false. This
1131 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1132 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1134 #define KUNIT_ASSERT_FALSE(test, condition) \
1135 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
1137 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1138 KUNIT_FALSE_MSG_ASSERTION(test, \
1145 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
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 values that @left and @right evaluate to are
1151 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1152 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1154 #define KUNIT_ASSERT_EQ(test, left, right) \
1155 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
1157 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1158 KUNIT_BINARY_INT_ASSERTION(test, \
1165 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1166 * @test: The test context object.
1167 * @left: an arbitrary expression that evaluates to a pointer.
1168 * @right: an arbitrary expression that evaluates to a pointer.
1170 * Sets an assertion that the values that @left and @right evaluate to are
1171 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1172 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1174 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1175 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
1177 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1178 KUNIT_BINARY_PTR_ASSERTION(test, \
1185 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1186 * @test: The test context object.
1187 * @left: an arbitrary expression that evaluates to a primitive C type.
1188 * @right: an arbitrary expression that evaluates to a primitive C type.
1190 * Sets an assertion that the values that @left and @right evaluate to are not
1191 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1192 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1194 #define KUNIT_ASSERT_NE(test, left, right) \
1195 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
1197 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1198 KUNIT_BINARY_INT_ASSERTION(test, \
1205 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1206 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1207 * @test: The test context object.
1208 * @left: an arbitrary expression that evaluates to a pointer.
1209 * @right: an arbitrary expression that evaluates to a pointer.
1211 * Sets an assertion that the values that @left and @right evaluate to are not
1212 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1213 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1215 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1216 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1218 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1219 KUNIT_BINARY_PTR_ASSERTION(test, \
1225 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1226 * @test: The test context object.
1227 * @left: an arbitrary expression that evaluates to a primitive C type.
1228 * @right: an arbitrary expression that evaluates to a primitive C type.
1230 * Sets an assertion that the value that @left evaluates to is less than the
1231 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1232 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1235 #define KUNIT_ASSERT_LT(test, left, right) \
1236 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1238 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1239 KUNIT_BINARY_INT_ASSERTION(test, \
1245 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1246 * @test: The test context object.
1247 * @left: an arbitrary expression that evaluates to a primitive C type.
1248 * @right: an arbitrary expression that evaluates to a primitive C type.
1250 * Sets an assertion that the value that @left evaluates to is less than or
1251 * equal to the value that @right evaluates to. This is the same as
1252 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1253 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1255 #define KUNIT_ASSERT_LE(test, left, right) \
1256 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1258 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1259 KUNIT_BINARY_INT_ASSERTION(test, \
1266 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1267 * @test: The test context object.
1268 * @left: an arbitrary expression that evaluates to a primitive C type.
1269 * @right: an arbitrary expression that evaluates to a primitive C type.
1271 * Sets an assertion that the value that @left evaluates to is greater than the
1272 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1273 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1276 #define KUNIT_ASSERT_GT(test, left, right) \
1277 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1279 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1280 KUNIT_BINARY_INT_ASSERTION(test, \
1287 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1288 * @test: The test context object.
1289 * @left: an arbitrary expression that evaluates to a primitive C type.
1290 * @right: an arbitrary expression that evaluates to a primitive C type.
1292 * Sets an assertion that the value that @left evaluates to is greater than the
1293 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1294 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1297 #define KUNIT_ASSERT_GE(test, left, right) \
1298 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1300 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1301 KUNIT_BINARY_INT_ASSERTION(test, \
1308 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1309 * @test: The test context object.
1310 * @left: an arbitrary expression that evaluates to a null terminated string.
1311 * @right: an arbitrary expression that evaluates to a null terminated string.
1313 * Sets an assertion that the values that @left and @right evaluate to are
1314 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1315 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1317 #define KUNIT_ASSERT_STREQ(test, left, right) \
1318 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1320 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1321 KUNIT_BINARY_STR_ASSERTION(test, \
1328 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1329 * @test: The test context object.
1330 * @left: an arbitrary expression that evaluates to a null terminated string.
1331 * @right: an arbitrary expression that evaluates to a null terminated string.
1333 * Sets an expectation that the values that @left and @right evaluate to are
1334 * not equal. This is semantically equivalent to
1335 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1336 * for more information.
1338 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1339 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1341 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1342 KUNIT_BINARY_STR_ASSERTION(test, \
1349 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1350 * @test: The test context object.
1351 * @ptr: an arbitrary pointer.
1353 * Sets an assertion that the values that @ptr evaluates to is null. This is
1354 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1355 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1357 #define KUNIT_ASSERT_NULL(test, ptr) \
1358 KUNIT_ASSERT_NULL_MSG(test, \
1362 #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1363 KUNIT_BINARY_PTR_ASSERTION(test, \
1370 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1371 * @test: The test context object.
1372 * @ptr: an arbitrary pointer.
1374 * Sets an assertion that the values that @ptr evaluates to is not null. This
1375 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1376 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1378 #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1379 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1383 #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1384 KUNIT_BINARY_PTR_ASSERTION(test, \
1391 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1392 * @test: The test context object.
1393 * @ptr: an arbitrary pointer.
1395 * Sets an assertion that the value that @ptr evaluates to is not null and not
1396 * an errno stored in a pointer. This is the same as
1397 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1398 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1400 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1401 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1403 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1404 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1411 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1412 * @name: prefix for the test parameter generator function.
1413 * @array: array of test parameters.
1414 * @get_desc: function to convert param to description; NULL to use default
1416 * Define function @name_gen_params which uses @array to generate parameters.
1418 #define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1419 static const void *name##_gen_params(const void *prev, char *desc) \
1421 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1422 if (__next - (array) < ARRAY_SIZE((array))) { \
1423 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1425 __get_desc(__next, desc); \
1431 // TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1432 // include resource.h themselves if they need it.
1433 #include <kunit/resource.h>
1435 #endif /* _KUNIT_TEST_H */