blob: 7646d1bcf6857da48ee29ab14aabea7b79420962 [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
30struct 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 } \
David Gowc2726122022-07-01 16:47:43 +0800280 module_exit(kunit_test_suites_exit) \
281 MODULE_INFO(test, "Y");
Alan Maguireaac35462020-08-04 13:47:42 -0700282#else
283#define kunit_test_suites_for_module(__suites)
284#endif /* MODULE */
285
286#define __kunit_test_suites(unique_array, unique_suites, ...) \
287 static struct kunit_suite *unique_array[] = { __VA_ARGS__, NULL }; \
288 kunit_test_suites_for_module(unique_array); \
289 static struct kunit_suite **unique_suites \
Joe Perches33def842020-10-21 19:36:07 -0700290 __used __section(".kunit_test_suites") = unique_array
Alan Maguireaac35462020-08-04 13:47:42 -0700291
292/**
293 * kunit_test_suites() - used to register one or more &struct kunit_suite
294 * with KUnit.
295 *
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200296 * @__suites: a statically allocated list of &struct kunit_suite.
Alan Maguireaac35462020-08-04 13:47:42 -0700297 *
298 * Registers @suites with the test framework. See &struct kunit_suite for
299 * more information.
300 *
301 * When builtin, KUnit tests are all run via executor; this is done
302 * by placing the array of struct kunit_suite * in the .kunit_test_suites
303 * ELF section.
304 *
305 * An alternative is to build the tests as a module. Because modules do not
306 * support multiple initcall()s, we need to initialize an array of suites for a
307 * module.
308 *
309 */
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200310#define kunit_test_suites(__suites...) \
Alan Maguireaac35462020-08-04 13:47:42 -0700311 __kunit_test_suites(__UNIQUE_ID(array), \
312 __UNIQUE_ID(suites), \
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200313 ##__suites)
Alan Maguirec475c772020-01-06 22:28:20 +0000314
315#define kunit_test_suite(suite) kunit_test_suites(&suite)
Brendan Higgins914cc632019-09-23 02:02:31 -0700316
Brendan Higgins9bf2eed2022-04-18 21:05:15 -0700317/**
318 * kunit_test_init_section_suites() - used to register one or more &struct
319 * kunit_suite containing init functions or
320 * init data.
321 *
322 * @__suites: a statically allocated list of &struct kunit_suite.
323 *
Mauro Carvalho Chehab7b237942022-07-02 12:07:40 +0100324 * This functions identically as kunit_test_suites() except that it suppresses
Brendan Higgins9bf2eed2022-04-18 21:05:15 -0700325 * modpost warnings for referencing functions marked __init or data marked
326 * __initdata; this is OK because currently KUnit only runs tests upon boot
327 * during the init phase or upon loading a module during the init phase.
328 *
329 * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
330 * tests must be excluded.
331 *
332 * The only thing this macro does that's different from kunit_test_suites is
333 * that it suffixes the array and suite declarations it makes with _probe;
334 * modpost suppresses warnings about referencing init data for symbols named in
335 * this manner.
336 */
337#define kunit_test_init_section_suites(__suites...) \
338 __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
339 CONCATENATE(__UNIQUE_ID(suites), _probe), \
340 ##__suites)
341
342#define kunit_test_init_section_suite(suite) \
343 kunit_test_init_section_suites(&suite)
344
Alan Maguiree2219db2020-03-26 14:25:07 +0000345#define kunit_suite_for_each_test_case(suite, test_case) \
346 for (test_case = suite->test_cases; test_case->run_case; test_case++)
347
David Gow6d2426b2021-06-24 23:58:12 -0700348enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
Alan Maguiree2219db2020-03-26 14:25:07 +0000349
Alan Maguired4cdd142020-05-29 22:46:20 +0100350/**
Daniel Latypov7122deb2021-05-03 13:58:34 -0700351 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
352 * @test: The test context object.
353 * @n: number of elements.
354 * @size: The size in bytes of the desired memory.
355 * @gfp: flags passed to underlying kmalloc().
356 *
357 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
358 * and is automatically cleaned up after the test case concludes. See &struct
359 * kunit_resource for more information.
360 */
Daniel Latypov361b57d2021-10-05 13:46:32 -0700361void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
Daniel Latypov7122deb2021-05-03 13:58:34 -0700362
363/**
Brendan Higgins0a7568532019-09-23 02:02:32 -0700364 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
365 * @test: The test context object.
366 * @size: The size in bytes of the desired memory.
367 * @gfp: flags passed to underlying kmalloc().
368 *
Daniel Latypov7122deb2021-05-03 13:58:34 -0700369 * See kmalloc() and kunit_kmalloc_array() for more information.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700370 */
Daniel Latypov7122deb2021-05-03 13:58:34 -0700371static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
372{
373 return kunit_kmalloc_array(test, 1, size, gfp);
374}
Brendan Higgins0a7568532019-09-23 02:02:32 -0700375
376/**
377 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
378 * @test: The test case to which the resource belongs.
379 * @ptr: The memory allocation to free.
380 */
381void kunit_kfree(struct kunit *test, const void *ptr);
382
383/**
384 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
385 * @test: The test context object.
386 * @size: The size in bytes of the desired memory.
387 * @gfp: flags passed to underlying kmalloc().
388 *
Daniel Latypov7122deb2021-05-03 13:58:34 -0700389 * See kzalloc() and kunit_kmalloc_array() for more information.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700390 */
391static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
392{
393 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
394}
395
Daniel Latypov7122deb2021-05-03 13:58:34 -0700396/**
397 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
398 * @test: The test context object.
399 * @n: number of elements.
400 * @size: The size in bytes of the desired memory.
401 * @gfp: flags passed to underlying kmalloc().
402 *
403 * See kcalloc() and kunit_kmalloc_array() for more information.
404 */
Daniel Latypov361b57d2021-10-05 13:46:32 -0700405static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
Daniel Latypov7122deb2021-05-03 13:58:34 -0700406{
Daniel Latypov361b57d2021-10-05 13:46:32 -0700407 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
Daniel Latypov7122deb2021-05-03 13:58:34 -0700408}
409
Brendan Higgins0a7568532019-09-23 02:02:32 -0700410void kunit_cleanup(struct kunit *test);
411
David Gow44acdbb2021-05-13 13:03:50 -0700412void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
Alan Maguiree2219db2020-03-26 14:25:07 +0000413
David Gow6d2426b2021-06-24 23:58:12 -0700414/**
415 * kunit_mark_skipped() - Marks @test_or_suite as skipped
416 *
417 * @test_or_suite: The test context object.
418 * @fmt: A printk() style format string.
419 *
420 * Marks the test as skipped. @fmt is given output as the test status
421 * comment, typically the reason the test was skipped.
422 *
423 * Test execution continues after kunit_mark_skipped() is called.
424 */
425#define kunit_mark_skipped(test_or_suite, fmt, ...) \
426 do { \
427 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
428 scnprintf((test_or_suite)->status_comment, \
429 KUNIT_STATUS_COMMENT_SIZE, \
430 fmt, ##__VA_ARGS__); \
431 } while (0)
432
433/**
434 * kunit_skip() - Marks @test_or_suite as skipped
435 *
436 * @test_or_suite: The test context object.
437 * @fmt: A printk() style format string.
438 *
439 * Skips the test. @fmt is given output as the test status
440 * comment, typically the reason the test was skipped.
441 *
442 * Test execution is halted after kunit_skip() is called.
443 */
444#define kunit_skip(test_or_suite, fmt, ...) \
445 do { \
446 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
447 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
448 } while (0)
Alan Maguiree2219db2020-03-26 14:25:07 +0000449
450/*
451 * printk and log to per-test or per-suite log buffer. Logging only done
452 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
453 */
454#define kunit_log(lvl, test_or_suite, fmt, ...) \
455 do { \
456 printk(lvl fmt, ##__VA_ARGS__); \
457 kunit_log_append((test_or_suite)->log, fmt "\n", \
458 ##__VA_ARGS__); \
459 } while (0)
460
461#define kunit_printk(lvl, test, fmt, ...) \
Alan Maguirec3bba692020-03-26 14:25:09 +0000462 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
463 (test)->name, ##__VA_ARGS__)
Brendan Higgins914cc632019-09-23 02:02:31 -0700464
465/**
466 * kunit_info() - Prints an INFO level message associated with @test.
467 *
468 * @test: The test context object.
469 * @fmt: A printk() style format string.
470 *
471 * Prints an info level message associated with the test suite being run.
472 * Takes a variable number of format parameters just like printk().
473 */
474#define kunit_info(test, fmt, ...) \
475 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
476
477/**
478 * kunit_warn() - Prints a WARN level message associated with @test.
479 *
480 * @test: The test context object.
481 * @fmt: A printk() style format string.
482 *
483 * Prints a warning level message.
484 */
485#define kunit_warn(test, fmt, ...) \
486 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
487
488/**
489 * kunit_err() - Prints an ERROR level message associated with @test.
490 *
491 * @test: The test context object.
492 * @fmt: A printk() style format string.
493 *
494 * Prints an error level message.
495 */
496#define kunit_err(test, fmt, ...) \
497 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
498
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700499/**
500 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
501 * @test: The test context object.
502 *
503 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
504 * words, it does nothing and only exists for code clarity. See
505 * KUNIT_EXPECT_TRUE() for more information.
506 */
507#define KUNIT_SUCCEED(test) do {} while (0)
508
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800509void kunit_do_failed_assertion(struct kunit *test,
Daniel Latypov21957f92022-01-13 08:59:30 -0800510 const struct kunit_loc *loc,
511 enum kunit_assert_type type,
Miguel Ojeda74668862022-05-02 11:36:25 +0200512 const struct kunit_assert *assert,
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800513 const char *fmt, ...);
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700514
Daniel Latypov21957f92022-01-13 08:59:30 -0800515#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800516 if (unlikely(!(pass))) { \
Daniel Latypovc2741452022-01-27 13:52:22 -0800517 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800518 struct assert_class __assertion = INITIALIZER; \
519 kunit_do_failed_assertion(test, \
Daniel Latypovc2741452022-01-27 13:52:22 -0800520 &__loc, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800521 assert_type, \
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800522 &__assertion.assert, \
523 fmt, \
524 ##__VA_ARGS__); \
525 } \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700526} while (0)
527
528
529#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
530 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800531 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700532 false, \
533 kunit_fail_assert, \
Daniel Latypov05a7da82022-01-13 08:59:31 -0800534 KUNIT_INIT_FAIL_ASSERT_STRUCT, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700535 fmt, \
536 ##__VA_ARGS__)
537
538/**
539 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
540 * @test: The test context object.
541 * @fmt: an informational message to be printed when the assertion is made.
542 * @...: string format arguments.
543 *
544 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
545 * other words, it always results in a failed expectation, and consequently
546 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
547 * for more information.
548 */
549#define KUNIT_FAIL(test, fmt, ...) \
550 KUNIT_FAIL_ASSERTION(test, \
551 KUNIT_EXPECTATION, \
552 fmt, \
553 ##__VA_ARGS__)
554
555#define KUNIT_UNARY_ASSERTION(test, \
556 assert_type, \
557 condition, \
558 expected_true, \
559 fmt, \
560 ...) \
561 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800562 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700563 !!(condition) == !!expected_true, \
564 kunit_unary_assert, \
Daniel Latypov05a7da82022-01-13 08:59:31 -0800565 KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700566 expected_true), \
567 fmt, \
568 ##__VA_ARGS__)
569
570#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
571 KUNIT_UNARY_ASSERTION(test, \
572 assert_type, \
573 condition, \
574 true, \
575 fmt, \
576 ##__VA_ARGS__)
577
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700578#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
579 KUNIT_UNARY_ASSERTION(test, \
580 assert_type, \
581 condition, \
582 false, \
583 fmt, \
584 ##__VA_ARGS__)
585
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700586/*
587 * A factory macro for defining the assertions and expectations for the basic
588 * comparisons defined for the built in types.
589 *
590 * Unfortunately, there is no common type that all types can be promoted to for
591 * which all the binary operators behave the same way as for the actual types
592 * (for example, there is no type that long long and unsigned long long can
593 * both be cast to where the comparison result is preserved for all values). So
594 * the best we can do is do the comparison in the original types and then coerce
595 * everything to long long for printing; this way, the comparison behaves
596 * correctly and the printed out value usually makes sense without
597 * interpretation, but can always be interpreted to figure out the actual
598 * value.
599 */
600#define KUNIT_BASE_BINARY_ASSERTION(test, \
601 assert_class, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800602 format_func, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700603 assert_type, \
604 left, \
605 op, \
606 right, \
607 fmt, \
608 ...) \
609do { \
Daniel Latypovc2741452022-01-27 13:52:22 -0800610 const typeof(left) __left = (left); \
611 const typeof(right) __right = (right); \
Daniel Latypov2b6861e2022-01-25 13:00:11 -0800612 static const struct kunit_binary_assert_text __text = { \
613 .operation = #op, \
614 .left_text = #left, \
615 .right_text = #right, \
616 }; \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700617 \
618 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800619 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700620 __left op __right, \
621 assert_class, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800622 KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \
Daniel Latypov2b6861e2022-01-25 13:00:11 -0800623 &__text, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800624 __left, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800625 __right), \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700626 fmt, \
627 ##__VA_ARGS__); \
628} while (0)
629
Daniel Latypov40f397772022-01-18 14:35:05 -0800630#define KUNIT_BINARY_INT_ASSERTION(test, \
631 assert_type, \
632 left, \
633 op, \
634 right, \
635 fmt, \
636 ...) \
637 KUNIT_BASE_BINARY_ASSERTION(test, \
638 kunit_binary_assert, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800639 kunit_binary_assert_format, \
Daniel Latypov40f397772022-01-18 14:35:05 -0800640 assert_type, \
641 left, op, right, \
642 fmt, \
643 ##__VA_ARGS__)
644
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800645#define KUNIT_BINARY_PTR_ASSERTION(test, \
646 assert_type, \
647 left, \
648 op, \
649 right, \
650 fmt, \
651 ...) \
652 KUNIT_BASE_BINARY_ASSERTION(test, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700653 kunit_binary_ptr_assert, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800654 kunit_binary_ptr_assert_format, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700655 assert_type, \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800656 left, op, right, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700657 fmt, \
658 ##__VA_ARGS__)
659
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700660#define KUNIT_BINARY_STR_ASSERTION(test, \
661 assert_type, \
662 left, \
663 op, \
664 right, \
665 fmt, \
666 ...) \
667do { \
David Gow3747b5c2021-05-13 12:31:56 -0700668 const char *__left = (left); \
Daniel Latypov2b6861e2022-01-25 13:00:11 -0800669 const char *__right = (right); \
670 static const struct kunit_binary_assert_text __text = { \
671 .operation = #op, \
672 .left_text = #left, \
673 .right_text = #right, \
674 }; \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700675 \
676 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800677 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700678 strcmp(__left, __right) op 0, \
679 kunit_binary_str_assert, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800680 KUNIT_INIT_BINARY_ASSERT_STRUCT(kunit_binary_str_assert_format,\
Daniel Latypov2b6861e2022-01-25 13:00:11 -0800681 &__text, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700682 __left, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700683 __right), \
684 fmt, \
685 ##__VA_ARGS__); \
686} while (0)
687
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700688#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
689 assert_type, \
690 ptr, \
691 fmt, \
692 ...) \
693do { \
Daniel Latypovc2741452022-01-27 13:52:22 -0800694 const typeof(ptr) __ptr = (ptr); \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700695 \
696 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800697 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700698 !IS_ERR_OR_NULL(__ptr), \
699 kunit_ptr_not_err_assert, \
Daniel Latypov05a7da82022-01-13 08:59:31 -0800700 KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700701 __ptr), \
702 fmt, \
703 ##__VA_ARGS__); \
704} while (0)
705
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700706/**
707 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
708 * @test: The test context object.
709 * @condition: an arbitrary boolean expression. The test fails when this does
710 * not evaluate to true.
711 *
712 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
713 * to fail when the specified condition is not met; however, it will not prevent
714 * the test case from continuing to run; this is otherwise known as an
715 * *expectation failure*.
716 */
717#define KUNIT_EXPECT_TRUE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800718 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700719
720#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
721 KUNIT_TRUE_MSG_ASSERTION(test, \
722 KUNIT_EXPECTATION, \
723 condition, \
724 fmt, \
725 ##__VA_ARGS__)
726
727/**
728 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
729 * @test: The test context object.
730 * @condition: an arbitrary boolean expression. The test fails when this does
731 * not evaluate to false.
732 *
733 * Sets an expectation that @condition evaluates to false. See
734 * KUNIT_EXPECT_TRUE() for more information.
735 */
736#define KUNIT_EXPECT_FALSE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800737 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700738
739#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
740 KUNIT_FALSE_MSG_ASSERTION(test, \
741 KUNIT_EXPECTATION, \
742 condition, \
743 fmt, \
744 ##__VA_ARGS__)
745
746/**
747 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
748 * @test: The test context object.
749 * @left: an arbitrary expression that evaluates to a primitive C type.
750 * @right: an arbitrary expression that evaluates to a primitive C type.
751 *
752 * Sets an expectation that the values that @left and @right evaluate to are
753 * equal. This is semantically equivalent to
754 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
755 * more information.
756 */
757#define KUNIT_EXPECT_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800758 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700759
760#define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800761 KUNIT_BINARY_INT_ASSERTION(test, \
762 KUNIT_EXPECTATION, \
763 left, ==, right, \
764 fmt, \
765 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700766
767/**
768 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
769 * @test: The test context object.
770 * @left: an arbitrary expression that evaluates to a pointer.
771 * @right: an arbitrary expression that evaluates to a pointer.
772 *
773 * Sets an expectation that the values that @left and @right evaluate to are
774 * equal. This is semantically equivalent to
775 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
776 * more information.
777 */
778#define KUNIT_EXPECT_PTR_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800779 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700780
781#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800782 KUNIT_BINARY_PTR_ASSERTION(test, \
783 KUNIT_EXPECTATION, \
784 left, ==, right, \
785 fmt, \
786 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700787
788/**
789 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
790 * @test: The test context object.
791 * @left: an arbitrary expression that evaluates to a primitive C type.
792 * @right: an arbitrary expression that evaluates to a primitive C type.
793 *
794 * Sets an expectation that the values that @left and @right evaluate to are not
795 * equal. This is semantically equivalent to
796 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
797 * more information.
798 */
799#define KUNIT_EXPECT_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800800 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700801
802#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800803 KUNIT_BINARY_INT_ASSERTION(test, \
804 KUNIT_EXPECTATION, \
805 left, !=, right, \
806 fmt, \
807 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700808
809/**
810 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
811 * @test: The test context object.
812 * @left: an arbitrary expression that evaluates to a pointer.
813 * @right: an arbitrary expression that evaluates to a pointer.
814 *
815 * Sets an expectation that the values that @left and @right evaluate to are not
816 * equal. This is semantically equivalent to
817 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
818 * more information.
819 */
820#define KUNIT_EXPECT_PTR_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800821 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700822
823#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800824 KUNIT_BINARY_PTR_ASSERTION(test, \
825 KUNIT_EXPECTATION, \
826 left, !=, right, \
827 fmt, \
828 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700829
830/**
831 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
832 * @test: The test context object.
833 * @left: an arbitrary expression that evaluates to a primitive C type.
834 * @right: an arbitrary expression that evaluates to a primitive C type.
835 *
836 * Sets an expectation that the value that @left evaluates to is less than the
837 * value that @right evaluates to. This is semantically equivalent to
838 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
839 * more information.
840 */
841#define KUNIT_EXPECT_LT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800842 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700843
844#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800845 KUNIT_BINARY_INT_ASSERTION(test, \
846 KUNIT_EXPECTATION, \
847 left, <, right, \
848 fmt, \
849 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700850
851/**
852 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
853 * @test: The test context object.
854 * @left: an arbitrary expression that evaluates to a primitive C type.
855 * @right: an arbitrary expression that evaluates to a primitive C type.
856 *
857 * Sets an expectation that the value that @left evaluates to is less than or
858 * equal to the value that @right evaluates to. Semantically this is equivalent
859 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
860 * more information.
861 */
862#define KUNIT_EXPECT_LE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800863 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700864
865#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800866 KUNIT_BINARY_INT_ASSERTION(test, \
867 KUNIT_ASSERTION, \
868 left, <=, right, \
869 fmt, \
870 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700871
872/**
873 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
874 * @test: The test context object.
875 * @left: an arbitrary expression that evaluates to a primitive C type.
876 * @right: an arbitrary expression that evaluates to a primitive C type.
877 *
878 * Sets an expectation that the value that @left evaluates to is greater than
879 * the value that @right evaluates to. This is semantically equivalent to
880 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
881 * more information.
882 */
883#define KUNIT_EXPECT_GT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800884 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700885
886#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800887 KUNIT_BINARY_INT_ASSERTION(test, \
888 KUNIT_EXPECTATION, \
889 left, >, right, \
890 fmt, \
891 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700892
893/**
894 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
895 * @test: The test context object.
896 * @left: an arbitrary expression that evaluates to a primitive C type.
897 * @right: an arbitrary expression that evaluates to a primitive C type.
898 *
899 * Sets an expectation that the value that @left evaluates to is greater than
900 * the value that @right evaluates to. This is semantically equivalent to
901 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
902 * more information.
903 */
904#define KUNIT_EXPECT_GE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800905 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700906
907#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800908 KUNIT_BINARY_INT_ASSERTION(test, \
909 KUNIT_EXPECTATION, \
910 left, >=, right, \
911 fmt, \
912 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700913
914/**
915 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
916 * @test: The test context object.
917 * @left: an arbitrary expression that evaluates to a null terminated string.
918 * @right: an arbitrary expression that evaluates to a null terminated string.
919 *
920 * Sets an expectation that the values that @left and @right evaluate to are
921 * equal. This is semantically equivalent to
922 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
923 * for more information.
924 */
925#define KUNIT_EXPECT_STREQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800926 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700927
928#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -0800929 KUNIT_BINARY_STR_ASSERTION(test, \
930 KUNIT_EXPECTATION, \
931 left, ==, right, \
932 fmt, \
933 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700934
935/**
936 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
937 * @test: The test context object.
938 * @left: an arbitrary expression that evaluates to a null terminated string.
939 * @right: an arbitrary expression that evaluates to a null terminated string.
940 *
941 * Sets an expectation that the values that @left and @right evaluate to are
942 * not equal. This is semantically equivalent to
943 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
944 * for more information.
945 */
946#define KUNIT_EXPECT_STRNEQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800947 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700948
949#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -0800950 KUNIT_BINARY_STR_ASSERTION(test, \
951 KUNIT_EXPECTATION, \
952 left, !=, right, \
953 fmt, \
954 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700955
956/**
Ricardo Ribaldacaae9452022-02-11 17:42:41 +0100957 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
958 * @test: The test context object.
959 * @ptr: an arbitrary pointer.
960 *
961 * Sets an expectation that the value that @ptr evaluates to is null. This is
962 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
963 * See KUNIT_EXPECT_TRUE() for more information.
964 */
965#define KUNIT_EXPECT_NULL(test, ptr) \
966 KUNIT_EXPECT_NULL_MSG(test, \
967 ptr, \
968 NULL)
969
970#define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
971 KUNIT_BINARY_PTR_ASSERTION(test, \
972 KUNIT_EXPECTATION, \
973 ptr, ==, NULL, \
974 fmt, \
975 ##__VA_ARGS__)
976
977/**
978 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
979 * @test: The test context object.
980 * @ptr: an arbitrary pointer.
981 *
982 * Sets an expectation that the value that @ptr evaluates to is not null. This
983 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
984 * See KUNIT_EXPECT_TRUE() for more information.
985 */
986#define KUNIT_EXPECT_NOT_NULL(test, ptr) \
987 KUNIT_EXPECT_NOT_NULL_MSG(test, \
988 ptr, \
989 NULL)
990
991#define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
992 KUNIT_BINARY_PTR_ASSERTION(test, \
993 KUNIT_EXPECTATION, \
994 ptr, !=, NULL, \
995 fmt, \
996 ##__VA_ARGS__)
997
998/**
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700999 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1000 * @test: The test context object.
1001 * @ptr: an arbitrary pointer.
1002 *
1003 * Sets an expectation that the value that @ptr evaluates to is not null and not
1004 * an errno stored in a pointer. This is semantically equivalent to
1005 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1006 * more information.
1007 */
1008#define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001009 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001010
1011#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1012 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1013 KUNIT_EXPECTATION, \
1014 ptr, \
1015 fmt, \
1016 ##__VA_ARGS__)
1017
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001018#define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1019 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1020
1021/**
1022 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1023 * @test: The test context object.
1024 * @condition: an arbitrary boolean expression. The test fails and aborts when
1025 * this does not evaluate to true.
1026 *
1027 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1028 * fail *and immediately abort* when the specified condition is not met. Unlike
1029 * an expectation failure, it will prevent the test case from continuing to run;
1030 * this is otherwise known as an *assertion failure*.
1031 */
1032#define KUNIT_ASSERT_TRUE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001033 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001034
1035#define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1036 KUNIT_TRUE_MSG_ASSERTION(test, \
1037 KUNIT_ASSERTION, \
1038 condition, \
1039 fmt, \
1040 ##__VA_ARGS__)
1041
1042/**
1043 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1044 * @test: The test context object.
1045 * @condition: an arbitrary boolean expression.
1046 *
1047 * Sets an assertion that the value that @condition evaluates to is false. This
1048 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1049 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1050 */
1051#define KUNIT_ASSERT_FALSE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001052 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001053
1054#define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1055 KUNIT_FALSE_MSG_ASSERTION(test, \
1056 KUNIT_ASSERTION, \
1057 condition, \
1058 fmt, \
1059 ##__VA_ARGS__)
1060
1061/**
1062 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1063 * @test: The test context object.
1064 * @left: an arbitrary expression that evaluates to a primitive C type.
1065 * @right: an arbitrary expression that evaluates to a primitive C type.
1066 *
1067 * Sets an assertion that the values that @left and @right evaluate to are
1068 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1069 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1070 */
1071#define KUNIT_ASSERT_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001072 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001073
1074#define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001075 KUNIT_BINARY_INT_ASSERTION(test, \
1076 KUNIT_ASSERTION, \
1077 left, ==, right, \
1078 fmt, \
1079 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001080
1081/**
1082 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1083 * @test: The test context object.
1084 * @left: an arbitrary expression that evaluates to a pointer.
1085 * @right: an arbitrary expression that evaluates to a pointer.
1086 *
1087 * Sets an assertion that the values that @left and @right evaluate to are
1088 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1089 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1090 */
1091#define KUNIT_ASSERT_PTR_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001092 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001093
1094#define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001095 KUNIT_BINARY_PTR_ASSERTION(test, \
1096 KUNIT_ASSERTION, \
1097 left, ==, right, \
1098 fmt, \
1099 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001100
1101/**
1102 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1103 * @test: The test context object.
1104 * @left: an arbitrary expression that evaluates to a primitive C type.
1105 * @right: an arbitrary expression that evaluates to a primitive C type.
1106 *
1107 * Sets an assertion that the values that @left and @right evaluate to are not
1108 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1109 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1110 */
1111#define KUNIT_ASSERT_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001112 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001113
1114#define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001115 KUNIT_BINARY_INT_ASSERTION(test, \
1116 KUNIT_ASSERTION, \
1117 left, !=, right, \
1118 fmt, \
1119 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001120
1121/**
1122 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1123 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1124 * @test: The test context object.
1125 * @left: an arbitrary expression that evaluates to a pointer.
1126 * @right: an arbitrary expression that evaluates to a pointer.
1127 *
1128 * Sets an assertion that the values that @left and @right evaluate to are not
1129 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1130 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1131 */
1132#define KUNIT_ASSERT_PTR_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001133 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001134
1135#define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001136 KUNIT_BINARY_PTR_ASSERTION(test, \
1137 KUNIT_ASSERTION, \
1138 left, !=, right, \
1139 fmt, \
1140 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001141/**
1142 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1143 * @test: The test context object.
1144 * @left: an arbitrary expression that evaluates to a primitive C type.
1145 * @right: an arbitrary expression that evaluates to a primitive C type.
1146 *
1147 * Sets an assertion that the value that @left evaluates to is less than the
1148 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1149 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1150 * is not met.
1151 */
1152#define KUNIT_ASSERT_LT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001153 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001154
1155#define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001156 KUNIT_BINARY_INT_ASSERTION(test, \
1157 KUNIT_EXPECTATION, \
1158 left, <, right, \
1159 fmt, \
1160 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001161/**
1162 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1163 * @test: The test context object.
1164 * @left: an arbitrary expression that evaluates to a primitive C type.
1165 * @right: an arbitrary expression that evaluates to a primitive C type.
1166 *
1167 * Sets an assertion that the value that @left evaluates to is less than or
1168 * equal to the value that @right evaluates to. This is the same as
1169 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1170 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1171 */
1172#define KUNIT_ASSERT_LE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001173 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001174
1175#define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001176 KUNIT_BINARY_INT_ASSERTION(test, \
1177 KUNIT_ASSERTION, \
1178 left, <=, right, \
1179 fmt, \
1180 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001181
1182/**
1183 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1184 * @test: The test context object.
1185 * @left: an arbitrary expression that evaluates to a primitive C type.
1186 * @right: an arbitrary expression that evaluates to a primitive C type.
1187 *
1188 * Sets an assertion that the value that @left evaluates to is greater than the
1189 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1190 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1191 * is not met.
1192 */
1193#define KUNIT_ASSERT_GT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001194 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001195
1196#define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001197 KUNIT_BINARY_INT_ASSERTION(test, \
1198 KUNIT_EXPECTATION, \
1199 left, >, right, \
1200 fmt, \
1201 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001202
1203/**
1204 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1205 * @test: The test context object.
1206 * @left: an arbitrary expression that evaluates to a primitive C type.
1207 * @right: an arbitrary expression that evaluates to a primitive C type.
1208 *
1209 * Sets an assertion that the value that @left evaluates to is greater than the
1210 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1211 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1212 * is not met.
1213 */
1214#define KUNIT_ASSERT_GE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001215 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001216
1217#define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001218 KUNIT_BINARY_INT_ASSERTION(test, \
1219 KUNIT_ASSERTION, \
1220 left, >=, right, \
1221 fmt, \
1222 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001223
1224/**
1225 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1226 * @test: The test context object.
1227 * @left: an arbitrary expression that evaluates to a null terminated string.
1228 * @right: an arbitrary expression that evaluates to a null terminated string.
1229 *
1230 * Sets an assertion that the values that @left and @right evaluate to are
1231 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1232 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1233 */
1234#define KUNIT_ASSERT_STREQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001235 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001236
1237#define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -08001238 KUNIT_BINARY_STR_ASSERTION(test, \
1239 KUNIT_ASSERTION, \
1240 left, ==, right, \
1241 fmt, \
1242 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001243
1244/**
1245 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1246 * @test: The test context object.
1247 * @left: an arbitrary expression that evaluates to a null terminated string.
1248 * @right: an arbitrary expression that evaluates to a null terminated string.
1249 *
1250 * Sets an expectation that the values that @left and @right evaluate to are
1251 * not equal. This is semantically equivalent to
1252 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1253 * for more information.
1254 */
1255#define KUNIT_ASSERT_STRNEQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001256 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001257
1258#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -08001259 KUNIT_BINARY_STR_ASSERTION(test, \
1260 KUNIT_ASSERTION, \
1261 left, !=, right, \
1262 fmt, \
1263 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001264
1265/**
Ricardo Ribaldacaae9452022-02-11 17:42:41 +01001266 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1267 * @test: The test context object.
1268 * @ptr: an arbitrary pointer.
1269 *
1270 * Sets an assertion that the values that @ptr evaluates to is null. This is
1271 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1272 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1273 */
1274#define KUNIT_ASSERT_NULL(test, ptr) \
1275 KUNIT_ASSERT_NULL_MSG(test, \
1276 ptr, \
1277 NULL)
1278
1279#define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1280 KUNIT_BINARY_PTR_ASSERTION(test, \
1281 KUNIT_ASSERTION, \
1282 ptr, ==, NULL, \
1283 fmt, \
1284 ##__VA_ARGS__)
1285
1286/**
1287 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1288 * @test: The test context object.
1289 * @ptr: an arbitrary pointer.
1290 *
1291 * Sets an assertion that the values that @ptr evaluates to is not null. This
1292 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1293 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1294 */
1295#define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1296 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1297 ptr, \
1298 NULL)
1299
1300#define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1301 KUNIT_BINARY_PTR_ASSERTION(test, \
1302 KUNIT_ASSERTION, \
1303 ptr, !=, NULL, \
1304 fmt, \
1305 ##__VA_ARGS__)
1306
1307/**
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001308 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1309 * @test: The test context object.
1310 * @ptr: an arbitrary pointer.
1311 *
1312 * Sets an assertion that the value that @ptr evaluates to is not null and not
1313 * an errno stored in a pointer. This is the same as
1314 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1315 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1316 */
1317#define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001318 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001319
1320#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1321 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1322 KUNIT_ASSERTION, \
1323 ptr, \
1324 fmt, \
1325 ##__VA_ARGS__)
1326
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +05301327/**
1328 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1329 * @name: prefix for the test parameter generator function.
1330 * @array: array of test parameters.
1331 * @get_desc: function to convert param to description; NULL to use default
1332 *
1333 * Define function @name_gen_params which uses @array to generate parameters.
1334 */
1335#define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1336 static const void *name##_gen_params(const void *prev, char *desc) \
1337 { \
1338 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1339 if (__next - (array) < ARRAY_SIZE((array))) { \
1340 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1341 if (__get_desc) \
1342 __get_desc(__next, desc); \
1343 return __next; \
1344 } \
1345 return NULL; \
1346 }
1347
Daniel Latypov61695f82022-03-28 10:41:42 -07001348// TODO([email protected]): consider eventually migrating users to explicitly
1349// include resource.h themselves if they need it.
1350#include <kunit/resource.h>
1351
Brendan Higgins914cc632019-09-23 02:02:31 -07001352#endif /* _KUNIT_TEST_H */