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