blob: ebcdbddf8344de579bc5acce64433a5194ce64ce [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
Joe Fradleyd20a6ba2022-08-23 07:24:54 -0700231bool kunit_enabled(void);
232
Alan Maguiree2219db2020-03-26 14:25:07 +0000233void kunit_init_test(struct kunit *test, const char *name, char *log);
Brendan Higgins914cc632019-09-23 02:02:31 -0700234
235int kunit_run_tests(struct kunit_suite *suite);
236
Alan Maguiree2219db2020-03-26 14:25:07 +0000237size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
238
239unsigned int kunit_test_case_num(struct kunit_suite *suite,
240 struct kunit_case *test_case);
241
Daniel Latypove5857d32022-07-09 11:19:58 +0800242int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
Alan Maguiree2219db2020-03-26 14:25:07 +0000243
Daniel Latypove5857d32022-07-09 11:19:58 +0800244void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
Alan Maguiree2219db2020-03-26 14:25:07 +0000245
Brendan Higgins8c0d8842020-08-04 13:47:43 -0700246#if IS_BUILTIN(CONFIG_KUNIT)
247int kunit_run_all_tests(void);
248#else
249static inline int kunit_run_all_tests(void)
250{
251 return 0;
252}
253#endif /* IS_BUILTIN(CONFIG_KUNIT) */
254
Daniel Latypove5857d32022-07-09 11:19:58 +0800255#define __kunit_test_suites(unique_array, ...) \
Daniel Latypove5857d32022-07-09 11:19:58 +0800256 static struct kunit_suite *unique_array[] \
257 __aligned(sizeof(struct kunit_suite *)) \
258 __used __section(".kunit_test_suites") = { __VA_ARGS__ }
Alan Maguireaac35462020-08-04 13:47:42 -0700259
260/**
261 * kunit_test_suites() - used to register one or more &struct kunit_suite
262 * with KUnit.
263 *
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200264 * @__suites: a statically allocated list of &struct kunit_suite.
Alan Maguireaac35462020-08-04 13:47:42 -0700265 *
Jeremy Kerr3d6e4462022-07-09 11:19:57 +0800266 * Registers @suites with the test framework.
267 * This is done by placing the array of struct kunit_suite * in the
268 * .kunit_test_suites ELF section.
Alan Maguireaac35462020-08-04 13:47:42 -0700269 *
Jeremy Kerr3d6e4462022-07-09 11:19:57 +0800270 * When builtin, KUnit tests are all run via the executor at boot, and when
271 * built as a module, they run on module load.
Alan Maguireaac35462020-08-04 13:47:42 -0700272 *
273 */
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200274#define kunit_test_suites(__suites...) \
Alan Maguireaac35462020-08-04 13:47:42 -0700275 __kunit_test_suites(__UNIQUE_ID(array), \
Mauro Carvalho Chehab7f32b102020-10-21 14:17:26 +0200276 ##__suites)
Alan Maguirec475c772020-01-06 22:28:20 +0000277
278#define kunit_test_suite(suite) kunit_test_suites(&suite)
Brendan Higgins914cc632019-09-23 02:02:31 -0700279
Brendan Higgins9bf2eed2022-04-18 21:05:15 -0700280/**
281 * kunit_test_init_section_suites() - used to register one or more &struct
282 * kunit_suite containing init functions or
283 * init data.
284 *
285 * @__suites: a statically allocated list of &struct kunit_suite.
286 *
Mauro Carvalho Chehab7b237942022-07-02 12:07:40 +0100287 * This functions identically as kunit_test_suites() except that it suppresses
Brendan Higgins9bf2eed2022-04-18 21:05:15 -0700288 * modpost warnings for referencing functions marked __init or data marked
289 * __initdata; this is OK because currently KUnit only runs tests upon boot
290 * during the init phase or upon loading a module during the init phase.
291 *
292 * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
293 * tests must be excluded.
294 *
295 * The only thing this macro does that's different from kunit_test_suites is
296 * that it suffixes the array and suite declarations it makes with _probe;
297 * modpost suppresses warnings about referencing init data for symbols named in
298 * this manner.
299 */
300#define kunit_test_init_section_suites(__suites...) \
301 __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
Brendan Higgins9bf2eed2022-04-18 21:05:15 -0700302 ##__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 Latypova8495ad2022-09-30 17:26:35 -0700475 assert_format_t assert_format,
Daniel Latypov4fdacef2022-01-13 08:59:27 -0800476 const char *fmt, ...);
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700477
Daniel Latypov97d453b2022-09-30 17:26:36 -0700478#define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
479 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
Daniel Latypovc1144e02022-09-30 17:26:38 -0700480 const struct assert_class __assertion = INITIALIZER; \
Daniel Latypov97d453b2022-09-30 17:26:36 -0700481 kunit_do_failed_assertion(test, \
482 &__loc, \
483 assert_type, \
484 &__assertion.assert, \
485 assert_format, \
486 fmt, \
487 ##__VA_ARGS__); \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700488} while (0)
489
490
491#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
Daniel Latypov97d453b2022-09-30 17:26:36 -0700492 _KUNIT_FAILED(test, \
493 assert_type, \
494 kunit_fail_assert, \
495 kunit_fail_assert_format, \
496 {}, \
497 fmt, \
498 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700499
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 ...) \
Daniel Latypov97d453b2022-09-30 17:26:36 -0700523do { \
524 if (likely(!!(condition) == !!expected_true)) \
525 break; \
526 \
527 _KUNIT_FAILED(test, \
528 assert_type, \
529 kunit_unary_assert, \
530 kunit_unary_assert_format, \
531 KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
532 expected_true), \
533 fmt, \
534 ##__VA_ARGS__); \
535} while (0)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700536
537#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
538 KUNIT_UNARY_ASSERTION(test, \
539 assert_type, \
540 condition, \
541 true, \
542 fmt, \
543 ##__VA_ARGS__)
544
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700545#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
546 KUNIT_UNARY_ASSERTION(test, \
547 assert_type, \
548 condition, \
549 false, \
550 fmt, \
551 ##__VA_ARGS__)
552
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700553/*
554 * A factory macro for defining the assertions and expectations for the basic
555 * comparisons defined for the built in types.
556 *
557 * Unfortunately, there is no common type that all types can be promoted to for
558 * which all the binary operators behave the same way as for the actual types
559 * (for example, there is no type that long long and unsigned long long can
560 * both be cast to where the comparison result is preserved for all values). So
561 * the best we can do is do the comparison in the original types and then coerce
562 * everything to long long for printing; this way, the comparison behaves
563 * correctly and the printed out value usually makes sense without
564 * interpretation, but can always be interpreted to figure out the actual
565 * value.
566 */
567#define KUNIT_BASE_BINARY_ASSERTION(test, \
568 assert_class, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800569 format_func, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700570 assert_type, \
571 left, \
572 op, \
573 right, \
574 fmt, \
575 ...) \
576do { \
Daniel Latypovc2741452022-01-27 13:52:22 -0800577 const typeof(left) __left = (left); \
578 const typeof(right) __right = (right); \
Daniel Latypov2b6861e2022-01-25 13:00:11 -0800579 static const struct kunit_binary_assert_text __text = { \
580 .operation = #op, \
581 .left_text = #left, \
582 .right_text = #right, \
583 }; \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700584 \
Daniel Latypov97d453b2022-09-30 17:26:36 -0700585 if (likely(__left op __right)) \
586 break; \
587 \
588 _KUNIT_FAILED(test, \
589 assert_type, \
590 assert_class, \
591 format_func, \
592 KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \
593 __left, \
594 __right), \
595 fmt, \
596 ##__VA_ARGS__); \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700597} while (0)
598
Daniel Latypov40f397772022-01-18 14:35:05 -0800599#define KUNIT_BINARY_INT_ASSERTION(test, \
600 assert_type, \
601 left, \
602 op, \
603 right, \
604 fmt, \
605 ...) \
606 KUNIT_BASE_BINARY_ASSERTION(test, \
607 kunit_binary_assert, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800608 kunit_binary_assert_format, \
Daniel Latypov40f397772022-01-18 14:35:05 -0800609 assert_type, \
610 left, op, right, \
611 fmt, \
612 ##__VA_ARGS__)
613
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800614#define KUNIT_BINARY_PTR_ASSERTION(test, \
615 assert_type, \
616 left, \
617 op, \
618 right, \
619 fmt, \
620 ...) \
621 KUNIT_BASE_BINARY_ASSERTION(test, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700622 kunit_binary_ptr_assert, \
Daniel Latypov064ff292022-01-25 13:00:10 -0800623 kunit_binary_ptr_assert_format, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700624 assert_type, \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800625 left, op, right, \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700626 fmt, \
627 ##__VA_ARGS__)
628
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700629#define KUNIT_BINARY_STR_ASSERTION(test, \
630 assert_type, \
631 left, \
632 op, \
633 right, \
634 fmt, \
635 ...) \
636do { \
David Gow3747b5c2021-05-13 12:31:56 -0700637 const char *__left = (left); \
Daniel Latypov2b6861e2022-01-25 13:00:11 -0800638 const char *__right = (right); \
639 static const struct kunit_binary_assert_text __text = { \
640 .operation = #op, \
641 .left_text = #left, \
642 .right_text = #right, \
643 }; \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700644 \
Daniel Latypov97d453b2022-09-30 17:26:36 -0700645 if (likely(strcmp(__left, __right) op 0)) \
646 break; \
647 \
648 \
649 _KUNIT_FAILED(test, \
650 assert_type, \
651 kunit_binary_str_assert, \
652 kunit_binary_str_assert_format, \
653 KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \
654 __left, \
655 __right), \
656 fmt, \
657 ##__VA_ARGS__); \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700658} while (0)
659
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700660#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
661 assert_type, \
662 ptr, \
663 fmt, \
664 ...) \
665do { \
Daniel Latypovc2741452022-01-27 13:52:22 -0800666 const typeof(ptr) __ptr = (ptr); \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700667 \
Daniel Latypov97d453b2022-09-30 17:26:36 -0700668 if (!IS_ERR_OR_NULL(__ptr)) \
669 break; \
670 \
671 _KUNIT_FAILED(test, \
672 assert_type, \
673 kunit_ptr_not_err_assert, \
674 kunit_ptr_not_err_assert_format, \
675 KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr), \
676 fmt, \
677 ##__VA_ARGS__); \
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700678} while (0)
679
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700680/**
681 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
682 * @test: The test context object.
683 * @condition: an arbitrary boolean expression. The test fails when this does
684 * not evaluate to true.
685 *
686 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
687 * to fail when the specified condition is not met; however, it will not prevent
688 * the test case from continuing to run; this is otherwise known as an
689 * *expectation failure*.
690 */
691#define KUNIT_EXPECT_TRUE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800692 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700693
694#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
695 KUNIT_TRUE_MSG_ASSERTION(test, \
696 KUNIT_EXPECTATION, \
697 condition, \
698 fmt, \
699 ##__VA_ARGS__)
700
701/**
702 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
703 * @test: The test context object.
704 * @condition: an arbitrary boolean expression. The test fails when this does
705 * not evaluate to false.
706 *
707 * Sets an expectation that @condition evaluates to false. See
708 * KUNIT_EXPECT_TRUE() for more information.
709 */
710#define KUNIT_EXPECT_FALSE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800711 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700712
713#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
714 KUNIT_FALSE_MSG_ASSERTION(test, \
715 KUNIT_EXPECTATION, \
716 condition, \
717 fmt, \
718 ##__VA_ARGS__)
719
720/**
721 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
722 * @test: The test context object.
723 * @left: an arbitrary expression that evaluates to a primitive C type.
724 * @right: an arbitrary expression that evaluates to a primitive C type.
725 *
726 * Sets an expectation that the values that @left and @right evaluate to are
727 * equal. This is semantically equivalent to
728 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
729 * more information.
730 */
731#define KUNIT_EXPECT_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800732 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700733
734#define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800735 KUNIT_BINARY_INT_ASSERTION(test, \
736 KUNIT_EXPECTATION, \
737 left, ==, right, \
738 fmt, \
739 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700740
741/**
742 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
743 * @test: The test context object.
744 * @left: an arbitrary expression that evaluates to a pointer.
745 * @right: an arbitrary expression that evaluates to a pointer.
746 *
747 * Sets an expectation that the values that @left and @right evaluate to are
748 * equal. This is semantically equivalent to
749 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
750 * more information.
751 */
752#define KUNIT_EXPECT_PTR_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800753 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700754
755#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800756 KUNIT_BINARY_PTR_ASSERTION(test, \
757 KUNIT_EXPECTATION, \
758 left, ==, right, \
759 fmt, \
760 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700761
762/**
763 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
764 * @test: The test context object.
765 * @left: an arbitrary expression that evaluates to a primitive C type.
766 * @right: an arbitrary expression that evaluates to a primitive C type.
767 *
768 * Sets an expectation that the values that @left and @right evaluate to are not
769 * equal. This is semantically equivalent to
770 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
771 * more information.
772 */
773#define KUNIT_EXPECT_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800774 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700775
776#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800777 KUNIT_BINARY_INT_ASSERTION(test, \
778 KUNIT_EXPECTATION, \
779 left, !=, right, \
780 fmt, \
781 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700782
783/**
784 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
785 * @test: The test context object.
786 * @left: an arbitrary expression that evaluates to a pointer.
787 * @right: an arbitrary expression that evaluates to a pointer.
788 *
789 * Sets an expectation that the values that @left and @right evaluate to are not
790 * equal. This is semantically equivalent to
791 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
792 * more information.
793 */
794#define KUNIT_EXPECT_PTR_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800795 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700796
797#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -0800798 KUNIT_BINARY_PTR_ASSERTION(test, \
799 KUNIT_EXPECTATION, \
800 left, !=, right, \
801 fmt, \
802 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700803
804/**
805 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
806 * @test: The test context object.
807 * @left: an arbitrary expression that evaluates to a primitive C type.
808 * @right: an arbitrary expression that evaluates to a primitive C type.
809 *
810 * Sets an expectation that the value that @left evaluates to is less than the
811 * value that @right evaluates to. This is semantically equivalent to
812 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
813 * more information.
814 */
815#define KUNIT_EXPECT_LT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800816 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700817
818#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800819 KUNIT_BINARY_INT_ASSERTION(test, \
820 KUNIT_EXPECTATION, \
821 left, <, right, \
822 fmt, \
823 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700824
825/**
826 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
827 * @test: The test context object.
828 * @left: an arbitrary expression that evaluates to a primitive C type.
829 * @right: an arbitrary expression that evaluates to a primitive C type.
830 *
831 * Sets an expectation that the value that @left evaluates to is less than or
832 * equal to the value that @right evaluates to. Semantically this is equivalent
833 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
834 * more information.
835 */
836#define KUNIT_EXPECT_LE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800837 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700838
839#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800840 KUNIT_BINARY_INT_ASSERTION(test, \
Sander Vanheuleaded3ca2022-08-21 17:01:47 +0200841 KUNIT_EXPECTATION, \
Daniel Latypov40f397772022-01-18 14:35:05 -0800842 left, <=, right, \
843 fmt, \
844 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700845
846/**
847 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
848 * @test: The test context object.
849 * @left: an arbitrary expression that evaluates to a primitive C type.
850 * @right: an arbitrary expression that evaluates to a primitive C type.
851 *
852 * Sets an expectation that the value that @left evaluates to is greater than
853 * the value that @right evaluates to. This is semantically equivalent to
854 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
855 * more information.
856 */
857#define KUNIT_EXPECT_GT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800858 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700859
860#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800861 KUNIT_BINARY_INT_ASSERTION(test, \
862 KUNIT_EXPECTATION, \
863 left, >, right, \
864 fmt, \
865 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700866
867/**
868 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
869 * @test: The test context object.
870 * @left: an arbitrary expression that evaluates to a primitive C type.
871 * @right: an arbitrary expression that evaluates to a primitive C type.
872 *
873 * Sets an expectation that the value that @left evaluates to is greater than
874 * the value that @right evaluates to. This is semantically equivalent to
875 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
876 * more information.
877 */
878#define KUNIT_EXPECT_GE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800879 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700880
881#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -0800882 KUNIT_BINARY_INT_ASSERTION(test, \
883 KUNIT_EXPECTATION, \
884 left, >=, right, \
885 fmt, \
886 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700887
888/**
889 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
890 * @test: The test context object.
891 * @left: an arbitrary expression that evaluates to a null terminated string.
892 * @right: an arbitrary expression that evaluates to a null terminated string.
893 *
894 * Sets an expectation that the values that @left and @right evaluate to are
895 * equal. This is semantically equivalent to
896 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
897 * for more information.
898 */
899#define KUNIT_EXPECT_STREQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800900 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700901
902#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -0800903 KUNIT_BINARY_STR_ASSERTION(test, \
904 KUNIT_EXPECTATION, \
905 left, ==, right, \
906 fmt, \
907 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700908
909/**
910 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
911 * @test: The test context object.
912 * @left: an arbitrary expression that evaluates to a null terminated string.
913 * @right: an arbitrary expression that evaluates to a null terminated string.
914 *
915 * Sets an expectation that the values that @left and @right evaluate to are
916 * not equal. This is semantically equivalent to
917 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
918 * for more information.
919 */
920#define KUNIT_EXPECT_STRNEQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800921 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700922
923#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -0800924 KUNIT_BINARY_STR_ASSERTION(test, \
925 KUNIT_EXPECTATION, \
926 left, !=, right, \
927 fmt, \
928 ##__VA_ARGS__)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700929
930/**
Ricardo Ribaldacaae9452022-02-11 17:42:41 +0100931 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
932 * @test: The test context object.
933 * @ptr: an arbitrary pointer.
934 *
935 * Sets an expectation that the value that @ptr evaluates to is null. This is
936 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
937 * See KUNIT_EXPECT_TRUE() for more information.
938 */
939#define KUNIT_EXPECT_NULL(test, ptr) \
940 KUNIT_EXPECT_NULL_MSG(test, \
941 ptr, \
942 NULL)
943
944#define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
945 KUNIT_BINARY_PTR_ASSERTION(test, \
946 KUNIT_EXPECTATION, \
947 ptr, ==, NULL, \
948 fmt, \
949 ##__VA_ARGS__)
950
951/**
952 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
953 * @test: The test context object.
954 * @ptr: an arbitrary pointer.
955 *
956 * Sets an expectation that the value that @ptr evaluates to is not null. This
957 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
958 * See KUNIT_EXPECT_TRUE() for more information.
959 */
960#define KUNIT_EXPECT_NOT_NULL(test, ptr) \
961 KUNIT_EXPECT_NOT_NULL_MSG(test, \
962 ptr, \
963 NULL)
964
965#define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
966 KUNIT_BINARY_PTR_ASSERTION(test, \
967 KUNIT_EXPECTATION, \
968 ptr, !=, NULL, \
969 fmt, \
970 ##__VA_ARGS__)
971
972/**
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700973 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
974 * @test: The test context object.
975 * @ptr: an arbitrary pointer.
976 *
977 * Sets an expectation that the value that @ptr evaluates to is not null and not
978 * an errno stored in a pointer. This is semantically equivalent to
979 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
980 * more information.
981 */
982#define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -0800983 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
Brendan Higgins73cda7b2019-09-23 02:02:35 -0700984
985#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
986 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
987 KUNIT_EXPECTATION, \
988 ptr, \
989 fmt, \
990 ##__VA_ARGS__)
991
Brendan Higginse4aea8f2019-09-23 02:02:41 -0700992#define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
993 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
994
995/**
996 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
997 * @test: The test context object.
998 * @condition: an arbitrary boolean expression. The test fails and aborts when
999 * this does not evaluate to true.
1000 *
1001 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1002 * fail *and immediately abort* when the specified condition is not met. Unlike
1003 * an expectation failure, it will prevent the test case from continuing to run;
1004 * this is otherwise known as an *assertion failure*.
1005 */
1006#define KUNIT_ASSERT_TRUE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001007 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001008
1009#define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1010 KUNIT_TRUE_MSG_ASSERTION(test, \
1011 KUNIT_ASSERTION, \
1012 condition, \
1013 fmt, \
1014 ##__VA_ARGS__)
1015
1016/**
1017 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1018 * @test: The test context object.
1019 * @condition: an arbitrary boolean expression.
1020 *
1021 * Sets an assertion that the value that @condition evaluates to is false. This
1022 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1023 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1024 */
1025#define KUNIT_ASSERT_FALSE(test, condition) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001026 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001027
1028#define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1029 KUNIT_FALSE_MSG_ASSERTION(test, \
1030 KUNIT_ASSERTION, \
1031 condition, \
1032 fmt, \
1033 ##__VA_ARGS__)
1034
1035/**
1036 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1037 * @test: The test context object.
1038 * @left: an arbitrary expression that evaluates to a primitive C type.
1039 * @right: an arbitrary expression that evaluates to a primitive C type.
1040 *
1041 * Sets an assertion that the values that @left and @right evaluate to are
1042 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1043 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1044 */
1045#define KUNIT_ASSERT_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001046 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001047
1048#define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001049 KUNIT_BINARY_INT_ASSERTION(test, \
1050 KUNIT_ASSERTION, \
1051 left, ==, right, \
1052 fmt, \
1053 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001054
1055/**
1056 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1057 * @test: The test context object.
1058 * @left: an arbitrary expression that evaluates to a pointer.
1059 * @right: an arbitrary expression that evaluates to a pointer.
1060 *
1061 * Sets an assertion that the values that @left and @right evaluate to are
1062 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1063 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1064 */
1065#define KUNIT_ASSERT_PTR_EQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001066 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001067
1068#define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001069 KUNIT_BINARY_PTR_ASSERTION(test, \
1070 KUNIT_ASSERTION, \
1071 left, ==, right, \
1072 fmt, \
1073 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001074
1075/**
1076 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1077 * @test: The test context object.
1078 * @left: an arbitrary expression that evaluates to a primitive C type.
1079 * @right: an arbitrary expression that evaluates to a primitive C type.
1080 *
1081 * Sets an assertion that the values that @left and @right evaluate to are not
1082 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1083 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1084 */
1085#define KUNIT_ASSERT_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001086 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001087
1088#define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001089 KUNIT_BINARY_INT_ASSERTION(test, \
1090 KUNIT_ASSERTION, \
1091 left, !=, right, \
1092 fmt, \
1093 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001094
1095/**
1096 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1097 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1098 * @test: The test context object.
1099 * @left: an arbitrary expression that evaluates to a pointer.
1100 * @right: an arbitrary expression that evaluates to a pointer.
1101 *
1102 * Sets an assertion that the values that @left and @right evaluate to are not
1103 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1104 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1105 */
1106#define KUNIT_ASSERT_PTR_NE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001107 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001108
1109#define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
Daniel Latypov6125a5c2022-01-18 14:35:06 -08001110 KUNIT_BINARY_PTR_ASSERTION(test, \
1111 KUNIT_ASSERTION, \
1112 left, !=, right, \
1113 fmt, \
1114 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001115/**
1116 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1117 * @test: The test context object.
1118 * @left: an arbitrary expression that evaluates to a primitive C type.
1119 * @right: an arbitrary expression that evaluates to a primitive C type.
1120 *
1121 * Sets an assertion that the value that @left evaluates to is less than the
1122 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1123 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1124 * is not met.
1125 */
1126#define KUNIT_ASSERT_LT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001127 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001128
1129#define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001130 KUNIT_BINARY_INT_ASSERTION(test, \
Sander Vanheuleaded3ca2022-08-21 17:01:47 +02001131 KUNIT_ASSERTION, \
Daniel Latypov40f397772022-01-18 14:35:05 -08001132 left, <, right, \
1133 fmt, \
1134 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001135/**
1136 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1137 * @test: The test context object.
1138 * @left: an arbitrary expression that evaluates to a primitive C type.
1139 * @right: an arbitrary expression that evaluates to a primitive C type.
1140 *
1141 * Sets an assertion that the value that @left evaluates to is less than or
1142 * equal to the value that @right evaluates to. This is the same as
1143 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1144 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1145 */
1146#define KUNIT_ASSERT_LE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001147 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001148
1149#define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001150 KUNIT_BINARY_INT_ASSERTION(test, \
1151 KUNIT_ASSERTION, \
1152 left, <=, right, \
1153 fmt, \
1154 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001155
1156/**
1157 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1158 * @test: The test context object.
1159 * @left: an arbitrary expression that evaluates to a primitive C type.
1160 * @right: an arbitrary expression that evaluates to a primitive C type.
1161 *
1162 * Sets an assertion that the value that @left evaluates to is greater than the
1163 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1164 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1165 * is not met.
1166 */
1167#define KUNIT_ASSERT_GT(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001168 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001169
1170#define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001171 KUNIT_BINARY_INT_ASSERTION(test, \
Sander Vanheuleaded3ca2022-08-21 17:01:47 +02001172 KUNIT_ASSERTION, \
Daniel Latypov40f397772022-01-18 14:35:05 -08001173 left, >, right, \
1174 fmt, \
1175 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001176
1177/**
1178 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1179 * @test: The test context object.
1180 * @left: an arbitrary expression that evaluates to a primitive C type.
1181 * @right: an arbitrary expression that evaluates to a primitive C type.
1182 *
1183 * Sets an assertion that the value that @left evaluates to is greater than the
1184 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1185 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1186 * is not met.
1187 */
1188#define KUNIT_ASSERT_GE(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001189 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001190
1191#define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
Daniel Latypov40f397772022-01-18 14:35:05 -08001192 KUNIT_BINARY_INT_ASSERTION(test, \
1193 KUNIT_ASSERTION, \
1194 left, >=, right, \
1195 fmt, \
1196 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001197
1198/**
1199 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1200 * @test: The test context object.
1201 * @left: an arbitrary expression that evaluates to a null terminated string.
1202 * @right: an arbitrary expression that evaluates to a null terminated string.
1203 *
1204 * Sets an assertion that the values that @left and @right evaluate to are
1205 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1206 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1207 */
1208#define KUNIT_ASSERT_STREQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001209 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001210
1211#define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -08001212 KUNIT_BINARY_STR_ASSERTION(test, \
1213 KUNIT_ASSERTION, \
1214 left, ==, right, \
1215 fmt, \
1216 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001217
1218/**
1219 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1220 * @test: The test context object.
1221 * @left: an arbitrary expression that evaluates to a null terminated string.
1222 * @right: an arbitrary expression that evaluates to a null terminated string.
1223 *
1224 * Sets an expectation that the values that @left and @right evaluate to are
1225 * not equal. This is semantically equivalent to
1226 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1227 * for more information.
1228 */
1229#define KUNIT_ASSERT_STRNEQ(test, left, right) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001230 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001231
1232#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
Daniel Latypov955df7d2022-01-18 14:35:04 -08001233 KUNIT_BINARY_STR_ASSERTION(test, \
1234 KUNIT_ASSERTION, \
1235 left, !=, right, \
1236 fmt, \
1237 ##__VA_ARGS__)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001238
1239/**
Ricardo Ribaldacaae9452022-02-11 17:42:41 +01001240 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1241 * @test: The test context object.
1242 * @ptr: an arbitrary pointer.
1243 *
1244 * Sets an assertion that the values that @ptr evaluates to is null. This is
1245 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1246 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1247 */
1248#define KUNIT_ASSERT_NULL(test, ptr) \
1249 KUNIT_ASSERT_NULL_MSG(test, \
1250 ptr, \
1251 NULL)
1252
1253#define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1254 KUNIT_BINARY_PTR_ASSERTION(test, \
1255 KUNIT_ASSERTION, \
1256 ptr, ==, NULL, \
1257 fmt, \
1258 ##__VA_ARGS__)
1259
1260/**
1261 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1262 * @test: The test context object.
1263 * @ptr: an arbitrary pointer.
1264 *
1265 * Sets an assertion that the values that @ptr evaluates to is not null. This
1266 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1267 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1268 */
1269#define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1270 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1271 ptr, \
1272 NULL)
1273
1274#define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1275 KUNIT_BINARY_PTR_ASSERTION(test, \
1276 KUNIT_ASSERTION, \
1277 ptr, !=, NULL, \
1278 fmt, \
1279 ##__VA_ARGS__)
1280
1281/**
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001282 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1283 * @test: The test context object.
1284 * @ptr: an arbitrary pointer.
1285 *
1286 * Sets an assertion that the value that @ptr evaluates to is not null and not
1287 * an errno stored in a pointer. This is the same as
1288 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1289 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1290 */
1291#define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
Daniel Latypov6709d0f2022-01-18 14:35:02 -08001292 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
Brendan Higginse4aea8f2019-09-23 02:02:41 -07001293
1294#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1295 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1296 KUNIT_ASSERTION, \
1297 ptr, \
1298 fmt, \
1299 ##__VA_ARGS__)
1300
Arpitha Raghunandanfadb08e72020-11-16 11:10:35 +05301301/**
1302 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1303 * @name: prefix for the test parameter generator function.
1304 * @array: array of test parameters.
1305 * @get_desc: function to convert param to description; NULL to use default
1306 *
1307 * Define function @name_gen_params which uses @array to generate parameters.
1308 */
1309#define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1310 static const void *name##_gen_params(const void *prev, char *desc) \
1311 { \
1312 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1313 if (__next - (array) < ARRAY_SIZE((array))) { \
1314 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1315 if (__get_desc) \
1316 __get_desc(__next, desc); \
1317 return __next; \
1318 } \
1319 return NULL; \
1320 }
1321
Daniel Latypov61695f82022-03-28 10:41:42 -07001322// TODO([email protected]): consider eventually migrating users to explicitly
1323// include resource.h themselves if they need it.
1324#include <kunit/resource.h>
1325
Brendan Higgins914cc632019-09-23 02:02:31 -07001326#endif /* _KUNIT_TEST_H */