blob: b26400731c021b72bf49fc315ab0ff6d6ce4ab4f [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 <brendanhiggins@google.com>
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
15#include <linux/container_of.h>
16#include <linux/err.h>
17#include <linux/init.h>
18#include <linux/kconfig.h>
19#include <linux/kref.h>
20#include <linux/list.h>
Alan Maguirec475c772020-01-06 22:28:20 +000021#include <linux/module.h>
Brendan Higgins0a7568532019-09-23 02:02:32 -070022#include <linux/slab.h>
Andy Shevchenkoec54c282021-11-08 18:32:15 -080023#include <linux/spinlock.h>
24#include <linux/string.h>
Brendan Higgins914cc632019-09-23 02:02:31 -070025#include <linux/types.h>
Andy Shevchenkoec54c282021-11-08 18:32:15 -080026
27#include <asm/rwonce.h>
Brendan Higgins914cc632019-09-23 02:02:31 -070028
Brendan Higgins0a7568532019-09-23 02:02:32 -070029struct kunit_resource;
30
31typedef int (*kunit_resource_init_t)(struct kunit_resource *, void *);
32typedef void (*kunit_resource_free_t)(struct kunit_resource *);
33
34/**
35 * struct kunit_resource - represents a *test managed resource*
Alan Maguired4cdd142020-05-29 22:46:20 +010036 * @data: for the user to store arbitrary data.
Mauro Carvalho Chehab38d9b902020-08-27 08:31:43 +020037 * @name: optional name
Brendan Higgins0a7568532019-09-23 02:02:32 -070038 * @free: a user supplied function to free the resource. Populated by
Alan Maguired4cdd142020-05-29 22:46:20 +010039 * kunit_resource_alloc().
Brendan Higgins0a7568532019-09-23 02:02:32 -070040 *
41 * Represents a *test managed resource*, a resource which will automatically be
42 * cleaned up at the end of a test case.
43 *
Alan Maguired4cdd142020-05-29 22:46:20 +010044 * Resources are reference counted so if a resource is retrieved via
45 * kunit_alloc_and_get_resource() or kunit_find_resource(), we need
46 * to call kunit_put_resource() to reduce the resource reference count
47 * when finished with it. Note that kunit_alloc_resource() does not require a
48 * kunit_resource_put() because it does not retrieve the resource itself.
49 *
Brendan Higgins0a7568532019-09-23 02:02:32 -070050 * Example:
51 *
52 * .. code-block:: c
53 *
54 * struct kunit_kmalloc_params {
55 * size_t size;
56 * gfp_t gfp;
57 * };
58 *
59 * static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
60 * {
61 * struct kunit_kmalloc_params *params = context;
Alan Maguired4cdd142020-05-29 22:46:20 +010062 * res->data = kmalloc(params->size, params->gfp);
Brendan Higgins0a7568532019-09-23 02:02:32 -070063 *
Alan Maguired4cdd142020-05-29 22:46:20 +010064 * if (!res->data)
Brendan Higgins0a7568532019-09-23 02:02:32 -070065 * return -ENOMEM;
66 *
67 * return 0;
68 * }
69 *
70 * static void kunit_kmalloc_free(struct kunit_resource *res)
71 * {
Alan Maguired4cdd142020-05-29 22:46:20 +010072 * kfree(res->data);
Brendan Higgins0a7568532019-09-23 02:02:32 -070073 * }
74 *
75 * void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
76 * {
77 * struct kunit_kmalloc_params params;
Brendan Higgins0a7568532019-09-23 02:02:32 -070078 *
79 * params.size = size;
80 * params.gfp = gfp;
81 *
Alan Maguired4cdd142020-05-29 22:46:20 +010082 * return kunit_alloc_resource(test, kunit_kmalloc_init,
Brendan Higgins0a7568532019-09-23 02:02:32 -070083 * kunit_kmalloc_free, &params);
Brendan Higgins0a7568532019-09-23 02:02:32 -070084 * }
Alan Maguire725aca92020-05-29 22:46:21 +010085 *
86 * Resources can also be named, with lookup/removal done on a name
87 * basis also. kunit_add_named_resource(), kunit_find_named_resource()
88 * and kunit_destroy_named_resource(). Resource names must be
89 * unique within the test instance.
Brendan Higgins0a7568532019-09-23 02:02:32 -070090 */
91struct kunit_resource {
Alan Maguired4cdd142020-05-29 22:46:20 +010092 void *data;
Mauro Carvalho Chehab38d9b902020-08-27 08:31:43 +020093 const char *name;
94 kunit_resource_free_t free;
Brendan Higgins0a7568532019-09-23 02:02:32 -070095
96 /* private: internal use only. */
Alan Maguired4cdd142020-05-29 22:46:20 +010097 struct kref refcount;
Brendan Higgins0a7568532019-09-23 02:02:32 -070098 struct list_head node;
99};
100
Brendan Higgins914cc632019-09-23 02:02:31 -0700101struct kunit;
102
Alan Maguiree2219db2020-03-26 14:25:07 +0000103/* Size of log associated with test. */
104#define KUNIT_LOG_SIZE 512
105
Arpitha Raghunandanfadb08e2020-11-16 11:10:35 +0530106/* Maximum size of parameter description string. */
107#define KUNIT_PARAM_DESC_SIZE 128
108
David Gow6d2426b2021-06-24 23:58:12 -0700109/* Maximum size of a status comment. */
110#define KUNIT_STATUS_COMMENT_SIZE 256
111
Alan Maguirec3bba692020-03-26 14:25:09 +0000112/*
113 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
114 * sub-subtest. See the "Subtests" section in
115 * https://node-tap.org/tap-protocol/
116 */
117#define KUNIT_SUBTEST_INDENT " "
118#define KUNIT_SUBSUBTEST_INDENT " "
119
Brendan Higgins914cc632019-09-23 02:02:31 -0700120/**
David Gow6d2426b2021-06-24 23:58:12 -0700121 * enum kunit_status - Type of result for a test or test suite
122 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
123 * @KUNIT_FAILURE: Denotes the test has failed.
124 * @KUNIT_SKIPPED: Denotes the test has been skipped.
125 */
126enum kunit_status {
127 KUNIT_SUCCESS,
128 KUNIT_FAILURE,
129 KUNIT_SKIPPED,
130};
131
132/**
Brendan Higgins914cc632019-09-23 02:02:31 -0700133 * struct kunit_case - represents an individual test case.
134 *
135 * @run_case: the function representing the actual test case.
136 * @name: the name of the test case.
Arpitha Raghunandanfadb08e2020-11-16 11:10:35 +0530137 * @generate_params: the generator function for parameterized tests.
Brendan Higgins914cc632019-09-23 02:02:31 -0700138 *
139 * A test case is a function with the signature,
Brendan Higginse4aea8f2019-09-23 02:02:41 -0700140 * ``void (*)(struct kunit *)``
141 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
142 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
Brendan Higgins914cc632019-09-23 02:02:31 -0700143 * with a &struct kunit_suite and will be run after the suite's init
144 * function and followed by the suite's exit function.
145 *
146 * A test case should be static and should only be created with the
147 * KUNIT_CASE() macro; additionally, every array of test cases should be
148 * terminated with an empty test case.
149 *
150 * Example:
151 *
152 * .. code-block:: c
153 *
154 * void add_test_basic(struct kunit *test)
155 * {
156 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
157 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
158 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
159 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
160 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
161 * }
162 *
163 * static struct kunit_case example_test_cases[] = {
164 * KUNIT_CASE(add_test_basic),
165 * {}
166 * };
167 *
168 */
169struct kunit_case {
170 void (*run_case)(struct kunit *test);
171 const char *name;
Arpitha Raghunandanfadb08e2020-11-16 11:10:35 +0530172 const void* (*generate_params)(const void *prev, char *desc);
Brendan Higgins914cc632019-09-23 02:02:31 -0700173
174 /* private: internal use only. */
David Gow6d2426b2021-06-24 23:58:12 -0700175 enum kunit_status status;
Alan Maguiree2219db2020-03-26 14:25:07 +0000176 char *log;
Brendan Higgins914cc632019-09-23 02:02:31 -0700177};
178
David Gow6d2426b2021-06-24 23:58:12 -0700179static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
Alan Maguiree2219db2020-03-26 14:25:07 +0000180{
David Gow6d2426b2021-06-24 23:58:12 -0700181 switch (status) {
182 case KUNIT_SKIPPED:
183 case KUNIT_SUCCESS:
184 return "ok";
185 case KUNIT_FAILURE:
186 return "not ok";
187 }
188 return "invalid";
Alan Maguiree2219db2020-03-26 14:25:07 +0000189}
190
Brendan Higgins914cc632019-09-23 02:02:31 -0700191/**
192 * KUNIT_CASE - A helper for creating a &struct kunit_case
193 *
194 * @test_name: a reference to a test case function.
195 *
196 * Takes a symbol for a function representing a test case and creates a
197 * &struct kunit_case object from it. See the documentation for
198 * &struct kunit_case for an example on how to use it.
199 */
200#define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
201
202/**
Arpitha Raghunandanfadb08e2020-11-16 11:10:35 +0530203 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
204 *
205 * @test_name: a reference to a test case function.
206 * @gen_params: a reference to a parameter generator function.
207 *
208 * The generator function::
209 *
210 * const void* gen_params(const void *prev, char *desc)
211 *
212 * is used to lazily generate a series of arbitrarily typed values that fit into
213 * a void*. The argument @prev is the previously returned value, which should be
214 * used to derive the next value; @prev is set to NULL on the initial generator
215 * call. When no more values are available, the generator must return NULL.
216 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
217 * describing the parameter.
218 */
219#define KUNIT_CASE_PARAM(test_name, gen_params) \
220 { .run_case = test_name, .name = #test_name, \
221 .generate_params = gen_params }
222
223/**
Brendan Higgins914cc632019-09-23 02:02:31 -0700224 * struct kunit_suite - describes a related collection of &struct kunit_case
225 *
226 * @name: the name of the test. Purely informational.
227 * @init: called before every test case.
228 * @exit: called after every test case.
229 * @test_cases: a null terminated array of test cases.
230 *
231 * A kunit_suite is a collection of related &struct kunit_case s, such that
232 * @init is called before every test case and @exit is called after every
233 * test case, similar to the notion of a *test fixture* or a *test class*
234 * in other unit testing frameworks like JUnit or Googletest.
235 *
236 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
237 * to run it.
238 */
239struct kunit_suite {
240 const char name[256];
241 int (*init)(struct kunit *test);
242 void (*exit)(struct kunit *test);
243 struct kunit_case *test_cases;
Alan Maguiree2219db2020-03-26 14:25:07 +0000244
Lothar Rubuschc4714b02020-04-15 20:16:53 +0000245 /* private: internal use only */
David Gow6d2426b2021-06-24 23:58:12 -0700246 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
Alan Maguiree2219db2020-03-26 14:25:07 +0000247 struct dentry *debugfs;
248 char *log;
Brendan Higgins914cc632019-09-23 02:02:31 -0700249};
250
251/**
252 * struct kunit - represents a running instance of a test.
253 *
254 * @priv: for user to store arbitrary data. Commonly used to pass data
255 * created in the init function (see &struct kunit_suite).
256 *
257 * Used to store information about the current context under which the test
258 * is running. Most of this data is private and should only be accessed
259 * indirectly via public functions; the one exception is @priv which can be
260 * used by the test writer to store arbitrary data.
261 */
262struct kunit {
263 void *priv;
264
265 /* private: internal use only. */
266 const char *name; /* Read only after initialization! */
Alan Maguiree2219db2020-03-26 14:25:07 +0000267 char *log; /* Points at case log after initialization */
Brendan Higgins5f3e0622019-09-23 02:02:39 -0700268 struct kunit_try_catch try_catch;
Arpitha Raghunandanfadb08e2020-11-16 11:10:35 +0530269 /* param_value is the current parameter value for a test case. */
270 const void *param_value;
271 /* param_index stores the index of the parameter in parameterized tests. */
272 int param_index;
Brendan Higgins914cc632019-09-23 02:02:31 -0700273 /*
274 * success starts as true, and may only be set to false during a
275 * test case; thus, it is safe to update this across multiple
276 * threads using WRITE_ONCE; however, as a consequence, it may only
277 * be read after the test case finishes once all threads associated
278 * with the test case have terminated.
279 */
Brendan Higgins0a7568532019-09-23 02:02:32 -0700280 spinlock_t lock; /* Guards all mutable test state. */
David Gow6d2426b2021-06-24 23:58:12 -0700281 enum kunit_status status; /* Read only after test_case finishes! */
Brendan Higgins0a7568532019-09-23 02:02:32 -0700282 /*
283 * Because resources is a list that may be updated multiple times (with
284 * new resources) from any thread associated with a test case, we must
285 * protect it with some type of lock.
286 */
287 struct list_head resources; /* Protected by lock. */
David Gow6d2426b2021-06-24 23:58:12 -0700288
289 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
Brendan Higgins914cc632019-09-23 02:02:31 -0700290};
291
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -0700292static inline void kunit_set_failure(struct kunit *test)
293{
David Gow6d2426b2021-06-24 23:58:12 -0700294 WRITE_ONCE(test->status, KUNIT_FAILURE);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -0700295}
296
Alan Maguiree2219db2020-03-26 14:25:07 +0000297void kunit_init_test(struct kunit *test, const char *name, char *log);
Brendan Higgins914cc632019-09-23 02:02:31 -0700298
299int kunit_run_tests(struct kunit_suite *suite);
300
Alan Maguiree2219db2020-03-26 14:25:07 +0000301size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
302
303unsigned int kunit_test_case_num(struct kunit_suite *suite,
304 struct kunit_case *test_case);
305
Alan Maguireaac35462020-08-04 13:47:42 -0700306int __kunit_test_suites_init(struct kunit_suite * const * const suites);
Alan Maguiree2219db2020-03-26 14:25:07 +0000307
308void __kunit_test_suites_exit(struct kunit_suite **suites);
309
Brendan Higgins8c0d8842020-08-04 13:47:43 -0700310#if IS_BUILTIN(CONFIG_KUNIT)
311int kunit_run_all_tests(void);
312#else
313static inline int kunit_run_all_tests(void)
314{
315 return 0;
316}
317#endif /* IS_BUILTIN(CONFIG_KUNIT) */
318
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200319#ifdef MODULE
Brendan Higgins914cc632019-09-23 02:02:31 -0700320/**
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200321 * kunit_test_suites_for_module() - used to register one or more
322 * &struct kunit_suite with KUnit.
Brendan Higgins914cc632019-09-23 02:02:31 -0700323 *
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200324 * @__suites: a statically allocated list of &struct kunit_suite.
Brendan Higgins914cc632019-09-23 02:02:31 -0700325 *
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200326 * Registers @__suites with the test framework. See &struct kunit_suite for
Brendan Higgins914cc632019-09-23 02:02:31 -0700327 * more information.
328 *
Alan Maguireaac35462020-08-04 13:47:42 -0700329 * If a test suite is built-in, module_init() gets translated into
330 * an initcall which we don't want as the idea is that for builtins
331 * the executor will manage execution. So ensure we do not define
332 * module_{init|exit} functions for the builtin case when registering
333 * suites via kunit_test_suites() below.
Brendan Higgins914cc632019-09-23 02:02:31 -0700334 */
Alan Maguireaac35462020-08-04 13:47:42 -0700335#define kunit_test_suites_for_module(__suites) \
336 static int __init kunit_test_suites_init(void) \
Alan Maguirec475c772020-01-06 22:28:20 +0000337 { \
Alan Maguireaac35462020-08-04 13:47:42 -0700338 return __kunit_test_suites_init(__suites); \
Alan Maguirec475c772020-01-06 22:28:20 +0000339 } \
Alan Maguireaac35462020-08-04 13:47:42 -0700340 module_init(kunit_test_suites_init); \
341 \
Alan Maguirec475c772020-01-06 22:28:20 +0000342 static void __exit kunit_test_suites_exit(void) \
343 { \
Alan Maguireaac35462020-08-04 13:47:42 -0700344 return __kunit_test_suites_exit(__suites); \
Alan Maguirec475c772020-01-06 22:28:20 +0000345 } \
346 module_exit(kunit_test_suites_exit)
Alan Maguireaac35462020-08-04 13:47:42 -0700347#else
348#define kunit_test_suites_for_module(__suites)
349#endif /* MODULE */
350
351#define __kunit_test_suites(unique_array, unique_suites, ...) \
352 static struct kunit_suite *unique_array[] = { __VA_ARGS__, NULL }; \
353 kunit_test_suites_for_module(unique_array); \
354 static struct kunit_suite **unique_suites \
Joe Perches33def842020-10-21 19:36:07 -0700355 __used __section(".kunit_test_suites") = unique_array
Alan Maguireaac35462020-08-04 13:47:42 -0700356
357/**
358 * kunit_test_suites() - used to register one or more &struct kunit_suite
359 * with KUnit.
360 *
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200361 * @__suites: a statically allocated list of &struct kunit_suite.
Alan Maguireaac35462020-08-04 13:47:42 -0700362 *
363 * Registers @suites with the test framework. See &struct kunit_suite for
364 * more information.
365 *
366 * When builtin, KUnit tests are all run via executor; this is done
367 * by placing the array of struct kunit_suite * in the .kunit_test_suites
368 * ELF section.
369 *
370 * An alternative is to build the tests as a module. Because modules do not
371 * support multiple initcall()s, we need to initialize an array of suites for a
372 * module.
373 *
374 */
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200375#define kunit_test_suites(__suites...) \
Alan Maguireaac35462020-08-04 13:47:42 -0700376 __kunit_test_suites(__UNIQUE_ID(array), \
377 __UNIQUE_ID(suites), \
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200378 ##__suites)
Alan Maguirec475c772020-01-06 22:28:20 +0000379
380#define kunit_test_suite(suite) kunit_test_suites(&suite)
Brendan Higgins914cc632019-09-23 02:02:31 -0700381
Alan Maguiree2219db2020-03-26 14:25:07 +0000382#define kunit_suite_for_each_test_case(suite, test_case) \
383 for (test_case = suite->test_cases; test_case->run_case; test_case++)
384
David Gow6d2426b2021-06-24 23:58:12 -0700385enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
Alan Maguiree2219db2020-03-26 14:25:07 +0000386
Brendan Higgins0a7568532019-09-23 02:02:32 -0700387/*
388 * Like kunit_alloc_resource() below, but returns the struct kunit_resource
389 * object that contains the allocation. This is mostly for testing purposes.
390 */
391struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
392 kunit_resource_init_t init,
393 kunit_resource_free_t free,
394 gfp_t internal_gfp,
395 void *context);
396
397/**
Alan Maguired4cdd142020-05-29 22:46:20 +0100398 * kunit_get_resource() - Hold resource for use. Should not need to be used
399 * by most users as we automatically get resources
400 * retrieved by kunit_find_resource*().
401 * @res: resource
402 */
403static inline void kunit_get_resource(struct kunit_resource *res)
404{
405 kref_get(&res->refcount);
406}
407
408/*
409 * Called when refcount reaches zero via kunit_put_resources();
410 * should not be called directly.
411 */
412static inline void kunit_release_resource(struct kref *kref)
413{
414 struct kunit_resource *res = container_of(kref, struct kunit_resource,
415 refcount);
416
417 /* If free function is defined, resource was dynamically allocated. */
418 if (res->free) {
419 res->free(res);
420 kfree(res);
421 }
422}
423
424/**
425 * kunit_put_resource() - When caller is done with retrieved resource,
426 * kunit_put_resource() should be called to drop
427 * reference count. The resource list maintains
428 * a reference count on resources, so if no users
429 * are utilizing a resource and it is removed from
430 * the resource list, it will be freed via the
431 * associated free function (if any). Only
432 * needs to be used if we alloc_and_get() or
433 * find() resource.
434 * @res: resource
435 */
436static inline void kunit_put_resource(struct kunit_resource *res)
437{
438 kref_put(&res->refcount, kunit_release_resource);
439}
440
441/**
442 * kunit_add_resource() - Add a *test managed resource*.
443 * @test: The test context object.
444 * @init: a user-supplied function to initialize the result (if needed). If
445 * none is supplied, the resource data value is simply set to @data.
446 * If an init function is supplied, @data is passed to it instead.
447 * @free: a user-supplied function to free the resource (if needed).
Mauro Carvalho Chehab38d9b902020-08-27 08:31:43 +0200448 * @res: The resource.
Alan Maguired4cdd142020-05-29 22:46:20 +0100449 * @data: value to pass to init function or set in resource data field.
450 */
451int kunit_add_resource(struct kunit *test,
452 kunit_resource_init_t init,
453 kunit_resource_free_t free,
454 struct kunit_resource *res,
455 void *data);
Alan Maguire725aca92020-05-29 22:46:21 +0100456
457/**
458 * kunit_add_named_resource() - Add a named *test managed resource*.
459 * @test: The test context object.
460 * @init: a user-supplied function to initialize the resource data, if needed.
461 * @free: a user-supplied function to free the resource data, if needed.
Mauro Carvalho Chehab38d9b902020-08-27 08:31:43 +0200462 * @res: The resource.
463 * @name: name to be set for resource.
464 * @data: value to pass to init function or set in resource data field.
Alan Maguire725aca92020-05-29 22:46:21 +0100465 */
466int kunit_add_named_resource(struct kunit *test,
467 kunit_resource_init_t init,
468 kunit_resource_free_t free,
469 struct kunit_resource *res,
470 const char *name,
471 void *data);
472
Alan Maguired4cdd142020-05-29 22:46:20 +0100473/**
Brendan Higgins0a7568532019-09-23 02:02:32 -0700474 * kunit_alloc_resource() - Allocates a *test managed resource*.
475 * @test: The test context object.
476 * @init: a user supplied function to initialize the resource.
477 * @free: a user supplied function to free the resource.
478 * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
479 * @context: for the user to pass in arbitrary data to the init function.
480 *
481 * Allocates a *test managed resource*, a resource which will automatically be
482 * cleaned up at the end of a test case. See &struct kunit_resource for an
483 * example.
484 *
Alan Maguired4cdd142020-05-29 22:46:20 +0100485 * Note: KUnit needs to allocate memory for a kunit_resource object. You must
Brendan Higgins0a7568532019-09-23 02:02:32 -0700486 * specify an @internal_gfp that is compatible with the use context of your
487 * resource.
488 */
489static inline void *kunit_alloc_resource(struct kunit *test,
490 kunit_resource_init_t init,
491 kunit_resource_free_t free,
492 gfp_t internal_gfp,
493 void *context)
494{
495 struct kunit_resource *res;
496
Alan Maguired4cdd142020-05-29 22:46:20 +0100497 res = kzalloc(sizeof(*res), internal_gfp);
498 if (!res)
499 return NULL;
Brendan Higgins0a7568532019-09-23 02:02:32 -0700500
Alan Maguired4cdd142020-05-29 22:46:20 +0100501 if (!kunit_add_resource(test, init, free, res, context))
502 return res->data;
Brendan Higgins0a7568532019-09-23 02:02:32 -0700503
504 return NULL;
505}
506
507typedef bool (*kunit_resource_match_t)(struct kunit *test,
Alan Maguired4cdd142020-05-29 22:46:20 +0100508 struct kunit_resource *res,
Brendan Higgins0a7568532019-09-23 02:02:32 -0700509 void *match_data);
510
511/**
512 * kunit_resource_instance_match() - Match a resource with the same instance.
513 * @test: Test case to which the resource belongs.
Alan Maguired4cdd142020-05-29 22:46:20 +0100514 * @res: The resource.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700515 * @match_data: The resource pointer to match against.
516 *
517 * An instance of kunit_resource_match_t that matches a resource whose
518 * allocation matches @match_data.
519 */
520static inline bool kunit_resource_instance_match(struct kunit *test,
Alan Maguired4cdd142020-05-29 22:46:20 +0100521 struct kunit_resource *res,
Brendan Higgins0a7568532019-09-23 02:02:32 -0700522 void *match_data)
523{
Alan Maguired4cdd142020-05-29 22:46:20 +0100524 return res->data == match_data;
Brendan Higgins0a7568532019-09-23 02:02:32 -0700525}
526
527/**
Alan Maguire725aca92020-05-29 22:46:21 +0100528 * kunit_resource_name_match() - Match a resource with the same name.
529 * @test: Test case to which the resource belongs.
530 * @res: The resource.
531 * @match_name: The name to match against.
532 */
533static inline bool kunit_resource_name_match(struct kunit *test,
534 struct kunit_resource *res,
535 void *match_name)
536{
537 return res->name && strcmp(res->name, match_name) == 0;
538}
539
540/**
Alan Maguired4cdd142020-05-29 22:46:20 +0100541 * kunit_find_resource() - Find a resource using match function/data.
542 * @test: Test case to which the resource belongs.
543 * @match: match function to be applied to resources/match data.
544 * @match_data: data to be used in matching.
545 */
546static inline struct kunit_resource *
547kunit_find_resource(struct kunit *test,
548 kunit_resource_match_t match,
549 void *match_data)
550{
551 struct kunit_resource *res, *found = NULL;
Vlastimil Babka26c6cb72021-06-28 19:34:30 -0700552 unsigned long flags;
Alan Maguired4cdd142020-05-29 22:46:20 +0100553
Vlastimil Babka26c6cb72021-06-28 19:34:30 -0700554 spin_lock_irqsave(&test->lock, flags);
Alan Maguired4cdd142020-05-29 22:46:20 +0100555
556 list_for_each_entry_reverse(res, &test->resources, node) {
557 if (match(test, res, (void *)match_data)) {
558 found = res;
559 kunit_get_resource(found);
560 break;
561 }
562 }
563
Vlastimil Babka26c6cb72021-06-28 19:34:30 -0700564 spin_unlock_irqrestore(&test->lock, flags);
Alan Maguired4cdd142020-05-29 22:46:20 +0100565
566 return found;
567}
568
569/**
Alan Maguire725aca92020-05-29 22:46:21 +0100570 * kunit_find_named_resource() - Find a resource using match name.
571 * @test: Test case to which the resource belongs.
572 * @name: match name.
573 */
574static inline struct kunit_resource *
575kunit_find_named_resource(struct kunit *test,
576 const char *name)
577{
578 return kunit_find_resource(test, kunit_resource_name_match,
579 (void *)name);
580}
581
582/**
Alan Maguired4cdd142020-05-29 22:46:20 +0100583 * kunit_destroy_resource() - Find a kunit_resource and destroy it.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700584 * @test: Test case to which the resource belongs.
585 * @match: Match function. Returns whether a given resource matches @match_data.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700586 * @match_data: Data passed into @match.
587 *
Brendan Higgins0a7568532019-09-23 02:02:32 -0700588 * RETURNS:
589 * 0 if kunit_resource is found and freed, -ENOENT if not found.
590 */
Alan Maguired4cdd142020-05-29 22:46:20 +0100591int kunit_destroy_resource(struct kunit *test,
Brendan Higgins0a7568532019-09-23 02:02:32 -0700592 kunit_resource_match_t match,
Brendan Higgins0a7568532019-09-23 02:02:32 -0700593 void *match_data);
594
Alan Maguire725aca92020-05-29 22:46:21 +0100595static inline int kunit_destroy_named_resource(struct kunit *test,
596 const char *name)
597{
598 return kunit_destroy_resource(test, kunit_resource_name_match,
599 (void *)name);
600}
601
Brendan Higgins0a7568532019-09-23 02:02:32 -0700602/**
Mauro Carvalho Chehab623050a2020-09-09 12:07:49 +0200603 * kunit_remove_resource() - remove resource from resource list associated with
604 * test.
Alan Maguired4cdd142020-05-29 22:46:20 +0100605 * @test: The test context object.
606 * @res: The resource to be removed.
607 *
608 * Note that the resource will not be immediately freed since it is likely
609 * the caller has a reference to it via alloc_and_get() or find();
610 * in this case a final call to kunit_put_resource() is required.
611 */
612void kunit_remove_resource(struct kunit *test, struct kunit_resource *res);
613
614/**
Daniel Latypov7122deb2021-05-03 13:58:34 -0700615 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
616 * @test: The test context object.
617 * @n: number of elements.
618 * @size: The size in bytes of the desired memory.
619 * @gfp: flags passed to underlying kmalloc().
620 *
621 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
622 * and is automatically cleaned up after the test case concludes. See &struct
623 * kunit_resource for more information.
624 */
Daniel Latypov361b57d2021-10-05 13:46:32 -0700625void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
Daniel Latypov7122deb2021-05-03 13:58:34 -0700626
627/**
Brendan Higgins0a7568532019-09-23 02:02:32 -0700628 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
629 * @test: The test context object.
630 * @size: The size in bytes of the desired memory.
631 * @gfp: flags passed to underlying kmalloc().
632 *
Daniel Latypov7122deb2021-05-03 13:58:34 -0700633 * See kmalloc() and kunit_kmalloc_array() for more information.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700634 */
Daniel Latypov7122deb2021-05-03 13:58:34 -0700635static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
636{
637 return kunit_kmalloc_array(test, 1, size, gfp);
638}
Brendan Higgins0a7568532019-09-23 02:02:32 -0700639
640/**
641 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
642 * @test: The test case to which the resource belongs.
643 * @ptr: The memory allocation to free.
644 */
645void kunit_kfree(struct kunit *test, const void *ptr);
646
647/**
648 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
649 * @test: The test context object.
650 * @size: The size in bytes of the desired memory.
651 * @gfp: flags passed to underlying kmalloc().
652 *
Daniel Latypov7122deb2021-05-03 13:58:34 -0700653 * See kzalloc() and kunit_kmalloc_array() for more information.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700654 */
655static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
656{
657 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
658}
659
Daniel Latypov7122deb2021-05-03 13:58:34 -0700660/**
661 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
662 * @test: The test context object.
663 * @n: number of elements.
664 * @size: The size in bytes of the desired memory.
665 * @gfp: flags passed to underlying kmalloc().
666 *
667 * See kcalloc() and kunit_kmalloc_array() for more information.
668 */
Daniel Latypov361b57d2021-10-05 13:46:32 -0700669static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
Daniel Latypov7122deb2021-05-03 13:58:34 -0700670{
Daniel Latypov361b57d2021-10-05 13:46:32 -0700671 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
Daniel Latypov7122deb2021-05-03 13:58:34 -0700672}
673
Brendan Higgins0a7568532019-09-23 02:02:32 -0700674void kunit_cleanup(struct kunit *test);
675
David Gow44acdbb2021-05-13 13:03:50 -0700676void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
Alan Maguiree2219db2020-03-26 14:25:07 +0000677
David Gow6d2426b2021-06-24 23:58:12 -0700678/**
679 * kunit_mark_skipped() - Marks @test_or_suite as skipped
680 *
681 * @test_or_suite: The test context object.
682 * @fmt: A printk() style format string.
683 *
684 * Marks the test as skipped. @fmt is given output as the test status
685 * comment, typically the reason the test was skipped.
686 *
687 * Test execution continues after kunit_mark_skipped() is called.
688 */
689#define kunit_mark_skipped(test_or_suite, fmt, ...) \
690 do { \
691 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
692 scnprintf((test_or_suite)->status_comment, \
693 KUNIT_STATUS_COMMENT_SIZE, \
694 fmt, ##__VA_ARGS__); \
695 } while (0)
696
697/**
698 * kunit_skip() - Marks @test_or_suite as skipped
699 *
700 * @test_or_suite: The test context object.
701 * @fmt: A printk() style format string.
702 *
703 * Skips the test. @fmt is given output as the test status
704 * comment, typically the reason the test was skipped.
705 *
706 * Test execution is halted after kunit_skip() is called.
707 */
708#define kunit_skip(test_or_suite, fmt, ...) \
709 do { \
710 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
711 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
712 } while (0)
Alan Maguiree2219db2020-03-26 14:25:07 +0000713
714/*
715 * printk and log to per-test or per-suite log buffer. Logging only done
716 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
717 */
718#define kunit_log(lvl, test_or_suite, fmt, ...) \
719 do { \
720 printk(lvl fmt, ##__VA_ARGS__); \
721 kunit_log_append((test_or_suite)->log, fmt "\n", \
722 ##__VA_ARGS__); \
723 } while (0)
724
725#define kunit_printk(lvl, test, fmt, ...) \
Alan Maguirec3bba692020-03-26 14:25:09 +0000726 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
727 (test)->name, ##__VA_ARGS__)
Brendan Higgins914cc632019-09-23 02:02:31 -0700728
729/**
730 * kunit_info() - Prints an INFO level message associated with @test.
731 *
732 * @test: The test context object.
733 * @fmt: A printk() style format string.
734 *
735 * Prints an info level message associated with the test suite being run.
736 * Takes a variable number of format parameters just like printk().
737 */
738#define kunit_info(test, fmt, ...) \
739 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
740
741/**
742 * kunit_warn() - Prints a WARN level message associated with @test.
743 *
744 * @test: The test context object.
745 * @fmt: A printk() style format string.
746 *
747 * Prints a warning level message.
748 */
749#define kunit_warn(test, fmt, ...) \
750 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
751
752/**
753 * kunit_err() - Prints an ERROR level message associated with @test.
754 *
755 * @test: The test context object.
756 * @fmt: A printk() style format string.
757 *
758 * Prints an error level message.
759 */
760#define kunit_err(test, fmt, ...) \
761 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
762
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700763/**
764 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
765 * @test: The test context object.
766 *
767 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
768 * words, it does nothing and only exists for code clarity. See
769 * KUNIT_EXPECT_TRUE() for more information.
770 */
771#define KUNIT_SUCCEED(test) do {} while (0)
772
773void kunit_do_assertion(struct kunit *test,
774 struct kunit_assert *assert,
775 bool pass,
776 const char *fmt, ...);
777
778#define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do { \
779 struct assert_class __assertion = INITIALIZER; \
780 kunit_do_assertion(test, \
781 &__assertion.assert, \
782 pass, \
783 fmt, \
784 ##__VA_ARGS__); \
785} while (0)
786
787
788#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
789 KUNIT_ASSERTION(test, \
790 false, \
791 kunit_fail_assert, \
792 KUNIT_INIT_FAIL_ASSERT_STRUCT(test, assert_type), \
793 fmt, \
794 ##__VA_ARGS__)
795
796/**
797 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
798 * @test: The test context object.
799 * @fmt: an informational message to be printed when the assertion is made.
800 * @...: string format arguments.
801 *
802 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
803 * other words, it always results in a failed expectation, and consequently
804 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
805 * for more information.
806 */
807#define KUNIT_FAIL(test, fmt, ...) \
808 KUNIT_FAIL_ASSERTION(test, \
809 KUNIT_EXPECTATION, \
810 fmt, \
811 ##__VA_ARGS__)
812
813#define KUNIT_UNARY_ASSERTION(test, \
814 assert_type, \
815 condition, \
816 expected_true, \
817 fmt, \
818 ...) \
819 KUNIT_ASSERTION(test, \
820 !!(condition) == !!expected_true, \
821 kunit_unary_assert, \
822 KUNIT_INIT_UNARY_ASSERT_STRUCT(test, \
823 assert_type, \
824 #condition, \
825 expected_true), \
826 fmt, \
827 ##__VA_ARGS__)
828
829#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
830 KUNIT_UNARY_ASSERTION(test, \
831 assert_type, \
832 condition, \
833 true, \
834 fmt, \
835 ##__VA_ARGS__)
836
837#define KUNIT_TRUE_ASSERTION(test, assert_type, condition) \
838 KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, NULL)
839
840#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
841 KUNIT_UNARY_ASSERTION(test, \
842 assert_type, \
843 condition, \
844 false, \
845 fmt, \
846 ##__VA_ARGS__)
847
848#define KUNIT_FALSE_ASSERTION(test, assert_type, condition) \
849 KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, NULL)
850
851/*
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, \
879 __left op __right, \
880 assert_class, \
881 ASSERT_CLASS_INIT(test, \
882 assert_type, \
883 #op, \
884 #left, \
885 __left, \
886 #right, \
887 __right), \
888 fmt, \
889 ##__VA_ARGS__); \
890} while (0)
891
892#define KUNIT_BASE_EQ_MSG_ASSERTION(test, \
893 assert_class, \
894 ASSERT_CLASS_INIT, \
895 assert_type, \
896 left, \
897 right, \
898 fmt, \
899 ...) \
900 KUNIT_BASE_BINARY_ASSERTION(test, \
901 assert_class, \
902 ASSERT_CLASS_INIT, \
903 assert_type, \
904 left, ==, right, \
905 fmt, \
906 ##__VA_ARGS__)
907
908#define KUNIT_BASE_NE_MSG_ASSERTION(test, \
909 assert_class, \
910 ASSERT_CLASS_INIT, \
911 assert_type, \
912 left, \
913 right, \
914 fmt, \
915 ...) \
916 KUNIT_BASE_BINARY_ASSERTION(test, \
917 assert_class, \
918 ASSERT_CLASS_INIT, \
919 assert_type, \
920 left, !=, right, \
921 fmt, \
922 ##__VA_ARGS__)
923
924#define KUNIT_BASE_LT_MSG_ASSERTION(test, \
925 assert_class, \
926 ASSERT_CLASS_INIT, \
927 assert_type, \
928 left, \
929 right, \
930 fmt, \
931 ...) \
932 KUNIT_BASE_BINARY_ASSERTION(test, \
933 assert_class, \
934 ASSERT_CLASS_INIT, \
935 assert_type, \
936 left, <, right, \
937 fmt, \
938 ##__VA_ARGS__)
939
940#define KUNIT_BASE_LE_MSG_ASSERTION(test, \
941 assert_class, \
942 ASSERT_CLASS_INIT, \
943 assert_type, \
944 left, \
945 right, \
946 fmt, \
947 ...) \
948 KUNIT_BASE_BINARY_ASSERTION(test, \
949 assert_class, \
950 ASSERT_CLASS_INIT, \
951 assert_type, \
952 left, <=, right, \
953 fmt, \
954 ##__VA_ARGS__)
955
956#define KUNIT_BASE_GT_MSG_ASSERTION(test, \
957 assert_class, \
958 ASSERT_CLASS_INIT, \
959 assert_type, \
960 left, \
961 right, \
962 fmt, \
963 ...) \
964 KUNIT_BASE_BINARY_ASSERTION(test, \
965 assert_class, \
966 ASSERT_CLASS_INIT, \
967 assert_type, \
968 left, >, right, \
969 fmt, \
970 ##__VA_ARGS__)
971
972#define KUNIT_BASE_GE_MSG_ASSERTION(test, \
973 assert_class, \
974 ASSERT_CLASS_INIT, \
975 assert_type, \
976 left, \
977 right, \
978 fmt, \
979 ...) \
980 KUNIT_BASE_BINARY_ASSERTION(test, \
981 assert_class, \
982 ASSERT_CLASS_INIT, \
983 assert_type, \
984 left, >=, right, \
985 fmt, \
986 ##__VA_ARGS__)
987
988#define KUNIT_BINARY_EQ_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
989 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
990 kunit_binary_assert, \
991 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
992 assert_type, \
993 left, \
994 right, \
995 fmt, \
996 ##__VA_ARGS__)
997
998#define KUNIT_BINARY_EQ_ASSERTION(test, assert_type, left, right) \
999 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1000 assert_type, \
1001 left, \
1002 right, \
1003 NULL)
1004
1005#define KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1006 assert_type, \
1007 left, \
1008 right, \
1009 fmt, \
1010 ...) \
1011 KUNIT_BASE_EQ_MSG_ASSERTION(test, \
1012 kunit_binary_ptr_assert, \
1013 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1014 assert_type, \
1015 left, \
1016 right, \
1017 fmt, \
1018 ##__VA_ARGS__)
1019
1020#define KUNIT_BINARY_PTR_EQ_ASSERTION(test, assert_type, left, right) \
1021 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1022 assert_type, \
1023 left, \
1024 right, \
1025 NULL)
1026
1027#define KUNIT_BINARY_NE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1028 KUNIT_BASE_NE_MSG_ASSERTION(test, \
1029 kunit_binary_assert, \
1030 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1031 assert_type, \
1032 left, \
1033 right, \
1034 fmt, \
1035 ##__VA_ARGS__)
1036
1037#define KUNIT_BINARY_NE_ASSERTION(test, assert_type, left, right) \
1038 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1039 assert_type, \
1040 left, \
1041 right, \
1042 NULL)
1043
1044#define KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1045 assert_type, \
1046 left, \
1047 right, \
1048 fmt, \
1049 ...) \
1050 KUNIT_BASE_NE_MSG_ASSERTION(test, \
1051 kunit_binary_ptr_assert, \
1052 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1053 assert_type, \
1054 left, \
1055 right, \
1056 fmt, \
1057 ##__VA_ARGS__)
1058
1059#define KUNIT_BINARY_PTR_NE_ASSERTION(test, assert_type, left, right) \
1060 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1061 assert_type, \
1062 left, \
1063 right, \
1064 NULL)
1065
1066#define KUNIT_BINARY_LT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1067 KUNIT_BASE_LT_MSG_ASSERTION(test, \
1068 kunit_binary_assert, \
1069 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1070 assert_type, \
1071 left, \
1072 right, \
1073 fmt, \
1074 ##__VA_ARGS__)
1075
1076#define KUNIT_BINARY_LT_ASSERTION(test, assert_type, left, right) \
1077 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1078 assert_type, \
1079 left, \
1080 right, \
1081 NULL)
1082
1083#define KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
1084 assert_type, \
1085 left, \
1086 right, \
1087 fmt, \
1088 ...) \
1089 KUNIT_BASE_LT_MSG_ASSERTION(test, \
1090 kunit_binary_ptr_assert, \
1091 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1092 assert_type, \
1093 left, \
1094 right, \
1095 fmt, \
1096 ##__VA_ARGS__)
1097
1098#define KUNIT_BINARY_PTR_LT_ASSERTION(test, assert_type, left, right) \
1099 KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
1100 assert_type, \
1101 left, \
1102 right, \
1103 NULL)
1104
1105#define KUNIT_BINARY_LE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1106 KUNIT_BASE_LE_MSG_ASSERTION(test, \
1107 kunit_binary_assert, \
1108 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1109 assert_type, \
1110 left, \
1111 right, \
1112 fmt, \
1113 ##__VA_ARGS__)
1114
1115#define KUNIT_BINARY_LE_ASSERTION(test, assert_type, left, right) \
1116 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1117 assert_type, \
1118 left, \
1119 right, \
1120 NULL)
1121
1122#define KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
1123 assert_type, \
1124 left, \
1125 right, \
1126 fmt, \
1127 ...) \
1128 KUNIT_BASE_LE_MSG_ASSERTION(test, \
1129 kunit_binary_ptr_assert, \
1130 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1131 assert_type, \
1132 left, \
1133 right, \
1134 fmt, \
1135 ##__VA_ARGS__)
1136
1137#define KUNIT_BINARY_PTR_LE_ASSERTION(test, assert_type, left, right) \
1138 KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
1139 assert_type, \
1140 left, \
1141 right, \
1142 NULL)
1143
1144#define KUNIT_BINARY_GT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1145 KUNIT_BASE_GT_MSG_ASSERTION(test, \
1146 kunit_binary_assert, \
1147 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1148 assert_type, \
1149 left, \
1150 right, \
1151 fmt, \
1152 ##__VA_ARGS__)
1153
1154#define KUNIT_BINARY_GT_ASSERTION(test, assert_type, left, right) \
1155 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1156 assert_type, \
1157 left, \
1158 right, \
1159 NULL)
1160
1161#define KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
1162 assert_type, \
1163 left, \
1164 right, \
1165 fmt, \
1166 ...) \
1167 KUNIT_BASE_GT_MSG_ASSERTION(test, \
1168 kunit_binary_ptr_assert, \
1169 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1170 assert_type, \
1171 left, \
1172 right, \
1173 fmt, \
1174 ##__VA_ARGS__)
1175
1176#define KUNIT_BINARY_PTR_GT_ASSERTION(test, assert_type, left, right) \
1177 KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
1178 assert_type, \
1179 left, \
1180 right, \
1181 NULL)
1182
1183#define KUNIT_BINARY_GE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
1184 KUNIT_BASE_GE_MSG_ASSERTION(test, \
1185 kunit_binary_assert, \
1186 KUNIT_INIT_BINARY_ASSERT_STRUCT, \
1187 assert_type, \
1188 left, \
1189 right, \
1190 fmt, \
1191 ##__VA_ARGS__)
1192
1193#define KUNIT_BINARY_GE_ASSERTION(test, assert_type, left, right) \
1194 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1195 assert_type, \
1196 left, \
1197 right, \
1198 NULL)
1199
1200#define KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
1201 assert_type, \
1202 left, \
1203 right, \
1204 fmt, \
1205 ...) \
1206 KUNIT_BASE_GE_MSG_ASSERTION(test, \
1207 kunit_binary_ptr_assert, \
1208 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
1209 assert_type, \
1210 left, \
1211 right, \
1212 fmt, \
1213 ##__VA_ARGS__)
1214
1215#define KUNIT_BINARY_PTR_GE_ASSERTION(test, assert_type, left, right) \
1216 KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
1217 assert_type, \
1218 left, \
1219 right, \
1220 NULL)
1221
1222#define KUNIT_BINARY_STR_ASSERTION(test, \
1223 assert_type, \
1224 left, \
1225 op, \
1226 right, \
1227 fmt, \
1228 ...) \
1229do { \
David Gow3747b5c2021-05-13 12:31:56 -07001230 const char *__left = (left); \
1231 const char *__right = (right); \
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001232 \
1233 KUNIT_ASSERTION(test, \
1234 strcmp(__left, __right) op 0, \
1235 kunit_binary_str_assert, \
Daniel Latypov3084db02020-11-02 15:23:04 -08001236 KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(test, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -07001237 assert_type, \
1238 #op, \
1239 #left, \
1240 __left, \
1241 #right, \
1242 __right), \
1243 fmt, \
1244 ##__VA_ARGS__); \
1245} while (0)
1246
1247#define KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1248 assert_type, \
1249 left, \
1250 right, \
1251 fmt, \
1252 ...) \
1253 KUNIT_BINARY_STR_ASSERTION(test, \
1254 assert_type, \
1255 left, ==, right, \
1256 fmt, \
1257 ##__VA_ARGS__)
1258
1259#define KUNIT_BINARY_STR_EQ_ASSERTION(test, assert_type, left, right) \
1260 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1261 assert_type, \
1262 left, \
1263 right, \
1264 NULL)
1265
1266#define KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1267 assert_type, \
1268 left, \
1269 right, \
1270 fmt, \
1271 ...) \
1272 KUNIT_BINARY_STR_ASSERTION(test, \
1273 assert_type, \
1274 left, !=, right, \
1275 fmt, \
1276 ##__VA_ARGS__)
1277
1278#define KUNIT_BINARY_STR_NE_ASSERTION(test, assert_type, left, right) \
1279 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1280 assert_type, \
1281 left, \
1282 right, \
1283 NULL)
1284
1285#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1286 assert_type, \
1287 ptr, \
1288 fmt, \
1289 ...) \
1290do { \
1291 typeof(ptr) __ptr = (ptr); \
1292 \
1293 KUNIT_ASSERTION(test, \
1294 !IS_ERR_OR_NULL(__ptr), \
1295 kunit_ptr_not_err_assert, \
1296 KUNIT_INIT_PTR_NOT_ERR_STRUCT(test, \
1297 assert_type, \
1298 #ptr, \
1299 __ptr), \
1300 fmt, \
1301 ##__VA_ARGS__); \
1302} while (0)
1303
1304#define KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, assert_type, ptr) \
1305 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1306 assert_type, \
1307 ptr, \
1308 NULL)
1309
1310/**
1311 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
1312 * @test: The test context object.
1313 * @condition: an arbitrary boolean expression. The test fails when this does
1314 * not evaluate to true.
1315 *
1316 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
1317 * to fail when the specified condition is not met; however, it will not prevent
1318 * the test case from continuing to run; this is otherwise known as an
1319 * *expectation failure*.
1320 */
1321#define KUNIT_EXPECT_TRUE(test, condition) \
1322 KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)
1323
1324#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
1325 KUNIT_TRUE_MSG_ASSERTION(test, \
1326 KUNIT_EXPECTATION, \
1327 condition, \
1328 fmt, \
1329 ##__VA_ARGS__)
1330
1331/**
1332 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
1333 * @test: The test context object.
1334 * @condition: an arbitrary boolean expression. The test fails when this does
1335 * not evaluate to false.
1336 *
1337 * Sets an expectation that @condition evaluates to false. See
1338 * KUNIT_EXPECT_TRUE() for more information.
1339 */
1340#define KUNIT_EXPECT_FALSE(test, condition) \
1341 KUNIT_FALSE_ASSERTION(test, KUNIT_EXPECTATION, condition)
1342
1343#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
1344 KUNIT_FALSE_MSG_ASSERTION(test, \
1345 KUNIT_EXPECTATION, \
1346 condition, \
1347 fmt, \
1348 ##__VA_ARGS__)
1349
1350/**
1351 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
1352 * @test: The test context object.
1353 * @left: an arbitrary expression that evaluates to a primitive C type.
1354 * @right: an arbitrary expression that evaluates to a primitive C type.
1355 *
1356 * Sets an expectation that the values that @left and @right evaluate to are
1357 * equal. This is semantically equivalent to
1358 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1359 * more information.
1360 */
1361#define KUNIT_EXPECT_EQ(test, left, right) \
1362 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1363
1364#define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
1365 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1366 KUNIT_EXPECTATION, \
1367 left, \
1368 right, \
1369 fmt, \
1370 ##__VA_ARGS__)
1371
1372/**
1373 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
1374 * @test: The test context object.
1375 * @left: an arbitrary expression that evaluates to a pointer.
1376 * @right: an arbitrary expression that evaluates to a pointer.
1377 *
1378 * Sets an expectation that the values that @left and @right evaluate to are
1379 * equal. This is semantically equivalent to
1380 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1381 * more information.
1382 */
1383#define KUNIT_EXPECT_PTR_EQ(test, left, right) \
1384 KUNIT_BINARY_PTR_EQ_ASSERTION(test, \
1385 KUNIT_EXPECTATION, \
1386 left, \
1387 right)
1388
1389#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1390 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1391 KUNIT_EXPECTATION, \
1392 left, \
1393 right, \
1394 fmt, \
1395 ##__VA_ARGS__)
1396
1397/**
1398 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
1399 * @test: The test context object.
1400 * @left: an arbitrary expression that evaluates to a primitive C type.
1401 * @right: an arbitrary expression that evaluates to a primitive C type.
1402 *
1403 * Sets an expectation that the values that @left and @right evaluate to are not
1404 * equal. This is semantically equivalent to
1405 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1406 * more information.
1407 */
1408#define KUNIT_EXPECT_NE(test, left, right) \
1409 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1410
1411#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
1412 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1413 KUNIT_EXPECTATION, \
1414 left, \
1415 right, \
1416 fmt, \
1417 ##__VA_ARGS__)
1418
1419/**
1420 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
1421 * @test: The test context object.
1422 * @left: an arbitrary expression that evaluates to a pointer.
1423 * @right: an arbitrary expression that evaluates to a pointer.
1424 *
1425 * Sets an expectation that the values that @left and @right evaluate to are not
1426 * equal. This is semantically equivalent to
1427 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1428 * more information.
1429 */
1430#define KUNIT_EXPECT_PTR_NE(test, left, right) \
1431 KUNIT_BINARY_PTR_NE_ASSERTION(test, \
1432 KUNIT_EXPECTATION, \
1433 left, \
1434 right)
1435
1436#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
1437 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1438 KUNIT_EXPECTATION, \
1439 left, \
1440 right, \
1441 fmt, \
1442 ##__VA_ARGS__)
1443
1444/**
1445 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
1446 * @test: The test context object.
1447 * @left: an arbitrary expression that evaluates to a primitive C type.
1448 * @right: an arbitrary expression that evaluates to a primitive C type.
1449 *
1450 * Sets an expectation that the value that @left evaluates to is less than the
1451 * value that @right evaluates to. This is semantically equivalent to
1452 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
1453 * more information.
1454 */
1455#define KUNIT_EXPECT_LT(test, left, right) \
1456 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1457
1458#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
1459 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1460 KUNIT_EXPECTATION, \
1461 left, \
1462 right, \
1463 fmt, \
1464 ##__VA_ARGS__)
1465
1466/**
1467 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
1468 * @test: The test context object.
1469 * @left: an arbitrary expression that evaluates to a primitive C type.
1470 * @right: an arbitrary expression that evaluates to a primitive C type.
1471 *
1472 * Sets an expectation that the value that @left evaluates to is less than or
1473 * equal to the value that @right evaluates to. Semantically this is equivalent
1474 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
1475 * more information.
1476 */
1477#define KUNIT_EXPECT_LE(test, left, right) \
1478 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1479
1480#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
1481 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1482 KUNIT_EXPECTATION, \
1483 left, \
1484 right, \
1485 fmt, \
1486 ##__VA_ARGS__)
1487
1488/**
1489 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
1490 * @test: The test context object.
1491 * @left: an arbitrary expression that evaluates to a primitive C type.
1492 * @right: an arbitrary expression that evaluates to a primitive C type.
1493 *
1494 * Sets an expectation that the value that @left evaluates to is greater than
1495 * the value that @right evaluates to. This is semantically equivalent to
1496 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
1497 * more information.
1498 */
1499#define KUNIT_EXPECT_GT(test, left, right) \
1500 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1501
1502#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
1503 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1504 KUNIT_EXPECTATION, \
1505 left, \
1506 right, \
1507 fmt, \
1508 ##__VA_ARGS__)
1509
1510/**
1511 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1512 * @test: The test context object.
1513 * @left: an arbitrary expression that evaluates to a primitive C type.
1514 * @right: an arbitrary expression that evaluates to a primitive C type.
1515 *
1516 * Sets an expectation that the value that @left evaluates to is greater than
1517 * the value that @right evaluates to. This is semantically equivalent to
1518 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1519 * more information.
1520 */
1521#define KUNIT_EXPECT_GE(test, left, right) \
1522 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1523
1524#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
1525 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1526 KUNIT_EXPECTATION, \
1527 left, \
1528 right, \
1529 fmt, \
1530 ##__VA_ARGS__)
1531
1532/**
1533 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1534 * @test: The test context object.
1535 * @left: an arbitrary expression that evaluates to a null terminated string.
1536 * @right: an arbitrary expression that evaluates to a null terminated string.
1537 *
1538 * Sets an expectation that the values that @left and @right evaluate to are
1539 * equal. This is semantically equivalent to
1540 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1541 * for more information.
1542 */
1543#define KUNIT_EXPECT_STREQ(test, left, right) \
1544 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1545
1546#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
1547 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1548 KUNIT_EXPECTATION, \
1549 left, \
1550 right, \
1551 fmt, \
1552 ##__VA_ARGS__)
1553
1554/**
1555 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1556 * @test: The test context object.
1557 * @left: an arbitrary expression that evaluates to a null terminated string.
1558 * @right: an arbitrary expression that evaluates to a null terminated string.
1559 *
1560 * Sets an expectation that the values that @left and @right evaluate to are
1561 * not equal. This is semantically equivalent to
1562 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1563 * for more information.
1564 */
1565#define KUNIT_EXPECT_STRNEQ(test, left, right) \
1566 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
1567
1568#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
1569 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1570 KUNIT_EXPECTATION, \
1571 left, \
1572 right, \
1573 fmt, \
1574 ##__VA_ARGS__)
1575
1576/**
1577 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1578 * @test: The test context object.
1579 * @ptr: an arbitrary pointer.
1580 *
1581 * Sets an expectation that the value that @ptr evaluates to is not null and not
1582 * an errno stored in a pointer. This is semantically equivalent to
1583 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1584 * more information.
1585 */
1586#define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1587 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_EXPECTATION, ptr)
1588
1589#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1590 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1591 KUNIT_EXPECTATION, \
1592 ptr, \
1593 fmt, \
1594 ##__VA_ARGS__)
1595
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001596#define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1597 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1598
1599/**
1600 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1601 * @test: The test context object.
1602 * @condition: an arbitrary boolean expression. The test fails and aborts when
1603 * this does not evaluate to true.
1604 *
1605 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1606 * fail *and immediately abort* when the specified condition is not met. Unlike
1607 * an expectation failure, it will prevent the test case from continuing to run;
1608 * this is otherwise known as an *assertion failure*.
1609 */
1610#define KUNIT_ASSERT_TRUE(test, condition) \
1611 KUNIT_TRUE_ASSERTION(test, KUNIT_ASSERTION, condition)
1612
1613#define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1614 KUNIT_TRUE_MSG_ASSERTION(test, \
1615 KUNIT_ASSERTION, \
1616 condition, \
1617 fmt, \
1618 ##__VA_ARGS__)
1619
1620/**
1621 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1622 * @test: The test context object.
1623 * @condition: an arbitrary boolean expression.
1624 *
1625 * Sets an assertion that the value that @condition evaluates to is false. This
1626 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1627 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1628 */
1629#define KUNIT_ASSERT_FALSE(test, condition) \
1630 KUNIT_FALSE_ASSERTION(test, KUNIT_ASSERTION, condition)
1631
1632#define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1633 KUNIT_FALSE_MSG_ASSERTION(test, \
1634 KUNIT_ASSERTION, \
1635 condition, \
1636 fmt, \
1637 ##__VA_ARGS__)
1638
1639/**
1640 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1641 * @test: The test context object.
1642 * @left: an arbitrary expression that evaluates to a primitive C type.
1643 * @right: an arbitrary expression that evaluates to a primitive C type.
1644 *
1645 * Sets an assertion that the values that @left and @right evaluate to are
1646 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1647 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1648 */
1649#define KUNIT_ASSERT_EQ(test, left, right) \
1650 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1651
1652#define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1653 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
1654 KUNIT_ASSERTION, \
1655 left, \
1656 right, \
1657 fmt, \
1658 ##__VA_ARGS__)
1659
1660/**
1661 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1662 * @test: The test context object.
1663 * @left: an arbitrary expression that evaluates to a pointer.
1664 * @right: an arbitrary expression that evaluates to a pointer.
1665 *
1666 * Sets an assertion that the values that @left and @right evaluate to are
1667 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1668 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1669 */
1670#define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1671 KUNIT_BINARY_PTR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1672
1673#define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1674 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
1675 KUNIT_ASSERTION, \
1676 left, \
1677 right, \
1678 fmt, \
1679 ##__VA_ARGS__)
1680
1681/**
1682 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1683 * @test: The test context object.
1684 * @left: an arbitrary expression that evaluates to a primitive C type.
1685 * @right: an arbitrary expression that evaluates to a primitive C type.
1686 *
1687 * Sets an assertion that the values that @left and @right evaluate to are not
1688 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1689 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1690 */
1691#define KUNIT_ASSERT_NE(test, left, right) \
1692 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1693
1694#define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1695 KUNIT_BINARY_NE_MSG_ASSERTION(test, \
1696 KUNIT_ASSERTION, \
1697 left, \
1698 right, \
1699 fmt, \
1700 ##__VA_ARGS__)
1701
1702/**
1703 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1704 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1705 * @test: The test context object.
1706 * @left: an arbitrary expression that evaluates to a pointer.
1707 * @right: an arbitrary expression that evaluates to a pointer.
1708 *
1709 * Sets an assertion that the values that @left and @right evaluate to are not
1710 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1711 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1712 */
1713#define KUNIT_ASSERT_PTR_NE(test, left, right) \
1714 KUNIT_BINARY_PTR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1715
1716#define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1717 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
1718 KUNIT_ASSERTION, \
1719 left, \
1720 right, \
1721 fmt, \
1722 ##__VA_ARGS__)
1723/**
1724 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1725 * @test: The test context object.
1726 * @left: an arbitrary expression that evaluates to a primitive C type.
1727 * @right: an arbitrary expression that evaluates to a primitive C type.
1728 *
1729 * Sets an assertion that the value that @left evaluates to is less than the
1730 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1731 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1732 * is not met.
1733 */
1734#define KUNIT_ASSERT_LT(test, left, right) \
1735 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1736
1737#define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1738 KUNIT_BINARY_LT_MSG_ASSERTION(test, \
1739 KUNIT_ASSERTION, \
1740 left, \
1741 right, \
1742 fmt, \
1743 ##__VA_ARGS__)
1744/**
1745 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1746 * @test: The test context object.
1747 * @left: an arbitrary expression that evaluates to a primitive C type.
1748 * @right: an arbitrary expression that evaluates to a primitive C type.
1749 *
1750 * Sets an assertion that the value that @left evaluates to is less than or
1751 * equal to the value that @right evaluates to. This is the same as
1752 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1753 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1754 */
1755#define KUNIT_ASSERT_LE(test, left, right) \
1756 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1757
1758#define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1759 KUNIT_BINARY_LE_MSG_ASSERTION(test, \
1760 KUNIT_ASSERTION, \
1761 left, \
1762 right, \
1763 fmt, \
1764 ##__VA_ARGS__)
1765
1766/**
1767 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1768 * @test: The test context object.
1769 * @left: an arbitrary expression that evaluates to a primitive C type.
1770 * @right: an arbitrary expression that evaluates to a primitive C type.
1771 *
1772 * Sets an assertion that the value that @left evaluates to is greater than the
1773 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1774 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1775 * is not met.
1776 */
1777#define KUNIT_ASSERT_GT(test, left, right) \
1778 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_ASSERTION, left, right)
1779
1780#define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1781 KUNIT_BINARY_GT_MSG_ASSERTION(test, \
1782 KUNIT_ASSERTION, \
1783 left, \
1784 right, \
1785 fmt, \
1786 ##__VA_ARGS__)
1787
1788/**
1789 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1790 * @test: The test context object.
1791 * @left: an arbitrary expression that evaluates to a primitive C type.
1792 * @right: an arbitrary expression that evaluates to a primitive C type.
1793 *
1794 * Sets an assertion that the value that @left evaluates to is greater than the
1795 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1796 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1797 * is not met.
1798 */
1799#define KUNIT_ASSERT_GE(test, left, right) \
1800 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1801
1802#define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1803 KUNIT_BINARY_GE_MSG_ASSERTION(test, \
1804 KUNIT_ASSERTION, \
1805 left, \
1806 right, \
1807 fmt, \
1808 ##__VA_ARGS__)
1809
1810/**
1811 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1812 * @test: The test context object.
1813 * @left: an arbitrary expression that evaluates to a null terminated string.
1814 * @right: an arbitrary expression that evaluates to a null terminated string.
1815 *
1816 * Sets an assertion that the values that @left and @right evaluate to are
1817 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1818 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1819 */
1820#define KUNIT_ASSERT_STREQ(test, left, right) \
1821 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
1822
1823#define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1824 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
1825 KUNIT_ASSERTION, \
1826 left, \
1827 right, \
1828 fmt, \
1829 ##__VA_ARGS__)
1830
1831/**
1832 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1833 * @test: The test context object.
1834 * @left: an arbitrary expression that evaluates to a null terminated string.
1835 * @right: an arbitrary expression that evaluates to a null terminated string.
1836 *
1837 * Sets an expectation that the values that @left and @right evaluate to are
1838 * not equal. This is semantically equivalent to
1839 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1840 * for more information.
1841 */
1842#define KUNIT_ASSERT_STRNEQ(test, left, right) \
1843 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
1844
1845#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1846 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
1847 KUNIT_ASSERTION, \
1848 left, \
1849 right, \
1850 fmt, \
1851 ##__VA_ARGS__)
1852
1853/**
1854 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1855 * @test: The test context object.
1856 * @ptr: an arbitrary pointer.
1857 *
1858 * Sets an assertion that the value that @ptr evaluates to is not null and not
1859 * an errno stored in a pointer. This is the same as
1860 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1861 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1862 */
1863#define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1864 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_ASSERTION, ptr)
1865
1866#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1867 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1868 KUNIT_ASSERTION, \
1869 ptr, \
1870 fmt, \
1871 ##__VA_ARGS__)
1872
Arpitha Raghunandanfadb08e2020-11-16 11:10:35 +05301873/**
1874 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1875 * @name: prefix for the test parameter generator function.
1876 * @array: array of test parameters.
1877 * @get_desc: function to convert param to description; NULL to use default
1878 *
1879 * Define function @name_gen_params which uses @array to generate parameters.
1880 */
1881#define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1882 static const void *name##_gen_params(const void *prev, char *desc) \
1883 { \
1884 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1885 if (__next - (array) < ARRAY_SIZE((array))) { \
1886 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1887 if (__get_desc) \
1888 __get_desc(__next, desc); \
1889 return __next; \
1890 } \
1891 return NULL; \
1892 }
1893
Brendan Higgins914cc632019-09-23 02:02:31 -07001894#endif /* _KUNIT_TEST_H */