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>
18 #include <linux/kref.h>
20 struct kunit_resource;
22 typedef int (*kunit_resource_init_t)(struct kunit_resource *, void *);
23 typedef void (*kunit_resource_free_t)(struct kunit_resource *);
26 * struct kunit_resource - represents a *test managed resource*
27 * @data: for the user to store arbitrary data.
28 * @name: optional name
29 * @free: a user supplied function to free the resource. Populated by
30 * kunit_resource_alloc().
32 * Represents a *test managed resource*, a resource which will automatically be
33 * cleaned up at the end of a test case.
35 * Resources are reference counted so if a resource is retrieved via
36 * kunit_alloc_and_get_resource() or kunit_find_resource(), we need
37 * to call kunit_put_resource() to reduce the resource reference count
38 * when finished with it. Note that kunit_alloc_resource() does not require a
39 * kunit_resource_put() because it does not retrieve the resource itself.
45 * struct kunit_kmalloc_params {
50 * static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
52 * struct kunit_kmalloc_params *params = context;
53 * res->data = kmalloc(params->size, params->gfp);
61 * static void kunit_kmalloc_free(struct kunit_resource *res)
66 * void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
68 * struct kunit_kmalloc_params params;
73 * return kunit_alloc_resource(test, kunit_kmalloc_init,
74 * kunit_kmalloc_free, ¶ms);
77 * Resources can also be named, with lookup/removal done on a name
78 * basis also. kunit_add_named_resource(), kunit_find_named_resource()
79 * and kunit_destroy_named_resource(). Resource names must be
80 * unique within the test instance.
82 struct kunit_resource {
85 kunit_resource_free_t free;
87 /* private: internal use only. */
89 struct list_head node;
94 /* Size of log associated with test. */
95 #define KUNIT_LOG_SIZE 512
97 /* Maximum size of parameter description string. */
98 #define KUNIT_PARAM_DESC_SIZE 128
100 /* Maximum size of a status comment. */
101 #define KUNIT_STATUS_COMMENT_SIZE 256
104 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
105 * sub-subtest. See the "Subtests" section in
106 * https://node-tap.org/tap-protocol/
108 #define KUNIT_SUBTEST_INDENT " "
109 #define KUNIT_SUBSUBTEST_INDENT " "
112 * enum kunit_status - Type of result for a test or test suite
113 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
114 * @KUNIT_FAILURE: Denotes the test has failed.
115 * @KUNIT_SKIPPED: Denotes the test has been skipped.
124 * struct kunit_case - represents an individual test case.
126 * @run_case: the function representing the actual test case.
127 * @name: the name of the test case.
128 * @generate_params: the generator function for parameterized tests.
130 * A test case is a function with the signature,
131 * ``void (*)(struct kunit *)``
132 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
133 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
134 * with a &struct kunit_suite and will be run after the suite's init
135 * function and followed by the suite's exit function.
137 * A test case should be static and should only be created with the
138 * KUNIT_CASE() macro; additionally, every array of test cases should be
139 * terminated with an empty test case.
145 * void add_test_basic(struct kunit *test)
147 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
148 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
149 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
150 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
151 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
154 * static struct kunit_case example_test_cases[] = {
155 * KUNIT_CASE(add_test_basic),
161 void (*run_case)(struct kunit *test);
163 const void* (*generate_params)(const void *prev, char *desc);
165 /* private: internal use only. */
166 enum kunit_status status;
170 static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
183 * KUNIT_CASE - A helper for creating a &struct kunit_case
185 * @test_name: a reference to a test case function.
187 * Takes a symbol for a function representing a test case and creates a
188 * &struct kunit_case object from it. See the documentation for
189 * &struct kunit_case for an example on how to use it.
191 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
194 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
196 * @test_name: a reference to a test case function.
197 * @gen_params: a reference to a parameter generator function.
199 * The generator function::
201 * const void* gen_params(const void *prev, char *desc)
203 * is used to lazily generate a series of arbitrarily typed values that fit into
204 * a void*. The argument @prev is the previously returned value, which should be
205 * used to derive the next value; @prev is set to NULL on the initial generator
206 * call. When no more values are available, the generator must return NULL.
207 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
208 * describing the parameter.
210 #define KUNIT_CASE_PARAM(test_name, gen_params) \
211 { .run_case = test_name, .name = #test_name, \
212 .generate_params = gen_params }
215 * struct kunit_suite - describes a related collection of &struct kunit_case
217 * @name: the name of the test. Purely informational.
218 * @init: called before every test case.
219 * @exit: called after every test case.
220 * @test_cases: a null terminated array of test cases.
222 * A kunit_suite is a collection of related &struct kunit_case s, such that
223 * @init is called before every test case and @exit is called after every
224 * test case, similar to the notion of a *test fixture* or a *test class*
225 * in other unit testing frameworks like JUnit or Googletest.
227 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
231 const char name[256];
232 int (*init)(struct kunit *test);
233 void (*exit)(struct kunit *test);
234 struct kunit_case *test_cases;
236 /* private: internal use only */
237 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
238 struct dentry *debugfs;
243 * struct kunit - represents a running instance of a test.
245 * @priv: for user to store arbitrary data. Commonly used to pass data
246 * created in the init function (see &struct kunit_suite).
248 * Used to store information about the current context under which the test
249 * is running. Most of this data is private and should only be accessed
250 * indirectly via public functions; the one exception is @priv which can be
251 * used by the test writer to store arbitrary data.
256 /* private: internal use only. */
257 const char *name; /* Read only after initialization! */
258 char *log; /* Points at case log after initialization */
259 struct kunit_try_catch try_catch;
260 /* param_value is the current parameter value for a test case. */
261 const void *param_value;
262 /* param_index stores the index of the parameter in parameterized tests. */
265 * success starts as true, and may only be set to false during a
266 * test case; thus, it is safe to update this across multiple
267 * threads using WRITE_ONCE; however, as a consequence, it may only
268 * be read after the test case finishes once all threads associated
269 * with the test case have terminated.
271 spinlock_t lock; /* Guards all mutable test state. */
272 enum kunit_status status; /* Read only after test_case finishes! */
274 * Because resources is a list that may be updated multiple times (with
275 * new resources) from any thread associated with a test case, we must
276 * protect it with some type of lock.
278 struct list_head resources; /* Protected by lock. */
280 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
283 static inline void kunit_set_failure(struct kunit *test)
285 WRITE_ONCE(test->status, KUNIT_FAILURE);
288 void kunit_init_test(struct kunit *test, const char *name, char *log);
290 int kunit_run_tests(struct kunit_suite *suite);
292 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
294 unsigned int kunit_test_case_num(struct kunit_suite *suite,
295 struct kunit_case *test_case);
297 int __kunit_test_suites_init(struct kunit_suite * const * const suites);
299 void __kunit_test_suites_exit(struct kunit_suite **suites);
301 #if IS_BUILTIN(CONFIG_KUNIT)
302 int kunit_run_all_tests(void);
304 static inline int kunit_run_all_tests(void)
308 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
312 * kunit_test_suites_for_module() - used to register one or more
313 * &struct kunit_suite with KUnit.
315 * @__suites: a statically allocated list of &struct kunit_suite.
317 * Registers @__suites with the test framework. See &struct kunit_suite for
320 * If a test suite is built-in, module_init() gets translated into
321 * an initcall which we don't want as the idea is that for builtins
322 * the executor will manage execution. So ensure we do not define
323 * module_{init|exit} functions for the builtin case when registering
324 * suites via kunit_test_suites() below.
326 #define kunit_test_suites_for_module(__suites) \
327 static int __init kunit_test_suites_init(void) \
329 return __kunit_test_suites_init(__suites); \
331 module_init(kunit_test_suites_init); \
333 static void __exit kunit_test_suites_exit(void) \
335 return __kunit_test_suites_exit(__suites); \
337 module_exit(kunit_test_suites_exit)
339 #define kunit_test_suites_for_module(__suites)
342 #define __kunit_test_suites(unique_array, unique_suites, ...) \
343 static struct kunit_suite *unique_array[] = { __VA_ARGS__, NULL }; \
344 kunit_test_suites_for_module(unique_array); \
345 static struct kunit_suite **unique_suites \
346 __used __section(".kunit_test_suites") = unique_array
349 * kunit_test_suites() - used to register one or more &struct kunit_suite
352 * @__suites: a statically allocated list of &struct kunit_suite.
354 * Registers @suites with the test framework. See &struct kunit_suite for
357 * When builtin, KUnit tests are all run via executor; this is done
358 * by placing the array of struct kunit_suite * in the .kunit_test_suites
361 * An alternative is to build the tests as a module. Because modules do not
362 * support multiple initcall()s, we need to initialize an array of suites for a
366 #define kunit_test_suites(__suites...) \
367 __kunit_test_suites(__UNIQUE_ID(array), \
368 __UNIQUE_ID(suites), \
371 #define kunit_test_suite(suite) kunit_test_suites(&suite)
373 #define kunit_suite_for_each_test_case(suite, test_case) \
374 for (test_case = suite->test_cases; test_case->run_case; test_case++)
376 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
379 * Like kunit_alloc_resource() below, but returns the struct kunit_resource
380 * object that contains the allocation. This is mostly for testing purposes.
382 struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
383 kunit_resource_init_t init,
384 kunit_resource_free_t free,
389 * kunit_get_resource() - Hold resource for use. Should not need to be used
390 * by most users as we automatically get resources
391 * retrieved by kunit_find_resource*().
394 static inline void kunit_get_resource(struct kunit_resource *res)
396 kref_get(&res->refcount);
400 * Called when refcount reaches zero via kunit_put_resources();
401 * should not be called directly.
403 static inline void kunit_release_resource(struct kref *kref)
405 struct kunit_resource *res = container_of(kref, struct kunit_resource,
408 /* If free function is defined, resource was dynamically allocated. */
416 * kunit_put_resource() - When caller is done with retrieved resource,
417 * kunit_put_resource() should be called to drop
418 * reference count. The resource list maintains
419 * a reference count on resources, so if no users
420 * are utilizing a resource and it is removed from
421 * the resource list, it will be freed via the
422 * associated free function (if any). Only
423 * needs to be used if we alloc_and_get() or
427 static inline void kunit_put_resource(struct kunit_resource *res)
429 kref_put(&res->refcount, kunit_release_resource);
433 * kunit_add_resource() - Add a *test managed resource*.
434 * @test: The test context object.
435 * @init: a user-supplied function to initialize the result (if needed). If
436 * none is supplied, the resource data value is simply set to @data.
437 * If an init function is supplied, @data is passed to it instead.
438 * @free: a user-supplied function to free the resource (if needed).
439 * @res: The resource.
440 * @data: value to pass to init function or set in resource data field.
442 int kunit_add_resource(struct kunit *test,
443 kunit_resource_init_t init,
444 kunit_resource_free_t free,
445 struct kunit_resource *res,
449 * kunit_add_named_resource() - Add a named *test managed resource*.
450 * @test: The test context object.
451 * @init: a user-supplied function to initialize the resource data, if needed.
452 * @free: a user-supplied function to free the resource data, if needed.
453 * @res: The resource.
454 * @name: name to be set for resource.
455 * @data: value to pass to init function or set in resource data field.
457 int kunit_add_named_resource(struct kunit *test,
458 kunit_resource_init_t init,
459 kunit_resource_free_t free,
460 struct kunit_resource *res,
465 * kunit_alloc_resource() - Allocates a *test managed resource*.
466 * @test: The test context object.
467 * @init: a user supplied function to initialize the resource.
468 * @free: a user supplied function to free the resource.
469 * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
470 * @context: for the user to pass in arbitrary data to the init function.
472 * Allocates a *test managed resource*, a resource which will automatically be
473 * cleaned up at the end of a test case. See &struct kunit_resource for an
476 * Note: KUnit needs to allocate memory for a kunit_resource object. You must
477 * specify an @internal_gfp that is compatible with the use context of your
480 static inline void *kunit_alloc_resource(struct kunit *test,
481 kunit_resource_init_t init,
482 kunit_resource_free_t free,
486 struct kunit_resource *res;
488 res = kzalloc(sizeof(*res), internal_gfp);
492 if (!kunit_add_resource(test, init, free, res, context))
498 typedef bool (*kunit_resource_match_t)(struct kunit *test,
499 struct kunit_resource *res,
503 * kunit_resource_instance_match() - Match a resource with the same instance.
504 * @test: Test case to which the resource belongs.
505 * @res: The resource.
506 * @match_data: The resource pointer to match against.
508 * An instance of kunit_resource_match_t that matches a resource whose
509 * allocation matches @match_data.
511 static inline bool kunit_resource_instance_match(struct kunit *test,
512 struct kunit_resource *res,
515 return res->data == match_data;
519 * kunit_resource_name_match() - Match a resource with the same name.
520 * @test: Test case to which the resource belongs.
521 * @res: The resource.
522 * @match_name: The name to match against.
524 static inline bool kunit_resource_name_match(struct kunit *test,
525 struct kunit_resource *res,
528 return res->name && strcmp(res->name, match_name) == 0;
532 * kunit_find_resource() - Find a resource using match function/data.
533 * @test: Test case to which the resource belongs.
534 * @match: match function to be applied to resources/match data.
535 * @match_data: data to be used in matching.
537 static inline struct kunit_resource *
538 kunit_find_resource(struct kunit *test,
539 kunit_resource_match_t match,
542 struct kunit_resource *res, *found = NULL;
545 spin_lock_irqsave(&test->lock, flags);
547 list_for_each_entry_reverse(res, &test->resources, node) {
548 if (match(test, res, (void *)match_data)) {
550 kunit_get_resource(found);
555 spin_unlock_irqrestore(&test->lock, flags);
561 * kunit_find_named_resource() - Find a resource using match name.
562 * @test: Test case to which the resource belongs.
565 static inline struct kunit_resource *
566 kunit_find_named_resource(struct kunit *test,
569 return kunit_find_resource(test, kunit_resource_name_match,
574 * kunit_destroy_resource() - Find a kunit_resource and destroy it.
575 * @test: Test case to which the resource belongs.
576 * @match: Match function. Returns whether a given resource matches @match_data.
577 * @match_data: Data passed into @match.
580 * 0 if kunit_resource is found and freed, -ENOENT if not found.
582 int kunit_destroy_resource(struct kunit *test,
583 kunit_resource_match_t match,
586 static inline int kunit_destroy_named_resource(struct kunit *test,
589 return kunit_destroy_resource(test, kunit_resource_name_match,
594 * kunit_remove_resource() - remove resource from resource list associated with
596 * @test: The test context object.
597 * @res: The resource to be removed.
599 * Note that the resource will not be immediately freed since it is likely
600 * the caller has a reference to it via alloc_and_get() or find();
601 * in this case a final call to kunit_put_resource() is required.
603 void kunit_remove_resource(struct kunit *test, struct kunit_resource *res);
606 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
607 * @test: The test context object.
608 * @n: number of elements.
609 * @size: The size in bytes of the desired memory.
610 * @gfp: flags passed to underlying kmalloc().
612 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
613 * and is automatically cleaned up after the test case concludes. See &struct
614 * kunit_resource for more information.
616 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
619 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
620 * @test: The test context object.
621 * @size: The size in bytes of the desired memory.
622 * @gfp: flags passed to underlying kmalloc().
624 * See kmalloc() and kunit_kmalloc_array() for more information.
626 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
628 return kunit_kmalloc_array(test, 1, size, gfp);
632 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
633 * @test: The test case to which the resource belongs.
634 * @ptr: The memory allocation to free.
636 void kunit_kfree(struct kunit *test, const void *ptr);
639 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
640 * @test: The test context object.
641 * @size: The size in bytes of the desired memory.
642 * @gfp: flags passed to underlying kmalloc().
644 * See kzalloc() and kunit_kmalloc_array() for more information.
646 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
648 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
652 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
653 * @test: The test context object.
654 * @n: number of elements.
655 * @size: The size in bytes of the desired memory.
656 * @gfp: flags passed to underlying kmalloc().
658 * See kcalloc() and kunit_kmalloc_array() for more information.
660 static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
662 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
665 void kunit_cleanup(struct kunit *test);
667 void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
670 * kunit_mark_skipped() - Marks @test_or_suite as skipped
672 * @test_or_suite: The test context object.
673 * @fmt: A printk() style format string.
675 * Marks the test as skipped. @fmt is given output as the test status
676 * comment, typically the reason the test was skipped.
678 * Test execution continues after kunit_mark_skipped() is called.
680 #define kunit_mark_skipped(test_or_suite, fmt, ...) \
682 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
683 scnprintf((test_or_suite)->status_comment, \
684 KUNIT_STATUS_COMMENT_SIZE, \
685 fmt, ##__VA_ARGS__); \
689 * kunit_skip() - Marks @test_or_suite as skipped
691 * @test_or_suite: The test context object.
692 * @fmt: A printk() style format string.
694 * Skips the test. @fmt is given output as the test status
695 * comment, typically the reason the test was skipped.
697 * Test execution is halted after kunit_skip() is called.
699 #define kunit_skip(test_or_suite, fmt, ...) \
701 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
702 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
706 * printk and log to per-test or per-suite log buffer. Logging only done
707 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
709 #define kunit_log(lvl, test_or_suite, fmt, ...) \
711 printk(lvl fmt, ##__VA_ARGS__); \
712 kunit_log_append((test_or_suite)->log, fmt "\n", \
716 #define kunit_printk(lvl, test, fmt, ...) \
717 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
718 (test)->name, ##__VA_ARGS__)
721 * kunit_info() - Prints an INFO level message associated with @test.
723 * @test: The test context object.
724 * @fmt: A printk() style format string.
726 * Prints an info level message associated with the test suite being run.
727 * Takes a variable number of format parameters just like printk().
729 #define kunit_info(test, fmt, ...) \
730 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
733 * kunit_warn() - Prints a WARN level message associated with @test.
735 * @test: The test context object.
736 * @fmt: A printk() style format string.
738 * Prints a warning level message.
740 #define kunit_warn(test, fmt, ...) \
741 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
744 * kunit_err() - Prints an ERROR level message associated with @test.
746 * @test: The test context object.
747 * @fmt: A printk() style format string.
749 * Prints an error level message.
751 #define kunit_err(test, fmt, ...) \
752 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
755 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
756 * @test: The test context object.
758 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
759 * words, it does nothing and only exists for code clarity. See
760 * KUNIT_EXPECT_TRUE() for more information.
762 #define KUNIT_SUCCEED(test) do {} while (0)
764 void kunit_do_assertion(struct kunit *test,
765 struct kunit_assert *assert,
767 const char *fmt, ...);
769 #define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do { \
770 struct assert_class __assertion = INITIALIZER; \
771 kunit_do_assertion(test, \
772 &__assertion.assert, \
779 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
780 KUNIT_ASSERTION(test, \
783 KUNIT_INIT_FAIL_ASSERT_STRUCT(test, assert_type), \
788 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
789 * @test: The test context object.
790 * @fmt: an informational message to be printed when the assertion is made.
791 * @...: string format arguments.
793 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
794 * other words, it always results in a failed expectation, and consequently
795 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
796 * for more information.
798 #define KUNIT_FAIL(test, fmt, ...) \
799 KUNIT_FAIL_ASSERTION(test, \
804 #define KUNIT_UNARY_ASSERTION(test, \
810 KUNIT_ASSERTION(test, \
811 !!(condition) == !!expected_true, \
812 kunit_unary_assert, \
813 KUNIT_INIT_UNARY_ASSERT_STRUCT(test, \
820 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
821 KUNIT_UNARY_ASSERTION(test, \
828 #define KUNIT_TRUE_ASSERTION(test, assert_type, condition) \
829 KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, NULL)
831 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
832 KUNIT_UNARY_ASSERTION(test, \
839 #define KUNIT_FALSE_ASSERTION(test, assert_type, condition) \
840 KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, NULL)
843 * A factory macro for defining the assertions and expectations for the basic
844 * comparisons defined for the built in types.
846 * Unfortunately, there is no common type that all types can be promoted to for
847 * which all the binary operators behave the same way as for the actual types
848 * (for example, there is no type that long long and unsigned long long can
849 * both be cast to where the comparison result is preserved for all values). So
850 * the best we can do is do the comparison in the original types and then coerce
851 * everything to long long for printing; this way, the comparison behaves
852 * correctly and the printed out value usually makes sense without
853 * interpretation, but can always be interpreted to figure out the actual
856 #define KUNIT_BASE_BINARY_ASSERTION(test, \
866 typeof(left) __left = (left); \
867 typeof(right) __right = (right); \
869 KUNIT_ASSERTION(test, \
872 ASSERT_CLASS_INIT(test, \
883 #define KUNIT_BASE_EQ_MSG_ASSERTION(test, \
891 KUNIT_BASE_BINARY_ASSERTION(test, \
899 #define KUNIT_BASE_NE_MSG_ASSERTION(test, \
907 KUNIT_BASE_BINARY_ASSERTION(test, \
915 #define KUNIT_BASE_LT_MSG_ASSERTION(test, \
923 KUNIT_BASE_BINARY_ASSERTION(test, \
931 #define KUNIT_BASE_LE_MSG_ASSERTION(test, \
939 KUNIT_BASE_BINARY_ASSERTION(test, \
947 #define KUNIT_BASE_GT_MSG_ASSERTION(test, \
955 KUNIT_BASE_BINARY_ASSERTION(test, \
963 #define KUNIT_BASE_GE_MSG_ASSERTION(test, \
971 KUNIT_BASE_BINARY_ASSERTION(test, \
979 #define KUNIT_BINARY_EQ_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
980 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
981 kunit_binary_assert, \
982 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
989 #define KUNIT_BINARY_EQ_ASSERTION(test, assert_type, left, right) \
990 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
996 #define KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1002 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
1003 kunit_binary_ptr_assert, \
1004 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1011 #define KUNIT_BINARY_PTR_EQ_ASSERTION(test, assert_type, left, right) \
1012 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1018 #define KUNIT_BINARY_NE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1019 KUNIT_BASE_NE_MSG_ASSERTION(test, \
1020 kunit_binary_assert, \
1021 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1028 #define KUNIT_BINARY_NE_ASSERTION(test, assert_type, left, right) \
1029 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1035 #define KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1041 KUNIT_BASE_NE_MSG_ASSERTION(test, \
1042 kunit_binary_ptr_assert, \
1043 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1050 #define KUNIT_BINARY_PTR_NE_ASSERTION(test, assert_type, left, right) \
1051 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1057 #define KUNIT_BINARY_LT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1058 KUNIT_BASE_LT_MSG_ASSERTION(test, \
1059 kunit_binary_assert, \
1060 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1067 #define KUNIT_BINARY_LT_ASSERTION(test, assert_type, left, right) \
1068 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1074 #define KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
1080 KUNIT_BASE_LT_MSG_ASSERTION(test, \
1081 kunit_binary_ptr_assert, \
1082 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1089 #define KUNIT_BINARY_PTR_LT_ASSERTION(test, assert_type, left, right) \
1090 KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
1096 #define KUNIT_BINARY_LE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1097 KUNIT_BASE_LE_MSG_ASSERTION(test, \
1098 kunit_binary_assert, \
1099 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1106 #define KUNIT_BINARY_LE_ASSERTION(test, assert_type, left, right) \
1107 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1113 #define KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
1119 KUNIT_BASE_LE_MSG_ASSERTION(test, \
1120 kunit_binary_ptr_assert, \
1121 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1128 #define KUNIT_BINARY_PTR_LE_ASSERTION(test, assert_type, left, right) \
1129 KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
1135 #define KUNIT_BINARY_GT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1136 KUNIT_BASE_GT_MSG_ASSERTION(test, \
1137 kunit_binary_assert, \
1138 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1145 #define KUNIT_BINARY_GT_ASSERTION(test, assert_type, left, right) \
1146 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1152 #define KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
1158 KUNIT_BASE_GT_MSG_ASSERTION(test, \
1159 kunit_binary_ptr_assert, \
1160 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1167 #define KUNIT_BINARY_PTR_GT_ASSERTION(test, assert_type, left, right) \
1168 KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
1174 #define KUNIT_BINARY_GE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1175 KUNIT_BASE_GE_MSG_ASSERTION(test, \
1176 kunit_binary_assert, \
1177 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1184 #define KUNIT_BINARY_GE_ASSERTION(test, assert_type, left, right) \
1185 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1191 #define KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
1197 KUNIT_BASE_GE_MSG_ASSERTION(test, \
1198 kunit_binary_ptr_assert, \
1199 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1206 #define KUNIT_BINARY_PTR_GE_ASSERTION(test, assert_type, left, right) \
1207 KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
1213 #define KUNIT_BINARY_STR_ASSERTION(test, \
1221 const char *__left = (left); \
1222 const char *__right = (right); \
1224 KUNIT_ASSERTION(test, \
1225 strcmp(__left, __right) op 0, \
1226 kunit_binary_str_assert, \
1227 KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(test, \
1238 #define KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1244 KUNIT_BINARY_STR_ASSERTION(test, \
1250 #define KUNIT_BINARY_STR_EQ_ASSERTION(test, assert_type, left, right) \
1251 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1257 #define KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1263 KUNIT_BINARY_STR_ASSERTION(test, \
1269 #define KUNIT_BINARY_STR_NE_ASSERTION(test, assert_type, left, right) \
1270 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1276 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1282 typeof(ptr) __ptr = (ptr); \
1284 KUNIT_ASSERTION(test, \
1285 !IS_ERR_OR_NULL(__ptr), \
1286 kunit_ptr_not_err_assert, \
1287 KUNIT_INIT_PTR_NOT_ERR_STRUCT(test, \
1295 #define KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, assert_type, ptr) \
1296 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1302 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
1303 * @test: The test context object.
1304 * @condition: an arbitrary boolean expression. The test fails when this does
1305 * not evaluate to true.
1307 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
1308 * to fail when the specified condition is not met; however, it will not prevent
1309 * the test case from continuing to run; this is otherwise known as an
1310 * *expectation failure*.
1312 #define KUNIT_EXPECT_TRUE(test, condition) \
1313 KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)
1315 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
1316 KUNIT_TRUE_MSG_ASSERTION(test, \
1317 KUNIT_EXPECTATION, \
1323 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
1324 * @test: The test context object.
1325 * @condition: an arbitrary boolean expression. The test fails when this does
1326 * not evaluate to false.
1328 * Sets an expectation that @condition evaluates to false. See
1329 * KUNIT_EXPECT_TRUE() for more information.
1331 #define KUNIT_EXPECT_FALSE(test, condition) \
1332 KUNIT_FALSE_ASSERTION(test, KUNIT_EXPECTATION, condition)
1334 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
1335 KUNIT_FALSE_MSG_ASSERTION(test, \
1336 KUNIT_EXPECTATION, \
1342 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
1343 * @test: The test context object.
1344 * @left: an arbitrary expression that evaluates to a primitive C type.
1345 * @right: an arbitrary expression that evaluates to a primitive C type.
1347 * Sets an expectation that the values that @left and @right evaluate to are
1348 * equal. This is semantically equivalent to
1349 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1352 #define KUNIT_EXPECT_EQ(test, left, right) \
1353 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1355 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
1356 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1357 KUNIT_EXPECTATION, \
1364 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
1365 * @test: The test context object.
1366 * @left: an arbitrary expression that evaluates to a pointer.
1367 * @right: an arbitrary expression that evaluates to a pointer.
1369 * Sets an expectation that the values that @left and @right evaluate to are
1370 * equal. This is semantically equivalent to
1371 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1374 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
1375 KUNIT_BINARY_PTR_EQ_ASSERTION(test, \
1376 KUNIT_EXPECTATION, \
1380 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1381 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1382 KUNIT_EXPECTATION, \
1389 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
1390 * @test: The test context object.
1391 * @left: an arbitrary expression that evaluates to a primitive C type.
1392 * @right: an arbitrary expression that evaluates to a primitive C type.
1394 * Sets an expectation that the values that @left and @right evaluate to are not
1395 * equal. This is semantically equivalent to
1396 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1399 #define KUNIT_EXPECT_NE(test, left, right) \
1400 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1402 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
1403 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1404 KUNIT_EXPECTATION, \
1411 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
1412 * @test: The test context object.
1413 * @left: an arbitrary expression that evaluates to a pointer.
1414 * @right: an arbitrary expression that evaluates to a pointer.
1416 * Sets an expectation that the values that @left and @right evaluate to are not
1417 * equal. This is semantically equivalent to
1418 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1421 #define KUNIT_EXPECT_PTR_NE(test, left, right) \
1422 KUNIT_BINARY_PTR_NE_ASSERTION(test, \
1423 KUNIT_EXPECTATION, \
1427 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
1428 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1429 KUNIT_EXPECTATION, \
1436 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
1437 * @test: The test context object.
1438 * @left: an arbitrary expression that evaluates to a primitive C type.
1439 * @right: an arbitrary expression that evaluates to a primitive C type.
1441 * Sets an expectation that the value that @left evaluates to is less than the
1442 * value that @right evaluates to. This is semantically equivalent to
1443 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
1446 #define KUNIT_EXPECT_LT(test, left, right) \
1447 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1449 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
1450 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1451 KUNIT_EXPECTATION, \
1458 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
1459 * @test: The test context object.
1460 * @left: an arbitrary expression that evaluates to a primitive C type.
1461 * @right: an arbitrary expression that evaluates to a primitive C type.
1463 * Sets an expectation that the value that @left evaluates to is less than or
1464 * equal to the value that @right evaluates to. Semantically this is equivalent
1465 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
1468 #define KUNIT_EXPECT_LE(test, left, right) \
1469 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1471 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
1472 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1473 KUNIT_EXPECTATION, \
1480 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
1481 * @test: The test context object.
1482 * @left: an arbitrary expression that evaluates to a primitive C type.
1483 * @right: an arbitrary expression that evaluates to a primitive C type.
1485 * Sets an expectation that the value that @left evaluates to is greater than
1486 * the value that @right evaluates to. This is semantically equivalent to
1487 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
1490 #define KUNIT_EXPECT_GT(test, left, right) \
1491 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1493 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
1494 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1495 KUNIT_EXPECTATION, \
1502 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1503 * @test: The test context object.
1504 * @left: an arbitrary expression that evaluates to a primitive C type.
1505 * @right: an arbitrary expression that evaluates to a primitive C type.
1507 * Sets an expectation that the value that @left evaluates to is greater than
1508 * the value that @right evaluates to. This is semantically equivalent to
1509 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1512 #define KUNIT_EXPECT_GE(test, left, right) \
1513 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1515 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
1516 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1517 KUNIT_EXPECTATION, \
1524 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1525 * @test: The test context object.
1526 * @left: an arbitrary expression that evaluates to a null terminated string.
1527 * @right: an arbitrary expression that evaluates to a null terminated string.
1529 * Sets an expectation that the values that @left and @right evaluate to are
1530 * equal. This is semantically equivalent to
1531 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1532 * for more information.
1534 #define KUNIT_EXPECT_STREQ(test, left, right) \
1535 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1537 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
1538 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1539 KUNIT_EXPECTATION, \
1546 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1547 * @test: The test context object.
1548 * @left: an arbitrary expression that evaluates to a null terminated string.
1549 * @right: an arbitrary expression that evaluates to a null terminated string.
1551 * Sets an expectation that the values that @left and @right evaluate to are
1552 * not equal. This is semantically equivalent to
1553 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1554 * for more information.
1556 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
1557 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1559 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
1560 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1561 KUNIT_EXPECTATION, \
1568 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1569 * @test: The test context object.
1570 * @ptr: an arbitrary pointer.
1572 * Sets an expectation that the value that @ptr evaluates to is not null and not
1573 * an errno stored in a pointer. This is semantically equivalent to
1574 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1577 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1578 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_EXPECTATION, ptr)
1580 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1581 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1582 KUNIT_EXPECTATION, \
1587 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1588 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1591 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1592 * @test: The test context object.
1593 * @condition: an arbitrary boolean expression. The test fails and aborts when
1594 * this does not evaluate to true.
1596 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1597 * fail *and immediately abort* when the specified condition is not met. Unlike
1598 * an expectation failure, it will prevent the test case from continuing to run;
1599 * this is otherwise known as an *assertion failure*.
1601 #define KUNIT_ASSERT_TRUE(test, condition) \
1602 KUNIT_TRUE_ASSERTION(test, KUNIT_ASSERTION, condition)
1604 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1605 KUNIT_TRUE_MSG_ASSERTION(test, \
1612 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1613 * @test: The test context object.
1614 * @condition: an arbitrary boolean expression.
1616 * Sets an assertion that the value that @condition evaluates to is false. This
1617 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1618 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1620 #define KUNIT_ASSERT_FALSE(test, condition) \
1621 KUNIT_FALSE_ASSERTION(test, KUNIT_ASSERTION, condition)
1623 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1624 KUNIT_FALSE_MSG_ASSERTION(test, \
1631 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1632 * @test: The test context object.
1633 * @left: an arbitrary expression that evaluates to a primitive C type.
1634 * @right: an arbitrary expression that evaluates to a primitive C type.
1636 * Sets an assertion that the values that @left and @right evaluate to are
1637 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1638 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1640 #define KUNIT_ASSERT_EQ(test, left, right) \
1641 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1643 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1644 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1652 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1653 * @test: The test context object.
1654 * @left: an arbitrary expression that evaluates to a pointer.
1655 * @right: an arbitrary expression that evaluates to a pointer.
1657 * Sets an assertion that the values that @left and @right evaluate to are
1658 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1659 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1661 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1662 KUNIT_BINARY_PTR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1664 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1665 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1673 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1674 * @test: The test context object.
1675 * @left: an arbitrary expression that evaluates to a primitive C type.
1676 * @right: an arbitrary expression that evaluates to a primitive C type.
1678 * Sets an assertion that the values that @left and @right evaluate to are not
1679 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1680 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1682 #define KUNIT_ASSERT_NE(test, left, right) \
1683 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1685 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1686 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1694 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1695 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1696 * @test: The test context object.
1697 * @left: an arbitrary expression that evaluates to a pointer.
1698 * @right: an arbitrary expression that evaluates to a pointer.
1700 * Sets an assertion that the values that @left and @right evaluate to are not
1701 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1702 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1704 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1705 KUNIT_BINARY_PTR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1707 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1708 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1715 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1716 * @test: The test context object.
1717 * @left: an arbitrary expression that evaluates to a primitive C type.
1718 * @right: an arbitrary expression that evaluates to a primitive C type.
1720 * Sets an assertion that the value that @left evaluates to is less than the
1721 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1722 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1725 #define KUNIT_ASSERT_LT(test, left, right) \
1726 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1728 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1729 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1736 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1737 * @test: The test context object.
1738 * @left: an arbitrary expression that evaluates to a primitive C type.
1739 * @right: an arbitrary expression that evaluates to a primitive C type.
1741 * Sets an assertion that the value that @left evaluates to is less than or
1742 * equal to the value that @right evaluates to. This is the same as
1743 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1744 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1746 #define KUNIT_ASSERT_LE(test, left, right) \
1747 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1749 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1750 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1758 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1759 * @test: The test context object.
1760 * @left: an arbitrary expression that evaluates to a primitive C type.
1761 * @right: an arbitrary expression that evaluates to a primitive C type.
1763 * Sets an assertion that the value that @left evaluates to is greater than the
1764 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1765 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1768 #define KUNIT_ASSERT_GT(test, left, right) \
1769 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1771 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1772 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1780 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1781 * @test: The test context object.
1782 * @left: an arbitrary expression that evaluates to a primitive C type.
1783 * @right: an arbitrary expression that evaluates to a primitive C type.
1785 * Sets an assertion that the value that @left evaluates to is greater than the
1786 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1787 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1790 #define KUNIT_ASSERT_GE(test, left, right) \
1791 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1793 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1794 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1802 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1803 * @test: The test context object.
1804 * @left: an arbitrary expression that evaluates to a null terminated string.
1805 * @right: an arbitrary expression that evaluates to a null terminated string.
1807 * Sets an assertion that the values that @left and @right evaluate to are
1808 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1809 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1811 #define KUNIT_ASSERT_STREQ(test, left, right) \
1812 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1814 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1815 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1823 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1824 * @test: The test context object.
1825 * @left: an arbitrary expression that evaluates to a null terminated string.
1826 * @right: an arbitrary expression that evaluates to a null terminated string.
1828 * Sets an expectation that the values that @left and @right evaluate to are
1829 * not equal. This is semantically equivalent to
1830 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1831 * for more information.
1833 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1834 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1836 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1837 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1845 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1846 * @test: The test context object.
1847 * @ptr: an arbitrary pointer.
1849 * Sets an assertion that the value that @ptr evaluates to is not null and not
1850 * an errno stored in a pointer. This is the same as
1851 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1852 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1854 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1855 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_ASSERTION, ptr)
1857 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1858 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1865 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1866 * @name: prefix for the test parameter generator function.
1867 * @array: array of test parameters.
1868 * @get_desc: function to convert param to description; NULL to use default
1870 * Define function @name_gen_params which uses @array to generate parameters.
1872 #define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1873 static const void *name##_gen_params(const void *prev, char *desc) \
1875 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1876 if (__next - (array) < ARRAY_SIZE((array))) { \
1877 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1879 __get_desc(__next, desc); \
1885 #endif /* _KUNIT_TEST_H */