blob: c958855681ccdd205bc8aa358aa0a9829d0cfede [file] [log] [blame]
Brendan Higgins914cc632019-09-23 02:02:31 -07001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Base unit test (KUnit) API.
4 *
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <[email protected]>
7 */
8
9#ifndef _KUNIT_TEST_H
10#define _KUNIT_TEST_H
11
Brendan Higgins73cda7b2019-09-23 02:02:35 -070012#include <kunit/assert.h>
Brendan Higgins5f3e0622019-09-23 02:02:39 -070013#include <kunit/try-catch.h>
Andy Shevchenkoec54c282021-11-08 18:32:15 -080014
Daniel Latypov4fdacef2022-01-13 08:59:27 -080015#include <linux/compiler.h>
Andy Shevchenkoec54c282021-11-08 18:32:15 -080016#include <linux/container_of.h>
17#include <linux/err.h>
18#include <linux/init.h>
19#include <linux/kconfig.h>
20#include <linux/kref.h>
21#include <linux/list.h>
Alan Maguirec475c772020-01-06 22:28:20 +000022#include <linux/module.h>
Brendan Higgins0a7568532019-09-23 02:02:32 -070023#include <linux/slab.h>
Andy Shevchenkoec54c282021-11-08 18:32:15 -080024#include <linux/spinlock.h>
25#include <linux/string.h>
Brendan Higgins914cc632019-09-23 02:02:31 -070026#include <linux/types.h>
Andy Shevchenkoec54c282021-11-08 18:32:15 -080027
28#include <asm/rwonce.h>
Brendan Higgins914cc632019-09-23 02:02:31 -070029
30struct kunit;
31
Alan Maguiree2219db2020-03-26 14:25:07 +000032/* Size of log associated with test. */
33#define KUNIT_LOG_SIZE 512
34
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +053035/* Maximum size of parameter description string. */
36#define KUNIT_PARAM_DESC_SIZE 128
37
David Gow6d2426b2021-06-24 23:58:12 -070038/* Maximum size of a status comment. */
39#define KUNIT_STATUS_COMMENT_SIZE 256
40
Alan Maguirec3bba692020-03-26 14:25:09 +000041/*
42 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
43 * sub-subtest. See the "Subtests" section in
44 * https://node-tap.org/tap-protocol/
45 */
46#define KUNIT_SUBTEST_INDENT " "
47#define KUNIT_SUBSUBTEST_INDENT " "
48
Brendan Higgins914cc632019-09-23 02:02:31 -070049/**
David Gow6d2426b2021-06-24 23:58:12 -070050 * enum kunit_status - Type of result for a test or test suite
51 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
52 * @KUNIT_FAILURE: Denotes the test has failed.
53 * @KUNIT_SKIPPED: Denotes the test has been skipped.
54 */
55enum kunit_status {
56 KUNIT_SUCCESS,
57 KUNIT_FAILURE,
58 KUNIT_SKIPPED,
59};
60
61/**
Brendan Higgins914cc632019-09-23 02:02:31 -070062 * struct kunit_case - represents an individual test case.
63 *
64 * @run_case: the function representing the actual test case.
65 * @name: the name of the test case.
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +053066 * @generate_params: the generator function for parameterized tests.
Brendan Higgins914cc632019-09-23 02:02:31 -070067 *
68 * A test case is a function with the signature,
Brendan Higginse4aea8f2019-09-23 02:02:41 -070069 * ``void (*)(struct kunit *)``
70 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
71 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
Brendan Higgins914cc632019-09-23 02:02:31 -070072 * with a &struct kunit_suite and will be run after the suite's init
73 * function and followed by the suite's exit function.
74 *
75 * A test case should be static and should only be created with the
76 * KUNIT_CASE() macro; additionally, every array of test cases should be
77 * terminated with an empty test case.
78 *
79 * Example:
80 *
81 * .. code-block:: c
82 *
83 * void add_test_basic(struct kunit *test)
84 * {
85 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
86 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
87 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
88 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
89 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
90 * }
91 *
92 * static struct kunit_case example_test_cases[] = {
93 * KUNIT_CASE(add_test_basic),
94 * {}
95 * };
96 *
97 */
98struct kunit_case {
99 void (*run_case)(struct kunit *test);
100 const char *name;
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +0530101 const void* (*generate_params)(const void *prev, char *desc);
Brendan Higgins914cc632019-09-23 02:02:31 -0700102
103 /* private: internal use only. */
David Gow6d2426b2021-06-24 23:58:12 -0700104 enum kunit_status status;
Alan Maguiree2219db2020-03-26 14:25:07 +0000105 char *log;
Brendan Higgins914cc632019-09-23 02:02:31 -0700106};
107
David Gow6d2426b2021-06-24 23:58:12 -0700108static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
Alan Maguiree2219db2020-03-26 14:25:07 +0000109{
David Gow6d2426b2021-06-24 23:58:12 -0700110 switch (status) {
111 case KUNIT_SKIPPED:
112 case KUNIT_SUCCESS:
113 return "ok";
114 case KUNIT_FAILURE:
115 return "not ok";
116 }
117 return "invalid";
Alan Maguiree2219db2020-03-26 14:25:07 +0000118}
119
Brendan Higgins914cc632019-09-23 02:02:31 -0700120/**
121 * KUNIT_CASE - A helper for creating a &struct kunit_case
122 *
123 * @test_name: a reference to a test case function.
124 *
125 * Takes a symbol for a function representing a test case and creates a
126 * &struct kunit_case object from it. See the documentation for
127 * &struct kunit_case for an example on how to use it.
128 */
129#define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
130
131/**
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +0530132 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
133 *
134 * @test_name: a reference to a test case function.
135 * @gen_params: a reference to a parameter generator function.
136 *
137 * The generator function::
138 *
139 * const void* gen_params(const void *prev, char *desc)
140 *
141 * is used to lazily generate a series of arbitrarily typed values that fit into
142 * a void*. The argument @prev is the previously returned value, which should be
143 * used to derive the next value; @prev is set to NULL on the initial generator
144 * call. When no more values are available, the generator must return NULL.
145 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
146 * describing the parameter.
147 */
148#define KUNIT_CASE_PARAM(test_name, gen_params) \
149 { .run_case = test_name, .name = #test_name, \
150 .generate_params = gen_params }
151
152/**
Brendan Higgins914cc632019-09-23 02:02:31 -0700153 * struct kunit_suite - describes a related collection of &struct kunit_case
154 *
155 * @name: the name of the test. Purely informational.
Daniel Latypov1cdba212022-04-29 11:12:57 -0700156 * @suite_init: called once per test suite before the test cases.
157 * @suite_exit: called once per test suite after all test cases.
Brendan Higgins914cc632019-09-23 02:02:31 -0700158 * @init: called before every test case.
159 * @exit: called after every test case.
160 * @test_cases: a null terminated array of test cases.
161 *
162 * A kunit_suite is a collection of related &struct kunit_case s, such that
163 * @init is called before every test case and @exit is called after every
164 * test case, similar to the notion of a *test fixture* or a *test class*
165 * in other unit testing frameworks like JUnit or Googletest.
166 *
167 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
168 * to run it.
169 */
170struct kunit_suite {
171 const char name[256];
Daniel Latypov1cdba212022-04-29 11:12:57 -0700172 int (*suite_init)(struct kunit_suite *suite);
173 void (*suite_exit)(struct kunit_suite *suite);
Brendan Higgins914cc632019-09-23 02:02:31 -0700174 int (*init)(struct kunit *test);
175 void (*exit)(struct kunit *test);
176 struct kunit_case *test_cases;
Alan Maguiree2219db2020-03-26 14:25:07 +0000177
Lothar Rubuschc4714b02020-04-15 20:16:53 +0000178 /* private: internal use only */
David Gow6d2426b2021-06-24 23:58:12 -0700179 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
Alan Maguiree2219db2020-03-26 14:25:07 +0000180 struct dentry *debugfs;
181 char *log;
Daniel Latypov1cdba212022-04-29 11:12:57 -0700182 int suite_init_err;
Brendan Higgins914cc632019-09-23 02:02:31 -0700183};
184
185/**
186 * struct kunit - represents a running instance of a test.
187 *
188 * @priv: for user to store arbitrary data. Commonly used to pass data
189 * created in the init function (see &struct kunit_suite).
190 *
191 * Used to store information about the current context under which the test
192 * is running. Most of this data is private and should only be accessed
193 * indirectly via public functions; the one exception is @priv which can be
194 * used by the test writer to store arbitrary data.
195 */
196struct kunit {
197 void *priv;
198
199 /* private: internal use only. */
200 const char *name; /* Read only after initialization! */
Alan Maguiree2219db2020-03-26 14:25:07 +0000201 char *log; /* Points at case log after initialization */
Brendan Higgins5f3e0622019-09-23 02:02:39 -0700202 struct kunit_try_catch try_catch;
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +0530203 /* param_value is the current parameter value for a test case. */
204 const void *param_value;
205 /* param_index stores the index of the parameter in parameterized tests. */
206 int param_index;
Brendan Higgins914cc632019-09-23 02:02:31 -0700207 /*
208 * success starts as true, and may only be set to false during a
209 * test case; thus, it is safe to update this across multiple
210 * threads using WRITE_ONCE; however, as a consequence, it may only
211 * be read after the test case finishes once all threads associated
212 * with the test case have terminated.
213 */
Brendan Higgins0a7568532019-09-23 02:02:32 -0700214 spinlock_t lock; /* Guards all mutable test state. */
David Gow6d2426b2021-06-24 23:58:12 -0700215 enum kunit_status status; /* Read only after test_case finishes! */
Brendan Higgins0a7568532019-09-23 02:02:32 -0700216 /*
217 * Because resources is a list that may be updated multiple times (with
218 * new resources) from any thread associated with a test case, we must
219 * protect it with some type of lock.
220 */
221 struct list_head resources; /* Protected by lock. */
David Gow6d2426b2021-06-24 23:58:12 -0700222
223 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
Brendan Higgins914cc632019-09-23 02:02:31 -0700224};
225
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -0700226static inline void kunit_set_failure(struct kunit *test)
227{
David Gow6d2426b2021-06-24 23:58:12 -0700228 WRITE_ONCE(test->status, KUNIT_FAILURE);
Patricia Alfonso83c4e7a2020-10-13 16:55:02 -0700229}
230
Alan Maguiree2219db2020-03-26 14:25:07 +0000231void kunit_init_test(struct kunit *test, const char *name, char *log);
Brendan Higgins914cc632019-09-23 02:02:31 -0700232
233int kunit_run_tests(struct kunit_suite *suite);
234
Alan Maguiree2219db2020-03-26 14:25:07 +0000235size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
236
237unsigned int kunit_test_case_num(struct kunit_suite *suite,
238 struct kunit_case *test_case);
239
Daniel Latypove5857d32022-07-09 11:19:58 +0800240int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
Alan Maguiree2219db2020-03-26 14:25:07 +0000241
Daniel Latypove5857d32022-07-09 11:19:58 +0800242void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
Alan Maguiree2219db2020-03-26 14:25:07 +0000243
Brendan Higgins8c0d8842020-08-04 13:47:43 -0700244#if IS_BUILTIN(CONFIG_KUNIT)
245int kunit_run_all_tests(void);
246#else
247static inline int kunit_run_all_tests(void)
248{
249 return 0;
250}
251#endif /* IS_BUILTIN(CONFIG_KUNIT) */
252
Daniel Latypove5857d32022-07-09 11:19:58 +0800253#define __kunit_test_suites(unique_array, ...) \
Jeremy Kerr3d6e4462022-07-09 11:19:57 +0800254 MODULE_INFO(test, "Y"); \
Daniel Latypove5857d32022-07-09 11:19:58 +0800255 static struct kunit_suite *unique_array[] \
256 __aligned(sizeof(struct kunit_suite *)) \
257 __used __section(".kunit_test_suites") = { __VA_ARGS__ }
Alan Maguireaac35462020-08-04 13:47:42 -0700258
259/**
260 * kunit_test_suites() - used to register one or more &struct kunit_suite
261 * with KUnit.
262 *
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200263 * @__suites: a statically allocated list of &struct kunit_suite.
Alan Maguireaac35462020-08-04 13:47:42 -0700264 *
Jeremy Kerr3d6e4462022-07-09 11:19:57 +0800265 * Registers @suites with the test framework.
266 * This is done by placing the array of struct kunit_suite * in the
267 * .kunit_test_suites ELF section.
Alan Maguireaac35462020-08-04 13:47:42 -0700268 *
Jeremy Kerr3d6e4462022-07-09 11:19:57 +0800269 * When builtin, KUnit tests are all run via the executor at boot, and when
270 * built as a module, they run on module load.
Alan Maguireaac35462020-08-04 13:47:42 -0700271 *
272 */
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200273#define kunit_test_suites(__suites...) \
Alan Maguireaac35462020-08-04 13:47:42 -0700274 __kunit_test_suites(__UNIQUE_ID(array), \
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200275 ##__suites)
Alan Maguirec475c772020-01-06 22:28:20 +0000276
277#define kunit_test_suite(suite) kunit_test_suites(&suite)
Brendan Higgins914cc632019-09-23 02:02:31 -0700278
Brendan Higgins9bf2eed2022-04-18 21:05:15 -0700279/**
280 * kunit_test_init_section_suites() - used to register one or more &struct
281 * kunit_suite containing init functions or
282 * init data.
283 *
284 * @__suites: a statically allocated list of &struct kunit_suite.
285 *
Mauro Carvalho Chehab7b237942022-07-02 12:07:40 +0100286 * This functions identically as kunit_test_suites() except that it suppresses
Brendan Higgins9bf2eed2022-04-18 21:05:15 -0700287 * modpost warnings for referencing functions marked __init or data marked
288 * __initdata; this is OK because currently KUnit only runs tests upon boot
289 * during the init phase or upon loading a module during the init phase.
290 *
291 * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
292 * tests must be excluded.
293 *
294 * The only thing this macro does that's different from kunit_test_suites is
295 * that it suffixes the array and suite declarations it makes with _probe;
296 * modpost suppresses warnings about referencing init data for symbols named in
297 * this manner.
298 */
299#define kunit_test_init_section_suites(__suites...) \
300 __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
301 CONCATENATE(__UNIQUE_ID(suites), _probe), \
302 ##__suites)
303
304#define kunit_test_init_section_suite(suite) \
305 kunit_test_init_section_suites(&suite)
306
Alan Maguiree2219db2020-03-26 14:25:07 +0000307#define kunit_suite_for_each_test_case(suite, test_case) \
308 for (test_case = suite->test_cases; test_case->run_case; test_case++)
309
David Gow6d2426b2021-06-24 23:58:12 -0700310enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
Alan Maguiree2219db2020-03-26 14:25:07 +0000311
Alan Maguired4cdd142020-05-29 22:46:20 +0100312/**
Daniel Latypov7122deb2021-05-03 13:58:34 -0700313 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
314 * @test: The test context object.
315 * @n: number of elements.
316 * @size: The size in bytes of the desired memory.
317 * @gfp: flags passed to underlying kmalloc().
318 *
319 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
320 * and is automatically cleaned up after the test case concludes. See &struct
321 * kunit_resource for more information.
322 */
Daniel Latypov361b57d2021-10-05 13:46:32 -0700323void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
Daniel Latypov7122deb2021-05-03 13:58:34 -0700324
325/**
Brendan Higgins0a7568532019-09-23 02:02:32 -0700326 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
327 * @test: The test context object.
328 * @size: The size in bytes of the desired memory.
329 * @gfp: flags passed to underlying kmalloc().
330 *
Daniel Latypov7122deb2021-05-03 13:58:34 -0700331 * See kmalloc() and kunit_kmalloc_array() for more information.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700332 */
Daniel Latypov7122deb2021-05-03 13:58:34 -0700333static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
334{
335 return kunit_kmalloc_array(test, 1, size, gfp);
336}
Brendan Higgins0a7568532019-09-23 02:02:32 -0700337
338/**
339 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
340 * @test: The test case to which the resource belongs.
341 * @ptr: The memory allocation to free.
342 */
343void kunit_kfree(struct kunit *test, const void *ptr);
344
345/**
346 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
347 * @test: The test context object.
348 * @size: The size in bytes of the desired memory.
349 * @gfp: flags passed to underlying kmalloc().
350 *
Daniel Latypov7122deb2021-05-03 13:58:34 -0700351 * See kzalloc() and kunit_kmalloc_array() for more information.
Brendan Higgins0a7568532019-09-23 02:02:32 -0700352 */
353static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
354{
355 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
356}
357
Daniel Latypov7122deb2021-05-03 13:58:34 -0700358/**
359 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
360 * @test: The test context object.
361 * @n: number of elements.
362 * @size: The size in bytes of the desired memory.
363 * @gfp: flags passed to underlying kmalloc().
364 *
365 * See kcalloc() and kunit_kmalloc_array() for more information.
366 */
Daniel Latypov361b57d2021-10-05 13:46:32 -0700367static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
Daniel Latypov7122deb2021-05-03 13:58:34 -0700368{
Daniel Latypov361b57d2021-10-05 13:46:32 -0700369 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
Daniel Latypov7122deb2021-05-03 13:58:34 -0700370}
371
Brendan Higgins0a7568532019-09-23 02:02:32 -0700372void kunit_cleanup(struct kunit *test);
373
David Gow44acdbb2021-05-13 13:03:50 -0700374void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
Alan Maguiree2219db2020-03-26 14:25:07 +0000375
David Gow6d2426b2021-06-24 23:58:12 -0700376/**
377 * kunit_mark_skipped() - Marks @test_or_suite as skipped
378 *
379 * @test_or_suite: The test context object.
380 * @fmt: A printk() style format string.
381 *
382 * Marks the test as skipped. @fmt is given output as the test status
383 * comment, typically the reason the test was skipped.
384 *
385 * Test execution continues after kunit_mark_skipped() is called.
386 */
387#define kunit_mark_skipped(test_or_suite, fmt, ...) \
388 do { \
389 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
390 scnprintf((test_or_suite)->status_comment, \
391 KUNIT_STATUS_COMMENT_SIZE, \
392 fmt, ##__VA_ARGS__); \
393 } while (0)
394
395/**
396 * kunit_skip() - Marks @test_or_suite as skipped
397 *
398 * @test_or_suite: The test context object.
399 * @fmt: A printk() style format string.
400 *
401 * Skips the test. @fmt is given output as the test status
402 * comment, typically the reason the test was skipped.
403 *
404 * Test execution is halted after kunit_skip() is called.
405 */
406#define kunit_skip(test_or_suite, fmt, ...) \
407 do { \
408 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
409 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
410 } while (0)
Alan Maguiree2219db2020-03-26 14:25:07 +0000411
412/*
413 * printk and log to per-test or per-suite log buffer. Logging only done
414 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
415 */
416#define kunit_log(lvl, test_or_suite, fmt, ...) \
417 do { \
418 printk(lvl fmt, ##__VA_ARGS__); \
419 kunit_log_append((test_or_suite)->log, fmt "\n", \
420 ##__VA_ARGS__); \
421 } while (0)
422
423#define kunit_printk(lvl, test, fmt, ...) \
Alan Maguirec3bba692020-03-26 14:25:09 +0000424 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
425 (test)->name, ##__VA_ARGS__)
Brendan Higgins914cc632019-09-23 02:02:31 -0700426
427/**
428 * kunit_info() - Prints an INFO level message associated with @test.
429 *
430 * @test: The test context object.
431 * @fmt: A printk() style format string.
432 *
433 * Prints an info level message associated with the test suite being run.
434 * Takes a variable number of format parameters just like printk().
435 */
436#define kunit_info(test, fmt, ...) \
437 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
438
439/**
440 * kunit_warn() - Prints a WARN level message associated with @test.
441 *
442 * @test: The test context object.
443 * @fmt: A printk() style format string.
444 *
445 * Prints a warning level message.
446 */
447#define kunit_warn(test, fmt, ...) \
448 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
449
450/**
451 * kunit_err() - Prints an ERROR level message associated with @test.
452 *
453 * @test: The test context object.
454 * @fmt: A printk() style format string.
455 *
456 * Prints an error level message.
457 */
458#define kunit_err(test, fmt, ...) \
459 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
460
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700461/**
462 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
463 * @test: The test context object.
464 *
465 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
466 * words, it does nothing and only exists for code clarity. See
467 * KUNIT_EXPECT_TRUE() for more information.
468 */
469#define KUNIT_SUCCEED(test) do {} while (0)
470
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800471void kunit_do_failed_assertion(struct kunit *test,
Daniel Latypov21957f92022-01-13 08:59:30 -0800472 const struct kunit_loc *loc,
473 enum kunit_assert_type type,
Miguel Ojeda74668862022-05-02 11:36:25 +0200474 const struct kunit_assert *assert,
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800475 const char *fmt, ...);
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700476
Daniel Latypov21957f92022-01-13 08:59:30 -0800477#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800478 if (unlikely(!(pass))) { \
Daniel Latypovc2741452022-01-27 13:52:22 -0800479 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800480 struct assert_class __assertion = INITIALIZER; \
481 kunit_do_failed_assertion(test, \
Daniel Latypovc2741452022-01-27 13:52:22 -0800482 &__loc, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800483 assert_type, \
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800484 &__assertion.assert, \
485 fmt, \
486 ##__VA_ARGS__); \
487 } \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700488} while (0)
489
490
491#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
492 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800493 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700494 false, \
495 kunit_fail_assert, \
Daniel Latypov05a7da82022-01-13 08:59:31 -0800496 KUNIT_INIT_FAIL_ASSERT_STRUCT, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700497 fmt, \
498 ##__VA_ARGS__)
499
500/**
501 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
502 * @test: The test context object.
503 * @fmt: an informational message to be printed when the assertion is made.
504 * @...: string format arguments.
505 *
506 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
507 * other words, it always results in a failed expectation, and consequently
508 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
509 * for more information.
510 */
511#define KUNIT_FAIL(test, fmt, ...) \
512 KUNIT_FAIL_ASSERTION(test, \
513 KUNIT_EXPECTATION, \
514 fmt, \
515 ##__VA_ARGS__)
516
517#define KUNIT_UNARY_ASSERTION(test, \
518 assert_type, \
519 condition, \
520 expected_true, \
521 fmt, \
522 ...) \
523 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800524 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700525 !!(condition) == !!expected_true, \
526 kunit_unary_assert, \
Daniel Latypov05a7da82022-01-13 08:59:31 -0800527 KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700528 expected_true), \
529 fmt, \
530 ##__VA_ARGS__)
531
532#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
533 KUNIT_UNARY_ASSERTION(test, \
534 assert_type, \
535 condition, \
536 true, \
537 fmt, \
538 ##__VA_ARGS__)
539
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700540#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
541 KUNIT_UNARY_ASSERTION(test, \
542 assert_type, \
543 condition, \
544 false, \
545 fmt, \
546 ##__VA_ARGS__)
547
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700548/*
549 * A factory macro for defining the assertions and expectations for the basic
550 * comparisons defined for the built in types.
551 *
552 * Unfortunately, there is no common type that all types can be promoted to for
553 * which all the binary operators behave the same way as for the actual types
554 * (for example, there is no type that long long and unsigned long long can
555 * both be cast to where the comparison result is preserved for all values). So
556 * the best we can do is do the comparison in the original types and then coerce
557 * everything to long long for printing; this way, the comparison behaves
558 * correctly and the printed out value usually makes sense without
559 * interpretation, but can always be interpreted to figure out the actual
560 * value.
561 */
562#define KUNIT_BASE_BINARY_ASSERTION(test, \
563 assert_class, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800564 format_func, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700565 assert_type, \
566 left, \
567 op, \
568 right, \
569 fmt, \
570 ...) \
571do { \
Daniel Latypovc2741452022-01-27 13:52:22 -0800572 const typeof(left) __left = (left); \
573 const typeof(right) __right = (right); \
Daniel Latypov2b6861e2022-01-25 13:00:11 -0800574 static const struct kunit_binary_assert_text __text = { \
575 .operation = #op, \
576 .left_text = #left, \
577 .right_text = #right, \
578 }; \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700579 \
580 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800581 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700582 __left op __right, \
583 assert_class, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800584 KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \
Daniel Latypov2b6861e2022-01-25 13:00:11 -0800585 &__text, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800586 __left, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800587 __right), \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700588 fmt, \
589 ##__VA_ARGS__); \
590} while (0)
591
Daniel Latypov40f397772022-01-18 14:35:05 -0800592#define KUNIT_BINARY_INT_ASSERTION(test, \
593 assert_type, \
594 left, \
595 op, \
596 right, \
597 fmt, \
598 ...) \
599 KUNIT_BASE_BINARY_ASSERTION(test, \
600 kunit_binary_assert, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800601 kunit_binary_assert_format, \
Daniel Latypov40f397772022-01-18 14:35:05 -0800602 assert_type, \
603 left, op, right, \
604 fmt, \
605 ##__VA_ARGS__)
606
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800607#define KUNIT_BINARY_PTR_ASSERTION(test, \
608 assert_type, \
609 left, \
610 op, \
611 right, \
612 fmt, \
613 ...) \
614 KUNIT_BASE_BINARY_ASSERTION(test, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700615 kunit_binary_ptr_assert, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800616 kunit_binary_ptr_assert_format, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700617 assert_type, \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800618 left, op, right, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700619 fmt, \
620 ##__VA_ARGS__)
621
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700622#define KUNIT_BINARY_STR_ASSERTION(test, \
623 assert_type, \
624 left, \
625 op, \
626 right, \
627 fmt, \
628 ...) \
629do { \
David Gow3747b5c2021-05-13 12:31:56 -0700630 const char *__left = (left); \
Daniel Latypov2b6861e2022-01-25 13:00:11 -0800631 const char *__right = (right); \
632 static const struct kunit_binary_assert_text __text = { \
633 .operation = #op, \
634 .left_text = #left, \
635 .right_text = #right, \
636 }; \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700637 \
638 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800639 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700640 strcmp(__left, __right) op 0, \
641 kunit_binary_str_assert, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800642 KUNIT_INIT_BINARY_ASSERT_STRUCT(kunit_binary_str_assert_format,\
Daniel Latypov2b6861e2022-01-25 13:00:11 -0800643 &__text, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700644 __left, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700645 __right), \
646 fmt, \
647 ##__VA_ARGS__); \
648} while (0)
649
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700650#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
651 assert_type, \
652 ptr, \
653 fmt, \
654 ...) \
655do { \
Daniel Latypovc2741452022-01-27 13:52:22 -0800656 const typeof(ptr) __ptr = (ptr); \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700657 \
658 KUNIT_ASSERTION(test, \
Daniel Latypov21957f92022-01-13 08:59:30 -0800659 assert_type, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700660 !IS_ERR_OR_NULL(__ptr), \
661 kunit_ptr_not_err_assert, \
Daniel Latypov05a7da82022-01-13 08:59:31 -0800662 KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700663 __ptr), \
664 fmt, \
665 ##__VA_ARGS__); \
666} while (0)
667
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700668/**
669 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
670 * @test: The test context object.
671 * @condition: an arbitrary boolean expression. The test fails when this does
672 * not evaluate to true.
673 *
674 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
675 * to fail when the specified condition is not met; however, it will not prevent
676 * the test case from continuing to run; this is otherwise known as an
677 * *expectation failure*.
678 */
679#define KUNIT_EXPECT_TRUE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800680 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700681
682#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
683 KUNIT_TRUE_MSG_ASSERTION(test, \
684 KUNIT_EXPECTATION, \
685 condition, \
686 fmt, \
687 ##__VA_ARGS__)
688
689/**
690 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
691 * @test: The test context object.
692 * @condition: an arbitrary boolean expression. The test fails when this does
693 * not evaluate to false.
694 *
695 * Sets an expectation that @condition evaluates to false. See
696 * KUNIT_EXPECT_TRUE() for more information.
697 */
698#define KUNIT_EXPECT_FALSE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800699 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700700
701#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
702 KUNIT_FALSE_MSG_ASSERTION(test, \
703 KUNIT_EXPECTATION, \
704 condition, \
705 fmt, \
706 ##__VA_ARGS__)
707
708/**
709 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
710 * @test: The test context object.
711 * @left: an arbitrary expression that evaluates to a primitive C type.
712 * @right: an arbitrary expression that evaluates to a primitive C type.
713 *
714 * Sets an expectation that the values that @left and @right evaluate to are
715 * equal. This is semantically equivalent to
716 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
717 * more information.
718 */
719#define KUNIT_EXPECT_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800720 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700721
722#define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800723 KUNIT_BINARY_INT_ASSERTION(test, \
724 KUNIT_EXPECTATION, \
725 left, ==, right, \
726 fmt, \
727 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700728
729/**
730 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
731 * @test: The test context object.
732 * @left: an arbitrary expression that evaluates to a pointer.
733 * @right: an arbitrary expression that evaluates to a pointer.
734 *
735 * Sets an expectation that the values that @left and @right evaluate to are
736 * equal. This is semantically equivalent to
737 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
738 * more information.
739 */
740#define KUNIT_EXPECT_PTR_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800741 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700742
743#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800744 KUNIT_BINARY_PTR_ASSERTION(test, \
745 KUNIT_EXPECTATION, \
746 left, ==, right, \
747 fmt, \
748 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700749
750/**
751 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
752 * @test: The test context object.
753 * @left: an arbitrary expression that evaluates to a primitive C type.
754 * @right: an arbitrary expression that evaluates to a primitive C type.
755 *
756 * Sets an expectation that the values that @left and @right evaluate to are not
757 * equal. This is semantically equivalent to
758 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
759 * more information.
760 */
761#define KUNIT_EXPECT_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800762 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700763
764#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800765 KUNIT_BINARY_INT_ASSERTION(test, \
766 KUNIT_EXPECTATION, \
767 left, !=, right, \
768 fmt, \
769 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700770
771/**
772 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
773 * @test: The test context object.
774 * @left: an arbitrary expression that evaluates to a pointer.
775 * @right: an arbitrary expression that evaluates to a pointer.
776 *
777 * Sets an expectation that the values that @left and @right evaluate to are not
778 * equal. This is semantically equivalent to
779 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
780 * more information.
781 */
782#define KUNIT_EXPECT_PTR_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800783 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700784
785#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800786 KUNIT_BINARY_PTR_ASSERTION(test, \
787 KUNIT_EXPECTATION, \
788 left, !=, right, \
789 fmt, \
790 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700791
792/**
793 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
794 * @test: The test context object.
795 * @left: an arbitrary expression that evaluates to a primitive C type.
796 * @right: an arbitrary expression that evaluates to a primitive C type.
797 *
798 * Sets an expectation that the value that @left evaluates to is less than the
799 * value that @right evaluates to. This is semantically equivalent to
800 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
801 * more information.
802 */
803#define KUNIT_EXPECT_LT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800804 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700805
806#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800807 KUNIT_BINARY_INT_ASSERTION(test, \
808 KUNIT_EXPECTATION, \
809 left, <, right, \
810 fmt, \
811 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700812
813/**
814 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
815 * @test: The test context object.
816 * @left: an arbitrary expression that evaluates to a primitive C type.
817 * @right: an arbitrary expression that evaluates to a primitive C type.
818 *
819 * Sets an expectation that the value that @left evaluates to is less than or
820 * equal to the value that @right evaluates to. Semantically this is equivalent
821 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
822 * more information.
823 */
824#define KUNIT_EXPECT_LE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800825 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700826
827#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800828 KUNIT_BINARY_INT_ASSERTION(test, \
829 KUNIT_ASSERTION, \
830 left, <=, right, \
831 fmt, \
832 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700833
834/**
835 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
836 * @test: The test context object.
837 * @left: an arbitrary expression that evaluates to a primitive C type.
838 * @right: an arbitrary expression that evaluates to a primitive C type.
839 *
840 * Sets an expectation that the value that @left evaluates to is greater than
841 * the value that @right evaluates to. This is semantically equivalent to
842 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
843 * more information.
844 */
845#define KUNIT_EXPECT_GT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800846 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700847
848#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800849 KUNIT_BINARY_INT_ASSERTION(test, \
850 KUNIT_EXPECTATION, \
851 left, >, right, \
852 fmt, \
853 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700854
855/**
856 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
857 * @test: The test context object.
858 * @left: an arbitrary expression that evaluates to a primitive C type.
859 * @right: an arbitrary expression that evaluates to a primitive C type.
860 *
861 * Sets an expectation that the value that @left evaluates to is greater than
862 * the value that @right evaluates to. This is semantically equivalent to
863 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
864 * more information.
865 */
866#define KUNIT_EXPECT_GE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800867 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700868
869#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800870 KUNIT_BINARY_INT_ASSERTION(test, \
871 KUNIT_EXPECTATION, \
872 left, >=, right, \
873 fmt, \
874 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700875
876/**
877 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
878 * @test: The test context object.
879 * @left: an arbitrary expression that evaluates to a null terminated string.
880 * @right: an arbitrary expression that evaluates to a null terminated string.
881 *
882 * Sets an expectation that the values that @left and @right evaluate to are
883 * equal. This is semantically equivalent to
884 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
885 * for more information.
886 */
887#define KUNIT_EXPECT_STREQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800888 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700889
890#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -0800891 KUNIT_BINARY_STR_ASSERTION(test, \
892 KUNIT_EXPECTATION, \
893 left, ==, right, \
894 fmt, \
895 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700896
897/**
898 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
899 * @test: The test context object.
900 * @left: an arbitrary expression that evaluates to a null terminated string.
901 * @right: an arbitrary expression that evaluates to a null terminated string.
902 *
903 * Sets an expectation that the values that @left and @right evaluate to are
904 * not equal. This is semantically equivalent to
905 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
906 * for more information.
907 */
908#define KUNIT_EXPECT_STRNEQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800909 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700910
911#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -0800912 KUNIT_BINARY_STR_ASSERTION(test, \
913 KUNIT_EXPECTATION, \
914 left, !=, right, \
915 fmt, \
916 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700917
918/**
Ricardo Ribaldacaae9452022-02-11 17:42:41 +0100919 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
920 * @test: The test context object.
921 * @ptr: an arbitrary pointer.
922 *
923 * Sets an expectation that the value that @ptr evaluates to is null. This is
924 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
925 * See KUNIT_EXPECT_TRUE() for more information.
926 */
927#define KUNIT_EXPECT_NULL(test, ptr) \
928 KUNIT_EXPECT_NULL_MSG(test, \
929 ptr, \
930 NULL)
931
932#define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
933 KUNIT_BINARY_PTR_ASSERTION(test, \
934 KUNIT_EXPECTATION, \
935 ptr, ==, NULL, \
936 fmt, \
937 ##__VA_ARGS__)
938
939/**
940 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
941 * @test: The test context object.
942 * @ptr: an arbitrary pointer.
943 *
944 * Sets an expectation that the value that @ptr evaluates to is not null. This
945 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
946 * See KUNIT_EXPECT_TRUE() for more information.
947 */
948#define KUNIT_EXPECT_NOT_NULL(test, ptr) \
949 KUNIT_EXPECT_NOT_NULL_MSG(test, \
950 ptr, \
951 NULL)
952
953#define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
954 KUNIT_BINARY_PTR_ASSERTION(test, \
955 KUNIT_EXPECTATION, \
956 ptr, !=, NULL, \
957 fmt, \
958 ##__VA_ARGS__)
959
960/**
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700961 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
962 * @test: The test context object.
963 * @ptr: an arbitrary pointer.
964 *
965 * Sets an expectation that the value that @ptr evaluates to is not null and not
966 * an errno stored in a pointer. This is semantically equivalent to
967 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
968 * more information.
969 */
970#define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800971 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700972
973#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
974 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
975 KUNIT_EXPECTATION, \
976 ptr, \
977 fmt, \
978 ##__VA_ARGS__)
979
Brendan Higginse4aea8f2019-09-23 02:02:41 -0700980#define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
981 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
982
983/**
984 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
985 * @test: The test context object.
986 * @condition: an arbitrary boolean expression. The test fails and aborts when
987 * this does not evaluate to true.
988 *
989 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
990 * fail *and immediately abort* when the specified condition is not met. Unlike
991 * an expectation failure, it will prevent the test case from continuing to run;
992 * this is otherwise known as an *assertion failure*.
993 */
994#define KUNIT_ASSERT_TRUE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800995 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -0700996
997#define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
998 KUNIT_TRUE_MSG_ASSERTION(test, \
999 KUNIT_ASSERTION, \
1000 condition, \
1001 fmt, \
1002 ##__VA_ARGS__)
1003
1004/**
1005 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1006 * @test: The test context object.
1007 * @condition: an arbitrary boolean expression.
1008 *
1009 * Sets an assertion that the value that @condition evaluates to is false. This
1010 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1011 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1012 */
1013#define KUNIT_ASSERT_FALSE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001014 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001015
1016#define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1017 KUNIT_FALSE_MSG_ASSERTION(test, \
1018 KUNIT_ASSERTION, \
1019 condition, \
1020 fmt, \
1021 ##__VA_ARGS__)
1022
1023/**
1024 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1025 * @test: The test context object.
1026 * @left: an arbitrary expression that evaluates to a primitive C type.
1027 * @right: an arbitrary expression that evaluates to a primitive C type.
1028 *
1029 * Sets an assertion that the values that @left and @right evaluate to are
1030 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1031 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1032 */
1033#define KUNIT_ASSERT_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001034 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001035
1036#define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001037 KUNIT_BINARY_INT_ASSERTION(test, \
1038 KUNIT_ASSERTION, \
1039 left, ==, right, \
1040 fmt, \
1041 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001042
1043/**
1044 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1045 * @test: The test context object.
1046 * @left: an arbitrary expression that evaluates to a pointer.
1047 * @right: an arbitrary expression that evaluates to a pointer.
1048 *
1049 * Sets an assertion that the values that @left and @right evaluate to are
1050 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1051 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1052 */
1053#define KUNIT_ASSERT_PTR_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001054 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001055
1056#define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001057 KUNIT_BINARY_PTR_ASSERTION(test, \
1058 KUNIT_ASSERTION, \
1059 left, ==, right, \
1060 fmt, \
1061 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001062
1063/**
1064 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1065 * @test: The test context object.
1066 * @left: an arbitrary expression that evaluates to a primitive C type.
1067 * @right: an arbitrary expression that evaluates to a primitive C type.
1068 *
1069 * Sets an assertion that the values that @left and @right evaluate to are not
1070 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1071 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1072 */
1073#define KUNIT_ASSERT_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001074 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001075
1076#define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001077 KUNIT_BINARY_INT_ASSERTION(test, \
1078 KUNIT_ASSERTION, \
1079 left, !=, right, \
1080 fmt, \
1081 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001082
1083/**
1084 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1085 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1086 * @test: The test context object.
1087 * @left: an arbitrary expression that evaluates to a pointer.
1088 * @right: an arbitrary expression that evaluates to a pointer.
1089 *
1090 * Sets an assertion that the values that @left and @right evaluate to are not
1091 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1092 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1093 */
1094#define KUNIT_ASSERT_PTR_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001095 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001096
1097#define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001098 KUNIT_BINARY_PTR_ASSERTION(test, \
1099 KUNIT_ASSERTION, \
1100 left, !=, right, \
1101 fmt, \
1102 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001103/**
1104 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1105 * @test: The test context object.
1106 * @left: an arbitrary expression that evaluates to a primitive C type.
1107 * @right: an arbitrary expression that evaluates to a primitive C type.
1108 *
1109 * Sets an assertion that the value that @left evaluates to is less than the
1110 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1111 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1112 * is not met.
1113 */
1114#define KUNIT_ASSERT_LT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001115 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001116
1117#define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001118 KUNIT_BINARY_INT_ASSERTION(test, \
1119 KUNIT_EXPECTATION, \
1120 left, <, right, \
1121 fmt, \
1122 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001123/**
1124 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1125 * @test: The test context object.
1126 * @left: an arbitrary expression that evaluates to a primitive C type.
1127 * @right: an arbitrary expression that evaluates to a primitive C type.
1128 *
1129 * Sets an assertion that the value that @left evaluates to is less than or
1130 * equal to the value that @right evaluates to. This is the same as
1131 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1132 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1133 */
1134#define KUNIT_ASSERT_LE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001135 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001136
1137#define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001138 KUNIT_BINARY_INT_ASSERTION(test, \
1139 KUNIT_ASSERTION, \
1140 left, <=, right, \
1141 fmt, \
1142 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001143
1144/**
1145 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1146 * @test: The test context object.
1147 * @left: an arbitrary expression that evaluates to a primitive C type.
1148 * @right: an arbitrary expression that evaluates to a primitive C type.
1149 *
1150 * Sets an assertion that the value that @left evaluates to is greater than the
1151 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1152 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1153 * is not met.
1154 */
1155#define KUNIT_ASSERT_GT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001156 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001157
1158#define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001159 KUNIT_BINARY_INT_ASSERTION(test, \
1160 KUNIT_EXPECTATION, \
1161 left, >, right, \
1162 fmt, \
1163 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001164
1165/**
1166 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1167 * @test: The test context object.
1168 * @left: an arbitrary expression that evaluates to a primitive C type.
1169 * @right: an arbitrary expression that evaluates to a primitive C type.
1170 *
1171 * Sets an assertion that the value that @left evaluates to is greater than the
1172 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1173 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1174 * is not met.
1175 */
1176#define KUNIT_ASSERT_GE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001177 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001178
1179#define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001180 KUNIT_BINARY_INT_ASSERTION(test, \
1181 KUNIT_ASSERTION, \
1182 left, >=, right, \
1183 fmt, \
1184 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001185
1186/**
1187 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1188 * @test: The test context object.
1189 * @left: an arbitrary expression that evaluates to a null terminated string.
1190 * @right: an arbitrary expression that evaluates to a null terminated string.
1191 *
1192 * Sets an assertion that the values that @left and @right evaluate to are
1193 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1194 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1195 */
1196#define KUNIT_ASSERT_STREQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001197 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001198
1199#define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -08001200 KUNIT_BINARY_STR_ASSERTION(test, \
1201 KUNIT_ASSERTION, \
1202 left, ==, right, \
1203 fmt, \
1204 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001205
1206/**
1207 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1208 * @test: The test context object.
1209 * @left: an arbitrary expression that evaluates to a null terminated string.
1210 * @right: an arbitrary expression that evaluates to a null terminated string.
1211 *
1212 * Sets an expectation that the values that @left and @right evaluate to are
1213 * not equal. This is semantically equivalent to
1214 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1215 * for more information.
1216 */
1217#define KUNIT_ASSERT_STRNEQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001218 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001219
1220#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -08001221 KUNIT_BINARY_STR_ASSERTION(test, \
1222 KUNIT_ASSERTION, \
1223 left, !=, right, \
1224 fmt, \
1225 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001226
1227/**
Ricardo Ribaldacaae9452022-02-11 17:42:41 +01001228 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1229 * @test: The test context object.
1230 * @ptr: an arbitrary pointer.
1231 *
1232 * Sets an assertion that the values that @ptr evaluates to is null. This is
1233 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1234 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1235 */
1236#define KUNIT_ASSERT_NULL(test, ptr) \
1237 KUNIT_ASSERT_NULL_MSG(test, \
1238 ptr, \
1239 NULL)
1240
1241#define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1242 KUNIT_BINARY_PTR_ASSERTION(test, \
1243 KUNIT_ASSERTION, \
1244 ptr, ==, NULL, \
1245 fmt, \
1246 ##__VA_ARGS__)
1247
1248/**
1249 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1250 * @test: The test context object.
1251 * @ptr: an arbitrary pointer.
1252 *
1253 * Sets an assertion that the values that @ptr evaluates to is not null. This
1254 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1255 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1256 */
1257#define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1258 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1259 ptr, \
1260 NULL)
1261
1262#define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1263 KUNIT_BINARY_PTR_ASSERTION(test, \
1264 KUNIT_ASSERTION, \
1265 ptr, !=, NULL, \
1266 fmt, \
1267 ##__VA_ARGS__)
1268
1269/**
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001270 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1271 * @test: The test context object.
1272 * @ptr: an arbitrary pointer.
1273 *
1274 * Sets an assertion that the value that @ptr evaluates to is not null and not
1275 * an errno stored in a pointer. This is the same as
1276 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1277 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1278 */
1279#define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001280 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001281
1282#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1283 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1284 KUNIT_ASSERTION, \
1285 ptr, \
1286 fmt, \
1287 ##__VA_ARGS__)
1288
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +05301289/**
1290 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1291 * @name: prefix for the test parameter generator function.
1292 * @array: array of test parameters.
1293 * @get_desc: function to convert param to description; NULL to use default
1294 *
1295 * Define function @name_gen_params which uses @array to generate parameters.
1296 */
1297#define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1298 static const void *name##_gen_params(const void *prev, char *desc) \
1299 { \
1300 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1301 if (__next - (array) < ARRAY_SIZE((array))) { \
1302 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1303 if (__get_desc) \
1304 __get_desc(__next, desc); \
1305 return __next; \
1306 } \
1307 return NULL; \
1308 }
1309
Daniel Latypov61695f82022-03-28 10:41:42 -07001310// TODO([email protected]): consider eventually migrating users to explicitly
1311// include resource.h themselves if they need it.
1312#include <kunit/resource.h>
1313
Brendan Higgins914cc632019-09-23 02:02:31 -07001314#endif /* _KUNIT_TEST_H */