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 
15 #include <linux/compiler.h>
16 #include <linux/container_of.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/kconfig.h>
20 #include <linux/kref.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/string.h>
26 #include <linux/types.h>
27 
28 #include <asm/rwonce.h>
29 
30 struct kunit;
31 
32 /* Size of log associated with test. */
33 #define KUNIT_LOG_SIZE	512
34 
35 /* Maximum size of parameter description string. */
36 #define KUNIT_PARAM_DESC_SIZE 128
37 
38 /* Maximum size of a status comment. */
39 #define KUNIT_STATUS_COMMENT_SIZE 256
40 
41 /*
42  * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
43  * sub-subtest.  See the "Subtests" section in
44  * https://node-tap.org/tap-protocol/
45  */
46 #define KUNIT_SUBTEST_INDENT		"    "
47 #define KUNIT_SUBSUBTEST_INDENT		"        "
48 
49 /**
50  * enum kunit_status - Type of result for a test or test suite
51  * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
52  * @KUNIT_FAILURE: Denotes the test has failed.
53  * @KUNIT_SKIPPED: Denotes the test has been skipped.
54  */
55 enum kunit_status {
56 	KUNIT_SUCCESS,
57 	KUNIT_FAILURE,
58 	KUNIT_SKIPPED,
59 };
60 
61 /**
62  * struct kunit_case - represents an individual test case.
63  *
64  * @run_case: the function representing the actual test case.
65  * @name:     the name of the test case.
66  * @generate_params: the generator function for parameterized tests.
67  *
68  * A test case is a function with the signature,
69  * ``void (*)(struct kunit *)``
70  * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
71  * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
72  * with a &struct kunit_suite and will be run after the suite's init
73  * function and followed by the suite's exit function.
74  *
75  * A test case should be static and should only be created with the
76  * KUNIT_CASE() macro; additionally, every array of test cases should be
77  * terminated with an empty test case.
78  *
79  * Example:
80  *
81  * .. code-block:: c
82  *
83  *	void add_test_basic(struct kunit *test)
84  *	{
85  *		KUNIT_EXPECT_EQ(test, 1, add(1, 0));
86  *		KUNIT_EXPECT_EQ(test, 2, add(1, 1));
87  *		KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
88  *		KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
89  *		KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
90  *	}
91  *
92  *	static struct kunit_case example_test_cases[] = {
93  *		KUNIT_CASE(add_test_basic),
94  *		{}
95  *	};
96  *
97  */
98 struct kunit_case {
99 	void (*run_case)(struct kunit *test);
100 	const char *name;
101 	const void* (*generate_params)(const void *prev, char *desc);
102 
103 	/* private: internal use only. */
104 	enum kunit_status status;
105 	char *log;
106 };
107 
kunit_status_to_ok_not_ok(enum kunit_status status)108 static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
109 {
110 	switch (status) {
111 	case KUNIT_SKIPPED:
112 	case KUNIT_SUCCESS:
113 		return "ok";
114 	case KUNIT_FAILURE:
115 		return "not ok";
116 	}
117 	return "invalid";
118 }
119 
120 /**
121  * KUNIT_CASE - A helper for creating a &struct kunit_case
122  *
123  * @test_name: a reference to a test case function.
124  *
125  * Takes a symbol for a function representing a test case and creates a
126  * &struct kunit_case object from it. See the documentation for
127  * &struct kunit_case for an example on how to use it.
128  */
129 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
130 
131 /**
132  * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
133  *
134  * @test_name: a reference to a test case function.
135  * @gen_params: a reference to a parameter generator function.
136  *
137  * The generator function::
138  *
139  *	const void* gen_params(const void *prev, char *desc)
140  *
141  * is used to lazily generate a series of arbitrarily typed values that fit into
142  * a void*. The argument @prev is the previously returned value, which should be
143  * used to derive the next value; @prev is set to NULL on the initial generator
144  * call. When no more values are available, the generator must return NULL.
145  * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
146  * describing the parameter.
147  */
148 #define KUNIT_CASE_PARAM(test_name, gen_params)			\
149 		{ .run_case = test_name, .name = #test_name,	\
150 		  .generate_params = gen_params }
151 
152 /**
153  * struct kunit_suite - describes a related collection of &struct kunit_case
154  *
155  * @name:	the name of the test. Purely informational.
156  * @suite_init:	called once per test suite before the test cases.
157  * @suite_exit:	called once per test suite after all test cases.
158  * @init:	called before every test case.
159  * @exit:	called after every test case.
160  * @test_cases:	a null terminated array of test cases.
161  *
162  * A kunit_suite is a collection of related &struct kunit_case s, such that
163  * @init is called before every test case and @exit is called after every
164  * test case, similar to the notion of a *test fixture* or a *test class*
165  * in other unit testing frameworks like JUnit or Googletest.
166  *
167  * Every &struct kunit_case must be associated with a kunit_suite for KUnit
168  * to run it.
169  */
170 struct kunit_suite {
171 	const char name[256];
172 	int (*suite_init)(struct kunit_suite *suite);
173 	void (*suite_exit)(struct kunit_suite *suite);
174 	int (*init)(struct kunit *test);
175 	void (*exit)(struct kunit *test);
176 	struct kunit_case *test_cases;
177 
178 	/* private: internal use only */
179 	char status_comment[KUNIT_STATUS_COMMENT_SIZE];
180 	struct dentry *debugfs;
181 	char *log;
182 	int suite_init_err;
183 };
184 
185 /**
186  * struct kunit - represents a running instance of a test.
187  *
188  * @priv: for user to store arbitrary data. Commonly used to pass data
189  *	  created in the init function (see &struct kunit_suite).
190  *
191  * Used to store information about the current context under which the test
192  * is running. Most of this data is private and should only be accessed
193  * indirectly via public functions; the one exception is @priv which can be
194  * used by the test writer to store arbitrary data.
195  */
196 struct kunit {
197 	void *priv;
198 
199 	/* private: internal use only. */
200 	const char *name; /* Read only after initialization! */
201 	char *log; /* Points at case log after initialization */
202 	struct kunit_try_catch try_catch;
203 	/* param_value is the current parameter value for a test case. */
204 	const void *param_value;
205 	/* param_index stores the index of the parameter in parameterized tests. */
206 	int param_index;
207 	/*
208 	 * success starts as true, and may only be set to false during a
209 	 * test case; thus, it is safe to update this across multiple
210 	 * threads using WRITE_ONCE; however, as a consequence, it may only
211 	 * be read after the test case finishes once all threads associated
212 	 * with the test case have terminated.
213 	 */
214 	spinlock_t lock; /* Guards all mutable test state. */
215 	enum kunit_status status; /* Read only after test_case finishes! */
216 	/*
217 	 * Because resources is a list that may be updated multiple times (with
218 	 * new resources) from any thread associated with a test case, we must
219 	 * protect it with some type of lock.
220 	 */
221 	struct list_head resources; /* Protected by lock. */
222 
223 	char status_comment[KUNIT_STATUS_COMMENT_SIZE];
224 };
225 
kunit_set_failure(struct kunit * test)226 static inline void kunit_set_failure(struct kunit *test)
227 {
228 	WRITE_ONCE(test->status, KUNIT_FAILURE);
229 }
230 
231 bool kunit_enabled(void);
232 
233 void kunit_init_test(struct kunit *test, const char *name, char *log);
234 
235 int kunit_run_tests(struct kunit_suite *suite);
236 
237 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
238 
239 unsigned int kunit_test_case_num(struct kunit_suite *suite,
240 				 struct kunit_case *test_case);
241 
242 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
243 
244 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
245 
246 #if IS_BUILTIN(CONFIG_KUNIT)
247 int kunit_run_all_tests(void);
248 #else
kunit_run_all_tests(void)249 static inline int kunit_run_all_tests(void)
250 {
251 	return 0;
252 }
253 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
254 
255 #define __kunit_test_suites(unique_array, ...)				       \
256 	static struct kunit_suite *unique_array[]			       \
257 	__aligned(sizeof(struct kunit_suite *))				       \
258 	__used __section(".kunit_test_suites") = { __VA_ARGS__ }
259 
260 /**
261  * kunit_test_suites() - used to register one or more &struct kunit_suite
262  *			 with KUnit.
263  *
264  * @__suites: a statically allocated list of &struct kunit_suite.
265  *
266  * Registers @suites with the test framework.
267  * This is done by placing the array of struct kunit_suite * in the
268  * .kunit_test_suites ELF section.
269  *
270  * When builtin, KUnit tests are all run via the executor at boot, and when
271  * built as a module, they run on module load.
272  *
273  */
274 #define kunit_test_suites(__suites...)						\
275 	__kunit_test_suites(__UNIQUE_ID(array),				\
276 			    ##__suites)
277 
278 #define kunit_test_suite(suite)	kunit_test_suites(&suite)
279 
280 /**
281  * kunit_test_init_section_suites() - used to register one or more &struct
282  *				      kunit_suite containing init functions or
283  *				      init data.
284  *
285  * @__suites: a statically allocated list of &struct kunit_suite.
286  *
287  * This functions identically as kunit_test_suites() except that it suppresses
288  * modpost warnings for referencing functions marked __init or data marked
289  * __initdata; this is OK because currently KUnit only runs tests upon boot
290  * during the init phase or upon loading a module during the init phase.
291  *
292  * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
293  * tests must be excluded.
294  *
295  * The only thing this macro does that's different from kunit_test_suites is
296  * that it suffixes the array and suite declarations it makes with _probe;
297  * modpost suppresses warnings about referencing init data for symbols named in
298  * this manner.
299  */
300 #define kunit_test_init_section_suites(__suites...)			\
301 	__kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe),	\
302 			    CONCATENATE(__UNIQUE_ID(suites), _probe),	\
303 			    ##__suites)
304 
305 #define kunit_test_init_section_suite(suite)	\
306 	kunit_test_init_section_suites(&suite)
307 
308 #define kunit_suite_for_each_test_case(suite, test_case)		\
309 	for (test_case = suite->test_cases; test_case->run_case; test_case++)
310 
311 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
312 
313 /**
314  * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
315  * @test: The test context object.
316  * @n: number of elements.
317  * @size: The size in bytes of the desired memory.
318  * @gfp: flags passed to underlying kmalloc().
319  *
320  * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
321  * and is automatically cleaned up after the test case concludes. See &struct
322  * kunit_resource for more information.
323  */
324 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
325 
326 /**
327  * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
328  * @test: The test context object.
329  * @size: The size in bytes of the desired memory.
330  * @gfp: flags passed to underlying kmalloc().
331  *
332  * See kmalloc() and kunit_kmalloc_array() for more information.
333  */
kunit_kmalloc(struct kunit * test,size_t size,gfp_t gfp)334 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
335 {
336 	return kunit_kmalloc_array(test, 1, size, gfp);
337 }
338 
339 /**
340  * kunit_kfree() - Like kfree except for allocations managed by KUnit.
341  * @test: The test case to which the resource belongs.
342  * @ptr: The memory allocation to free.
343  */
344 void kunit_kfree(struct kunit *test, const void *ptr);
345 
346 /**
347  * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
348  * @test: The test context object.
349  * @size: The size in bytes of the desired memory.
350  * @gfp: flags passed to underlying kmalloc().
351  *
352  * See kzalloc() and kunit_kmalloc_array() for more information.
353  */
kunit_kzalloc(struct kunit * test,size_t size,gfp_t gfp)354 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
355 {
356 	return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
357 }
358 
359 /**
360  * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
361  * @test: The test context object.
362  * @n: number of elements.
363  * @size: The size in bytes of the desired memory.
364  * @gfp: flags passed to underlying kmalloc().
365  *
366  * See kcalloc() and kunit_kmalloc_array() for more information.
367  */
kunit_kcalloc(struct kunit * test,size_t n,size_t size,gfp_t gfp)368 static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
369 {
370 	return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
371 }
372 
373 void kunit_cleanup(struct kunit *test);
374 
375 void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
376 
377 /**
378  * kunit_mark_skipped() - Marks @test_or_suite as skipped
379  *
380  * @test_or_suite: The test context object.
381  * @fmt:  A printk() style format string.
382  *
383  * Marks the test as skipped. @fmt is given output as the test status
384  * comment, typically the reason the test was skipped.
385  *
386  * Test execution continues after kunit_mark_skipped() is called.
387  */
388 #define kunit_mark_skipped(test_or_suite, fmt, ...)			\
389 	do {								\
390 		WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED);	\
391 		scnprintf((test_or_suite)->status_comment,		\
392 			  KUNIT_STATUS_COMMENT_SIZE,			\
393 			  fmt, ##__VA_ARGS__);				\
394 	} while (0)
395 
396 /**
397  * kunit_skip() - Marks @test_or_suite as skipped
398  *
399  * @test_or_suite: The test context object.
400  * @fmt:  A printk() style format string.
401  *
402  * Skips the test. @fmt is given output as the test status
403  * comment, typically the reason the test was skipped.
404  *
405  * Test execution is halted after kunit_skip() is called.
406  */
407 #define kunit_skip(test_or_suite, fmt, ...)				\
408 	do {								\
409 		kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
410 		kunit_try_catch_throw(&((test_or_suite)->try_catch));	\
411 	} while (0)
412 
413 /*
414  * printk and log to per-test or per-suite log buffer.  Logging only done
415  * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
416  */
417 #define kunit_log(lvl, test_or_suite, fmt, ...)				\
418 	do {								\
419 		printk(lvl fmt, ##__VA_ARGS__);				\
420 		kunit_log_append((test_or_suite)->log,	fmt "\n",	\
421 				 ##__VA_ARGS__);			\
422 	} while (0)
423 
424 #define kunit_printk(lvl, test, fmt, ...)				\
425 	kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt,		\
426 		  (test)->name,	##__VA_ARGS__)
427 
428 /**
429  * kunit_info() - Prints an INFO level message associated with @test.
430  *
431  * @test: The test context object.
432  * @fmt:  A printk() style format string.
433  *
434  * Prints an info level message associated with the test suite being run.
435  * Takes a variable number of format parameters just like printk().
436  */
437 #define kunit_info(test, fmt, ...) \
438 	kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
439 
440 /**
441  * kunit_warn() - Prints a WARN level message associated with @test.
442  *
443  * @test: The test context object.
444  * @fmt:  A printk() style format string.
445  *
446  * Prints a warning level message.
447  */
448 #define kunit_warn(test, fmt, ...) \
449 	kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
450 
451 /**
452  * kunit_err() - Prints an ERROR level message associated with @test.
453  *
454  * @test: The test context object.
455  * @fmt:  A printk() style format string.
456  *
457  * Prints an error level message.
458  */
459 #define kunit_err(test, fmt, ...) \
460 	kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
461 
462 /**
463  * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
464  * @test: The test context object.
465  *
466  * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
467  * words, it does nothing and only exists for code clarity. See
468  * KUNIT_EXPECT_TRUE() for more information.
469  */
470 #define KUNIT_SUCCEED(test) do {} while (0)
471 
472 void kunit_do_failed_assertion(struct kunit *test,
473 			       const struct kunit_loc *loc,
474 			       enum kunit_assert_type type,
475 			       const struct kunit_assert *assert,
476 			       assert_format_t assert_format,
477 			       const char *fmt, ...);
478 
479 #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
480 	static const struct kunit_loc __loc = KUNIT_CURRENT_LOC;	       \
481 	const struct assert_class __assertion = INITIALIZER;		       \
482 	kunit_do_failed_assertion(test,					       \
483 				  &__loc,				       \
484 				  assert_type,				       \
485 				  &__assertion.assert,			       \
486 				  assert_format,			       \
487 				  fmt,					       \
488 				  ##__VA_ARGS__);			       \
489 } while (0)
490 
491 
492 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...)		       \
493 	_KUNIT_FAILED(test,						       \
494 		      assert_type,					       \
495 		      kunit_fail_assert,				       \
496 		      kunit_fail_assert_format,				       \
497 		      {},						       \
498 		      fmt,						       \
499 		      ##__VA_ARGS__)
500 
501 /**
502  * KUNIT_FAIL() - Always causes a test to fail when evaluated.
503  * @test: The test context object.
504  * @fmt: an informational message to be printed when the assertion is made.
505  * @...: string format arguments.
506  *
507  * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
508  * other words, it always results in a failed expectation, and consequently
509  * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
510  * for more information.
511  */
512 #define KUNIT_FAIL(test, fmt, ...)					       \
513 	KUNIT_FAIL_ASSERTION(test,					       \
514 			     KUNIT_EXPECTATION,				       \
515 			     fmt,					       \
516 			     ##__VA_ARGS__)
517 
518 #define KUNIT_UNARY_ASSERTION(test,					       \
519 			      assert_type,				       \
520 			      condition,				       \
521 			      expected_true,				       \
522 			      fmt,					       \
523 			      ...)					       \
524 do {									       \
525 	if (likely(!!(condition) == !!expected_true))			       \
526 		break;							       \
527 									       \
528 	_KUNIT_FAILED(test,						       \
529 		      assert_type,					       \
530 		      kunit_unary_assert,				       \
531 		      kunit_unary_assert_format,			       \
532 		      KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition,	       \
533 						     expected_true),	       \
534 		      fmt,						       \
535 		      ##__VA_ARGS__);					       \
536 } while (0)
537 
538 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)       \
539 	KUNIT_UNARY_ASSERTION(test,					       \
540 			      assert_type,				       \
541 			      condition,				       \
542 			      true,					       \
543 			      fmt,					       \
544 			      ##__VA_ARGS__)
545 
546 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)      \
547 	KUNIT_UNARY_ASSERTION(test,					       \
548 			      assert_type,				       \
549 			      condition,				       \
550 			      false,					       \
551 			      fmt,					       \
552 			      ##__VA_ARGS__)
553 
554 /*
555  * A factory macro for defining the assertions and expectations for the basic
556  * comparisons defined for the built in types.
557  *
558  * Unfortunately, there is no common type that all types can be promoted to for
559  * which all the binary operators behave the same way as for the actual types
560  * (for example, there is no type that long long and unsigned long long can
561  * both be cast to where the comparison result is preserved for all values). So
562  * the best we can do is do the comparison in the original types and then coerce
563  * everything to long long for printing; this way, the comparison behaves
564  * correctly and the printed out value usually makes sense without
565  * interpretation, but can always be interpreted to figure out the actual
566  * value.
567  */
568 #define KUNIT_BASE_BINARY_ASSERTION(test,				       \
569 				    assert_class,			       \
570 				    format_func,			       \
571 				    assert_type,			       \
572 				    left,				       \
573 				    op,					       \
574 				    right,				       \
575 				    fmt,				       \
576 				    ...)				       \
577 do {									       \
578 	const typeof(left) __left = (left);				       \
579 	const typeof(right) __right = (right);				       \
580 	static const struct kunit_binary_assert_text __text = {		       \
581 		.operation = #op,					       \
582 		.left_text = #left,					       \
583 		.right_text = #right,					       \
584 	};								       \
585 									       \
586 	if (likely(__left op __right))					       \
587 		break;							       \
588 									       \
589 	_KUNIT_FAILED(test,						       \
590 		      assert_type,					       \
591 		      assert_class,					       \
592 		      format_func,					       \
593 		      KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text,		       \
594 						      __left,		       \
595 						      __right),		       \
596 		      fmt,						       \
597 		      ##__VA_ARGS__);					       \
598 } while (0)
599 
600 #define KUNIT_BINARY_INT_ASSERTION(test,				       \
601 				   assert_type,				       \
602 				   left,				       \
603 				   op,					       \
604 				   right,				       \
605 				   fmt,					       \
606 				    ...)				       \
607 	KUNIT_BASE_BINARY_ASSERTION(test,				       \
608 				    kunit_binary_assert,		       \
609 				    kunit_binary_assert_format,		       \
610 				    assert_type,			       \
611 				    left, op, right,			       \
612 				    fmt,				       \
613 				    ##__VA_ARGS__)
614 
615 #define KUNIT_BINARY_PTR_ASSERTION(test,				       \
616 				   assert_type,				       \
617 				   left,				       \
618 				   op,					       \
619 				   right,				       \
620 				   fmt,					       \
621 				    ...)				       \
622 	KUNIT_BASE_BINARY_ASSERTION(test,				       \
623 				    kunit_binary_ptr_assert,		       \
624 				    kunit_binary_ptr_assert_format,	       \
625 				    assert_type,			       \
626 				    left, op, right,			       \
627 				    fmt,				       \
628 				    ##__VA_ARGS__)
629 
630 #define KUNIT_BINARY_STR_ASSERTION(test,				       \
631 				   assert_type,				       \
632 				   left,				       \
633 				   op,					       \
634 				   right,				       \
635 				   fmt,					       \
636 				   ...)					       \
637 do {									       \
638 	const char *__left = (left);					       \
639 	const char *__right = (right);					       \
640 	static const struct kunit_binary_assert_text __text = {		       \
641 		.operation = #op,					       \
642 		.left_text = #left,					       \
643 		.right_text = #right,					       \
644 	};								       \
645 									       \
646 	if (likely(strcmp(__left, __right) op 0))			       \
647 		break;							       \
648 									       \
649 									       \
650 	_KUNIT_FAILED(test,						       \
651 		      assert_type,					       \
652 		      kunit_binary_str_assert,				       \
653 		      kunit_binary_str_assert_format,			       \
654 		      KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text,		       \
655 						      __left,		       \
656 						      __right),		       \
657 		      fmt,						       \
658 		      ##__VA_ARGS__);					       \
659 } while (0)
660 
661 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
662 						assert_type,		       \
663 						ptr,			       \
664 						fmt,			       \
665 						...)			       \
666 do {									       \
667 	const typeof(ptr) __ptr = (ptr);				       \
668 									       \
669 	if (!IS_ERR_OR_NULL(__ptr))					       \
670 		break;							       \
671 									       \
672 	_KUNIT_FAILED(test,						       \
673 		      assert_type,					       \
674 		      kunit_ptr_not_err_assert,				       \
675 		      kunit_ptr_not_err_assert_format,			       \
676 		      KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr),	       \
677 		      fmt,						       \
678 		      ##__VA_ARGS__);					       \
679 } while (0)
680 
681 /**
682  * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
683  * @test: The test context object.
684  * @condition: an arbitrary boolean expression. The test fails when this does
685  * not evaluate to true.
686  *
687  * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
688  * to fail when the specified condition is not met; however, it will not prevent
689  * the test case from continuing to run; this is otherwise known as an
690  * *expectation failure*.
691  */
692 #define KUNIT_EXPECT_TRUE(test, condition) \
693 	KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
694 
695 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...)		       \
696 	KUNIT_TRUE_MSG_ASSERTION(test,					       \
697 				 KUNIT_EXPECTATION,			       \
698 				 condition,				       \
699 				 fmt,					       \
700 				 ##__VA_ARGS__)
701 
702 /**
703  * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
704  * @test: The test context object.
705  * @condition: an arbitrary boolean expression. The test fails when this does
706  * not evaluate to false.
707  *
708  * Sets an expectation that @condition evaluates to false. See
709  * KUNIT_EXPECT_TRUE() for more information.
710  */
711 #define KUNIT_EXPECT_FALSE(test, condition) \
712 	KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
713 
714 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...)		       \
715 	KUNIT_FALSE_MSG_ASSERTION(test,					       \
716 				  KUNIT_EXPECTATION,			       \
717 				  condition,				       \
718 				  fmt,					       \
719 				  ##__VA_ARGS__)
720 
721 /**
722  * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
723  * @test: The test context object.
724  * @left: an arbitrary expression that evaluates to a primitive C type.
725  * @right: an arbitrary expression that evaluates to a primitive C type.
726  *
727  * Sets an expectation that the values that @left and @right evaluate to are
728  * equal. This is semantically equivalent to
729  * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
730  * more information.
731  */
732 #define KUNIT_EXPECT_EQ(test, left, right) \
733 	KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
734 
735 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...)		       \
736 	KUNIT_BINARY_INT_ASSERTION(test,				       \
737 				   KUNIT_EXPECTATION,			       \
738 				   left, ==, right,			       \
739 				   fmt,					       \
740 				    ##__VA_ARGS__)
741 
742 /**
743  * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
744  * @test: The test context object.
745  * @left: an arbitrary expression that evaluates to a pointer.
746  * @right: an arbitrary expression that evaluates to a pointer.
747  *
748  * Sets an expectation that the values that @left and @right evaluate to are
749  * equal. This is semantically equivalent to
750  * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
751  * more information.
752  */
753 #define KUNIT_EXPECT_PTR_EQ(test, left, right)				       \
754 	KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
755 
756 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
757 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
758 				   KUNIT_EXPECTATION,			       \
759 				   left, ==, right,			       \
760 				   fmt,					       \
761 				   ##__VA_ARGS__)
762 
763 /**
764  * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
765  * @test: The test context object.
766  * @left: an arbitrary expression that evaluates to a primitive C type.
767  * @right: an arbitrary expression that evaluates to a primitive C type.
768  *
769  * Sets an expectation that the values that @left and @right evaluate to are not
770  * equal. This is semantically equivalent to
771  * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
772  * more information.
773  */
774 #define KUNIT_EXPECT_NE(test, left, right) \
775 	KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
776 
777 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...)		       \
778 	KUNIT_BINARY_INT_ASSERTION(test,				       \
779 				   KUNIT_EXPECTATION,			       \
780 				   left, !=, right,			       \
781 				   fmt,					       \
782 				    ##__VA_ARGS__)
783 
784 /**
785  * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
786  * @test: The test context object.
787  * @left: an arbitrary expression that evaluates to a pointer.
788  * @right: an arbitrary expression that evaluates to a pointer.
789  *
790  * Sets an expectation that the values that @left and @right evaluate to are not
791  * equal. This is semantically equivalent to
792  * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
793  * more information.
794  */
795 #define KUNIT_EXPECT_PTR_NE(test, left, right)				       \
796 	KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
797 
798 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
799 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
800 				   KUNIT_EXPECTATION,			       \
801 				   left, !=, right,			       \
802 				   fmt,					       \
803 				   ##__VA_ARGS__)
804 
805 /**
806  * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
807  * @test: The test context object.
808  * @left: an arbitrary expression that evaluates to a primitive C type.
809  * @right: an arbitrary expression that evaluates to a primitive C type.
810  *
811  * Sets an expectation that the value that @left evaluates to is less than the
812  * value that @right evaluates to. This is semantically equivalent to
813  * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
814  * more information.
815  */
816 #define KUNIT_EXPECT_LT(test, left, right) \
817 	KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
818 
819 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...)		       \
820 	KUNIT_BINARY_INT_ASSERTION(test,				       \
821 				   KUNIT_EXPECTATION,			       \
822 				   left, <, right,			       \
823 				   fmt,					       \
824 				    ##__VA_ARGS__)
825 
826 /**
827  * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
828  * @test: The test context object.
829  * @left: an arbitrary expression that evaluates to a primitive C type.
830  * @right: an arbitrary expression that evaluates to a primitive C type.
831  *
832  * Sets an expectation that the value that @left evaluates to is less than or
833  * equal to the value that @right evaluates to. Semantically this is equivalent
834  * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
835  * more information.
836  */
837 #define KUNIT_EXPECT_LE(test, left, right) \
838 	KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
839 
840 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...)		       \
841 	KUNIT_BINARY_INT_ASSERTION(test,				       \
842 				   KUNIT_EXPECTATION,			       \
843 				   left, <=, right,			       \
844 				   fmt,					       \
845 				    ##__VA_ARGS__)
846 
847 /**
848  * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
849  * @test: The test context object.
850  * @left: an arbitrary expression that evaluates to a primitive C type.
851  * @right: an arbitrary expression that evaluates to a primitive C type.
852  *
853  * Sets an expectation that the value that @left evaluates to is greater than
854  * the value that @right evaluates to. This is semantically equivalent to
855  * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
856  * more information.
857  */
858 #define KUNIT_EXPECT_GT(test, left, right) \
859 	KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
860 
861 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...)		       \
862 	KUNIT_BINARY_INT_ASSERTION(test,				       \
863 				   KUNIT_EXPECTATION,			       \
864 				   left, >, right,			       \
865 				   fmt,					       \
866 				    ##__VA_ARGS__)
867 
868 /**
869  * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
870  * @test: The test context object.
871  * @left: an arbitrary expression that evaluates to a primitive C type.
872  * @right: an arbitrary expression that evaluates to a primitive C type.
873  *
874  * Sets an expectation that the value that @left evaluates to is greater than
875  * the value that @right evaluates to. This is semantically equivalent to
876  * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
877  * more information.
878  */
879 #define KUNIT_EXPECT_GE(test, left, right) \
880 	KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
881 
882 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...)		       \
883 	KUNIT_BINARY_INT_ASSERTION(test,				       \
884 				   KUNIT_EXPECTATION,			       \
885 				   left, >=, right,			       \
886 				   fmt,					       \
887 				    ##__VA_ARGS__)
888 
889 /**
890  * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
891  * @test: The test context object.
892  * @left: an arbitrary expression that evaluates to a null terminated string.
893  * @right: an arbitrary expression that evaluates to a null terminated string.
894  *
895  * Sets an expectation that the values that @left and @right evaluate to are
896  * equal. This is semantically equivalent to
897  * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
898  * for more information.
899  */
900 #define KUNIT_EXPECT_STREQ(test, left, right) \
901 	KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
902 
903 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...)		       \
904 	KUNIT_BINARY_STR_ASSERTION(test,				       \
905 				   KUNIT_EXPECTATION,			       \
906 				   left, ==, right,			       \
907 				   fmt,					       \
908 				   ##__VA_ARGS__)
909 
910 /**
911  * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
912  * @test: The test context object.
913  * @left: an arbitrary expression that evaluates to a null terminated string.
914  * @right: an arbitrary expression that evaluates to a null terminated string.
915  *
916  * Sets an expectation that the values that @left and @right evaluate to are
917  * not equal. This is semantically equivalent to
918  * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
919  * for more information.
920  */
921 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
922 	KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
923 
924 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
925 	KUNIT_BINARY_STR_ASSERTION(test,				       \
926 				   KUNIT_EXPECTATION,			       \
927 				   left, !=, right,			       \
928 				   fmt,					       \
929 				   ##__VA_ARGS__)
930 
931 /**
932  * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
933  * @test: The test context object.
934  * @ptr: an arbitrary pointer.
935  *
936  * Sets an expectation that the value that @ptr evaluates to is null. This is
937  * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
938  * See KUNIT_EXPECT_TRUE() for more information.
939  */
940 #define KUNIT_EXPECT_NULL(test, ptr)				               \
941 	KUNIT_EXPECT_NULL_MSG(test,					       \
942 			      ptr,					       \
943 			      NULL)
944 
945 #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...)	                       \
946 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
947 				   KUNIT_EXPECTATION,			       \
948 				   ptr, ==, NULL,			       \
949 				   fmt,					       \
950 				   ##__VA_ARGS__)
951 
952 /**
953  * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
954  * @test: The test context object.
955  * @ptr: an arbitrary pointer.
956  *
957  * Sets an expectation that the value that @ptr evaluates to is not null. This
958  * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
959  * See KUNIT_EXPECT_TRUE() for more information.
960  */
961 #define KUNIT_EXPECT_NOT_NULL(test, ptr)			               \
962 	KUNIT_EXPECT_NOT_NULL_MSG(test,					       \
963 				  ptr,					       \
964 				  NULL)
965 
966 #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...)	                       \
967 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
968 				   KUNIT_EXPECTATION,			       \
969 				   ptr, !=, NULL,			       \
970 				   fmt,					       \
971 				   ##__VA_ARGS__)
972 
973 /**
974  * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
975  * @test: The test context object.
976  * @ptr: an arbitrary pointer.
977  *
978  * Sets an expectation that the value that @ptr evaluates to is not null and not
979  * an errno stored in a pointer. This is semantically equivalent to
980  * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
981  * more information.
982  */
983 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
984 	KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
985 
986 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
987 	KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
988 						KUNIT_EXPECTATION,	       \
989 						ptr,			       \
990 						fmt,			       \
991 						##__VA_ARGS__)
992 
993 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
994 	KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
995 
996 /**
997  * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
998  * @test: The test context object.
999  * @condition: an arbitrary boolean expression. The test fails and aborts when
1000  * this does not evaluate to true.
1001  *
1002  * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1003  * fail *and immediately abort* when the specified condition is not met. Unlike
1004  * an expectation failure, it will prevent the test case from continuing to run;
1005  * this is otherwise known as an *assertion failure*.
1006  */
1007 #define KUNIT_ASSERT_TRUE(test, condition) \
1008 	KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
1009 
1010 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...)		       \
1011 	KUNIT_TRUE_MSG_ASSERTION(test,					       \
1012 				 KUNIT_ASSERTION,			       \
1013 				 condition,				       \
1014 				 fmt,					       \
1015 				 ##__VA_ARGS__)
1016 
1017 /**
1018  * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1019  * @test: The test context object.
1020  * @condition: an arbitrary boolean expression.
1021  *
1022  * Sets an assertion that the value that @condition evaluates to is false. This
1023  * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1024  * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1025  */
1026 #define KUNIT_ASSERT_FALSE(test, condition) \
1027 	KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
1028 
1029 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...)		       \
1030 	KUNIT_FALSE_MSG_ASSERTION(test,					       \
1031 				  KUNIT_ASSERTION,			       \
1032 				  condition,				       \
1033 				  fmt,					       \
1034 				  ##__VA_ARGS__)
1035 
1036 /**
1037  * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1038  * @test: The test context object.
1039  * @left: an arbitrary expression that evaluates to a primitive C type.
1040  * @right: an arbitrary expression that evaluates to a primitive C type.
1041  *
1042  * Sets an assertion that the values that @left and @right evaluate to are
1043  * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1044  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1045  */
1046 #define KUNIT_ASSERT_EQ(test, left, right) \
1047 	KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
1048 
1049 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...)		       \
1050 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1051 				   KUNIT_ASSERTION,			       \
1052 				   left, ==, right,			       \
1053 				   fmt,					       \
1054 				    ##__VA_ARGS__)
1055 
1056 /**
1057  * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1058  * @test: The test context object.
1059  * @left: an arbitrary expression that evaluates to a pointer.
1060  * @right: an arbitrary expression that evaluates to a pointer.
1061  *
1062  * Sets an assertion that the values that @left and @right evaluate to are
1063  * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1064  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1065  */
1066 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1067 	KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
1068 
1069 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
1070 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1071 				   KUNIT_ASSERTION,			       \
1072 				   left, ==, right,			       \
1073 				   fmt,					       \
1074 				   ##__VA_ARGS__)
1075 
1076 /**
1077  * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1078  * @test: The test context object.
1079  * @left: an arbitrary expression that evaluates to a primitive C type.
1080  * @right: an arbitrary expression that evaluates to a primitive C type.
1081  *
1082  * Sets an assertion that the values that @left and @right evaluate to are not
1083  * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1084  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1085  */
1086 #define KUNIT_ASSERT_NE(test, left, right) \
1087 	KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
1088 
1089 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...)		       \
1090 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1091 				   KUNIT_ASSERTION,			       \
1092 				   left, !=, right,			       \
1093 				   fmt,					       \
1094 				    ##__VA_ARGS__)
1095 
1096 /**
1097  * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1098  * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1099  * @test: The test context object.
1100  * @left: an arbitrary expression that evaluates to a pointer.
1101  * @right: an arbitrary expression that evaluates to a pointer.
1102  *
1103  * Sets an assertion that the values that @left and @right evaluate to are not
1104  * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1105  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1106  */
1107 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1108 	KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1109 
1110 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
1111 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1112 				   KUNIT_ASSERTION,			       \
1113 				   left, !=, right,			       \
1114 				   fmt,					       \
1115 				   ##__VA_ARGS__)
1116 /**
1117  * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1118  * @test: The test context object.
1119  * @left: an arbitrary expression that evaluates to a primitive C type.
1120  * @right: an arbitrary expression that evaluates to a primitive C type.
1121  *
1122  * Sets an assertion that the value that @left evaluates to is less than the
1123  * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1124  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1125  * is not met.
1126  */
1127 #define KUNIT_ASSERT_LT(test, left, right) \
1128 	KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1129 
1130 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...)		       \
1131 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1132 				   KUNIT_ASSERTION,			       \
1133 				   left, <, right,			       \
1134 				   fmt,					       \
1135 				    ##__VA_ARGS__)
1136 /**
1137  * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1138  * @test: The test context object.
1139  * @left: an arbitrary expression that evaluates to a primitive C type.
1140  * @right: an arbitrary expression that evaluates to a primitive C type.
1141  *
1142  * Sets an assertion that the value that @left evaluates to is less than or
1143  * equal to the value that @right evaluates to. This is the same as
1144  * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1145  * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1146  */
1147 #define KUNIT_ASSERT_LE(test, left, right) \
1148 	KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1149 
1150 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...)		       \
1151 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1152 				   KUNIT_ASSERTION,			       \
1153 				   left, <=, right,			       \
1154 				   fmt,					       \
1155 				    ##__VA_ARGS__)
1156 
1157 /**
1158  * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1159  * @test: The test context object.
1160  * @left: an arbitrary expression that evaluates to a primitive C type.
1161  * @right: an arbitrary expression that evaluates to a primitive C type.
1162  *
1163  * Sets an assertion that the value that @left evaluates to is greater than the
1164  * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1165  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1166  * is not met.
1167  */
1168 #define KUNIT_ASSERT_GT(test, left, right) \
1169 	KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1170 
1171 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...)		       \
1172 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1173 				   KUNIT_ASSERTION,			       \
1174 				   left, >, right,			       \
1175 				   fmt,					       \
1176 				    ##__VA_ARGS__)
1177 
1178 /**
1179  * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1180  * @test: The test context object.
1181  * @left: an arbitrary expression that evaluates to a primitive C type.
1182  * @right: an arbitrary expression that evaluates to a primitive C type.
1183  *
1184  * Sets an assertion that the value that @left evaluates to is greater than the
1185  * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1186  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1187  * is not met.
1188  */
1189 #define KUNIT_ASSERT_GE(test, left, right) \
1190 	KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1191 
1192 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...)		       \
1193 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1194 				   KUNIT_ASSERTION,			       \
1195 				   left, >=, right,			       \
1196 				   fmt,					       \
1197 				    ##__VA_ARGS__)
1198 
1199 /**
1200  * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1201  * @test: The test context object.
1202  * @left: an arbitrary expression that evaluates to a null terminated string.
1203  * @right: an arbitrary expression that evaluates to a null terminated string.
1204  *
1205  * Sets an assertion that the values that @left and @right evaluate to are
1206  * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1207  * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1208  */
1209 #define KUNIT_ASSERT_STREQ(test, left, right) \
1210 	KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1211 
1212 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...)		       \
1213 	KUNIT_BINARY_STR_ASSERTION(test,				       \
1214 				   KUNIT_ASSERTION,			       \
1215 				   left, ==, right,			       \
1216 				   fmt,					       \
1217 				   ##__VA_ARGS__)
1218 
1219 /**
1220  * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1221  * @test: The test context object.
1222  * @left: an arbitrary expression that evaluates to a null terminated string.
1223  * @right: an arbitrary expression that evaluates to a null terminated string.
1224  *
1225  * Sets an expectation that the values that @left and @right evaluate to are
1226  * not equal. This is semantically equivalent to
1227  * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1228  * for more information.
1229  */
1230 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1231 	KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1232 
1233 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
1234 	KUNIT_BINARY_STR_ASSERTION(test,				       \
1235 				   KUNIT_ASSERTION,			       \
1236 				   left, !=, right,			       \
1237 				   fmt,					       \
1238 				   ##__VA_ARGS__)
1239 
1240 /**
1241  * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1242  * @test: The test context object.
1243  * @ptr: an arbitrary pointer.
1244  *
1245  * Sets an assertion that the values that @ptr evaluates to is null. This is
1246  * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1247  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1248  */
1249 #define KUNIT_ASSERT_NULL(test, ptr) \
1250 	KUNIT_ASSERT_NULL_MSG(test,					       \
1251 			      ptr,					       \
1252 			      NULL)
1253 
1254 #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1255 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1256 				   KUNIT_ASSERTION,			       \
1257 				   ptr, ==, NULL,			       \
1258 				   fmt,					       \
1259 				   ##__VA_ARGS__)
1260 
1261 /**
1262  * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1263  * @test: The test context object.
1264  * @ptr: an arbitrary pointer.
1265  *
1266  * Sets an assertion that the values that @ptr evaluates to is not null. This
1267  * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1268  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1269  */
1270 #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1271 	KUNIT_ASSERT_NOT_NULL_MSG(test,					       \
1272 				  ptr,					       \
1273 				  NULL)
1274 
1275 #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1276 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1277 				   KUNIT_ASSERTION,			       \
1278 				   ptr, !=, NULL,			       \
1279 				   fmt,					       \
1280 				   ##__VA_ARGS__)
1281 
1282 /**
1283  * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1284  * @test: The test context object.
1285  * @ptr: an arbitrary pointer.
1286  *
1287  * Sets an assertion that the value that @ptr evaluates to is not null and not
1288  * an errno stored in a pointer. This is the same as
1289  * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1290  * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1291  */
1292 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1293 	KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1294 
1295 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
1296 	KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
1297 						KUNIT_ASSERTION,	       \
1298 						ptr,			       \
1299 						fmt,			       \
1300 						##__VA_ARGS__)
1301 
1302 /**
1303  * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1304  * @name:  prefix for the test parameter generator function.
1305  * @array: array of test parameters.
1306  * @get_desc: function to convert param to description; NULL to use default
1307  *
1308  * Define function @name_gen_params which uses @array to generate parameters.
1309  */
1310 #define KUNIT_ARRAY_PARAM(name, array, get_desc)						\
1311 	static const void *name##_gen_params(const void *prev, char *desc)			\
1312 	{											\
1313 		typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array);	\
1314 		if (__next - (array) < ARRAY_SIZE((array))) {					\
1315 			void (*__get_desc)(typeof(__next), char *) = get_desc;			\
1316 			if (__get_desc)								\
1317 				__get_desc(__next, desc);					\
1318 			return __next;								\
1319 		}										\
1320 		return NULL;									\
1321 	}
1322 
1323 // TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1324 // include resource.h themselves if they need it.
1325 #include <kunit/resource.h>
1326 
1327 #endif /* _KUNIT_TEST_H */
1328