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