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 512
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_SUBTEST_INDENT " "
51 #define KUNIT_SUBSUBTEST_INDENT " "
54 * enum kunit_status - Type of result for a test or test suite
55 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
56 * @KUNIT_FAILURE: Denotes the test has failed.
57 * @KUNIT_SKIPPED: Denotes the test has been skipped.
66 * struct kunit_case - represents an individual test case.
68 * @run_case: the function representing the actual test case.
69 * @name: the name of the test case.
70 * @generate_params: the generator function for parameterized tests.
72 * A test case is a function with the signature,
73 * ``void (*)(struct kunit *)``
74 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
75 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
76 * with a &struct kunit_suite and will be run after the suite's init
77 * function and followed by the suite's exit function.
79 * A test case should be static and should only be created with the
80 * KUNIT_CASE() macro; additionally, every array of test cases should be
81 * terminated with an empty test case.
87 * void add_test_basic(struct kunit *test)
89 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
90 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
91 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
92 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
93 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
96 * static struct kunit_case example_test_cases[] = {
97 * KUNIT_CASE(add_test_basic),
103 void (*run_case)(struct kunit *test);
105 const void* (*generate_params)(const void *prev, char *desc);
107 /* private: internal use only. */
108 enum kunit_status status;
112 static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
125 * KUNIT_CASE - A helper for creating a &struct kunit_case
127 * @test_name: a reference to a test case function.
129 * Takes a symbol for a function representing a test case and creates a
130 * &struct kunit_case object from it. See the documentation for
131 * &struct kunit_case for an example on how to use it.
133 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
136 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
138 * @test_name: a reference to a test case function.
139 * @gen_params: a reference to a parameter generator function.
141 * The generator function::
143 * const void* gen_params(const void *prev, char *desc)
145 * is used to lazily generate a series of arbitrarily typed values that fit into
146 * a void*. The argument @prev is the previously returned value, which should be
147 * used to derive the next value; @prev is set to NULL on the initial generator
148 * call. When no more values are available, the generator must return NULL.
149 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
150 * describing the parameter.
152 #define KUNIT_CASE_PARAM(test_name, gen_params) \
153 { .run_case = test_name, .name = #test_name, \
154 .generate_params = gen_params }
157 * struct kunit_suite - describes a related collection of &struct kunit_case
159 * @name: the name of the test. Purely informational.
160 * @suite_init: called once per test suite before the test cases.
161 * @suite_exit: called once per test suite after all test cases.
162 * @init: called before every test case.
163 * @exit: called after every test case.
164 * @test_cases: a null terminated array of test cases.
166 * A kunit_suite is a collection of related &struct kunit_case s, such that
167 * @init is called before every test case and @exit is called after every
168 * test case, similar to the notion of a *test fixture* or a *test class*
169 * in other unit testing frameworks like JUnit or Googletest.
171 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
175 const char name[256];
176 int (*suite_init)(struct kunit_suite *suite);
177 void (*suite_exit)(struct kunit_suite *suite);
178 int (*init)(struct kunit *test);
179 void (*exit)(struct kunit *test);
180 struct kunit_case *test_cases;
182 /* private: internal use only */
183 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
184 struct dentry *debugfs;
190 * struct kunit - represents a running instance of a test.
192 * @priv: for user to store arbitrary data. Commonly used to pass data
193 * created in the init function (see &struct kunit_suite).
195 * Used to store information about the current context under which the test
196 * is running. Most of this data is private and should only be accessed
197 * indirectly via public functions; the one exception is @priv which can be
198 * used by the test writer to store arbitrary data.
203 /* private: internal use only. */
204 const char *name; /* Read only after initialization! */
205 char *log; /* Points at case log after initialization */
206 struct kunit_try_catch try_catch;
207 /* param_value is the current parameter value for a test case. */
208 const void *param_value;
209 /* param_index stores the index of the parameter in parameterized tests. */
212 * success starts as true, and may only be set to false during a
213 * test case; thus, it is safe to update this across multiple
214 * threads using WRITE_ONCE; however, as a consequence, it may only
215 * be read after the test case finishes once all threads associated
216 * with the test case have terminated.
218 spinlock_t lock; /* Guards all mutable test state. */
219 enum kunit_status status; /* Read only after test_case finishes! */
221 * Because resources is a list that may be updated multiple times (with
222 * new resources) from any thread associated with a test case, we must
223 * protect it with some type of lock.
225 struct list_head resources; /* Protected by lock. */
227 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
230 static inline void kunit_set_failure(struct kunit *test)
232 WRITE_ONCE(test->status, KUNIT_FAILURE);
235 bool kunit_enabled(void);
237 void kunit_init_test(struct kunit *test, const char *name, char *log);
239 int kunit_run_tests(struct kunit_suite *suite);
241 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
243 unsigned int kunit_test_case_num(struct kunit_suite *suite,
244 struct kunit_case *test_case);
246 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
248 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
250 #if IS_BUILTIN(CONFIG_KUNIT)
251 int kunit_run_all_tests(void);
253 static inline int kunit_run_all_tests(void)
257 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
259 #define __kunit_test_suites(unique_array, ...) \
260 static struct kunit_suite *unique_array[] \
261 __aligned(sizeof(struct kunit_suite *)) \
262 __used __section(".kunit_test_suites") = { __VA_ARGS__ }
265 * kunit_test_suites() - used to register one or more &struct kunit_suite
268 * @__suites: a statically allocated list of &struct kunit_suite.
270 * Registers @suites with the test framework.
271 * This is done by placing the array of struct kunit_suite * in the
272 * .kunit_test_suites ELF section.
274 * When builtin, KUnit tests are all run via the executor at boot, and when
275 * built as a module, they run on module load.
278 #define kunit_test_suites(__suites...) \
279 __kunit_test_suites(__UNIQUE_ID(array), \
282 #define kunit_test_suite(suite) kunit_test_suites(&suite)
285 * kunit_test_init_section_suites() - used to register one or more &struct
286 * kunit_suite containing init functions or
289 * @__suites: a statically allocated list of &struct kunit_suite.
291 * This functions identically as kunit_test_suites() except that it suppresses
292 * modpost warnings for referencing functions marked __init or data marked
293 * __initdata; this is OK because currently KUnit only runs tests upon boot
294 * during the init phase or upon loading a module during the init phase.
296 * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
297 * tests must be excluded.
299 * The only thing this macro does that's different from kunit_test_suites is
300 * that it suffixes the array and suite declarations it makes with _probe;
301 * modpost suppresses warnings about referencing init data for symbols named in
304 #define kunit_test_init_section_suites(__suites...) \
305 __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
308 #define kunit_test_init_section_suite(suite) \
309 kunit_test_init_section_suites(&suite)
311 #define kunit_suite_for_each_test_case(suite, test_case) \
312 for (test_case = suite->test_cases; test_case->run_case; test_case++)
314 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
317 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
318 * @test: The test context object.
319 * @n: number of elements.
320 * @size: The size in bytes of the desired memory.
321 * @gfp: flags passed to underlying kmalloc().
323 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
324 * and is automatically cleaned up after the test case concludes. See &struct
325 * kunit_resource for more information.
327 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
330 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
331 * @test: The test context object.
332 * @size: The size in bytes of the desired memory.
333 * @gfp: flags passed to underlying kmalloc().
335 * See kmalloc() and kunit_kmalloc_array() for more information.
337 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
339 return kunit_kmalloc_array(test, 1, size, gfp);
343 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
344 * @test: The test case to which the resource belongs.
345 * @ptr: The memory allocation to free.
347 void kunit_kfree(struct kunit *test, const void *ptr);
350 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
351 * @test: The test context object.
352 * @size: The size in bytes of the desired memory.
353 * @gfp: flags passed to underlying kmalloc().
355 * See kzalloc() and kunit_kmalloc_array() for more information.
357 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
359 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
363 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
364 * @test: The test context object.
365 * @n: number of elements.
366 * @size: The size in bytes of the desired memory.
367 * @gfp: flags passed to underlying kmalloc().
369 * See kcalloc() and kunit_kmalloc_array() for more information.
371 static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
373 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
376 void kunit_cleanup(struct kunit *test);
378 void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
381 * kunit_mark_skipped() - Marks @test_or_suite as skipped
383 * @test_or_suite: The test context object.
384 * @fmt: A printk() style format string.
386 * Marks the test as skipped. @fmt is given output as the test status
387 * comment, typically the reason the test was skipped.
389 * Test execution continues after kunit_mark_skipped() is called.
391 #define kunit_mark_skipped(test_or_suite, fmt, ...) \
393 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
394 scnprintf((test_or_suite)->status_comment, \
395 KUNIT_STATUS_COMMENT_SIZE, \
396 fmt, ##__VA_ARGS__); \
400 * kunit_skip() - Marks @test_or_suite as skipped
402 * @test_or_suite: The test context object.
403 * @fmt: A printk() style format string.
405 * Skips the test. @fmt is given output as the test status
406 * comment, typically the reason the test was skipped.
408 * Test execution is halted after kunit_skip() is called.
410 #define kunit_skip(test_or_suite, fmt, ...) \
412 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
413 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
417 * printk and log to per-test or per-suite log buffer. Logging only done
418 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
420 #define kunit_log(lvl, test_or_suite, fmt, ...) \
422 printk(lvl fmt, ##__VA_ARGS__); \
423 kunit_log_append((test_or_suite)->log, fmt "\n", \
427 #define kunit_printk(lvl, test, fmt, ...) \
428 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
429 (test)->name, ##__VA_ARGS__)
432 * kunit_info() - Prints an INFO level message associated with @test.
434 * @test: The test context object.
435 * @fmt: A printk() style format string.
437 * Prints an info level message associated with the test suite being run.
438 * Takes a variable number of format parameters just like printk().
440 #define kunit_info(test, fmt, ...) \
441 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
444 * kunit_warn() - Prints a WARN level message associated with @test.
446 * @test: The test context object.
447 * @fmt: A printk() style format string.
449 * Prints a warning level message.
451 #define kunit_warn(test, fmt, ...) \
452 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
455 * kunit_err() - Prints an ERROR level message associated with @test.
457 * @test: The test context object.
458 * @fmt: A printk() style format string.
460 * Prints an error level message.
462 #define kunit_err(test, fmt, ...) \
463 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
466 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
467 * @test: The test context object.
469 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
470 * words, it does nothing and only exists for code clarity. See
471 * KUNIT_EXPECT_TRUE() for more information.
473 #define KUNIT_SUCCEED(test) do {} while (0)
475 void kunit_do_failed_assertion(struct kunit *test,
476 const struct kunit_loc *loc,
477 enum kunit_assert_type type,
478 const struct kunit_assert *assert,
479 assert_format_t assert_format,
480 const char *fmt, ...);
482 #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
483 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
484 const struct assert_class __assertion = INITIALIZER; \
485 kunit_do_failed_assertion(test, \
488 &__assertion.assert, \
495 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
496 _KUNIT_FAILED(test, \
499 kunit_fail_assert_format, \
505 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
506 * @test: The test context object.
507 * @fmt: an informational message to be printed when the assertion is made.
508 * @...: string format arguments.
510 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
511 * other words, it always results in a failed expectation, and consequently
512 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
513 * for more information.
515 #define KUNIT_FAIL(test, fmt, ...) \
516 KUNIT_FAIL_ASSERTION(test, \
521 /* Helper to safely pass around an initializer list to other macros. */
522 #define KUNIT_INIT_ASSERT(initializers...) { initializers }
524 #define KUNIT_UNARY_ASSERTION(test, \
531 if (likely(!!(condition_) == !!expected_true_)) \
534 _KUNIT_FAILED(test, \
536 kunit_unary_assert, \
537 kunit_unary_assert_format, \
538 KUNIT_INIT_ASSERT(.condition = #condition_, \
539 .expected_true = expected_true_), \
544 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
545 KUNIT_UNARY_ASSERTION(test, \
552 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
553 KUNIT_UNARY_ASSERTION(test, \
561 * A factory macro for defining the assertions and expectations for the basic
562 * comparisons defined for the built in types.
564 * Unfortunately, there is no common type that all types can be promoted to for
565 * which all the binary operators behave the same way as for the actual types
566 * (for example, there is no type that long long and unsigned long long can
567 * both be cast to where the comparison result is preserved for all values). So
568 * the best we can do is do the comparison in the original types and then coerce
569 * everything to long long for printing; this way, the comparison behaves
570 * correctly and the printed out value usually makes sense without
571 * interpretation, but can always be interpreted to figure out the actual
574 #define KUNIT_BASE_BINARY_ASSERTION(test, \
584 const typeof(left) __left = (left); \
585 const typeof(right) __right = (right); \
586 static const struct kunit_binary_assert_text __text = { \
588 .left_text = #left, \
589 .right_text = #right, \
592 if (likely(__left op __right)) \
595 _KUNIT_FAILED(test, \
599 KUNIT_INIT_ASSERT(.text = &__text, \
600 .left_value = __left, \
601 .right_value = __right), \
606 #define KUNIT_BINARY_INT_ASSERTION(test, \
613 KUNIT_BASE_BINARY_ASSERTION(test, \
614 kunit_binary_assert, \
615 kunit_binary_assert_format, \
621 #define KUNIT_BINARY_PTR_ASSERTION(test, \
628 KUNIT_BASE_BINARY_ASSERTION(test, \
629 kunit_binary_ptr_assert, \
630 kunit_binary_ptr_assert_format, \
636 #define KUNIT_BINARY_STR_ASSERTION(test, \
644 const char *__left = (left); \
645 const char *__right = (right); \
646 static const struct kunit_binary_assert_text __text = { \
648 .left_text = #left, \
649 .right_text = #right, \
652 if (likely(strcmp(__left, __right) op 0)) \
656 _KUNIT_FAILED(test, \
658 kunit_binary_str_assert, \
659 kunit_binary_str_assert_format, \
660 KUNIT_INIT_ASSERT(.text = &__text, \
661 .left_value = __left, \
662 .right_value = __right), \
667 #define KUNIT_MEM_ASSERTION(test, \
676 const void *__left = (left); \
677 const void *__right = (right); \
678 const size_t __size = (size_); \
679 static const struct kunit_binary_assert_text __text = { \
681 .left_text = #left, \
682 .right_text = #right, \
685 if (likely(__left && __right)) \
686 if (likely(memcmp(__left, __right, __size) op 0)) \
689 _KUNIT_FAILED(test, \
692 kunit_mem_assert_format, \
693 KUNIT_INIT_ASSERT(.text = &__text, \
694 .left_value = __left, \
695 .right_value = __right, \
701 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
707 const typeof(ptr) __ptr = (ptr); \
709 if (!IS_ERR_OR_NULL(__ptr)) \
712 _KUNIT_FAILED(test, \
714 kunit_ptr_not_err_assert, \
715 kunit_ptr_not_err_assert_format, \
716 KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \
722 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
723 * @test: The test context object.
724 * @condition: an arbitrary boolean expression. The test fails when this does
725 * not evaluate to true.
727 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
728 * to fail when the specified condition is not met; however, it will not prevent
729 * the test case from continuing to run; this is otherwise known as an
730 * *expectation failure*.
732 #define KUNIT_EXPECT_TRUE(test, condition) \
733 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
735 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
736 KUNIT_TRUE_MSG_ASSERTION(test, \
743 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
744 * @test: The test context object.
745 * @condition: an arbitrary boolean expression. The test fails when this does
746 * not evaluate to false.
748 * Sets an expectation that @condition evaluates to false. See
749 * KUNIT_EXPECT_TRUE() for more information.
751 #define KUNIT_EXPECT_FALSE(test, condition) \
752 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
754 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
755 KUNIT_FALSE_MSG_ASSERTION(test, \
762 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
763 * @test: The test context object.
764 * @left: an arbitrary expression that evaluates to a primitive C type.
765 * @right: an arbitrary expression that evaluates to a primitive C type.
767 * Sets an expectation that the values that @left and @right evaluate to are
768 * equal. This is semantically equivalent to
769 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
772 #define KUNIT_EXPECT_EQ(test, left, right) \
773 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
775 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
776 KUNIT_BINARY_INT_ASSERTION(test, \
783 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
784 * @test: The test context object.
785 * @left: an arbitrary expression that evaluates to a pointer.
786 * @right: an arbitrary expression that evaluates to a pointer.
788 * Sets an expectation that the values that @left and @right evaluate to are
789 * equal. This is semantically equivalent to
790 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
793 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
794 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
796 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
797 KUNIT_BINARY_PTR_ASSERTION(test, \
804 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
805 * @test: The test context object.
806 * @left: an arbitrary expression that evaluates to a primitive C type.
807 * @right: an arbitrary expression that evaluates to a primitive C type.
809 * Sets an expectation that the values that @left and @right evaluate to are not
810 * equal. This is semantically equivalent to
811 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
814 #define KUNIT_EXPECT_NE(test, left, right) \
815 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
817 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
818 KUNIT_BINARY_INT_ASSERTION(test, \
825 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
826 * @test: The test context object.
827 * @left: an arbitrary expression that evaluates to a pointer.
828 * @right: an arbitrary expression that evaluates to a pointer.
830 * Sets an expectation that the values that @left and @right evaluate to are not
831 * equal. This is semantically equivalent to
832 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
835 #define KUNIT_EXPECT_PTR_NE(test, left, right) \
836 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
838 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
839 KUNIT_BINARY_PTR_ASSERTION(test, \
846 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
847 * @test: The test context object.
848 * @left: an arbitrary expression that evaluates to a primitive C type.
849 * @right: an arbitrary expression that evaluates to a primitive C type.
851 * Sets an expectation that the value that @left evaluates to is less than the
852 * value that @right evaluates to. This is semantically equivalent to
853 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
856 #define KUNIT_EXPECT_LT(test, left, right) \
857 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
859 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
860 KUNIT_BINARY_INT_ASSERTION(test, \
867 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
868 * @test: The test context object.
869 * @left: an arbitrary expression that evaluates to a primitive C type.
870 * @right: an arbitrary expression that evaluates to a primitive C type.
872 * Sets an expectation that the value that @left evaluates to is less than or
873 * equal to the value that @right evaluates to. Semantically this is equivalent
874 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
877 #define KUNIT_EXPECT_LE(test, left, right) \
878 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
880 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
881 KUNIT_BINARY_INT_ASSERTION(test, \
888 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
889 * @test: The test context object.
890 * @left: an arbitrary expression that evaluates to a primitive C type.
891 * @right: an arbitrary expression that evaluates to a primitive C type.
893 * Sets an expectation that the value that @left evaluates to is greater than
894 * the value that @right evaluates to. This is semantically equivalent to
895 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
898 #define KUNIT_EXPECT_GT(test, left, right) \
899 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
901 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
902 KUNIT_BINARY_INT_ASSERTION(test, \
909 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
910 * @test: The test context object.
911 * @left: an arbitrary expression that evaluates to a primitive C type.
912 * @right: an arbitrary expression that evaluates to a primitive C type.
914 * Sets an expectation that the value that @left evaluates to is greater than
915 * the value that @right evaluates to. This is semantically equivalent to
916 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
919 #define KUNIT_EXPECT_GE(test, left, right) \
920 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
922 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
923 KUNIT_BINARY_INT_ASSERTION(test, \
930 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
931 * @test: The test context object.
932 * @left: an arbitrary expression that evaluates to a null terminated string.
933 * @right: an arbitrary expression that evaluates to a null terminated string.
935 * Sets an expectation that the values that @left and @right evaluate to are
936 * equal. This is semantically equivalent to
937 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
938 * for more information.
940 #define KUNIT_EXPECT_STREQ(test, left, right) \
941 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
943 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
944 KUNIT_BINARY_STR_ASSERTION(test, \
951 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
952 * @test: The test context object.
953 * @left: an arbitrary expression that evaluates to a null terminated string.
954 * @right: an arbitrary expression that evaluates to a null terminated string.
956 * Sets an expectation that the values that @left and @right evaluate to are
957 * not equal. This is semantically equivalent to
958 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
959 * for more information.
961 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
962 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
964 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
965 KUNIT_BINARY_STR_ASSERTION(test, \
972 * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.
973 * @test: The test context object.
974 * @left: An arbitrary expression that evaluates to the specified size.
975 * @right: An arbitrary expression that evaluates to the specified size.
976 * @size: Number of bytes compared.
978 * Sets an expectation that the values that @left and @right evaluate to are
979 * equal. This is semantically equivalent to
980 * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
981 * KUNIT_EXPECT_TRUE() for more information.
983 * Although this expectation works for any memory block, it is not recommended
984 * for comparing more structured data, such as structs. This expectation is
985 * recommended for comparing, for example, data arrays.
987 #define KUNIT_EXPECT_MEMEQ(test, left, right, size) \
988 KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)
990 #define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
991 KUNIT_MEM_ASSERTION(test, \
999 * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.
1000 * @test: The test context object.
1001 * @left: An arbitrary expression that evaluates to the specified size.
1002 * @right: An arbitrary expression that evaluates to the specified size.
1003 * @size: Number of bytes compared.
1005 * Sets an expectation that the values that @left and @right evaluate to are
1006 * not equal. This is semantically equivalent to
1007 * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1008 * KUNIT_EXPECT_TRUE() for more information.
1010 * Although this expectation works for any memory block, it is not recommended
1011 * for comparing more structured data, such as structs. This expectation is
1012 * recommended for comparing, for example, data arrays.
1014 #define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \
1015 KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)
1017 #define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1018 KUNIT_MEM_ASSERTION(test, \
1019 KUNIT_EXPECTATION, \
1026 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
1027 * @test: The test context object.
1028 * @ptr: an arbitrary pointer.
1030 * Sets an expectation that the value that @ptr evaluates to is null. This is
1031 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
1032 * See KUNIT_EXPECT_TRUE() for more information.
1034 #define KUNIT_EXPECT_NULL(test, ptr) \
1035 KUNIT_EXPECT_NULL_MSG(test, \
1039 #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
1040 KUNIT_BINARY_PTR_ASSERTION(test, \
1041 KUNIT_EXPECTATION, \
1047 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
1048 * @test: The test context object.
1049 * @ptr: an arbitrary pointer.
1051 * Sets an expectation that the value that @ptr evaluates to is not null. This
1052 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
1053 * See KUNIT_EXPECT_TRUE() for more information.
1055 #define KUNIT_EXPECT_NOT_NULL(test, ptr) \
1056 KUNIT_EXPECT_NOT_NULL_MSG(test, \
1060 #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1061 KUNIT_BINARY_PTR_ASSERTION(test, \
1062 KUNIT_EXPECTATION, \
1068 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1069 * @test: The test context object.
1070 * @ptr: an arbitrary pointer.
1072 * Sets an expectation that the value that @ptr evaluates to is not null and not
1073 * an errno stored in a pointer. This is semantically equivalent to
1074 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1077 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1078 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1080 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1081 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1082 KUNIT_EXPECTATION, \
1087 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1088 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1091 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1092 * @test: The test context object.
1093 * @condition: an arbitrary boolean expression. The test fails and aborts when
1094 * this does not evaluate to true.
1096 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1097 * fail *and immediately abort* when the specified condition is not met. Unlike
1098 * an expectation failure, it will prevent the test case from continuing to run;
1099 * this is otherwise known as an *assertion failure*.
1101 #define KUNIT_ASSERT_TRUE(test, condition) \
1102 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
1104 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1105 KUNIT_TRUE_MSG_ASSERTION(test, \
1112 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1113 * @test: The test context object.
1114 * @condition: an arbitrary boolean expression.
1116 * Sets an assertion that the value that @condition evaluates to is false. This
1117 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1118 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1120 #define KUNIT_ASSERT_FALSE(test, condition) \
1121 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
1123 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1124 KUNIT_FALSE_MSG_ASSERTION(test, \
1131 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1132 * @test: The test context object.
1133 * @left: an arbitrary expression that evaluates to a primitive C type.
1134 * @right: an arbitrary expression that evaluates to a primitive C type.
1136 * Sets an assertion that the values that @left and @right evaluate to are
1137 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1138 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1140 #define KUNIT_ASSERT_EQ(test, left, right) \
1141 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
1143 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1144 KUNIT_BINARY_INT_ASSERTION(test, \
1151 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1152 * @test: The test context object.
1153 * @left: an arbitrary expression that evaluates to a pointer.
1154 * @right: an arbitrary expression that evaluates to a pointer.
1156 * Sets an assertion that the values that @left and @right evaluate to are
1157 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1158 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1160 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1161 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
1163 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1164 KUNIT_BINARY_PTR_ASSERTION(test, \
1171 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1172 * @test: The test context object.
1173 * @left: an arbitrary expression that evaluates to a primitive C type.
1174 * @right: an arbitrary expression that evaluates to a primitive C type.
1176 * Sets an assertion that the values that @left and @right evaluate to are not
1177 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1178 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1180 #define KUNIT_ASSERT_NE(test, left, right) \
1181 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
1183 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1184 KUNIT_BINARY_INT_ASSERTION(test, \
1191 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1192 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1193 * @test: The test context object.
1194 * @left: an arbitrary expression that evaluates to a pointer.
1195 * @right: an arbitrary expression that evaluates to a pointer.
1197 * Sets an assertion that the values that @left and @right evaluate to are not
1198 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1199 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1201 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1202 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1204 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1205 KUNIT_BINARY_PTR_ASSERTION(test, \
1211 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1212 * @test: The test context object.
1213 * @left: an arbitrary expression that evaluates to a primitive C type.
1214 * @right: an arbitrary expression that evaluates to a primitive C type.
1216 * Sets an assertion that the value that @left evaluates to is less than the
1217 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1218 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1221 #define KUNIT_ASSERT_LT(test, left, right) \
1222 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1224 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1225 KUNIT_BINARY_INT_ASSERTION(test, \
1231 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1232 * @test: The test context object.
1233 * @left: an arbitrary expression that evaluates to a primitive C type.
1234 * @right: an arbitrary expression that evaluates to a primitive C type.
1236 * Sets an assertion that the value that @left evaluates to is less than or
1237 * equal to the value that @right evaluates to. This is the same as
1238 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1239 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1241 #define KUNIT_ASSERT_LE(test, left, right) \
1242 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1244 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1245 KUNIT_BINARY_INT_ASSERTION(test, \
1252 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1253 * @test: The test context object.
1254 * @left: an arbitrary expression that evaluates to a primitive C type.
1255 * @right: an arbitrary expression that evaluates to a primitive C type.
1257 * Sets an assertion that the value that @left evaluates to is greater than the
1258 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1259 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1262 #define KUNIT_ASSERT_GT(test, left, right) \
1263 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1265 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1266 KUNIT_BINARY_INT_ASSERTION(test, \
1273 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1274 * @test: The test context object.
1275 * @left: an arbitrary expression that evaluates to a primitive C type.
1276 * @right: an arbitrary expression that evaluates to a primitive C type.
1278 * Sets an assertion that the value that @left evaluates to is greater than the
1279 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1280 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1283 #define KUNIT_ASSERT_GE(test, left, right) \
1284 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1286 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1287 KUNIT_BINARY_INT_ASSERTION(test, \
1294 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1295 * @test: The test context object.
1296 * @left: an arbitrary expression that evaluates to a null terminated string.
1297 * @right: an arbitrary expression that evaluates to a null terminated string.
1299 * Sets an assertion that the values that @left and @right evaluate to are
1300 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1301 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1303 #define KUNIT_ASSERT_STREQ(test, left, right) \
1304 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1306 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1307 KUNIT_BINARY_STR_ASSERTION(test, \
1314 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1315 * @test: The test context object.
1316 * @left: an arbitrary expression that evaluates to a null terminated string.
1317 * @right: an arbitrary expression that evaluates to a null terminated string.
1319 * Sets an expectation that the values that @left and @right evaluate to are
1320 * not equal. This is semantically equivalent to
1321 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1322 * for more information.
1324 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1325 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1327 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1328 KUNIT_BINARY_STR_ASSERTION(test, \
1335 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1336 * @test: The test context object.
1337 * @ptr: an arbitrary pointer.
1339 * Sets an assertion that the values that @ptr evaluates to is null. This is
1340 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1341 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1343 #define KUNIT_ASSERT_NULL(test, ptr) \
1344 KUNIT_ASSERT_NULL_MSG(test, \
1348 #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1349 KUNIT_BINARY_PTR_ASSERTION(test, \
1356 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1357 * @test: The test context object.
1358 * @ptr: an arbitrary pointer.
1360 * Sets an assertion that the values that @ptr evaluates to is not null. This
1361 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1362 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1364 #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1365 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1369 #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1370 KUNIT_BINARY_PTR_ASSERTION(test, \
1377 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1378 * @test: The test context object.
1379 * @ptr: an arbitrary pointer.
1381 * Sets an assertion that the value that @ptr evaluates to is not null and not
1382 * an errno stored in a pointer. This is the same as
1383 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1384 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1386 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1387 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1389 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1390 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1397 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1398 * @name: prefix for the test parameter generator function.
1399 * @array: array of test parameters.
1400 * @get_desc: function to convert param to description; NULL to use default
1402 * Define function @name_gen_params which uses @array to generate parameters.
1404 #define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1405 static const void *name##_gen_params(const void *prev, char *desc) \
1407 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1408 if (__next - (array) < ARRAY_SIZE((array))) { \
1409 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1411 __get_desc(__next, desc); \
1417 // TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1418 // include resource.h themselves if they need it.
1419 #include <kunit/resource.h>
1421 #endif /* _KUNIT_TEST_H */