blob: 607e02b8e167cd8b894c2b7b02fe5a07496922a2 [file] [log] [blame]
Brendan Higgins914cc632019-09-23 02:02:31 -07001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Base unit test (KUnit) API.
4 *
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <[email protected]>
7 */
8
9#ifndef _KUNIT_TEST_H
10#define _KUNIT_TEST_H
11
Brendan Higgins73cda7b2019-09-23 02:02:35 -070012#include <kunit/assert.h>
Brendan Higgins5f3e0622019-09-23 02:02:39 -070013#include <kunit/try-catch.h>
Andy Shevchenkoec54c282021-11-08 18:32:15 -080014
Daniel Latypov4fdacef2022-01-13 08:59:27 -080015#include <linux/compiler.h>
Andy Shevchenkoec54c282021-11-08 18:32:15 -080016#include <linux/container_of.h>
17#include <linux/err.h>
18#include <linux/init.h>
19#include <linux/kconfig.h>
20#include <linux/kref.h>
21#include <linux/list.h>
Alan Maguirec475c772020-01-06 22:28:20 +000022#include <linux/module.h>
Brendan Higgins0a7568532019-09-23 02:02:32 -070023#include <linux/slab.h>
Andy Shevchenkoec54c282021-11-08 18:32:15 -080024#include <linux/spinlock.h>
25#include <linux/string.h>
Brendan Higgins914cc632019-09-23 02:02:31 -070026#include <linux/types.h>
Andy Shevchenkoec54c282021-11-08 18:32:15 -080027
28#include <asm/rwonce.h>
Brendan Higgins914cc632019-09-23 02:02:31 -070029
Brendan Higgins914cc632019-09-23 02:02:31 -070030struct kunit;
31
Alan Maguiree2219db2020-03-26 14:25:07 +000032/* Size of log associated with test. */
33#define KUNIT_LOG_SIZE 512
34
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +053035/* Maximum size of parameter description string. */
36#define KUNIT_PARAM_DESC_SIZE 128
37
David Gow6d2426b2021-06-24 23:58:12 -070038/* Maximum size of a status comment. */
39#define KUNIT_STATUS_COMMENT_SIZE 256
40
Alan Maguirec3bba692020-03-26 14:25:09 +000041/*
42 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
43 * sub-subtest. See the "Subtests" section in
44 * https://node-tap.org/tap-protocol/
45 */
46#define KUNIT_SUBTEST_INDENT " "
47#define KUNIT_SUBSUBTEST_INDENT " "
48
Brendan Higgins914cc632019-09-23 02:02:31 -070049/**
David Gow6d2426b2021-06-24 23:58:12 -070050 * enum kunit_status - Type of result for a test or test suite
51 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
52 * @KUNIT_FAILURE: Denotes the test has failed.
53 * @KUNIT_SKIPPED: Denotes the test has been skipped.
54 */
55enum kunit_status {
56 KUNIT_SUCCESS,
57 KUNIT_FAILURE,
58 KUNIT_SKIPPED,
59};
60
61/**
Brendan Higgins914cc632019-09-23 02:02:31 -070062 * struct kunit_case - represents an individual test case.
63 *
64 * @run_case: the function representing the actual test case.
65 * @name: the name of the test case.
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +053066 * @generate_params: the generator function for parameterized tests.
Brendan Higgins914cc632019-09-23 02:02:31 -070067 *
68 * A test case is a function with the signature,
Brendan Higginse4aea8f2019-09-23 02:02:41 -070069 * ``void (*)(struct kunit *)``
70 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
71 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
Brendan Higgins914cc632019-09-23 02:02:31 -070072 * with a &struct kunit_suite and will be run after the suite's init
73 * function and followed by the suite's exit function.
74 *
75 * A test case should be static and should only be created with the
76 * KUNIT_CASE() macro; additionally, every array of test cases should be
77 * terminated with an empty test case.
78 *
79 * Example:
80 *
81 * .. code-block:: c
82 *
83 * void add_test_basic(struct kunit *test)
84 * {
85 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
86 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
87 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
88 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
89 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
90 * }
91 *
92 * static struct kunit_case example_test_cases[] = {
93 * KUNIT_CASE(add_test_basic),
94 * {}
95 * };
96 *
97 */
98struct kunit_case {
99 void (*run_case)(struct kunit *test);
100 const char *name;
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +0530101 const void* (*generate_params)(const void *prev, char *desc);
Brendan Higgins914cc632019-09-23 02:02:31 -0700102
103 /* private: internal use only. */
David Gow6d2426b2021-06-24 23:58:12 -0700104 enum kunit_status status;
Alan Maguiree2219db2020-03-26 14:25:07 +0000105 char *log;
Brendan Higgins914cc632019-09-23 02:02:31 -0700106};
107
David Gow6d2426b2021-06-24 23:58:12 -0700108static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
Alan Maguiree2219db2020-03-26 14:25:07 +0000109{
David Gow6d2426b2021-06-24 23:58:12 -0700110 switch (status) {
111 case KUNIT_SKIPPED:
112 case KUNIT_SUCCESS:
113 return "ok";
114 case KUNIT_FAILURE:
115 return "not ok";
116 }
117 return "invalid";
Alan Maguiree2219db2020-03-26 14:25:07 +0000118}
119
Brendan Higgins914cc632019-09-23 02:02:31 -0700120/**
121 * KUNIT_CASE - A helper for creating a &struct kunit_case
122 *
123 * @test_name: a reference to a test case function.
124 *
125 * Takes a symbol for a function representing a test case and creates a
126 * &struct kunit_case object from it. See the documentation for
127 * &struct kunit_case for an example on how to use it.
128 */
129#define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
130
131/**
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +0530132 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
133 *
134 * @test_name: a reference to a test case function.
135 * @gen_params: a reference to a parameter generator function.
136 *
137 * The generator function::
138 *
139 * const void* gen_params(const void *prev, char *desc)
140 *
141 * is used to lazily generate a series of arbitrarily typed values that fit into
142 * a void*. The argument @prev is the previously returned value, which should be
143 * used to derive the next value; @prev is set to NULL on the initial generator
144 * call. When no more values are available, the generator must return NULL.
145 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
146 * describing the parameter.
147 */
148#define KUNIT_CASE_PARAM(test_name, gen_params) \
149 { .run_case = test_name, .name = #test_name, \
150 .generate_params = gen_params }
151
152/**
Brendan Higgins914cc632019-09-23 02:02:31 -0700153 * struct kunit_suite - describes a related collection of &struct kunit_case
154 *
155 * @name: the name of the test. Purely informational.
Daniel Latypov1cdba212022-04-29 11:12:57 -0700156 * @suite_init: called once per test suite before the test cases.
157 * @suite_exit: called once per test suite after all test cases.
Brendan Higgins914cc632019-09-23 02:02:31 -0700158 * @init: called before every test case.
159 * @exit: called after every test case.
160 * @test_cases: a null terminated array of test cases.
161 *
162 * A kunit_suite is a collection of related &struct kunit_case s, such that
163 * @init is called before every test case and @exit is called after every
164 * test case, similar to the notion of a *test fixture* or a *test class*
165 * in other unit testing frameworks like JUnit or Googletest.
166 *
167 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
168 * to run it.
169 */
170struct kunit_suite {
171 const char name[256];
Daniel Latypov1cdba212022-04-29 11:12:57 -0700172 int (*suite_init)(struct kunit_suite *suite);
173 void (*suite_exit)(struct kunit_suite *suite);
Brendan Higgins914cc632019-09-23 02:02:31 -0700174 int (*init)(struct kunit *test);
175 void (*exit)(struct kunit *test);
176 struct kunit_case *test_cases;
Alan Maguiree2219db2020-03-26 14:25:07 +0000177
Lothar Rubuschc4714b02020-04-15 20:16:53 +0000178 /* private: internal use only */
David Gow6d2426b2021-06-24 23:58:12 -0700179 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
Alan Maguiree2219db2020-03-26 14:25:07 +0000180 struct dentry *debugfs;
181 char *log;
Daniel Latypov1cdba212022-04-29 11:12:57 -0700182 int suite_init_err;
Brendan Higgins914cc632019-09-23 02:02:31 -0700183};
184
185/**
186 * struct kunit - represents a running instance of a test.
187 *
188 * @priv: for user to store arbitrary data. Commonly used to pass data
189 * created in the init function (see &struct kunit_suite).
190 *
191 * Used to store information about the current context under which the test
192 * is running. Most of this data is private and should only be accessed
193 * indirectly via public functions; the one exception is @priv which can be
194 * used by the test writer to store arbitrary data.
195 */
196struct kunit {
197 void *priv;
198
199 /* private: internal use only. */
200 const char *name; /* Read only after initialization! */
Alan Maguiree2219db2020-03-26 14:25:07 +0000201 char *log; /* Points at case log after initialization */
Brendan Higgins5f3e0622019-09-23 02:02:39 -0700202 struct kunit_try_catch try_catch;
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +0530203 /* param_value is the current parameter value for a test case. */
204 const void *param_value;
205 /* param_index stores the index of the parameter in parameterized tests. */
206 int param_index;
Brendan Higgins914cc632019-09-23 02:02:31 -0700207 /*
208 * success starts as true, and may only be set to false during a
209 * test case; thus, it is safe to update this across multiple
210 * threads using WRITE_ONCE; however, as a consequence, it may only
211 * be read after the test case finishes once all threads associated
212 * with the test case have terminated.
213 */
Brendan Higgins0a7568532019-09-23 02:02:32 -0700214 spinlock_t lock; /* Guards all mutable test state. */
David Gow6d2426b2021-06-24 23:58:12 -0700215 enum kunit_status status; /* Read only after test_case finishes! */
Brendan Higgins0a7568532019-09-23 02:02:32 -0700216 /*
217 * Because resources is a list that may be updated multiple times (with
218 * new resources) from any thread associated with a test case, we must
219 * protect it with some type of lock.
220 */
221 struct list_head resources; /* Protected by lock. */
David Gow6d2426b2021-06-24 23:58:12 -0700222
223 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
Brendan Higgins914cc632019-09-23 02:02:31 -0700224};
225
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -0700226static inline void kunit_set_failure(struct kunit *test)
227{
David Gow6d2426b2021-06-24 23:58:12 -0700228 WRITE_ONCE(test->status, KUNIT_FAILURE);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -0700229}
230
Alan Maguiree2219db2020-03-26 14:25:07 +0000231void kunit_init_test(struct kunit *test, const char *name, char *log);
Brendan Higgins914cc632019-09-23 02:02:31 -0700232
233int kunit_run_tests(struct kunit_suite *suite);
234
Alan Maguiree2219db2020-03-26 14:25:07 +0000235size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
236
237unsigned int kunit_test_case_num(struct kunit_suite *suite,
238 struct kunit_case *test_case);
239
Alan Maguireaac35462020-08-04 13:47:42 -0700240int __kunit_test_suites_init(struct kunit_suite * const * const suites);
Alan Maguiree2219db2020-03-26 14:25:07 +0000241
242void __kunit_test_suites_exit(struct kunit_suite **suites);
243
Brendan Higgins8c0d8842020-08-04 13:47:43 -0700244#if IS_BUILTIN(CONFIG_KUNIT)
245int kunit_run_all_tests(void);
246#else
247static inline int kunit_run_all_tests(void)
248{
249 return 0;
250}
251#endif /* IS_BUILTIN(CONFIG_KUNIT) */
252
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200253#ifdef MODULE
Brendan Higgins914cc632019-09-23 02:02:31 -0700254/**
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200255 * kunit_test_suites_for_module() - used to register one or more
256 * &struct kunit_suite with KUnit.
Brendan Higgins914cc632019-09-23 02:02:31 -0700257 *
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200258 * @__suites: a statically allocated list of &struct kunit_suite.
Brendan Higgins914cc632019-09-23 02:02:31 -0700259 *
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200260 * Registers @__suites with the test framework. See &struct kunit_suite for
Brendan Higgins914cc632019-09-23 02:02:31 -0700261 * more information.
262 *
Alan Maguireaac35462020-08-04 13:47:42 -0700263 * If a test suite is built-in, module_init() gets translated into
264 * an initcall which we don't want as the idea is that for builtins
265 * the executor will manage execution. So ensure we do not define
266 * module_{init|exit} functions for the builtin case when registering
267 * suites via kunit_test_suites() below.
Brendan Higgins914cc632019-09-23 02:02:31 -0700268 */
Alan Maguireaac35462020-08-04 13:47:42 -0700269#define kunit_test_suites_for_module(__suites) \
270 static int __init kunit_test_suites_init(void) \
Alan Maguirec475c772020-01-06 22:28:20 +0000271 { \
Alan Maguireaac35462020-08-04 13:47:42 -0700272 return __kunit_test_suites_init(__suites); \
Alan Maguirec475c772020-01-06 22:28:20 +0000273 } \
Alan Maguireaac35462020-08-04 13:47:42 -0700274 module_init(kunit_test_suites_init); \
275 \
Alan Maguirec475c772020-01-06 22:28:20 +0000276 static void __exit kunit_test_suites_exit(void) \
277 { \
Alan Maguireaac35462020-08-04 13:47:42 -0700278 return __kunit_test_suites_exit(__suites); \
Alan Maguirec475c772020-01-06 22:28:20 +0000279 } \
280 module_exit(kunit_test_suites_exit)
Alan Maguireaac35462020-08-04 13:47:42 -0700281#else
282#define kunit_test_suites_for_module(__suites)
283#endif /* MODULE */
284
285#define __kunit_test_suites(unique_array, unique_suites, ...) \
286 static struct kunit_suite *unique_array[] = { __VA_ARGS__, NULL }; \
287 kunit_test_suites_for_module(unique_array); \
288 static struct kunit_suite **unique_suites \
Joe Perches33def842020-10-21 19:36:07 -0700289 __used __section(".kunit_test_suites") = unique_array
Alan Maguireaac35462020-08-04 13:47:42 -0700290
291/**
292 * kunit_test_suites() - used to register one or more &struct kunit_suite
293 * with KUnit.
294 *
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200295 * @__suites: a statically allocated list of &struct kunit_suite.
Alan Maguireaac35462020-08-04 13:47:42 -0700296 *
297 * Registers @suites with the test framework. See &struct kunit_suite for
298 * more information.
299 *
300 * When builtin, KUnit tests are all run via executor; this is done
301 * by placing the array of struct kunit_suite * in the .kunit_test_suites
302 * ELF section.
303 *
304 * An alternative is to build the tests as a module. Because modules do not
305 * support multiple initcall()s, we need to initialize an array of suites for a
306 * module.
307 *
308 */
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200309#define kunit_test_suites(__suites...) \
Alan Maguireaac35462020-08-04 13:47:42 -0700310 __kunit_test_suites(__UNIQUE_ID(array), \
311 __UNIQUE_ID(suites), \
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200312 ##__suites)
Alan Maguirec475c772020-01-06 22:28:20 +0000313
314#define kunit_test_suite(suite) kunit_test_suites(&suite)
Brendan Higgins914cc632019-09-23 02:02:31 -0700315
Brendan Higgins9bf2eed2022-04-18 21:05:15 -0700316/**
317 * kunit_test_init_section_suites() - used to register one or more &struct
318 * kunit_suite containing init functions or
319 * init data.
320 *
321 * @__suites: a statically allocated list of &struct kunit_suite.
322 *
323 * This functions identically as &kunit_test_suites() except that it suppresses
324 * modpost warnings for referencing functions marked __init or data marked
325 * __initdata; this is OK because currently KUnit only runs tests upon boot
326 * during the init phase or upon loading a module during the init phase.
327 *
328 * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
329 * tests must be excluded.
330 *
331 * The only thing this macro does that's different from kunit_test_suites is
332 * that it suffixes the array and suite declarations it makes with _probe;
333 * modpost suppresses warnings about referencing init data for symbols named in
334 * this manner.
335 */
336#define kunit_test_init_section_suites(__suites...) \
337 __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
338 CONCATENATE(__UNIQUE_ID(suites), _probe), \
339 ##__suites)
340
341#define kunit_test_init_section_suite(suite) \
342 kunit_test_init_section_suites(&suite)
343
Alan Maguiree2219db2020-03-26 14:25:07 +0000344#define kunit_suite_for_each_test_case(suite, test_case) \
345 for (test_case = suite->test_cases; test_case->run_case; test_case++)
346
David Gow6d2426b2021-06-24 23:58:12 -0700347enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
Alan Maguiree2219db2020-03-26 14:25:07 +0000348
Alan Maguired4cdd142020-05-29 22:46:20 +0100349/**
Daniel Latypov7122deb2021-05-03 13:58:34 -0700350 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
351 * @test: The test context object.
352 * @n: number of elements.
353 * @size: The size in bytes of the desired memory.
354 * @gfp: flags passed to underlying kmalloc().
355 *
356 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
357 * and is automatically cleaned up after the test case concludes. See &struct
358 * kunit_resource for more information.
359 */
Daniel Latypov361b57d2021-10-05 13:46:32 -0700360void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
Daniel Latypov7122deb2021-05-03 13:58:34 -0700361
362/**
Brendan Higgins0a7568532019-09-23 02:02:32 -0700363 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
364 * @test: The test context object.
365 * @size: The size in bytes of the desired memory.
366 * @gfp: flags passed to underlying kmalloc().
367 *
Daniel Latypov7122deb2021-05-03 13:58:34 -0700368 * See kmalloc() and kunit_kmalloc_array() for more information.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700369 */
Daniel Latypov7122deb2021-05-03 13:58:34 -0700370static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
371{
372 return kunit_kmalloc_array(test, 1, size, gfp);
373}
Brendan Higgins0a7568532019-09-23 02:02:32 -0700374
375/**
376 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
377 * @test: The test case to which the resource belongs.
378 * @ptr: The memory allocation to free.
379 */
380void kunit_kfree(struct kunit *test, const void *ptr);
381
382/**
383 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
384 * @test: The test context object.
385 * @size: The size in bytes of the desired memory.
386 * @gfp: flags passed to underlying kmalloc().
387 *
Daniel Latypov7122deb2021-05-03 13:58:34 -0700388 * See kzalloc() and kunit_kmalloc_array() for more information.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700389 */
390static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
391{
392 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
393}
394
Daniel Latypov7122deb2021-05-03 13:58:34 -0700395/**
396 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
397 * @test: The test context object.
398 * @n: number of elements.
399 * @size: The size in bytes of the desired memory.
400 * @gfp: flags passed to underlying kmalloc().
401 *
402 * See kcalloc() and kunit_kmalloc_array() for more information.
403 */
Daniel Latypov361b57d2021-10-05 13:46:32 -0700404static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
Daniel Latypov7122deb2021-05-03 13:58:34 -0700405{
Daniel Latypov361b57d2021-10-05 13:46:32 -0700406 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
Daniel Latypov7122deb2021-05-03 13:58:34 -0700407}
408
Brendan Higgins0a7568532019-09-23 02:02:32 -0700409void kunit_cleanup(struct kunit *test);
410
David Gow44acdbb2021-05-13 13:03:50 -0700411void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
Alan Maguiree2219db2020-03-26 14:25:07 +0000412
David Gow6d2426b2021-06-24 23:58:12 -0700413/**
414 * kunit_mark_skipped() - Marks @test_or_suite as skipped
415 *
416 * @test_or_suite: The test context object.
417 * @fmt: A printk() style format string.
418 *
419 * Marks the test as skipped. @fmt is given output as the test status
420 * comment, typically the reason the test was skipped.
421 *
422 * Test execution continues after kunit_mark_skipped() is called.
423 */
424#define kunit_mark_skipped(test_or_suite, fmt, ...) \
425 do { \
426 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
427 scnprintf((test_or_suite)->status_comment, \
428 KUNIT_STATUS_COMMENT_SIZE, \
429 fmt, ##__VA_ARGS__); \
430 } while (0)
431
432/**
433 * kunit_skip() - Marks @test_or_suite as skipped
434 *
435 * @test_or_suite: The test context object.
436 * @fmt: A printk() style format string.
437 *
438 * Skips the test. @fmt is given output as the test status
439 * comment, typically the reason the test was skipped.
440 *
441 * Test execution is halted after kunit_skip() is called.
442 */
443#define kunit_skip(test_or_suite, fmt, ...) \
444 do { \
445 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
446 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
447 } while (0)
Alan Maguiree2219db2020-03-26 14:25:07 +0000448
449/*
450 * printk and log to per-test or per-suite log buffer. Logging only done
451 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
452 */
453#define kunit_log(lvl, test_or_suite, fmt, ...) \
454 do { \
455 printk(lvl fmt, ##__VA_ARGS__); \
456 kunit_log_append((test_or_suite)->log, fmt "\n", \
457 ##__VA_ARGS__); \
458 } while (0)
459
460#define kunit_printk(lvl, test, fmt, ...) \
Alan Maguirec3bba692020-03-26 14:25:09 +0000461 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
462 (test)->name, ##__VA_ARGS__)
Brendan Higgins914cc632019-09-23 02:02:31 -0700463
464/**
465 * kunit_info() - Prints an INFO level message associated with @test.
466 *
467 * @test: The test context object.
468 * @fmt: A printk() style format string.
469 *
470 * Prints an info level message associated with the test suite being run.
471 * Takes a variable number of format parameters just like printk().
472 */
473#define kunit_info(test, fmt, ...) \
474 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
475
476/**
477 * kunit_warn() - Prints a WARN level message associated with @test.
478 *
479 * @test: The test context object.
480 * @fmt: A printk() style format string.
481 *
482 * Prints a warning level message.
483 */
484#define kunit_warn(test, fmt, ...) \
485 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
486
487/**
488 * kunit_err() - Prints an ERROR level message associated with @test.
489 *
490 * @test: The test context object.
491 * @fmt: A printk() style format string.
492 *
493 * Prints an error level message.
494 */
495#define kunit_err(test, fmt, ...) \
496 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
497
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700498/**
499 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
500 * @test: The test context object.
501 *
502 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
503 * words, it does nothing and only exists for code clarity. See
504 * KUNIT_EXPECT_TRUE() for more information.
505 */
506#define KUNIT_SUCCEED(test) do {} while (0)
507
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800508void kunit_do_failed_assertion(struct kunit *test,
Daniel Latypov21957f92022-01-13 08:59:30 -0800509 const struct kunit_loc *loc,
510 enum kunit_assert_type type,
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800511 struct kunit_assert *assert,
512 const char *fmt, ...);
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700513
Daniel Latypov21957f92022-01-13 08:59:30 -0800514#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800515 if (unlikely(!(pass))) { \
Daniel Latypovc2741452022-01-27 13:52:22 -0800516 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800517 struct assert_class __assertion = INITIALIZER; \
518 kunit_do_failed_assertion(test, \
Daniel Latypovc2741452022-01-27 13:52:22 -0800519 &__loc, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800520 assert_type, \
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800521 &__assertion.assert, \
522 fmt, \
523 ##__VA_ARGS__); \
524 } \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700525} while (0)
526
527
528#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
529 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800530 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700531 false, \
532 kunit_fail_assert, \
Daniel Latypov05a7da82022-01-13 08:59:31 -0800533 KUNIT_INIT_FAIL_ASSERT_STRUCT, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700534 fmt, \
535 ##__VA_ARGS__)
536
537/**
538 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
539 * @test: The test context object.
540 * @fmt: an informational message to be printed when the assertion is made.
541 * @...: string format arguments.
542 *
543 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
544 * other words, it always results in a failed expectation, and consequently
545 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
546 * for more information.
547 */
548#define KUNIT_FAIL(test, fmt, ...) \
549 KUNIT_FAIL_ASSERTION(test, \
550 KUNIT_EXPECTATION, \
551 fmt, \
552 ##__VA_ARGS__)
553
554#define KUNIT_UNARY_ASSERTION(test, \
555 assert_type, \
556 condition, \
557 expected_true, \
558 fmt, \
559 ...) \
560 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800561 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700562 !!(condition) == !!expected_true, \
563 kunit_unary_assert, \
Daniel Latypov05a7da82022-01-13 08:59:31 -0800564 KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700565 expected_true), \
566 fmt, \
567 ##__VA_ARGS__)
568
569#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
570 KUNIT_UNARY_ASSERTION(test, \
571 assert_type, \
572 condition, \
573 true, \
574 fmt, \
575 ##__VA_ARGS__)
576
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700577#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
578 KUNIT_UNARY_ASSERTION(test, \
579 assert_type, \
580 condition, \
581 false, \
582 fmt, \
583 ##__VA_ARGS__)
584
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700585/*
586 * A factory macro for defining the assertions and expectations for the basic
587 * comparisons defined for the built in types.
588 *
589 * Unfortunately, there is no common type that all types can be promoted to for
590 * which all the binary operators behave the same way as for the actual types
591 * (for example, there is no type that long long and unsigned long long can
592 * both be cast to where the comparison result is preserved for all values). So
593 * the best we can do is do the comparison in the original types and then coerce
594 * everything to long long for printing; this way, the comparison behaves
595 * correctly and the printed out value usually makes sense without
596 * interpretation, but can always be interpreted to figure out the actual
597 * value.
598 */
599#define KUNIT_BASE_BINARY_ASSERTION(test, \
600 assert_class, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800601 format_func, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700602 assert_type, \
603 left, \
604 op, \
605 right, \
606 fmt, \
607 ...) \
608do { \
Daniel Latypovc2741452022-01-27 13:52:22 -0800609 const typeof(left) __left = (left); \
610 const typeof(right) __right = (right); \
Daniel Latypov2b6861e2022-01-25 13:00:11 -0800611 static const struct kunit_binary_assert_text __text = { \
612 .operation = #op, \
613 .left_text = #left, \
614 .right_text = #right, \
615 }; \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700616 \
617 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800618 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700619 __left op __right, \
620 assert_class, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800621 KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \
Daniel Latypov2b6861e2022-01-25 13:00:11 -0800622 &__text, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800623 __left, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800624 __right), \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700625 fmt, \
626 ##__VA_ARGS__); \
627} while (0)
628
Daniel Latypov40f397772022-01-18 14:35:05 -0800629#define KUNIT_BINARY_INT_ASSERTION(test, \
630 assert_type, \
631 left, \
632 op, \
633 right, \
634 fmt, \
635 ...) \
636 KUNIT_BASE_BINARY_ASSERTION(test, \
637 kunit_binary_assert, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800638 kunit_binary_assert_format, \
Daniel Latypov40f397772022-01-18 14:35:05 -0800639 assert_type, \
640 left, op, right, \
641 fmt, \
642 ##__VA_ARGS__)
643
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800644#define KUNIT_BINARY_PTR_ASSERTION(test, \
645 assert_type, \
646 left, \
647 op, \
648 right, \
649 fmt, \
650 ...) \
651 KUNIT_BASE_BINARY_ASSERTION(test, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700652 kunit_binary_ptr_assert, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800653 kunit_binary_ptr_assert_format, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700654 assert_type, \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800655 left, op, right, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700656 fmt, \
657 ##__VA_ARGS__)
658
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700659#define KUNIT_BINARY_STR_ASSERTION(test, \
660 assert_type, \
661 left, \
662 op, \
663 right, \
664 fmt, \
665 ...) \
666do { \
David Gow3747b5c2021-05-13 12:31:56 -0700667 const char *__left = (left); \
Daniel Latypov2b6861e2022-01-25 13:00:11 -0800668 const char *__right = (right); \
669 static const struct kunit_binary_assert_text __text = { \
670 .operation = #op, \
671 .left_text = #left, \
672 .right_text = #right, \
673 }; \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700674 \
675 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800676 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700677 strcmp(__left, __right) op 0, \
678 kunit_binary_str_assert, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800679 KUNIT_INIT_BINARY_ASSERT_STRUCT(kunit_binary_str_assert_format,\
Daniel Latypov2b6861e2022-01-25 13:00:11 -0800680 &__text, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700681 __left, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700682 __right), \
683 fmt, \
684 ##__VA_ARGS__); \
685} while (0)
686
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700687#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
688 assert_type, \
689 ptr, \
690 fmt, \
691 ...) \
692do { \
Daniel Latypovc2741452022-01-27 13:52:22 -0800693 const typeof(ptr) __ptr = (ptr); \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700694 \
695 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800696 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700697 !IS_ERR_OR_NULL(__ptr), \
698 kunit_ptr_not_err_assert, \
Daniel Latypov05a7da82022-01-13 08:59:31 -0800699 KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700700 __ptr), \
701 fmt, \
702 ##__VA_ARGS__); \
703} while (0)
704
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700705/**
706 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
707 * @test: The test context object.
708 * @condition: an arbitrary boolean expression. The test fails when this does
709 * not evaluate to true.
710 *
711 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
712 * to fail when the specified condition is not met; however, it will not prevent
713 * the test case from continuing to run; this is otherwise known as an
714 * *expectation failure*.
715 */
716#define KUNIT_EXPECT_TRUE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800717 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700718
719#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
720 KUNIT_TRUE_MSG_ASSERTION(test, \
721 KUNIT_EXPECTATION, \
722 condition, \
723 fmt, \
724 ##__VA_ARGS__)
725
726/**
727 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
728 * @test: The test context object.
729 * @condition: an arbitrary boolean expression. The test fails when this does
730 * not evaluate to false.
731 *
732 * Sets an expectation that @condition evaluates to false. See
733 * KUNIT_EXPECT_TRUE() for more information.
734 */
735#define KUNIT_EXPECT_FALSE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800736 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700737
738#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
739 KUNIT_FALSE_MSG_ASSERTION(test, \
740 KUNIT_EXPECTATION, \
741 condition, \
742 fmt, \
743 ##__VA_ARGS__)
744
745/**
746 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
747 * @test: The test context object.
748 * @left: an arbitrary expression that evaluates to a primitive C type.
749 * @right: an arbitrary expression that evaluates to a primitive C type.
750 *
751 * Sets an expectation that the values that @left and @right evaluate to are
752 * equal. This is semantically equivalent to
753 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
754 * more information.
755 */
756#define KUNIT_EXPECT_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800757 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700758
759#define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800760 KUNIT_BINARY_INT_ASSERTION(test, \
761 KUNIT_EXPECTATION, \
762 left, ==, right, \
763 fmt, \
764 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700765
766/**
767 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
768 * @test: The test context object.
769 * @left: an arbitrary expression that evaluates to a pointer.
770 * @right: an arbitrary expression that evaluates to a pointer.
771 *
772 * Sets an expectation that the values that @left and @right evaluate to are
773 * equal. This is semantically equivalent to
774 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
775 * more information.
776 */
777#define KUNIT_EXPECT_PTR_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800778 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700779
780#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800781 KUNIT_BINARY_PTR_ASSERTION(test, \
782 KUNIT_EXPECTATION, \
783 left, ==, right, \
784 fmt, \
785 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700786
787/**
788 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
789 * @test: The test context object.
790 * @left: an arbitrary expression that evaluates to a primitive C type.
791 * @right: an arbitrary expression that evaluates to a primitive C type.
792 *
793 * Sets an expectation that the values that @left and @right evaluate to are not
794 * equal. This is semantically equivalent to
795 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
796 * more information.
797 */
798#define KUNIT_EXPECT_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800799 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700800
801#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800802 KUNIT_BINARY_INT_ASSERTION(test, \
803 KUNIT_EXPECTATION, \
804 left, !=, right, \
805 fmt, \
806 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700807
808/**
809 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
810 * @test: The test context object.
811 * @left: an arbitrary expression that evaluates to a pointer.
812 * @right: an arbitrary expression that evaluates to a pointer.
813 *
814 * Sets an expectation that the values that @left and @right evaluate to are not
815 * equal. This is semantically equivalent to
816 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
817 * more information.
818 */
819#define KUNIT_EXPECT_PTR_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800820 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700821
822#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800823 KUNIT_BINARY_PTR_ASSERTION(test, \
824 KUNIT_EXPECTATION, \
825 left, !=, right, \
826 fmt, \
827 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700828
829/**
830 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
831 * @test: The test context object.
832 * @left: an arbitrary expression that evaluates to a primitive C type.
833 * @right: an arbitrary expression that evaluates to a primitive C type.
834 *
835 * Sets an expectation that the value that @left evaluates to is less than the
836 * value that @right evaluates to. This is semantically equivalent to
837 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
838 * more information.
839 */
840#define KUNIT_EXPECT_LT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800841 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700842
843#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800844 KUNIT_BINARY_INT_ASSERTION(test, \
845 KUNIT_EXPECTATION, \
846 left, <, right, \
847 fmt, \
848 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700849
850/**
851 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
852 * @test: The test context object.
853 * @left: an arbitrary expression that evaluates to a primitive C type.
854 * @right: an arbitrary expression that evaluates to a primitive C type.
855 *
856 * Sets an expectation that the value that @left evaluates to is less than or
857 * equal to the value that @right evaluates to. Semantically this is equivalent
858 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
859 * more information.
860 */
861#define KUNIT_EXPECT_LE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800862 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700863
864#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800865 KUNIT_BINARY_INT_ASSERTION(test, \
866 KUNIT_ASSERTION, \
867 left, <=, right, \
868 fmt, \
869 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700870
871/**
872 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
873 * @test: The test context object.
874 * @left: an arbitrary expression that evaluates to a primitive C type.
875 * @right: an arbitrary expression that evaluates to a primitive C type.
876 *
877 * Sets an expectation that the value that @left evaluates to is greater than
878 * the value that @right evaluates to. This is semantically equivalent to
879 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
880 * more information.
881 */
882#define KUNIT_EXPECT_GT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800883 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700884
885#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800886 KUNIT_BINARY_INT_ASSERTION(test, \
887 KUNIT_EXPECTATION, \
888 left, >, right, \
889 fmt, \
890 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700891
892/**
893 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
894 * @test: The test context object.
895 * @left: an arbitrary expression that evaluates to a primitive C type.
896 * @right: an arbitrary expression that evaluates to a primitive C type.
897 *
898 * Sets an expectation that the value that @left evaluates to is greater than
899 * the value that @right evaluates to. This is semantically equivalent to
900 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
901 * more information.
902 */
903#define KUNIT_EXPECT_GE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800904 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700905
906#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800907 KUNIT_BINARY_INT_ASSERTION(test, \
908 KUNIT_EXPECTATION, \
909 left, >=, right, \
910 fmt, \
911 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700912
913/**
914 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
915 * @test: The test context object.
916 * @left: an arbitrary expression that evaluates to a null terminated string.
917 * @right: an arbitrary expression that evaluates to a null terminated string.
918 *
919 * Sets an expectation that the values that @left and @right evaluate to are
920 * equal. This is semantically equivalent to
921 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
922 * for more information.
923 */
924#define KUNIT_EXPECT_STREQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800925 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700926
927#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -0800928 KUNIT_BINARY_STR_ASSERTION(test, \
929 KUNIT_EXPECTATION, \
930 left, ==, right, \
931 fmt, \
932 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700933
934/**
935 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
936 * @test: The test context object.
937 * @left: an arbitrary expression that evaluates to a null terminated string.
938 * @right: an arbitrary expression that evaluates to a null terminated string.
939 *
940 * Sets an expectation that the values that @left and @right evaluate to are
941 * not equal. This is semantically equivalent to
942 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
943 * for more information.
944 */
945#define KUNIT_EXPECT_STRNEQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800946 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700947
948#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -0800949 KUNIT_BINARY_STR_ASSERTION(test, \
950 KUNIT_EXPECTATION, \
951 left, !=, right, \
952 fmt, \
953 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700954
955/**
Ricardo Ribaldacaae9452022-02-11 17:42:41 +0100956 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
957 * @test: The test context object.
958 * @ptr: an arbitrary pointer.
959 *
960 * Sets an expectation that the value that @ptr evaluates to is null. This is
961 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
962 * See KUNIT_EXPECT_TRUE() for more information.
963 */
964#define KUNIT_EXPECT_NULL(test, ptr) \
965 KUNIT_EXPECT_NULL_MSG(test, \
966 ptr, \
967 NULL)
968
969#define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
970 KUNIT_BINARY_PTR_ASSERTION(test, \
971 KUNIT_EXPECTATION, \
972 ptr, ==, NULL, \
973 fmt, \
974 ##__VA_ARGS__)
975
976/**
977 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
978 * @test: The test context object.
979 * @ptr: an arbitrary pointer.
980 *
981 * Sets an expectation that the value that @ptr evaluates to is not null. This
982 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
983 * See KUNIT_EXPECT_TRUE() for more information.
984 */
985#define KUNIT_EXPECT_NOT_NULL(test, ptr) \
986 KUNIT_EXPECT_NOT_NULL_MSG(test, \
987 ptr, \
988 NULL)
989
990#define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
991 KUNIT_BINARY_PTR_ASSERTION(test, \
992 KUNIT_EXPECTATION, \
993 ptr, !=, NULL, \
994 fmt, \
995 ##__VA_ARGS__)
996
997/**
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700998 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
999 * @test: The test context object.
1000 * @ptr: an arbitrary pointer.
1001 *
1002 * Sets an expectation that the value that @ptr evaluates to is not null and not
1003 * an errno stored in a pointer. This is semantically equivalent to
1004 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1005 * more information.
1006 */
1007#define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001008 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001009
1010#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1011 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1012 KUNIT_EXPECTATION, \
1013 ptr, \
1014 fmt, \
1015 ##__VA_ARGS__)
1016
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001017#define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1018 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1019
1020/**
1021 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1022 * @test: The test context object.
1023 * @condition: an arbitrary boolean expression. The test fails and aborts when
1024 * this does not evaluate to true.
1025 *
1026 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1027 * fail *and immediately abort* when the specified condition is not met. Unlike
1028 * an expectation failure, it will prevent the test case from continuing to run;
1029 * this is otherwise known as an *assertion failure*.
1030 */
1031#define KUNIT_ASSERT_TRUE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001032 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001033
1034#define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1035 KUNIT_TRUE_MSG_ASSERTION(test, \
1036 KUNIT_ASSERTION, \
1037 condition, \
1038 fmt, \
1039 ##__VA_ARGS__)
1040
1041/**
1042 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1043 * @test: The test context object.
1044 * @condition: an arbitrary boolean expression.
1045 *
1046 * Sets an assertion that the value that @condition evaluates to is false. This
1047 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1048 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1049 */
1050#define KUNIT_ASSERT_FALSE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001051 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001052
1053#define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1054 KUNIT_FALSE_MSG_ASSERTION(test, \
1055 KUNIT_ASSERTION, \
1056 condition, \
1057 fmt, \
1058 ##__VA_ARGS__)
1059
1060/**
1061 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1062 * @test: The test context object.
1063 * @left: an arbitrary expression that evaluates to a primitive C type.
1064 * @right: an arbitrary expression that evaluates to a primitive C type.
1065 *
1066 * Sets an assertion that the values that @left and @right evaluate to are
1067 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1068 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1069 */
1070#define KUNIT_ASSERT_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001071 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001072
1073#define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001074 KUNIT_BINARY_INT_ASSERTION(test, \
1075 KUNIT_ASSERTION, \
1076 left, ==, right, \
1077 fmt, \
1078 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001079
1080/**
1081 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1082 * @test: The test context object.
1083 * @left: an arbitrary expression that evaluates to a pointer.
1084 * @right: an arbitrary expression that evaluates to a pointer.
1085 *
1086 * Sets an assertion that the values that @left and @right evaluate to are
1087 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1088 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1089 */
1090#define KUNIT_ASSERT_PTR_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001091 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001092
1093#define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001094 KUNIT_BINARY_PTR_ASSERTION(test, \
1095 KUNIT_ASSERTION, \
1096 left, ==, right, \
1097 fmt, \
1098 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001099
1100/**
1101 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1102 * @test: The test context object.
1103 * @left: an arbitrary expression that evaluates to a primitive C type.
1104 * @right: an arbitrary expression that evaluates to a primitive C type.
1105 *
1106 * Sets an assertion that the values that @left and @right evaluate to are not
1107 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1108 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1109 */
1110#define KUNIT_ASSERT_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001111 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001112
1113#define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001114 KUNIT_BINARY_INT_ASSERTION(test, \
1115 KUNIT_ASSERTION, \
1116 left, !=, right, \
1117 fmt, \
1118 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001119
1120/**
1121 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1122 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1123 * @test: The test context object.
1124 * @left: an arbitrary expression that evaluates to a pointer.
1125 * @right: an arbitrary expression that evaluates to a pointer.
1126 *
1127 * Sets an assertion that the values that @left and @right evaluate to are not
1128 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1129 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1130 */
1131#define KUNIT_ASSERT_PTR_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001132 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001133
1134#define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001135 KUNIT_BINARY_PTR_ASSERTION(test, \
1136 KUNIT_ASSERTION, \
1137 left, !=, right, \
1138 fmt, \
1139 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001140/**
1141 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1142 * @test: The test context object.
1143 * @left: an arbitrary expression that evaluates to a primitive C type.
1144 * @right: an arbitrary expression that evaluates to a primitive C type.
1145 *
1146 * Sets an assertion that the value that @left evaluates to is less than the
1147 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1148 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1149 * is not met.
1150 */
1151#define KUNIT_ASSERT_LT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001152 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001153
1154#define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001155 KUNIT_BINARY_INT_ASSERTION(test, \
1156 KUNIT_EXPECTATION, \
1157 left, <, right, \
1158 fmt, \
1159 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001160/**
1161 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1162 * @test: The test context object.
1163 * @left: an arbitrary expression that evaluates to a primitive C type.
1164 * @right: an arbitrary expression that evaluates to a primitive C type.
1165 *
1166 * Sets an assertion that the value that @left evaluates to is less than or
1167 * equal to the value that @right evaluates to. This is the same as
1168 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1169 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1170 */
1171#define KUNIT_ASSERT_LE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001172 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001173
1174#define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001175 KUNIT_BINARY_INT_ASSERTION(test, \
1176 KUNIT_ASSERTION, \
1177 left, <=, right, \
1178 fmt, \
1179 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001180
1181/**
1182 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1183 * @test: The test context object.
1184 * @left: an arbitrary expression that evaluates to a primitive C type.
1185 * @right: an arbitrary expression that evaluates to a primitive C type.
1186 *
1187 * Sets an assertion that the value that @left evaluates to is greater than the
1188 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1189 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1190 * is not met.
1191 */
1192#define KUNIT_ASSERT_GT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001193 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001194
1195#define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001196 KUNIT_BINARY_INT_ASSERTION(test, \
1197 KUNIT_EXPECTATION, \
1198 left, >, right, \
1199 fmt, \
1200 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001201
1202/**
1203 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1204 * @test: The test context object.
1205 * @left: an arbitrary expression that evaluates to a primitive C type.
1206 * @right: an arbitrary expression that evaluates to a primitive C type.
1207 *
1208 * Sets an assertion that the value that @left evaluates to is greater than the
1209 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1210 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1211 * is not met.
1212 */
1213#define KUNIT_ASSERT_GE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001214 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001215
1216#define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001217 KUNIT_BINARY_INT_ASSERTION(test, \
1218 KUNIT_ASSERTION, \
1219 left, >=, right, \
1220 fmt, \
1221 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001222
1223/**
1224 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1225 * @test: The test context object.
1226 * @left: an arbitrary expression that evaluates to a null terminated string.
1227 * @right: an arbitrary expression that evaluates to a null terminated string.
1228 *
1229 * Sets an assertion that the values that @left and @right evaluate to are
1230 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1231 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1232 */
1233#define KUNIT_ASSERT_STREQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001234 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001235
1236#define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -08001237 KUNIT_BINARY_STR_ASSERTION(test, \
1238 KUNIT_ASSERTION, \
1239 left, ==, right, \
1240 fmt, \
1241 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001242
1243/**
1244 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1245 * @test: The test context object.
1246 * @left: an arbitrary expression that evaluates to a null terminated string.
1247 * @right: an arbitrary expression that evaluates to a null terminated string.
1248 *
1249 * Sets an expectation that the values that @left and @right evaluate to are
1250 * not equal. This is semantically equivalent to
1251 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1252 * for more information.
1253 */
1254#define KUNIT_ASSERT_STRNEQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001255 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001256
1257#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -08001258 KUNIT_BINARY_STR_ASSERTION(test, \
1259 KUNIT_ASSERTION, \
1260 left, !=, right, \
1261 fmt, \
1262 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001263
1264/**
Ricardo Ribaldacaae9452022-02-11 17:42:41 +01001265 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1266 * @test: The test context object.
1267 * @ptr: an arbitrary pointer.
1268 *
1269 * Sets an assertion that the values that @ptr evaluates to is null. This is
1270 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1271 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1272 */
1273#define KUNIT_ASSERT_NULL(test, ptr) \
1274 KUNIT_ASSERT_NULL_MSG(test, \
1275 ptr, \
1276 NULL)
1277
1278#define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1279 KUNIT_BINARY_PTR_ASSERTION(test, \
1280 KUNIT_ASSERTION, \
1281 ptr, ==, NULL, \
1282 fmt, \
1283 ##__VA_ARGS__)
1284
1285/**
1286 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1287 * @test: The test context object.
1288 * @ptr: an arbitrary pointer.
1289 *
1290 * Sets an assertion that the values that @ptr evaluates to is not null. This
1291 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1292 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1293 */
1294#define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1295 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1296 ptr, \
1297 NULL)
1298
1299#define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1300 KUNIT_BINARY_PTR_ASSERTION(test, \
1301 KUNIT_ASSERTION, \
1302 ptr, !=, NULL, \
1303 fmt, \
1304 ##__VA_ARGS__)
1305
1306/**
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001307 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1308 * @test: The test context object.
1309 * @ptr: an arbitrary pointer.
1310 *
1311 * Sets an assertion that the value that @ptr evaluates to is not null and not
1312 * an errno stored in a pointer. This is the same as
1313 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1314 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1315 */
1316#define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001317 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001318
1319#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1320 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1321 KUNIT_ASSERTION, \
1322 ptr, \
1323 fmt, \
1324 ##__VA_ARGS__)
1325
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +05301326/**
1327 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1328 * @name: prefix for the test parameter generator function.
1329 * @array: array of test parameters.
1330 * @get_desc: function to convert param to description; NULL to use default
1331 *
1332 * Define function @name_gen_params which uses @array to generate parameters.
1333 */
1334#define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1335 static const void *name##_gen_params(const void *prev, char *desc) \
1336 { \
1337 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1338 if (__next - (array) < ARRAY_SIZE((array))) { \
1339 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1340 if (__get_desc) \
1341 __get_desc(__next, desc); \
1342 return __next; \
1343 } \
1344 return NULL; \
1345 }
1346
Daniel Latypov61695f82022-03-28 10:41:42 -07001347// TODO([email protected]): consider eventually migrating users to explicitly
1348// include resource.h themselves if they need it.
1349#include <kunit/resource.h>
1350
Brendan Higgins914cc632019-09-23 02:02:31 -07001351#endif /* _KUNIT_TEST_H */