blob: 48cf520b69ce6f23c1fe75e1bd188035d2cbbf5b [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 Higgins0a7568532019-09-23 02:02:32 -070030struct kunit_resource;
31
32typedef int (*kunit_resource_init_t)(struct kunit_resource *, void *);
33typedef void (*kunit_resource_free_t)(struct kunit_resource *);
34
35/**
36 * struct kunit_resource - represents a *test managed resource*
Alan Maguired4cdd142020-05-29 22:46:20 +010037 * @data: for the user to store arbitrary data.
Mauro Carvalho Chehab38d9b902020-08-27 08:31:43 +020038 * @name: optional name
Brendan Higgins0a7568532019-09-23 02:02:32 -070039 * @free: a user supplied function to free the resource. Populated by
Alan Maguired4cdd142020-05-29 22:46:20 +010040 * kunit_resource_alloc().
Brendan Higgins0a7568532019-09-23 02:02:32 -070041 *
42 * Represents a *test managed resource*, a resource which will automatically be
43 * cleaned up at the end of a test case.
44 *
Alan Maguired4cdd142020-05-29 22:46:20 +010045 * Resources are reference counted so if a resource is retrieved via
46 * kunit_alloc_and_get_resource() or kunit_find_resource(), we need
47 * to call kunit_put_resource() to reduce the resource reference count
48 * when finished with it. Note that kunit_alloc_resource() does not require a
49 * kunit_resource_put() because it does not retrieve the resource itself.
50 *
Brendan Higgins0a7568532019-09-23 02:02:32 -070051 * Example:
52 *
53 * .. code-block:: c
54 *
55 * struct kunit_kmalloc_params {
56 * size_t size;
57 * gfp_t gfp;
58 * };
59 *
60 * static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
61 * {
62 * struct kunit_kmalloc_params *params = context;
Alan Maguired4cdd142020-05-29 22:46:20 +010063 * res->data = kmalloc(params->size, params->gfp);
Brendan Higgins0a7568532019-09-23 02:02:32 -070064 *
Alan Maguired4cdd142020-05-29 22:46:20 +010065 * if (!res->data)
Brendan Higgins0a7568532019-09-23 02:02:32 -070066 * return -ENOMEM;
67 *
68 * return 0;
69 * }
70 *
71 * static void kunit_kmalloc_free(struct kunit_resource *res)
72 * {
Alan Maguired4cdd142020-05-29 22:46:20 +010073 * kfree(res->data);
Brendan Higgins0a7568532019-09-23 02:02:32 -070074 * }
75 *
76 * void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
77 * {
78 * struct kunit_kmalloc_params params;
Brendan Higgins0a7568532019-09-23 02:02:32 -070079 *
80 * params.size = size;
81 * params.gfp = gfp;
82 *
Alan Maguired4cdd142020-05-29 22:46:20 +010083 * return kunit_alloc_resource(test, kunit_kmalloc_init,
Brendan Higgins0a7568532019-09-23 02:02:32 -070084 * kunit_kmalloc_free, &params);
Brendan Higgins0a7568532019-09-23 02:02:32 -070085 * }
Alan Maguire725aca92020-05-29 22:46:21 +010086 *
87 * Resources can also be named, with lookup/removal done on a name
88 * basis also. kunit_add_named_resource(), kunit_find_named_resource()
89 * and kunit_destroy_named_resource(). Resource names must be
90 * unique within the test instance.
Brendan Higgins0a7568532019-09-23 02:02:32 -070091 */
92struct kunit_resource {
Alan Maguired4cdd142020-05-29 22:46:20 +010093 void *data;
Mauro Carvalho Chehab38d9b902020-08-27 08:31:43 +020094 const char *name;
95 kunit_resource_free_t free;
Brendan Higgins0a7568532019-09-23 02:02:32 -070096
97 /* private: internal use only. */
Alan Maguired4cdd142020-05-29 22:46:20 +010098 struct kref refcount;
Brendan Higgins0a7568532019-09-23 02:02:32 -070099 struct list_head node;
100};
101
Brendan Higgins914cc632019-09-23 02:02:31 -0700102struct kunit;
103
Alan Maguiree2219db2020-03-26 14:25:07 +0000104/* Size of log associated with test. */
105#define KUNIT_LOG_SIZE 512
106
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +0530107/* Maximum size of parameter description string. */
108#define KUNIT_PARAM_DESC_SIZE 128
109
David Gow6d2426b2021-06-24 23:58:12 -0700110/* Maximum size of a status comment. */
111#define KUNIT_STATUS_COMMENT_SIZE 256
112
Alan Maguirec3bba692020-03-26 14:25:09 +0000113/*
114 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
115 * sub-subtest. See the "Subtests" section in
116 * https://node-tap.org/tap-protocol/
117 */
118#define KUNIT_SUBTEST_INDENT " "
119#define KUNIT_SUBSUBTEST_INDENT " "
120
Brendan Higgins914cc632019-09-23 02:02:31 -0700121/**
David Gow6d2426b2021-06-24 23:58:12 -0700122 * enum kunit_status - Type of result for a test or test suite
123 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
124 * @KUNIT_FAILURE: Denotes the test has failed.
125 * @KUNIT_SKIPPED: Denotes the test has been skipped.
126 */
127enum kunit_status {
128 KUNIT_SUCCESS,
129 KUNIT_FAILURE,
130 KUNIT_SKIPPED,
131};
132
133/**
Brendan Higgins914cc632019-09-23 02:02:31 -0700134 * struct kunit_case - represents an individual test case.
135 *
136 * @run_case: the function representing the actual test case.
137 * @name: the name of the test case.
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +0530138 * @generate_params: the generator function for parameterized tests.
Brendan Higgins914cc632019-09-23 02:02:31 -0700139 *
140 * A test case is a function with the signature,
Brendan Higginse4aea8f2019-09-23 02:02:41 -0700141 * ``void (*)(struct kunit *)``
142 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
143 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
Brendan Higgins914cc632019-09-23 02:02:31 -0700144 * with a &struct kunit_suite and will be run after the suite's init
145 * function and followed by the suite's exit function.
146 *
147 * A test case should be static and should only be created with the
148 * KUNIT_CASE() macro; additionally, every array of test cases should be
149 * terminated with an empty test case.
150 *
151 * Example:
152 *
153 * .. code-block:: c
154 *
155 * void add_test_basic(struct kunit *test)
156 * {
157 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
158 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
159 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
160 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
161 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
162 * }
163 *
164 * static struct kunit_case example_test_cases[] = {
165 * KUNIT_CASE(add_test_basic),
166 * {}
167 * };
168 *
169 */
170struct kunit_case {
171 void (*run_case)(struct kunit *test);
172 const char *name;
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +0530173 const void* (*generate_params)(const void *prev, char *desc);
Brendan Higgins914cc632019-09-23 02:02:31 -0700174
175 /* private: internal use only. */
David Gow6d2426b2021-06-24 23:58:12 -0700176 enum kunit_status status;
Alan Maguiree2219db2020-03-26 14:25:07 +0000177 char *log;
Brendan Higgins914cc632019-09-23 02:02:31 -0700178};
179
David Gow6d2426b2021-06-24 23:58:12 -0700180static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
Alan Maguiree2219db2020-03-26 14:25:07 +0000181{
David Gow6d2426b2021-06-24 23:58:12 -0700182 switch (status) {
183 case KUNIT_SKIPPED:
184 case KUNIT_SUCCESS:
185 return "ok";
186 case KUNIT_FAILURE:
187 return "not ok";
188 }
189 return "invalid";
Alan Maguiree2219db2020-03-26 14:25:07 +0000190}
191
Brendan Higgins914cc632019-09-23 02:02:31 -0700192/**
193 * KUNIT_CASE - A helper for creating a &struct kunit_case
194 *
195 * @test_name: a reference to a test case function.
196 *
197 * Takes a symbol for a function representing a test case and creates a
198 * &struct kunit_case object from it. See the documentation for
199 * &struct kunit_case for an example on how to use it.
200 */
201#define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
202
203/**
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +0530204 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
205 *
206 * @test_name: a reference to a test case function.
207 * @gen_params: a reference to a parameter generator function.
208 *
209 * The generator function::
210 *
211 * const void* gen_params(const void *prev, char *desc)
212 *
213 * is used to lazily generate a series of arbitrarily typed values that fit into
214 * a void*. The argument @prev is the previously returned value, which should be
215 * used to derive the next value; @prev is set to NULL on the initial generator
216 * call. When no more values are available, the generator must return NULL.
217 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
218 * describing the parameter.
219 */
220#define KUNIT_CASE_PARAM(test_name, gen_params) \
221 { .run_case = test_name, .name = #test_name, \
222 .generate_params = gen_params }
223
224/**
Brendan Higgins914cc632019-09-23 02:02:31 -0700225 * struct kunit_suite - describes a related collection of &struct kunit_case
226 *
227 * @name: the name of the test. Purely informational.
228 * @init: called before every test case.
229 * @exit: called after every test case.
230 * @test_cases: a null terminated array of test cases.
231 *
232 * A kunit_suite is a collection of related &struct kunit_case s, such that
233 * @init is called before every test case and @exit is called after every
234 * test case, similar to the notion of a *test fixture* or a *test class*
235 * in other unit testing frameworks like JUnit or Googletest.
236 *
237 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
238 * to run it.
239 */
240struct kunit_suite {
241 const char name[256];
242 int (*init)(struct kunit *test);
243 void (*exit)(struct kunit *test);
244 struct kunit_case *test_cases;
Alan Maguiree2219db2020-03-26 14:25:07 +0000245
Lothar Rubuschc4714b02020-04-15 20:16:53 +0000246 /* private: internal use only */
David Gow6d2426b2021-06-24 23:58:12 -0700247 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
Alan Maguiree2219db2020-03-26 14:25:07 +0000248 struct dentry *debugfs;
249 char *log;
Brendan Higgins914cc632019-09-23 02:02:31 -0700250};
251
252/**
253 * struct kunit - represents a running instance of a test.
254 *
255 * @priv: for user to store arbitrary data. Commonly used to pass data
256 * created in the init function (see &struct kunit_suite).
257 *
258 * Used to store information about the current context under which the test
259 * is running. Most of this data is private and should only be accessed
260 * indirectly via public functions; the one exception is @priv which can be
261 * used by the test writer to store arbitrary data.
262 */
263struct kunit {
264 void *priv;
265
266 /* private: internal use only. */
267 const char *name; /* Read only after initialization! */
Alan Maguiree2219db2020-03-26 14:25:07 +0000268 char *log; /* Points at case log after initialization */
Brendan Higgins5f3e0622019-09-23 02:02:39 -0700269 struct kunit_try_catch try_catch;
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +0530270 /* param_value is the current parameter value for a test case. */
271 const void *param_value;
272 /* param_index stores the index of the parameter in parameterized tests. */
273 int param_index;
Brendan Higgins914cc632019-09-23 02:02:31 -0700274 /*
275 * success starts as true, and may only be set to false during a
276 * test case; thus, it is safe to update this across multiple
277 * threads using WRITE_ONCE; however, as a consequence, it may only
278 * be read after the test case finishes once all threads associated
279 * with the test case have terminated.
280 */
Brendan Higgins0a7568532019-09-23 02:02:32 -0700281 spinlock_t lock; /* Guards all mutable test state. */
David Gow6d2426b2021-06-24 23:58:12 -0700282 enum kunit_status status; /* Read only after test_case finishes! */
Brendan Higgins0a7568532019-09-23 02:02:32 -0700283 /*
284 * Because resources is a list that may be updated multiple times (with
285 * new resources) from any thread associated with a test case, we must
286 * protect it with some type of lock.
287 */
288 struct list_head resources; /* Protected by lock. */
David Gow6d2426b2021-06-24 23:58:12 -0700289
290 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
Brendan Higgins914cc632019-09-23 02:02:31 -0700291};
292
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -0700293static inline void kunit_set_failure(struct kunit *test)
294{
David Gow6d2426b2021-06-24 23:58:12 -0700295 WRITE_ONCE(test->status, KUNIT_FAILURE);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -0700296}
297
Alan Maguiree2219db2020-03-26 14:25:07 +0000298void kunit_init_test(struct kunit *test, const char *name, char *log);
Brendan Higgins914cc632019-09-23 02:02:31 -0700299
300int kunit_run_tests(struct kunit_suite *suite);
301
Alan Maguiree2219db2020-03-26 14:25:07 +0000302size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
303
304unsigned int kunit_test_case_num(struct kunit_suite *suite,
305 struct kunit_case *test_case);
306
Alan Maguireaac35462020-08-04 13:47:42 -0700307int __kunit_test_suites_init(struct kunit_suite * const * const suites);
Alan Maguiree2219db2020-03-26 14:25:07 +0000308
309void __kunit_test_suites_exit(struct kunit_suite **suites);
310
Brendan Higgins8c0d8842020-08-04 13:47:43 -0700311#if IS_BUILTIN(CONFIG_KUNIT)
312int kunit_run_all_tests(void);
313#else
314static inline int kunit_run_all_tests(void)
315{
316 return 0;
317}
318#endif /* IS_BUILTIN(CONFIG_KUNIT) */
319
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200320#ifdef MODULE
Brendan Higgins914cc632019-09-23 02:02:31 -0700321/**
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200322 * kunit_test_suites_for_module() - used to register one or more
323 * &struct kunit_suite with KUnit.
Brendan Higgins914cc632019-09-23 02:02:31 -0700324 *
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200325 * @__suites: a statically allocated list of &struct kunit_suite.
Brendan Higgins914cc632019-09-23 02:02:31 -0700326 *
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200327 * Registers @__suites with the test framework. See &struct kunit_suite for
Brendan Higgins914cc632019-09-23 02:02:31 -0700328 * more information.
329 *
Alan Maguireaac35462020-08-04 13:47:42 -0700330 * If a test suite is built-in, module_init() gets translated into
331 * an initcall which we don't want as the idea is that for builtins
332 * the executor will manage execution. So ensure we do not define
333 * module_{init|exit} functions for the builtin case when registering
334 * suites via kunit_test_suites() below.
Brendan Higgins914cc632019-09-23 02:02:31 -0700335 */
Alan Maguireaac35462020-08-04 13:47:42 -0700336#define kunit_test_suites_for_module(__suites) \
337 static int __init kunit_test_suites_init(void) \
Alan Maguirec475c772020-01-06 22:28:20 +0000338 { \
Alan Maguireaac35462020-08-04 13:47:42 -0700339 return __kunit_test_suites_init(__suites); \
Alan Maguirec475c772020-01-06 22:28:20 +0000340 } \
Alan Maguireaac35462020-08-04 13:47:42 -0700341 module_init(kunit_test_suites_init); \
342 \
Alan Maguirec475c772020-01-06 22:28:20 +0000343 static void __exit kunit_test_suites_exit(void) \
344 { \
Alan Maguireaac35462020-08-04 13:47:42 -0700345 return __kunit_test_suites_exit(__suites); \
Alan Maguirec475c772020-01-06 22:28:20 +0000346 } \
347 module_exit(kunit_test_suites_exit)
Alan Maguireaac35462020-08-04 13:47:42 -0700348#else
349#define kunit_test_suites_for_module(__suites)
350#endif /* MODULE */
351
352#define __kunit_test_suites(unique_array, unique_suites, ...) \
353 static struct kunit_suite *unique_array[] = { __VA_ARGS__, NULL }; \
354 kunit_test_suites_for_module(unique_array); \
355 static struct kunit_suite **unique_suites \
Joe Perches33def842020-10-21 19:36:07 -0700356 __used __section(".kunit_test_suites") = unique_array
Alan Maguireaac35462020-08-04 13:47:42 -0700357
358/**
359 * kunit_test_suites() - used to register one or more &struct kunit_suite
360 * with KUnit.
361 *
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200362 * @__suites: a statically allocated list of &struct kunit_suite.
Alan Maguireaac35462020-08-04 13:47:42 -0700363 *
364 * Registers @suites with the test framework. See &struct kunit_suite for
365 * more information.
366 *
367 * When builtin, KUnit tests are all run via executor; this is done
368 * by placing the array of struct kunit_suite * in the .kunit_test_suites
369 * ELF section.
370 *
371 * An alternative is to build the tests as a module. Because modules do not
372 * support multiple initcall()s, we need to initialize an array of suites for a
373 * module.
374 *
375 */
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200376#define kunit_test_suites(__suites...) \
Alan Maguireaac35462020-08-04 13:47:42 -0700377 __kunit_test_suites(__UNIQUE_ID(array), \
378 __UNIQUE_ID(suites), \
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200379 ##__suites)
Alan Maguirec475c772020-01-06 22:28:20 +0000380
381#define kunit_test_suite(suite) kunit_test_suites(&suite)
Brendan Higgins914cc632019-09-23 02:02:31 -0700382
Alan Maguiree2219db2020-03-26 14:25:07 +0000383#define kunit_suite_for_each_test_case(suite, test_case) \
384 for (test_case = suite->test_cases; test_case->run_case; test_case++)
385
David Gow6d2426b2021-06-24 23:58:12 -0700386enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
Alan Maguiree2219db2020-03-26 14:25:07 +0000387
Brendan Higgins0a7568532019-09-23 02:02:32 -0700388/*
389 * Like kunit_alloc_resource() below, but returns the struct kunit_resource
390 * object that contains the allocation. This is mostly for testing purposes.
391 */
392struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
393 kunit_resource_init_t init,
394 kunit_resource_free_t free,
395 gfp_t internal_gfp,
396 void *context);
397
398/**
Alan Maguired4cdd142020-05-29 22:46:20 +0100399 * kunit_get_resource() - Hold resource for use. Should not need to be used
400 * by most users as we automatically get resources
401 * retrieved by kunit_find_resource*().
402 * @res: resource
403 */
404static inline void kunit_get_resource(struct kunit_resource *res)
405{
406 kref_get(&res->refcount);
407}
408
409/*
410 * Called when refcount reaches zero via kunit_put_resources();
411 * should not be called directly.
412 */
413static inline void kunit_release_resource(struct kref *kref)
414{
415 struct kunit_resource *res = container_of(kref, struct kunit_resource,
416 refcount);
417
418 /* If free function is defined, resource was dynamically allocated. */
419 if (res->free) {
420 res->free(res);
421 kfree(res);
422 }
423}
424
425/**
426 * kunit_put_resource() - When caller is done with retrieved resource,
427 * kunit_put_resource() should be called to drop
428 * reference count. The resource list maintains
429 * a reference count on resources, so if no users
430 * are utilizing a resource and it is removed from
431 * the resource list, it will be freed via the
432 * associated free function (if any). Only
433 * needs to be used if we alloc_and_get() or
434 * find() resource.
435 * @res: resource
436 */
437static inline void kunit_put_resource(struct kunit_resource *res)
438{
439 kref_put(&res->refcount, kunit_release_resource);
440}
441
442/**
443 * kunit_add_resource() - Add a *test managed resource*.
444 * @test: The test context object.
445 * @init: a user-supplied function to initialize the result (if needed). If
446 * none is supplied, the resource data value is simply set to @data.
447 * If an init function is supplied, @data is passed to it instead.
448 * @free: a user-supplied function to free the resource (if needed).
Mauro Carvalho Chehab38d9b902020-08-27 08:31:43 +0200449 * @res: The resource.
Alan Maguired4cdd142020-05-29 22:46:20 +0100450 * @data: value to pass to init function or set in resource data field.
451 */
452int kunit_add_resource(struct kunit *test,
453 kunit_resource_init_t init,
454 kunit_resource_free_t free,
455 struct kunit_resource *res,
456 void *data);
Alan Maguire725aca92020-05-29 22:46:21 +0100457
458/**
459 * kunit_add_named_resource() - Add a named *test managed resource*.
460 * @test: The test context object.
461 * @init: a user-supplied function to initialize the resource data, if needed.
462 * @free: a user-supplied function to free the resource data, if needed.
Mauro Carvalho Chehab38d9b902020-08-27 08:31:43 +0200463 * @res: The resource.
464 * @name: name to be set for resource.
465 * @data: value to pass to init function or set in resource data field.
Alan Maguire725aca92020-05-29 22:46:21 +0100466 */
467int kunit_add_named_resource(struct kunit *test,
468 kunit_resource_init_t init,
469 kunit_resource_free_t free,
470 struct kunit_resource *res,
471 const char *name,
472 void *data);
473
Alan Maguired4cdd142020-05-29 22:46:20 +0100474/**
Brendan Higgins0a7568532019-09-23 02:02:32 -0700475 * kunit_alloc_resource() - Allocates a *test managed resource*.
476 * @test: The test context object.
477 * @init: a user supplied function to initialize the resource.
478 * @free: a user supplied function to free the resource.
479 * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
480 * @context: for the user to pass in arbitrary data to the init function.
481 *
482 * Allocates a *test managed resource*, a resource which will automatically be
483 * cleaned up at the end of a test case. See &struct kunit_resource for an
484 * example.
485 *
Alan Maguired4cdd142020-05-29 22:46:20 +0100486 * Note: KUnit needs to allocate memory for a kunit_resource object. You must
Brendan Higgins0a7568532019-09-23 02:02:32 -0700487 * specify an @internal_gfp that is compatible with the use context of your
488 * resource.
489 */
490static inline void *kunit_alloc_resource(struct kunit *test,
491 kunit_resource_init_t init,
492 kunit_resource_free_t free,
493 gfp_t internal_gfp,
494 void *context)
495{
496 struct kunit_resource *res;
497
Alan Maguired4cdd142020-05-29 22:46:20 +0100498 res = kzalloc(sizeof(*res), internal_gfp);
499 if (!res)
500 return NULL;
Brendan Higgins0a7568532019-09-23 02:02:32 -0700501
Alan Maguired4cdd142020-05-29 22:46:20 +0100502 if (!kunit_add_resource(test, init, free, res, context))
503 return res->data;
Brendan Higgins0a7568532019-09-23 02:02:32 -0700504
505 return NULL;
506}
507
508typedef bool (*kunit_resource_match_t)(struct kunit *test,
Alan Maguired4cdd142020-05-29 22:46:20 +0100509 struct kunit_resource *res,
Brendan Higgins0a7568532019-09-23 02:02:32 -0700510 void *match_data);
511
512/**
513 * kunit_resource_instance_match() - Match a resource with the same instance.
514 * @test: Test case to which the resource belongs.
Alan Maguired4cdd142020-05-29 22:46:20 +0100515 * @res: The resource.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700516 * @match_data: The resource pointer to match against.
517 *
518 * An instance of kunit_resource_match_t that matches a resource whose
519 * allocation matches @match_data.
520 */
521static inline bool kunit_resource_instance_match(struct kunit *test,
Alan Maguired4cdd142020-05-29 22:46:20 +0100522 struct kunit_resource *res,
Brendan Higgins0a7568532019-09-23 02:02:32 -0700523 void *match_data)
524{
Alan Maguired4cdd142020-05-29 22:46:20 +0100525 return res->data == match_data;
Brendan Higgins0a7568532019-09-23 02:02:32 -0700526}
527
528/**
Alan Maguire725aca92020-05-29 22:46:21 +0100529 * kunit_resource_name_match() - Match a resource with the same name.
530 * @test: Test case to which the resource belongs.
531 * @res: The resource.
532 * @match_name: The name to match against.
533 */
534static inline bool kunit_resource_name_match(struct kunit *test,
535 struct kunit_resource *res,
536 void *match_name)
537{
538 return res->name && strcmp(res->name, match_name) == 0;
539}
540
541/**
Alan Maguired4cdd142020-05-29 22:46:20 +0100542 * kunit_find_resource() - Find a resource using match function/data.
543 * @test: Test case to which the resource belongs.
544 * @match: match function to be applied to resources/match data.
545 * @match_data: data to be used in matching.
546 */
547static inline struct kunit_resource *
548kunit_find_resource(struct kunit *test,
549 kunit_resource_match_t match,
550 void *match_data)
551{
552 struct kunit_resource *res, *found = NULL;
Vlastimil Babka26c6cb72021-06-28 19:34:30 -0700553 unsigned long flags;
Alan Maguired4cdd142020-05-29 22:46:20 +0100554
Vlastimil Babka26c6cb72021-06-28 19:34:30 -0700555 spin_lock_irqsave(&test->lock, flags);
Alan Maguired4cdd142020-05-29 22:46:20 +0100556
557 list_for_each_entry_reverse(res, &test->resources, node) {
558 if (match(test, res, (void *)match_data)) {
559 found = res;
560 kunit_get_resource(found);
561 break;
562 }
563 }
564
Vlastimil Babka26c6cb72021-06-28 19:34:30 -0700565 spin_unlock_irqrestore(&test->lock, flags);
Alan Maguired4cdd142020-05-29 22:46:20 +0100566
567 return found;
568}
569
570/**
Alan Maguire725aca92020-05-29 22:46:21 +0100571 * kunit_find_named_resource() - Find a resource using match name.
572 * @test: Test case to which the resource belongs.
573 * @name: match name.
574 */
575static inline struct kunit_resource *
576kunit_find_named_resource(struct kunit *test,
577 const char *name)
578{
579 return kunit_find_resource(test, kunit_resource_name_match,
580 (void *)name);
581}
582
583/**
Alan Maguired4cdd142020-05-29 22:46:20 +0100584 * kunit_destroy_resource() - Find a kunit_resource and destroy it.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700585 * @test: Test case to which the resource belongs.
586 * @match: Match function. Returns whether a given resource matches @match_data.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700587 * @match_data: Data passed into @match.
588 *
Brendan Higgins0a7568532019-09-23 02:02:32 -0700589 * RETURNS:
590 * 0 if kunit_resource is found and freed, -ENOENT if not found.
591 */
Alan Maguired4cdd142020-05-29 22:46:20 +0100592int kunit_destroy_resource(struct kunit *test,
Brendan Higgins0a7568532019-09-23 02:02:32 -0700593 kunit_resource_match_t match,
Brendan Higgins0a7568532019-09-23 02:02:32 -0700594 void *match_data);
595
Alan Maguire725aca92020-05-29 22:46:21 +0100596static inline int kunit_destroy_named_resource(struct kunit *test,
597 const char *name)
598{
599 return kunit_destroy_resource(test, kunit_resource_name_match,
600 (void *)name);
601}
602
Brendan Higgins0a7568532019-09-23 02:02:32 -0700603/**
Mauro Carvalho Chehab623050a2020-09-09 12:07:49 +0200604 * kunit_remove_resource() - remove resource from resource list associated with
605 * test.
Alan Maguired4cdd142020-05-29 22:46:20 +0100606 * @test: The test context object.
607 * @res: The resource to be removed.
608 *
609 * Note that the resource will not be immediately freed since it is likely
610 * the caller has a reference to it via alloc_and_get() or find();
611 * in this case a final call to kunit_put_resource() is required.
612 */
613void kunit_remove_resource(struct kunit *test, struct kunit_resource *res);
614
615/**
Daniel Latypov7122deb2021-05-03 13:58:34 -0700616 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
617 * @test: The test context object.
618 * @n: number of elements.
619 * @size: The size in bytes of the desired memory.
620 * @gfp: flags passed to underlying kmalloc().
621 *
622 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
623 * and is automatically cleaned up after the test case concludes. See &struct
624 * kunit_resource for more information.
625 */
Daniel Latypov361b57d2021-10-05 13:46:32 -0700626void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
Daniel Latypov7122deb2021-05-03 13:58:34 -0700627
628/**
Brendan Higgins0a7568532019-09-23 02:02:32 -0700629 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
630 * @test: The test context object.
631 * @size: The size in bytes of the desired memory.
632 * @gfp: flags passed to underlying kmalloc().
633 *
Daniel Latypov7122deb2021-05-03 13:58:34 -0700634 * See kmalloc() and kunit_kmalloc_array() for more information.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700635 */
Daniel Latypov7122deb2021-05-03 13:58:34 -0700636static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
637{
638 return kunit_kmalloc_array(test, 1, size, gfp);
639}
Brendan Higgins0a7568532019-09-23 02:02:32 -0700640
641/**
642 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
643 * @test: The test case to which the resource belongs.
644 * @ptr: The memory allocation to free.
645 */
646void kunit_kfree(struct kunit *test, const void *ptr);
647
648/**
649 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
650 * @test: The test context object.
651 * @size: The size in bytes of the desired memory.
652 * @gfp: flags passed to underlying kmalloc().
653 *
Daniel Latypov7122deb2021-05-03 13:58:34 -0700654 * See kzalloc() and kunit_kmalloc_array() for more information.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700655 */
656static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
657{
658 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
659}
660
Daniel Latypov7122deb2021-05-03 13:58:34 -0700661/**
662 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
663 * @test: The test context object.
664 * @n: number of elements.
665 * @size: The size in bytes of the desired memory.
666 * @gfp: flags passed to underlying kmalloc().
667 *
668 * See kcalloc() and kunit_kmalloc_array() for more information.
669 */
Daniel Latypov361b57d2021-10-05 13:46:32 -0700670static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
Daniel Latypov7122deb2021-05-03 13:58:34 -0700671{
Daniel Latypov361b57d2021-10-05 13:46:32 -0700672 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
Daniel Latypov7122deb2021-05-03 13:58:34 -0700673}
674
Brendan Higgins0a7568532019-09-23 02:02:32 -0700675void kunit_cleanup(struct kunit *test);
676
David Gow44acdbb2021-05-13 13:03:50 -0700677void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
Alan Maguiree2219db2020-03-26 14:25:07 +0000678
David Gow6d2426b2021-06-24 23:58:12 -0700679/**
680 * kunit_mark_skipped() - Marks @test_or_suite as skipped
681 *
682 * @test_or_suite: The test context object.
683 * @fmt: A printk() style format string.
684 *
685 * Marks the test as skipped. @fmt is given output as the test status
686 * comment, typically the reason the test was skipped.
687 *
688 * Test execution continues after kunit_mark_skipped() is called.
689 */
690#define kunit_mark_skipped(test_or_suite, fmt, ...) \
691 do { \
692 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
693 scnprintf((test_or_suite)->status_comment, \
694 KUNIT_STATUS_COMMENT_SIZE, \
695 fmt, ##__VA_ARGS__); \
696 } while (0)
697
698/**
699 * kunit_skip() - Marks @test_or_suite as skipped
700 *
701 * @test_or_suite: The test context object.
702 * @fmt: A printk() style format string.
703 *
704 * Skips the test. @fmt is given output as the test status
705 * comment, typically the reason the test was skipped.
706 *
707 * Test execution is halted after kunit_skip() is called.
708 */
709#define kunit_skip(test_or_suite, fmt, ...) \
710 do { \
711 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
712 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
713 } while (0)
Alan Maguiree2219db2020-03-26 14:25:07 +0000714
715/*
716 * printk and log to per-test or per-suite log buffer. Logging only done
717 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
718 */
719#define kunit_log(lvl, test_or_suite, fmt, ...) \
720 do { \
721 printk(lvl fmt, ##__VA_ARGS__); \
722 kunit_log_append((test_or_suite)->log, fmt "\n", \
723 ##__VA_ARGS__); \
724 } while (0)
725
726#define kunit_printk(lvl, test, fmt, ...) \
Alan Maguirec3bba692020-03-26 14:25:09 +0000727 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
728 (test)->name, ##__VA_ARGS__)
Brendan Higgins914cc632019-09-23 02:02:31 -0700729
730/**
731 * kunit_info() - Prints an INFO level message associated with @test.
732 *
733 * @test: The test context object.
734 * @fmt: A printk() style format string.
735 *
736 * Prints an info level message associated with the test suite being run.
737 * Takes a variable number of format parameters just like printk().
738 */
739#define kunit_info(test, fmt, ...) \
740 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
741
742/**
743 * kunit_warn() - Prints a WARN level message associated with @test.
744 *
745 * @test: The test context object.
746 * @fmt: A printk() style format string.
747 *
748 * Prints a warning level message.
749 */
750#define kunit_warn(test, fmt, ...) \
751 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
752
753/**
754 * kunit_err() - Prints an ERROR level message associated with @test.
755 *
756 * @test: The test context object.
757 * @fmt: A printk() style format string.
758 *
759 * Prints an error level message.
760 */
761#define kunit_err(test, fmt, ...) \
762 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
763
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700764/**
765 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
766 * @test: The test context object.
767 *
768 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
769 * words, it does nothing and only exists for code clarity. See
770 * KUNIT_EXPECT_TRUE() for more information.
771 */
772#define KUNIT_SUCCEED(test) do {} while (0)
773
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800774void kunit_do_failed_assertion(struct kunit *test,
Daniel Latypov21957f92022-01-13 08:59:30 -0800775 const struct kunit_loc *loc,
776 enum kunit_assert_type type,
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800777 struct kunit_assert *assert,
778 const char *fmt, ...);
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700779
Daniel Latypov21957f92022-01-13 08:59:30 -0800780#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800781 if (unlikely(!(pass))) { \
Daniel Latypov21957f92022-01-13 08:59:30 -0800782 static const struct kunit_loc loc = KUNIT_CURRENT_LOC; \
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800783 struct assert_class __assertion = INITIALIZER; \
784 kunit_do_failed_assertion(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800785 &loc, \
786 assert_type, \
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800787 &__assertion.assert, \
788 fmt, \
789 ##__VA_ARGS__); \
790 } \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700791} while (0)
792
793
794#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
795 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800796 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700797 false, \
798 kunit_fail_assert, \
Daniel Latypov05a7da82022-01-13 08:59:31 -0800799 KUNIT_INIT_FAIL_ASSERT_STRUCT, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700800 fmt, \
801 ##__VA_ARGS__)
802
803/**
804 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
805 * @test: The test context object.
806 * @fmt: an informational message to be printed when the assertion is made.
807 * @...: string format arguments.
808 *
809 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
810 * other words, it always results in a failed expectation, and consequently
811 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
812 * for more information.
813 */
814#define KUNIT_FAIL(test, fmt, ...) \
815 KUNIT_FAIL_ASSERTION(test, \
816 KUNIT_EXPECTATION, \
817 fmt, \
818 ##__VA_ARGS__)
819
820#define KUNIT_UNARY_ASSERTION(test, \
821 assert_type, \
822 condition, \
823 expected_true, \
824 fmt, \
825 ...) \
826 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800827 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700828 !!(condition) == !!expected_true, \
829 kunit_unary_assert, \
Daniel Latypov05a7da82022-01-13 08:59:31 -0800830 KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700831 expected_true), \
832 fmt, \
833 ##__VA_ARGS__)
834
835#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
836 KUNIT_UNARY_ASSERTION(test, \
837 assert_type, \
838 condition, \
839 true, \
840 fmt, \
841 ##__VA_ARGS__)
842
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700843#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
844 KUNIT_UNARY_ASSERTION(test, \
845 assert_type, \
846 condition, \
847 false, \
848 fmt, \
849 ##__VA_ARGS__)
850
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700851/*
852 * A factory macro for defining the assertions and expectations for the basic
853 * comparisons defined for the built in types.
854 *
855 * Unfortunately, there is no common type that all types can be promoted to for
856 * which all the binary operators behave the same way as for the actual types
857 * (for example, there is no type that long long and unsigned long long can
858 * both be cast to where the comparison result is preserved for all values). So
859 * the best we can do is do the comparison in the original types and then coerce
860 * everything to long long for printing; this way, the comparison behaves
861 * correctly and the printed out value usually makes sense without
862 * interpretation, but can always be interpreted to figure out the actual
863 * value.
864 */
865#define KUNIT_BASE_BINARY_ASSERTION(test, \
866 assert_class, \
867 ASSERT_CLASS_INIT, \
868 assert_type, \
869 left, \
870 op, \
871 right, \
872 fmt, \
873 ...) \
874do { \
875 typeof(left) __left = (left); \
876 typeof(right) __right = (right); \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700877 \
878 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800879 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700880 __left op __right, \
881 assert_class, \
Daniel Latypov05a7da82022-01-13 08:59:31 -0800882 ASSERT_CLASS_INIT(#op, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700883 #left, \
884 __left, \
885 #right, \
886 __right), \
887 fmt, \
888 ##__VA_ARGS__); \
889} while (0)
890
891#define KUNIT_BASE_EQ_MSG_ASSERTION(test, \
892 assert_class, \
893 ASSERT_CLASS_INIT, \
894 assert_type, \
895 left, \
896 right, \
897 fmt, \
898 ...) \
899 KUNIT_BASE_BINARY_ASSERTION(test, \
900 assert_class, \
901 ASSERT_CLASS_INIT, \
902 assert_type, \
903 left, ==, right, \
904 fmt, \
905 ##__VA_ARGS__)
906
907#define KUNIT_BASE_NE_MSG_ASSERTION(test, \
908 assert_class, \
909 ASSERT_CLASS_INIT, \
910 assert_type, \
911 left, \
912 right, \
913 fmt, \
914 ...) \
915 KUNIT_BASE_BINARY_ASSERTION(test, \
916 assert_class, \
917 ASSERT_CLASS_INIT, \
918 assert_type, \
919 left, !=, right, \
920 fmt, \
921 ##__VA_ARGS__)
922
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700923#define KUNIT_BINARY_EQ_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
924 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
925 kunit_binary_assert, \
926 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
927 assert_type, \
928 left, \
929 right, \
930 fmt, \
931 ##__VA_ARGS__)
932
Daniel Latypov40f397772022-01-18 14:35:05 -0800933#define KUNIT_BINARY_INT_ASSERTION(test, \
934 assert_type, \
935 left, \
936 op, \
937 right, \
938 fmt, \
939 ...) \
940 KUNIT_BASE_BINARY_ASSERTION(test, \
941 kunit_binary_assert, \
942 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
943 assert_type, \
944 left, op, right, \
945 fmt, \
946 ##__VA_ARGS__)
947
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700948#define KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
949 assert_type, \
950 left, \
951 right, \
952 fmt, \
953 ...) \
954 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
955 kunit_binary_ptr_assert, \
956 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
957 assert_type, \
958 left, \
959 right, \
960 fmt, \
961 ##__VA_ARGS__)
962
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700963#define KUNIT_BINARY_NE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
964 KUNIT_BASE_NE_MSG_ASSERTION(test, \
965 kunit_binary_assert, \
966 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
967 assert_type, \
968 left, \
969 right, \
970 fmt, \
971 ##__VA_ARGS__)
972
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700973#define KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
974 assert_type, \
975 left, \
976 right, \
977 fmt, \
978 ...) \
979 KUNIT_BASE_NE_MSG_ASSERTION(test, \
980 kunit_binary_ptr_assert, \
981 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
982 assert_type, \
983 left, \
984 right, \
985 fmt, \
986 ##__VA_ARGS__)
987
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700988#define KUNIT_BINARY_STR_ASSERTION(test, \
989 assert_type, \
990 left, \
991 op, \
992 right, \
993 fmt, \
994 ...) \
995do { \
David Gow3747b5c2021-05-13 12:31:56 -0700996 const char *__left = (left); \
997 const char *__right = (right); \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700998 \
999 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -08001000 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001001 strcmp(__left, __right) op 0, \
1002 kunit_binary_str_assert, \
Daniel Latypov05a7da82022-01-13 08:59:31 -08001003 KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(#op, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001004 #left, \
1005 __left, \
1006 #right, \
1007 __right), \
1008 fmt, \
1009 ##__VA_ARGS__); \
1010} while (0)
1011
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001012#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1013 assert_type, \
1014 ptr, \
1015 fmt, \
1016 ...) \
1017do { \
1018 typeof(ptr) __ptr = (ptr); \
1019 \
1020 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -08001021 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001022 !IS_ERR_OR_NULL(__ptr), \
1023 kunit_ptr_not_err_assert, \
Daniel Latypov05a7da82022-01-13 08:59:31 -08001024 KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001025 __ptr), \
1026 fmt, \
1027 ##__VA_ARGS__); \
1028} while (0)
1029
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001030/**
1031 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
1032 * @test: The test context object.
1033 * @condition: an arbitrary boolean expression. The test fails when this does
1034 * not evaluate to true.
1035 *
1036 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
1037 * to fail when the specified condition is not met; however, it will not prevent
1038 * the test case from continuing to run; this is otherwise known as an
1039 * *expectation failure*.
1040 */
1041#define KUNIT_EXPECT_TRUE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001042 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001043
1044#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
1045 KUNIT_TRUE_MSG_ASSERTION(test, \
1046 KUNIT_EXPECTATION, \
1047 condition, \
1048 fmt, \
1049 ##__VA_ARGS__)
1050
1051/**
1052 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
1053 * @test: The test context object.
1054 * @condition: an arbitrary boolean expression. The test fails when this does
1055 * not evaluate to false.
1056 *
1057 * Sets an expectation that @condition evaluates to false. See
1058 * KUNIT_EXPECT_TRUE() for more information.
1059 */
1060#define KUNIT_EXPECT_FALSE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001061 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001062
1063#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
1064 KUNIT_FALSE_MSG_ASSERTION(test, \
1065 KUNIT_EXPECTATION, \
1066 condition, \
1067 fmt, \
1068 ##__VA_ARGS__)
1069
1070/**
1071 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
1072 * @test: The test context object.
1073 * @left: an arbitrary expression that evaluates to a primitive C type.
1074 * @right: an arbitrary expression that evaluates to a primitive C type.
1075 *
1076 * Sets an expectation that the values that @left and @right evaluate to are
1077 * equal. This is semantically equivalent to
1078 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1079 * more information.
1080 */
1081#define KUNIT_EXPECT_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001082 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001083
1084#define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
1085 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1086 KUNIT_EXPECTATION, \
1087 left, \
1088 right, \
1089 fmt, \
1090 ##__VA_ARGS__)
1091
1092/**
1093 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
1094 * @test: The test context object.
1095 * @left: an arbitrary expression that evaluates to a pointer.
1096 * @right: an arbitrary expression that evaluates to a pointer.
1097 *
1098 * Sets an expectation that the values that @left and @right evaluate to are
1099 * equal. This is semantically equivalent to
1100 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1101 * more information.
1102 */
1103#define KUNIT_EXPECT_PTR_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001104 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001105
1106#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1107 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1108 KUNIT_EXPECTATION, \
1109 left, \
1110 right, \
1111 fmt, \
1112 ##__VA_ARGS__)
1113
1114/**
1115 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
1116 * @test: The test context object.
1117 * @left: an arbitrary expression that evaluates to a primitive C type.
1118 * @right: an arbitrary expression that evaluates to a primitive C type.
1119 *
1120 * Sets an expectation that the values that @left and @right evaluate to are not
1121 * equal. This is semantically equivalent to
1122 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1123 * more information.
1124 */
1125#define KUNIT_EXPECT_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001126 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001127
1128#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
1129 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1130 KUNIT_EXPECTATION, \
1131 left, \
1132 right, \
1133 fmt, \
1134 ##__VA_ARGS__)
1135
1136/**
1137 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
1138 * @test: The test context object.
1139 * @left: an arbitrary expression that evaluates to a pointer.
1140 * @right: an arbitrary expression that evaluates to a pointer.
1141 *
1142 * Sets an expectation that the values that @left and @right evaluate to are not
1143 * equal. This is semantically equivalent to
1144 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1145 * more information.
1146 */
1147#define KUNIT_EXPECT_PTR_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001148 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001149
1150#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
1151 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1152 KUNIT_EXPECTATION, \
1153 left, \
1154 right, \
1155 fmt, \
1156 ##__VA_ARGS__)
1157
1158/**
1159 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
1160 * @test: The test context object.
1161 * @left: an arbitrary expression that evaluates to a primitive C type.
1162 * @right: an arbitrary expression that evaluates to a primitive C type.
1163 *
1164 * Sets an expectation that the value that @left evaluates to is less than the
1165 * value that @right evaluates to. This is semantically equivalent to
1166 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
1167 * more information.
1168 */
1169#define KUNIT_EXPECT_LT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001170 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001171
1172#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001173 KUNIT_BINARY_INT_ASSERTION(test, \
1174 KUNIT_EXPECTATION, \
1175 left, <, right, \
1176 fmt, \
1177 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001178
1179/**
1180 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
1181 * @test: The test context object.
1182 * @left: an arbitrary expression that evaluates to a primitive C type.
1183 * @right: an arbitrary expression that evaluates to a primitive C type.
1184 *
1185 * Sets an expectation that the value that @left evaluates to is less than or
1186 * equal to the value that @right evaluates to. Semantically this is equivalent
1187 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
1188 * more information.
1189 */
1190#define KUNIT_EXPECT_LE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001191 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001192
1193#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001194 KUNIT_BINARY_INT_ASSERTION(test, \
1195 KUNIT_ASSERTION, \
1196 left, <=, right, \
1197 fmt, \
1198 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001199
1200/**
1201 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
1202 * @test: The test context object.
1203 * @left: an arbitrary expression that evaluates to a primitive C type.
1204 * @right: an arbitrary expression that evaluates to a primitive C type.
1205 *
1206 * Sets an expectation that the value that @left evaluates to is greater than
1207 * the value that @right evaluates to. This is semantically equivalent to
1208 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
1209 * more information.
1210 */
1211#define KUNIT_EXPECT_GT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001212 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001213
1214#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001215 KUNIT_BINARY_INT_ASSERTION(test, \
1216 KUNIT_EXPECTATION, \
1217 left, >, right, \
1218 fmt, \
1219 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001220
1221/**
1222 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1223 * @test: The test context object.
1224 * @left: an arbitrary expression that evaluates to a primitive C type.
1225 * @right: an arbitrary expression that evaluates to a primitive C type.
1226 *
1227 * Sets an expectation that the value that @left evaluates to is greater than
1228 * the value that @right evaluates to. This is semantically equivalent to
1229 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1230 * more information.
1231 */
1232#define KUNIT_EXPECT_GE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001233 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001234
1235#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001236 KUNIT_BINARY_INT_ASSERTION(test, \
1237 KUNIT_EXPECTATION, \
1238 left, >=, right, \
1239 fmt, \
1240 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001241
1242/**
1243 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1244 * @test: The test context object.
1245 * @left: an arbitrary expression that evaluates to a null terminated string.
1246 * @right: an arbitrary expression that evaluates to a null terminated string.
1247 *
1248 * Sets an expectation that the values that @left and @right evaluate to are
1249 * equal. This is semantically equivalent to
1250 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1251 * for more information.
1252 */
1253#define KUNIT_EXPECT_STREQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001254 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001255
1256#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -08001257 KUNIT_BINARY_STR_ASSERTION(test, \
1258 KUNIT_EXPECTATION, \
1259 left, ==, right, \
1260 fmt, \
1261 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001262
1263/**
1264 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1265 * @test: The test context object.
1266 * @left: an arbitrary expression that evaluates to a null terminated string.
1267 * @right: an arbitrary expression that evaluates to a null terminated string.
1268 *
1269 * Sets an expectation that the values that @left and @right evaluate to are
1270 * not equal. This is semantically equivalent to
1271 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1272 * for more information.
1273 */
1274#define KUNIT_EXPECT_STRNEQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001275 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001276
1277#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -08001278 KUNIT_BINARY_STR_ASSERTION(test, \
1279 KUNIT_EXPECTATION, \
1280 left, !=, right, \
1281 fmt, \
1282 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001283
1284/**
1285 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1286 * @test: The test context object.
1287 * @ptr: an arbitrary pointer.
1288 *
1289 * Sets an expectation that the value that @ptr evaluates to is not null and not
1290 * an errno stored in a pointer. This is semantically equivalent to
1291 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1292 * more information.
1293 */
1294#define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001295 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001296
1297#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1298 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1299 KUNIT_EXPECTATION, \
1300 ptr, \
1301 fmt, \
1302 ##__VA_ARGS__)
1303
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001304#define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1305 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1306
1307/**
1308 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1309 * @test: The test context object.
1310 * @condition: an arbitrary boolean expression. The test fails and aborts when
1311 * this does not evaluate to true.
1312 *
1313 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1314 * fail *and immediately abort* when the specified condition is not met. Unlike
1315 * an expectation failure, it will prevent the test case from continuing to run;
1316 * this is otherwise known as an *assertion failure*.
1317 */
1318#define KUNIT_ASSERT_TRUE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001319 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001320
1321#define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1322 KUNIT_TRUE_MSG_ASSERTION(test, \
1323 KUNIT_ASSERTION, \
1324 condition, \
1325 fmt, \
1326 ##__VA_ARGS__)
1327
1328/**
1329 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1330 * @test: The test context object.
1331 * @condition: an arbitrary boolean expression.
1332 *
1333 * Sets an assertion that the value that @condition evaluates to is false. This
1334 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1335 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1336 */
1337#define KUNIT_ASSERT_FALSE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001338 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001339
1340#define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1341 KUNIT_FALSE_MSG_ASSERTION(test, \
1342 KUNIT_ASSERTION, \
1343 condition, \
1344 fmt, \
1345 ##__VA_ARGS__)
1346
1347/**
1348 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1349 * @test: The test context object.
1350 * @left: an arbitrary expression that evaluates to a primitive C type.
1351 * @right: an arbitrary expression that evaluates to a primitive C type.
1352 *
1353 * Sets an assertion that the values that @left and @right evaluate to are
1354 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1355 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1356 */
1357#define KUNIT_ASSERT_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001358 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001359
1360#define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1361 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1362 KUNIT_ASSERTION, \
1363 left, \
1364 right, \
1365 fmt, \
1366 ##__VA_ARGS__)
1367
1368/**
1369 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1370 * @test: The test context object.
1371 * @left: an arbitrary expression that evaluates to a pointer.
1372 * @right: an arbitrary expression that evaluates to a pointer.
1373 *
1374 * Sets an assertion that the values that @left and @right evaluate to are
1375 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1376 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1377 */
1378#define KUNIT_ASSERT_PTR_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001379 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001380
1381#define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1382 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1383 KUNIT_ASSERTION, \
1384 left, \
1385 right, \
1386 fmt, \
1387 ##__VA_ARGS__)
1388
1389/**
1390 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1391 * @test: The test context object.
1392 * @left: an arbitrary expression that evaluates to a primitive C type.
1393 * @right: an arbitrary expression that evaluates to a primitive C type.
1394 *
1395 * Sets an assertion that the values that @left and @right evaluate to are not
1396 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1397 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1398 */
1399#define KUNIT_ASSERT_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001400 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001401
1402#define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1403 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1404 KUNIT_ASSERTION, \
1405 left, \
1406 right, \
1407 fmt, \
1408 ##__VA_ARGS__)
1409
1410/**
1411 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1412 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1413 * @test: The test context object.
1414 * @left: an arbitrary expression that evaluates to a pointer.
1415 * @right: an arbitrary expression that evaluates to a pointer.
1416 *
1417 * Sets an assertion that the values that @left and @right evaluate to are not
1418 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1419 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1420 */
1421#define KUNIT_ASSERT_PTR_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001422 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001423
1424#define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1425 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1426 KUNIT_ASSERTION, \
1427 left, \
1428 right, \
1429 fmt, \
1430 ##__VA_ARGS__)
1431/**
1432 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1433 * @test: The test context object.
1434 * @left: an arbitrary expression that evaluates to a primitive C type.
1435 * @right: an arbitrary expression that evaluates to a primitive C type.
1436 *
1437 * Sets an assertion that the value that @left evaluates to is less than the
1438 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1439 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1440 * is not met.
1441 */
1442#define KUNIT_ASSERT_LT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001443 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001444
1445#define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001446 KUNIT_BINARY_INT_ASSERTION(test, \
1447 KUNIT_EXPECTATION, \
1448 left, <, right, \
1449 fmt, \
1450 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001451/**
1452 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1453 * @test: The test context object.
1454 * @left: an arbitrary expression that evaluates to a primitive C type.
1455 * @right: an arbitrary expression that evaluates to a primitive C type.
1456 *
1457 * Sets an assertion that the value that @left evaluates to is less than or
1458 * equal to the value that @right evaluates to. This is the same as
1459 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1460 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1461 */
1462#define KUNIT_ASSERT_LE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001463 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001464
1465#define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001466 KUNIT_BINARY_INT_ASSERTION(test, \
1467 KUNIT_ASSERTION, \
1468 left, <=, right, \
1469 fmt, \
1470 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001471
1472/**
1473 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1474 * @test: The test context object.
1475 * @left: an arbitrary expression that evaluates to a primitive C type.
1476 * @right: an arbitrary expression that evaluates to a primitive C type.
1477 *
1478 * Sets an assertion that the value that @left evaluates to is greater than the
1479 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1480 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1481 * is not met.
1482 */
1483#define KUNIT_ASSERT_GT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001484 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001485
1486#define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001487 KUNIT_BINARY_INT_ASSERTION(test, \
1488 KUNIT_EXPECTATION, \
1489 left, >, right, \
1490 fmt, \
1491 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001492
1493/**
1494 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1495 * @test: The test context object.
1496 * @left: an arbitrary expression that evaluates to a primitive C type.
1497 * @right: an arbitrary expression that evaluates to a primitive C type.
1498 *
1499 * Sets an assertion that the value that @left evaluates to is greater than the
1500 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1501 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1502 * is not met.
1503 */
1504#define KUNIT_ASSERT_GE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001505 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001506
1507#define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001508 KUNIT_BINARY_INT_ASSERTION(test, \
1509 KUNIT_ASSERTION, \
1510 left, >=, right, \
1511 fmt, \
1512 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001513
1514/**
1515 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1516 * @test: The test context object.
1517 * @left: an arbitrary expression that evaluates to a null terminated string.
1518 * @right: an arbitrary expression that evaluates to a null terminated string.
1519 *
1520 * Sets an assertion that the values that @left and @right evaluate to are
1521 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1522 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1523 */
1524#define KUNIT_ASSERT_STREQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001525 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001526
1527#define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -08001528 KUNIT_BINARY_STR_ASSERTION(test, \
1529 KUNIT_ASSERTION, \
1530 left, ==, right, \
1531 fmt, \
1532 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001533
1534/**
1535 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1536 * @test: The test context object.
1537 * @left: an arbitrary expression that evaluates to a null terminated string.
1538 * @right: an arbitrary expression that evaluates to a null terminated string.
1539 *
1540 * Sets an expectation that the values that @left and @right evaluate to are
1541 * not equal. This is semantically equivalent to
1542 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1543 * for more information.
1544 */
1545#define KUNIT_ASSERT_STRNEQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001546 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001547
1548#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -08001549 KUNIT_BINARY_STR_ASSERTION(test, \
1550 KUNIT_ASSERTION, \
1551 left, !=, right, \
1552 fmt, \
1553 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001554
1555/**
1556 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1557 * @test: The test context object.
1558 * @ptr: an arbitrary pointer.
1559 *
1560 * Sets an assertion that the value that @ptr evaluates to is not null and not
1561 * an errno stored in a pointer. This is the same as
1562 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1563 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1564 */
1565#define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001566 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001567
1568#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1569 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1570 KUNIT_ASSERTION, \
1571 ptr, \
1572 fmt, \
1573 ##__VA_ARGS__)
1574
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +05301575/**
1576 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1577 * @name: prefix for the test parameter generator function.
1578 * @array: array of test parameters.
1579 * @get_desc: function to convert param to description; NULL to use default
1580 *
1581 * Define function @name_gen_params which uses @array to generate parameters.
1582 */
1583#define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1584 static const void *name##_gen_params(const void *prev, char *desc) \
1585 { \
1586 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1587 if (__next - (array) < ARRAY_SIZE((array))) { \
1588 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1589 if (__get_desc) \
1590 __get_desc(__next, desc); \
1591 return __next; \
1592 } \
1593 return NULL; \
1594 }
1595
Brendan Higgins914cc632019-09-23 02:02:31 -07001596#endif /* _KUNIT_TEST_H */