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