xref: /linux/include/kunit/test.h (revision 4359a011e259a4608afc7fb3635370c9d4ba5943)
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 void kunit_init_test(struct kunit *test, const char *name, char *log);
232 
233 int kunit_run_tests(struct kunit_suite *suite);
234 
235 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
236 
237 unsigned int kunit_test_case_num(struct kunit_suite *suite,
238 				 struct kunit_case *test_case);
239 
240 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
241 
242 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
243 
244 #if IS_BUILTIN(CONFIG_KUNIT)
245 int kunit_run_all_tests(void);
246 #else
247 static inline int kunit_run_all_tests(void)
248 {
249 	return 0;
250 }
251 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
252 
253 #define __kunit_test_suites(unique_array, ...)				       \
254 	MODULE_INFO(test, "Y");						       \
255 	static struct kunit_suite *unique_array[]			       \
256 	__aligned(sizeof(struct kunit_suite *))				       \
257 	__used __section(".kunit_test_suites") = { __VA_ARGS__ }
258 
259 /**
260  * kunit_test_suites() - used to register one or more &struct kunit_suite
261  *			 with KUnit.
262  *
263  * @__suites: a statically allocated list of &struct kunit_suite.
264  *
265  * Registers @suites with the test framework.
266  * This is done by placing the array of struct kunit_suite * in the
267  * .kunit_test_suites ELF section.
268  *
269  * When builtin, KUnit tests are all run via the executor at boot, and when
270  * built as a module, they run on module load.
271  *
272  */
273 #define kunit_test_suites(__suites...)						\
274 	__kunit_test_suites(__UNIQUE_ID(array),				\
275 			    ##__suites)
276 
277 #define kunit_test_suite(suite)	kunit_test_suites(&suite)
278 
279 /**
280  * kunit_test_init_section_suites() - used to register one or more &struct
281  *				      kunit_suite containing init functions or
282  *				      init data.
283  *
284  * @__suites: a statically allocated list of &struct kunit_suite.
285  *
286  * This functions identically as kunit_test_suites() except that it suppresses
287  * modpost warnings for referencing functions marked __init or data marked
288  * __initdata; this is OK because currently KUnit only runs tests upon boot
289  * during the init phase or upon loading a module during the init phase.
290  *
291  * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
292  * tests must be excluded.
293  *
294  * The only thing this macro does that's different from kunit_test_suites is
295  * that it suffixes the array and suite declarations it makes with _probe;
296  * modpost suppresses warnings about referencing init data for symbols named in
297  * this manner.
298  */
299 #define kunit_test_init_section_suites(__suites...)			\
300 	__kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe),	\
301 			    CONCATENATE(__UNIQUE_ID(suites), _probe),	\
302 			    ##__suites)
303 
304 #define kunit_test_init_section_suite(suite)	\
305 	kunit_test_init_section_suites(&suite)
306 
307 #define kunit_suite_for_each_test_case(suite, test_case)		\
308 	for (test_case = suite->test_cases; test_case->run_case; test_case++)
309 
310 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
311 
312 /**
313  * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
314  * @test: The test context object.
315  * @n: number of elements.
316  * @size: The size in bytes of the desired memory.
317  * @gfp: flags passed to underlying kmalloc().
318  *
319  * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
320  * and is automatically cleaned up after the test case concludes. See &struct
321  * kunit_resource for more information.
322  */
323 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
324 
325 /**
326  * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
327  * @test: The test context object.
328  * @size: The size in bytes of the desired memory.
329  * @gfp: flags passed to underlying kmalloc().
330  *
331  * See kmalloc() and kunit_kmalloc_array() for more information.
332  */
333 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
334 {
335 	return kunit_kmalloc_array(test, 1, size, gfp);
336 }
337 
338 /**
339  * kunit_kfree() - Like kfree except for allocations managed by KUnit.
340  * @test: The test case to which the resource belongs.
341  * @ptr: The memory allocation to free.
342  */
343 void kunit_kfree(struct kunit *test, const void *ptr);
344 
345 /**
346  * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
347  * @test: The test context object.
348  * @size: The size in bytes of the desired memory.
349  * @gfp: flags passed to underlying kmalloc().
350  *
351  * See kzalloc() and kunit_kmalloc_array() for more information.
352  */
353 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
354 {
355 	return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
356 }
357 
358 /**
359  * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
360  * @test: The test context object.
361  * @n: number of elements.
362  * @size: The size in bytes of the desired memory.
363  * @gfp: flags passed to underlying kmalloc().
364  *
365  * See kcalloc() and kunit_kmalloc_array() for more information.
366  */
367 static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
368 {
369 	return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
370 }
371 
372 void kunit_cleanup(struct kunit *test);
373 
374 void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
375 
376 /**
377  * kunit_mark_skipped() - Marks @test_or_suite as skipped
378  *
379  * @test_or_suite: The test context object.
380  * @fmt:  A printk() style format string.
381  *
382  * Marks the test as skipped. @fmt is given output as the test status
383  * comment, typically the reason the test was skipped.
384  *
385  * Test execution continues after kunit_mark_skipped() is called.
386  */
387 #define kunit_mark_skipped(test_or_suite, fmt, ...)			\
388 	do {								\
389 		WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED);	\
390 		scnprintf((test_or_suite)->status_comment,		\
391 			  KUNIT_STATUS_COMMENT_SIZE,			\
392 			  fmt, ##__VA_ARGS__);				\
393 	} while (0)
394 
395 /**
396  * kunit_skip() - Marks @test_or_suite as skipped
397  *
398  * @test_or_suite: The test context object.
399  * @fmt:  A printk() style format string.
400  *
401  * Skips the test. @fmt is given output as the test status
402  * comment, typically the reason the test was skipped.
403  *
404  * Test execution is halted after kunit_skip() is called.
405  */
406 #define kunit_skip(test_or_suite, fmt, ...)				\
407 	do {								\
408 		kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
409 		kunit_try_catch_throw(&((test_or_suite)->try_catch));	\
410 	} while (0)
411 
412 /*
413  * printk and log to per-test or per-suite log buffer.  Logging only done
414  * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
415  */
416 #define kunit_log(lvl, test_or_suite, fmt, ...)				\
417 	do {								\
418 		printk(lvl fmt, ##__VA_ARGS__);				\
419 		kunit_log_append((test_or_suite)->log,	fmt "\n",	\
420 				 ##__VA_ARGS__);			\
421 	} while (0)
422 
423 #define kunit_printk(lvl, test, fmt, ...)				\
424 	kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt,		\
425 		  (test)->name,	##__VA_ARGS__)
426 
427 /**
428  * kunit_info() - Prints an INFO level message associated with @test.
429  *
430  * @test: The test context object.
431  * @fmt:  A printk() style format string.
432  *
433  * Prints an info level message associated with the test suite being run.
434  * Takes a variable number of format parameters just like printk().
435  */
436 #define kunit_info(test, fmt, ...) \
437 	kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
438 
439 /**
440  * kunit_warn() - Prints a WARN level message associated with @test.
441  *
442  * @test: The test context object.
443  * @fmt:  A printk() style format string.
444  *
445  * Prints a warning level message.
446  */
447 #define kunit_warn(test, fmt, ...) \
448 	kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
449 
450 /**
451  * kunit_err() - Prints an ERROR level message associated with @test.
452  *
453  * @test: The test context object.
454  * @fmt:  A printk() style format string.
455  *
456  * Prints an error level message.
457  */
458 #define kunit_err(test, fmt, ...) \
459 	kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
460 
461 /**
462  * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
463  * @test: The test context object.
464  *
465  * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
466  * words, it does nothing and only exists for code clarity. See
467  * KUNIT_EXPECT_TRUE() for more information.
468  */
469 #define KUNIT_SUCCEED(test) do {} while (0)
470 
471 void kunit_do_failed_assertion(struct kunit *test,
472 			       const struct kunit_loc *loc,
473 			       enum kunit_assert_type type,
474 			       const struct kunit_assert *assert,
475 			       const char *fmt, ...);
476 
477 #define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
478 	if (unlikely(!(pass))) {					       \
479 		static const struct kunit_loc __loc = KUNIT_CURRENT_LOC;       \
480 		struct assert_class __assertion = INITIALIZER;		       \
481 		kunit_do_failed_assertion(test,				       \
482 					  &__loc,			       \
483 					  assert_type,			       \
484 					  &__assertion.assert,		       \
485 					  fmt,				       \
486 					  ##__VA_ARGS__);		       \
487 	}								       \
488 } while (0)
489 
490 
491 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...)		       \
492 	KUNIT_ASSERTION(test,						       \
493 			assert_type,					       \
494 			false,						       \
495 			kunit_fail_assert,				       \
496 			KUNIT_INIT_FAIL_ASSERT_STRUCT,			       \
497 			fmt,						       \
498 			##__VA_ARGS__)
499 
500 /**
501  * KUNIT_FAIL() - Always causes a test to fail when evaluated.
502  * @test: The test context object.
503  * @fmt: an informational message to be printed when the assertion is made.
504  * @...: string format arguments.
505  *
506  * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
507  * other words, it always results in a failed expectation, and consequently
508  * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
509  * for more information.
510  */
511 #define KUNIT_FAIL(test, fmt, ...)					       \
512 	KUNIT_FAIL_ASSERTION(test,					       \
513 			     KUNIT_EXPECTATION,				       \
514 			     fmt,					       \
515 			     ##__VA_ARGS__)
516 
517 #define KUNIT_UNARY_ASSERTION(test,					       \
518 			      assert_type,				       \
519 			      condition,				       \
520 			      expected_true,				       \
521 			      fmt,					       \
522 			      ...)					       \
523 	KUNIT_ASSERTION(test,						       \
524 			assert_type,					       \
525 			!!(condition) == !!expected_true,		       \
526 			kunit_unary_assert,				       \
527 			KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition,	       \
528 						       expected_true),	       \
529 			fmt,						       \
530 			##__VA_ARGS__)
531 
532 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)       \
533 	KUNIT_UNARY_ASSERTION(test,					       \
534 			      assert_type,				       \
535 			      condition,				       \
536 			      true,					       \
537 			      fmt,					       \
538 			      ##__VA_ARGS__)
539 
540 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)      \
541 	KUNIT_UNARY_ASSERTION(test,					       \
542 			      assert_type,				       \
543 			      condition,				       \
544 			      false,					       \
545 			      fmt,					       \
546 			      ##__VA_ARGS__)
547 
548 /*
549  * A factory macro for defining the assertions and expectations for the basic
550  * comparisons defined for the built in types.
551  *
552  * Unfortunately, there is no common type that all types can be promoted to for
553  * which all the binary operators behave the same way as for the actual types
554  * (for example, there is no type that long long and unsigned long long can
555  * both be cast to where the comparison result is preserved for all values). So
556  * the best we can do is do the comparison in the original types and then coerce
557  * everything to long long for printing; this way, the comparison behaves
558  * correctly and the printed out value usually makes sense without
559  * interpretation, but can always be interpreted to figure out the actual
560  * value.
561  */
562 #define KUNIT_BASE_BINARY_ASSERTION(test,				       \
563 				    assert_class,			       \
564 				    format_func,			       \
565 				    assert_type,			       \
566 				    left,				       \
567 				    op,					       \
568 				    right,				       \
569 				    fmt,				       \
570 				    ...)				       \
571 do {									       \
572 	const typeof(left) __left = (left);				       \
573 	const typeof(right) __right = (right);				       \
574 	static const struct kunit_binary_assert_text __text = {		       \
575 		.operation = #op,					       \
576 		.left_text = #left,					       \
577 		.right_text = #right,					       \
578 	};								       \
579 									       \
580 	KUNIT_ASSERTION(test,						       \
581 			assert_type,					       \
582 			__left op __right,				       \
583 			assert_class,					       \
584 			KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func,	       \
585 							&__text,	       \
586 							__left,		       \
587 							__right),	       \
588 			fmt,						       \
589 			##__VA_ARGS__);					       \
590 } while (0)
591 
592 #define KUNIT_BINARY_INT_ASSERTION(test,				       \
593 				   assert_type,				       \
594 				   left,				       \
595 				   op,					       \
596 				   right,				       \
597 				   fmt,					       \
598 				    ...)				       \
599 	KUNIT_BASE_BINARY_ASSERTION(test,				       \
600 				    kunit_binary_assert,		       \
601 				    kunit_binary_assert_format,		       \
602 				    assert_type,			       \
603 				    left, op, right,			       \
604 				    fmt,				       \
605 				    ##__VA_ARGS__)
606 
607 #define KUNIT_BINARY_PTR_ASSERTION(test,				       \
608 				   assert_type,				       \
609 				   left,				       \
610 				   op,					       \
611 				   right,				       \
612 				   fmt,					       \
613 				    ...)				       \
614 	KUNIT_BASE_BINARY_ASSERTION(test,				       \
615 				    kunit_binary_ptr_assert,		       \
616 				    kunit_binary_ptr_assert_format,	       \
617 				    assert_type,			       \
618 				    left, op, right,			       \
619 				    fmt,				       \
620 				    ##__VA_ARGS__)
621 
622 #define KUNIT_BINARY_STR_ASSERTION(test,				       \
623 				   assert_type,				       \
624 				   left,				       \
625 				   op,					       \
626 				   right,				       \
627 				   fmt,					       \
628 				   ...)					       \
629 do {									       \
630 	const char *__left = (left);					       \
631 	const char *__right = (right);					       \
632 	static const struct kunit_binary_assert_text __text = {		       \
633 		.operation = #op,					       \
634 		.left_text = #left,					       \
635 		.right_text = #right,					       \
636 	};								       \
637 									       \
638 	KUNIT_ASSERTION(test,						       \
639 			assert_type,					       \
640 			strcmp(__left, __right) op 0,			       \
641 			kunit_binary_str_assert,			       \
642 			KUNIT_INIT_BINARY_ASSERT_STRUCT(kunit_binary_str_assert_format,\
643 							&__text,	       \
644 							__left,		       \
645 							__right),	       \
646 			fmt,						       \
647 			##__VA_ARGS__);					       \
648 } while (0)
649 
650 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
651 						assert_type,		       \
652 						ptr,			       \
653 						fmt,			       \
654 						...)			       \
655 do {									       \
656 	const typeof(ptr) __ptr = (ptr);				       \
657 									       \
658 	KUNIT_ASSERTION(test,						       \
659 			assert_type,					       \
660 			!IS_ERR_OR_NULL(__ptr),				       \
661 			kunit_ptr_not_err_assert,			       \
662 			KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr,		       \
663 						      __ptr),		       \
664 			fmt,						       \
665 			##__VA_ARGS__);					       \
666 } while (0)
667 
668 /**
669  * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
670  * @test: The test context object.
671  * @condition: an arbitrary boolean expression. The test fails when this does
672  * not evaluate to true.
673  *
674  * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
675  * to fail when the specified condition is not met; however, it will not prevent
676  * the test case from continuing to run; this is otherwise known as an
677  * *expectation failure*.
678  */
679 #define KUNIT_EXPECT_TRUE(test, condition) \
680 	KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
681 
682 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...)		       \
683 	KUNIT_TRUE_MSG_ASSERTION(test,					       \
684 				 KUNIT_EXPECTATION,			       \
685 				 condition,				       \
686 				 fmt,					       \
687 				 ##__VA_ARGS__)
688 
689 /**
690  * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
691  * @test: The test context object.
692  * @condition: an arbitrary boolean expression. The test fails when this does
693  * not evaluate to false.
694  *
695  * Sets an expectation that @condition evaluates to false. See
696  * KUNIT_EXPECT_TRUE() for more information.
697  */
698 #define KUNIT_EXPECT_FALSE(test, condition) \
699 	KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
700 
701 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...)		       \
702 	KUNIT_FALSE_MSG_ASSERTION(test,					       \
703 				  KUNIT_EXPECTATION,			       \
704 				  condition,				       \
705 				  fmt,					       \
706 				  ##__VA_ARGS__)
707 
708 /**
709  * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
710  * @test: The test context object.
711  * @left: an arbitrary expression that evaluates to a primitive C type.
712  * @right: an arbitrary expression that evaluates to a primitive C type.
713  *
714  * Sets an expectation that the values that @left and @right evaluate to are
715  * equal. This is semantically equivalent to
716  * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
717  * more information.
718  */
719 #define KUNIT_EXPECT_EQ(test, left, right) \
720 	KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
721 
722 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...)		       \
723 	KUNIT_BINARY_INT_ASSERTION(test,				       \
724 				   KUNIT_EXPECTATION,			       \
725 				   left, ==, right,			       \
726 				   fmt,					       \
727 				    ##__VA_ARGS__)
728 
729 /**
730  * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
731  * @test: The test context object.
732  * @left: an arbitrary expression that evaluates to a pointer.
733  * @right: an arbitrary expression that evaluates to a pointer.
734  *
735  * Sets an expectation that the values that @left and @right evaluate to are
736  * equal. This is semantically equivalent to
737  * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
738  * more information.
739  */
740 #define KUNIT_EXPECT_PTR_EQ(test, left, right)				       \
741 	KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
742 
743 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
744 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
745 				   KUNIT_EXPECTATION,			       \
746 				   left, ==, right,			       \
747 				   fmt,					       \
748 				   ##__VA_ARGS__)
749 
750 /**
751  * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
752  * @test: The test context object.
753  * @left: an arbitrary expression that evaluates to a primitive C type.
754  * @right: an arbitrary expression that evaluates to a primitive C type.
755  *
756  * Sets an expectation that the values that @left and @right evaluate to are not
757  * equal. This is semantically equivalent to
758  * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
759  * more information.
760  */
761 #define KUNIT_EXPECT_NE(test, left, right) \
762 	KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
763 
764 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...)		       \
765 	KUNIT_BINARY_INT_ASSERTION(test,				       \
766 				   KUNIT_EXPECTATION,			       \
767 				   left, !=, right,			       \
768 				   fmt,					       \
769 				    ##__VA_ARGS__)
770 
771 /**
772  * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
773  * @test: The test context object.
774  * @left: an arbitrary expression that evaluates to a pointer.
775  * @right: an arbitrary expression that evaluates to a pointer.
776  *
777  * Sets an expectation that the values that @left and @right evaluate to are not
778  * equal. This is semantically equivalent to
779  * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
780  * more information.
781  */
782 #define KUNIT_EXPECT_PTR_NE(test, left, right)				       \
783 	KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
784 
785 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
786 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
787 				   KUNIT_EXPECTATION,			       \
788 				   left, !=, right,			       \
789 				   fmt,					       \
790 				   ##__VA_ARGS__)
791 
792 /**
793  * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
794  * @test: The test context object.
795  * @left: an arbitrary expression that evaluates to a primitive C type.
796  * @right: an arbitrary expression that evaluates to a primitive C type.
797  *
798  * Sets an expectation that the value that @left evaluates to is less than the
799  * value that @right evaluates to. This is semantically equivalent to
800  * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
801  * more information.
802  */
803 #define KUNIT_EXPECT_LT(test, left, right) \
804 	KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
805 
806 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...)		       \
807 	KUNIT_BINARY_INT_ASSERTION(test,				       \
808 				   KUNIT_EXPECTATION,			       \
809 				   left, <, right,			       \
810 				   fmt,					       \
811 				    ##__VA_ARGS__)
812 
813 /**
814  * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
815  * @test: The test context object.
816  * @left: an arbitrary expression that evaluates to a primitive C type.
817  * @right: an arbitrary expression that evaluates to a primitive C type.
818  *
819  * Sets an expectation that the value that @left evaluates to is less than or
820  * equal to the value that @right evaluates to. Semantically this is equivalent
821  * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
822  * more information.
823  */
824 #define KUNIT_EXPECT_LE(test, left, right) \
825 	KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
826 
827 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...)		       \
828 	KUNIT_BINARY_INT_ASSERTION(test,				       \
829 				   KUNIT_EXPECTATION,			       \
830 				   left, <=, right,			       \
831 				   fmt,					       \
832 				    ##__VA_ARGS__)
833 
834 /**
835  * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
836  * @test: The test context object.
837  * @left: an arbitrary expression that evaluates to a primitive C type.
838  * @right: an arbitrary expression that evaluates to a primitive C type.
839  *
840  * Sets an expectation that the value that @left evaluates to is greater than
841  * the value that @right evaluates to. This is semantically equivalent to
842  * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
843  * more information.
844  */
845 #define KUNIT_EXPECT_GT(test, left, right) \
846 	KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
847 
848 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...)		       \
849 	KUNIT_BINARY_INT_ASSERTION(test,				       \
850 				   KUNIT_EXPECTATION,			       \
851 				   left, >, right,			       \
852 				   fmt,					       \
853 				    ##__VA_ARGS__)
854 
855 /**
856  * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
857  * @test: The test context object.
858  * @left: an arbitrary expression that evaluates to a primitive C type.
859  * @right: an arbitrary expression that evaluates to a primitive C type.
860  *
861  * Sets an expectation that the value that @left evaluates to is greater than
862  * the value that @right evaluates to. This is semantically equivalent to
863  * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
864  * more information.
865  */
866 #define KUNIT_EXPECT_GE(test, left, right) \
867 	KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
868 
869 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...)		       \
870 	KUNIT_BINARY_INT_ASSERTION(test,				       \
871 				   KUNIT_EXPECTATION,			       \
872 				   left, >=, right,			       \
873 				   fmt,					       \
874 				    ##__VA_ARGS__)
875 
876 /**
877  * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
878  * @test: The test context object.
879  * @left: an arbitrary expression that evaluates to a null terminated string.
880  * @right: an arbitrary expression that evaluates to a null terminated string.
881  *
882  * Sets an expectation that the values that @left and @right evaluate to are
883  * equal. This is semantically equivalent to
884  * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
885  * for more information.
886  */
887 #define KUNIT_EXPECT_STREQ(test, left, right) \
888 	KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
889 
890 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...)		       \
891 	KUNIT_BINARY_STR_ASSERTION(test,				       \
892 				   KUNIT_EXPECTATION,			       \
893 				   left, ==, right,			       \
894 				   fmt,					       \
895 				   ##__VA_ARGS__)
896 
897 /**
898  * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
899  * @test: The test context object.
900  * @left: an arbitrary expression that evaluates to a null terminated string.
901  * @right: an arbitrary expression that evaluates to a null terminated string.
902  *
903  * Sets an expectation that the values that @left and @right evaluate to are
904  * not equal. This is semantically equivalent to
905  * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
906  * for more information.
907  */
908 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
909 	KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
910 
911 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
912 	KUNIT_BINARY_STR_ASSERTION(test,				       \
913 				   KUNIT_EXPECTATION,			       \
914 				   left, !=, right,			       \
915 				   fmt,					       \
916 				   ##__VA_ARGS__)
917 
918 /**
919  * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
920  * @test: The test context object.
921  * @ptr: an arbitrary pointer.
922  *
923  * Sets an expectation that the value that @ptr evaluates to is null. This is
924  * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
925  * See KUNIT_EXPECT_TRUE() for more information.
926  */
927 #define KUNIT_EXPECT_NULL(test, ptr)				               \
928 	KUNIT_EXPECT_NULL_MSG(test,					       \
929 			      ptr,					       \
930 			      NULL)
931 
932 #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...)	                       \
933 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
934 				   KUNIT_EXPECTATION,			       \
935 				   ptr, ==, NULL,			       \
936 				   fmt,					       \
937 				   ##__VA_ARGS__)
938 
939 /**
940  * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
941  * @test: The test context object.
942  * @ptr: an arbitrary pointer.
943  *
944  * Sets an expectation that the value that @ptr evaluates to is not null. This
945  * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
946  * See KUNIT_EXPECT_TRUE() for more information.
947  */
948 #define KUNIT_EXPECT_NOT_NULL(test, ptr)			               \
949 	KUNIT_EXPECT_NOT_NULL_MSG(test,					       \
950 				  ptr,					       \
951 				  NULL)
952 
953 #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...)	                       \
954 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
955 				   KUNIT_EXPECTATION,			       \
956 				   ptr, !=, NULL,			       \
957 				   fmt,					       \
958 				   ##__VA_ARGS__)
959 
960 /**
961  * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
962  * @test: The test context object.
963  * @ptr: an arbitrary pointer.
964  *
965  * Sets an expectation that the value that @ptr evaluates to is not null and not
966  * an errno stored in a pointer. This is semantically equivalent to
967  * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
968  * more information.
969  */
970 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
971 	KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
972 
973 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
974 	KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
975 						KUNIT_EXPECTATION,	       \
976 						ptr,			       \
977 						fmt,			       \
978 						##__VA_ARGS__)
979 
980 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
981 	KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
982 
983 /**
984  * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
985  * @test: The test context object.
986  * @condition: an arbitrary boolean expression. The test fails and aborts when
987  * this does not evaluate to true.
988  *
989  * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
990  * fail *and immediately abort* when the specified condition is not met. Unlike
991  * an expectation failure, it will prevent the test case from continuing to run;
992  * this is otherwise known as an *assertion failure*.
993  */
994 #define KUNIT_ASSERT_TRUE(test, condition) \
995 	KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
996 
997 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...)		       \
998 	KUNIT_TRUE_MSG_ASSERTION(test,					       \
999 				 KUNIT_ASSERTION,			       \
1000 				 condition,				       \
1001 				 fmt,					       \
1002 				 ##__VA_ARGS__)
1003 
1004 /**
1005  * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1006  * @test: The test context object.
1007  * @condition: an arbitrary boolean expression.
1008  *
1009  * Sets an assertion that the value that @condition evaluates to is false. This
1010  * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1011  * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1012  */
1013 #define KUNIT_ASSERT_FALSE(test, condition) \
1014 	KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
1015 
1016 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...)		       \
1017 	KUNIT_FALSE_MSG_ASSERTION(test,					       \
1018 				  KUNIT_ASSERTION,			       \
1019 				  condition,				       \
1020 				  fmt,					       \
1021 				  ##__VA_ARGS__)
1022 
1023 /**
1024  * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1025  * @test: The test context object.
1026  * @left: an arbitrary expression that evaluates to a primitive C type.
1027  * @right: an arbitrary expression that evaluates to a primitive C type.
1028  *
1029  * Sets an assertion that the values that @left and @right evaluate to are
1030  * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1031  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1032  */
1033 #define KUNIT_ASSERT_EQ(test, left, right) \
1034 	KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
1035 
1036 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...)		       \
1037 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1038 				   KUNIT_ASSERTION,			       \
1039 				   left, ==, right,			       \
1040 				   fmt,					       \
1041 				    ##__VA_ARGS__)
1042 
1043 /**
1044  * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1045  * @test: The test context object.
1046  * @left: an arbitrary expression that evaluates to a pointer.
1047  * @right: an arbitrary expression that evaluates to a pointer.
1048  *
1049  * Sets an assertion that the values that @left and @right evaluate to are
1050  * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1051  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1052  */
1053 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1054 	KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
1055 
1056 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
1057 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1058 				   KUNIT_ASSERTION,			       \
1059 				   left, ==, right,			       \
1060 				   fmt,					       \
1061 				   ##__VA_ARGS__)
1062 
1063 /**
1064  * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1065  * @test: The test context object.
1066  * @left: an arbitrary expression that evaluates to a primitive C type.
1067  * @right: an arbitrary expression that evaluates to a primitive C type.
1068  *
1069  * Sets an assertion that the values that @left and @right evaluate to are not
1070  * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1071  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1072  */
1073 #define KUNIT_ASSERT_NE(test, left, right) \
1074 	KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
1075 
1076 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...)		       \
1077 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1078 				   KUNIT_ASSERTION,			       \
1079 				   left, !=, right,			       \
1080 				   fmt,					       \
1081 				    ##__VA_ARGS__)
1082 
1083 /**
1084  * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1085  * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1086  * @test: The test context object.
1087  * @left: an arbitrary expression that evaluates to a pointer.
1088  * @right: an arbitrary expression that evaluates to a pointer.
1089  *
1090  * Sets an assertion that the values that @left and @right evaluate to are not
1091  * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1092  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1093  */
1094 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1095 	KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1096 
1097 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
1098 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1099 				   KUNIT_ASSERTION,			       \
1100 				   left, !=, right,			       \
1101 				   fmt,					       \
1102 				   ##__VA_ARGS__)
1103 /**
1104  * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1105  * @test: The test context object.
1106  * @left: an arbitrary expression that evaluates to a primitive C type.
1107  * @right: an arbitrary expression that evaluates to a primitive C type.
1108  *
1109  * Sets an assertion that the value that @left evaluates to is less than the
1110  * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1111  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1112  * is not met.
1113  */
1114 #define KUNIT_ASSERT_LT(test, left, right) \
1115 	KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1116 
1117 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...)		       \
1118 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1119 				   KUNIT_ASSERTION,			       \
1120 				   left, <, right,			       \
1121 				   fmt,					       \
1122 				    ##__VA_ARGS__)
1123 /**
1124  * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1125  * @test: The test context object.
1126  * @left: an arbitrary expression that evaluates to a primitive C type.
1127  * @right: an arbitrary expression that evaluates to a primitive C type.
1128  *
1129  * Sets an assertion that the value that @left evaluates to is less than or
1130  * equal to the value that @right evaluates to. This is the same as
1131  * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1132  * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1133  */
1134 #define KUNIT_ASSERT_LE(test, left, right) \
1135 	KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1136 
1137 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...)		       \
1138 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1139 				   KUNIT_ASSERTION,			       \
1140 				   left, <=, right,			       \
1141 				   fmt,					       \
1142 				    ##__VA_ARGS__)
1143 
1144 /**
1145  * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1146  * @test: The test context object.
1147  * @left: an arbitrary expression that evaluates to a primitive C type.
1148  * @right: an arbitrary expression that evaluates to a primitive C type.
1149  *
1150  * Sets an assertion that the value that @left evaluates to is greater than the
1151  * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1152  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1153  * is not met.
1154  */
1155 #define KUNIT_ASSERT_GT(test, left, right) \
1156 	KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1157 
1158 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...)		       \
1159 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1160 				   KUNIT_ASSERTION,			       \
1161 				   left, >, right,			       \
1162 				   fmt,					       \
1163 				    ##__VA_ARGS__)
1164 
1165 /**
1166  * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1167  * @test: The test context object.
1168  * @left: an arbitrary expression that evaluates to a primitive C type.
1169  * @right: an arbitrary expression that evaluates to a primitive C type.
1170  *
1171  * Sets an assertion that the value that @left evaluates to is greater than the
1172  * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1173  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1174  * is not met.
1175  */
1176 #define KUNIT_ASSERT_GE(test, left, right) \
1177 	KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1178 
1179 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...)		       \
1180 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1181 				   KUNIT_ASSERTION,			       \
1182 				   left, >=, right,			       \
1183 				   fmt,					       \
1184 				    ##__VA_ARGS__)
1185 
1186 /**
1187  * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1188  * @test: The test context object.
1189  * @left: an arbitrary expression that evaluates to a null terminated string.
1190  * @right: an arbitrary expression that evaluates to a null terminated string.
1191  *
1192  * Sets an assertion that the values that @left and @right evaluate to are
1193  * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1194  * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1195  */
1196 #define KUNIT_ASSERT_STREQ(test, left, right) \
1197 	KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1198 
1199 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...)		       \
1200 	KUNIT_BINARY_STR_ASSERTION(test,				       \
1201 				   KUNIT_ASSERTION,			       \
1202 				   left, ==, right,			       \
1203 				   fmt,					       \
1204 				   ##__VA_ARGS__)
1205 
1206 /**
1207  * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1208  * @test: The test context object.
1209  * @left: an arbitrary expression that evaluates to a null terminated string.
1210  * @right: an arbitrary expression that evaluates to a null terminated string.
1211  *
1212  * Sets an expectation that the values that @left and @right evaluate to are
1213  * not equal. This is semantically equivalent to
1214  * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1215  * for more information.
1216  */
1217 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1218 	KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1219 
1220 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
1221 	KUNIT_BINARY_STR_ASSERTION(test,				       \
1222 				   KUNIT_ASSERTION,			       \
1223 				   left, !=, right,			       \
1224 				   fmt,					       \
1225 				   ##__VA_ARGS__)
1226 
1227 /**
1228  * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1229  * @test: The test context object.
1230  * @ptr: an arbitrary pointer.
1231  *
1232  * Sets an assertion that the values that @ptr evaluates to is null. This is
1233  * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1234  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1235  */
1236 #define KUNIT_ASSERT_NULL(test, ptr) \
1237 	KUNIT_ASSERT_NULL_MSG(test,					       \
1238 			      ptr,					       \
1239 			      NULL)
1240 
1241 #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1242 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1243 				   KUNIT_ASSERTION,			       \
1244 				   ptr, ==, NULL,			       \
1245 				   fmt,					       \
1246 				   ##__VA_ARGS__)
1247 
1248 /**
1249  * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1250  * @test: The test context object.
1251  * @ptr: an arbitrary pointer.
1252  *
1253  * Sets an assertion that the values that @ptr evaluates to is not null. This
1254  * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1255  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1256  */
1257 #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1258 	KUNIT_ASSERT_NOT_NULL_MSG(test,					       \
1259 				  ptr,					       \
1260 				  NULL)
1261 
1262 #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1263 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1264 				   KUNIT_ASSERTION,			       \
1265 				   ptr, !=, NULL,			       \
1266 				   fmt,					       \
1267 				   ##__VA_ARGS__)
1268 
1269 /**
1270  * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1271  * @test: The test context object.
1272  * @ptr: an arbitrary pointer.
1273  *
1274  * Sets an assertion that the value that @ptr evaluates to is not null and not
1275  * an errno stored in a pointer. This is the same as
1276  * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1277  * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1278  */
1279 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1280 	KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1281 
1282 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
1283 	KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
1284 						KUNIT_ASSERTION,	       \
1285 						ptr,			       \
1286 						fmt,			       \
1287 						##__VA_ARGS__)
1288 
1289 /**
1290  * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1291  * @name:  prefix for the test parameter generator function.
1292  * @array: array of test parameters.
1293  * @get_desc: function to convert param to description; NULL to use default
1294  *
1295  * Define function @name_gen_params which uses @array to generate parameters.
1296  */
1297 #define KUNIT_ARRAY_PARAM(name, array, get_desc)						\
1298 	static const void *name##_gen_params(const void *prev, char *desc)			\
1299 	{											\
1300 		typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array);	\
1301 		if (__next - (array) < ARRAY_SIZE((array))) {					\
1302 			void (*__get_desc)(typeof(__next), char *) = get_desc;			\
1303 			if (__get_desc)								\
1304 				__get_desc(__next, desc);					\
1305 			return __next;								\
1306 		}										\
1307 		return NULL;									\
1308 	}
1309 
1310 // TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1311 // include resource.h themselves if they need it.
1312 #include <kunit/resource.h>
1313 
1314 #endif /* _KUNIT_TEST_H */
1315