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
101 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
102 * sub-subtest. See the "Subtests" section in
103 * https://node-tap.org/tap-protocol/
105 #define KUNIT_SUBTEST_INDENT " "
106 #define KUNIT_SUBSUBTEST_INDENT " "
109 * struct kunit_case - represents an individual test case.
111 * @run_case: the function representing the actual test case.
112 * @name: the name of the test case.
113 * @generate_params: the generator function for parameterized tests.
115 * A test case is a function with the signature,
116 * ``void (*)(struct kunit *)``
117 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
118 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
119 * with a &struct kunit_suite and will be run after the suite's init
120 * function and followed by the suite's exit function.
122 * A test case should be static and should only be created with the
123 * KUNIT_CASE() macro; additionally, every array of test cases should be
124 * terminated with an empty test case.
130 * void add_test_basic(struct kunit *test)
132 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
133 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
134 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
135 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
136 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
139 * static struct kunit_case example_test_cases[] = {
140 * KUNIT_CASE(add_test_basic),
146 void (*run_case
)(struct kunit
*test
);
148 const void* (*generate_params
)(const void *prev
, char *desc
);
150 /* private: internal use only. */
155 static inline char *kunit_status_to_string(bool status
)
157 return status
? "ok" : "not ok";
161 * KUNIT_CASE - A helper for creating a &struct kunit_case
163 * @test_name: a reference to a test case function.
165 * Takes a symbol for a function representing a test case and creates a
166 * &struct kunit_case object from it. See the documentation for
167 * &struct kunit_case for an example on how to use it.
169 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
172 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
174 * @test_name: a reference to a test case function.
175 * @gen_params: a reference to a parameter generator function.
177 * The generator function::
179 * const void* gen_params(const void *prev, char *desc)
181 * is used to lazily generate a series of arbitrarily typed values that fit into
182 * a void*. The argument @prev is the previously returned value, which should be
183 * used to derive the next value; @prev is set to NULL on the initial generator
184 * call. When no more values are available, the generator must return NULL.
185 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
186 * describing the parameter.
188 #define KUNIT_CASE_PARAM(test_name, gen_params) \
189 { .run_case = test_name, .name = #test_name, \
190 .generate_params = gen_params }
193 * struct kunit_suite - describes a related collection of &struct kunit_case
195 * @name: the name of the test. Purely informational.
196 * @init: called before every test case.
197 * @exit: called after every test case.
198 * @test_cases: a null terminated array of test cases.
200 * A kunit_suite is a collection of related &struct kunit_case s, such that
201 * @init is called before every test case and @exit is called after every
202 * test case, similar to the notion of a *test fixture* or a *test class*
203 * in other unit testing frameworks like JUnit or Googletest.
205 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
209 const char name
[256];
210 int (*init
)(struct kunit
*test
);
211 void (*exit
)(struct kunit
*test
);
212 struct kunit_case
*test_cases
;
214 /* private: internal use only */
215 struct dentry
*debugfs
;
220 * struct kunit - represents a running instance of a test.
222 * @priv: for user to store arbitrary data. Commonly used to pass data
223 * created in the init function (see &struct kunit_suite).
225 * Used to store information about the current context under which the test
226 * is running. Most of this data is private and should only be accessed
227 * indirectly via public functions; the one exception is @priv which can be
228 * used by the test writer to store arbitrary data.
233 /* private: internal use only. */
234 const char *name
; /* Read only after initialization! */
235 char *log
; /* Points at case log after initialization */
236 struct kunit_try_catch try_catch
;
237 /* param_value is the current parameter value for a test case. */
238 const void *param_value
;
239 /* param_index stores the index of the parameter in parameterized tests. */
242 * success starts as true, and may only be set to false during a
243 * test case; thus, it is safe to update this across multiple
244 * threads using WRITE_ONCE; however, as a consequence, it may only
245 * be read after the test case finishes once all threads associated
246 * with the test case have terminated.
248 bool success
; /* Read only after test_case finishes! */
249 spinlock_t lock
; /* Guards all mutable test state. */
251 * Because resources is a list that may be updated multiple times (with
252 * new resources) from any thread associated with a test case, we must
253 * protect it with some type of lock.
255 struct list_head resources
; /* Protected by lock. */
258 static inline void kunit_set_failure(struct kunit
*test
)
260 WRITE_ONCE(test
->success
, false);
263 void kunit_init_test(struct kunit
*test
, const char *name
, char *log
);
265 int kunit_run_tests(struct kunit_suite
*suite
);
267 size_t kunit_suite_num_test_cases(struct kunit_suite
*suite
);
269 unsigned int kunit_test_case_num(struct kunit_suite
*suite
,
270 struct kunit_case
*test_case
);
272 int __kunit_test_suites_init(struct kunit_suite
* const * const suites
);
274 void __kunit_test_suites_exit(struct kunit_suite
**suites
);
276 #if IS_BUILTIN(CONFIG_KUNIT)
277 int kunit_run_all_tests(void);
279 static inline int kunit_run_all_tests(void)
283 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
287 * kunit_test_suites_for_module() - used to register one or more
288 * &struct kunit_suite with KUnit.
290 * @__suites: a statically allocated list of &struct kunit_suite.
292 * Registers @__suites with the test framework. See &struct kunit_suite for
295 * If a test suite is built-in, module_init() gets translated into
296 * an initcall which we don't want as the idea is that for builtins
297 * the executor will manage execution. So ensure we do not define
298 * module_{init|exit} functions for the builtin case when registering
299 * suites via kunit_test_suites() below.
301 #define kunit_test_suites_for_module(__suites) \
302 static int __init kunit_test_suites_init(void) \
304 return __kunit_test_suites_init(__suites); \
306 module_init(kunit_test_suites_init); \
308 static void __exit kunit_test_suites_exit(void) \
310 return __kunit_test_suites_exit(__suites); \
312 module_exit(kunit_test_suites_exit)
314 #define kunit_test_suites_for_module(__suites)
317 #define __kunit_test_suites(unique_array, unique_suites, ...) \
318 static struct kunit_suite *unique_array[] = { __VA_ARGS__, NULL }; \
319 kunit_test_suites_for_module(unique_array); \
320 static struct kunit_suite **unique_suites \
321 __used __section(".kunit_test_suites") = unique_array
324 * kunit_test_suites() - used to register one or more &struct kunit_suite
327 * @__suites: a statically allocated list of &struct kunit_suite.
329 * Registers @suites with the test framework. See &struct kunit_suite for
332 * When builtin, KUnit tests are all run via executor; this is done
333 * by placing the array of struct kunit_suite * in the .kunit_test_suites
336 * An alternative is to build the tests as a module. Because modules do not
337 * support multiple initcall()s, we need to initialize an array of suites for a
341 #define kunit_test_suites(__suites...) \
342 __kunit_test_suites(__UNIQUE_ID(array), \
343 __UNIQUE_ID(suites), \
346 #define kunit_test_suite(suite) kunit_test_suites(&suite)
348 #define kunit_suite_for_each_test_case(suite, test_case) \
349 for (test_case = suite->test_cases; test_case->run_case; test_case++)
351 bool kunit_suite_has_succeeded(struct kunit_suite
*suite
);
354 * Like kunit_alloc_resource() below, but returns the struct kunit_resource
355 * object that contains the allocation. This is mostly for testing purposes.
357 struct kunit_resource
*kunit_alloc_and_get_resource(struct kunit
*test
,
358 kunit_resource_init_t init
,
359 kunit_resource_free_t free
,
364 * kunit_get_resource() - Hold resource for use. Should not need to be used
365 * by most users as we automatically get resources
366 * retrieved by kunit_find_resource*().
369 static inline void kunit_get_resource(struct kunit_resource
*res
)
371 kref_get(&res
->refcount
);
375 * Called when refcount reaches zero via kunit_put_resources();
376 * should not be called directly.
378 static inline void kunit_release_resource(struct kref
*kref
)
380 struct kunit_resource
*res
= container_of(kref
, struct kunit_resource
,
383 /* If free function is defined, resource was dynamically allocated. */
391 * kunit_put_resource() - When caller is done with retrieved resource,
392 * kunit_put_resource() should be called to drop
393 * reference count. The resource list maintains
394 * a reference count on resources, so if no users
395 * are utilizing a resource and it is removed from
396 * the resource list, it will be freed via the
397 * associated free function (if any). Only
398 * needs to be used if we alloc_and_get() or
402 static inline void kunit_put_resource(struct kunit_resource
*res
)
404 kref_put(&res
->refcount
, kunit_release_resource
);
408 * kunit_add_resource() - Add a *test managed resource*.
409 * @test: The test context object.
410 * @init: a user-supplied function to initialize the result (if needed). If
411 * none is supplied, the resource data value is simply set to @data.
412 * If an init function is supplied, @data is passed to it instead.
413 * @free: a user-supplied function to free the resource (if needed).
414 * @res: The resource.
415 * @data: value to pass to init function or set in resource data field.
417 int kunit_add_resource(struct kunit
*test
,
418 kunit_resource_init_t init
,
419 kunit_resource_free_t free
,
420 struct kunit_resource
*res
,
424 * kunit_add_named_resource() - Add a named *test managed resource*.
425 * @test: The test context object.
426 * @init: a user-supplied function to initialize the resource data, if needed.
427 * @free: a user-supplied function to free the resource data, if needed.
428 * @res: The resource.
429 * @name: name to be set for resource.
430 * @data: value to pass to init function or set in resource data field.
432 int kunit_add_named_resource(struct kunit
*test
,
433 kunit_resource_init_t init
,
434 kunit_resource_free_t free
,
435 struct kunit_resource
*res
,
440 * kunit_alloc_resource() - Allocates a *test managed resource*.
441 * @test: The test context object.
442 * @init: a user supplied function to initialize the resource.
443 * @free: a user supplied function to free the resource.
444 * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
445 * @context: for the user to pass in arbitrary data to the init function.
447 * Allocates a *test managed resource*, a resource which will automatically be
448 * cleaned up at the end of a test case. See &struct kunit_resource for an
451 * Note: KUnit needs to allocate memory for a kunit_resource object. You must
452 * specify an @internal_gfp that is compatible with the use context of your
455 static inline void *kunit_alloc_resource(struct kunit
*test
,
456 kunit_resource_init_t init
,
457 kunit_resource_free_t free
,
461 struct kunit_resource
*res
;
463 res
= kzalloc(sizeof(*res
), internal_gfp
);
467 if (!kunit_add_resource(test
, init
, free
, res
, context
))
473 typedef bool (*kunit_resource_match_t
)(struct kunit
*test
,
474 struct kunit_resource
*res
,
478 * kunit_resource_instance_match() - Match a resource with the same instance.
479 * @test: Test case to which the resource belongs.
480 * @res: The resource.
481 * @match_data: The resource pointer to match against.
483 * An instance of kunit_resource_match_t that matches a resource whose
484 * allocation matches @match_data.
486 static inline bool kunit_resource_instance_match(struct kunit
*test
,
487 struct kunit_resource
*res
,
490 return res
->data
== match_data
;
494 * kunit_resource_name_match() - Match a resource with the same name.
495 * @test: Test case to which the resource belongs.
496 * @res: The resource.
497 * @match_name: The name to match against.
499 static inline bool kunit_resource_name_match(struct kunit
*test
,
500 struct kunit_resource
*res
,
503 return res
->name
&& strcmp(res
->name
, match_name
) == 0;
507 * kunit_find_resource() - Find a resource using match function/data.
508 * @test: Test case to which the resource belongs.
509 * @match: match function to be applied to resources/match data.
510 * @match_data: data to be used in matching.
512 static inline struct kunit_resource
*
513 kunit_find_resource(struct kunit
*test
,
514 kunit_resource_match_t match
,
517 struct kunit_resource
*res
, *found
= NULL
;
519 spin_lock(&test
->lock
);
521 list_for_each_entry_reverse(res
, &test
->resources
, node
) {
522 if (match(test
, res
, (void *)match_data
)) {
524 kunit_get_resource(found
);
529 spin_unlock(&test
->lock
);
535 * kunit_find_named_resource() - Find a resource using match name.
536 * @test: Test case to which the resource belongs.
539 static inline struct kunit_resource
*
540 kunit_find_named_resource(struct kunit
*test
,
543 return kunit_find_resource(test
, kunit_resource_name_match
,
548 * kunit_destroy_resource() - Find a kunit_resource and destroy it.
549 * @test: Test case to which the resource belongs.
550 * @match: Match function. Returns whether a given resource matches @match_data.
551 * @match_data: Data passed into @match.
554 * 0 if kunit_resource is found and freed, -ENOENT if not found.
556 int kunit_destroy_resource(struct kunit
*test
,
557 kunit_resource_match_t match
,
560 static inline int kunit_destroy_named_resource(struct kunit
*test
,
563 return kunit_destroy_resource(test
, kunit_resource_name_match
,
568 * kunit_remove_resource() - remove resource from resource list associated with
570 * @test: The test context object.
571 * @res: The resource to be removed.
573 * Note that the resource will not be immediately freed since it is likely
574 * the caller has a reference to it via alloc_and_get() or find();
575 * in this case a final call to kunit_put_resource() is required.
577 void kunit_remove_resource(struct kunit
*test
, struct kunit_resource
*res
);
580 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
581 * @test: The test context object.
582 * @size: The size in bytes of the desired memory.
583 * @gfp: flags passed to underlying kmalloc().
585 * Just like `kmalloc(...)`, except the allocation is managed by the test case
586 * and is automatically cleaned up after the test case concludes. See &struct
587 * kunit_resource for more information.
589 void *kunit_kmalloc(struct kunit
*test
, size_t size
, gfp_t gfp
);
592 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
593 * @test: The test case to which the resource belongs.
594 * @ptr: The memory allocation to free.
596 void kunit_kfree(struct kunit
*test
, const void *ptr
);
599 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
600 * @test: The test context object.
601 * @size: The size in bytes of the desired memory.
602 * @gfp: flags passed to underlying kmalloc().
604 * See kzalloc() and kunit_kmalloc() for more information.
606 static inline void *kunit_kzalloc(struct kunit
*test
, size_t size
, gfp_t gfp
)
608 return kunit_kmalloc(test
, size
, gfp
| __GFP_ZERO
);
611 void kunit_cleanup(struct kunit
*test
);
613 void kunit_log_append(char *log
, const char *fmt
, ...);
616 * printk and log to per-test or per-suite log buffer. Logging only done
617 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
619 #define kunit_log(lvl, test_or_suite, fmt, ...) \
621 printk(lvl fmt, ##__VA_ARGS__); \
622 kunit_log_append((test_or_suite)->log, fmt "\n", \
626 #define kunit_printk(lvl, test, fmt, ...) \
627 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
628 (test)->name, ##__VA_ARGS__)
631 * kunit_info() - Prints an INFO level message associated with @test.
633 * @test: The test context object.
634 * @fmt: A printk() style format string.
636 * Prints an info level message associated with the test suite being run.
637 * Takes a variable number of format parameters just like printk().
639 #define kunit_info(test, fmt, ...) \
640 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
643 * kunit_warn() - Prints a WARN level message associated with @test.
645 * @test: The test context object.
646 * @fmt: A printk() style format string.
648 * Prints a warning level message.
650 #define kunit_warn(test, fmt, ...) \
651 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
654 * kunit_err() - Prints an ERROR level message associated with @test.
656 * @test: The test context object.
657 * @fmt: A printk() style format string.
659 * Prints an error level message.
661 #define kunit_err(test, fmt, ...) \
662 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
665 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
666 * @test: The test context object.
668 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
669 * words, it does nothing and only exists for code clarity. See
670 * KUNIT_EXPECT_TRUE() for more information.
672 #define KUNIT_SUCCEED(test) do {} while (0)
674 void kunit_do_assertion(struct kunit
*test
,
675 struct kunit_assert
*assert,
677 const char *fmt
, ...);
679 #define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do { \
680 struct assert_class __assertion = INITIALIZER; \
681 kunit_do_assertion(test, \
682 &__assertion.assert, \
689 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
690 KUNIT_ASSERTION(test, \
693 KUNIT_INIT_FAIL_ASSERT_STRUCT(test, assert_type), \
698 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
699 * @test: The test context object.
700 * @fmt: an informational message to be printed when the assertion is made.
701 * @...: string format arguments.
703 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
704 * other words, it always results in a failed expectation, and consequently
705 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
706 * for more information.
708 #define KUNIT_FAIL(test, fmt, ...) \
709 KUNIT_FAIL_ASSERTION(test, \
714 #define KUNIT_UNARY_ASSERTION(test, \
720 KUNIT_ASSERTION(test, \
721 !!(condition) == !!expected_true, \
722 kunit_unary_assert, \
723 KUNIT_INIT_UNARY_ASSERT_STRUCT(test, \
730 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
731 KUNIT_UNARY_ASSERTION(test, \
738 #define KUNIT_TRUE_ASSERTION(test, assert_type, condition) \
739 KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, NULL)
741 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
742 KUNIT_UNARY_ASSERTION(test, \
749 #define KUNIT_FALSE_ASSERTION(test, assert_type, condition) \
750 KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, NULL)
753 * A factory macro for defining the assertions and expectations for the basic
754 * comparisons defined for the built in types.
756 * Unfortunately, there is no common type that all types can be promoted to for
757 * which all the binary operators behave the same way as for the actual types
758 * (for example, there is no type that long long and unsigned long long can
759 * both be cast to where the comparison result is preserved for all values). So
760 * the best we can do is do the comparison in the original types and then coerce
761 * everything to long long for printing; this way, the comparison behaves
762 * correctly and the printed out value usually makes sense without
763 * interpretation, but can always be interpreted to figure out the actual
766 #define KUNIT_BASE_BINARY_ASSERTION(test, \
776 typeof(left) __left = (left); \
777 typeof(right) __right = (right); \
778 ((void)__typecheck(__left, __right)); \
780 KUNIT_ASSERTION(test, \
783 ASSERT_CLASS_INIT(test, \
794 #define KUNIT_BASE_EQ_MSG_ASSERTION(test, \
802 KUNIT_BASE_BINARY_ASSERTION(test, \
810 #define KUNIT_BASE_NE_MSG_ASSERTION(test, \
818 KUNIT_BASE_BINARY_ASSERTION(test, \
826 #define KUNIT_BASE_LT_MSG_ASSERTION(test, \
834 KUNIT_BASE_BINARY_ASSERTION(test, \
842 #define KUNIT_BASE_LE_MSG_ASSERTION(test, \
850 KUNIT_BASE_BINARY_ASSERTION(test, \
858 #define KUNIT_BASE_GT_MSG_ASSERTION(test, \
866 KUNIT_BASE_BINARY_ASSERTION(test, \
874 #define KUNIT_BASE_GE_MSG_ASSERTION(test, \
882 KUNIT_BASE_BINARY_ASSERTION(test, \
890 #define KUNIT_BINARY_EQ_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
891 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
892 kunit_binary_assert, \
893 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
900 #define KUNIT_BINARY_EQ_ASSERTION(test, assert_type, left, right) \
901 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
907 #define KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
913 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
914 kunit_binary_ptr_assert, \
915 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
922 #define KUNIT_BINARY_PTR_EQ_ASSERTION(test, assert_type, left, right) \
923 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
929 #define KUNIT_BINARY_NE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
930 KUNIT_BASE_NE_MSG_ASSERTION(test, \
931 kunit_binary_assert, \
932 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
939 #define KUNIT_BINARY_NE_ASSERTION(test, assert_type, left, right) \
940 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
946 #define KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
952 KUNIT_BASE_NE_MSG_ASSERTION(test, \
953 kunit_binary_ptr_assert, \
954 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
961 #define KUNIT_BINARY_PTR_NE_ASSERTION(test, assert_type, left, right) \
962 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
968 #define KUNIT_BINARY_LT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
969 KUNIT_BASE_LT_MSG_ASSERTION(test, \
970 kunit_binary_assert, \
971 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
978 #define KUNIT_BINARY_LT_ASSERTION(test, assert_type, left, right) \
979 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
985 #define KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
991 KUNIT_BASE_LT_MSG_ASSERTION(test, \
992 kunit_binary_ptr_assert, \
993 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1000 #define KUNIT_BINARY_PTR_LT_ASSERTION(test, assert_type, left, right) \
1001 KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
1007 #define KUNIT_BINARY_LE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1008 KUNIT_BASE_LE_MSG_ASSERTION(test, \
1009 kunit_binary_assert, \
1010 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1017 #define KUNIT_BINARY_LE_ASSERTION(test, assert_type, left, right) \
1018 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1024 #define KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
1030 KUNIT_BASE_LE_MSG_ASSERTION(test, \
1031 kunit_binary_ptr_assert, \
1032 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1039 #define KUNIT_BINARY_PTR_LE_ASSERTION(test, assert_type, left, right) \
1040 KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
1046 #define KUNIT_BINARY_GT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1047 KUNIT_BASE_GT_MSG_ASSERTION(test, \
1048 kunit_binary_assert, \
1049 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1056 #define KUNIT_BINARY_GT_ASSERTION(test, assert_type, left, right) \
1057 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1063 #define KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
1069 KUNIT_BASE_GT_MSG_ASSERTION(test, \
1070 kunit_binary_ptr_assert, \
1071 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1078 #define KUNIT_BINARY_PTR_GT_ASSERTION(test, assert_type, left, right) \
1079 KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
1085 #define KUNIT_BINARY_GE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1086 KUNIT_BASE_GE_MSG_ASSERTION(test, \
1087 kunit_binary_assert, \
1088 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1095 #define KUNIT_BINARY_GE_ASSERTION(test, assert_type, left, right) \
1096 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1102 #define KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
1108 KUNIT_BASE_GE_MSG_ASSERTION(test, \
1109 kunit_binary_ptr_assert, \
1110 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1117 #define KUNIT_BINARY_PTR_GE_ASSERTION(test, assert_type, left, right) \
1118 KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
1124 #define KUNIT_BINARY_STR_ASSERTION(test, \
1132 typeof(left) __left = (left); \
1133 typeof(right) __right = (right); \
1135 KUNIT_ASSERTION(test, \
1136 strcmp(__left, __right) op 0, \
1137 kunit_binary_str_assert, \
1138 KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(test, \
1149 #define KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1155 KUNIT_BINARY_STR_ASSERTION(test, \
1161 #define KUNIT_BINARY_STR_EQ_ASSERTION(test, assert_type, left, right) \
1162 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1168 #define KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1174 KUNIT_BINARY_STR_ASSERTION(test, \
1180 #define KUNIT_BINARY_STR_NE_ASSERTION(test, assert_type, left, right) \
1181 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1187 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1193 typeof(ptr) __ptr = (ptr); \
1195 KUNIT_ASSERTION(test, \
1196 !IS_ERR_OR_NULL(__ptr), \
1197 kunit_ptr_not_err_assert, \
1198 KUNIT_INIT_PTR_NOT_ERR_STRUCT(test, \
1206 #define KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, assert_type, ptr) \
1207 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1213 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
1214 * @test: The test context object.
1215 * @condition: an arbitrary boolean expression. The test fails when this does
1216 * not evaluate to true.
1218 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
1219 * to fail when the specified condition is not met; however, it will not prevent
1220 * the test case from continuing to run; this is otherwise known as an
1221 * *expectation failure*.
1223 #define KUNIT_EXPECT_TRUE(test, condition) \
1224 KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)
1226 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
1227 KUNIT_TRUE_MSG_ASSERTION(test, \
1228 KUNIT_EXPECTATION, \
1234 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
1235 * @test: The test context object.
1236 * @condition: an arbitrary boolean expression. The test fails when this does
1237 * not evaluate to false.
1239 * Sets an expectation that @condition evaluates to false. See
1240 * KUNIT_EXPECT_TRUE() for more information.
1242 #define KUNIT_EXPECT_FALSE(test, condition) \
1243 KUNIT_FALSE_ASSERTION(test, KUNIT_EXPECTATION, condition)
1245 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
1246 KUNIT_FALSE_MSG_ASSERTION(test, \
1247 KUNIT_EXPECTATION, \
1253 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
1254 * @test: The test context object.
1255 * @left: an arbitrary expression that evaluates to a primitive C type.
1256 * @right: an arbitrary expression that evaluates to a primitive C type.
1258 * Sets an expectation that the values that @left and @right evaluate to are
1259 * equal. This is semantically equivalent to
1260 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1263 #define KUNIT_EXPECT_EQ(test, left, right) \
1264 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1266 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
1267 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1268 KUNIT_EXPECTATION, \
1275 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
1276 * @test: The test context object.
1277 * @left: an arbitrary expression that evaluates to a pointer.
1278 * @right: an arbitrary expression that evaluates to a pointer.
1280 * Sets an expectation that the values that @left and @right evaluate to are
1281 * equal. This is semantically equivalent to
1282 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1285 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
1286 KUNIT_BINARY_PTR_EQ_ASSERTION(test, \
1287 KUNIT_EXPECTATION, \
1291 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1292 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1293 KUNIT_EXPECTATION, \
1300 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
1301 * @test: The test context object.
1302 * @left: an arbitrary expression that evaluates to a primitive C type.
1303 * @right: an arbitrary expression that evaluates to a primitive C type.
1305 * Sets an expectation that the values that @left and @right evaluate to are not
1306 * equal. This is semantically equivalent to
1307 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1310 #define KUNIT_EXPECT_NE(test, left, right) \
1311 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1313 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
1314 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1315 KUNIT_EXPECTATION, \
1322 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
1323 * @test: The test context object.
1324 * @left: an arbitrary expression that evaluates to a pointer.
1325 * @right: an arbitrary expression that evaluates to a pointer.
1327 * Sets an expectation that the values that @left and @right evaluate to are not
1328 * equal. This is semantically equivalent to
1329 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1332 #define KUNIT_EXPECT_PTR_NE(test, left, right) \
1333 KUNIT_BINARY_PTR_NE_ASSERTION(test, \
1334 KUNIT_EXPECTATION, \
1338 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
1339 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1340 KUNIT_EXPECTATION, \
1347 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
1348 * @test: The test context object.
1349 * @left: an arbitrary expression that evaluates to a primitive C type.
1350 * @right: an arbitrary expression that evaluates to a primitive C type.
1352 * Sets an expectation that the value that @left evaluates to is less than the
1353 * value that @right evaluates to. This is semantically equivalent to
1354 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
1357 #define KUNIT_EXPECT_LT(test, left, right) \
1358 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1360 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
1361 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1362 KUNIT_EXPECTATION, \
1369 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
1370 * @test: The test context object.
1371 * @left: an arbitrary expression that evaluates to a primitive C type.
1372 * @right: an arbitrary expression that evaluates to a primitive C type.
1374 * Sets an expectation that the value that @left evaluates to is less than or
1375 * equal to the value that @right evaluates to. Semantically this is equivalent
1376 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
1379 #define KUNIT_EXPECT_LE(test, left, right) \
1380 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1382 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
1383 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1384 KUNIT_EXPECTATION, \
1391 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
1392 * @test: The test context object.
1393 * @left: an arbitrary expression that evaluates to a primitive C type.
1394 * @right: an arbitrary expression that evaluates to a primitive C type.
1396 * Sets an expectation that the value that @left evaluates to is greater than
1397 * the value that @right evaluates to. This is semantically equivalent to
1398 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
1401 #define KUNIT_EXPECT_GT(test, left, right) \
1402 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1404 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
1405 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1406 KUNIT_EXPECTATION, \
1413 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1414 * @test: The test context object.
1415 * @left: an arbitrary expression that evaluates to a primitive C type.
1416 * @right: an arbitrary expression that evaluates to a primitive C type.
1418 * Sets an expectation that the value that @left evaluates to is greater than
1419 * the value that @right evaluates to. This is semantically equivalent to
1420 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1423 #define KUNIT_EXPECT_GE(test, left, right) \
1424 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1426 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
1427 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1428 KUNIT_EXPECTATION, \
1435 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1436 * @test: The test context object.
1437 * @left: an arbitrary expression that evaluates to a null terminated string.
1438 * @right: an arbitrary expression that evaluates to a null terminated string.
1440 * Sets an expectation that the values that @left and @right evaluate to are
1441 * equal. This is semantically equivalent to
1442 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1443 * for more information.
1445 #define KUNIT_EXPECT_STREQ(test, left, right) \
1446 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1448 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
1449 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1450 KUNIT_EXPECTATION, \
1457 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1458 * @test: The test context object.
1459 * @left: an arbitrary expression that evaluates to a null terminated string.
1460 * @right: an arbitrary expression that evaluates to a null terminated string.
1462 * Sets an expectation that the values that @left and @right evaluate to are
1463 * not equal. This is semantically equivalent to
1464 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1465 * for more information.
1467 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
1468 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1470 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
1471 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1472 KUNIT_EXPECTATION, \
1479 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1480 * @test: The test context object.
1481 * @ptr: an arbitrary pointer.
1483 * Sets an expectation that the value that @ptr evaluates to is not null and not
1484 * an errno stored in a pointer. This is semantically equivalent to
1485 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1488 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1489 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_EXPECTATION, ptr)
1491 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1492 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1493 KUNIT_EXPECTATION, \
1498 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1499 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1502 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1503 * @test: The test context object.
1504 * @condition: an arbitrary boolean expression. The test fails and aborts when
1505 * this does not evaluate to true.
1507 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1508 * fail *and immediately abort* when the specified condition is not met. Unlike
1509 * an expectation failure, it will prevent the test case from continuing to run;
1510 * this is otherwise known as an *assertion failure*.
1512 #define KUNIT_ASSERT_TRUE(test, condition) \
1513 KUNIT_TRUE_ASSERTION(test, KUNIT_ASSERTION, condition)
1515 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1516 KUNIT_TRUE_MSG_ASSERTION(test, \
1523 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1524 * @test: The test context object.
1525 * @condition: an arbitrary boolean expression.
1527 * Sets an assertion that the value that @condition evaluates to is false. This
1528 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1529 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1531 #define KUNIT_ASSERT_FALSE(test, condition) \
1532 KUNIT_FALSE_ASSERTION(test, KUNIT_ASSERTION, condition)
1534 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1535 KUNIT_FALSE_MSG_ASSERTION(test, \
1542 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1543 * @test: The test context object.
1544 * @left: an arbitrary expression that evaluates to a primitive C type.
1545 * @right: an arbitrary expression that evaluates to a primitive C type.
1547 * Sets an assertion that the values that @left and @right evaluate to are
1548 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1549 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1551 #define KUNIT_ASSERT_EQ(test, left, right) \
1552 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1554 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1555 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1563 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1564 * @test: The test context object.
1565 * @left: an arbitrary expression that evaluates to a pointer.
1566 * @right: an arbitrary expression that evaluates to a pointer.
1568 * Sets an assertion that the values that @left and @right evaluate to are
1569 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1570 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1572 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1573 KUNIT_BINARY_PTR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1575 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1576 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1584 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1585 * @test: The test context object.
1586 * @left: an arbitrary expression that evaluates to a primitive C type.
1587 * @right: an arbitrary expression that evaluates to a primitive C type.
1589 * Sets an assertion that the values that @left and @right evaluate to are not
1590 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1591 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1593 #define KUNIT_ASSERT_NE(test, left, right) \
1594 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1596 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1597 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1605 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1606 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1607 * @test: The test context object.
1608 * @left: an arbitrary expression that evaluates to a pointer.
1609 * @right: an arbitrary expression that evaluates to a pointer.
1611 * Sets an assertion that the values that @left and @right evaluate to are not
1612 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1613 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1615 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1616 KUNIT_BINARY_PTR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1618 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1619 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1626 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1627 * @test: The test context object.
1628 * @left: an arbitrary expression that evaluates to a primitive C type.
1629 * @right: an arbitrary expression that evaluates to a primitive C type.
1631 * Sets an assertion that the value that @left evaluates to is less than the
1632 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1633 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1636 #define KUNIT_ASSERT_LT(test, left, right) \
1637 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1639 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1640 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1647 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1648 * @test: The test context object.
1649 * @left: an arbitrary expression that evaluates to a primitive C type.
1650 * @right: an arbitrary expression that evaluates to a primitive C type.
1652 * Sets an assertion that the value that @left evaluates to is less than or
1653 * equal to the value that @right evaluates to. This is the same as
1654 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1655 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1657 #define KUNIT_ASSERT_LE(test, left, right) \
1658 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1660 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1661 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1669 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1670 * @test: The test context object.
1671 * @left: an arbitrary expression that evaluates to a primitive C type.
1672 * @right: an arbitrary expression that evaluates to a primitive C type.
1674 * Sets an assertion that the value that @left evaluates to is greater than the
1675 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1676 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1679 #define KUNIT_ASSERT_GT(test, left, right) \
1680 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1682 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1683 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1691 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1692 * @test: The test context object.
1693 * @left: an arbitrary expression that evaluates to a primitive C type.
1694 * @right: an arbitrary expression that evaluates to a primitive C type.
1696 * Sets an assertion that the value that @left evaluates to is greater than the
1697 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1698 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1701 #define KUNIT_ASSERT_GE(test, left, right) \
1702 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1704 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1705 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1713 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1714 * @test: The test context object.
1715 * @left: an arbitrary expression that evaluates to a null terminated string.
1716 * @right: an arbitrary expression that evaluates to a null terminated string.
1718 * Sets an assertion that the values that @left and @right evaluate to are
1719 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1720 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1722 #define KUNIT_ASSERT_STREQ(test, left, right) \
1723 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1725 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1726 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1734 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1735 * @test: The test context object.
1736 * @left: an arbitrary expression that evaluates to a null terminated string.
1737 * @right: an arbitrary expression that evaluates to a null terminated string.
1739 * Sets an expectation that the values that @left and @right evaluate to are
1740 * not equal. This is semantically equivalent to
1741 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1742 * for more information.
1744 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1745 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1747 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1748 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1756 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1757 * @test: The test context object.
1758 * @ptr: an arbitrary pointer.
1760 * Sets an assertion that the value that @ptr evaluates to is not null and not
1761 * an errno stored in a pointer. This is the same as
1762 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1763 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1765 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1766 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_ASSERTION, ptr)
1768 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1769 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1776 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1777 * @name: prefix for the test parameter generator function.
1778 * @array: array of test parameters.
1779 * @get_desc: function to convert param to description; NULL to use default
1781 * Define function @name_gen_params which uses @array to generate parameters.
1783 #define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1784 static const void *name##_gen_params(const void *prev, char *desc) \
1786 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1787 if (__next - (array) < ARRAY_SIZE((array))) { \
1788 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1790 __get_desc(__next, desc); \
1796 #endif /* _KUNIT_TEST_H */