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