xref: /linux/include/kunit/test.h (revision 6224db7881936c8e1c3b352b5debbbbd8856911a)
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 
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 
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
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  */
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  */
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  */
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 			       const char *fmt, ...);
477 
478 #define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
479 	if (unlikely(!(pass))) {					       \
480 		static const struct kunit_loc __loc = KUNIT_CURRENT_LOC;       \
481 		struct assert_class __assertion = INITIALIZER;		       \
482 		kunit_do_failed_assertion(test,				       \
483 					  &__loc,			       \
484 					  assert_type,			       \
485 					  &__assertion.assert,		       \
486 					  fmt,				       \
487 					  ##__VA_ARGS__);		       \
488 	}								       \
489 } while (0)
490 
491 
492 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...)		       \
493 	KUNIT_ASSERTION(test,						       \
494 			assert_type,					       \
495 			false,						       \
496 			kunit_fail_assert,				       \
497 			KUNIT_INIT_FAIL_ASSERT_STRUCT,			       \
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 	KUNIT_ASSERTION(test,						       \
525 			assert_type,					       \
526 			!!(condition) == !!expected_true,		       \
527 			kunit_unary_assert,				       \
528 			KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition,	       \
529 						       expected_true),	       \
530 			fmt,						       \
531 			##__VA_ARGS__)
532 
533 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)       \
534 	KUNIT_UNARY_ASSERTION(test,					       \
535 			      assert_type,				       \
536 			      condition,				       \
537 			      true,					       \
538 			      fmt,					       \
539 			      ##__VA_ARGS__)
540 
541 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)      \
542 	KUNIT_UNARY_ASSERTION(test,					       \
543 			      assert_type,				       \
544 			      condition,				       \
545 			      false,					       \
546 			      fmt,					       \
547 			      ##__VA_ARGS__)
548 
549 /*
550  * A factory macro for defining the assertions and expectations for the basic
551  * comparisons defined for the built in types.
552  *
553  * Unfortunately, there is no common type that all types can be promoted to for
554  * which all the binary operators behave the same way as for the actual types
555  * (for example, there is no type that long long and unsigned long long can
556  * both be cast to where the comparison result is preserved for all values). So
557  * the best we can do is do the comparison in the original types and then coerce
558  * everything to long long for printing; this way, the comparison behaves
559  * correctly and the printed out value usually makes sense without
560  * interpretation, but can always be interpreted to figure out the actual
561  * value.
562  */
563 #define KUNIT_BASE_BINARY_ASSERTION(test,				       \
564 				    assert_class,			       \
565 				    format_func,			       \
566 				    assert_type,			       \
567 				    left,				       \
568 				    op,					       \
569 				    right,				       \
570 				    fmt,				       \
571 				    ...)				       \
572 do {									       \
573 	const typeof(left) __left = (left);				       \
574 	const typeof(right) __right = (right);				       \
575 	static const struct kunit_binary_assert_text __text = {		       \
576 		.operation = #op,					       \
577 		.left_text = #left,					       \
578 		.right_text = #right,					       \
579 	};								       \
580 									       \
581 	KUNIT_ASSERTION(test,						       \
582 			assert_type,					       \
583 			__left op __right,				       \
584 			assert_class,					       \
585 			KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func,	       \
586 							&__text,	       \
587 							__left,		       \
588 							__right),	       \
589 			fmt,						       \
590 			##__VA_ARGS__);					       \
591 } while (0)
592 
593 #define KUNIT_BINARY_INT_ASSERTION(test,				       \
594 				   assert_type,				       \
595 				   left,				       \
596 				   op,					       \
597 				   right,				       \
598 				   fmt,					       \
599 				    ...)				       \
600 	KUNIT_BASE_BINARY_ASSERTION(test,				       \
601 				    kunit_binary_assert,		       \
602 				    kunit_binary_assert_format,		       \
603 				    assert_type,			       \
604 				    left, op, right,			       \
605 				    fmt,				       \
606 				    ##__VA_ARGS__)
607 
608 #define KUNIT_BINARY_PTR_ASSERTION(test,				       \
609 				   assert_type,				       \
610 				   left,				       \
611 				   op,					       \
612 				   right,				       \
613 				   fmt,					       \
614 				    ...)				       \
615 	KUNIT_BASE_BINARY_ASSERTION(test,				       \
616 				    kunit_binary_ptr_assert,		       \
617 				    kunit_binary_ptr_assert_format,	       \
618 				    assert_type,			       \
619 				    left, op, right,			       \
620 				    fmt,				       \
621 				    ##__VA_ARGS__)
622 
623 #define KUNIT_BINARY_STR_ASSERTION(test,				       \
624 				   assert_type,				       \
625 				   left,				       \
626 				   op,					       \
627 				   right,				       \
628 				   fmt,					       \
629 				   ...)					       \
630 do {									       \
631 	const char *__left = (left);					       \
632 	const char *__right = (right);					       \
633 	static const struct kunit_binary_assert_text __text = {		       \
634 		.operation = #op,					       \
635 		.left_text = #left,					       \
636 		.right_text = #right,					       \
637 	};								       \
638 									       \
639 	KUNIT_ASSERTION(test,						       \
640 			assert_type,					       \
641 			strcmp(__left, __right) op 0,			       \
642 			kunit_binary_str_assert,			       \
643 			KUNIT_INIT_BINARY_ASSERT_STRUCT(kunit_binary_str_assert_format,\
644 							&__text,	       \
645 							__left,		       \
646 							__right),	       \
647 			fmt,						       \
648 			##__VA_ARGS__);					       \
649 } while (0)
650 
651 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
652 						assert_type,		       \
653 						ptr,			       \
654 						fmt,			       \
655 						...)			       \
656 do {									       \
657 	const typeof(ptr) __ptr = (ptr);				       \
658 									       \
659 	KUNIT_ASSERTION(test,						       \
660 			assert_type,					       \
661 			!IS_ERR_OR_NULL(__ptr),				       \
662 			kunit_ptr_not_err_assert,			       \
663 			KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr,		       \
664 						      __ptr),		       \
665 			fmt,						       \
666 			##__VA_ARGS__);					       \
667 } while (0)
668 
669 /**
670  * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
671  * @test: The test context object.
672  * @condition: an arbitrary boolean expression. The test fails when this does
673  * not evaluate to true.
674  *
675  * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
676  * to fail when the specified condition is not met; however, it will not prevent
677  * the test case from continuing to run; this is otherwise known as an
678  * *expectation failure*.
679  */
680 #define KUNIT_EXPECT_TRUE(test, condition) \
681 	KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
682 
683 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...)		       \
684 	KUNIT_TRUE_MSG_ASSERTION(test,					       \
685 				 KUNIT_EXPECTATION,			       \
686 				 condition,				       \
687 				 fmt,					       \
688 				 ##__VA_ARGS__)
689 
690 /**
691  * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
692  * @test: The test context object.
693  * @condition: an arbitrary boolean expression. The test fails when this does
694  * not evaluate to false.
695  *
696  * Sets an expectation that @condition evaluates to false. See
697  * KUNIT_EXPECT_TRUE() for more information.
698  */
699 #define KUNIT_EXPECT_FALSE(test, condition) \
700 	KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
701 
702 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...)		       \
703 	KUNIT_FALSE_MSG_ASSERTION(test,					       \
704 				  KUNIT_EXPECTATION,			       \
705 				  condition,				       \
706 				  fmt,					       \
707 				  ##__VA_ARGS__)
708 
709 /**
710  * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
711  * @test: The test context object.
712  * @left: an arbitrary expression that evaluates to a primitive C type.
713  * @right: an arbitrary expression that evaluates to a primitive C type.
714  *
715  * Sets an expectation that the values that @left and @right evaluate to are
716  * equal. This is semantically equivalent to
717  * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
718  * more information.
719  */
720 #define KUNIT_EXPECT_EQ(test, left, right) \
721 	KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
722 
723 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...)		       \
724 	KUNIT_BINARY_INT_ASSERTION(test,				       \
725 				   KUNIT_EXPECTATION,			       \
726 				   left, ==, right,			       \
727 				   fmt,					       \
728 				    ##__VA_ARGS__)
729 
730 /**
731  * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
732  * @test: The test context object.
733  * @left: an arbitrary expression that evaluates to a pointer.
734  * @right: an arbitrary expression that evaluates to a pointer.
735  *
736  * Sets an expectation that the values that @left and @right evaluate to are
737  * equal. This is semantically equivalent to
738  * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
739  * more information.
740  */
741 #define KUNIT_EXPECT_PTR_EQ(test, left, right)				       \
742 	KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
743 
744 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
745 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
746 				   KUNIT_EXPECTATION,			       \
747 				   left, ==, right,			       \
748 				   fmt,					       \
749 				   ##__VA_ARGS__)
750 
751 /**
752  * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
753  * @test: The test context object.
754  * @left: an arbitrary expression that evaluates to a primitive C type.
755  * @right: an arbitrary expression that evaluates to a primitive C type.
756  *
757  * Sets an expectation that the values that @left and @right evaluate to are not
758  * equal. This is semantically equivalent to
759  * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
760  * more information.
761  */
762 #define KUNIT_EXPECT_NE(test, left, right) \
763 	KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
764 
765 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...)		       \
766 	KUNIT_BINARY_INT_ASSERTION(test,				       \
767 				   KUNIT_EXPECTATION,			       \
768 				   left, !=, right,			       \
769 				   fmt,					       \
770 				    ##__VA_ARGS__)
771 
772 /**
773  * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
774  * @test: The test context object.
775  * @left: an arbitrary expression that evaluates to a pointer.
776  * @right: an arbitrary expression that evaluates to a pointer.
777  *
778  * Sets an expectation that the values that @left and @right evaluate to are not
779  * equal. This is semantically equivalent to
780  * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
781  * more information.
782  */
783 #define KUNIT_EXPECT_PTR_NE(test, left, right)				       \
784 	KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
785 
786 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
787 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
788 				   KUNIT_EXPECTATION,			       \
789 				   left, !=, right,			       \
790 				   fmt,					       \
791 				   ##__VA_ARGS__)
792 
793 /**
794  * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
795  * @test: The test context object.
796  * @left: an arbitrary expression that evaluates to a primitive C type.
797  * @right: an arbitrary expression that evaluates to a primitive C type.
798  *
799  * Sets an expectation that the value that @left evaluates to is less than the
800  * value that @right evaluates to. This is semantically equivalent to
801  * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
802  * more information.
803  */
804 #define KUNIT_EXPECT_LT(test, left, right) \
805 	KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
806 
807 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...)		       \
808 	KUNIT_BINARY_INT_ASSERTION(test,				       \
809 				   KUNIT_EXPECTATION,			       \
810 				   left, <, right,			       \
811 				   fmt,					       \
812 				    ##__VA_ARGS__)
813 
814 /**
815  * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
816  * @test: The test context object.
817  * @left: an arbitrary expression that evaluates to a primitive C type.
818  * @right: an arbitrary expression that evaluates to a primitive C type.
819  *
820  * Sets an expectation that the value that @left evaluates to is less than or
821  * equal to the value that @right evaluates to. Semantically this is equivalent
822  * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
823  * more information.
824  */
825 #define KUNIT_EXPECT_LE(test, left, right) \
826 	KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
827 
828 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...)		       \
829 	KUNIT_BINARY_INT_ASSERTION(test,				       \
830 				   KUNIT_EXPECTATION,			       \
831 				   left, <=, right,			       \
832 				   fmt,					       \
833 				    ##__VA_ARGS__)
834 
835 /**
836  * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
837  * @test: The test context object.
838  * @left: an arbitrary expression that evaluates to a primitive C type.
839  * @right: an arbitrary expression that evaluates to a primitive C type.
840  *
841  * Sets an expectation that the value that @left evaluates to is greater than
842  * the value that @right evaluates to. This is semantically equivalent to
843  * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
844  * more information.
845  */
846 #define KUNIT_EXPECT_GT(test, left, right) \
847 	KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
848 
849 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...)		       \
850 	KUNIT_BINARY_INT_ASSERTION(test,				       \
851 				   KUNIT_EXPECTATION,			       \
852 				   left, >, right,			       \
853 				   fmt,					       \
854 				    ##__VA_ARGS__)
855 
856 /**
857  * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
858  * @test: The test context object.
859  * @left: an arbitrary expression that evaluates to a primitive C type.
860  * @right: an arbitrary expression that evaluates to a primitive C type.
861  *
862  * Sets an expectation that the value that @left evaluates to is greater than
863  * the value that @right evaluates to. This is semantically equivalent to
864  * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
865  * more information.
866  */
867 #define KUNIT_EXPECT_GE(test, left, right) \
868 	KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
869 
870 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...)		       \
871 	KUNIT_BINARY_INT_ASSERTION(test,				       \
872 				   KUNIT_EXPECTATION,			       \
873 				   left, >=, right,			       \
874 				   fmt,					       \
875 				    ##__VA_ARGS__)
876 
877 /**
878  * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
879  * @test: The test context object.
880  * @left: an arbitrary expression that evaluates to a null terminated string.
881  * @right: an arbitrary expression that evaluates to a null terminated string.
882  *
883  * Sets an expectation that the values that @left and @right evaluate to are
884  * equal. This is semantically equivalent to
885  * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
886  * for more information.
887  */
888 #define KUNIT_EXPECT_STREQ(test, left, right) \
889 	KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
890 
891 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...)		       \
892 	KUNIT_BINARY_STR_ASSERTION(test,				       \
893 				   KUNIT_EXPECTATION,			       \
894 				   left, ==, right,			       \
895 				   fmt,					       \
896 				   ##__VA_ARGS__)
897 
898 /**
899  * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
900  * @test: The test context object.
901  * @left: an arbitrary expression that evaluates to a null terminated string.
902  * @right: an arbitrary expression that evaluates to a null terminated string.
903  *
904  * Sets an expectation that the values that @left and @right evaluate to are
905  * not equal. This is semantically equivalent to
906  * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
907  * for more information.
908  */
909 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
910 	KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
911 
912 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
913 	KUNIT_BINARY_STR_ASSERTION(test,				       \
914 				   KUNIT_EXPECTATION,			       \
915 				   left, !=, right,			       \
916 				   fmt,					       \
917 				   ##__VA_ARGS__)
918 
919 /**
920  * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
921  * @test: The test context object.
922  * @ptr: an arbitrary pointer.
923  *
924  * Sets an expectation that the value that @ptr evaluates to is null. This is
925  * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
926  * See KUNIT_EXPECT_TRUE() for more information.
927  */
928 #define KUNIT_EXPECT_NULL(test, ptr)				               \
929 	KUNIT_EXPECT_NULL_MSG(test,					       \
930 			      ptr,					       \
931 			      NULL)
932 
933 #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...)	                       \
934 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
935 				   KUNIT_EXPECTATION,			       \
936 				   ptr, ==, NULL,			       \
937 				   fmt,					       \
938 				   ##__VA_ARGS__)
939 
940 /**
941  * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
942  * @test: The test context object.
943  * @ptr: an arbitrary pointer.
944  *
945  * Sets an expectation that the value that @ptr evaluates to is not null. This
946  * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
947  * See KUNIT_EXPECT_TRUE() for more information.
948  */
949 #define KUNIT_EXPECT_NOT_NULL(test, ptr)			               \
950 	KUNIT_EXPECT_NOT_NULL_MSG(test,					       \
951 				  ptr,					       \
952 				  NULL)
953 
954 #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...)	                       \
955 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
956 				   KUNIT_EXPECTATION,			       \
957 				   ptr, !=, NULL,			       \
958 				   fmt,					       \
959 				   ##__VA_ARGS__)
960 
961 /**
962  * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
963  * @test: The test context object.
964  * @ptr: an arbitrary pointer.
965  *
966  * Sets an expectation that the value that @ptr evaluates to is not null and not
967  * an errno stored in a pointer. This is semantically equivalent to
968  * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
969  * more information.
970  */
971 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
972 	KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
973 
974 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
975 	KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
976 						KUNIT_EXPECTATION,	       \
977 						ptr,			       \
978 						fmt,			       \
979 						##__VA_ARGS__)
980 
981 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
982 	KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
983 
984 /**
985  * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
986  * @test: The test context object.
987  * @condition: an arbitrary boolean expression. The test fails and aborts when
988  * this does not evaluate to true.
989  *
990  * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
991  * fail *and immediately abort* when the specified condition is not met. Unlike
992  * an expectation failure, it will prevent the test case from continuing to run;
993  * this is otherwise known as an *assertion failure*.
994  */
995 #define KUNIT_ASSERT_TRUE(test, condition) \
996 	KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
997 
998 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...)		       \
999 	KUNIT_TRUE_MSG_ASSERTION(test,					       \
1000 				 KUNIT_ASSERTION,			       \
1001 				 condition,				       \
1002 				 fmt,					       \
1003 				 ##__VA_ARGS__)
1004 
1005 /**
1006  * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1007  * @test: The test context object.
1008  * @condition: an arbitrary boolean expression.
1009  *
1010  * Sets an assertion that the value that @condition evaluates to is false. This
1011  * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1012  * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1013  */
1014 #define KUNIT_ASSERT_FALSE(test, condition) \
1015 	KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
1016 
1017 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...)		       \
1018 	KUNIT_FALSE_MSG_ASSERTION(test,					       \
1019 				  KUNIT_ASSERTION,			       \
1020 				  condition,				       \
1021 				  fmt,					       \
1022 				  ##__VA_ARGS__)
1023 
1024 /**
1025  * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1026  * @test: The test context object.
1027  * @left: an arbitrary expression that evaluates to a primitive C type.
1028  * @right: an arbitrary expression that evaluates to a primitive C type.
1029  *
1030  * Sets an assertion that the values that @left and @right evaluate to are
1031  * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1032  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1033  */
1034 #define KUNIT_ASSERT_EQ(test, left, right) \
1035 	KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
1036 
1037 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...)		       \
1038 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1039 				   KUNIT_ASSERTION,			       \
1040 				   left, ==, right,			       \
1041 				   fmt,					       \
1042 				    ##__VA_ARGS__)
1043 
1044 /**
1045  * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1046  * @test: The test context object.
1047  * @left: an arbitrary expression that evaluates to a pointer.
1048  * @right: an arbitrary expression that evaluates to a pointer.
1049  *
1050  * Sets an assertion that the values that @left and @right evaluate to are
1051  * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1052  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1053  */
1054 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1055 	KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
1056 
1057 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
1058 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1059 				   KUNIT_ASSERTION,			       \
1060 				   left, ==, right,			       \
1061 				   fmt,					       \
1062 				   ##__VA_ARGS__)
1063 
1064 /**
1065  * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1066  * @test: The test context object.
1067  * @left: an arbitrary expression that evaluates to a primitive C type.
1068  * @right: an arbitrary expression that evaluates to a primitive C type.
1069  *
1070  * Sets an assertion that the values that @left and @right evaluate to are not
1071  * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1072  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1073  */
1074 #define KUNIT_ASSERT_NE(test, left, right) \
1075 	KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
1076 
1077 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...)		       \
1078 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1079 				   KUNIT_ASSERTION,			       \
1080 				   left, !=, right,			       \
1081 				   fmt,					       \
1082 				    ##__VA_ARGS__)
1083 
1084 /**
1085  * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1086  * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1087  * @test: The test context object.
1088  * @left: an arbitrary expression that evaluates to a pointer.
1089  * @right: an arbitrary expression that evaluates to a pointer.
1090  *
1091  * Sets an assertion that the values that @left and @right evaluate to are not
1092  * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1093  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1094  */
1095 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1096 	KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1097 
1098 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
1099 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1100 				   KUNIT_ASSERTION,			       \
1101 				   left, !=, right,			       \
1102 				   fmt,					       \
1103 				   ##__VA_ARGS__)
1104 /**
1105  * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1106  * @test: The test context object.
1107  * @left: an arbitrary expression that evaluates to a primitive C type.
1108  * @right: an arbitrary expression that evaluates to a primitive C type.
1109  *
1110  * Sets an assertion that the value that @left evaluates to is less than the
1111  * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1112  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1113  * is not met.
1114  */
1115 #define KUNIT_ASSERT_LT(test, left, right) \
1116 	KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1117 
1118 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...)		       \
1119 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1120 				   KUNIT_ASSERTION,			       \
1121 				   left, <, right,			       \
1122 				   fmt,					       \
1123 				    ##__VA_ARGS__)
1124 /**
1125  * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1126  * @test: The test context object.
1127  * @left: an arbitrary expression that evaluates to a primitive C type.
1128  * @right: an arbitrary expression that evaluates to a primitive C type.
1129  *
1130  * Sets an assertion that the value that @left evaluates to is less than or
1131  * equal to the value that @right evaluates to. This is the same as
1132  * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1133  * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1134  */
1135 #define KUNIT_ASSERT_LE(test, left, right) \
1136 	KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1137 
1138 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...)		       \
1139 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1140 				   KUNIT_ASSERTION,			       \
1141 				   left, <=, right,			       \
1142 				   fmt,					       \
1143 				    ##__VA_ARGS__)
1144 
1145 /**
1146  * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1147  * @test: The test context object.
1148  * @left: an arbitrary expression that evaluates to a primitive C type.
1149  * @right: an arbitrary expression that evaluates to a primitive C type.
1150  *
1151  * Sets an assertion that the value that @left evaluates to is greater than the
1152  * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1153  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1154  * is not met.
1155  */
1156 #define KUNIT_ASSERT_GT(test, left, right) \
1157 	KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1158 
1159 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...)		       \
1160 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1161 				   KUNIT_ASSERTION,			       \
1162 				   left, >, right,			       \
1163 				   fmt,					       \
1164 				    ##__VA_ARGS__)
1165 
1166 /**
1167  * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1168  * @test: The test context object.
1169  * @left: an arbitrary expression that evaluates to a primitive C type.
1170  * @right: an arbitrary expression that evaluates to a primitive C type.
1171  *
1172  * Sets an assertion that the value that @left evaluates to is greater than the
1173  * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1174  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1175  * is not met.
1176  */
1177 #define KUNIT_ASSERT_GE(test, left, right) \
1178 	KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1179 
1180 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...)		       \
1181 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1182 				   KUNIT_ASSERTION,			       \
1183 				   left, >=, right,			       \
1184 				   fmt,					       \
1185 				    ##__VA_ARGS__)
1186 
1187 /**
1188  * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1189  * @test: The test context object.
1190  * @left: an arbitrary expression that evaluates to a null terminated string.
1191  * @right: an arbitrary expression that evaluates to a null terminated string.
1192  *
1193  * Sets an assertion that the values that @left and @right evaluate to are
1194  * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1195  * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1196  */
1197 #define KUNIT_ASSERT_STREQ(test, left, right) \
1198 	KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1199 
1200 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...)		       \
1201 	KUNIT_BINARY_STR_ASSERTION(test,				       \
1202 				   KUNIT_ASSERTION,			       \
1203 				   left, ==, right,			       \
1204 				   fmt,					       \
1205 				   ##__VA_ARGS__)
1206 
1207 /**
1208  * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1209  * @test: The test context object.
1210  * @left: an arbitrary expression that evaluates to a null terminated string.
1211  * @right: an arbitrary expression that evaluates to a null terminated string.
1212  *
1213  * Sets an expectation that the values that @left and @right evaluate to are
1214  * not equal. This is semantically equivalent to
1215  * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1216  * for more information.
1217  */
1218 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1219 	KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1220 
1221 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
1222 	KUNIT_BINARY_STR_ASSERTION(test,				       \
1223 				   KUNIT_ASSERTION,			       \
1224 				   left, !=, right,			       \
1225 				   fmt,					       \
1226 				   ##__VA_ARGS__)
1227 
1228 /**
1229  * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1230  * @test: The test context object.
1231  * @ptr: an arbitrary pointer.
1232  *
1233  * Sets an assertion that the values that @ptr evaluates to is null. This is
1234  * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1235  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1236  */
1237 #define KUNIT_ASSERT_NULL(test, ptr) \
1238 	KUNIT_ASSERT_NULL_MSG(test,					       \
1239 			      ptr,					       \
1240 			      NULL)
1241 
1242 #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1243 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1244 				   KUNIT_ASSERTION,			       \
1245 				   ptr, ==, NULL,			       \
1246 				   fmt,					       \
1247 				   ##__VA_ARGS__)
1248 
1249 /**
1250  * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1251  * @test: The test context object.
1252  * @ptr: an arbitrary pointer.
1253  *
1254  * Sets an assertion that the values that @ptr evaluates to is not null. This
1255  * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1256  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1257  */
1258 #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1259 	KUNIT_ASSERT_NOT_NULL_MSG(test,					       \
1260 				  ptr,					       \
1261 				  NULL)
1262 
1263 #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1264 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1265 				   KUNIT_ASSERTION,			       \
1266 				   ptr, !=, NULL,			       \
1267 				   fmt,					       \
1268 				   ##__VA_ARGS__)
1269 
1270 /**
1271  * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1272  * @test: The test context object.
1273  * @ptr: an arbitrary pointer.
1274  *
1275  * Sets an assertion that the value that @ptr evaluates to is not null and not
1276  * an errno stored in a pointer. This is the same as
1277  * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1278  * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1279  */
1280 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1281 	KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1282 
1283 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
1284 	KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
1285 						KUNIT_ASSERTION,	       \
1286 						ptr,			       \
1287 						fmt,			       \
1288 						##__VA_ARGS__)
1289 
1290 /**
1291  * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1292  * @name:  prefix for the test parameter generator function.
1293  * @array: array of test parameters.
1294  * @get_desc: function to convert param to description; NULL to use default
1295  *
1296  * Define function @name_gen_params which uses @array to generate parameters.
1297  */
1298 #define KUNIT_ARRAY_PARAM(name, array, get_desc)						\
1299 	static const void *name##_gen_params(const void *prev, char *desc)			\
1300 	{											\
1301 		typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array);	\
1302 		if (__next - (array) < ARRAY_SIZE((array))) {					\
1303 			void (*__get_desc)(typeof(__next), char *) = get_desc;			\
1304 			if (__get_desc)								\
1305 				__get_desc(__next, desc);					\
1306 			return __next;								\
1307 		}										\
1308 		return NULL;									\
1309 	}
1310 
1311 // TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1312 // include resource.h themselves if they need it.
1313 #include <kunit/resource.h>
1314 
1315 #endif /* _KUNIT_TEST_H */
1316