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/slab.h>
16 #include <linux/types.h>
18 struct kunit_resource;
20 typedef int (*kunit_resource_init_t)(struct kunit_resource *, void *);
21 typedef void (*kunit_resource_free_t)(struct kunit_resource *);
24 * struct kunit_resource - represents a *test managed resource*
25 * @allocation: for the user to store arbitrary data.
26 * @free: a user supplied function to free the resource. Populated by
27 * kunit_alloc_resource().
29 * Represents a *test managed resource*, a resource which will automatically be
30 * cleaned up at the end of a test case.
36 * struct kunit_kmalloc_params {
41 * static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
43 * struct kunit_kmalloc_params *params = context;
44 * res->allocation = kmalloc(params->size, params->gfp);
46 * if (!res->allocation)
52 * static void kunit_kmalloc_free(struct kunit_resource *res)
54 * kfree(res->allocation);
57 * void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
59 * struct kunit_kmalloc_params params;
60 * struct kunit_resource *res;
65 * res = kunit_alloc_resource(test, kunit_kmalloc_init,
66 * kunit_kmalloc_free, ¶ms);
68 * return res->allocation;
73 struct kunit_resource {
75 kunit_resource_free_t free;
77 /* private: internal use only. */
78 struct list_head node;
84 * struct kunit_case - represents an individual test case.
86 * @run_case: the function representing the actual test case.
87 * @name: the name of the test case.
89 * A test case is a function with the signature,
90 * ``void (*)(struct kunit *)``
91 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
92 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
93 * with a &struct kunit_suite and will be run after the suite's init
94 * function and followed by the suite's exit function.
96 * A test case should be static and should only be created with the
97 * KUNIT_CASE() macro; additionally, every array of test cases should be
98 * terminated with an empty test case.
104 * void add_test_basic(struct kunit *test)
106 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
107 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
108 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
109 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
110 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
113 * static struct kunit_case example_test_cases[] = {
114 * KUNIT_CASE(add_test_basic),
120 void (*run_case)(struct kunit *test);
123 /* private: internal use only. */
128 * KUNIT_CASE - A helper for creating a &struct kunit_case
130 * @test_name: a reference to a test case function.
132 * Takes a symbol for a function representing a test case and creates a
133 * &struct kunit_case object from it. See the documentation for
134 * &struct kunit_case for an example on how to use it.
136 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
139 * struct kunit_suite - describes a related collection of &struct kunit_case
141 * @name: the name of the test. Purely informational.
142 * @init: called before every test case.
143 * @exit: called after every test case.
144 * @test_cases: a null terminated array of test cases.
146 * A kunit_suite is a collection of related &struct kunit_case s, such that
147 * @init is called before every test case and @exit is called after every
148 * test case, similar to the notion of a *test fixture* or a *test class*
149 * in other unit testing frameworks like JUnit or Googletest.
151 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
155 const char name[256];
156 int (*init)(struct kunit *test);
157 void (*exit)(struct kunit *test);
158 struct kunit_case *test_cases;
162 * struct kunit - represents a running instance of a test.
164 * @priv: for user to store arbitrary data. Commonly used to pass data
165 * created in the init function (see &struct kunit_suite).
167 * Used to store information about the current context under which the test
168 * is running. Most of this data is private and should only be accessed
169 * indirectly via public functions; the one exception is @priv which can be
170 * used by the test writer to store arbitrary data.
175 /* private: internal use only. */
176 const char *name; /* Read only after initialization! */
177 struct kunit_try_catch try_catch;
179 * success starts as true, and may only be set to false during a
180 * test case; thus, it is safe to update this across multiple
181 * threads using WRITE_ONCE; however, as a consequence, it may only
182 * be read after the test case finishes once all threads associated
183 * with the test case have terminated.
185 bool success; /* Read only after test_case finishes! */
186 spinlock_t lock; /* Guards all mutable test state. */
188 * Because resources is a list that may be updated multiple times (with
189 * new resources) from any thread associated with a test case, we must
190 * protect it with some type of lock.
192 struct list_head resources; /* Protected by lock. */
195 void kunit_init_test(struct kunit *test, const char *name);
197 int kunit_run_tests(struct kunit_suite *suite);
200 * kunit_test_suite() - used to register a &struct kunit_suite with KUnit.
202 * @suite: a statically allocated &struct kunit_suite.
204 * Registers @suite with the test framework. See &struct kunit_suite for
207 * NOTE: Currently KUnit tests are all run as late_initcalls; this means
208 * that they cannot test anything where tests must run at a different init
209 * phase. One significant restriction resulting from this is that KUnit
210 * cannot reliably test anything that is initialize in the late_init phase;
211 * another is that KUnit is useless to test things that need to be run in
212 * an earlier init phase.
214 * TODO(brendanhiggins@google.com): Don't run all KUnit tests as
215 * late_initcalls. I have some future work planned to dispatch all KUnit
216 * tests from the same place, and at the very least to do so after
217 * everything else is definitely initialized.
219 #define kunit_test_suite(suite) \
220 static int kunit_suite_init##suite(void) \
222 return kunit_run_tests(&suite); \
224 late_initcall(kunit_suite_init##suite)
227 * Like kunit_alloc_resource() below, but returns the struct kunit_resource
228 * object that contains the allocation. This is mostly for testing purposes.
230 struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
231 kunit_resource_init_t init,
232 kunit_resource_free_t free,
237 * kunit_alloc_resource() - Allocates a *test managed resource*.
238 * @test: The test context object.
239 * @init: a user supplied function to initialize the resource.
240 * @free: a user supplied function to free the resource.
241 * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
242 * @context: for the user to pass in arbitrary data to the init function.
244 * Allocates a *test managed resource*, a resource which will automatically be
245 * cleaned up at the end of a test case. See &struct kunit_resource for an
248 * NOTE: KUnit needs to allocate memory for each kunit_resource object. You must
249 * specify an @internal_gfp that is compatible with the use context of your
252 static inline void *kunit_alloc_resource(struct kunit *test,
253 kunit_resource_init_t init,
254 kunit_resource_free_t free,
258 struct kunit_resource *res;
260 res = kunit_alloc_and_get_resource(test, init, free, internal_gfp,
264 return res->allocation;
269 typedef bool (*kunit_resource_match_t)(struct kunit *test,
274 * kunit_resource_instance_match() - Match a resource with the same instance.
275 * @test: Test case to which the resource belongs.
276 * @res: The data stored in kunit_resource->allocation.
277 * @match_data: The resource pointer to match against.
279 * An instance of kunit_resource_match_t that matches a resource whose
280 * allocation matches @match_data.
282 static inline bool kunit_resource_instance_match(struct kunit *test,
286 return res == match_data;
290 * kunit_resource_destroy() - Find a kunit_resource and destroy it.
291 * @test: Test case to which the resource belongs.
292 * @match: Match function. Returns whether a given resource matches @match_data.
293 * @free: Must match free on the kunit_resource to free.
294 * @match_data: Data passed into @match.
296 * Free the latest kunit_resource of @test for which @free matches the
297 * kunit_resource_free_t associated with the resource and for which @match
301 * 0 if kunit_resource is found and freed, -ENOENT if not found.
303 int kunit_resource_destroy(struct kunit *test,
304 kunit_resource_match_t match,
305 kunit_resource_free_t free,
309 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
310 * @test: The test context object.
311 * @size: The size in bytes of the desired memory.
312 * @gfp: flags passed to underlying kmalloc().
314 * Just like `kmalloc(...)`, except the allocation is managed by the test case
315 * and is automatically cleaned up after the test case concludes. See &struct
316 * kunit_resource for more information.
318 void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp);
321 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
322 * @test: The test case to which the resource belongs.
323 * @ptr: The memory allocation to free.
325 void kunit_kfree(struct kunit *test, const void *ptr);
328 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
329 * @test: The test context object.
330 * @size: The size in bytes of the desired memory.
331 * @gfp: flags passed to underlying kmalloc().
333 * See kzalloc() and kunit_kmalloc() for more information.
335 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
337 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
340 void kunit_cleanup(struct kunit *test);
342 #define kunit_printk(lvl, test, fmt, ...) \
343 printk(lvl "\t# %s: " fmt, (test)->name, ##__VA_ARGS__)
346 * kunit_info() - Prints an INFO level message associated with @test.
348 * @test: The test context object.
349 * @fmt: A printk() style format string.
351 * Prints an info level message associated with the test suite being run.
352 * Takes a variable number of format parameters just like printk().
354 #define kunit_info(test, fmt, ...) \
355 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
358 * kunit_warn() - Prints a WARN level message associated with @test.
360 * @test: The test context object.
361 * @fmt: A printk() style format string.
363 * Prints a warning level message.
365 #define kunit_warn(test, fmt, ...) \
366 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
369 * kunit_err() - Prints an ERROR level message associated with @test.
371 * @test: The test context object.
372 * @fmt: A printk() style format string.
374 * Prints an error level message.
376 #define kunit_err(test, fmt, ...) \
377 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
380 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
381 * @test: The test context object.
383 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
384 * words, it does nothing and only exists for code clarity. See
385 * KUNIT_EXPECT_TRUE() for more information.
387 #define KUNIT_SUCCEED(test) do {} while (0)
389 void kunit_do_assertion(struct kunit *test,
390 struct kunit_assert *assert,
392 const char *fmt, ...);
394 #define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do { \
395 struct assert_class __assertion = INITIALIZER; \
396 kunit_do_assertion(test, \
397 &__assertion.assert, \
404 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
405 KUNIT_ASSERTION(test, \
408 KUNIT_INIT_FAIL_ASSERT_STRUCT(test, assert_type), \
413 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
414 * @test: The test context object.
415 * @fmt: an informational message to be printed when the assertion is made.
416 * @...: string format arguments.
418 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
419 * other words, it always results in a failed expectation, and consequently
420 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
421 * for more information.
423 #define KUNIT_FAIL(test, fmt, ...) \
424 KUNIT_FAIL_ASSERTION(test, \
429 #define KUNIT_UNARY_ASSERTION(test, \
435 KUNIT_ASSERTION(test, \
436 !!(condition) == !!expected_true, \
437 kunit_unary_assert, \
438 KUNIT_INIT_UNARY_ASSERT_STRUCT(test, \
445 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
446 KUNIT_UNARY_ASSERTION(test, \
453 #define KUNIT_TRUE_ASSERTION(test, assert_type, condition) \
454 KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, NULL)
456 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
457 KUNIT_UNARY_ASSERTION(test, \
464 #define KUNIT_FALSE_ASSERTION(test, assert_type, condition) \
465 KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, NULL)
468 * A factory macro for defining the assertions and expectations for the basic
469 * comparisons defined for the built in types.
471 * Unfortunately, there is no common type that all types can be promoted to for
472 * which all the binary operators behave the same way as for the actual types
473 * (for example, there is no type that long long and unsigned long long can
474 * both be cast to where the comparison result is preserved for all values). So
475 * the best we can do is do the comparison in the original types and then coerce
476 * everything to long long for printing; this way, the comparison behaves
477 * correctly and the printed out value usually makes sense without
478 * interpretation, but can always be interpreted to figure out the actual
481 #define KUNIT_BASE_BINARY_ASSERTION(test, \
491 typeof(left) __left = (left); \
492 typeof(right) __right = (right); \
493 ((void)__typecheck(__left, __right)); \
495 KUNIT_ASSERTION(test, \
498 ASSERT_CLASS_INIT(test, \
509 #define KUNIT_BASE_EQ_MSG_ASSERTION(test, \
517 KUNIT_BASE_BINARY_ASSERTION(test, \
525 #define KUNIT_BASE_NE_MSG_ASSERTION(test, \
533 KUNIT_BASE_BINARY_ASSERTION(test, \
541 #define KUNIT_BASE_LT_MSG_ASSERTION(test, \
549 KUNIT_BASE_BINARY_ASSERTION(test, \
557 #define KUNIT_BASE_LE_MSG_ASSERTION(test, \
565 KUNIT_BASE_BINARY_ASSERTION(test, \
573 #define KUNIT_BASE_GT_MSG_ASSERTION(test, \
581 KUNIT_BASE_BINARY_ASSERTION(test, \
589 #define KUNIT_BASE_GE_MSG_ASSERTION(test, \
597 KUNIT_BASE_BINARY_ASSERTION(test, \
605 #define KUNIT_BINARY_EQ_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
606 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
607 kunit_binary_assert, \
608 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
615 #define KUNIT_BINARY_EQ_ASSERTION(test, assert_type, left, right) \
616 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
622 #define KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
628 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
629 kunit_binary_ptr_assert, \
630 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
637 #define KUNIT_BINARY_PTR_EQ_ASSERTION(test, assert_type, left, right) \
638 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
644 #define KUNIT_BINARY_NE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
645 KUNIT_BASE_NE_MSG_ASSERTION(test, \
646 kunit_binary_assert, \
647 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
654 #define KUNIT_BINARY_NE_ASSERTION(test, assert_type, left, right) \
655 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
661 #define KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
667 KUNIT_BASE_NE_MSG_ASSERTION(test, \
668 kunit_binary_ptr_assert, \
669 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
676 #define KUNIT_BINARY_PTR_NE_ASSERTION(test, assert_type, left, right) \
677 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
683 #define KUNIT_BINARY_LT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
684 KUNIT_BASE_LT_MSG_ASSERTION(test, \
685 kunit_binary_assert, \
686 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
693 #define KUNIT_BINARY_LT_ASSERTION(test, assert_type, left, right) \
694 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
700 #define KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
706 KUNIT_BASE_LT_MSG_ASSERTION(test, \
707 kunit_binary_ptr_assert, \
708 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
715 #define KUNIT_BINARY_PTR_LT_ASSERTION(test, assert_type, left, right) \
716 KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
722 #define KUNIT_BINARY_LE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
723 KUNIT_BASE_LE_MSG_ASSERTION(test, \
724 kunit_binary_assert, \
725 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
732 #define KUNIT_BINARY_LE_ASSERTION(test, assert_type, left, right) \
733 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
739 #define KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
745 KUNIT_BASE_LE_MSG_ASSERTION(test, \
746 kunit_binary_ptr_assert, \
747 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
754 #define KUNIT_BINARY_PTR_LE_ASSERTION(test, assert_type, left, right) \
755 KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
761 #define KUNIT_BINARY_GT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
762 KUNIT_BASE_GT_MSG_ASSERTION(test, \
763 kunit_binary_assert, \
764 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
771 #define KUNIT_BINARY_GT_ASSERTION(test, assert_type, left, right) \
772 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
778 #define KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
784 KUNIT_BASE_GT_MSG_ASSERTION(test, \
785 kunit_binary_ptr_assert, \
786 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
793 #define KUNIT_BINARY_PTR_GT_ASSERTION(test, assert_type, left, right) \
794 KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
800 #define KUNIT_BINARY_GE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
801 KUNIT_BASE_GE_MSG_ASSERTION(test, \
802 kunit_binary_assert, \
803 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
810 #define KUNIT_BINARY_GE_ASSERTION(test, assert_type, left, right) \
811 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
817 #define KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
823 KUNIT_BASE_GE_MSG_ASSERTION(test, \
824 kunit_binary_ptr_assert, \
825 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
832 #define KUNIT_BINARY_PTR_GE_ASSERTION(test, assert_type, left, right) \
833 KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
839 #define KUNIT_BINARY_STR_ASSERTION(test, \
847 typeof(left) __left = (left); \
848 typeof(right) __right = (right); \
850 KUNIT_ASSERTION(test, \
851 strcmp(__left, __right) op 0, \
852 kunit_binary_str_assert, \
853 KUNIT_INIT_BINARY_ASSERT_STRUCT(test, \
864 #define KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
870 KUNIT_BINARY_STR_ASSERTION(test, \
876 #define KUNIT_BINARY_STR_EQ_ASSERTION(test, assert_type, left, right) \
877 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
883 #define KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
889 KUNIT_BINARY_STR_ASSERTION(test, \
895 #define KUNIT_BINARY_STR_NE_ASSERTION(test, assert_type, left, right) \
896 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
902 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
908 typeof(ptr) __ptr = (ptr); \
910 KUNIT_ASSERTION(test, \
911 !IS_ERR_OR_NULL(__ptr), \
912 kunit_ptr_not_err_assert, \
913 KUNIT_INIT_PTR_NOT_ERR_STRUCT(test, \
921 #define KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, assert_type, ptr) \
922 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
928 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
929 * @test: The test context object.
930 * @condition: an arbitrary boolean expression. The test fails when this does
931 * not evaluate to true.
933 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
934 * to fail when the specified condition is not met; however, it will not prevent
935 * the test case from continuing to run; this is otherwise known as an
936 * *expectation failure*.
938 #define KUNIT_EXPECT_TRUE(test, condition) \
939 KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)
941 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
942 KUNIT_TRUE_MSG_ASSERTION(test, \
949 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
950 * @test: The test context object.
951 * @condition: an arbitrary boolean expression. The test fails when this does
952 * not evaluate to false.
954 * Sets an expectation that @condition evaluates to false. See
955 * KUNIT_EXPECT_TRUE() for more information.
957 #define KUNIT_EXPECT_FALSE(test, condition) \
958 KUNIT_FALSE_ASSERTION(test, KUNIT_EXPECTATION, condition)
960 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
961 KUNIT_FALSE_MSG_ASSERTION(test, \
968 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
969 * @test: The test context object.
970 * @left: an arbitrary expression that evaluates to a primitive C type.
971 * @right: an arbitrary expression that evaluates to a primitive C type.
973 * Sets an expectation that the values that @left and @right evaluate to are
974 * equal. This is semantically equivalent to
975 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
978 #define KUNIT_EXPECT_EQ(test, left, right) \
979 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
981 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
982 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
990 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
991 * @test: The test context object.
992 * @left: an arbitrary expression that evaluates to a pointer.
993 * @right: an arbitrary expression that evaluates to a pointer.
995 * Sets an expectation that the values that @left and @right evaluate to are
996 * equal. This is semantically equivalent to
997 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1000 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
1001 KUNIT_BINARY_PTR_EQ_ASSERTION(test, \
1002 KUNIT_EXPECTATION, \
1006 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1007 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1008 KUNIT_EXPECTATION, \
1015 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
1016 * @test: The test context object.
1017 * @left: an arbitrary expression that evaluates to a primitive C type.
1018 * @right: an arbitrary expression that evaluates to a primitive C type.
1020 * Sets an expectation that the values that @left and @right evaluate to are not
1021 * equal. This is semantically equivalent to
1022 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1025 #define KUNIT_EXPECT_NE(test, left, right) \
1026 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1028 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
1029 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1030 KUNIT_EXPECTATION, \
1037 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
1038 * @test: The test context object.
1039 * @left: an arbitrary expression that evaluates to a pointer.
1040 * @right: an arbitrary expression that evaluates to a pointer.
1042 * Sets an expectation that the values that @left and @right evaluate to are not
1043 * equal. This is semantically equivalent to
1044 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1047 #define KUNIT_EXPECT_PTR_NE(test, left, right) \
1048 KUNIT_BINARY_PTR_NE_ASSERTION(test, \
1049 KUNIT_EXPECTATION, \
1053 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
1054 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1055 KUNIT_EXPECTATION, \
1062 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
1063 * @test: The test context object.
1064 * @left: an arbitrary expression that evaluates to a primitive C type.
1065 * @right: an arbitrary expression that evaluates to a primitive C type.
1067 * Sets an expectation that the value that @left evaluates to is less than the
1068 * value that @right evaluates to. This is semantically equivalent to
1069 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
1072 #define KUNIT_EXPECT_LT(test, left, right) \
1073 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1075 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
1076 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1077 KUNIT_EXPECTATION, \
1084 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
1085 * @test: The test context object.
1086 * @left: an arbitrary expression that evaluates to a primitive C type.
1087 * @right: an arbitrary expression that evaluates to a primitive C type.
1089 * Sets an expectation that the value that @left evaluates to is less than or
1090 * equal to the value that @right evaluates to. Semantically this is equivalent
1091 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
1094 #define KUNIT_EXPECT_LE(test, left, right) \
1095 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1097 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
1098 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1099 KUNIT_EXPECTATION, \
1106 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
1107 * @test: The test context object.
1108 * @left: an arbitrary expression that evaluates to a primitive C type.
1109 * @right: an arbitrary expression that evaluates to a primitive C type.
1111 * Sets an expectation that the value that @left evaluates to is greater than
1112 * the value that @right evaluates to. This is semantically equivalent to
1113 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
1116 #define KUNIT_EXPECT_GT(test, left, right) \
1117 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1119 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
1120 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1121 KUNIT_EXPECTATION, \
1128 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1129 * @test: The test context object.
1130 * @left: an arbitrary expression that evaluates to a primitive C type.
1131 * @right: an arbitrary expression that evaluates to a primitive C type.
1133 * Sets an expectation that the value that @left evaluates to is greater than
1134 * the value that @right evaluates to. This is semantically equivalent to
1135 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1138 #define KUNIT_EXPECT_GE(test, left, right) \
1139 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1141 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
1142 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1143 KUNIT_EXPECTATION, \
1150 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1151 * @test: The test context object.
1152 * @left: an arbitrary expression that evaluates to a null terminated string.
1153 * @right: an arbitrary expression that evaluates to a null terminated string.
1155 * Sets an expectation that the values that @left and @right evaluate to are
1156 * equal. This is semantically equivalent to
1157 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1158 * for more information.
1160 #define KUNIT_EXPECT_STREQ(test, left, right) \
1161 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1163 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
1164 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1165 KUNIT_EXPECTATION, \
1172 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1173 * @test: The test context object.
1174 * @left: an arbitrary expression that evaluates to a null terminated string.
1175 * @right: an arbitrary expression that evaluates to a null terminated string.
1177 * Sets an expectation that the values that @left and @right evaluate to are
1178 * not equal. This is semantically equivalent to
1179 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1180 * for more information.
1182 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
1183 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1185 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
1186 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1187 KUNIT_EXPECTATION, \
1194 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1195 * @test: The test context object.
1196 * @ptr: an arbitrary pointer.
1198 * Sets an expectation that the value that @ptr evaluates to is not null and not
1199 * an errno stored in a pointer. This is semantically equivalent to
1200 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1203 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1204 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_EXPECTATION, ptr)
1206 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1207 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1208 KUNIT_EXPECTATION, \
1213 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1214 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1217 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1218 * @test: The test context object.
1219 * @condition: an arbitrary boolean expression. The test fails and aborts when
1220 * this does not evaluate to true.
1222 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1223 * fail *and immediately abort* when the specified condition is not met. Unlike
1224 * an expectation failure, it will prevent the test case from continuing to run;
1225 * this is otherwise known as an *assertion failure*.
1227 #define KUNIT_ASSERT_TRUE(test, condition) \
1228 KUNIT_TRUE_ASSERTION(test, KUNIT_ASSERTION, condition)
1230 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1231 KUNIT_TRUE_MSG_ASSERTION(test, \
1238 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1239 * @test: The test context object.
1240 * @condition: an arbitrary boolean expression.
1242 * Sets an assertion that the value that @condition evaluates to is false. This
1243 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1244 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1246 #define KUNIT_ASSERT_FALSE(test, condition) \
1247 KUNIT_FALSE_ASSERTION(test, KUNIT_ASSERTION, condition)
1249 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1250 KUNIT_FALSE_MSG_ASSERTION(test, \
1257 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1258 * @test: The test context object.
1259 * @left: an arbitrary expression that evaluates to a primitive C type.
1260 * @right: an arbitrary expression that evaluates to a primitive C type.
1262 * Sets an assertion that the values that @left and @right evaluate to are
1263 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1264 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1266 #define KUNIT_ASSERT_EQ(test, left, right) \
1267 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1269 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1270 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1278 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1279 * @test: The test context object.
1280 * @left: an arbitrary expression that evaluates to a pointer.
1281 * @right: an arbitrary expression that evaluates to a pointer.
1283 * Sets an assertion that the values that @left and @right evaluate to are
1284 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1285 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1287 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1288 KUNIT_BINARY_PTR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1290 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1291 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1299 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1300 * @test: The test context object.
1301 * @left: an arbitrary expression that evaluates to a primitive C type.
1302 * @right: an arbitrary expression that evaluates to a primitive C type.
1304 * Sets an assertion that the values that @left and @right evaluate to are not
1305 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1306 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1308 #define KUNIT_ASSERT_NE(test, left, right) \
1309 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1311 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1312 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1320 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1321 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1322 * @test: The test context object.
1323 * @left: an arbitrary expression that evaluates to a pointer.
1324 * @right: an arbitrary expression that evaluates to a pointer.
1326 * Sets an assertion that the values that @left and @right evaluate to are not
1327 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1328 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1330 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1331 KUNIT_BINARY_PTR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1333 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1334 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1341 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1342 * @test: The test context object.
1343 * @left: an arbitrary expression that evaluates to a primitive C type.
1344 * @right: an arbitrary expression that evaluates to a primitive C type.
1346 * Sets an assertion that the value that @left evaluates to is less than the
1347 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1348 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1351 #define KUNIT_ASSERT_LT(test, left, right) \
1352 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1354 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1355 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1362 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1363 * @test: The test context object.
1364 * @left: an arbitrary expression that evaluates to a primitive C type.
1365 * @right: an arbitrary expression that evaluates to a primitive C type.
1367 * Sets an assertion that the value that @left evaluates to is less than or
1368 * equal to the value that @right evaluates to. This is the same as
1369 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1370 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1372 #define KUNIT_ASSERT_LE(test, left, right) \
1373 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1375 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1376 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1384 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1385 * @test: The test context object.
1386 * @left: an arbitrary expression that evaluates to a primitive C type.
1387 * @right: an arbitrary expression that evaluates to a primitive C type.
1389 * Sets an assertion that the value that @left evaluates to is greater than the
1390 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1391 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1394 #define KUNIT_ASSERT_GT(test, left, right) \
1395 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1397 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1398 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1406 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1407 * @test: The test context object.
1408 * @left: an arbitrary expression that evaluates to a primitive C type.
1409 * @right: an arbitrary expression that evaluates to a primitive C type.
1411 * Sets an assertion that the value that @left evaluates to is greater than the
1412 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1413 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1416 #define KUNIT_ASSERT_GE(test, left, right) \
1417 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1419 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1420 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1428 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1429 * @test: The test context object.
1430 * @left: an arbitrary expression that evaluates to a null terminated string.
1431 * @right: an arbitrary expression that evaluates to a null terminated string.
1433 * Sets an assertion that the values that @left and @right evaluate to are
1434 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1435 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1437 #define KUNIT_ASSERT_STREQ(test, left, right) \
1438 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1440 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1441 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1449 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1450 * @test: The test context object.
1451 * @left: an arbitrary expression that evaluates to a null terminated string.
1452 * @right: an arbitrary expression that evaluates to a null terminated string.
1454 * Sets an expectation that the values that @left and @right evaluate to are
1455 * not equal. This is semantically equivalent to
1456 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1457 * for more information.
1459 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1460 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1462 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1463 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1471 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1472 * @test: The test context object.
1473 * @ptr: an arbitrary pointer.
1475 * Sets an assertion that the value that @ptr evaluates to is not null and not
1476 * an errno stored in a pointer. This is the same as
1477 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1478 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1480 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1481 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_ASSERTION, ptr)
1483 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1484 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1490 #endif /* _KUNIT_TEST_H */