xref: /openbmc/linux/include/kunit/test.h (revision e0d07278)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Base unit test (KUnit) API.
4  *
5  * Copyright (C) 2019, Google LLC.
6  * Author: Brendan Higgins <brendanhiggins@google.com>
7  */
8 
9 #ifndef _KUNIT_TEST_H
10 #define _KUNIT_TEST_H
11 
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>
19 
20 struct kunit_resource;
21 
22 typedef int (*kunit_resource_init_t)(struct kunit_resource *, void *);
23 typedef void (*kunit_resource_free_t)(struct kunit_resource *);
24 
25 /**
26  * struct kunit_resource - represents a *test managed resource*
27  * @data: for the user to store arbitrary data.
28  * @free: a user supplied function to free the resource. Populated by
29  * kunit_resource_alloc().
30  *
31  * Represents a *test managed resource*, a resource which will automatically be
32  * cleaned up at the end of a test case.
33  *
34  * Resources are reference counted so if a resource is retrieved via
35  * kunit_alloc_and_get_resource() or kunit_find_resource(), we need
36  * to call kunit_put_resource() to reduce the resource reference count
37  * when finished with it.  Note that kunit_alloc_resource() does not require a
38  * kunit_resource_put() because it does not retrieve the resource itself.
39  *
40  * Example:
41  *
42  * .. code-block:: c
43  *
44  *	struct kunit_kmalloc_params {
45  *		size_t size;
46  *		gfp_t gfp;
47  *	};
48  *
49  *	static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
50  *	{
51  *		struct kunit_kmalloc_params *params = context;
52  *		res->data = kmalloc(params->size, params->gfp);
53  *
54  *		if (!res->data)
55  *			return -ENOMEM;
56  *
57  *		return 0;
58  *	}
59  *
60  *	static void kunit_kmalloc_free(struct kunit_resource *res)
61  *	{
62  *		kfree(res->data);
63  *	}
64  *
65  *	void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
66  *	{
67  *		struct kunit_kmalloc_params params;
68  *
69  *		params.size = size;
70  *		params.gfp = gfp;
71  *
72  *		return kunit_alloc_resource(test, kunit_kmalloc_init,
73  *			kunit_kmalloc_free, &params);
74  *	}
75  *
76  * Resources can also be named, with lookup/removal done on a name
77  * basis also.  kunit_add_named_resource(), kunit_find_named_resource()
78  * and kunit_destroy_named_resource().  Resource names must be
79  * unique within the test instance.
80  */
81 struct kunit_resource {
82 	void *data;
83 	const char *name;		/* optional name */
84 
85 	/* private: internal use only. */
86 	kunit_resource_free_t free;
87 	struct kref refcount;
88 	struct list_head node;
89 };
90 
91 struct kunit;
92 
93 /* Size of log associated with test. */
94 #define KUNIT_LOG_SIZE	512
95 
96 /*
97  * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
98  * sub-subtest.  See the "Subtests" section in
99  * https://node-tap.org/tap-protocol/
100  */
101 #define KUNIT_SUBTEST_INDENT		"    "
102 #define KUNIT_SUBSUBTEST_INDENT		"        "
103 
104 /**
105  * struct kunit_case - represents an individual test case.
106  *
107  * @run_case: the function representing the actual test case.
108  * @name:     the name of the test case.
109  *
110  * A test case is a function with the signature,
111  * ``void (*)(struct kunit *)``
112  * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
113  * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
114  * with a &struct kunit_suite and will be run after the suite's init
115  * function and followed by the suite's exit function.
116  *
117  * A test case should be static and should only be created with the
118  * KUNIT_CASE() macro; additionally, every array of test cases should be
119  * terminated with an empty test case.
120  *
121  * Example:
122  *
123  * .. code-block:: c
124  *
125  *	void add_test_basic(struct kunit *test)
126  *	{
127  *		KUNIT_EXPECT_EQ(test, 1, add(1, 0));
128  *		KUNIT_EXPECT_EQ(test, 2, add(1, 1));
129  *		KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
130  *		KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
131  *		KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
132  *	}
133  *
134  *	static struct kunit_case example_test_cases[] = {
135  *		KUNIT_CASE(add_test_basic),
136  *		{}
137  *	};
138  *
139  */
140 struct kunit_case {
141 	void (*run_case)(struct kunit *test);
142 	const char *name;
143 
144 	/* private: internal use only. */
145 	bool success;
146 	char *log;
147 };
148 
149 static inline char *kunit_status_to_string(bool status)
150 {
151 	return status ? "ok" : "not ok";
152 }
153 
154 /**
155  * KUNIT_CASE - A helper for creating a &struct kunit_case
156  *
157  * @test_name: a reference to a test case function.
158  *
159  * Takes a symbol for a function representing a test case and creates a
160  * &struct kunit_case object from it. See the documentation for
161  * &struct kunit_case for an example on how to use it.
162  */
163 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
164 
165 /**
166  * struct kunit_suite - describes a related collection of &struct kunit_case
167  *
168  * @name:	the name of the test. Purely informational.
169  * @init:	called before every test case.
170  * @exit:	called after every test case.
171  * @test_cases:	a null terminated array of test cases.
172  *
173  * A kunit_suite is a collection of related &struct kunit_case s, such that
174  * @init is called before every test case and @exit is called after every
175  * test case, similar to the notion of a *test fixture* or a *test class*
176  * in other unit testing frameworks like JUnit or Googletest.
177  *
178  * Every &struct kunit_case must be associated with a kunit_suite for KUnit
179  * to run it.
180  */
181 struct kunit_suite {
182 	const char name[256];
183 	int (*init)(struct kunit *test);
184 	void (*exit)(struct kunit *test);
185 	struct kunit_case *test_cases;
186 
187 	/* private: internal use only */
188 	struct dentry *debugfs;
189 	char *log;
190 };
191 
192 /**
193  * struct kunit - represents a running instance of a test.
194  *
195  * @priv: for user to store arbitrary data. Commonly used to pass data
196  *	  created in the init function (see &struct kunit_suite).
197  *
198  * Used to store information about the current context under which the test
199  * is running. Most of this data is private and should only be accessed
200  * indirectly via public functions; the one exception is @priv which can be
201  * used by the test writer to store arbitrary data.
202  */
203 struct kunit {
204 	void *priv;
205 
206 	/* private: internal use only. */
207 	const char *name; /* Read only after initialization! */
208 	char *log; /* Points at case log after initialization */
209 	struct kunit_try_catch try_catch;
210 	/*
211 	 * success starts as true, and may only be set to false during a
212 	 * test case; thus, it is safe to update this across multiple
213 	 * threads using WRITE_ONCE; however, as a consequence, it may only
214 	 * be read after the test case finishes once all threads associated
215 	 * with the test case have terminated.
216 	 */
217 	bool success; /* Read only after test_case finishes! */
218 	spinlock_t lock; /* Guards all mutable test state. */
219 	/*
220 	 * Because resources is a list that may be updated multiple times (with
221 	 * new resources) from any thread associated with a test case, we must
222 	 * protect it with some type of lock.
223 	 */
224 	struct list_head resources; /* Protected by lock. */
225 };
226 
227 void kunit_init_test(struct kunit *test, const char *name, char *log);
228 
229 int kunit_run_tests(struct kunit_suite *suite);
230 
231 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
232 
233 unsigned int kunit_test_case_num(struct kunit_suite *suite,
234 				 struct kunit_case *test_case);
235 
236 int __kunit_test_suites_init(struct kunit_suite **suites);
237 
238 void __kunit_test_suites_exit(struct kunit_suite **suites);
239 
240 /**
241  * kunit_test_suites() - used to register one or more &struct kunit_suite
242  *			 with KUnit.
243  *
244  * @suites_list...: a statically allocated list of &struct kunit_suite.
245  *
246  * Registers @suites_list with the test framework. See &struct kunit_suite for
247  * more information.
248  *
249  * When builtin, KUnit tests are all run as late_initcalls; this means
250  * that they cannot test anything where tests must run at a different init
251  * phase. One significant restriction resulting from this is that KUnit
252  * cannot reliably test anything that is initialize in the late_init phase;
253  * another is that KUnit is useless to test things that need to be run in
254  * an earlier init phase.
255  *
256  * An alternative is to build the tests as a module.  Because modules
257  * do not support multiple late_initcall()s, we need to initialize an
258  * array of suites for a module.
259  *
260  * TODO(brendanhiggins@google.com): Don't run all KUnit tests as
261  * late_initcalls.  I have some future work planned to dispatch all KUnit
262  * tests from the same place, and at the very least to do so after
263  * everything else is definitely initialized.
264  */
265 #define kunit_test_suites(suites_list...)				\
266 	static struct kunit_suite *suites[] = {suites_list, NULL};	\
267 	static int kunit_test_suites_init(void)				\
268 	{								\
269 		return __kunit_test_suites_init(suites);		\
270 	}								\
271 	late_initcall(kunit_test_suites_init);				\
272 	static void __exit kunit_test_suites_exit(void)			\
273 	{								\
274 		return __kunit_test_suites_exit(suites);		\
275 	}								\
276 	module_exit(kunit_test_suites_exit)
277 
278 #define kunit_test_suite(suite)	kunit_test_suites(&suite)
279 
280 #define kunit_suite_for_each_test_case(suite, test_case)		\
281 	for (test_case = suite->test_cases; test_case->run_case; test_case++)
282 
283 bool kunit_suite_has_succeeded(struct kunit_suite *suite);
284 
285 /*
286  * Like kunit_alloc_resource() below, but returns the struct kunit_resource
287  * object that contains the allocation. This is mostly for testing purposes.
288  */
289 struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
290 						    kunit_resource_init_t init,
291 						    kunit_resource_free_t free,
292 						    gfp_t internal_gfp,
293 						    void *context);
294 
295 /**
296  * kunit_get_resource() - Hold resource for use.  Should not need to be used
297  *			  by most users as we automatically get resources
298  *			  retrieved by kunit_find_resource*().
299  * @res: resource
300  */
301 static inline void kunit_get_resource(struct kunit_resource *res)
302 {
303 	kref_get(&res->refcount);
304 }
305 
306 /*
307  * Called when refcount reaches zero via kunit_put_resources();
308  * should not be called directly.
309  */
310 static inline void kunit_release_resource(struct kref *kref)
311 {
312 	struct kunit_resource *res = container_of(kref, struct kunit_resource,
313 						  refcount);
314 
315 	/* If free function is defined, resource was dynamically allocated. */
316 	if (res->free) {
317 		res->free(res);
318 		kfree(res);
319 	}
320 }
321 
322 /**
323  * kunit_put_resource() - When caller is done with retrieved resource,
324  *			  kunit_put_resource() should be called to drop
325  *			  reference count.  The resource list maintains
326  *			  a reference count on resources, so if no users
327  *			  are utilizing a resource and it is removed from
328  *			  the resource list, it will be freed via the
329  *			  associated free function (if any).  Only
330  *			  needs to be used if we alloc_and_get() or
331  *			  find() resource.
332  * @res: resource
333  */
334 static inline void kunit_put_resource(struct kunit_resource *res)
335 {
336 	kref_put(&res->refcount, kunit_release_resource);
337 }
338 
339 /**
340  * kunit_add_resource() - Add a *test managed resource*.
341  * @test: The test context object.
342  * @init: a user-supplied function to initialize the result (if needed).  If
343  *        none is supplied, the resource data value is simply set to @data.
344  *	  If an init function is supplied, @data is passed to it instead.
345  * @free: a user-supplied function to free the resource (if needed).
346  * @data: value to pass to init function or set in resource data field.
347  */
348 int kunit_add_resource(struct kunit *test,
349 		       kunit_resource_init_t init,
350 		       kunit_resource_free_t free,
351 		       struct kunit_resource *res,
352 		       void *data);
353 
354 /**
355  * kunit_add_named_resource() - Add a named *test managed resource*.
356  * @test: The test context object.
357  * @init: a user-supplied function to initialize the resource data, if needed.
358  * @free: a user-supplied function to free the resource data, if needed.
359  * @name_data: name and data to be set for resource.
360  */
361 int kunit_add_named_resource(struct kunit *test,
362 			     kunit_resource_init_t init,
363 			     kunit_resource_free_t free,
364 			     struct kunit_resource *res,
365 			     const char *name,
366 			     void *data);
367 
368 /**
369  * kunit_alloc_resource() - Allocates a *test managed resource*.
370  * @test: The test context object.
371  * @init: a user supplied function to initialize the resource.
372  * @free: a user supplied function to free the resource.
373  * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
374  * @context: for the user to pass in arbitrary data to the init function.
375  *
376  * Allocates a *test managed resource*, a resource which will automatically be
377  * cleaned up at the end of a test case. See &struct kunit_resource for an
378  * example.
379  *
380  * Note: KUnit needs to allocate memory for a kunit_resource object. You must
381  * specify an @internal_gfp that is compatible with the use context of your
382  * resource.
383  */
384 static inline void *kunit_alloc_resource(struct kunit *test,
385 					 kunit_resource_init_t init,
386 					 kunit_resource_free_t free,
387 					 gfp_t internal_gfp,
388 					 void *context)
389 {
390 	struct kunit_resource *res;
391 
392 	res = kzalloc(sizeof(*res), internal_gfp);
393 	if (!res)
394 		return NULL;
395 
396 	if (!kunit_add_resource(test, init, free, res, context))
397 		return res->data;
398 
399 	return NULL;
400 }
401 
402 typedef bool (*kunit_resource_match_t)(struct kunit *test,
403 				       struct kunit_resource *res,
404 				       void *match_data);
405 
406 /**
407  * kunit_resource_instance_match() - Match a resource with the same instance.
408  * @test: Test case to which the resource belongs.
409  * @res: The resource.
410  * @match_data: The resource pointer to match against.
411  *
412  * An instance of kunit_resource_match_t that matches a resource whose
413  * allocation matches @match_data.
414  */
415 static inline bool kunit_resource_instance_match(struct kunit *test,
416 						 struct kunit_resource *res,
417 						 void *match_data)
418 {
419 	return res->data == match_data;
420 }
421 
422 /**
423  * kunit_resource_name_match() - Match a resource with the same name.
424  * @test: Test case to which the resource belongs.
425  * @res: The resource.
426  * @match_name: The name to match against.
427  */
428 static inline bool kunit_resource_name_match(struct kunit *test,
429 					     struct kunit_resource *res,
430 					     void *match_name)
431 {
432 	return res->name && strcmp(res->name, match_name) == 0;
433 }
434 
435 /**
436  * kunit_find_resource() - Find a resource using match function/data.
437  * @test: Test case to which the resource belongs.
438  * @match: match function to be applied to resources/match data.
439  * @match_data: data to be used in matching.
440  */
441 static inline struct kunit_resource *
442 kunit_find_resource(struct kunit *test,
443 		    kunit_resource_match_t match,
444 		    void *match_data)
445 {
446 	struct kunit_resource *res, *found = NULL;
447 
448 	spin_lock(&test->lock);
449 
450 	list_for_each_entry_reverse(res, &test->resources, node) {
451 		if (match(test, res, (void *)match_data)) {
452 			found = res;
453 			kunit_get_resource(found);
454 			break;
455 		}
456 	}
457 
458 	spin_unlock(&test->lock);
459 
460 	return found;
461 }
462 
463 /**
464  * kunit_find_named_resource() - Find a resource using match name.
465  * @test: Test case to which the resource belongs.
466  * @name: match name.
467  */
468 static inline struct kunit_resource *
469 kunit_find_named_resource(struct kunit *test,
470 			  const char *name)
471 {
472 	return kunit_find_resource(test, kunit_resource_name_match,
473 				   (void *)name);
474 }
475 
476 /**
477  * kunit_destroy_resource() - Find a kunit_resource and destroy it.
478  * @test: Test case to which the resource belongs.
479  * @match: Match function. Returns whether a given resource matches @match_data.
480  * @match_data: Data passed into @match.
481  *
482  * RETURNS:
483  * 0 if kunit_resource is found and freed, -ENOENT if not found.
484  */
485 int kunit_destroy_resource(struct kunit *test,
486 			   kunit_resource_match_t match,
487 			   void *match_data);
488 
489 static inline int kunit_destroy_named_resource(struct kunit *test,
490 					       const char *name)
491 {
492 	return kunit_destroy_resource(test, kunit_resource_name_match,
493 				      (void *)name);
494 }
495 
496 /**
497  * kunit_remove_resource: remove resource from resource list associated with
498  *			  test.
499  * @test: The test context object.
500  * @res: The resource to be removed.
501  *
502  * Note that the resource will not be immediately freed since it is likely
503  * the caller has a reference to it via alloc_and_get() or find();
504  * in this case a final call to kunit_put_resource() is required.
505  */
506 void kunit_remove_resource(struct kunit *test, struct kunit_resource *res);
507 
508 /**
509  * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
510  * @test: The test context object.
511  * @size: The size in bytes of the desired memory.
512  * @gfp: flags passed to underlying kmalloc().
513  *
514  * Just like `kmalloc(...)`, except the allocation is managed by the test case
515  * and is automatically cleaned up after the test case concludes. See &struct
516  * kunit_resource for more information.
517  */
518 void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp);
519 
520 /**
521  * kunit_kfree() - Like kfree except for allocations managed by KUnit.
522  * @test: The test case to which the resource belongs.
523  * @ptr: The memory allocation to free.
524  */
525 void kunit_kfree(struct kunit *test, const void *ptr);
526 
527 /**
528  * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
529  * @test: The test context object.
530  * @size: The size in bytes of the desired memory.
531  * @gfp: flags passed to underlying kmalloc().
532  *
533  * See kzalloc() and kunit_kmalloc() for more information.
534  */
535 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
536 {
537 	return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
538 }
539 
540 void kunit_cleanup(struct kunit *test);
541 
542 void kunit_log_append(char *log, const char *fmt, ...);
543 
544 /*
545  * printk and log to per-test or per-suite log buffer.  Logging only done
546  * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
547  */
548 #define kunit_log(lvl, test_or_suite, fmt, ...)				\
549 	do {								\
550 		printk(lvl fmt, ##__VA_ARGS__);				\
551 		kunit_log_append((test_or_suite)->log,	fmt "\n",	\
552 				 ##__VA_ARGS__);			\
553 	} while (0)
554 
555 #define kunit_printk(lvl, test, fmt, ...)				\
556 	kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt,		\
557 		  (test)->name,	##__VA_ARGS__)
558 
559 /**
560  * kunit_info() - Prints an INFO level message associated with @test.
561  *
562  * @test: The test context object.
563  * @fmt:  A printk() style format string.
564  *
565  * Prints an info level message associated with the test suite being run.
566  * Takes a variable number of format parameters just like printk().
567  */
568 #define kunit_info(test, fmt, ...) \
569 	kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
570 
571 /**
572  * kunit_warn() - Prints a WARN level message associated with @test.
573  *
574  * @test: The test context object.
575  * @fmt:  A printk() style format string.
576  *
577  * Prints a warning level message.
578  */
579 #define kunit_warn(test, fmt, ...) \
580 	kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
581 
582 /**
583  * kunit_err() - Prints an ERROR level message associated with @test.
584  *
585  * @test: The test context object.
586  * @fmt:  A printk() style format string.
587  *
588  * Prints an error level message.
589  */
590 #define kunit_err(test, fmt, ...) \
591 	kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
592 
593 /**
594  * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
595  * @test: The test context object.
596  *
597  * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
598  * words, it does nothing and only exists for code clarity. See
599  * KUNIT_EXPECT_TRUE() for more information.
600  */
601 #define KUNIT_SUCCEED(test) do {} while (0)
602 
603 void kunit_do_assertion(struct kunit *test,
604 			struct kunit_assert *assert,
605 			bool pass,
606 			const char *fmt, ...);
607 
608 #define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do {  \
609 	struct assert_class __assertion = INITIALIZER;			       \
610 	kunit_do_assertion(test,					       \
611 			   &__assertion.assert,				       \
612 			   pass,					       \
613 			   fmt,						       \
614 			   ##__VA_ARGS__);				       \
615 } while (0)
616 
617 
618 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...)		       \
619 	KUNIT_ASSERTION(test,						       \
620 			false,						       \
621 			kunit_fail_assert,				       \
622 			KUNIT_INIT_FAIL_ASSERT_STRUCT(test, assert_type),      \
623 			fmt,						       \
624 			##__VA_ARGS__)
625 
626 /**
627  * KUNIT_FAIL() - Always causes a test to fail when evaluated.
628  * @test: The test context object.
629  * @fmt: an informational message to be printed when the assertion is made.
630  * @...: string format arguments.
631  *
632  * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
633  * other words, it always results in a failed expectation, and consequently
634  * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
635  * for more information.
636  */
637 #define KUNIT_FAIL(test, fmt, ...)					       \
638 	KUNIT_FAIL_ASSERTION(test,					       \
639 			     KUNIT_EXPECTATION,				       \
640 			     fmt,					       \
641 			     ##__VA_ARGS__)
642 
643 #define KUNIT_UNARY_ASSERTION(test,					       \
644 			      assert_type,				       \
645 			      condition,				       \
646 			      expected_true,				       \
647 			      fmt,					       \
648 			      ...)					       \
649 	KUNIT_ASSERTION(test,						       \
650 			!!(condition) == !!expected_true,		       \
651 			kunit_unary_assert,				       \
652 			KUNIT_INIT_UNARY_ASSERT_STRUCT(test,		       \
653 						       assert_type,	       \
654 						       #condition,	       \
655 						       expected_true),	       \
656 			fmt,						       \
657 			##__VA_ARGS__)
658 
659 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)       \
660 	KUNIT_UNARY_ASSERTION(test,					       \
661 			      assert_type,				       \
662 			      condition,				       \
663 			      true,					       \
664 			      fmt,					       \
665 			      ##__VA_ARGS__)
666 
667 #define KUNIT_TRUE_ASSERTION(test, assert_type, condition) \
668 	KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, NULL)
669 
670 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)      \
671 	KUNIT_UNARY_ASSERTION(test,					       \
672 			      assert_type,				       \
673 			      condition,				       \
674 			      false,					       \
675 			      fmt,					       \
676 			      ##__VA_ARGS__)
677 
678 #define KUNIT_FALSE_ASSERTION(test, assert_type, condition) \
679 	KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, NULL)
680 
681 /*
682  * A factory macro for defining the assertions and expectations for the basic
683  * comparisons defined for the built in types.
684  *
685  * Unfortunately, there is no common type that all types can be promoted to for
686  * which all the binary operators behave the same way as for the actual types
687  * (for example, there is no type that long long and unsigned long long can
688  * both be cast to where the comparison result is preserved for all values). So
689  * the best we can do is do the comparison in the original types and then coerce
690  * everything to long long for printing; this way, the comparison behaves
691  * correctly and the printed out value usually makes sense without
692  * interpretation, but can always be interpreted to figure out the actual
693  * value.
694  */
695 #define KUNIT_BASE_BINARY_ASSERTION(test,				       \
696 				    assert_class,			       \
697 				    ASSERT_CLASS_INIT,			       \
698 				    assert_type,			       \
699 				    left,				       \
700 				    op,					       \
701 				    right,				       \
702 				    fmt,				       \
703 				    ...)				       \
704 do {									       \
705 	typeof(left) __left = (left);					       \
706 	typeof(right) __right = (right);				       \
707 	((void)__typecheck(__left, __right));				       \
708 									       \
709 	KUNIT_ASSERTION(test,						       \
710 			__left op __right,				       \
711 			assert_class,					       \
712 			ASSERT_CLASS_INIT(test,				       \
713 					  assert_type,			       \
714 					  #op,				       \
715 					  #left,			       \
716 					  __left,			       \
717 					  #right,			       \
718 					  __right),			       \
719 			fmt,						       \
720 			##__VA_ARGS__);					       \
721 } while (0)
722 
723 #define KUNIT_BASE_EQ_MSG_ASSERTION(test,				       \
724 				    assert_class,			       \
725 				    ASSERT_CLASS_INIT,			       \
726 				    assert_type,			       \
727 				    left,				       \
728 				    right,				       \
729 				    fmt,				       \
730 				    ...)				       \
731 	KUNIT_BASE_BINARY_ASSERTION(test,				       \
732 				    assert_class,			       \
733 				    ASSERT_CLASS_INIT,			       \
734 				    assert_type,			       \
735 				    left, ==, right,			       \
736 				    fmt,				       \
737 				    ##__VA_ARGS__)
738 
739 #define KUNIT_BASE_NE_MSG_ASSERTION(test,				       \
740 				    assert_class,			       \
741 				    ASSERT_CLASS_INIT,			       \
742 				    assert_type,			       \
743 				    left,				       \
744 				    right,				       \
745 				    fmt,				       \
746 				    ...)				       \
747 	KUNIT_BASE_BINARY_ASSERTION(test,				       \
748 				    assert_class,			       \
749 				    ASSERT_CLASS_INIT,			       \
750 				    assert_type,			       \
751 				    left, !=, right,			       \
752 				    fmt,				       \
753 				    ##__VA_ARGS__)
754 
755 #define KUNIT_BASE_LT_MSG_ASSERTION(test,				       \
756 				    assert_class,			       \
757 				    ASSERT_CLASS_INIT,			       \
758 				    assert_type,			       \
759 				    left,				       \
760 				    right,				       \
761 				    fmt,				       \
762 				    ...)				       \
763 	KUNIT_BASE_BINARY_ASSERTION(test,				       \
764 				    assert_class,			       \
765 				    ASSERT_CLASS_INIT,			       \
766 				    assert_type,			       \
767 				    left, <, right,			       \
768 				    fmt,				       \
769 				    ##__VA_ARGS__)
770 
771 #define KUNIT_BASE_LE_MSG_ASSERTION(test,				       \
772 				    assert_class,			       \
773 				    ASSERT_CLASS_INIT,			       \
774 				    assert_type,			       \
775 				    left,				       \
776 				    right,				       \
777 				    fmt,				       \
778 				    ...)				       \
779 	KUNIT_BASE_BINARY_ASSERTION(test,				       \
780 				    assert_class,			       \
781 				    ASSERT_CLASS_INIT,			       \
782 				    assert_type,			       \
783 				    left, <=, right,			       \
784 				    fmt,				       \
785 				    ##__VA_ARGS__)
786 
787 #define KUNIT_BASE_GT_MSG_ASSERTION(test,				       \
788 				    assert_class,			       \
789 				    ASSERT_CLASS_INIT,			       \
790 				    assert_type,			       \
791 				    left,				       \
792 				    right,				       \
793 				    fmt,				       \
794 				    ...)				       \
795 	KUNIT_BASE_BINARY_ASSERTION(test,				       \
796 				    assert_class,			       \
797 				    ASSERT_CLASS_INIT,			       \
798 				    assert_type,			       \
799 				    left, >, right,			       \
800 				    fmt,				       \
801 				    ##__VA_ARGS__)
802 
803 #define KUNIT_BASE_GE_MSG_ASSERTION(test,				       \
804 				    assert_class,			       \
805 				    ASSERT_CLASS_INIT,			       \
806 				    assert_type,			       \
807 				    left,				       \
808 				    right,				       \
809 				    fmt,				       \
810 				    ...)				       \
811 	KUNIT_BASE_BINARY_ASSERTION(test,				       \
812 				    assert_class,			       \
813 				    ASSERT_CLASS_INIT,			       \
814 				    assert_type,			       \
815 				    left, >=, right,			       \
816 				    fmt,				       \
817 				    ##__VA_ARGS__)
818 
819 #define KUNIT_BINARY_EQ_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
820 	KUNIT_BASE_EQ_MSG_ASSERTION(test,				       \
821 				    kunit_binary_assert,		       \
822 				    KUNIT_INIT_BINARY_ASSERT_STRUCT,	       \
823 				    assert_type,			       \
824 				    left,				       \
825 				    right,				       \
826 				    fmt,				       \
827 				    ##__VA_ARGS__)
828 
829 #define KUNIT_BINARY_EQ_ASSERTION(test, assert_type, left, right)	       \
830 	KUNIT_BINARY_EQ_MSG_ASSERTION(test,				       \
831 				      assert_type,			       \
832 				      left,				       \
833 				      right,				       \
834 				      NULL)
835 
836 #define KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test,				       \
837 					  assert_type,			       \
838 					  left,				       \
839 					  right,			       \
840 					  fmt,				       \
841 					  ...)				       \
842 	KUNIT_BASE_EQ_MSG_ASSERTION(test,				       \
843 				    kunit_binary_ptr_assert,		       \
844 				    KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT,       \
845 				    assert_type,			       \
846 				    left,				       \
847 				    right,				       \
848 				    fmt,				       \
849 				    ##__VA_ARGS__)
850 
851 #define KUNIT_BINARY_PTR_EQ_ASSERTION(test, assert_type, left, right)	       \
852 	KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test,				       \
853 					  assert_type,			       \
854 					  left,				       \
855 					  right,			       \
856 					  NULL)
857 
858 #define KUNIT_BINARY_NE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
859 	KUNIT_BASE_NE_MSG_ASSERTION(test,				       \
860 				    kunit_binary_assert,		       \
861 				    KUNIT_INIT_BINARY_ASSERT_STRUCT,	       \
862 				    assert_type,			       \
863 				    left,				       \
864 				    right,				       \
865 				    fmt,				       \
866 				    ##__VA_ARGS__)
867 
868 #define KUNIT_BINARY_NE_ASSERTION(test, assert_type, left, right)	       \
869 	KUNIT_BINARY_NE_MSG_ASSERTION(test,				       \
870 				      assert_type,			       \
871 				      left,				       \
872 				      right,				       \
873 				      NULL)
874 
875 #define KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test,				       \
876 					  assert_type,			       \
877 					  left,				       \
878 					  right,			       \
879 					  fmt,				       \
880 					  ...)				       \
881 	KUNIT_BASE_NE_MSG_ASSERTION(test,				       \
882 				    kunit_binary_ptr_assert,		       \
883 				    KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT,       \
884 				    assert_type,			       \
885 				    left,				       \
886 				    right,				       \
887 				    fmt,				       \
888 				    ##__VA_ARGS__)
889 
890 #define KUNIT_BINARY_PTR_NE_ASSERTION(test, assert_type, left, right)	       \
891 	KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test,				       \
892 					  assert_type,			       \
893 					  left,				       \
894 					  right,			       \
895 					  NULL)
896 
897 #define KUNIT_BINARY_LT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
898 	KUNIT_BASE_LT_MSG_ASSERTION(test,				       \
899 				    kunit_binary_assert,		       \
900 				    KUNIT_INIT_BINARY_ASSERT_STRUCT,	       \
901 				    assert_type,			       \
902 				    left,				       \
903 				    right,				       \
904 				    fmt,				       \
905 				    ##__VA_ARGS__)
906 
907 #define KUNIT_BINARY_LT_ASSERTION(test, assert_type, left, right)	       \
908 	KUNIT_BINARY_LT_MSG_ASSERTION(test,				       \
909 				      assert_type,			       \
910 				      left,				       \
911 				      right,				       \
912 				      NULL)
913 
914 #define KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test,				       \
915 					  assert_type,			       \
916 					  left,				       \
917 					  right,			       \
918 					  fmt,				       \
919 					  ...)				       \
920 	KUNIT_BASE_LT_MSG_ASSERTION(test,				       \
921 				    kunit_binary_ptr_assert,		       \
922 				    KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT,       \
923 				    assert_type,			       \
924 				    left,				       \
925 				    right,				       \
926 				    fmt,				       \
927 				    ##__VA_ARGS__)
928 
929 #define KUNIT_BINARY_PTR_LT_ASSERTION(test, assert_type, left, right)	       \
930 	KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test,				       \
931 					  assert_type,			       \
932 					  left,				       \
933 					  right,			       \
934 					  NULL)
935 
936 #define KUNIT_BINARY_LE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
937 	KUNIT_BASE_LE_MSG_ASSERTION(test,				       \
938 				    kunit_binary_assert,		       \
939 				    KUNIT_INIT_BINARY_ASSERT_STRUCT,	       \
940 				    assert_type,			       \
941 				    left,				       \
942 				    right,				       \
943 				    fmt,				       \
944 				    ##__VA_ARGS__)
945 
946 #define KUNIT_BINARY_LE_ASSERTION(test, assert_type, left, right)	       \
947 	KUNIT_BINARY_LE_MSG_ASSERTION(test,				       \
948 				      assert_type,			       \
949 				      left,				       \
950 				      right,				       \
951 				      NULL)
952 
953 #define KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test,				       \
954 					  assert_type,			       \
955 					  left,				       \
956 					  right,			       \
957 					  fmt,				       \
958 					  ...)				       \
959 	KUNIT_BASE_LE_MSG_ASSERTION(test,				       \
960 				    kunit_binary_ptr_assert,		       \
961 				    KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT,       \
962 				    assert_type,			       \
963 				    left,				       \
964 				    right,				       \
965 				    fmt,				       \
966 				    ##__VA_ARGS__)
967 
968 #define KUNIT_BINARY_PTR_LE_ASSERTION(test, assert_type, left, right)	       \
969 	KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test,				       \
970 					  assert_type,			       \
971 					  left,				       \
972 					  right,			       \
973 					  NULL)
974 
975 #define KUNIT_BINARY_GT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
976 	KUNIT_BASE_GT_MSG_ASSERTION(test,				       \
977 				    kunit_binary_assert,		       \
978 				    KUNIT_INIT_BINARY_ASSERT_STRUCT,	       \
979 				    assert_type,			       \
980 				    left,				       \
981 				    right,				       \
982 				    fmt,				       \
983 				    ##__VA_ARGS__)
984 
985 #define KUNIT_BINARY_GT_ASSERTION(test, assert_type, left, right)	       \
986 	KUNIT_BINARY_GT_MSG_ASSERTION(test,				       \
987 				      assert_type,			       \
988 				      left,				       \
989 				      right,				       \
990 				      NULL)
991 
992 #define KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test,				       \
993 					  assert_type,			       \
994 					  left,				       \
995 					  right,			       \
996 					  fmt,				       \
997 					  ...)				       \
998 	KUNIT_BASE_GT_MSG_ASSERTION(test,				       \
999 				    kunit_binary_ptr_assert,		       \
1000 				    KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT,       \
1001 				    assert_type,			       \
1002 				    left,				       \
1003 				    right,				       \
1004 				    fmt,				       \
1005 				    ##__VA_ARGS__)
1006 
1007 #define KUNIT_BINARY_PTR_GT_ASSERTION(test, assert_type, left, right)	       \
1008 	KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test,				       \
1009 					  assert_type,			       \
1010 					  left,				       \
1011 					  right,			       \
1012 					  NULL)
1013 
1014 #define KUNIT_BINARY_GE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1015 	KUNIT_BASE_GE_MSG_ASSERTION(test,				       \
1016 				    kunit_binary_assert,		       \
1017 				    KUNIT_INIT_BINARY_ASSERT_STRUCT,	       \
1018 				    assert_type,			       \
1019 				    left,				       \
1020 				    right,				       \
1021 				    fmt,				       \
1022 				    ##__VA_ARGS__)
1023 
1024 #define KUNIT_BINARY_GE_ASSERTION(test, assert_type, left, right)	       \
1025 	KUNIT_BINARY_GE_MSG_ASSERTION(test,				       \
1026 				      assert_type,			       \
1027 				      left,				       \
1028 				      right,				       \
1029 				      NULL)
1030 
1031 #define KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test,				       \
1032 					  assert_type,			       \
1033 					  left,				       \
1034 					  right,			       \
1035 					  fmt,				       \
1036 					  ...)				       \
1037 	KUNIT_BASE_GE_MSG_ASSERTION(test,				       \
1038 				    kunit_binary_ptr_assert,		       \
1039 				    KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT,       \
1040 				    assert_type,			       \
1041 				    left,				       \
1042 				    right,				       \
1043 				    fmt,				       \
1044 				    ##__VA_ARGS__)
1045 
1046 #define KUNIT_BINARY_PTR_GE_ASSERTION(test, assert_type, left, right)	       \
1047 	KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test,				       \
1048 					  assert_type,			       \
1049 					  left,				       \
1050 					  right,			       \
1051 					  NULL)
1052 
1053 #define KUNIT_BINARY_STR_ASSERTION(test,				       \
1054 				   assert_type,				       \
1055 				   left,				       \
1056 				   op,					       \
1057 				   right,				       \
1058 				   fmt,					       \
1059 				   ...)					       \
1060 do {									       \
1061 	typeof(left) __left = (left);					       \
1062 	typeof(right) __right = (right);				       \
1063 									       \
1064 	KUNIT_ASSERTION(test,						       \
1065 			strcmp(__left, __right) op 0,			       \
1066 			kunit_binary_str_assert,			       \
1067 			KUNIT_INIT_BINARY_ASSERT_STRUCT(test,		       \
1068 							assert_type,	       \
1069 							#op,		       \
1070 							#left,		       \
1071 							__left,		       \
1072 							#right,		       \
1073 							__right),	       \
1074 			fmt,						       \
1075 			##__VA_ARGS__);					       \
1076 } while (0)
1077 
1078 #define KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test,				       \
1079 					  assert_type,			       \
1080 					  left,				       \
1081 					  right,			       \
1082 					  fmt,				       \
1083 					  ...)				       \
1084 	KUNIT_BINARY_STR_ASSERTION(test,				       \
1085 				   assert_type,				       \
1086 				   left, ==, right,			       \
1087 				   fmt,					       \
1088 				   ##__VA_ARGS__)
1089 
1090 #define KUNIT_BINARY_STR_EQ_ASSERTION(test, assert_type, left, right)	       \
1091 	KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test,				       \
1092 					  assert_type,			       \
1093 					  left,				       \
1094 					  right,			       \
1095 					  NULL)
1096 
1097 #define KUNIT_BINARY_STR_NE_MSG_ASSERTION(test,				       \
1098 					  assert_type,			       \
1099 					  left,				       \
1100 					  right,			       \
1101 					  fmt,				       \
1102 					  ...)				       \
1103 	KUNIT_BINARY_STR_ASSERTION(test,				       \
1104 				   assert_type,				       \
1105 				   left, !=, right,			       \
1106 				   fmt,					       \
1107 				   ##__VA_ARGS__)
1108 
1109 #define KUNIT_BINARY_STR_NE_ASSERTION(test, assert_type, left, right)	       \
1110 	KUNIT_BINARY_STR_NE_MSG_ASSERTION(test,				       \
1111 					  assert_type,			       \
1112 					  left,				       \
1113 					  right,			       \
1114 					  NULL)
1115 
1116 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
1117 						assert_type,		       \
1118 						ptr,			       \
1119 						fmt,			       \
1120 						...)			       \
1121 do {									       \
1122 	typeof(ptr) __ptr = (ptr);					       \
1123 									       \
1124 	KUNIT_ASSERTION(test,						       \
1125 			!IS_ERR_OR_NULL(__ptr),				       \
1126 			kunit_ptr_not_err_assert,			       \
1127 			KUNIT_INIT_PTR_NOT_ERR_STRUCT(test,		       \
1128 						      assert_type,	       \
1129 						      #ptr,		       \
1130 						      __ptr),		       \
1131 			fmt,						       \
1132 			##__VA_ARGS__);					       \
1133 } while (0)
1134 
1135 #define KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, assert_type, ptr)	       \
1136 	KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
1137 						assert_type,		       \
1138 						ptr,			       \
1139 						NULL)
1140 
1141 /**
1142  * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
1143  * @test: The test context object.
1144  * @condition: an arbitrary boolean expression. The test fails when this does
1145  * not evaluate to true.
1146  *
1147  * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
1148  * to fail when the specified condition is not met; however, it will not prevent
1149  * the test case from continuing to run; this is otherwise known as an
1150  * *expectation failure*.
1151  */
1152 #define KUNIT_EXPECT_TRUE(test, condition) \
1153 	KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)
1154 
1155 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...)		       \
1156 	KUNIT_TRUE_MSG_ASSERTION(test,					       \
1157 				 KUNIT_EXPECTATION,			       \
1158 				 condition,				       \
1159 				 fmt,					       \
1160 				 ##__VA_ARGS__)
1161 
1162 /**
1163  * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
1164  * @test: The test context object.
1165  * @condition: an arbitrary boolean expression. The test fails when this does
1166  * not evaluate to false.
1167  *
1168  * Sets an expectation that @condition evaluates to false. See
1169  * KUNIT_EXPECT_TRUE() for more information.
1170  */
1171 #define KUNIT_EXPECT_FALSE(test, condition) \
1172 	KUNIT_FALSE_ASSERTION(test, KUNIT_EXPECTATION, condition)
1173 
1174 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...)		       \
1175 	KUNIT_FALSE_MSG_ASSERTION(test,					       \
1176 				  KUNIT_EXPECTATION,			       \
1177 				  condition,				       \
1178 				  fmt,					       \
1179 				  ##__VA_ARGS__)
1180 
1181 /**
1182  * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
1183  * @test: The test context object.
1184  * @left: an arbitrary expression that evaluates to a primitive C type.
1185  * @right: an arbitrary expression that evaluates to a primitive C type.
1186  *
1187  * Sets an expectation that the values that @left and @right evaluate to are
1188  * equal. This is semantically equivalent to
1189  * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1190  * more information.
1191  */
1192 #define KUNIT_EXPECT_EQ(test, left, right) \
1193 	KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1194 
1195 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...)		       \
1196 	KUNIT_BINARY_EQ_MSG_ASSERTION(test,				       \
1197 				      KUNIT_EXPECTATION,		       \
1198 				      left,				       \
1199 				      right,				       \
1200 				      fmt,				       \
1201 				      ##__VA_ARGS__)
1202 
1203 /**
1204  * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
1205  * @test: The test context object.
1206  * @left: an arbitrary expression that evaluates to a pointer.
1207  * @right: an arbitrary expression that evaluates to a pointer.
1208  *
1209  * Sets an expectation that the values that @left and @right evaluate to are
1210  * equal. This is semantically equivalent to
1211  * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1212  * more information.
1213  */
1214 #define KUNIT_EXPECT_PTR_EQ(test, left, right)				       \
1215 	KUNIT_BINARY_PTR_EQ_ASSERTION(test,				       \
1216 				      KUNIT_EXPECTATION,		       \
1217 				      left,				       \
1218 				      right)
1219 
1220 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
1221 	KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test,				       \
1222 					  KUNIT_EXPECTATION,		       \
1223 					  left,				       \
1224 					  right,			       \
1225 					  fmt,				       \
1226 					  ##__VA_ARGS__)
1227 
1228 /**
1229  * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
1230  * @test: The test context object.
1231  * @left: an arbitrary expression that evaluates to a primitive C type.
1232  * @right: an arbitrary expression that evaluates to a primitive C type.
1233  *
1234  * Sets an expectation that the values that @left and @right evaluate to are not
1235  * equal. This is semantically equivalent to
1236  * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1237  * more information.
1238  */
1239 #define KUNIT_EXPECT_NE(test, left, right) \
1240 	KUNIT_BINARY_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1241 
1242 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...)		       \
1243 	KUNIT_BINARY_NE_MSG_ASSERTION(test,				       \
1244 				      KUNIT_EXPECTATION,		       \
1245 				      left,				       \
1246 				      right,				       \
1247 				      fmt,				       \
1248 				      ##__VA_ARGS__)
1249 
1250 /**
1251  * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
1252  * @test: The test context object.
1253  * @left: an arbitrary expression that evaluates to a pointer.
1254  * @right: an arbitrary expression that evaluates to a pointer.
1255  *
1256  * Sets an expectation that the values that @left and @right evaluate to are not
1257  * equal. This is semantically equivalent to
1258  * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1259  * more information.
1260  */
1261 #define KUNIT_EXPECT_PTR_NE(test, left, right)				       \
1262 	KUNIT_BINARY_PTR_NE_ASSERTION(test,				       \
1263 				      KUNIT_EXPECTATION,		       \
1264 				      left,				       \
1265 				      right)
1266 
1267 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
1268 	KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test,				       \
1269 					  KUNIT_EXPECTATION,		       \
1270 					  left,				       \
1271 					  right,			       \
1272 					  fmt,				       \
1273 					  ##__VA_ARGS__)
1274 
1275 /**
1276  * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
1277  * @test: The test context object.
1278  * @left: an arbitrary expression that evaluates to a primitive C type.
1279  * @right: an arbitrary expression that evaluates to a primitive C type.
1280  *
1281  * Sets an expectation that the value that @left evaluates to is less than the
1282  * value that @right evaluates to. This is semantically equivalent to
1283  * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
1284  * more information.
1285  */
1286 #define KUNIT_EXPECT_LT(test, left, right) \
1287 	KUNIT_BINARY_LT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1288 
1289 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...)		       \
1290 	KUNIT_BINARY_LT_MSG_ASSERTION(test,				       \
1291 				      KUNIT_EXPECTATION,		       \
1292 				      left,				       \
1293 				      right,				       \
1294 				      fmt,				       \
1295 				      ##__VA_ARGS__)
1296 
1297 /**
1298  * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
1299  * @test: The test context object.
1300  * @left: an arbitrary expression that evaluates to a primitive C type.
1301  * @right: an arbitrary expression that evaluates to a primitive C type.
1302  *
1303  * Sets an expectation that the value that @left evaluates to is less than or
1304  * equal to the value that @right evaluates to. Semantically this is equivalent
1305  * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
1306  * more information.
1307  */
1308 #define KUNIT_EXPECT_LE(test, left, right) \
1309 	KUNIT_BINARY_LE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1310 
1311 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...)		       \
1312 	KUNIT_BINARY_LE_MSG_ASSERTION(test,				       \
1313 				      KUNIT_EXPECTATION,		       \
1314 				      left,				       \
1315 				      right,				       \
1316 				      fmt,				       \
1317 				      ##__VA_ARGS__)
1318 
1319 /**
1320  * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
1321  * @test: The test context object.
1322  * @left: an arbitrary expression that evaluates to a primitive C type.
1323  * @right: an arbitrary expression that evaluates to a primitive C type.
1324  *
1325  * Sets an expectation that the value that @left evaluates to is greater than
1326  * the value that @right evaluates to. This is semantically equivalent to
1327  * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
1328  * more information.
1329  */
1330 #define KUNIT_EXPECT_GT(test, left, right) \
1331 	KUNIT_BINARY_GT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1332 
1333 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...)		       \
1334 	KUNIT_BINARY_GT_MSG_ASSERTION(test,				       \
1335 				      KUNIT_EXPECTATION,		       \
1336 				      left,				       \
1337 				      right,				       \
1338 				      fmt,				       \
1339 				      ##__VA_ARGS__)
1340 
1341 /**
1342  * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1343  * @test: The test context object.
1344  * @left: an arbitrary expression that evaluates to a primitive C type.
1345  * @right: an arbitrary expression that evaluates to a primitive C type.
1346  *
1347  * Sets an expectation that the value that @left evaluates to is greater than
1348  * the value that @right evaluates to. This is semantically equivalent to
1349  * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1350  * more information.
1351  */
1352 #define KUNIT_EXPECT_GE(test, left, right) \
1353 	KUNIT_BINARY_GE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1354 
1355 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...)		       \
1356 	KUNIT_BINARY_GE_MSG_ASSERTION(test,				       \
1357 				      KUNIT_EXPECTATION,		       \
1358 				      left,				       \
1359 				      right,				       \
1360 				      fmt,				       \
1361 				      ##__VA_ARGS__)
1362 
1363 /**
1364  * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1365  * @test: The test context object.
1366  * @left: an arbitrary expression that evaluates to a null terminated string.
1367  * @right: an arbitrary expression that evaluates to a null terminated string.
1368  *
1369  * Sets an expectation that the values that @left and @right evaluate to are
1370  * equal. This is semantically equivalent to
1371  * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1372  * for more information.
1373  */
1374 #define KUNIT_EXPECT_STREQ(test, left, right) \
1375 	KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1376 
1377 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...)		       \
1378 	KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test,				       \
1379 					  KUNIT_EXPECTATION,		       \
1380 					  left,				       \
1381 					  right,			       \
1382 					  fmt,				       \
1383 					  ##__VA_ARGS__)
1384 
1385 /**
1386  * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1387  * @test: The test context object.
1388  * @left: an arbitrary expression that evaluates to a null terminated string.
1389  * @right: an arbitrary expression that evaluates to a null terminated string.
1390  *
1391  * Sets an expectation that the values that @left and @right evaluate to are
1392  * not equal. This is semantically equivalent to
1393  * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1394  * for more information.
1395  */
1396 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
1397 	KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1398 
1399 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
1400 	KUNIT_BINARY_STR_NE_MSG_ASSERTION(test,				       \
1401 					  KUNIT_EXPECTATION,		       \
1402 					  left,				       \
1403 					  right,			       \
1404 					  fmt,				       \
1405 					  ##__VA_ARGS__)
1406 
1407 /**
1408  * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1409  * @test: The test context object.
1410  * @ptr: an arbitrary pointer.
1411  *
1412  * Sets an expectation that the value that @ptr evaluates to is not null and not
1413  * an errno stored in a pointer. This is semantically equivalent to
1414  * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1415  * more information.
1416  */
1417 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1418 	KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_EXPECTATION, ptr)
1419 
1420 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
1421 	KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
1422 						KUNIT_EXPECTATION,	       \
1423 						ptr,			       \
1424 						fmt,			       \
1425 						##__VA_ARGS__)
1426 
1427 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1428 	KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1429 
1430 /**
1431  * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1432  * @test: The test context object.
1433  * @condition: an arbitrary boolean expression. The test fails and aborts when
1434  * this does not evaluate to true.
1435  *
1436  * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1437  * fail *and immediately abort* when the specified condition is not met. Unlike
1438  * an expectation failure, it will prevent the test case from continuing to run;
1439  * this is otherwise known as an *assertion failure*.
1440  */
1441 #define KUNIT_ASSERT_TRUE(test, condition) \
1442 	KUNIT_TRUE_ASSERTION(test, KUNIT_ASSERTION, condition)
1443 
1444 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...)		       \
1445 	KUNIT_TRUE_MSG_ASSERTION(test,					       \
1446 				 KUNIT_ASSERTION,			       \
1447 				 condition,				       \
1448 				 fmt,					       \
1449 				 ##__VA_ARGS__)
1450 
1451 /**
1452  * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1453  * @test: The test context object.
1454  * @condition: an arbitrary boolean expression.
1455  *
1456  * Sets an assertion that the value that @condition evaluates to is false. This
1457  * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1458  * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1459  */
1460 #define KUNIT_ASSERT_FALSE(test, condition) \
1461 	KUNIT_FALSE_ASSERTION(test, KUNIT_ASSERTION, condition)
1462 
1463 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...)		       \
1464 	KUNIT_FALSE_MSG_ASSERTION(test,					       \
1465 				  KUNIT_ASSERTION,			       \
1466 				  condition,				       \
1467 				  fmt,					       \
1468 				  ##__VA_ARGS__)
1469 
1470 /**
1471  * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1472  * @test: The test context object.
1473  * @left: an arbitrary expression that evaluates to a primitive C type.
1474  * @right: an arbitrary expression that evaluates to a primitive C type.
1475  *
1476  * Sets an assertion that the values that @left and @right evaluate to are
1477  * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1478  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1479  */
1480 #define KUNIT_ASSERT_EQ(test, left, right) \
1481 	KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1482 
1483 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...)		       \
1484 	KUNIT_BINARY_EQ_MSG_ASSERTION(test,				       \
1485 				      KUNIT_ASSERTION,			       \
1486 				      left,				       \
1487 				      right,				       \
1488 				      fmt,				       \
1489 				      ##__VA_ARGS__)
1490 
1491 /**
1492  * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1493  * @test: The test context object.
1494  * @left: an arbitrary expression that evaluates to a pointer.
1495  * @right: an arbitrary expression that evaluates to a pointer.
1496  *
1497  * Sets an assertion that the values that @left and @right evaluate to are
1498  * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1499  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1500  */
1501 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1502 	KUNIT_BINARY_PTR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1503 
1504 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
1505 	KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test,				       \
1506 					  KUNIT_ASSERTION,		       \
1507 					  left,				       \
1508 					  right,			       \
1509 					  fmt,				       \
1510 					  ##__VA_ARGS__)
1511 
1512 /**
1513  * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1514  * @test: The test context object.
1515  * @left: an arbitrary expression that evaluates to a primitive C type.
1516  * @right: an arbitrary expression that evaluates to a primitive C type.
1517  *
1518  * Sets an assertion that the values that @left and @right evaluate to are not
1519  * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1520  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1521  */
1522 #define KUNIT_ASSERT_NE(test, left, right) \
1523 	KUNIT_BINARY_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1524 
1525 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...)		       \
1526 	KUNIT_BINARY_NE_MSG_ASSERTION(test,				       \
1527 				      KUNIT_ASSERTION,			       \
1528 				      left,				       \
1529 				      right,				       \
1530 				      fmt,				       \
1531 				      ##__VA_ARGS__)
1532 
1533 /**
1534  * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1535  * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1536  * @test: The test context object.
1537  * @left: an arbitrary expression that evaluates to a pointer.
1538  * @right: an arbitrary expression that evaluates to a pointer.
1539  *
1540  * Sets an assertion that the values that @left and @right evaluate to are not
1541  * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1542  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1543  */
1544 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1545 	KUNIT_BINARY_PTR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1546 
1547 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
1548 	KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test,				       \
1549 					  KUNIT_ASSERTION,		       \
1550 					  left,				       \
1551 					  right,			       \
1552 					  fmt,				       \
1553 					  ##__VA_ARGS__)
1554 /**
1555  * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1556  * @test: The test context object.
1557  * @left: an arbitrary expression that evaluates to a primitive C type.
1558  * @right: an arbitrary expression that evaluates to a primitive C type.
1559  *
1560  * Sets an assertion that the value that @left evaluates to is less than the
1561  * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1562  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1563  * is not met.
1564  */
1565 #define KUNIT_ASSERT_LT(test, left, right) \
1566 	KUNIT_BINARY_LT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1567 
1568 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...)		       \
1569 	KUNIT_BINARY_LT_MSG_ASSERTION(test,				       \
1570 				      KUNIT_ASSERTION,			       \
1571 				      left,				       \
1572 				      right,				       \
1573 				      fmt,				       \
1574 				      ##__VA_ARGS__)
1575 /**
1576  * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1577  * @test: The test context object.
1578  * @left: an arbitrary expression that evaluates to a primitive C type.
1579  * @right: an arbitrary expression that evaluates to a primitive C type.
1580  *
1581  * Sets an assertion that the value that @left evaluates to is less than or
1582  * equal to the value that @right evaluates to. This is the same as
1583  * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1584  * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1585  */
1586 #define KUNIT_ASSERT_LE(test, left, right) \
1587 	KUNIT_BINARY_LE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1588 
1589 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...)		       \
1590 	KUNIT_BINARY_LE_MSG_ASSERTION(test,				       \
1591 				      KUNIT_ASSERTION,			       \
1592 				      left,				       \
1593 				      right,				       \
1594 				      fmt,				       \
1595 				      ##__VA_ARGS__)
1596 
1597 /**
1598  * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1599  * @test: The test context object.
1600  * @left: an arbitrary expression that evaluates to a primitive C type.
1601  * @right: an arbitrary expression that evaluates to a primitive C type.
1602  *
1603  * Sets an assertion that the value that @left evaluates to is greater than the
1604  * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1605  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1606  * is not met.
1607  */
1608 #define KUNIT_ASSERT_GT(test, left, right) \
1609 	KUNIT_BINARY_GT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1610 
1611 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...)		       \
1612 	KUNIT_BINARY_GT_MSG_ASSERTION(test,				       \
1613 				      KUNIT_ASSERTION,			       \
1614 				      left,				       \
1615 				      right,				       \
1616 				      fmt,				       \
1617 				      ##__VA_ARGS__)
1618 
1619 /**
1620  * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1621  * @test: The test context object.
1622  * @left: an arbitrary expression that evaluates to a primitive C type.
1623  * @right: an arbitrary expression that evaluates to a primitive C type.
1624  *
1625  * Sets an assertion that the value that @left evaluates to is greater than the
1626  * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1627  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1628  * is not met.
1629  */
1630 #define KUNIT_ASSERT_GE(test, left, right) \
1631 	KUNIT_BINARY_GE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1632 
1633 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...)		       \
1634 	KUNIT_BINARY_GE_MSG_ASSERTION(test,				       \
1635 				      KUNIT_ASSERTION,			       \
1636 				      left,				       \
1637 				      right,				       \
1638 				      fmt,				       \
1639 				      ##__VA_ARGS__)
1640 
1641 /**
1642  * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1643  * @test: The test context object.
1644  * @left: an arbitrary expression that evaluates to a null terminated string.
1645  * @right: an arbitrary expression that evaluates to a null terminated string.
1646  *
1647  * Sets an assertion that the values that @left and @right evaluate to are
1648  * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1649  * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1650  */
1651 #define KUNIT_ASSERT_STREQ(test, left, right) \
1652 	KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1653 
1654 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...)		       \
1655 	KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test,				       \
1656 					  KUNIT_ASSERTION,		       \
1657 					  left,				       \
1658 					  right,			       \
1659 					  fmt,				       \
1660 					  ##__VA_ARGS__)
1661 
1662 /**
1663  * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1664  * @test: The test context object.
1665  * @left: an arbitrary expression that evaluates to a null terminated string.
1666  * @right: an arbitrary expression that evaluates to a null terminated string.
1667  *
1668  * Sets an expectation that the values that @left and @right evaluate to are
1669  * not equal. This is semantically equivalent to
1670  * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1671  * for more information.
1672  */
1673 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1674 	KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1675 
1676 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
1677 	KUNIT_BINARY_STR_NE_MSG_ASSERTION(test,				       \
1678 					  KUNIT_ASSERTION,		       \
1679 					  left,				       \
1680 					  right,			       \
1681 					  fmt,				       \
1682 					  ##__VA_ARGS__)
1683 
1684 /**
1685  * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1686  * @test: The test context object.
1687  * @ptr: an arbitrary pointer.
1688  *
1689  * Sets an assertion that the value that @ptr evaluates to is not null and not
1690  * an errno stored in a pointer. This is the same as
1691  * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1692  * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1693  */
1694 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1695 	KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_ASSERTION, ptr)
1696 
1697 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
1698 	KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
1699 						KUNIT_ASSERTION,	       \
1700 						ptr,			       \
1701 						fmt,			       \
1702 						##__VA_ARGS__)
1703 
1704 #endif /* _KUNIT_TEST_H */
1705