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