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 *)`` that makes expectations (see
91 * KUNIT_EXPECT_TRUE()) about code under test. Each test case is associated
92 * with a &struct kunit_suite and will be run after the suite's init
93 * function and followed by the suite's exit function.
95 * A test case should be static and should only be created with the
96 * KUNIT_CASE() macro; additionally, every array of test cases should be
97 * terminated with an empty test case.
103 * void add_test_basic(struct kunit *test)
105 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
106 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
107 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
108 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
109 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
112 * static struct kunit_case example_test_cases[] = {
113 * KUNIT_CASE(add_test_basic),
119 void (*run_case)(struct kunit *test);
122 /* private: internal use only. */
127 * KUNIT_CASE - A helper for creating a &struct kunit_case
129 * @test_name: a reference to a test case function.
131 * Takes a symbol for a function representing a test case and creates a
132 * &struct kunit_case object from it. See the documentation for
133 * &struct kunit_case for an example on how to use it.
135 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
138 * struct kunit_suite - describes a related collection of &struct kunit_case
140 * @name: the name of the test. Purely informational.
141 * @init: called before every test case.
142 * @exit: called after every test case.
143 * @test_cases: a null terminated array of test cases.
145 * A kunit_suite is a collection of related &struct kunit_case s, such that
146 * @init is called before every test case and @exit is called after every
147 * test case, similar to the notion of a *test fixture* or a *test class*
148 * in other unit testing frameworks like JUnit or Googletest.
150 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
154 const char name[256];
155 int (*init)(struct kunit *test);
156 void (*exit)(struct kunit *test);
157 struct kunit_case *test_cases;
161 * struct kunit - represents a running instance of a test.
163 * @priv: for user to store arbitrary data. Commonly used to pass data
164 * created in the init function (see &struct kunit_suite).
166 * Used to store information about the current context under which the test
167 * is running. Most of this data is private and should only be accessed
168 * indirectly via public functions; the one exception is @priv which can be
169 * used by the test writer to store arbitrary data.
174 /* private: internal use only. */
175 const char *name; /* Read only after initialization! */
176 struct kunit_try_catch try_catch;
178 * success starts as true, and may only be set to false during a
179 * test case; thus, it is safe to update this across multiple
180 * threads using WRITE_ONCE; however, as a consequence, it may only
181 * be read after the test case finishes once all threads associated
182 * with the test case have terminated.
184 bool success; /* Read only after test_case finishes! */
185 spinlock_t lock; /* Guards all mutable test state. */
187 * Because resources is a list that may be updated multiple times (with
188 * new resources) from any thread associated with a test case, we must
189 * protect it with some type of lock.
191 struct list_head resources; /* Protected by lock. */
194 void kunit_init_test(struct kunit *test, const char *name);
196 int kunit_run_tests(struct kunit_suite *suite);
199 * kunit_test_suite() - used to register a &struct kunit_suite with KUnit.
201 * @suite: a statically allocated &struct kunit_suite.
203 * Registers @suite with the test framework. See &struct kunit_suite for
206 * NOTE: Currently KUnit tests are all run as late_initcalls; this means
207 * that they cannot test anything where tests must run at a different init
208 * phase. One significant restriction resulting from this is that KUnit
209 * cannot reliably test anything that is initialize in the late_init phase;
210 * another is that KUnit is useless to test things that need to be run in
211 * an earlier init phase.
213 * TODO(brendanhiggins@google.com): Don't run all KUnit tests as
214 * late_initcalls. I have some future work planned to dispatch all KUnit
215 * tests from the same place, and at the very least to do so after
216 * everything else is definitely initialized.
218 #define kunit_test_suite(suite) \
219 static int kunit_suite_init##suite(void) \
221 return kunit_run_tests(&suite); \
223 late_initcall(kunit_suite_init##suite)
226 * Like kunit_alloc_resource() below, but returns the struct kunit_resource
227 * object that contains the allocation. This is mostly for testing purposes.
229 struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
230 kunit_resource_init_t init,
231 kunit_resource_free_t free,
236 * kunit_alloc_resource() - Allocates a *test managed resource*.
237 * @test: The test context object.
238 * @init: a user supplied function to initialize the resource.
239 * @free: a user supplied function to free the resource.
240 * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
241 * @context: for the user to pass in arbitrary data to the init function.
243 * Allocates a *test managed resource*, a resource which will automatically be
244 * cleaned up at the end of a test case. See &struct kunit_resource for an
247 * NOTE: KUnit needs to allocate memory for each kunit_resource object. You must
248 * specify an @internal_gfp that is compatible with the use context of your
251 static inline void *kunit_alloc_resource(struct kunit *test,
252 kunit_resource_init_t init,
253 kunit_resource_free_t free,
257 struct kunit_resource *res;
259 res = kunit_alloc_and_get_resource(test, init, free, internal_gfp,
263 return res->allocation;
268 typedef bool (*kunit_resource_match_t)(struct kunit *test,
273 * kunit_resource_instance_match() - Match a resource with the same instance.
274 * @test: Test case to which the resource belongs.
275 * @res: The data stored in kunit_resource->allocation.
276 * @match_data: The resource pointer to match against.
278 * An instance of kunit_resource_match_t that matches a resource whose
279 * allocation matches @match_data.
281 static inline bool kunit_resource_instance_match(struct kunit *test,
285 return res == match_data;
289 * kunit_resource_destroy() - Find a kunit_resource and destroy it.
290 * @test: Test case to which the resource belongs.
291 * @match: Match function. Returns whether a given resource matches @match_data.
292 * @free: Must match free on the kunit_resource to free.
293 * @match_data: Data passed into @match.
295 * Free the latest kunit_resource of @test for which @free matches the
296 * kunit_resource_free_t associated with the resource and for which @match
300 * 0 if kunit_resource is found and freed, -ENOENT if not found.
302 int kunit_resource_destroy(struct kunit *test,
303 kunit_resource_match_t match,
304 kunit_resource_free_t free,
308 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
309 * @test: The test context object.
310 * @size: The size in bytes of the desired memory.
311 * @gfp: flags passed to underlying kmalloc().
313 * Just like `kmalloc(...)`, except the allocation is managed by the test case
314 * and is automatically cleaned up after the test case concludes. See &struct
315 * kunit_resource for more information.
317 void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp);
320 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
321 * @test: The test case to which the resource belongs.
322 * @ptr: The memory allocation to free.
324 void kunit_kfree(struct kunit *test, const void *ptr);
327 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
328 * @test: The test context object.
329 * @size: The size in bytes of the desired memory.
330 * @gfp: flags passed to underlying kmalloc().
332 * See kzalloc() and kunit_kmalloc() for more information.
334 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
336 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
339 void kunit_cleanup(struct kunit *test);
341 void __printf(3, 4) kunit_printk(const char *level,
342 const struct kunit *test,
343 const char *fmt, ...);
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 #endif /* _KUNIT_TEST_H */