1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Base unit test (KUnit) API.
5 * Copyright (C) 2019, Google LLC.
12 #include <kunit/assert.h>
13 #include <kunit/try-catch.h>
15 #include <linux/container_of.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/kconfig.h>
19 #include <linux/kref.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24 #include <linux/string.h>
25 #include <linux/types.h>
27 #include <asm/rwonce.h>
29 struct kunit_resource;
31 typedef int (*kunit_resource_init_t)(struct kunit_resource *, void *);
32 typedef void (*kunit_resource_free_t)(struct kunit_resource *);
35 * struct kunit_resource - represents a *test managed resource*
36 * @data: for the user to store arbitrary data.
37 * @name: optional name
38 * @free: a user supplied function to free the resource. Populated by
39 * kunit_resource_alloc().
41 * Represents a *test managed resource*, a resource which will automatically be
42 * cleaned up at the end of a test case.
44 * Resources are reference counted so if a resource is retrieved via
45 * kunit_alloc_and_get_resource() or kunit_find_resource(), we need
46 * to call kunit_put_resource() to reduce the resource reference count
47 * when finished with it. Note that kunit_alloc_resource() does not require a
48 * kunit_resource_put() because it does not retrieve the resource itself.
54 * struct kunit_kmalloc_params {
59 * static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
61 * struct kunit_kmalloc_params *params = context;
62 * res->data = kmalloc(params->size, params->gfp);
70 * static void kunit_kmalloc_free(struct kunit_resource *res)
75 * void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
77 * struct kunit_kmalloc_params params;
82 * return kunit_alloc_resource(test, kunit_kmalloc_init,
83 * kunit_kmalloc_free, ¶ms);
86 * Resources can also be named, with lookup/removal done on a name
87 * basis also. kunit_add_named_resource(), kunit_find_named_resource()
88 * and kunit_destroy_named_resource(). Resource names must be
89 * unique within the test instance.
91 struct kunit_resource {
94 kunit_resource_free_t free;
96 /* private: internal use only. */
98 struct list_head node;
103 /* Size of log associated with test. */
104 #define KUNIT_LOG_SIZE 512
106 /* Maximum size of parameter description string. */
107 #define KUNIT_PARAM_DESC_SIZE 128
109 /* Maximum size of a status comment. */
110 #define KUNIT_STATUS_COMMENT_SIZE 256
113 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
114 * sub-subtest. See the "Subtests" section in
115 * https://node-tap.org/tap-protocol/
117 #define KUNIT_SUBTEST_INDENT " "
118 #define KUNIT_SUBSUBTEST_INDENT " "
121 * enum kunit_status - Type of result for a test or test suite
122 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
123 * @KUNIT_FAILURE: Denotes the test has failed.
124 * @KUNIT_SKIPPED: Denotes the test has been skipped.
133 * struct kunit_case - represents an individual test case.
135 * @run_case: the function representing the actual test case.
136 * @name: the name of the test case.
137 * @generate_params: the generator function for parameterized tests.
139 * A test case is a function with the signature,
140 * ``void (*)(struct kunit *)``
141 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
142 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
143 * with a &struct kunit_suite and will be run after the suite's init
144 * function and followed by the suite's exit function.
146 * A test case should be static and should only be created with the
147 * KUNIT_CASE() macro; additionally, every array of test cases should be
148 * terminated with an empty test case.
154 * void add_test_basic(struct kunit *test)
156 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
157 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
158 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
159 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
160 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
163 * static struct kunit_case example_test_cases[] = {
164 * KUNIT_CASE(add_test_basic),
170 void (*run_case)(struct kunit *test);
172 const void* (*generate_params)(const void *prev, char *desc);
174 /* private: internal use only. */
175 enum kunit_status status;
179 static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
192 * KUNIT_CASE - A helper for creating a &struct kunit_case
194 * @test_name: a reference to a test case function.
196 * Takes a symbol for a function representing a test case and creates a
197 * &struct kunit_case object from it. See the documentation for
198 * &struct kunit_case for an example on how to use it.
200 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
203 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
205 * @test_name: a reference to a test case function.
206 * @gen_params: a reference to a parameter generator function.
208 * The generator function::
210 * const void* gen_params(const void *prev, char *desc)
212 * is used to lazily generate a series of arbitrarily typed values that fit into
213 * a void*. The argument @prev is the previously returned value, which should be
214 * used to derive the next value; @prev is set to NULL on the initial generator
215 * call. When no more values are available, the generator must return NULL.
216 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
217 * describing the parameter.
219 #define KUNIT_CASE_PARAM(test_name, gen_params) \
220 { .run_case = test_name, .name = #test_name, \
221 .generate_params = gen_params }
224 * struct kunit_suite - describes a related collection of &struct kunit_case
226 * @name: the name of the test. Purely informational.
227 * @init: called before every test case.
228 * @exit: called after every test case.
229 * @test_cases: a null terminated array of test cases.
231 * A kunit_suite is a collection of related &struct kunit_case s, such that
232 * @init is called before every test case and @exit is called after every
233 * test case, similar to the notion of a *test fixture* or a *test class*
234 * in other unit testing frameworks like JUnit or Googletest.
236 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
240 const char name[256];
241 int (*init)(struct kunit *test);
242 void (*exit)(struct kunit *test);
243 struct kunit_case *test_cases;
245 /* private: internal use only */
246 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
247 struct dentry *debugfs;
252 * struct kunit - represents a running instance of a test.
254 * @priv: for user to store arbitrary data. Commonly used to pass data
255 * created in the init function (see &struct kunit_suite).
257 * Used to store information about the current context under which the test
258 * is running. Most of this data is private and should only be accessed
259 * indirectly via public functions; the one exception is @priv which can be
260 * used by the test writer to store arbitrary data.
265 /* private: internal use only. */
266 const char *name; /* Read only after initialization! */
267 char *log; /* Points at case log after initialization */
268 struct kunit_try_catch try_catch;
269 /* param_value is the current parameter value for a test case. */
270 const void *param_value;
271 /* param_index stores the index of the parameter in parameterized tests. */
274 * success starts as true, and may only be set to false during a
275 * test case; thus, it is safe to update this across multiple
276 * threads using WRITE_ONCE; however, as a consequence, it may only
277 * be read after the test case finishes once all threads associated
278 * with the test case have terminated.
280 spinlock_t lock; /* Guards all mutable test state. */
281 enum kunit_status status; /* Read only after test_case finishes! */
283 * Because resources is a list that may be updated multiple times (with
284 * new resources) from any thread associated with a test case, we must
285 * protect it with some type of lock.
287 struct list_head resources; /* Protected by lock. */
289 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
292 static inline void kunit_set_failure(struct kunit *test)
294 WRITE_ONCE(test->status, KUNIT_FAILURE);
297 void kunit_init_test(struct kunit *test, const char *name, char *log);
299 int kunit_run_tests(struct kunit_suite *suite);
301 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
303 unsigned int kunit_test_case_num(struct kunit_suite *suite,
304 struct kunit_case *test_case);
306 int __kunit_test_suites_init(struct kunit_suite * const * const suites);
308 void __kunit_test_suites_exit(struct kunit_suite **suites);
310 #if IS_BUILTIN(CONFIG_KUNIT)
311 int kunit_run_all_tests(void);
313 static inline int kunit_run_all_tests(void)
317 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
321 * kunit_test_suites_for_module() - used to register one or more
322 * &struct kunit_suite with KUnit.
324 * @__suites: a statically allocated list of &struct kunit_suite.
326 * Registers @__suites with the test framework. See &struct kunit_suite for
329 * If a test suite is built-in, module_init() gets translated into
330 * an initcall which we don't want as the idea is that for builtins
331 * the executor will manage execution. So ensure we do not define
332 * module_{init|exit} functions for the builtin case when registering
333 * suites via kunit_test_suites() below.
335 #define kunit_test_suites_for_module(__suites) \
336 static int __init kunit_test_suites_init(void) \
338 return __kunit_test_suites_init(__suites); \
340 module_init(kunit_test_suites_init); \
342 static void __exit kunit_test_suites_exit(void) \
344 return __kunit_test_suites_exit(__suites); \
346 module_exit(kunit_test_suites_exit)
348 #define kunit_test_suites_for_module(__suites)
351 #define __kunit_test_suites(unique_array, unique_suites, ...) \
352 static struct kunit_suite *unique_array[] = { __VA_ARGS__, NULL }; \
353 kunit_test_suites_for_module(unique_array); \
354 static struct kunit_suite **unique_suites \
355 __used __section(".kunit_test_suites") = unique_array
358 * kunit_test_suites() - used to register one or more &struct kunit_suite
361 * @__suites: a statically allocated list of &struct kunit_suite.
363 * Registers @suites with the test framework. See &struct kunit_suite for
366 * When builtin, KUnit tests are all run via executor; this is done
367 * by placing the array of struct kunit_suite * in the .kunit_test_suites
370 * An alternative is to build the tests as a module. Because modules do not
371 * support multiple initcall()s, we need to initialize an array of suites for a
375 #define kunit_test_suites(__suites...) \
376 __kunit_test_suites(__UNIQUE_ID(array), \
377 __UNIQUE_ID(suites), \
380 #define kunit_test_suite(suite) kunit_test_suites(&suite)
382 #define kunit_suite_for_each_test_case(suite, test_case) \
383 for (test_case = suite->test_cases; test_case->run_case; test_case++)
385 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
388 * Like kunit_alloc_resource() below, but returns the struct kunit_resource
389 * object that contains the allocation. This is mostly for testing purposes.
391 struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
392 kunit_resource_init_t init,
393 kunit_resource_free_t free,
398 * kunit_get_resource() - Hold resource for use. Should not need to be used
399 * by most users as we automatically get resources
400 * retrieved by kunit_find_resource*().
403 static inline void kunit_get_resource(struct kunit_resource *res)
405 kref_get(&res->refcount);
409 * Called when refcount reaches zero via kunit_put_resources();
410 * should not be called directly.
412 static inline void kunit_release_resource(struct kref *kref)
414 struct kunit_resource *res = container_of(kref, struct kunit_resource,
417 /* If free function is defined, resource was dynamically allocated. */
425 * kunit_put_resource() - When caller is done with retrieved resource,
426 * kunit_put_resource() should be called to drop
427 * reference count. The resource list maintains
428 * a reference count on resources, so if no users
429 * are utilizing a resource and it is removed from
430 * the resource list, it will be freed via the
431 * associated free function (if any). Only
432 * needs to be used if we alloc_and_get() or
436 static inline void kunit_put_resource(struct kunit_resource *res)
438 kref_put(&res->refcount, kunit_release_resource);
442 * kunit_add_resource() - Add a *test managed resource*.
443 * @test: The test context object.
444 * @init: a user-supplied function to initialize the result (if needed). If
445 * none is supplied, the resource data value is simply set to @data.
446 * If an init function is supplied, @data is passed to it instead.
447 * @free: a user-supplied function to free the resource (if needed).
448 * @res: The resource.
449 * @data: value to pass to init function or set in resource data field.
451 int kunit_add_resource(struct kunit *test,
452 kunit_resource_init_t init,
453 kunit_resource_free_t free,
454 struct kunit_resource *res,
458 * kunit_add_named_resource() - Add a named *test managed resource*.
459 * @test: The test context object.
460 * @init: a user-supplied function to initialize the resource data, if needed.
461 * @free: a user-supplied function to free the resource data, if needed.
462 * @res: The resource.
463 * @name: name to be set for resource.
464 * @data: value to pass to init function or set in resource data field.
466 int kunit_add_named_resource(struct kunit *test,
467 kunit_resource_init_t init,
468 kunit_resource_free_t free,
469 struct kunit_resource *res,
474 * kunit_alloc_resource() - Allocates a *test managed resource*.
475 * @test: The test context object.
476 * @init: a user supplied function to initialize the resource.
477 * @free: a user supplied function to free the resource.
478 * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
479 * @context: for the user to pass in arbitrary data to the init function.
481 * Allocates a *test managed resource*, a resource which will automatically be
482 * cleaned up at the end of a test case. See &struct kunit_resource for an
485 * Note: KUnit needs to allocate memory for a kunit_resource object. You must
486 * specify an @internal_gfp that is compatible with the use context of your
489 static inline void *kunit_alloc_resource(struct kunit *test,
490 kunit_resource_init_t init,
491 kunit_resource_free_t free,
495 struct kunit_resource *res;
497 res = kzalloc(sizeof(*res), internal_gfp);
501 if (!kunit_add_resource(test, init, free, res, context))
507 typedef bool (*kunit_resource_match_t)(struct kunit *test,
508 struct kunit_resource *res,
512 * kunit_resource_instance_match() - Match a resource with the same instance.
513 * @test: Test case to which the resource belongs.
514 * @res: The resource.
515 * @match_data: The resource pointer to match against.
517 * An instance of kunit_resource_match_t that matches a resource whose
518 * allocation matches @match_data.
520 static inline bool kunit_resource_instance_match(struct kunit *test,
521 struct kunit_resource *res,
524 return res->data == match_data;
528 * kunit_resource_name_match() - Match a resource with the same name.
529 * @test: Test case to which the resource belongs.
530 * @res: The resource.
531 * @match_name: The name to match against.
533 static inline bool kunit_resource_name_match(struct kunit *test,
534 struct kunit_resource *res,
537 return res->name && strcmp(res->name, match_name) == 0;
541 * kunit_find_resource() - Find a resource using match function/data.
542 * @test: Test case to which the resource belongs.
543 * @match: match function to be applied to resources/match data.
544 * @match_data: data to be used in matching.
546 static inline struct kunit_resource *
547 kunit_find_resource(struct kunit *test,
548 kunit_resource_match_t match,
551 struct kunit_resource *res, *found = NULL;
554 spin_lock_irqsave(&test->lock, flags);
556 list_for_each_entry_reverse(res, &test->resources, node) {
557 if (match(test, res, (void *)match_data)) {
559 kunit_get_resource(found);
564 spin_unlock_irqrestore(&test->lock, flags);
570 * kunit_find_named_resource() - Find a resource using match name.
571 * @test: Test case to which the resource belongs.
574 static inline struct kunit_resource *
575 kunit_find_named_resource(struct kunit *test,
578 return kunit_find_resource(test, kunit_resource_name_match,
583 * kunit_destroy_resource() - Find a kunit_resource and destroy it.
584 * @test: Test case to which the resource belongs.
585 * @match: Match function. Returns whether a given resource matches @match_data.
586 * @match_data: Data passed into @match.
589 * 0 if kunit_resource is found and freed, -ENOENT if not found.
591 int kunit_destroy_resource(struct kunit *test,
592 kunit_resource_match_t match,
595 static inline int kunit_destroy_named_resource(struct kunit *test,
598 return kunit_destroy_resource(test, kunit_resource_name_match,
603 * kunit_remove_resource() - remove resource from resource list associated with
605 * @test: The test context object.
606 * @res: The resource to be removed.
608 * Note that the resource will not be immediately freed since it is likely
609 * the caller has a reference to it via alloc_and_get() or find();
610 * in this case a final call to kunit_put_resource() is required.
612 void kunit_remove_resource(struct kunit *test, struct kunit_resource *res);
615 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
616 * @test: The test context object.
617 * @n: number of elements.
618 * @size: The size in bytes of the desired memory.
619 * @gfp: flags passed to underlying kmalloc().
621 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
622 * and is automatically cleaned up after the test case concludes. See &struct
623 * kunit_resource for more information.
625 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
628 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
629 * @test: The test context object.
630 * @size: The size in bytes of the desired memory.
631 * @gfp: flags passed to underlying kmalloc().
633 * See kmalloc() and kunit_kmalloc_array() for more information.
635 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
637 return kunit_kmalloc_array(test, 1, size, gfp);
641 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
642 * @test: The test case to which the resource belongs.
643 * @ptr: The memory allocation to free.
645 void kunit_kfree(struct kunit *test, const void *ptr);
648 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
649 * @test: The test context object.
650 * @size: The size in bytes of the desired memory.
651 * @gfp: flags passed to underlying kmalloc().
653 * See kzalloc() and kunit_kmalloc_array() for more information.
655 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
657 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
661 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
662 * @test: The test context object.
663 * @n: number of elements.
664 * @size: The size in bytes of the desired memory.
665 * @gfp: flags passed to underlying kmalloc().
667 * See kcalloc() and kunit_kmalloc_array() for more information.
669 static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
671 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
674 void kunit_cleanup(struct kunit *test);
676 void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
679 * kunit_mark_skipped() - Marks @test_or_suite as skipped
681 * @test_or_suite: The test context object.
682 * @fmt: A printk() style format string.
684 * Marks the test as skipped. @fmt is given output as the test status
685 * comment, typically the reason the test was skipped.
687 * Test execution continues after kunit_mark_skipped() is called.
689 #define kunit_mark_skipped(test_or_suite, fmt, ...) \
691 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
692 scnprintf((test_or_suite)->status_comment, \
693 KUNIT_STATUS_COMMENT_SIZE, \
694 fmt, ##__VA_ARGS__); \
698 * kunit_skip() - Marks @test_or_suite as skipped
700 * @test_or_suite: The test context object.
701 * @fmt: A printk() style format string.
703 * Skips the test. @fmt is given output as the test status
704 * comment, typically the reason the test was skipped.
706 * Test execution is halted after kunit_skip() is called.
708 #define kunit_skip(test_or_suite, fmt, ...) \
710 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
711 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
715 * printk and log to per-test or per-suite log buffer. Logging only done
716 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
718 #define kunit_log(lvl, test_or_suite, fmt, ...) \
720 printk(lvl fmt, ##__VA_ARGS__); \
721 kunit_log_append((test_or_suite)->log, fmt "\n", \
725 #define kunit_printk(lvl, test, fmt, ...) \
726 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
727 (test)->name, ##__VA_ARGS__)
730 * kunit_info() - Prints an INFO level message associated with @test.
732 * @test: The test context object.
733 * @fmt: A printk() style format string.
735 * Prints an info level message associated with the test suite being run.
736 * Takes a variable number of format parameters just like printk().
738 #define kunit_info(test, fmt, ...) \
739 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
742 * kunit_warn() - Prints a WARN level message associated with @test.
744 * @test: The test context object.
745 * @fmt: A printk() style format string.
747 * Prints a warning level message.
749 #define kunit_warn(test, fmt, ...) \
750 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
753 * kunit_err() - Prints an ERROR level message associated with @test.
755 * @test: The test context object.
756 * @fmt: A printk() style format string.
758 * Prints an error level message.
760 #define kunit_err(test, fmt, ...) \
761 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
764 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
765 * @test: The test context object.
767 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
768 * words, it does nothing and only exists for code clarity. See
769 * KUNIT_EXPECT_TRUE() for more information.
771 #define KUNIT_SUCCEED(test) do {} while (0)
773 void kunit_do_assertion(struct kunit *test,
774 struct kunit_assert *assert,
776 const char *fmt, ...);
778 #define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do { \
779 struct assert_class __assertion = INITIALIZER; \
780 kunit_do_assertion(test, \
781 &__assertion.assert, \
788 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
789 KUNIT_ASSERTION(test, \
792 KUNIT_INIT_FAIL_ASSERT_STRUCT(test, assert_type), \
797 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
798 * @test: The test context object.
799 * @fmt: an informational message to be printed when the assertion is made.
800 * @...: string format arguments.
802 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
803 * other words, it always results in a failed expectation, and consequently
804 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
805 * for more information.
807 #define KUNIT_FAIL(test, fmt, ...) \
808 KUNIT_FAIL_ASSERTION(test, \
813 #define KUNIT_UNARY_ASSERTION(test, \
819 KUNIT_ASSERTION(test, \
820 !!(condition) == !!expected_true, \
821 kunit_unary_assert, \
822 KUNIT_INIT_UNARY_ASSERT_STRUCT(test, \
829 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
830 KUNIT_UNARY_ASSERTION(test, \
837 #define KUNIT_TRUE_ASSERTION(test, assert_type, condition) \
838 KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, NULL)
840 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
841 KUNIT_UNARY_ASSERTION(test, \
848 #define KUNIT_FALSE_ASSERTION(test, assert_type, condition) \
849 KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, NULL)
852 * A factory macro for defining the assertions and expectations for the basic
853 * comparisons defined for the built in types.
855 * Unfortunately, there is no common type that all types can be promoted to for
856 * which all the binary operators behave the same way as for the actual types
857 * (for example, there is no type that long long and unsigned long long can
858 * both be cast to where the comparison result is preserved for all values). So
859 * the best we can do is do the comparison in the original types and then coerce
860 * everything to long long for printing; this way, the comparison behaves
861 * correctly and the printed out value usually makes sense without
862 * interpretation, but can always be interpreted to figure out the actual
865 #define KUNIT_BASE_BINARY_ASSERTION(test, \
875 typeof(left) __left = (left); \
876 typeof(right) __right = (right); \
878 KUNIT_ASSERTION(test, \
881 ASSERT_CLASS_INIT(test, \
892 #define KUNIT_BASE_EQ_MSG_ASSERTION(test, \
900 KUNIT_BASE_BINARY_ASSERTION(test, \
908 #define KUNIT_BASE_NE_MSG_ASSERTION(test, \
916 KUNIT_BASE_BINARY_ASSERTION(test, \
924 #define KUNIT_BASE_LT_MSG_ASSERTION(test, \
932 KUNIT_BASE_BINARY_ASSERTION(test, \
940 #define KUNIT_BASE_LE_MSG_ASSERTION(test, \
948 KUNIT_BASE_BINARY_ASSERTION(test, \
956 #define KUNIT_BASE_GT_MSG_ASSERTION(test, \
964 KUNIT_BASE_BINARY_ASSERTION(test, \
972 #define KUNIT_BASE_GE_MSG_ASSERTION(test, \
980 KUNIT_BASE_BINARY_ASSERTION(test, \
988 #define KUNIT_BINARY_EQ_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
989 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
990 kunit_binary_assert, \
991 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
998 #define KUNIT_BINARY_EQ_ASSERTION(test, assert_type, left, right) \
999 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1005 #define KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1011 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
1012 kunit_binary_ptr_assert, \
1013 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1020 #define KUNIT_BINARY_PTR_EQ_ASSERTION(test, assert_type, left, right) \
1021 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1027 #define KUNIT_BINARY_NE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1028 KUNIT_BASE_NE_MSG_ASSERTION(test, \
1029 kunit_binary_assert, \
1030 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1037 #define KUNIT_BINARY_NE_ASSERTION(test, assert_type, left, right) \
1038 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1044 #define KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1050 KUNIT_BASE_NE_MSG_ASSERTION(test, \
1051 kunit_binary_ptr_assert, \
1052 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1059 #define KUNIT_BINARY_PTR_NE_ASSERTION(test, assert_type, left, right) \
1060 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1066 #define KUNIT_BINARY_LT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1067 KUNIT_BASE_LT_MSG_ASSERTION(test, \
1068 kunit_binary_assert, \
1069 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1076 #define KUNIT_BINARY_LT_ASSERTION(test, assert_type, left, right) \
1077 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1083 #define KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
1089 KUNIT_BASE_LT_MSG_ASSERTION(test, \
1090 kunit_binary_ptr_assert, \
1091 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1098 #define KUNIT_BINARY_PTR_LT_ASSERTION(test, assert_type, left, right) \
1099 KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
1105 #define KUNIT_BINARY_LE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1106 KUNIT_BASE_LE_MSG_ASSERTION(test, \
1107 kunit_binary_assert, \
1108 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1115 #define KUNIT_BINARY_LE_ASSERTION(test, assert_type, left, right) \
1116 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1122 #define KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
1128 KUNIT_BASE_LE_MSG_ASSERTION(test, \
1129 kunit_binary_ptr_assert, \
1130 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1137 #define KUNIT_BINARY_PTR_LE_ASSERTION(test, assert_type, left, right) \
1138 KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
1144 #define KUNIT_BINARY_GT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1145 KUNIT_BASE_GT_MSG_ASSERTION(test, \
1146 kunit_binary_assert, \
1147 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1154 #define KUNIT_BINARY_GT_ASSERTION(test, assert_type, left, right) \
1155 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1161 #define KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
1167 KUNIT_BASE_GT_MSG_ASSERTION(test, \
1168 kunit_binary_ptr_assert, \
1169 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1176 #define KUNIT_BINARY_PTR_GT_ASSERTION(test, assert_type, left, right) \
1177 KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
1183 #define KUNIT_BINARY_GE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1184 KUNIT_BASE_GE_MSG_ASSERTION(test, \
1185 kunit_binary_assert, \
1186 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1193 #define KUNIT_BINARY_GE_ASSERTION(test, assert_type, left, right) \
1194 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1200 #define KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
1206 KUNIT_BASE_GE_MSG_ASSERTION(test, \
1207 kunit_binary_ptr_assert, \
1208 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1215 #define KUNIT_BINARY_PTR_GE_ASSERTION(test, assert_type, left, right) \
1216 KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
1222 #define KUNIT_BINARY_STR_ASSERTION(test, \
1230 const char *__left = (left); \
1231 const char *__right = (right); \
1233 KUNIT_ASSERTION(test, \
1234 strcmp(__left, __right) op 0, \
1235 kunit_binary_str_assert, \
1236 KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(test, \
1247 #define KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1253 KUNIT_BINARY_STR_ASSERTION(test, \
1259 #define KUNIT_BINARY_STR_EQ_ASSERTION(test, assert_type, left, right) \
1260 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1266 #define KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1272 KUNIT_BINARY_STR_ASSERTION(test, \
1278 #define KUNIT_BINARY_STR_NE_ASSERTION(test, assert_type, left, right) \
1279 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1285 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1291 typeof(ptr) __ptr = (ptr); \
1293 KUNIT_ASSERTION(test, \
1294 !IS_ERR_OR_NULL(__ptr), \
1295 kunit_ptr_not_err_assert, \
1296 KUNIT_INIT_PTR_NOT_ERR_STRUCT(test, \
1304 #define KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, assert_type, ptr) \
1305 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1311 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
1312 * @test: The test context object.
1313 * @condition: an arbitrary boolean expression. The test fails when this does
1314 * not evaluate to true.
1316 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
1317 * to fail when the specified condition is not met; however, it will not prevent
1318 * the test case from continuing to run; this is otherwise known as an
1319 * *expectation failure*.
1321 #define KUNIT_EXPECT_TRUE(test, condition) \
1322 KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)
1324 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
1325 KUNIT_TRUE_MSG_ASSERTION(test, \
1326 KUNIT_EXPECTATION, \
1332 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
1333 * @test: The test context object.
1334 * @condition: an arbitrary boolean expression. The test fails when this does
1335 * not evaluate to false.
1337 * Sets an expectation that @condition evaluates to false. See
1338 * KUNIT_EXPECT_TRUE() for more information.
1340 #define KUNIT_EXPECT_FALSE(test, condition) \
1341 KUNIT_FALSE_ASSERTION(test, KUNIT_EXPECTATION, condition)
1343 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
1344 KUNIT_FALSE_MSG_ASSERTION(test, \
1345 KUNIT_EXPECTATION, \
1351 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
1352 * @test: The test context object.
1353 * @left: an arbitrary expression that evaluates to a primitive C type.
1354 * @right: an arbitrary expression that evaluates to a primitive C type.
1356 * Sets an expectation that the values that @left and @right evaluate to are
1357 * equal. This is semantically equivalent to
1358 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1361 #define KUNIT_EXPECT_EQ(test, left, right) \
1362 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1364 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
1365 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1366 KUNIT_EXPECTATION, \
1373 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
1374 * @test: The test context object.
1375 * @left: an arbitrary expression that evaluates to a pointer.
1376 * @right: an arbitrary expression that evaluates to a pointer.
1378 * Sets an expectation that the values that @left and @right evaluate to are
1379 * equal. This is semantically equivalent to
1380 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1383 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
1384 KUNIT_BINARY_PTR_EQ_ASSERTION(test, \
1385 KUNIT_EXPECTATION, \
1389 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1390 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1391 KUNIT_EXPECTATION, \
1398 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
1399 * @test: The test context object.
1400 * @left: an arbitrary expression that evaluates to a primitive C type.
1401 * @right: an arbitrary expression that evaluates to a primitive C type.
1403 * Sets an expectation that the values that @left and @right evaluate to are not
1404 * equal. This is semantically equivalent to
1405 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1408 #define KUNIT_EXPECT_NE(test, left, right) \
1409 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1411 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
1412 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1413 KUNIT_EXPECTATION, \
1420 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
1421 * @test: The test context object.
1422 * @left: an arbitrary expression that evaluates to a pointer.
1423 * @right: an arbitrary expression that evaluates to a pointer.
1425 * Sets an expectation that the values that @left and @right evaluate to are not
1426 * equal. This is semantically equivalent to
1427 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1430 #define KUNIT_EXPECT_PTR_NE(test, left, right) \
1431 KUNIT_BINARY_PTR_NE_ASSERTION(test, \
1432 KUNIT_EXPECTATION, \
1436 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
1437 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1438 KUNIT_EXPECTATION, \
1445 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
1446 * @test: The test context object.
1447 * @left: an arbitrary expression that evaluates to a primitive C type.
1448 * @right: an arbitrary expression that evaluates to a primitive C type.
1450 * Sets an expectation that the value that @left evaluates to is less than the
1451 * value that @right evaluates to. This is semantically equivalent to
1452 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
1455 #define KUNIT_EXPECT_LT(test, left, right) \
1456 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1458 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
1459 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1460 KUNIT_EXPECTATION, \
1467 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
1468 * @test: The test context object.
1469 * @left: an arbitrary expression that evaluates to a primitive C type.
1470 * @right: an arbitrary expression that evaluates to a primitive C type.
1472 * Sets an expectation that the value that @left evaluates to is less than or
1473 * equal to the value that @right evaluates to. Semantically this is equivalent
1474 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
1477 #define KUNIT_EXPECT_LE(test, left, right) \
1478 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1480 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
1481 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1482 KUNIT_EXPECTATION, \
1489 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
1490 * @test: The test context object.
1491 * @left: an arbitrary expression that evaluates to a primitive C type.
1492 * @right: an arbitrary expression that evaluates to a primitive C type.
1494 * Sets an expectation that the value that @left evaluates to is greater than
1495 * the value that @right evaluates to. This is semantically equivalent to
1496 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
1499 #define KUNIT_EXPECT_GT(test, left, right) \
1500 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1502 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
1503 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1504 KUNIT_EXPECTATION, \
1511 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1512 * @test: The test context object.
1513 * @left: an arbitrary expression that evaluates to a primitive C type.
1514 * @right: an arbitrary expression that evaluates to a primitive C type.
1516 * Sets an expectation that the value that @left evaluates to is greater than
1517 * the value that @right evaluates to. This is semantically equivalent to
1518 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1521 #define KUNIT_EXPECT_GE(test, left, right) \
1522 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1524 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
1525 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1526 KUNIT_EXPECTATION, \
1533 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1534 * @test: The test context object.
1535 * @left: an arbitrary expression that evaluates to a null terminated string.
1536 * @right: an arbitrary expression that evaluates to a null terminated string.
1538 * Sets an expectation that the values that @left and @right evaluate to are
1539 * equal. This is semantically equivalent to
1540 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1541 * for more information.
1543 #define KUNIT_EXPECT_STREQ(test, left, right) \
1544 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1546 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
1547 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1548 KUNIT_EXPECTATION, \
1555 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1556 * @test: The test context object.
1557 * @left: an arbitrary expression that evaluates to a null terminated string.
1558 * @right: an arbitrary expression that evaluates to a null terminated string.
1560 * Sets an expectation that the values that @left and @right evaluate to are
1561 * not equal. This is semantically equivalent to
1562 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1563 * for more information.
1565 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
1566 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1568 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
1569 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1570 KUNIT_EXPECTATION, \
1577 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1578 * @test: The test context object.
1579 * @ptr: an arbitrary pointer.
1581 * Sets an expectation that the value that @ptr evaluates to is not null and not
1582 * an errno stored in a pointer. This is semantically equivalent to
1583 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1586 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1587 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_EXPECTATION, ptr)
1589 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1590 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1591 KUNIT_EXPECTATION, \
1596 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1597 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1600 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1601 * @test: The test context object.
1602 * @condition: an arbitrary boolean expression. The test fails and aborts when
1603 * this does not evaluate to true.
1605 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1606 * fail *and immediately abort* when the specified condition is not met. Unlike
1607 * an expectation failure, it will prevent the test case from continuing to run;
1608 * this is otherwise known as an *assertion failure*.
1610 #define KUNIT_ASSERT_TRUE(test, condition) \
1611 KUNIT_TRUE_ASSERTION(test, KUNIT_ASSERTION, condition)
1613 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1614 KUNIT_TRUE_MSG_ASSERTION(test, \
1621 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1622 * @test: The test context object.
1623 * @condition: an arbitrary boolean expression.
1625 * Sets an assertion that the value that @condition evaluates to is false. This
1626 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1627 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1629 #define KUNIT_ASSERT_FALSE(test, condition) \
1630 KUNIT_FALSE_ASSERTION(test, KUNIT_ASSERTION, condition)
1632 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1633 KUNIT_FALSE_MSG_ASSERTION(test, \
1640 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1641 * @test: The test context object.
1642 * @left: an arbitrary expression that evaluates to a primitive C type.
1643 * @right: an arbitrary expression that evaluates to a primitive C type.
1645 * Sets an assertion that the values that @left and @right evaluate to are
1646 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1647 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1649 #define KUNIT_ASSERT_EQ(test, left, right) \
1650 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1652 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1653 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1661 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1662 * @test: The test context object.
1663 * @left: an arbitrary expression that evaluates to a pointer.
1664 * @right: an arbitrary expression that evaluates to a pointer.
1666 * Sets an assertion that the values that @left and @right evaluate to are
1667 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1668 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1670 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1671 KUNIT_BINARY_PTR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1673 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1674 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1682 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1683 * @test: The test context object.
1684 * @left: an arbitrary expression that evaluates to a primitive C type.
1685 * @right: an arbitrary expression that evaluates to a primitive C type.
1687 * Sets an assertion that the values that @left and @right evaluate to are not
1688 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1689 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1691 #define KUNIT_ASSERT_NE(test, left, right) \
1692 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1694 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1695 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1703 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1704 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1705 * @test: The test context object.
1706 * @left: an arbitrary expression that evaluates to a pointer.
1707 * @right: an arbitrary expression that evaluates to a pointer.
1709 * Sets an assertion that the values that @left and @right evaluate to are not
1710 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1711 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1713 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1714 KUNIT_BINARY_PTR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1716 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1717 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1724 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1725 * @test: The test context object.
1726 * @left: an arbitrary expression that evaluates to a primitive C type.
1727 * @right: an arbitrary expression that evaluates to a primitive C type.
1729 * Sets an assertion that the value that @left evaluates to is less than the
1730 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1731 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1734 #define KUNIT_ASSERT_LT(test, left, right) \
1735 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1737 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1738 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1745 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1746 * @test: The test context object.
1747 * @left: an arbitrary expression that evaluates to a primitive C type.
1748 * @right: an arbitrary expression that evaluates to a primitive C type.
1750 * Sets an assertion that the value that @left evaluates to is less than or
1751 * equal to the value that @right evaluates to. This is the same as
1752 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1753 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1755 #define KUNIT_ASSERT_LE(test, left, right) \
1756 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1758 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1759 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1767 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1768 * @test: The test context object.
1769 * @left: an arbitrary expression that evaluates to a primitive C type.
1770 * @right: an arbitrary expression that evaluates to a primitive C type.
1772 * Sets an assertion that the value that @left evaluates to is greater than the
1773 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1774 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1777 #define KUNIT_ASSERT_GT(test, left, right) \
1778 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1780 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1781 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1789 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1790 * @test: The test context object.
1791 * @left: an arbitrary expression that evaluates to a primitive C type.
1792 * @right: an arbitrary expression that evaluates to a primitive C type.
1794 * Sets an assertion that the value that @left evaluates to is greater than the
1795 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1796 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1799 #define KUNIT_ASSERT_GE(test, left, right) \
1800 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1802 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1803 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1811 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1812 * @test: The test context object.
1813 * @left: an arbitrary expression that evaluates to a null terminated string.
1814 * @right: an arbitrary expression that evaluates to a null terminated string.
1816 * Sets an assertion that the values that @left and @right evaluate to are
1817 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1818 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1820 #define KUNIT_ASSERT_STREQ(test, left, right) \
1821 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1823 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1824 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1832 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1833 * @test: The test context object.
1834 * @left: an arbitrary expression that evaluates to a null terminated string.
1835 * @right: an arbitrary expression that evaluates to a null terminated string.
1837 * Sets an expectation that the values that @left and @right evaluate to are
1838 * not equal. This is semantically equivalent to
1839 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1840 * for more information.
1842 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1843 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1845 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1846 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1854 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1855 * @test: The test context object.
1856 * @ptr: an arbitrary pointer.
1858 * Sets an assertion that the value that @ptr evaluates to is not null and not
1859 * an errno stored in a pointer. This is the same as
1860 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1861 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1863 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1864 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_ASSERTION, ptr)
1866 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1867 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1874 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1875 * @name: prefix for the test parameter generator function.
1876 * @array: array of test parameters.
1877 * @get_desc: function to convert param to description; NULL to use default
1879 * Define function @name_gen_params which uses @array to generate parameters.
1881 #define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1882 static const void *name##_gen_params(const void *prev, char *desc) \
1884 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1885 if (__next - (array) < ARRAY_SIZE((array))) { \
1886 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1888 __get_desc(__next, desc); \
1894 #endif /* _KUNIT_TEST_H */