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