1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Base unit test (KUnit) API.
4 *
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
7 */
8
9 #ifndef _KUNIT_TEST_H
10 #define _KUNIT_TEST_H
11
12 #include <kunit/assert.h>
13 #include <kunit/try-catch.h>
14
15 #include <linux/args.h>
16 #include <linux/compiler.h>
17 #include <linux/container_of.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/jump_label.h>
21 #include <linux/kconfig.h>
22 #include <linux/kref.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
27 #include <linux/string.h>
28 #include <linux/types.h>
29
30 #include <asm/rwonce.h>
31 #include <asm/sections.h>
32
33 /* Static key: true if any KUnit tests are currently running */
34 DECLARE_STATIC_KEY_FALSE(kunit_running);
35
36 struct kunit;
37 struct string_stream;
38
39 /* Maximum size of parameter description string. */
40 #define KUNIT_PARAM_DESC_SIZE 128
41
42 /* Maximum size of a status comment. */
43 #define KUNIT_STATUS_COMMENT_SIZE 256
44
45 /*
46 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
47 * sub-subtest. See the "Subtests" section in
48 * https://node-tap.org/tap-protocol/
49 */
50 #define KUNIT_INDENT_LEN 4
51 #define KUNIT_SUBTEST_INDENT " "
52 #define KUNIT_SUBSUBTEST_INDENT " "
53
54 /**
55 * enum kunit_status - Type of result for a test or test suite
56 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
57 * @KUNIT_FAILURE: Denotes the test has failed.
58 * @KUNIT_SKIPPED: Denotes the test has been skipped.
59 */
60 enum kunit_status {
61 KUNIT_SUCCESS,
62 KUNIT_FAILURE,
63 KUNIT_SKIPPED,
64 };
65
66 /* Attribute struct/enum definitions */
67
68 /*
69 * Speed Attribute is stored as an enum and separated into categories of
70 * speed: very_slow, slow, and normal. These speeds are relative to
71 * other KUnit tests.
72 *
73 * Note: unset speed attribute acts as default of KUNIT_SPEED_NORMAL.
74 */
75 enum kunit_speed {
76 KUNIT_SPEED_UNSET,
77 KUNIT_SPEED_VERY_SLOW,
78 KUNIT_SPEED_SLOW,
79 KUNIT_SPEED_NORMAL,
80 KUNIT_SPEED_MAX = KUNIT_SPEED_NORMAL,
81 };
82
83 /* Holds attributes for each test case and suite */
84 struct kunit_attributes {
85 enum kunit_speed speed;
86 };
87
88 /**
89 * struct kunit_case - represents an individual test case.
90 *
91 * @run_case: the function representing the actual test case.
92 * @name: the name of the test case.
93 * @generate_params: the generator function for parameterized tests.
94 * @attr: the attributes associated with the test
95 * @param_init: The init function to run before a parameterized test.
96 * @param_exit: The exit function to run after a parameterized test.
97 *
98 * A test case is a function with the signature,
99 * ``void (*)(struct kunit *)``
100 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
101 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
102 * with a &struct kunit_suite and will be run after the suite's init
103 * function and followed by the suite's exit function.
104 *
105 * A test case should be static and should only be created with the
106 * KUNIT_CASE() macro; additionally, every array of test cases should be
107 * terminated with an empty test case.
108 *
109 * Example:
110 *
111 * .. code-block:: c
112 *
113 * void add_test_basic(struct kunit *test)
114 * {
115 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
116 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
117 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
118 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
119 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
120 * }
121 *
122 * static struct kunit_case example_test_cases[] = {
123 * KUNIT_CASE(add_test_basic),
124 * {}
125 * };
126 *
127 */
128 struct kunit_case {
129 void (*run_case)(struct kunit *test);
130 const char *name;
131 const void* (*generate_params)(struct kunit *test,
132 const void *prev, char *desc);
133 struct kunit_attributes attr;
134 int (*param_init)(struct kunit *test);
135 void (*param_exit)(struct kunit *test);
136
137 /* private: internal use only. */
138 enum kunit_status status;
139 char *module_name;
140 struct string_stream *log;
141 };
142
kunit_status_to_ok_not_ok(enum kunit_status status)143 static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
144 {
145 switch (status) {
146 case KUNIT_SKIPPED:
147 case KUNIT_SUCCESS:
148 return "ok";
149 case KUNIT_FAILURE:
150 return "not ok";
151 }
152 return "invalid";
153 }
154
155 /**
156 * KUNIT_CASE - A helper for creating a &struct kunit_case
157 *
158 * @test_name: a reference to a test case function.
159 *
160 * Takes a symbol for a function representing a test case and creates a
161 * &struct kunit_case object from it. See the documentation for
162 * &struct kunit_case for an example on how to use it.
163 */
164 #define KUNIT_CASE(test_name) \
165 { .run_case = test_name, .name = #test_name, \
166 .module_name = KBUILD_MODNAME}
167
168 /**
169 * KUNIT_CASE_ATTR - A helper for creating a &struct kunit_case
170 * with attributes
171 *
172 * @test_name: a reference to a test case function.
173 * @attributes: a reference to a struct kunit_attributes object containing
174 * test attributes
175 */
176 #define KUNIT_CASE_ATTR(test_name, attributes) \
177 { .run_case = test_name, .name = #test_name, \
178 .attr = attributes, .module_name = KBUILD_MODNAME}
179
180 /**
181 * KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case
182 * with the slow attribute
183 *
184 * @test_name: a reference to a test case function.
185 */
186
187 #define KUNIT_CASE_SLOW(test_name) \
188 { .run_case = test_name, .name = #test_name, \
189 .attr.speed = KUNIT_SPEED_SLOW, .module_name = KBUILD_MODNAME}
190
191 /**
192 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
193 *
194 * @test_name: a reference to a test case function.
195 * @gen_params: a reference to a parameter generator function.
196 *
197 * The generator function::
198 *
199 * const void* gen_params(const void *prev, char *desc)
200 *
201 * is used to lazily generate a series of arbitrarily typed values that fit into
202 * a void*. The argument @prev is the previously returned value, which should be
203 * used to derive the next value; @prev is set to NULL on the initial generator
204 * call. When no more values are available, the generator must return NULL.
205 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
206 * describing the parameter.
207 */
208 #define KUNIT_CASE_PARAM(test_name, gen_params) \
209 { .run_case = test_name, .name = #test_name, \
210 .generate_params = gen_params, .module_name = KBUILD_MODNAME}
211
212 /**
213 * KUNIT_CASE_PARAM_ATTR - A helper for creating a parameterized &struct
214 * kunit_case with attributes
215 *
216 * @test_name: a reference to a test case function.
217 * @gen_params: a reference to a parameter generator function.
218 * @attributes: a reference to a struct kunit_attributes object containing
219 * test attributes
220 */
221 #define KUNIT_CASE_PARAM_ATTR(test_name, gen_params, attributes) \
222 { .run_case = test_name, .name = #test_name, \
223 .generate_params = gen_params, \
224 .attr = attributes, .module_name = KBUILD_MODNAME}
225
226 /**
227 * 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 */
273 struct 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 */
291 struct 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. */
297 struct 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 */
325 struct 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
kunit_set_failure(struct kunit * test)359 static inline void kunit_set_failure(struct kunit *test)
360 {
361 WRITE_ONCE(test->status, KUNIT_FAILURE);
362 }
363
364 bool kunit_enabled(void);
365 bool kunit_autorun(void);
366 const char *kunit_action(void);
367 const char *kunit_filter_glob(void);
368 char *kunit_filter(void);
369 char *kunit_filter_action(void);
370
371 void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log);
372
373 int kunit_run_tests(struct kunit_suite *suite);
374
375 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
376
377 unsigned int kunit_test_case_num(struct kunit_suite *suite,
378 struct kunit_case *test_case);
379
380 struct kunit_suite_set
381 kunit_filter_suites(const struct kunit_suite_set *suite_set,
382 const char *filter_glob,
383 char *filters,
384 char *filter_action,
385 int *err);
386 void kunit_free_suite_set(struct kunit_suite_set suite_set);
387
388 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites,
389 bool run_tests);
390
391 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
392
393 void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin);
394 void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr);
395
396 struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set,
397 struct kunit_suite_set suite_set);
398
399 const void *kunit_array_gen_params(struct kunit *test, const void *prev, char *desc);
400
401 #if IS_BUILTIN(CONFIG_KUNIT)
402 int kunit_run_all_tests(void);
403 #else
kunit_run_all_tests(void)404 static 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
470 enum 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 */
486 void *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 */
kunit_kmalloc(struct kunit * test,size_t size,gfp_t gfp)499 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
500 {
501 return kunit_kmalloc_array(test, 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 */
509 void 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 */
kunit_kzalloc(struct kunit * test,size_t size,gfp_t gfp)519 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
520 {
521 return kunit_kmalloc(test, size, 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 */
kunit_kcalloc(struct kunit * test,size_t n,size_t size,gfp_t gfp)533 static 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_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 */
547 void 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 */
kunit_kstrdup(struct kunit * test,const char * str,gfp_t gfp)558 static 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, len, gfp);
568 if (buf)
569 memcpy(buf, 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 */
584 const 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 */
596 int 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 */
610 unsigned 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
615 void kunit_cleanup(struct kunit *test);
616 void kunit_free_boot_suites(void);
617
618 void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...);
619
620 /**
621 * kunit_mark_skipped() - Marks @test as skipped
622 *
623 * @test: The test context object.
624 * @fmt: A printk() style format string.
625 *
626 * Marks the test as skipped. @fmt is given output as the test status
627 * comment, typically the reason the test was skipped.
628 *
629 * Test execution continues after kunit_mark_skipped() is called.
630 */
631 #define kunit_mark_skipped(test, fmt, ...) \
632 do { \
633 WRITE_ONCE((test)->status, KUNIT_SKIPPED); \
634 scnprintf((test)->status_comment, \
635 KUNIT_STATUS_COMMENT_SIZE, \
636 fmt, ##__VA_ARGS__); \
637 } while (0)
638
639 /**
640 * kunit_skip() - Marks @test as skipped
641 *
642 * @test: The test context object.
643 * @fmt: A printk() style format string.
644 *
645 * Skips the test. @fmt is given output as the test status
646 * comment, typically the reason the test was skipped.
647 *
648 * Test execution is halted after kunit_skip() is called.
649 */
650 #define kunit_skip(test, fmt, ...) \
651 do { \
652 kunit_mark_skipped((test), fmt, ##__VA_ARGS__); \
653 kunit_try_catch_throw(&((test)->try_catch)); \
654 } while (0)
655
656 /*
657 * printk and log to per-test or per-suite log buffer. Logging only done
658 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
659 */
660 #define kunit_log(lvl, test_or_suite, fmt, ...) \
661 do { \
662 printk(lvl fmt, ##__VA_ARGS__); \
663 kunit_log_append((test_or_suite)->log, fmt, \
664 ##__VA_ARGS__); \
665 } while (0)
666
667 #define kunit_printk(lvl, test, fmt, ...) \
668 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
669 (test)->name, ##__VA_ARGS__)
670
671 /**
672 * kunit_info() - Prints an INFO level message associated with @test.
673 *
674 * @test: The test context object.
675 * @fmt: A printk() style format string.
676 *
677 * Prints an info level message associated with the test suite being run.
678 * Takes a variable number of format parameters just like printk().
679 */
680 #define kunit_info(test, fmt, ...) \
681 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
682
683 /**
684 * kunit_warn() - Prints a WARN level message associated with @test.
685 *
686 * @test: The test context object.
687 * @fmt: A printk() style format string.
688 *
689 * Prints a warning level message.
690 */
691 #define kunit_warn(test, fmt, ...) \
692 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
693
694 /**
695 * kunit_err() - Prints an ERROR level message associated with @test.
696 *
697 * @test: The test context object.
698 * @fmt: A printk() style format string.
699 *
700 * Prints an error level message.
701 */
702 #define kunit_err(test, fmt, ...) \
703 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
704
705 /*
706 * Must be called at the beginning of each KUNIT_*_ASSERTION().
707 * Cf. KUNIT_CURRENT_LOC.
708 */
709 #define _KUNIT_SAVE_LOC(test) do { \
710 WRITE_ONCE(test->last_seen.file, __FILE__); \
711 WRITE_ONCE(test->last_seen.line, __LINE__); \
712 } while (0)
713
714 /**
715 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
716 * @test: The test context object.
717 *
718 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
719 * words, it does nothing and only exists for code clarity. See
720 * KUNIT_EXPECT_TRUE() for more information.
721 */
722 #define KUNIT_SUCCEED(test) _KUNIT_SAVE_LOC(test)
723
724 void __noreturn __kunit_abort(struct kunit *test);
725
726 void __printf(6, 7) __kunit_do_failed_assertion(struct kunit *test,
727 const struct kunit_loc *loc,
728 enum kunit_assert_type type,
729 const struct kunit_assert *assert,
730 assert_format_t assert_format,
731 const char *fmt, ...);
732
733 #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
734 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
735 const struct assert_class __assertion = INITIALIZER; \
736 __kunit_do_failed_assertion(test, \
737 &__loc, \
738 assert_type, \
739 &__assertion.assert, \
740 assert_format, \
741 fmt, \
742 ##__VA_ARGS__); \
743 if (assert_type == KUNIT_ASSERTION) \
744 __kunit_abort(test); \
745 } while (0)
746
747
748 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) do { \
749 _KUNIT_SAVE_LOC(test); \
750 _KUNIT_FAILED(test, \
751 assert_type, \
752 kunit_fail_assert, \
753 kunit_fail_assert_format, \
754 {}, \
755 fmt, \
756 ##__VA_ARGS__); \
757 } while (0)
758
759 /**
760 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
761 * @test: The test context object.
762 * @fmt: an informational message to be printed when the assertion is made.
763 * @...: string format arguments.
764 *
765 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
766 * other words, it always results in a failed expectation, and consequently
767 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
768 * for more information.
769 */
770 #define KUNIT_FAIL(test, fmt, ...) \
771 KUNIT_FAIL_ASSERTION(test, \
772 KUNIT_EXPECTATION, \
773 fmt, \
774 ##__VA_ARGS__)
775
776 /* Helper to safely pass around an initializer list to other macros. */
777 #define KUNIT_INIT_ASSERT(initializers...) { initializers }
778
779 #define KUNIT_UNARY_ASSERTION(test, \
780 assert_type, \
781 condition_, \
782 expected_true_, \
783 fmt, \
784 ...) \
785 do { \
786 _KUNIT_SAVE_LOC(test); \
787 if (likely(!!(condition_) == !!expected_true_)) \
788 break; \
789 \
790 _KUNIT_FAILED(test, \
791 assert_type, \
792 kunit_unary_assert, \
793 kunit_unary_assert_format, \
794 KUNIT_INIT_ASSERT(.condition = #condition_, \
795 .expected_true = expected_true_), \
796 fmt, \
797 ##__VA_ARGS__); \
798 } while (0)
799
800 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
801 KUNIT_UNARY_ASSERTION(test, \
802 assert_type, \
803 condition, \
804 true, \
805 fmt, \
806 ##__VA_ARGS__)
807
808 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
809 KUNIT_UNARY_ASSERTION(test, \
810 assert_type, \
811 condition, \
812 false, \
813 fmt, \
814 ##__VA_ARGS__)
815
816 /*
817 * A factory macro for defining the assertions and expectations for the basic
818 * comparisons defined for the built in types.
819 *
820 * Unfortunately, there is no common type that all types can be promoted to for
821 * which all the binary operators behave the same way as for the actual types
822 * (for example, there is no type that long long and unsigned long long can
823 * both be cast to where the comparison result is preserved for all values). So
824 * the best we can do is do the comparison in the original types and then coerce
825 * everything to long long for printing; this way, the comparison behaves
826 * correctly and the printed out value usually makes sense without
827 * interpretation, but can always be interpreted to figure out the actual
828 * value.
829 */
830 #define KUNIT_BASE_BINARY_ASSERTION(test, \
831 assert_class, \
832 format_func, \
833 assert_type, \
834 left, \
835 op, \
836 right, \
837 fmt, \
838 ...) \
839 do { \
840 const typeof(left) __left = (left); \
841 const typeof(right) __right = (right); \
842 static const struct kunit_binary_assert_text __text = { \
843 .operation = #op, \
844 .left_text = #left, \
845 .right_text = #right, \
846 }; \
847 \
848 _KUNIT_SAVE_LOC(test); \
849 if (likely(__left op __right)) \
850 break; \
851 \
852 _KUNIT_FAILED(test, \
853 assert_type, \
854 assert_class, \
855 format_func, \
856 KUNIT_INIT_ASSERT(.text = &__text, \
857 .left_value = __left, \
858 .right_value = __right), \
859 fmt, \
860 ##__VA_ARGS__); \
861 } while (0)
862
863 #define KUNIT_BINARY_INT_ASSERTION(test, \
864 assert_type, \
865 left, \
866 op, \
867 right, \
868 fmt, \
869 ...) \
870 KUNIT_BASE_BINARY_ASSERTION(test, \
871 kunit_binary_assert, \
872 kunit_binary_assert_format, \
873 assert_type, \
874 left, op, right, \
875 fmt, \
876 ##__VA_ARGS__)
877
878 #define KUNIT_BINARY_PTR_ASSERTION(test, \
879 assert_type, \
880 left, \
881 op, \
882 right, \
883 fmt, \
884 ...) \
885 KUNIT_BASE_BINARY_ASSERTION(test, \
886 kunit_binary_ptr_assert, \
887 kunit_binary_ptr_assert_format, \
888 assert_type, \
889 left, op, right, \
890 fmt, \
891 ##__VA_ARGS__)
892
893 #define KUNIT_BINARY_STR_ASSERTION(test, \
894 assert_type, \
895 left, \
896 op, \
897 right, \
898 fmt, \
899 ...) \
900 do { \
901 const char *__left = (left); \
902 const char *__right = (right); \
903 static const struct kunit_binary_assert_text __text = { \
904 .operation = #op, \
905 .left_text = #left, \
906 .right_text = #right, \
907 }; \
908 \
909 _KUNIT_SAVE_LOC(test); \
910 if (likely(!IS_ERR_OR_NULL(__left) && !IS_ERR_OR_NULL(__right) && \
911 (strcmp(__left, __right) op 0))) \
912 break; \
913 \
914 \
915 _KUNIT_FAILED(test, \
916 assert_type, \
917 kunit_binary_str_assert, \
918 kunit_binary_str_assert_format, \
919 KUNIT_INIT_ASSERT(.text = &__text, \
920 .left_value = __left, \
921 .right_value = __right), \
922 fmt, \
923 ##__VA_ARGS__); \
924 } while (0)
925
926 #define KUNIT_MEM_ASSERTION(test, \
927 assert_type, \
928 left, \
929 op, \
930 right, \
931 size_, \
932 fmt, \
933 ...) \
934 do { \
935 const void *__left = (left); \
936 const void *__right = (right); \
937 const size_t __size = (size_); \
938 static const struct kunit_binary_assert_text __text = { \
939 .operation = #op, \
940 .left_text = #left, \
941 .right_text = #right, \
942 }; \
943 \
944 _KUNIT_SAVE_LOC(test); \
945 if (likely(__left && __right)) \
946 if (likely(memcmp(__left, __right, __size) op 0)) \
947 break; \
948 \
949 _KUNIT_FAILED(test, \
950 assert_type, \
951 kunit_mem_assert, \
952 kunit_mem_assert_format, \
953 KUNIT_INIT_ASSERT(.text = &__text, \
954 .left_value = __left, \
955 .right_value = __right, \
956 .size = __size), \
957 fmt, \
958 ##__VA_ARGS__); \
959 } while (0)
960
961 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
962 assert_type, \
963 ptr, \
964 fmt, \
965 ...) \
966 do { \
967 const typeof(ptr) __ptr = (ptr); \
968 \
969 _KUNIT_SAVE_LOC(test); \
970 if (!IS_ERR_OR_NULL(__ptr)) \
971 break; \
972 \
973 _KUNIT_FAILED(test, \
974 assert_type, \
975 kunit_ptr_not_err_assert, \
976 kunit_ptr_not_err_assert_format, \
977 KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \
978 fmt, \
979 ##__VA_ARGS__); \
980 } while (0)
981
982 /**
983 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
984 * @test: The test context object.
985 * @condition: an arbitrary boolean expression. The test fails when this does
986 * not evaluate to true.
987 *
988 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
989 * to fail when the specified condition is not met; however, it will not prevent
990 * the test case from continuing to run; this is otherwise known as an
991 * *expectation failure*.
992 */
993 #define KUNIT_EXPECT_TRUE(test, condition) \
994 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
995
996 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
997 KUNIT_TRUE_MSG_ASSERTION(test, \
998 KUNIT_EXPECTATION, \
999 condition, \
1000 fmt, \
1001 ##__VA_ARGS__)
1002
1003 /**
1004 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
1005 * @test: The test context object.
1006 * @condition: an arbitrary boolean expression. The test fails when this does
1007 * not evaluate to false.
1008 *
1009 * Sets an expectation that @condition evaluates to false. See
1010 * KUNIT_EXPECT_TRUE() for more information.
1011 */
1012 #define KUNIT_EXPECT_FALSE(test, condition) \
1013 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
1014
1015 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
1016 KUNIT_FALSE_MSG_ASSERTION(test, \
1017 KUNIT_EXPECTATION, \
1018 condition, \
1019 fmt, \
1020 ##__VA_ARGS__)
1021
1022 /**
1023 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
1024 * @test: The test context object.
1025 * @left: an arbitrary expression that evaluates to a primitive C type.
1026 * @right: an arbitrary expression that evaluates to a primitive C type.
1027 *
1028 * Sets an expectation that the values that @left and @right evaluate to are
1029 * equal. This is semantically equivalent to
1030 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1031 * more information.
1032 */
1033 #define KUNIT_EXPECT_EQ(test, left, right) \
1034 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
1035
1036 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
1037 KUNIT_BINARY_INT_ASSERTION(test, \
1038 KUNIT_EXPECTATION, \
1039 left, ==, right, \
1040 fmt, \
1041 ##__VA_ARGS__)
1042
1043 /**
1044 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
1045 * @test: The test context object.
1046 * @left: an arbitrary expression that evaluates to a pointer.
1047 * @right: an arbitrary expression that evaluates to a pointer.
1048 *
1049 * Sets an expectation that the values that @left and @right evaluate to are
1050 * equal. This is semantically equivalent to
1051 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1052 * more information.
1053 */
1054 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
1055 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
1056
1057 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1058 KUNIT_BINARY_PTR_ASSERTION(test, \
1059 KUNIT_EXPECTATION, \
1060 left, ==, right, \
1061 fmt, \
1062 ##__VA_ARGS__)
1063
1064 /**
1065 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
1066 * @test: The test context object.
1067 * @left: an arbitrary expression that evaluates to a primitive C type.
1068 * @right: an arbitrary expression that evaluates to a primitive C type.
1069 *
1070 * Sets an expectation that the values that @left and @right evaluate to are not
1071 * equal. This is semantically equivalent to
1072 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1073 * more information.
1074 */
1075 #define KUNIT_EXPECT_NE(test, left, right) \
1076 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
1077
1078 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
1079 KUNIT_BINARY_INT_ASSERTION(test, \
1080 KUNIT_EXPECTATION, \
1081 left, !=, right, \
1082 fmt, \
1083 ##__VA_ARGS__)
1084
1085 /**
1086 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
1087 * @test: The test context object.
1088 * @left: an arbitrary expression that evaluates to a pointer.
1089 * @right: an arbitrary expression that evaluates to a pointer.
1090 *
1091 * Sets an expectation that the values that @left and @right evaluate to are not
1092 * equal. This is semantically equivalent to
1093 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1094 * more information.
1095 */
1096 #define KUNIT_EXPECT_PTR_NE(test, left, right) \
1097 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
1098
1099 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
1100 KUNIT_BINARY_PTR_ASSERTION(test, \
1101 KUNIT_EXPECTATION, \
1102 left, !=, right, \
1103 fmt, \
1104 ##__VA_ARGS__)
1105
1106 /**
1107 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
1108 * @test: The test context object.
1109 * @left: an arbitrary expression that evaluates to a primitive C type.
1110 * @right: an arbitrary expression that evaluates to a primitive C type.
1111 *
1112 * Sets an expectation that the value that @left evaluates to is less than the
1113 * value that @right evaluates to. This is semantically equivalent to
1114 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
1115 * more information.
1116 */
1117 #define KUNIT_EXPECT_LT(test, left, right) \
1118 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
1119
1120 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
1121 KUNIT_BINARY_INT_ASSERTION(test, \
1122 KUNIT_EXPECTATION, \
1123 left, <, right, \
1124 fmt, \
1125 ##__VA_ARGS__)
1126
1127 /**
1128 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
1129 * @test: The test context object.
1130 * @left: an arbitrary expression that evaluates to a primitive C type.
1131 * @right: an arbitrary expression that evaluates to a primitive C type.
1132 *
1133 * Sets an expectation that the value that @left evaluates to is less than or
1134 * equal to the value that @right evaluates to. Semantically this is equivalent
1135 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
1136 * more information.
1137 */
1138 #define KUNIT_EXPECT_LE(test, left, right) \
1139 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
1140
1141 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
1142 KUNIT_BINARY_INT_ASSERTION(test, \
1143 KUNIT_EXPECTATION, \
1144 left, <=, right, \
1145 fmt, \
1146 ##__VA_ARGS__)
1147
1148 /**
1149 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
1150 * @test: The test context object.
1151 * @left: an arbitrary expression that evaluates to a primitive C type.
1152 * @right: an arbitrary expression that evaluates to a primitive C type.
1153 *
1154 * Sets an expectation that the value that @left evaluates to is greater than
1155 * the value that @right evaluates to. This is semantically equivalent to
1156 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
1157 * more information.
1158 */
1159 #define KUNIT_EXPECT_GT(test, left, right) \
1160 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
1161
1162 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
1163 KUNIT_BINARY_INT_ASSERTION(test, \
1164 KUNIT_EXPECTATION, \
1165 left, >, right, \
1166 fmt, \
1167 ##__VA_ARGS__)
1168
1169 /**
1170 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1171 * @test: The test context object.
1172 * @left: an arbitrary expression that evaluates to a primitive C type.
1173 * @right: an arbitrary expression that evaluates to a primitive C type.
1174 *
1175 * Sets an expectation that the value that @left evaluates to is greater than
1176 * the value that @right evaluates to. This is semantically equivalent to
1177 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1178 * more information.
1179 */
1180 #define KUNIT_EXPECT_GE(test, left, right) \
1181 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
1182
1183 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
1184 KUNIT_BINARY_INT_ASSERTION(test, \
1185 KUNIT_EXPECTATION, \
1186 left, >=, right, \
1187 fmt, \
1188 ##__VA_ARGS__)
1189
1190 /**
1191 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1192 * @test: The test context object.
1193 * @left: an arbitrary expression that evaluates to a null terminated string.
1194 * @right: an arbitrary expression that evaluates to a null terminated string.
1195 *
1196 * Sets an expectation that the values that @left and @right evaluate to are
1197 * equal. This is semantically equivalent to
1198 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1199 * for more information.
1200 */
1201 #define KUNIT_EXPECT_STREQ(test, left, right) \
1202 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
1203
1204 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
1205 KUNIT_BINARY_STR_ASSERTION(test, \
1206 KUNIT_EXPECTATION, \
1207 left, ==, right, \
1208 fmt, \
1209 ##__VA_ARGS__)
1210
1211 /**
1212 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1213 * @test: The test context object.
1214 * @left: an arbitrary expression that evaluates to a null terminated string.
1215 * @right: an arbitrary expression that evaluates to a null terminated string.
1216 *
1217 * Sets an expectation that the values that @left and @right evaluate to are
1218 * not equal. This is semantically equivalent to
1219 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1220 * for more information.
1221 */
1222 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
1223 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
1224
1225 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
1226 KUNIT_BINARY_STR_ASSERTION(test, \
1227 KUNIT_EXPECTATION, \
1228 left, !=, right, \
1229 fmt, \
1230 ##__VA_ARGS__)
1231
1232 /**
1233 * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.
1234 * @test: The test context object.
1235 * @left: An arbitrary expression that evaluates to the specified size.
1236 * @right: An arbitrary expression that evaluates to the specified size.
1237 * @size: Number of bytes compared.
1238 *
1239 * Sets an expectation that the values that @left and @right evaluate to are
1240 * equal. This is semantically equivalent to
1241 * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
1242 * KUNIT_EXPECT_TRUE() for more information.
1243 *
1244 * Although this expectation works for any memory block, it is not recommended
1245 * for comparing more structured data, such as structs. This expectation is
1246 * recommended for comparing, for example, data arrays.
1247 */
1248 #define KUNIT_EXPECT_MEMEQ(test, left, right, size) \
1249 KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)
1250
1251 #define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
1252 KUNIT_MEM_ASSERTION(test, \
1253 KUNIT_EXPECTATION, \
1254 left, ==, right, \
1255 size, \
1256 fmt, \
1257 ##__VA_ARGS__)
1258
1259 /**
1260 * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.
1261 * @test: The test context object.
1262 * @left: An arbitrary expression that evaluates to the specified size.
1263 * @right: An arbitrary expression that evaluates to the specified size.
1264 * @size: Number of bytes compared.
1265 *
1266 * Sets an expectation that the values that @left and @right evaluate to are
1267 * not equal. This is semantically equivalent to
1268 * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1269 * KUNIT_EXPECT_TRUE() for more information.
1270 *
1271 * Although this expectation works for any memory block, it is not recommended
1272 * for comparing more structured data, such as structs. This expectation is
1273 * recommended for comparing, for example, data arrays.
1274 */
1275 #define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \
1276 KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)
1277
1278 #define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1279 KUNIT_MEM_ASSERTION(test, \
1280 KUNIT_EXPECTATION, \
1281 left, !=, right, \
1282 size, \
1283 fmt, \
1284 ##__VA_ARGS__)
1285
1286 /**
1287 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
1288 * @test: The test context object.
1289 * @ptr: an arbitrary pointer.
1290 *
1291 * Sets an expectation that the value that @ptr evaluates to is null. This is
1292 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
1293 * See KUNIT_EXPECT_TRUE() for more information.
1294 */
1295 #define KUNIT_EXPECT_NULL(test, ptr) \
1296 KUNIT_EXPECT_NULL_MSG(test, \
1297 ptr, \
1298 NULL)
1299
1300 #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
1301 KUNIT_BINARY_PTR_ASSERTION(test, \
1302 KUNIT_EXPECTATION, \
1303 ptr, ==, NULL, \
1304 fmt, \
1305 ##__VA_ARGS__)
1306
1307 /**
1308 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
1309 * @test: The test context object.
1310 * @ptr: an arbitrary pointer.
1311 *
1312 * Sets an expectation that the value that @ptr evaluates to is not null. This
1313 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
1314 * See KUNIT_EXPECT_TRUE() for more information.
1315 */
1316 #define KUNIT_EXPECT_NOT_NULL(test, ptr) \
1317 KUNIT_EXPECT_NOT_NULL_MSG(test, \
1318 ptr, \
1319 NULL)
1320
1321 #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1322 KUNIT_BINARY_PTR_ASSERTION(test, \
1323 KUNIT_EXPECTATION, \
1324 ptr, !=, NULL, \
1325 fmt, \
1326 ##__VA_ARGS__)
1327
1328 /**
1329 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1330 * @test: The test context object.
1331 * @ptr: an arbitrary pointer.
1332 *
1333 * Sets an expectation that the value that @ptr evaluates to is not null and not
1334 * an errno stored in a pointer. This is semantically equivalent to
1335 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1336 * more information.
1337 */
1338 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1339 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1340
1341 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1342 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1343 KUNIT_EXPECTATION, \
1344 ptr, \
1345 fmt, \
1346 ##__VA_ARGS__)
1347
1348 /**
1349 * KUNIT_FAIL_AND_ABORT() - Always causes a test to fail and abort when evaluated.
1350 * @test: The test context object.
1351 * @fmt: an informational message to be printed when the assertion is made.
1352 * @...: string format arguments.
1353 *
1354 * The opposite of KUNIT_SUCCEED(), it is an assertion that always fails. In
1355 * other words, it always results in a failed assertion, and consequently
1356 * always causes the test case to fail and abort when evaluated.
1357 * See KUNIT_ASSERT_TRUE() for more information.
1358 */
1359 #define KUNIT_FAIL_AND_ABORT(test, fmt, ...) \
1360 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1361
1362 /**
1363 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1364 * @test: The test context object.
1365 * @condition: an arbitrary boolean expression. The test fails and aborts when
1366 * this does not evaluate to true.
1367 *
1368 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1369 * fail *and immediately abort* when the specified condition is not met. Unlike
1370 * an expectation failure, it will prevent the test case from continuing to run;
1371 * this is otherwise known as an *assertion failure*.
1372 */
1373 #define KUNIT_ASSERT_TRUE(test, condition) \
1374 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
1375
1376 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1377 KUNIT_TRUE_MSG_ASSERTION(test, \
1378 KUNIT_ASSERTION, \
1379 condition, \
1380 fmt, \
1381 ##__VA_ARGS__)
1382
1383 /**
1384 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1385 * @test: The test context object.
1386 * @condition: an arbitrary boolean expression.
1387 *
1388 * Sets an assertion that the value that @condition evaluates to is false. This
1389 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1390 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1391 */
1392 #define KUNIT_ASSERT_FALSE(test, condition) \
1393 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
1394
1395 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1396 KUNIT_FALSE_MSG_ASSERTION(test, \
1397 KUNIT_ASSERTION, \
1398 condition, \
1399 fmt, \
1400 ##__VA_ARGS__)
1401
1402 /**
1403 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1404 * @test: The test context object.
1405 * @left: an arbitrary expression that evaluates to a primitive C type.
1406 * @right: an arbitrary expression that evaluates to a primitive C type.
1407 *
1408 * Sets an assertion that the values that @left and @right evaluate to are
1409 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1410 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1411 */
1412 #define KUNIT_ASSERT_EQ(test, left, right) \
1413 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
1414
1415 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1416 KUNIT_BINARY_INT_ASSERTION(test, \
1417 KUNIT_ASSERTION, \
1418 left, ==, right, \
1419 fmt, \
1420 ##__VA_ARGS__)
1421
1422 /**
1423 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1424 * @test: The test context object.
1425 * @left: an arbitrary expression that evaluates to a pointer.
1426 * @right: an arbitrary expression that evaluates to a pointer.
1427 *
1428 * Sets an assertion that the values that @left and @right evaluate to are
1429 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1430 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1431 */
1432 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1433 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
1434
1435 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1436 KUNIT_BINARY_PTR_ASSERTION(test, \
1437 KUNIT_ASSERTION, \
1438 left, ==, right, \
1439 fmt, \
1440 ##__VA_ARGS__)
1441
1442 /**
1443 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1444 * @test: The test context object.
1445 * @left: an arbitrary expression that evaluates to a primitive C type.
1446 * @right: an arbitrary expression that evaluates to a primitive C type.
1447 *
1448 * Sets an assertion that the values that @left and @right evaluate to are not
1449 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1450 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1451 */
1452 #define KUNIT_ASSERT_NE(test, left, right) \
1453 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
1454
1455 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1456 KUNIT_BINARY_INT_ASSERTION(test, \
1457 KUNIT_ASSERTION, \
1458 left, !=, right, \
1459 fmt, \
1460 ##__VA_ARGS__)
1461
1462 /**
1463 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1464 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1465 * @test: The test context object.
1466 * @left: an arbitrary expression that evaluates to a pointer.
1467 * @right: an arbitrary expression that evaluates to a pointer.
1468 *
1469 * Sets an assertion that the values that @left and @right evaluate to are not
1470 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1471 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1472 */
1473 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1474 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1475
1476 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1477 KUNIT_BINARY_PTR_ASSERTION(test, \
1478 KUNIT_ASSERTION, \
1479 left, !=, right, \
1480 fmt, \
1481 ##__VA_ARGS__)
1482 /**
1483 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1484 * @test: The test context object.
1485 * @left: an arbitrary expression that evaluates to a primitive C type.
1486 * @right: an arbitrary expression that evaluates to a primitive C type.
1487 *
1488 * Sets an assertion that the value that @left evaluates to is less than the
1489 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1490 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1491 * is not met.
1492 */
1493 #define KUNIT_ASSERT_LT(test, left, right) \
1494 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1495
1496 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1497 KUNIT_BINARY_INT_ASSERTION(test, \
1498 KUNIT_ASSERTION, \
1499 left, <, right, \
1500 fmt, \
1501 ##__VA_ARGS__)
1502 /**
1503 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1504 * @test: The test context object.
1505 * @left: an arbitrary expression that evaluates to a primitive C type.
1506 * @right: an arbitrary expression that evaluates to a primitive C type.
1507 *
1508 * Sets an assertion that the value that @left evaluates to is less than or
1509 * equal to the value that @right evaluates to. This is the same as
1510 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1511 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1512 */
1513 #define KUNIT_ASSERT_LE(test, left, right) \
1514 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1515
1516 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1517 KUNIT_BINARY_INT_ASSERTION(test, \
1518 KUNIT_ASSERTION, \
1519 left, <=, right, \
1520 fmt, \
1521 ##__VA_ARGS__)
1522
1523 /**
1524 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1525 * @test: The test context object.
1526 * @left: an arbitrary expression that evaluates to a primitive C type.
1527 * @right: an arbitrary expression that evaluates to a primitive C type.
1528 *
1529 * Sets an assertion that the value that @left evaluates to is greater than the
1530 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1531 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1532 * is not met.
1533 */
1534 #define KUNIT_ASSERT_GT(test, left, right) \
1535 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1536
1537 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1538 KUNIT_BINARY_INT_ASSERTION(test, \
1539 KUNIT_ASSERTION, \
1540 left, >, right, \
1541 fmt, \
1542 ##__VA_ARGS__)
1543
1544 /**
1545 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1546 * @test: The test context object.
1547 * @left: an arbitrary expression that evaluates to a primitive C type.
1548 * @right: an arbitrary expression that evaluates to a primitive C type.
1549 *
1550 * Sets an assertion that the value that @left evaluates to is greater than the
1551 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1552 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1553 * is not met.
1554 */
1555 #define KUNIT_ASSERT_GE(test, left, right) \
1556 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1557
1558 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1559 KUNIT_BINARY_INT_ASSERTION(test, \
1560 KUNIT_ASSERTION, \
1561 left, >=, right, \
1562 fmt, \
1563 ##__VA_ARGS__)
1564
1565 /**
1566 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1567 * @test: The test context object.
1568 * @left: an arbitrary expression that evaluates to a null terminated string.
1569 * @right: an arbitrary expression that evaluates to a null terminated string.
1570 *
1571 * Sets an assertion that the values that @left and @right evaluate to are
1572 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1573 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1574 */
1575 #define KUNIT_ASSERT_STREQ(test, left, right) \
1576 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1577
1578 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1579 KUNIT_BINARY_STR_ASSERTION(test, \
1580 KUNIT_ASSERTION, \
1581 left, ==, right, \
1582 fmt, \
1583 ##__VA_ARGS__)
1584
1585 /**
1586 * KUNIT_ASSERT_STRNEQ() - An assertion that strings @left and @right are not equal.
1587 * @test: The test context object.
1588 * @left: an arbitrary expression that evaluates to a null terminated string.
1589 * @right: an arbitrary expression that evaluates to a null terminated string.
1590 *
1591 * Sets an assertion that the values that @left and @right evaluate to are
1592 * not equal. This is semantically equivalent to
1593 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1594 * for more information.
1595 */
1596 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1597 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1598
1599 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1600 KUNIT_BINARY_STR_ASSERTION(test, \
1601 KUNIT_ASSERTION, \
1602 left, !=, right, \
1603 fmt, \
1604 ##__VA_ARGS__)
1605
1606 /**
1607 * KUNIT_ASSERT_MEMEQ() - Asserts that the first @size bytes of @left and @right are equal.
1608 * @test: The test context object.
1609 * @left: An arbitrary expression that evaluates to the specified size.
1610 * @right: An arbitrary expression that evaluates to the specified size.
1611 * @size: Number of bytes compared.
1612 *
1613 * Sets an assertion that the values that @left and @right evaluate to are
1614 * equal. This is semantically equivalent to
1615 * KUNIT_ASSERT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
1616 * KUNIT_ASSERT_TRUE() for more information.
1617 *
1618 * Although this assertion works for any memory block, it is not recommended
1619 * for comparing more structured data, such as structs. This assertion is
1620 * recommended for comparing, for example, data arrays.
1621 */
1622 #define KUNIT_ASSERT_MEMEQ(test, left, right, size) \
1623 KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, NULL)
1624
1625 #define KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
1626 KUNIT_MEM_ASSERTION(test, \
1627 KUNIT_ASSERTION, \
1628 left, ==, right, \
1629 size, \
1630 fmt, \
1631 ##__VA_ARGS__)
1632
1633 /**
1634 * KUNIT_ASSERT_MEMNEQ() - Asserts that the first @size bytes of @left and @right are not equal.
1635 * @test: The test context object.
1636 * @left: An arbitrary expression that evaluates to the specified size.
1637 * @right: An arbitrary expression that evaluates to the specified size.
1638 * @size: Number of bytes compared.
1639 *
1640 * Sets an assertion that the values that @left and @right evaluate to are
1641 * not equal. This is semantically equivalent to
1642 * KUNIT_ASSERT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1643 * KUNIT_ASSERT_TRUE() for more information.
1644 *
1645 * Although this assertion works for any memory block, it is not recommended
1646 * for comparing more structured data, such as structs. This assertion is
1647 * recommended for comparing, for example, data arrays.
1648 */
1649 #define KUNIT_ASSERT_MEMNEQ(test, left, right, size) \
1650 KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, NULL)
1651
1652 #define KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1653 KUNIT_MEM_ASSERTION(test, \
1654 KUNIT_ASSERTION, \
1655 left, !=, right, \
1656 size, \
1657 fmt, \
1658 ##__VA_ARGS__)
1659
1660 /**
1661 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1662 * @test: The test context object.
1663 * @ptr: an arbitrary pointer.
1664 *
1665 * Sets an assertion that the values that @ptr evaluates to is null. This is
1666 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1667 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1668 */
1669 #define KUNIT_ASSERT_NULL(test, ptr) \
1670 KUNIT_ASSERT_NULL_MSG(test, \
1671 ptr, \
1672 NULL)
1673
1674 #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1675 KUNIT_BINARY_PTR_ASSERTION(test, \
1676 KUNIT_ASSERTION, \
1677 ptr, ==, NULL, \
1678 fmt, \
1679 ##__VA_ARGS__)
1680
1681 /**
1682 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1683 * @test: The test context object.
1684 * @ptr: an arbitrary pointer.
1685 *
1686 * Sets an assertion that the values that @ptr evaluates to is not null. This
1687 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1688 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1689 */
1690 #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1691 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1692 ptr, \
1693 NULL)
1694
1695 #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1696 KUNIT_BINARY_PTR_ASSERTION(test, \
1697 KUNIT_ASSERTION, \
1698 ptr, !=, NULL, \
1699 fmt, \
1700 ##__VA_ARGS__)
1701
1702 /**
1703 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1704 * @test: The test context object.
1705 * @ptr: an arbitrary pointer.
1706 *
1707 * Sets an assertion that the value that @ptr evaluates to is not null and not
1708 * an errno stored in a pointer. This is the same as
1709 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1710 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1711 */
1712 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1713 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1714
1715 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1716 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1717 KUNIT_ASSERTION, \
1718 ptr, \
1719 fmt, \
1720 ##__VA_ARGS__)
1721
1722 /**
1723 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1724 * @name: prefix for the test parameter generator function.
1725 * @array: array of test parameters.
1726 * @get_desc: function to convert param to description; NULL to use default
1727 *
1728 * Define function @name_gen_params which uses @array to generate parameters.
1729 */
1730 #define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1731 static const void *name##_gen_params(struct kunit *test, \
1732 const void *prev, char *desc) \
1733 { \
1734 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1735 if (!prev) \
1736 kunit_register_params_array(test, array, ARRAY_SIZE(array), NULL); \
1737 if (__next - (array) < ARRAY_SIZE((array))) { \
1738 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1739 if (__get_desc) \
1740 __get_desc(__next, desc); \
1741 return __next; \
1742 } \
1743 return NULL; \
1744 }
1745
1746 /**
1747 * KUNIT_ARRAY_PARAM_DESC() - Define test parameter generator from an array.
1748 * @name: prefix for the test parameter generator function.
1749 * @array: array of test parameters.
1750 * @desc_member: structure member from array element to use as description
1751 *
1752 * Define function @name_gen_params which uses @array to generate parameters.
1753 */
1754 #define KUNIT_ARRAY_PARAM_DESC(name, array, desc_member) \
1755 static const void *name##_gen_params(struct kunit *test, \
1756 const void *prev, char *desc) \
1757 { \
1758 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1759 if (!prev) \
1760 kunit_register_params_array(test, array, ARRAY_SIZE(array), NULL); \
1761 if (__next - (array) < ARRAY_SIZE((array))) { \
1762 strscpy(desc, __next->desc_member, KUNIT_PARAM_DESC_SIZE); \
1763 return __next; \
1764 } \
1765 return NULL; \
1766 }
1767
1768 /**
1769 * kunit_register_params_array() - Register parameter array for a KUnit test.
1770 * @test: The KUnit test structure to which parameters will be added.
1771 * @array: An array of test parameters.
1772 * @param_count: Number of parameters.
1773 * @get_desc: Function that generates a string description for a given parameter
1774 * element.
1775 *
1776 * This macro initializes the @test's parameter array data, storing information
1777 * including the parameter array, its count, the element size, and the parameter
1778 * description function within `test->params_array`.
1779 *
1780 * Note: If using this macro in param_init(), kunit_array_gen_params()
1781 * will then need to be manually provided as the parameter generator function to
1782 * KUNIT_CASE_PARAM_WITH_INIT(). kunit_array_gen_params() is a KUnit
1783 * function that uses the registered array to generate parameters
1784 */
1785 #define kunit_register_params_array(test, array, param_count, get_desc) \
1786 do { \
1787 struct kunit *_test = (test); \
1788 const typeof((array)[0]) * _params_ptr = &(array)[0]; \
1789 _test->params_array.params = _params_ptr; \
1790 _test->params_array.num_params = (param_count); \
1791 _test->params_array.elem_size = sizeof(*_params_ptr); \
1792 _test->params_array.get_description = (get_desc); \
1793 } while (0)
1794
1795 // TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1796 // include resource.h themselves if they need it.
1797 #include <kunit/resource.h>
1798
1799 #endif /* _KUNIT_TEST_H */
1800