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>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
19 struct kunit_resource;
21 typedef int (*kunit_resource_init_t)(struct kunit_resource *, void *);
22 typedef void (*kunit_resource_free_t)(struct kunit_resource *);
25 * struct kunit_resource - represents a *test managed resource*
26 * @allocation: for the user to store arbitrary data.
27 * @free: a user supplied function to free the resource. Populated by
28 * kunit_alloc_resource().
30 * Represents a *test managed resource*, a resource which will automatically be
31 * cleaned up at the end of a test case.
37 * struct kunit_kmalloc_params {
42 * static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
44 * struct kunit_kmalloc_params *params = context;
45 * res->allocation = kmalloc(params->size, params->gfp);
47 * if (!res->allocation)
53 * static void kunit_kmalloc_free(struct kunit_resource *res)
55 * kfree(res->allocation);
58 * void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
60 * struct kunit_kmalloc_params params;
61 * struct kunit_resource *res;
66 * res = kunit_alloc_resource(test, kunit_kmalloc_init,
67 * kunit_kmalloc_free, ¶ms);
69 * return res->allocation;
74 struct kunit_resource {
76 kunit_resource_free_t free;
78 /* private: internal use only. */
79 struct list_head node;
84 /* Size of log associated with test. */
85 #define KUNIT_LOG_SIZE 512
88 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
89 * sub-subtest. See the "Subtests" section in
90 * https://node-tap.org/tap-protocol/
92 #define KUNIT_SUBTEST_INDENT " "
93 #define KUNIT_SUBSUBTEST_INDENT " "
96 * struct kunit_case - represents an individual test case.
98 * @run_case: the function representing the actual test case.
99 * @name: the name of the test case.
101 * A test case is a function with the signature,
102 * ``void (*)(struct kunit *)``
103 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
104 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
105 * with a &struct kunit_suite and will be run after the suite's init
106 * function and followed by the suite's exit function.
108 * A test case should be static and should only be created with the
109 * KUNIT_CASE() macro; additionally, every array of test cases should be
110 * terminated with an empty test case.
116 * void add_test_basic(struct kunit *test)
118 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
119 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
120 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
121 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
122 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
125 * static struct kunit_case example_test_cases[] = {
126 * KUNIT_CASE(add_test_basic),
132 void (*run_case)(struct kunit *test);
135 /* private: internal use only. */
140 static inline char *kunit_status_to_string(bool status)
142 return status ? "ok" : "not ok";
146 * KUNIT_CASE - A helper for creating a &struct kunit_case
148 * @test_name: a reference to a test case function.
150 * Takes a symbol for a function representing a test case and creates a
151 * &struct kunit_case object from it. See the documentation for
152 * &struct kunit_case for an example on how to use it.
154 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
157 * struct kunit_suite - describes a related collection of &struct kunit_case
159 * @name: the name of the test. Purely informational.
160 * @init: called before every test case.
161 * @exit: called after every test case.
162 * @test_cases: a null terminated array of test cases.
164 * A kunit_suite is a collection of related &struct kunit_case s, such that
165 * @init is called before every test case and @exit is called after every
166 * test case, similar to the notion of a *test fixture* or a *test class*
167 * in other unit testing frameworks like JUnit or Googletest.
169 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
173 const char name[256];
174 int (*init)(struct kunit *test);
175 void (*exit)(struct kunit *test);
176 struct kunit_case *test_cases;
178 /* private: internal use only */
179 struct dentry *debugfs;
184 * struct kunit - represents a running instance of a test.
186 * @priv: for user to store arbitrary data. Commonly used to pass data
187 * created in the init function (see &struct kunit_suite).
189 * Used to store information about the current context under which the test
190 * is running. Most of this data is private and should only be accessed
191 * indirectly via public functions; the one exception is @priv which can be
192 * used by the test writer to store arbitrary data.
197 /* private: internal use only. */
198 const char *name; /* Read only after initialization! */
199 char *log; /* Points at case log after initialization */
200 struct kunit_try_catch try_catch;
202 * success starts as true, and may only be set to false during a
203 * test case; thus, it is safe to update this across multiple
204 * threads using WRITE_ONCE; however, as a consequence, it may only
205 * be read after the test case finishes once all threads associated
206 * with the test case have terminated.
208 bool success; /* Read only after test_case finishes! */
209 spinlock_t lock; /* Guards all mutable test state. */
211 * Because resources is a list that may be updated multiple times (with
212 * new resources) from any thread associated with a test case, we must
213 * protect it with some type of lock.
215 struct list_head resources; /* Protected by lock. */
218 void kunit_init_test(struct kunit *test, const char *name, char *log);
220 int kunit_run_tests(struct kunit_suite *suite);
222 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
224 unsigned int kunit_test_case_num(struct kunit_suite *suite,
225 struct kunit_case *test_case);
227 int __kunit_test_suites_init(struct kunit_suite **suites);
229 void __kunit_test_suites_exit(struct kunit_suite **suites);
232 * kunit_test_suites() - used to register one or more &struct kunit_suite
235 * @suites_list...: a statically allocated list of &struct kunit_suite.
237 * Registers @suites_list with the test framework. See &struct kunit_suite for
240 * When builtin, KUnit tests are all run as late_initcalls; this means
241 * that they cannot test anything where tests must run at a different init
242 * phase. One significant restriction resulting from this is that KUnit
243 * cannot reliably test anything that is initialize in the late_init phase;
244 * another is that KUnit is useless to test things that need to be run in
245 * an earlier init phase.
247 * An alternative is to build the tests as a module. Because modules
248 * do not support multiple late_initcall()s, we need to initialize an
249 * array of suites for a module.
251 * TODO(brendanhiggins@google.com): Don't run all KUnit tests as
252 * late_initcalls. I have some future work planned to dispatch all KUnit
253 * tests from the same place, and at the very least to do so after
254 * everything else is definitely initialized.
256 #define kunit_test_suites(suites_list...) \
257 static struct kunit_suite *suites[] = {suites_list, NULL}; \
258 static int kunit_test_suites_init(void) \
260 return __kunit_test_suites_init(suites); \
262 late_initcall(kunit_test_suites_init); \
263 static void __exit kunit_test_suites_exit(void) \
265 return __kunit_test_suites_exit(suites); \
267 module_exit(kunit_test_suites_exit)
269 #define kunit_test_suite(suite) kunit_test_suites(&suite)
271 #define kunit_suite_for_each_test_case(suite, test_case) \
272 for (test_case = suite->test_cases; test_case->run_case; test_case++)
274 bool kunit_suite_has_succeeded(struct kunit_suite *suite);
277 * Like kunit_alloc_resource() below, but returns the struct kunit_resource
278 * object that contains the allocation. This is mostly for testing purposes.
280 struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
281 kunit_resource_init_t init,
282 kunit_resource_free_t free,
287 * kunit_alloc_resource() - Allocates a *test managed resource*.
288 * @test: The test context object.
289 * @init: a user supplied function to initialize the resource.
290 * @free: a user supplied function to free the resource.
291 * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
292 * @context: for the user to pass in arbitrary data to the init function.
294 * Allocates a *test managed resource*, a resource which will automatically be
295 * cleaned up at the end of a test case. See &struct kunit_resource for an
298 * NOTE: KUnit needs to allocate memory for each kunit_resource object. You must
299 * specify an @internal_gfp that is compatible with the use context of your
302 static inline void *kunit_alloc_resource(struct kunit *test,
303 kunit_resource_init_t init,
304 kunit_resource_free_t free,
308 struct kunit_resource *res;
310 res = kunit_alloc_and_get_resource(test, init, free, internal_gfp,
314 return res->allocation;
319 typedef bool (*kunit_resource_match_t)(struct kunit *test,
324 * kunit_resource_instance_match() - Match a resource with the same instance.
325 * @test: Test case to which the resource belongs.
326 * @res: The data stored in kunit_resource->allocation.
327 * @match_data: The resource pointer to match against.
329 * An instance of kunit_resource_match_t that matches a resource whose
330 * allocation matches @match_data.
332 static inline bool kunit_resource_instance_match(struct kunit *test,
336 return res == match_data;
340 * kunit_resource_destroy() - Find a kunit_resource and destroy it.
341 * @test: Test case to which the resource belongs.
342 * @match: Match function. Returns whether a given resource matches @match_data.
343 * @free: Must match free on the kunit_resource to free.
344 * @match_data: Data passed into @match.
346 * Free the latest kunit_resource of @test for which @free matches the
347 * kunit_resource_free_t associated with the resource and for which @match
351 * 0 if kunit_resource is found and freed, -ENOENT if not found.
353 int kunit_resource_destroy(struct kunit *test,
354 kunit_resource_match_t match,
355 kunit_resource_free_t free,
359 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
360 * @test: The test context object.
361 * @size: The size in bytes of the desired memory.
362 * @gfp: flags passed to underlying kmalloc().
364 * Just like `kmalloc(...)`, except the allocation is managed by the test case
365 * and is automatically cleaned up after the test case concludes. See &struct
366 * kunit_resource for more information.
368 void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp);
371 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
372 * @test: The test case to which the resource belongs.
373 * @ptr: The memory allocation to free.
375 void kunit_kfree(struct kunit *test, const void *ptr);
378 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
379 * @test: The test context object.
380 * @size: The size in bytes of the desired memory.
381 * @gfp: flags passed to underlying kmalloc().
383 * See kzalloc() and kunit_kmalloc() for more information.
385 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
387 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
390 void kunit_cleanup(struct kunit *test);
392 void kunit_log_append(char *log, const char *fmt, ...);
395 * printk and log to per-test or per-suite log buffer. Logging only done
396 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
398 #define kunit_log(lvl, test_or_suite, fmt, ...) \
400 printk(lvl fmt, ##__VA_ARGS__); \
401 kunit_log_append((test_or_suite)->log, fmt "\n", \
405 #define kunit_printk(lvl, test, fmt, ...) \
406 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
407 (test)->name, ##__VA_ARGS__)
410 * kunit_info() - Prints an INFO level message associated with @test.
412 * @test: The test context object.
413 * @fmt: A printk() style format string.
415 * Prints an info level message associated with the test suite being run.
416 * Takes a variable number of format parameters just like printk().
418 #define kunit_info(test, fmt, ...) \
419 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
422 * kunit_warn() - Prints a WARN level message associated with @test.
424 * @test: The test context object.
425 * @fmt: A printk() style format string.
427 * Prints a warning level message.
429 #define kunit_warn(test, fmt, ...) \
430 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
433 * kunit_err() - Prints an ERROR level message associated with @test.
435 * @test: The test context object.
436 * @fmt: A printk() style format string.
438 * Prints an error level message.
440 #define kunit_err(test, fmt, ...) \
441 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
444 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
445 * @test: The test context object.
447 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
448 * words, it does nothing and only exists for code clarity. See
449 * KUNIT_EXPECT_TRUE() for more information.
451 #define KUNIT_SUCCEED(test) do {} while (0)
453 void kunit_do_assertion(struct kunit *test,
454 struct kunit_assert *assert,
456 const char *fmt, ...);
458 #define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do { \
459 struct assert_class __assertion = INITIALIZER; \
460 kunit_do_assertion(test, \
461 &__assertion.assert, \
468 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
469 KUNIT_ASSERTION(test, \
472 KUNIT_INIT_FAIL_ASSERT_STRUCT(test, assert_type), \
477 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
478 * @test: The test context object.
479 * @fmt: an informational message to be printed when the assertion is made.
480 * @...: string format arguments.
482 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
483 * other words, it always results in a failed expectation, and consequently
484 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
485 * for more information.
487 #define KUNIT_FAIL(test, fmt, ...) \
488 KUNIT_FAIL_ASSERTION(test, \
493 #define KUNIT_UNARY_ASSERTION(test, \
499 KUNIT_ASSERTION(test, \
500 !!(condition) == !!expected_true, \
501 kunit_unary_assert, \
502 KUNIT_INIT_UNARY_ASSERT_STRUCT(test, \
509 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
510 KUNIT_UNARY_ASSERTION(test, \
517 #define KUNIT_TRUE_ASSERTION(test, assert_type, condition) \
518 KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, NULL)
520 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
521 KUNIT_UNARY_ASSERTION(test, \
528 #define KUNIT_FALSE_ASSERTION(test, assert_type, condition) \
529 KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, NULL)
532 * A factory macro for defining the assertions and expectations for the basic
533 * comparisons defined for the built in types.
535 * Unfortunately, there is no common type that all types can be promoted to for
536 * which all the binary operators behave the same way as for the actual types
537 * (for example, there is no type that long long and unsigned long long can
538 * both be cast to where the comparison result is preserved for all values). So
539 * the best we can do is do the comparison in the original types and then coerce
540 * everything to long long for printing; this way, the comparison behaves
541 * correctly and the printed out value usually makes sense without
542 * interpretation, but can always be interpreted to figure out the actual
545 #define KUNIT_BASE_BINARY_ASSERTION(test, \
555 typeof(left) __left = (left); \
556 typeof(right) __right = (right); \
557 ((void)__typecheck(__left, __right)); \
559 KUNIT_ASSERTION(test, \
562 ASSERT_CLASS_INIT(test, \
573 #define KUNIT_BASE_EQ_MSG_ASSERTION(test, \
581 KUNIT_BASE_BINARY_ASSERTION(test, \
589 #define KUNIT_BASE_NE_MSG_ASSERTION(test, \
597 KUNIT_BASE_BINARY_ASSERTION(test, \
605 #define KUNIT_BASE_LT_MSG_ASSERTION(test, \
613 KUNIT_BASE_BINARY_ASSERTION(test, \
621 #define KUNIT_BASE_LE_MSG_ASSERTION(test, \
629 KUNIT_BASE_BINARY_ASSERTION(test, \
637 #define KUNIT_BASE_GT_MSG_ASSERTION(test, \
645 KUNIT_BASE_BINARY_ASSERTION(test, \
653 #define KUNIT_BASE_GE_MSG_ASSERTION(test, \
661 KUNIT_BASE_BINARY_ASSERTION(test, \
669 #define KUNIT_BINARY_EQ_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
670 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
671 kunit_binary_assert, \
672 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
679 #define KUNIT_BINARY_EQ_ASSERTION(test, assert_type, left, right) \
680 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
686 #define KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
692 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
693 kunit_binary_ptr_assert, \
694 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
701 #define KUNIT_BINARY_PTR_EQ_ASSERTION(test, assert_type, left, right) \
702 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
708 #define KUNIT_BINARY_NE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
709 KUNIT_BASE_NE_MSG_ASSERTION(test, \
710 kunit_binary_assert, \
711 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
718 #define KUNIT_BINARY_NE_ASSERTION(test, assert_type, left, right) \
719 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
725 #define KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
731 KUNIT_BASE_NE_MSG_ASSERTION(test, \
732 kunit_binary_ptr_assert, \
733 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
740 #define KUNIT_BINARY_PTR_NE_ASSERTION(test, assert_type, left, right) \
741 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
747 #define KUNIT_BINARY_LT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
748 KUNIT_BASE_LT_MSG_ASSERTION(test, \
749 kunit_binary_assert, \
750 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
757 #define KUNIT_BINARY_LT_ASSERTION(test, assert_type, left, right) \
758 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
764 #define KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
770 KUNIT_BASE_LT_MSG_ASSERTION(test, \
771 kunit_binary_ptr_assert, \
772 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
779 #define KUNIT_BINARY_PTR_LT_ASSERTION(test, assert_type, left, right) \
780 KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
786 #define KUNIT_BINARY_LE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
787 KUNIT_BASE_LE_MSG_ASSERTION(test, \
788 kunit_binary_assert, \
789 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
796 #define KUNIT_BINARY_LE_ASSERTION(test, assert_type, left, right) \
797 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
803 #define KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
809 KUNIT_BASE_LE_MSG_ASSERTION(test, \
810 kunit_binary_ptr_assert, \
811 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
818 #define KUNIT_BINARY_PTR_LE_ASSERTION(test, assert_type, left, right) \
819 KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
825 #define KUNIT_BINARY_GT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
826 KUNIT_BASE_GT_MSG_ASSERTION(test, \
827 kunit_binary_assert, \
828 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
835 #define KUNIT_BINARY_GT_ASSERTION(test, assert_type, left, right) \
836 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
842 #define KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
848 KUNIT_BASE_GT_MSG_ASSERTION(test, \
849 kunit_binary_ptr_assert, \
850 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
857 #define KUNIT_BINARY_PTR_GT_ASSERTION(test, assert_type, left, right) \
858 KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
864 #define KUNIT_BINARY_GE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
865 KUNIT_BASE_GE_MSG_ASSERTION(test, \
866 kunit_binary_assert, \
867 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
874 #define KUNIT_BINARY_GE_ASSERTION(test, assert_type, left, right) \
875 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
881 #define KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
887 KUNIT_BASE_GE_MSG_ASSERTION(test, \
888 kunit_binary_ptr_assert, \
889 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
896 #define KUNIT_BINARY_PTR_GE_ASSERTION(test, assert_type, left, right) \
897 KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
903 #define KUNIT_BINARY_STR_ASSERTION(test, \
911 typeof(left) __left = (left); \
912 typeof(right) __right = (right); \
914 KUNIT_ASSERTION(test, \
915 strcmp(__left, __right) op 0, \
916 kunit_binary_str_assert, \
917 KUNIT_INIT_BINARY_ASSERT_STRUCT(test, \
928 #define KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
934 KUNIT_BINARY_STR_ASSERTION(test, \
940 #define KUNIT_BINARY_STR_EQ_ASSERTION(test, assert_type, left, right) \
941 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
947 #define KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
953 KUNIT_BINARY_STR_ASSERTION(test, \
959 #define KUNIT_BINARY_STR_NE_ASSERTION(test, assert_type, left, right) \
960 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
966 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
972 typeof(ptr) __ptr = (ptr); \
974 KUNIT_ASSERTION(test, \
975 !IS_ERR_OR_NULL(__ptr), \
976 kunit_ptr_not_err_assert, \
977 KUNIT_INIT_PTR_NOT_ERR_STRUCT(test, \
985 #define KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, assert_type, ptr) \
986 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
992 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
993 * @test: The test context object.
994 * @condition: an arbitrary boolean expression. The test fails when this does
995 * not evaluate to true.
997 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
998 * to fail when the specified condition is not met; however, it will not prevent
999 * the test case from continuing to run; this is otherwise known as an
1000 * *expectation failure*.
1002 #define KUNIT_EXPECT_TRUE(test, condition) \
1003 KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)
1005 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
1006 KUNIT_TRUE_MSG_ASSERTION(test, \
1007 KUNIT_EXPECTATION, \
1013 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
1014 * @test: The test context object.
1015 * @condition: an arbitrary boolean expression. The test fails when this does
1016 * not evaluate to false.
1018 * Sets an expectation that @condition evaluates to false. See
1019 * KUNIT_EXPECT_TRUE() for more information.
1021 #define KUNIT_EXPECT_FALSE(test, condition) \
1022 KUNIT_FALSE_ASSERTION(test, KUNIT_EXPECTATION, condition)
1024 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
1025 KUNIT_FALSE_MSG_ASSERTION(test, \
1026 KUNIT_EXPECTATION, \
1032 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
1033 * @test: The test context object.
1034 * @left: an arbitrary expression that evaluates to a primitive C type.
1035 * @right: an arbitrary expression that evaluates to a primitive C type.
1037 * Sets an expectation that the values that @left and @right evaluate to are
1038 * equal. This is semantically equivalent to
1039 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1042 #define KUNIT_EXPECT_EQ(test, left, right) \
1043 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1045 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
1046 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1047 KUNIT_EXPECTATION, \
1054 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
1055 * @test: The test context object.
1056 * @left: an arbitrary expression that evaluates to a pointer.
1057 * @right: an arbitrary expression that evaluates to a pointer.
1059 * Sets an expectation that the values that @left and @right evaluate to are
1060 * equal. This is semantically equivalent to
1061 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1064 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
1065 KUNIT_BINARY_PTR_EQ_ASSERTION(test, \
1066 KUNIT_EXPECTATION, \
1070 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1071 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1072 KUNIT_EXPECTATION, \
1079 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
1080 * @test: The test context object.
1081 * @left: an arbitrary expression that evaluates to a primitive C type.
1082 * @right: an arbitrary expression that evaluates to a primitive C type.
1084 * Sets an expectation that the values that @left and @right evaluate to are not
1085 * equal. This is semantically equivalent to
1086 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1089 #define KUNIT_EXPECT_NE(test, left, right) \
1090 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1092 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
1093 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1094 KUNIT_EXPECTATION, \
1101 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
1102 * @test: The test context object.
1103 * @left: an arbitrary expression that evaluates to a pointer.
1104 * @right: an arbitrary expression that evaluates to a pointer.
1106 * Sets an expectation that the values that @left and @right evaluate to are not
1107 * equal. This is semantically equivalent to
1108 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1111 #define KUNIT_EXPECT_PTR_NE(test, left, right) \
1112 KUNIT_BINARY_PTR_NE_ASSERTION(test, \
1113 KUNIT_EXPECTATION, \
1117 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
1118 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1119 KUNIT_EXPECTATION, \
1126 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
1127 * @test: The test context object.
1128 * @left: an arbitrary expression that evaluates to a primitive C type.
1129 * @right: an arbitrary expression that evaluates to a primitive C type.
1131 * Sets an expectation that the value that @left evaluates to is less than the
1132 * value that @right evaluates to. This is semantically equivalent to
1133 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
1136 #define KUNIT_EXPECT_LT(test, left, right) \
1137 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1139 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
1140 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1141 KUNIT_EXPECTATION, \
1148 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
1149 * @test: The test context object.
1150 * @left: an arbitrary expression that evaluates to a primitive C type.
1151 * @right: an arbitrary expression that evaluates to a primitive C type.
1153 * Sets an expectation that the value that @left evaluates to is less than or
1154 * equal to the value that @right evaluates to. Semantically this is equivalent
1155 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
1158 #define KUNIT_EXPECT_LE(test, left, right) \
1159 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1161 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
1162 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1163 KUNIT_EXPECTATION, \
1170 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
1171 * @test: The test context object.
1172 * @left: an arbitrary expression that evaluates to a primitive C type.
1173 * @right: an arbitrary expression that evaluates to a primitive C type.
1175 * Sets an expectation that the value that @left evaluates to is greater than
1176 * the value that @right evaluates to. This is semantically equivalent to
1177 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
1180 #define KUNIT_EXPECT_GT(test, left, right) \
1181 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1183 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
1184 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1185 KUNIT_EXPECTATION, \
1192 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1193 * @test: The test context object.
1194 * @left: an arbitrary expression that evaluates to a primitive C type.
1195 * @right: an arbitrary expression that evaluates to a primitive C type.
1197 * Sets an expectation that the value that @left evaluates to is greater than
1198 * the value that @right evaluates to. This is semantically equivalent to
1199 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1202 #define KUNIT_EXPECT_GE(test, left, right) \
1203 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1205 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
1206 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1207 KUNIT_EXPECTATION, \
1214 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1215 * @test: The test context object.
1216 * @left: an arbitrary expression that evaluates to a null terminated string.
1217 * @right: an arbitrary expression that evaluates to a null terminated string.
1219 * Sets an expectation that the values that @left and @right evaluate to are
1220 * equal. This is semantically equivalent to
1221 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1222 * for more information.
1224 #define KUNIT_EXPECT_STREQ(test, left, right) \
1225 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1227 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
1228 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1229 KUNIT_EXPECTATION, \
1236 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1237 * @test: The test context object.
1238 * @left: an arbitrary expression that evaluates to a null terminated string.
1239 * @right: an arbitrary expression that evaluates to a null terminated string.
1241 * Sets an expectation that the values that @left and @right evaluate to are
1242 * not equal. This is semantically equivalent to
1243 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1244 * for more information.
1246 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
1247 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1249 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
1250 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1251 KUNIT_EXPECTATION, \
1258 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1259 * @test: The test context object.
1260 * @ptr: an arbitrary pointer.
1262 * Sets an expectation that the value that @ptr evaluates to is not null and not
1263 * an errno stored in a pointer. This is semantically equivalent to
1264 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1267 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1268 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_EXPECTATION, ptr)
1270 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1271 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1272 KUNIT_EXPECTATION, \
1277 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1278 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1281 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1282 * @test: The test context object.
1283 * @condition: an arbitrary boolean expression. The test fails and aborts when
1284 * this does not evaluate to true.
1286 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1287 * fail *and immediately abort* when the specified condition is not met. Unlike
1288 * an expectation failure, it will prevent the test case from continuing to run;
1289 * this is otherwise known as an *assertion failure*.
1291 #define KUNIT_ASSERT_TRUE(test, condition) \
1292 KUNIT_TRUE_ASSERTION(test, KUNIT_ASSERTION, condition)
1294 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1295 KUNIT_TRUE_MSG_ASSERTION(test, \
1302 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1303 * @test: The test context object.
1304 * @condition: an arbitrary boolean expression.
1306 * Sets an assertion that the value that @condition evaluates to is false. This
1307 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1308 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1310 #define KUNIT_ASSERT_FALSE(test, condition) \
1311 KUNIT_FALSE_ASSERTION(test, KUNIT_ASSERTION, condition)
1313 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1314 KUNIT_FALSE_MSG_ASSERTION(test, \
1321 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1322 * @test: The test context object.
1323 * @left: an arbitrary expression that evaluates to a primitive C type.
1324 * @right: an arbitrary expression that evaluates to a primitive C type.
1326 * Sets an assertion that the values that @left and @right evaluate to are
1327 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1328 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1330 #define KUNIT_ASSERT_EQ(test, left, right) \
1331 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1333 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1334 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1342 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1343 * @test: The test context object.
1344 * @left: an arbitrary expression that evaluates to a pointer.
1345 * @right: an arbitrary expression that evaluates to a pointer.
1347 * Sets an assertion that the values that @left and @right evaluate to are
1348 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1349 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1351 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1352 KUNIT_BINARY_PTR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1354 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1355 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1363 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1364 * @test: The test context object.
1365 * @left: an arbitrary expression that evaluates to a primitive C type.
1366 * @right: an arbitrary expression that evaluates to a primitive C type.
1368 * Sets an assertion that the values that @left and @right evaluate to are not
1369 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1370 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1372 #define KUNIT_ASSERT_NE(test, left, right) \
1373 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1375 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1376 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1384 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1385 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1386 * @test: The test context object.
1387 * @left: an arbitrary expression that evaluates to a pointer.
1388 * @right: an arbitrary expression that evaluates to a pointer.
1390 * Sets an assertion that the values that @left and @right evaluate to are not
1391 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1392 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1394 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1395 KUNIT_BINARY_PTR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1397 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1398 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1405 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1406 * @test: The test context object.
1407 * @left: an arbitrary expression that evaluates to a primitive C type.
1408 * @right: an arbitrary expression that evaluates to a primitive C type.
1410 * Sets an assertion that the value that @left evaluates to is less than the
1411 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1412 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1415 #define KUNIT_ASSERT_LT(test, left, right) \
1416 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1418 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1419 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1426 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1427 * @test: The test context object.
1428 * @left: an arbitrary expression that evaluates to a primitive C type.
1429 * @right: an arbitrary expression that evaluates to a primitive C type.
1431 * Sets an assertion that the value that @left evaluates to is less than or
1432 * equal to the value that @right evaluates to. This is the same as
1433 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1434 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1436 #define KUNIT_ASSERT_LE(test, left, right) \
1437 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1439 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1440 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1448 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1449 * @test: The test context object.
1450 * @left: an arbitrary expression that evaluates to a primitive C type.
1451 * @right: an arbitrary expression that evaluates to a primitive C type.
1453 * Sets an assertion that the value that @left evaluates to is greater than the
1454 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1455 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1458 #define KUNIT_ASSERT_GT(test, left, right) \
1459 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1461 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1462 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1470 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1471 * @test: The test context object.
1472 * @left: an arbitrary expression that evaluates to a primitive C type.
1473 * @right: an arbitrary expression that evaluates to a primitive C type.
1475 * Sets an assertion that the value that @left evaluates to is greater than the
1476 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1477 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1480 #define KUNIT_ASSERT_GE(test, left, right) \
1481 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1483 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1484 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1492 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1493 * @test: The test context object.
1494 * @left: an arbitrary expression that evaluates to a null terminated string.
1495 * @right: an arbitrary expression that evaluates to a null terminated string.
1497 * Sets an assertion that the values that @left and @right evaluate to are
1498 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1499 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1501 #define KUNIT_ASSERT_STREQ(test, left, right) \
1502 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1504 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1505 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1513 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1514 * @test: The test context object.
1515 * @left: an arbitrary expression that evaluates to a null terminated string.
1516 * @right: an arbitrary expression that evaluates to a null terminated string.
1518 * Sets an expectation that the values that @left and @right evaluate to are
1519 * not equal. This is semantically equivalent to
1520 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1521 * for more information.
1523 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1524 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1526 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1527 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1535 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1536 * @test: The test context object.
1537 * @ptr: an arbitrary pointer.
1539 * Sets an assertion that the value that @ptr evaluates to is not null and not
1540 * an errno stored in a pointer. This is the same as
1541 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1542 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1544 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1545 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_ASSERTION, ptr)
1547 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1548 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1554 #endif /* _KUNIT_TEST_H */