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