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
;
85 * struct kunit_case - represents an individual test case.
87 * @run_case: the function representing the actual test case.
88 * @name: the name of the test case.
90 * A test case is a function with the signature,
91 * ``void (*)(struct kunit *)``
92 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
93 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
94 * with a &struct kunit_suite and will be run after the suite's init
95 * function and followed by the suite's exit function.
97 * A test case should be static and should only be created with the
98 * KUNIT_CASE() macro; additionally, every array of test cases should be
99 * terminated with an empty test case.
105 * void add_test_basic(struct kunit *test)
107 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
108 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
109 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
110 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
111 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
114 * static struct kunit_case example_test_cases[] = {
115 * KUNIT_CASE(add_test_basic),
121 void (*run_case
)(struct kunit
*test
);
124 /* private: internal use only. */
129 * KUNIT_CASE - A helper for creating a &struct kunit_case
131 * @test_name: a reference to a test case function.
133 * Takes a symbol for a function representing a test case and creates a
134 * &struct kunit_case object from it. See the documentation for
135 * &struct kunit_case for an example on how to use it.
137 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
140 * struct kunit_suite - describes a related collection of &struct kunit_case
142 * @name: the name of the test. Purely informational.
143 * @init: called before every test case.
144 * @exit: called after every test case.
145 * @test_cases: a null terminated array of test cases.
147 * A kunit_suite is a collection of related &struct kunit_case s, such that
148 * @init is called before every test case and @exit is called after every
149 * test case, similar to the notion of a *test fixture* or a *test class*
150 * in other unit testing frameworks like JUnit or Googletest.
152 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
156 const char name
[256];
157 int (*init
)(struct kunit
*test
);
158 void (*exit
)(struct kunit
*test
);
159 struct kunit_case
*test_cases
;
163 * struct kunit - represents a running instance of a test.
165 * @priv: for user to store arbitrary data. Commonly used to pass data
166 * created in the init function (see &struct kunit_suite).
168 * Used to store information about the current context under which the test
169 * is running. Most of this data is private and should only be accessed
170 * indirectly via public functions; the one exception is @priv which can be
171 * used by the test writer to store arbitrary data.
176 /* private: internal use only. */
177 const char *name
; /* Read only after initialization! */
178 struct kunit_try_catch try_catch
;
180 * success starts as true, and may only be set to false during a
181 * test case; thus, it is safe to update this across multiple
182 * threads using WRITE_ONCE; however, as a consequence, it may only
183 * be read after the test case finishes once all threads associated
184 * with the test case have terminated.
186 bool success
; /* Read only after test_case finishes! */
187 spinlock_t lock
; /* Guards all mutable test state. */
189 * Because resources is a list that may be updated multiple times (with
190 * new resources) from any thread associated with a test case, we must
191 * protect it with some type of lock.
193 struct list_head resources
; /* Protected by lock. */
196 void kunit_init_test(struct kunit
*test
, const char *name
);
198 int kunit_run_tests(struct kunit_suite
*suite
);
201 * kunit_test_suites() - used to register one or more &struct kunit_suite
204 * @suites: a statically allocated list of &struct kunit_suite.
206 * Registers @suites with the test framework. See &struct kunit_suite for
209 * When builtin, KUnit tests are all run as late_initcalls; this means
210 * that they cannot test anything where tests must run at a different init
211 * phase. One significant restriction resulting from this is that KUnit
212 * cannot reliably test anything that is initialize in the late_init phase;
213 * another is that KUnit is useless to test things that need to be run in
214 * an earlier init phase.
216 * An alternative is to build the tests as a module. Because modules
217 * do not support multiple late_initcall()s, we need to initialize an
218 * array of suites for a module.
220 * TODO(brendanhiggins@google.com): Don't run all KUnit tests as
221 * late_initcalls. I have some future work planned to dispatch all KUnit
222 * tests from the same place, and at the very least to do so after
223 * everything else is definitely initialized.
225 #define kunit_test_suites(...) \
226 static struct kunit_suite *suites[] = { __VA_ARGS__, NULL}; \
227 static int kunit_test_suites_init(void) \
230 for (i = 0; suites[i] != NULL; i++) \
231 kunit_run_tests(suites[i]); \
234 late_initcall(kunit_test_suites_init); \
235 static void __exit kunit_test_suites_exit(void) \
239 module_exit(kunit_test_suites_exit)
241 #define kunit_test_suite(suite) kunit_test_suites(&suite)
244 * Like kunit_alloc_resource() below, but returns the struct kunit_resource
245 * object that contains the allocation. This is mostly for testing purposes.
247 struct kunit_resource
*kunit_alloc_and_get_resource(struct kunit
*test
,
248 kunit_resource_init_t init
,
249 kunit_resource_free_t free
,
254 * kunit_alloc_resource() - Allocates a *test managed resource*.
255 * @test: The test context object.
256 * @init: a user supplied function to initialize the resource.
257 * @free: a user supplied function to free the resource.
258 * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
259 * @context: for the user to pass in arbitrary data to the init function.
261 * Allocates a *test managed resource*, a resource which will automatically be
262 * cleaned up at the end of a test case. See &struct kunit_resource for an
265 * NOTE: KUnit needs to allocate memory for each kunit_resource object. You must
266 * specify an @internal_gfp that is compatible with the use context of your
269 static inline void *kunit_alloc_resource(struct kunit
*test
,
270 kunit_resource_init_t init
,
271 kunit_resource_free_t free
,
275 struct kunit_resource
*res
;
277 res
= kunit_alloc_and_get_resource(test
, init
, free
, internal_gfp
,
281 return res
->allocation
;
286 typedef bool (*kunit_resource_match_t
)(struct kunit
*test
,
291 * kunit_resource_instance_match() - Match a resource with the same instance.
292 * @test: Test case to which the resource belongs.
293 * @res: The data stored in kunit_resource->allocation.
294 * @match_data: The resource pointer to match against.
296 * An instance of kunit_resource_match_t that matches a resource whose
297 * allocation matches @match_data.
299 static inline bool kunit_resource_instance_match(struct kunit
*test
,
303 return res
== match_data
;
307 * kunit_resource_destroy() - Find a kunit_resource and destroy it.
308 * @test: Test case to which the resource belongs.
309 * @match: Match function. Returns whether a given resource matches @match_data.
310 * @free: Must match free on the kunit_resource to free.
311 * @match_data: Data passed into @match.
313 * Free the latest kunit_resource of @test for which @free matches the
314 * kunit_resource_free_t associated with the resource and for which @match
318 * 0 if kunit_resource is found and freed, -ENOENT if not found.
320 int kunit_resource_destroy(struct kunit
*test
,
321 kunit_resource_match_t match
,
322 kunit_resource_free_t free
,
326 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
327 * @test: The test context object.
328 * @size: The size in bytes of the desired memory.
329 * @gfp: flags passed to underlying kmalloc().
331 * Just like `kmalloc(...)`, except the allocation is managed by the test case
332 * and is automatically cleaned up after the test case concludes. See &struct
333 * kunit_resource for more information.
335 void *kunit_kmalloc(struct kunit
*test
, size_t size
, gfp_t gfp
);
338 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
339 * @test: The test case to which the resource belongs.
340 * @ptr: The memory allocation to free.
342 void kunit_kfree(struct kunit
*test
, const void *ptr
);
345 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
346 * @test: The test context object.
347 * @size: The size in bytes of the desired memory.
348 * @gfp: flags passed to underlying kmalloc().
350 * See kzalloc() and kunit_kmalloc() for more information.
352 static inline void *kunit_kzalloc(struct kunit
*test
, size_t size
, gfp_t gfp
)
354 return kunit_kmalloc(test
, size
, gfp
| __GFP_ZERO
);
357 void kunit_cleanup(struct kunit
*test
);
359 #define kunit_printk(lvl, test, fmt, ...) \
360 printk(lvl "\t# %s: " fmt, (test)->name, ##__VA_ARGS__)
363 * kunit_info() - Prints an INFO level message associated with @test.
365 * @test: The test context object.
366 * @fmt: A printk() style format string.
368 * Prints an info level message associated with the test suite being run.
369 * Takes a variable number of format parameters just like printk().
371 #define kunit_info(test, fmt, ...) \
372 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
375 * kunit_warn() - Prints a WARN level message associated with @test.
377 * @test: The test context object.
378 * @fmt: A printk() style format string.
380 * Prints a warning level message.
382 #define kunit_warn(test, fmt, ...) \
383 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
386 * kunit_err() - Prints an ERROR level message associated with @test.
388 * @test: The test context object.
389 * @fmt: A printk() style format string.
391 * Prints an error level message.
393 #define kunit_err(test, fmt, ...) \
394 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
397 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
398 * @test: The test context object.
400 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
401 * words, it does nothing and only exists for code clarity. See
402 * KUNIT_EXPECT_TRUE() for more information.
404 #define KUNIT_SUCCEED(test) do {} while (0)
406 void kunit_do_assertion(struct kunit
*test
,
407 struct kunit_assert
*assert,
409 const char *fmt
, ...);
411 #define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do { \
412 struct assert_class __assertion = INITIALIZER; \
413 kunit_do_assertion(test, \
414 &__assertion.assert, \
421 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
422 KUNIT_ASSERTION(test, \
425 KUNIT_INIT_FAIL_ASSERT_STRUCT(test, assert_type), \
430 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
431 * @test: The test context object.
432 * @fmt: an informational message to be printed when the assertion is made.
433 * @...: string format arguments.
435 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
436 * other words, it always results in a failed expectation, and consequently
437 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
438 * for more information.
440 #define KUNIT_FAIL(test, fmt, ...) \
441 KUNIT_FAIL_ASSERTION(test, \
446 #define KUNIT_UNARY_ASSERTION(test, \
452 KUNIT_ASSERTION(test, \
453 !!(condition) == !!expected_true, \
454 kunit_unary_assert, \
455 KUNIT_INIT_UNARY_ASSERT_STRUCT(test, \
462 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
463 KUNIT_UNARY_ASSERTION(test, \
470 #define KUNIT_TRUE_ASSERTION(test, assert_type, condition) \
471 KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, NULL)
473 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
474 KUNIT_UNARY_ASSERTION(test, \
481 #define KUNIT_FALSE_ASSERTION(test, assert_type, condition) \
482 KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, NULL)
485 * A factory macro for defining the assertions and expectations for the basic
486 * comparisons defined for the built in types.
488 * Unfortunately, there is no common type that all types can be promoted to for
489 * which all the binary operators behave the same way as for the actual types
490 * (for example, there is no type that long long and unsigned long long can
491 * both be cast to where the comparison result is preserved for all values). So
492 * the best we can do is do the comparison in the original types and then coerce
493 * everything to long long for printing; this way, the comparison behaves
494 * correctly and the printed out value usually makes sense without
495 * interpretation, but can always be interpreted to figure out the actual
498 #define KUNIT_BASE_BINARY_ASSERTION(test, \
508 typeof(left) __left = (left); \
509 typeof(right) __right = (right); \
510 ((void)__typecheck(__left, __right)); \
512 KUNIT_ASSERTION(test, \
515 ASSERT_CLASS_INIT(test, \
526 #define KUNIT_BASE_EQ_MSG_ASSERTION(test, \
534 KUNIT_BASE_BINARY_ASSERTION(test, \
542 #define KUNIT_BASE_NE_MSG_ASSERTION(test, \
550 KUNIT_BASE_BINARY_ASSERTION(test, \
558 #define KUNIT_BASE_LT_MSG_ASSERTION(test, \
566 KUNIT_BASE_BINARY_ASSERTION(test, \
574 #define KUNIT_BASE_LE_MSG_ASSERTION(test, \
582 KUNIT_BASE_BINARY_ASSERTION(test, \
590 #define KUNIT_BASE_GT_MSG_ASSERTION(test, \
598 KUNIT_BASE_BINARY_ASSERTION(test, \
606 #define KUNIT_BASE_GE_MSG_ASSERTION(test, \
614 KUNIT_BASE_BINARY_ASSERTION(test, \
622 #define KUNIT_BINARY_EQ_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
623 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
624 kunit_binary_assert, \
625 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
632 #define KUNIT_BINARY_EQ_ASSERTION(test, assert_type, left, right) \
633 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
639 #define KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
645 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
646 kunit_binary_ptr_assert, \
647 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
654 #define KUNIT_BINARY_PTR_EQ_ASSERTION(test, assert_type, left, right) \
655 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
661 #define KUNIT_BINARY_NE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
662 KUNIT_BASE_NE_MSG_ASSERTION(test, \
663 kunit_binary_assert, \
664 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
671 #define KUNIT_BINARY_NE_ASSERTION(test, assert_type, left, right) \
672 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
678 #define KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
684 KUNIT_BASE_NE_MSG_ASSERTION(test, \
685 kunit_binary_ptr_assert, \
686 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
693 #define KUNIT_BINARY_PTR_NE_ASSERTION(test, assert_type, left, right) \
694 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
700 #define KUNIT_BINARY_LT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
701 KUNIT_BASE_LT_MSG_ASSERTION(test, \
702 kunit_binary_assert, \
703 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
710 #define KUNIT_BINARY_LT_ASSERTION(test, assert_type, left, right) \
711 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
717 #define KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
723 KUNIT_BASE_LT_MSG_ASSERTION(test, \
724 kunit_binary_ptr_assert, \
725 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
732 #define KUNIT_BINARY_PTR_LT_ASSERTION(test, assert_type, left, right) \
733 KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
739 #define KUNIT_BINARY_LE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
740 KUNIT_BASE_LE_MSG_ASSERTION(test, \
741 kunit_binary_assert, \
742 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
749 #define KUNIT_BINARY_LE_ASSERTION(test, assert_type, left, right) \
750 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
756 #define KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
762 KUNIT_BASE_LE_MSG_ASSERTION(test, \
763 kunit_binary_ptr_assert, \
764 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
771 #define KUNIT_BINARY_PTR_LE_ASSERTION(test, assert_type, left, right) \
772 KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
778 #define KUNIT_BINARY_GT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
779 KUNIT_BASE_GT_MSG_ASSERTION(test, \
780 kunit_binary_assert, \
781 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
788 #define KUNIT_BINARY_GT_ASSERTION(test, assert_type, left, right) \
789 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
795 #define KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
801 KUNIT_BASE_GT_MSG_ASSERTION(test, \
802 kunit_binary_ptr_assert, \
803 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
810 #define KUNIT_BINARY_PTR_GT_ASSERTION(test, assert_type, left, right) \
811 KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
817 #define KUNIT_BINARY_GE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
818 KUNIT_BASE_GE_MSG_ASSERTION(test, \
819 kunit_binary_assert, \
820 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
827 #define KUNIT_BINARY_GE_ASSERTION(test, assert_type, left, right) \
828 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
834 #define KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
840 KUNIT_BASE_GE_MSG_ASSERTION(test, \
841 kunit_binary_ptr_assert, \
842 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
849 #define KUNIT_BINARY_PTR_GE_ASSERTION(test, assert_type, left, right) \
850 KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
856 #define KUNIT_BINARY_STR_ASSERTION(test, \
864 typeof(left) __left = (left); \
865 typeof(right) __right = (right); \
867 KUNIT_ASSERTION(test, \
868 strcmp(__left, __right) op 0, \
869 kunit_binary_str_assert, \
870 KUNIT_INIT_BINARY_ASSERT_STRUCT(test, \
881 #define KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
887 KUNIT_BINARY_STR_ASSERTION(test, \
893 #define KUNIT_BINARY_STR_EQ_ASSERTION(test, assert_type, left, right) \
894 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
900 #define KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
906 KUNIT_BINARY_STR_ASSERTION(test, \
912 #define KUNIT_BINARY_STR_NE_ASSERTION(test, assert_type, left, right) \
913 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
919 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
925 typeof(ptr) __ptr = (ptr); \
927 KUNIT_ASSERTION(test, \
928 !IS_ERR_OR_NULL(__ptr), \
929 kunit_ptr_not_err_assert, \
930 KUNIT_INIT_PTR_NOT_ERR_STRUCT(test, \
938 #define KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, assert_type, ptr) \
939 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
945 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
946 * @test: The test context object.
947 * @condition: an arbitrary boolean expression. The test fails when this does
948 * not evaluate to true.
950 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
951 * to fail when the specified condition is not met; however, it will not prevent
952 * the test case from continuing to run; this is otherwise known as an
953 * *expectation failure*.
955 #define KUNIT_EXPECT_TRUE(test, condition) \
956 KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)
958 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
959 KUNIT_TRUE_MSG_ASSERTION(test, \
966 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
967 * @test: The test context object.
968 * @condition: an arbitrary boolean expression. The test fails when this does
969 * not evaluate to false.
971 * Sets an expectation that @condition evaluates to false. See
972 * KUNIT_EXPECT_TRUE() for more information.
974 #define KUNIT_EXPECT_FALSE(test, condition) \
975 KUNIT_FALSE_ASSERTION(test, KUNIT_EXPECTATION, condition)
977 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
978 KUNIT_FALSE_MSG_ASSERTION(test, \
985 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
986 * @test: The test context object.
987 * @left: an arbitrary expression that evaluates to a primitive C type.
988 * @right: an arbitrary expression that evaluates to a primitive C type.
990 * Sets an expectation that the values that @left and @right evaluate to are
991 * equal. This is semantically equivalent to
992 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
995 #define KUNIT_EXPECT_EQ(test, left, right) \
996 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
998 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
999 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1000 KUNIT_EXPECTATION, \
1007 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
1008 * @test: The test context object.
1009 * @left: an arbitrary expression that evaluates to a pointer.
1010 * @right: an arbitrary expression that evaluates to a pointer.
1012 * Sets an expectation that the values that @left and @right evaluate to are
1013 * equal. This is semantically equivalent to
1014 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1017 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
1018 KUNIT_BINARY_PTR_EQ_ASSERTION(test, \
1019 KUNIT_EXPECTATION, \
1023 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1024 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1025 KUNIT_EXPECTATION, \
1032 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not 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 not
1038 * equal. This is semantically equivalent to
1039 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1042 #define KUNIT_EXPECT_NE(test, left, right) \
1043 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1045 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
1046 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1047 KUNIT_EXPECTATION, \
1054 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not 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 not
1060 * equal. This is semantically equivalent to
1061 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1064 #define KUNIT_EXPECT_PTR_NE(test, left, right) \
1065 KUNIT_BINARY_PTR_NE_ASSERTION(test, \
1066 KUNIT_EXPECTATION, \
1070 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
1071 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1072 KUNIT_EXPECTATION, \
1079 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
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 value that @left evaluates to is less than the
1085 * value that @right evaluates to. This is semantically equivalent to
1086 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
1089 #define KUNIT_EXPECT_LT(test, left, right) \
1090 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1092 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
1093 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1094 KUNIT_EXPECTATION, \
1101 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
1102 * @test: The test context object.
1103 * @left: an arbitrary expression that evaluates to a primitive C type.
1104 * @right: an arbitrary expression that evaluates to a primitive C type.
1106 * Sets an expectation that the value that @left evaluates to is less than or
1107 * equal to the value that @right evaluates to. Semantically this is equivalent
1108 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
1111 #define KUNIT_EXPECT_LE(test, left, right) \
1112 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1114 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
1115 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1116 KUNIT_EXPECTATION, \
1123 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
1124 * @test: The test context object.
1125 * @left: an arbitrary expression that evaluates to a primitive C type.
1126 * @right: an arbitrary expression that evaluates to a primitive C type.
1128 * Sets an expectation that the value that @left evaluates to is greater than
1129 * the value that @right evaluates to. This is semantically equivalent to
1130 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
1133 #define KUNIT_EXPECT_GT(test, left, right) \
1134 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1136 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
1137 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1138 KUNIT_EXPECTATION, \
1145 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1146 * @test: The test context object.
1147 * @left: an arbitrary expression that evaluates to a primitive C type.
1148 * @right: an arbitrary expression that evaluates to a primitive C type.
1150 * Sets an expectation that the value that @left evaluates to is greater than
1151 * the value that @right evaluates to. This is semantically equivalent to
1152 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1155 #define KUNIT_EXPECT_GE(test, left, right) \
1156 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1158 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
1159 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1160 KUNIT_EXPECTATION, \
1167 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1168 * @test: The test context object.
1169 * @left: an arbitrary expression that evaluates to a null terminated string.
1170 * @right: an arbitrary expression that evaluates to a null terminated string.
1172 * Sets an expectation that the values that @left and @right evaluate to are
1173 * equal. This is semantically equivalent to
1174 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1175 * for more information.
1177 #define KUNIT_EXPECT_STREQ(test, left, right) \
1178 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1180 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
1181 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1182 KUNIT_EXPECTATION, \
1189 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1190 * @test: The test context object.
1191 * @left: an arbitrary expression that evaluates to a null terminated string.
1192 * @right: an arbitrary expression that evaluates to a null terminated string.
1194 * Sets an expectation that the values that @left and @right evaluate to are
1195 * not equal. This is semantically equivalent to
1196 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1197 * for more information.
1199 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
1200 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1202 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
1203 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1204 KUNIT_EXPECTATION, \
1211 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1212 * @test: The test context object.
1213 * @ptr: an arbitrary pointer.
1215 * Sets an expectation that the value that @ptr evaluates to is not null and not
1216 * an errno stored in a pointer. This is semantically equivalent to
1217 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1220 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1221 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_EXPECTATION, ptr)
1223 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1224 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1225 KUNIT_EXPECTATION, \
1230 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1231 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1234 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1235 * @test: The test context object.
1236 * @condition: an arbitrary boolean expression. The test fails and aborts when
1237 * this does not evaluate to true.
1239 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1240 * fail *and immediately abort* when the specified condition is not met. Unlike
1241 * an expectation failure, it will prevent the test case from continuing to run;
1242 * this is otherwise known as an *assertion failure*.
1244 #define KUNIT_ASSERT_TRUE(test, condition) \
1245 KUNIT_TRUE_ASSERTION(test, KUNIT_ASSERTION, condition)
1247 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1248 KUNIT_TRUE_MSG_ASSERTION(test, \
1255 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1256 * @test: The test context object.
1257 * @condition: an arbitrary boolean expression.
1259 * Sets an assertion that the value that @condition evaluates to is false. This
1260 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1261 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1263 #define KUNIT_ASSERT_FALSE(test, condition) \
1264 KUNIT_FALSE_ASSERTION(test, KUNIT_ASSERTION, condition)
1266 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1267 KUNIT_FALSE_MSG_ASSERTION(test, \
1274 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1275 * @test: The test context object.
1276 * @left: an arbitrary expression that evaluates to a primitive C type.
1277 * @right: an arbitrary expression that evaluates to a primitive C type.
1279 * Sets an assertion that the values that @left and @right evaluate to are
1280 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1281 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1283 #define KUNIT_ASSERT_EQ(test, left, right) \
1284 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1286 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1287 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1295 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1296 * @test: The test context object.
1297 * @left: an arbitrary expression that evaluates to a pointer.
1298 * @right: an arbitrary expression that evaluates to a pointer.
1300 * Sets an assertion that the values that @left and @right evaluate to are
1301 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1302 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1304 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1305 KUNIT_BINARY_PTR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1307 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1308 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1316 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1317 * @test: The test context object.
1318 * @left: an arbitrary expression that evaluates to a primitive C type.
1319 * @right: an arbitrary expression that evaluates to a primitive C type.
1321 * Sets an assertion that the values that @left and @right evaluate to are not
1322 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1323 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1325 #define KUNIT_ASSERT_NE(test, left, right) \
1326 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1328 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1329 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1337 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1338 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1339 * @test: The test context object.
1340 * @left: an arbitrary expression that evaluates to a pointer.
1341 * @right: an arbitrary expression that evaluates to a pointer.
1343 * Sets an assertion that the values that @left and @right evaluate to are not
1344 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1345 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1347 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1348 KUNIT_BINARY_PTR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1350 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1351 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1358 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1359 * @test: The test context object.
1360 * @left: an arbitrary expression that evaluates to a primitive C type.
1361 * @right: an arbitrary expression that evaluates to a primitive C type.
1363 * Sets an assertion that the value that @left evaluates to is less than the
1364 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1365 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1368 #define KUNIT_ASSERT_LT(test, left, right) \
1369 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1371 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1372 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1379 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1380 * @test: The test context object.
1381 * @left: an arbitrary expression that evaluates to a primitive C type.
1382 * @right: an arbitrary expression that evaluates to a primitive C type.
1384 * Sets an assertion that the value that @left evaluates to is less than or
1385 * equal to the value that @right evaluates to. This is the same as
1386 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1387 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1389 #define KUNIT_ASSERT_LE(test, left, right) \
1390 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1392 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1393 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1401 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1402 * @test: The test context object.
1403 * @left: an arbitrary expression that evaluates to a primitive C type.
1404 * @right: an arbitrary expression that evaluates to a primitive C type.
1406 * Sets an assertion that the value that @left evaluates to is greater than the
1407 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1408 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1411 #define KUNIT_ASSERT_GT(test, left, right) \
1412 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1414 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1415 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1423 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1424 * @test: The test context object.
1425 * @left: an arbitrary expression that evaluates to a primitive C type.
1426 * @right: an arbitrary expression that evaluates to a primitive C type.
1428 * Sets an assertion that the value that @left evaluates to is greater than the
1429 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1430 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1433 #define KUNIT_ASSERT_GE(test, left, right) \
1434 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1436 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1437 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1445 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1446 * @test: The test context object.
1447 * @left: an arbitrary expression that evaluates to a null terminated string.
1448 * @right: an arbitrary expression that evaluates to a null terminated string.
1450 * Sets an assertion that the values that @left and @right evaluate to are
1451 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1452 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1454 #define KUNIT_ASSERT_STREQ(test, left, right) \
1455 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1457 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1458 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1466 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1467 * @test: The test context object.
1468 * @left: an arbitrary expression that evaluates to a null terminated string.
1469 * @right: an arbitrary expression that evaluates to a null terminated string.
1471 * Sets an expectation that the values that @left and @right evaluate to are
1472 * not equal. This is semantically equivalent to
1473 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1474 * for more information.
1476 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1477 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1479 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1480 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1488 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1489 * @test: The test context object.
1490 * @ptr: an arbitrary pointer.
1492 * Sets an assertion that the value that @ptr evaluates to is not null and not
1493 * an errno stored in a pointer. This is the same as
1494 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1495 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1497 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1498 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_ASSERTION, ptr)
1500 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1501 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1507 #endif /* _KUNIT_TEST_H */