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