kunit: Make 'list' action available to kunit test modules
[linux-2.6-block.git] / include / kunit / test.h
CommitLineData
914cc63e
BH
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Base unit test (KUnit) API.
4 *
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
7 */
8
9#ifndef _KUNIT_TEST_H
10#define _KUNIT_TEST_H
11
73cda7bb 12#include <kunit/assert.h>
5f3e0620 13#include <kunit/try-catch.h>
ec54c289 14
4fdacef8 15#include <linux/compiler.h>
ec54c289
AS
16#include <linux/container_of.h>
17#include <linux/err.h>
18#include <linux/init.h>
908d0c17 19#include <linux/jump_label.h>
ec54c289
AS
20#include <linux/kconfig.h>
21#include <linux/kref.h>
22#include <linux/list.h>
c475c77d 23#include <linux/module.h>
0a756853 24#include <linux/slab.h>
ec54c289
AS
25#include <linux/spinlock.h>
26#include <linux/string.h>
914cc63e 27#include <linux/types.h>
ec54c289
AS
28
29#include <asm/rwonce.h>
914cc63e 30
908d0c17
DG
31/* Static key: true if any KUnit tests are currently running */
32DECLARE_STATIC_KEY_FALSE(kunit_running);
33
914cc63e
BH
34struct kunit;
35
e2219db2 36/* Size of log associated with test. */
7232282d 37#define KUNIT_LOG_SIZE 2048
e2219db2 38
fadb08e7
AR
39/* Maximum size of parameter description string. */
40#define KUNIT_PARAM_DESC_SIZE 128
41
6d2426b2
DG
42/* Maximum size of a status comment. */
43#define KUNIT_STATUS_COMMENT_SIZE 256
44
c3bba690
AM
45/*
46 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
47 * sub-subtest. See the "Subtests" section in
48 * https://node-tap.org/tap-protocol/
49 */
b1eaa8b2 50#define KUNIT_INDENT_LEN 4
c3bba690
AM
51#define KUNIT_SUBTEST_INDENT " "
52#define KUNIT_SUBSUBTEST_INDENT " "
53
6d2426b2
DG
54/**
55 * enum kunit_status - Type of result for a test or test suite
56 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
57 * @KUNIT_FAILURE: Denotes the test has failed.
58 * @KUNIT_SKIPPED: Denotes the test has been skipped.
59 */
60enum kunit_status {
61 KUNIT_SUCCESS,
62 KUNIT_FAILURE,
63 KUNIT_SKIPPED,
64};
65
02c2d0c2
RM
66/* Attribute struct/enum definitions */
67
68/*
69 * Speed Attribute is stored as an enum and separated into categories of
70 * speed: very_slowm, slow, and normal. These speeds are relative to
71 * other KUnit tests.
72 *
73 * Note: unset speed attribute acts as default of KUNIT_SPEED_NORMAL.
74 */
75enum kunit_speed {
76 KUNIT_SPEED_UNSET,
77 KUNIT_SPEED_VERY_SLOW,
78 KUNIT_SPEED_SLOW,
79 KUNIT_SPEED_NORMAL,
80 KUNIT_SPEED_MAX = KUNIT_SPEED_NORMAL,
81};
82
39e92cb1 83/* Holds attributes for each test case and suite */
02c2d0c2
RM
84struct kunit_attributes {
85 enum kunit_speed speed;
86};
39e92cb1 87
914cc63e
BH
88/**
89 * struct kunit_case - represents an individual test case.
90 *
91 * @run_case: the function representing the actual test case.
92 * @name: the name of the test case.
fadb08e7 93 * @generate_params: the generator function for parameterized tests.
39e92cb1 94 * @attr: the attributes associated with the test
914cc63e
BH
95 *
96 * A test case is a function with the signature,
e4aea8f8
BH
97 * ``void (*)(struct kunit *)``
98 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
99 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
914cc63e
BH
100 * with a &struct kunit_suite and will be run after the suite's init
101 * function and followed by the suite's exit function.
102 *
103 * A test case should be static and should only be created with the
104 * KUNIT_CASE() macro; additionally, every array of test cases should be
105 * terminated with an empty test case.
106 *
107 * Example:
108 *
109 * .. code-block:: c
110 *
111 * void add_test_basic(struct kunit *test)
112 * {
113 * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
114 * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
115 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
116 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
117 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
118 * }
119 *
120 * static struct kunit_case example_test_cases[] = {
121 * KUNIT_CASE(add_test_basic),
122 * {}
123 * };
124 *
125 */
126struct kunit_case {
127 void (*run_case)(struct kunit *test);
128 const char *name;
fadb08e7 129 const void* (*generate_params)(const void *prev, char *desc);
39e92cb1 130 struct kunit_attributes attr;
914cc63e
BH
131
132 /* private: internal use only. */
6d2426b2 133 enum kunit_status status;
a00a7270 134 char *module_name;
e2219db2 135 char *log;
914cc63e
BH
136};
137
6d2426b2 138static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
e2219db2 139{
6d2426b2
DG
140 switch (status) {
141 case KUNIT_SKIPPED:
142 case KUNIT_SUCCESS:
143 return "ok";
144 case KUNIT_FAILURE:
145 return "not ok";
146 }
147 return "invalid";
e2219db2
AM
148}
149
914cc63e
BH
150/**
151 * KUNIT_CASE - A helper for creating a &struct kunit_case
152 *
153 * @test_name: a reference to a test case function.
154 *
155 * Takes a symbol for a function representing a test case and creates a
156 * &struct kunit_case object from it. See the documentation for
157 * &struct kunit_case for an example on how to use it.
158 */
a00a7270
RM
159#define KUNIT_CASE(test_name) \
160 { .run_case = test_name, .name = #test_name, \
161 .module_name = KBUILD_MODNAME}
914cc63e 162
39e92cb1
RM
163/**
164 * KUNIT_CASE_ATTR - A helper for creating a &struct kunit_case
165 * with attributes
166 *
167 * @test_name: a reference to a test case function.
168 * @attributes: a reference to a struct kunit_attributes object containing
169 * test attributes
170 */
171#define KUNIT_CASE_ATTR(test_name, attributes) \
172 { .run_case = test_name, .name = #test_name, \
a00a7270 173 .attr = attributes, .module_name = KBUILD_MODNAME}
39e92cb1 174
02c2d0c2
RM
175/**
176 * KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case
177 * with the slow attribute
178 *
179 * @test_name: a reference to a test case function.
180 */
181
182#define KUNIT_CASE_SLOW(test_name) \
183 { .run_case = test_name, .name = #test_name, \
a00a7270 184 .attr.speed = KUNIT_SPEED_SLOW, .module_name = KBUILD_MODNAME}
02c2d0c2 185
fadb08e7
AR
186/**
187 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
188 *
189 * @test_name: a reference to a test case function.
190 * @gen_params: a reference to a parameter generator function.
191 *
192 * The generator function::
193 *
194 * const void* gen_params(const void *prev, char *desc)
195 *
196 * is used to lazily generate a series of arbitrarily typed values that fit into
197 * a void*. The argument @prev is the previously returned value, which should be
198 * used to derive the next value; @prev is set to NULL on the initial generator
199 * call. When no more values are available, the generator must return NULL.
200 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
201 * describing the parameter.
202 */
203#define KUNIT_CASE_PARAM(test_name, gen_params) \
204 { .run_case = test_name, .name = #test_name, \
a00a7270 205 .generate_params = gen_params, .module_name = KBUILD_MODNAME}
fadb08e7 206
39e92cb1
RM
207/**
208 * KUNIT_CASE_PARAM_ATTR - A helper for creating a parameterized &struct
209 * kunit_case with attributes
210 *
211 * @test_name: a reference to a test case function.
212 * @gen_params: a reference to a parameter generator function.
213 * @attributes: a reference to a struct kunit_attributes object containing
214 * test attributes
215 */
216#define KUNIT_CASE_PARAM_ATTR(test_name, gen_params, attributes) \
217 { .run_case = test_name, .name = #test_name, \
218 .generate_params = gen_params, \
a00a7270 219 .attr = attributes, .module_name = KBUILD_MODNAME}
39e92cb1 220
914cc63e
BH
221/**
222 * struct kunit_suite - describes a related collection of &struct kunit_case
223 *
224 * @name: the name of the test. Purely informational.
1cdba21d
DL
225 * @suite_init: called once per test suite before the test cases.
226 * @suite_exit: called once per test suite after all test cases.
914cc63e
BH
227 * @init: called before every test case.
228 * @exit: called after every test case.
229 * @test_cases: a null terminated array of test cases.
39e92cb1 230 * @attr: the attributes associated with the test suite
914cc63e
BH
231 *
232 * A kunit_suite is a collection of related &struct kunit_case s, such that
233 * @init is called before every test case and @exit is called after every
234 * test case, similar to the notion of a *test fixture* or a *test class*
235 * in other unit testing frameworks like JUnit or Googletest.
236 *
cdc87bda
DG
237 * Note that @exit and @suite_exit will run even if @init or @suite_init
238 * fail: make sure they can handle any inconsistent state which may result.
239 *
914cc63e
BH
240 * Every &struct kunit_case must be associated with a kunit_suite for KUnit
241 * to run it.
242 */
243struct kunit_suite {
244 const char name[256];
1cdba21d
DL
245 int (*suite_init)(struct kunit_suite *suite);
246 void (*suite_exit)(struct kunit_suite *suite);
914cc63e
BH
247 int (*init)(struct kunit *test);
248 void (*exit)(struct kunit *test);
249 struct kunit_case *test_cases;
39e92cb1 250 struct kunit_attributes attr;
e2219db2 251
c4714b00 252 /* private: internal use only */
6d2426b2 253 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
e2219db2
AM
254 struct dentry *debugfs;
255 char *log;
1cdba21d 256 int suite_init_err;
914cc63e
BH
257};
258
c95e7c05
JK
259/* Stores an array of suites, end points one past the end */
260struct kunit_suite_set {
261 struct kunit_suite * const *start;
262 struct kunit_suite * const *end;
263};
264
914cc63e
BH
265/**
266 * struct kunit - represents a running instance of a test.
267 *
268 * @priv: for user to store arbitrary data. Commonly used to pass data
269 * created in the init function (see &struct kunit_suite).
270 *
271 * Used to store information about the current context under which the test
272 * is running. Most of this data is private and should only be accessed
273 * indirectly via public functions; the one exception is @priv which can be
274 * used by the test writer to store arbitrary data.
275 */
276struct kunit {
277 void *priv;
278
279 /* private: internal use only. */
280 const char *name; /* Read only after initialization! */
e2219db2 281 char *log; /* Points at case log after initialization */
5f3e0620 282 struct kunit_try_catch try_catch;
fadb08e7
AR
283 /* param_value is the current parameter value for a test case. */
284 const void *param_value;
285 /* param_index stores the index of the parameter in parameterized tests. */
286 int param_index;
914cc63e
BH
287 /*
288 * success starts as true, and may only be set to false during a
289 * test case; thus, it is safe to update this across multiple
290 * threads using WRITE_ONCE; however, as a consequence, it may only
291 * be read after the test case finishes once all threads associated
292 * with the test case have terminated.
293 */
0a756853 294 spinlock_t lock; /* Guards all mutable test state. */
6d2426b2 295 enum kunit_status status; /* Read only after test_case finishes! */
0a756853
BH
296 /*
297 * Because resources is a list that may be updated multiple times (with
298 * new resources) from any thread associated with a test case, we must
299 * protect it with some type of lock.
300 */
301 struct list_head resources; /* Protected by lock. */
6d2426b2
DG
302
303 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
914cc63e
BH
304};
305
83c4e7a0
PA
306static inline void kunit_set_failure(struct kunit *test)
307{
6d2426b2 308 WRITE_ONCE(test->status, KUNIT_FAILURE);
83c4e7a0
PA
309}
310
d20a6ba5 311bool kunit_enabled(void);
18258c60 312const char *kunit_action(void);
d20a6ba5 313
e2219db2 314void kunit_init_test(struct kunit *test, const char *name, char *log);
914cc63e
BH
315
316int kunit_run_tests(struct kunit_suite *suite);
317
e2219db2
AM
318size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
319
320unsigned int kunit_test_case_num(struct kunit_suite *suite,
321 struct kunit_case *test_case);
322
e5857d39 323int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
e2219db2 324
e5857d39 325void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
e2219db2 326
c95e7c05 327void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin);
18258c60 328void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr);
c95e7c05 329
8c0d8849
BH
330#if IS_BUILTIN(CONFIG_KUNIT)
331int kunit_run_all_tests(void);
332#else
333static inline int kunit_run_all_tests(void)
334{
335 return 0;
336}
337#endif /* IS_BUILTIN(CONFIG_KUNIT) */
338
e5857d39 339#define __kunit_test_suites(unique_array, ...) \
e5857d39
DL
340 static struct kunit_suite *unique_array[] \
341 __aligned(sizeof(struct kunit_suite *)) \
342 __used __section(".kunit_test_suites") = { __VA_ARGS__ }
aac35468
AM
343
344/**
345 * kunit_test_suites() - used to register one or more &struct kunit_suite
346 * with KUnit.
347 *
7f32b10c 348 * @__suites: a statically allocated list of &struct kunit_suite.
aac35468 349 *
3d6e4462
JK
350 * Registers @suites with the test framework.
351 * This is done by placing the array of struct kunit_suite * in the
352 * .kunit_test_suites ELF section.
aac35468 353 *
3d6e4462
JK
354 * When builtin, KUnit tests are all run via the executor at boot, and when
355 * built as a module, they run on module load.
aac35468
AM
356 *
357 */
7f32b10c 358#define kunit_test_suites(__suites...) \
aac35468 359 __kunit_test_suites(__UNIQUE_ID(array), \
7f32b10c 360 ##__suites)
c475c77d
AM
361
362#define kunit_test_suite(suite) kunit_test_suites(&suite)
914cc63e 363
9bf2eed9
BH
364/**
365 * kunit_test_init_section_suites() - used to register one or more &struct
366 * kunit_suite containing init functions or
367 * init data.
368 *
369 * @__suites: a statically allocated list of &struct kunit_suite.
370 *
7b237945 371 * This functions identically as kunit_test_suites() except that it suppresses
9bf2eed9
BH
372 * modpost warnings for referencing functions marked __init or data marked
373 * __initdata; this is OK because currently KUnit only runs tests upon boot
374 * during the init phase or upon loading a module during the init phase.
375 *
376 * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
377 * tests must be excluded.
378 *
379 * The only thing this macro does that's different from kunit_test_suites is
380 * that it suffixes the array and suite declarations it makes with _probe;
381 * modpost suppresses warnings about referencing init data for symbols named in
382 * this manner.
383 */
384#define kunit_test_init_section_suites(__suites...) \
385 __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
9bf2eed9
BH
386 ##__suites)
387
388#define kunit_test_init_section_suite(suite) \
389 kunit_test_init_section_suites(&suite)
390
e2219db2
AM
391#define kunit_suite_for_each_test_case(suite, test_case) \
392 for (test_case = suite->test_cases; test_case->run_case; test_case++)
393
6d2426b2 394enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
e2219db2 395
0a756853 396/**
7122debb 397 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
0a756853 398 * @test: The test context object.
7122debb 399 * @n: number of elements.
0a756853
BH
400 * @size: The size in bytes of the desired memory.
401 * @gfp: flags passed to underlying kmalloc().
402 *
7122debb 403 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
57e3cded
DG
404 * and is automatically cleaned up after the test case concludes. See kunit_add_action()
405 * for more information.
406 *
407 * Note that some internal context data is also allocated with GFP_KERNEL,
408 * regardless of the gfp passed in.
0a756853 409 */
361b57df 410void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
7122debb
DL
411
412/**
413 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
414 * @test: The test context object.
415 * @size: The size in bytes of the desired memory.
416 * @gfp: flags passed to underlying kmalloc().
417 *
418 * See kmalloc() and kunit_kmalloc_array() for more information.
57e3cded
DG
419 *
420 * Note that some internal context data is also allocated with GFP_KERNEL,
421 * regardless of the gfp passed in.
7122debb
DL
422 */
423static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
424{
425 return kunit_kmalloc_array(test, 1, size, gfp);
426}
0a756853
BH
427
428/**
429 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
430 * @test: The test case to which the resource belongs.
431 * @ptr: The memory allocation to free.
432 */
433void kunit_kfree(struct kunit *test, const void *ptr);
434
435/**
436 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
437 * @test: The test context object.
438 * @size: The size in bytes of the desired memory.
439 * @gfp: flags passed to underlying kmalloc().
440 *
7122debb 441 * See kzalloc() and kunit_kmalloc_array() for more information.
0a756853
BH
442 */
443static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
444{
445 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
446}
447
7122debb
DL
448/**
449 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
450 * @test: The test context object.
451 * @n: number of elements.
452 * @size: The size in bytes of the desired memory.
453 * @gfp: flags passed to underlying kmalloc().
454 *
455 * See kcalloc() and kunit_kmalloc_array() for more information.
456 */
361b57df 457static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
7122debb 458{
361b57df 459 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
7122debb
DL
460}
461
0a756853
BH
462void kunit_cleanup(struct kunit *test);
463
44acdbb2 464void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
e2219db2 465
6d2426b2
DG
466/**
467 * kunit_mark_skipped() - Marks @test_or_suite as skipped
468 *
469 * @test_or_suite: The test context object.
470 * @fmt: A printk() style format string.
471 *
472 * Marks the test as skipped. @fmt is given output as the test status
473 * comment, typically the reason the test was skipped.
474 *
475 * Test execution continues after kunit_mark_skipped() is called.
476 */
477#define kunit_mark_skipped(test_or_suite, fmt, ...) \
478 do { \
479 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
480 scnprintf((test_or_suite)->status_comment, \
481 KUNIT_STATUS_COMMENT_SIZE, \
482 fmt, ##__VA_ARGS__); \
483 } while (0)
484
485/**
486 * kunit_skip() - Marks @test_or_suite as skipped
487 *
488 * @test_or_suite: The test context object.
489 * @fmt: A printk() style format string.
490 *
491 * Skips the test. @fmt is given output as the test status
492 * comment, typically the reason the test was skipped.
493 *
494 * Test execution is halted after kunit_skip() is called.
495 */
496#define kunit_skip(test_or_suite, fmt, ...) \
497 do { \
498 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
499 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
500 } while (0)
e2219db2
AM
501
502/*
503 * printk and log to per-test or per-suite log buffer. Logging only done
504 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
505 */
506#define kunit_log(lvl, test_or_suite, fmt, ...) \
507 do { \
508 printk(lvl fmt, ##__VA_ARGS__); \
2c6a96da 509 kunit_log_append((test_or_suite)->log, fmt, \
e2219db2
AM
510 ##__VA_ARGS__); \
511 } while (0)
512
513#define kunit_printk(lvl, test, fmt, ...) \
c3bba690
AM
514 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
515 (test)->name, ##__VA_ARGS__)
914cc63e
BH
516
517/**
518 * kunit_info() - Prints an INFO level message associated with @test.
519 *
520 * @test: The test context object.
521 * @fmt: A printk() style format string.
522 *
523 * Prints an info level message associated with the test suite being run.
524 * Takes a variable number of format parameters just like printk().
525 */
526#define kunit_info(test, fmt, ...) \
527 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
528
529/**
530 * kunit_warn() - Prints a WARN level message associated with @test.
531 *
532 * @test: The test context object.
533 * @fmt: A printk() style format string.
534 *
535 * Prints a warning level message.
536 */
537#define kunit_warn(test, fmt, ...) \
538 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
539
540/**
541 * kunit_err() - Prints an ERROR level message associated with @test.
542 *
543 * @test: The test context object.
544 * @fmt: A printk() style format string.
545 *
546 * Prints an error level message.
547 */
548#define kunit_err(test, fmt, ...) \
549 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
550
73cda7bb
BH
551/**
552 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
553 * @test: The test context object.
554 *
555 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
556 * words, it does nothing and only exists for code clarity. See
557 * KUNIT_EXPECT_TRUE() for more information.
558 */
559#define KUNIT_SUCCEED(test) do {} while (0)
560
26075518
DG
561void __noreturn __kunit_abort(struct kunit *test);
562
563void __kunit_do_failed_assertion(struct kunit *test,
21957f90
DL
564 const struct kunit_loc *loc,
565 enum kunit_assert_type type,
7466886b 566 const struct kunit_assert *assert,
a8495ad8 567 assert_format_t assert_format,
4fdacef8 568 const char *fmt, ...);
73cda7bb 569
97d453bc
DL
570#define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
571 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
c1144e01 572 const struct assert_class __assertion = INITIALIZER; \
26075518
DG
573 __kunit_do_failed_assertion(test, \
574 &__loc, \
575 assert_type, \
576 &__assertion.assert, \
577 assert_format, \
578 fmt, \
579 ##__VA_ARGS__); \
580 if (assert_type == KUNIT_ASSERTION) \
581 __kunit_abort(test); \
73cda7bb
BH
582} while (0)
583
584
585#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
97d453bc
DL
586 _KUNIT_FAILED(test, \
587 assert_type, \
588 kunit_fail_assert, \
589 kunit_fail_assert_format, \
590 {}, \
591 fmt, \
592 ##__VA_ARGS__)
73cda7bb
BH
593
594/**
595 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
596 * @test: The test context object.
597 * @fmt: an informational message to be printed when the assertion is made.
598 * @...: string format arguments.
599 *
600 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
601 * other words, it always results in a failed expectation, and consequently
602 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
603 * for more information.
604 */
605#define KUNIT_FAIL(test, fmt, ...) \
606 KUNIT_FAIL_ASSERTION(test, \
607 KUNIT_EXPECTATION, \
608 fmt, \
609 ##__VA_ARGS__)
610
697365c0
DL
611/* Helper to safely pass around an initializer list to other macros. */
612#define KUNIT_INIT_ASSERT(initializers...) { initializers }
613
73cda7bb
BH
614#define KUNIT_UNARY_ASSERTION(test, \
615 assert_type, \
697365c0
DL
616 condition_, \
617 expected_true_, \
73cda7bb
BH
618 fmt, \
619 ...) \
97d453bc 620do { \
697365c0 621 if (likely(!!(condition_) == !!expected_true_)) \
97d453bc
DL
622 break; \
623 \
624 _KUNIT_FAILED(test, \
625 assert_type, \
626 kunit_unary_assert, \
627 kunit_unary_assert_format, \
697365c0
DL
628 KUNIT_INIT_ASSERT(.condition = #condition_, \
629 .expected_true = expected_true_), \
97d453bc
DL
630 fmt, \
631 ##__VA_ARGS__); \
632} while (0)
73cda7bb
BH
633
634#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
635 KUNIT_UNARY_ASSERTION(test, \
636 assert_type, \
637 condition, \
638 true, \
639 fmt, \
640 ##__VA_ARGS__)
641
73cda7bb
BH
642#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
643 KUNIT_UNARY_ASSERTION(test, \
644 assert_type, \
645 condition, \
646 false, \
647 fmt, \
648 ##__VA_ARGS__)
649
73cda7bb
BH
650/*
651 * A factory macro for defining the assertions and expectations for the basic
652 * comparisons defined for the built in types.
653 *
654 * Unfortunately, there is no common type that all types can be promoted to for
655 * which all the binary operators behave the same way as for the actual types
656 * (for example, there is no type that long long and unsigned long long can
657 * both be cast to where the comparison result is preserved for all values). So
658 * the best we can do is do the comparison in the original types and then coerce
659 * everything to long long for printing; this way, the comparison behaves
660 * correctly and the printed out value usually makes sense without
661 * interpretation, but can always be interpreted to figure out the actual
662 * value.
663 */
664#define KUNIT_BASE_BINARY_ASSERTION(test, \
665 assert_class, \
064ff292 666 format_func, \
73cda7bb
BH
667 assert_type, \
668 left, \
669 op, \
670 right, \
671 fmt, \
672 ...) \
673do { \
c2741453
DL
674 const typeof(left) __left = (left); \
675 const typeof(right) __right = (right); \
2b6861e2
DL
676 static const struct kunit_binary_assert_text __text = { \
677 .operation = #op, \
678 .left_text = #left, \
679 .right_text = #right, \
680 }; \
73cda7bb 681 \
97d453bc
DL
682 if (likely(__left op __right)) \
683 break; \
684 \
685 _KUNIT_FAILED(test, \
686 assert_type, \
687 assert_class, \
688 format_func, \
697365c0
DL
689 KUNIT_INIT_ASSERT(.text = &__text, \
690 .left_value = __left, \
691 .right_value = __right), \
97d453bc
DL
692 fmt, \
693 ##__VA_ARGS__); \
73cda7bb
BH
694} while (0)
695
40f39777
DL
696#define KUNIT_BINARY_INT_ASSERTION(test, \
697 assert_type, \
698 left, \
699 op, \
700 right, \
701 fmt, \
73cda7bb
BH
702 ...) \
703 KUNIT_BASE_BINARY_ASSERTION(test, \
73cda7bb 704 kunit_binary_assert, \
064ff292 705 kunit_binary_assert_format, \
73cda7bb 706 assert_type, \
40f39777 707 left, op, right, \
73cda7bb
BH
708 fmt, \
709 ##__VA_ARGS__)
710
6125a5c7
DL
711#define KUNIT_BINARY_PTR_ASSERTION(test, \
712 assert_type, \
713 left, \
714 op, \
715 right, \
716 fmt, \
717 ...) \
718 KUNIT_BASE_BINARY_ASSERTION(test, \
73cda7bb 719 kunit_binary_ptr_assert, \
064ff292 720 kunit_binary_ptr_assert_format, \
73cda7bb 721 assert_type, \
6125a5c7 722 left, op, right, \
73cda7bb
BH
723 fmt, \
724 ##__VA_ARGS__)
725
73cda7bb
BH
726#define KUNIT_BINARY_STR_ASSERTION(test, \
727 assert_type, \
728 left, \
729 op, \
730 right, \
731 fmt, \
732 ...) \
733do { \
3747b5c0 734 const char *__left = (left); \
2b6861e2
DL
735 const char *__right = (right); \
736 static const struct kunit_binary_assert_text __text = { \
737 .operation = #op, \
738 .left_text = #left, \
739 .right_text = #right, \
740 }; \
73cda7bb 741 \
97d453bc
DL
742 if (likely(strcmp(__left, __right) op 0)) \
743 break; \
744 \
745 \
746 _KUNIT_FAILED(test, \
747 assert_type, \
748 kunit_binary_str_assert, \
749 kunit_binary_str_assert_format, \
697365c0
DL
750 KUNIT_INIT_ASSERT(.text = &__text, \
751 .left_value = __left, \
752 .right_value = __right), \
97d453bc
DL
753 fmt, \
754 ##__VA_ARGS__); \
73cda7bb
BH
755} while (0)
756
b8a926be
MC
757#define KUNIT_MEM_ASSERTION(test, \
758 assert_type, \
759 left, \
760 op, \
761 right, \
34c68f43 762 size_, \
b8a926be
MC
763 fmt, \
764 ...) \
765do { \
766 const void *__left = (left); \
767 const void *__right = (right); \
34c68f43 768 const size_t __size = (size_); \
b8a926be
MC
769 static const struct kunit_binary_assert_text __text = { \
770 .operation = #op, \
771 .left_text = #left, \
772 .right_text = #right, \
773 }; \
774 \
dd2f0a0a
RM
775 if (likely(__left && __right)) \
776 if (likely(memcmp(__left, __right, __size) op 0)) \
777 break; \
b8a926be
MC
778 \
779 _KUNIT_FAILED(test, \
780 assert_type, \
781 kunit_mem_assert, \
782 kunit_mem_assert_format, \
34c68f43
DL
783 KUNIT_INIT_ASSERT(.text = &__text, \
784 .left_value = __left, \
785 .right_value = __right, \
786 .size = __size), \
b8a926be
MC
787 fmt, \
788 ##__VA_ARGS__); \
789} while (0)
790
73cda7bb
BH
791#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
792 assert_type, \
793 ptr, \
794 fmt, \
795 ...) \
796do { \
c2741453 797 const typeof(ptr) __ptr = (ptr); \
73cda7bb 798 \
97d453bc
DL
799 if (!IS_ERR_OR_NULL(__ptr)) \
800 break; \
801 \
802 _KUNIT_FAILED(test, \
803 assert_type, \
804 kunit_ptr_not_err_assert, \
805 kunit_ptr_not_err_assert_format, \
697365c0 806 KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \
97d453bc
DL
807 fmt, \
808 ##__VA_ARGS__); \
73cda7bb
BH
809} while (0)
810
73cda7bb
BH
811/**
812 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
813 * @test: The test context object.
814 * @condition: an arbitrary boolean expression. The test fails when this does
815 * not evaluate to true.
816 *
817 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
818 * to fail when the specified condition is not met; however, it will not prevent
819 * the test case from continuing to run; this is otherwise known as an
820 * *expectation failure*.
821 */
822#define KUNIT_EXPECT_TRUE(test, condition) \
6709d0fe 823 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
73cda7bb
BH
824
825#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
826 KUNIT_TRUE_MSG_ASSERTION(test, \
827 KUNIT_EXPECTATION, \
828 condition, \
829 fmt, \
830 ##__VA_ARGS__)
831
832/**
833 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
834 * @test: The test context object.
835 * @condition: an arbitrary boolean expression. The test fails when this does
836 * not evaluate to false.
837 *
838 * Sets an expectation that @condition evaluates to false. See
839 * KUNIT_EXPECT_TRUE() for more information.
840 */
841#define KUNIT_EXPECT_FALSE(test, condition) \
6709d0fe 842 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
73cda7bb
BH
843
844#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
845 KUNIT_FALSE_MSG_ASSERTION(test, \
846 KUNIT_EXPECTATION, \
847 condition, \
848 fmt, \
849 ##__VA_ARGS__)
850
851/**
852 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
853 * @test: The test context object.
854 * @left: an arbitrary expression that evaluates to a primitive C type.
855 * @right: an arbitrary expression that evaluates to a primitive C type.
856 *
857 * Sets an expectation that the values that @left and @right evaluate to are
858 * equal. This is semantically equivalent to
859 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
860 * more information.
861 */
862#define KUNIT_EXPECT_EQ(test, left, right) \
6709d0fe 863 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
73cda7bb
BH
864
865#define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
866 KUNIT_BINARY_INT_ASSERTION(test, \
867 KUNIT_EXPECTATION, \
868 left, ==, right, \
869 fmt, \
870 ##__VA_ARGS__)
73cda7bb
BH
871
872/**
873 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
874 * @test: The test context object.
875 * @left: an arbitrary expression that evaluates to a pointer.
876 * @right: an arbitrary expression that evaluates to a pointer.
877 *
878 * Sets an expectation that the values that @left and @right evaluate to are
879 * equal. This is semantically equivalent to
880 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
881 * more information.
882 */
883#define KUNIT_EXPECT_PTR_EQ(test, left, right) \
6709d0fe 884 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
73cda7bb
BH
885
886#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
887 KUNIT_BINARY_PTR_ASSERTION(test, \
888 KUNIT_EXPECTATION, \
889 left, ==, right, \
890 fmt, \
891 ##__VA_ARGS__)
73cda7bb
BH
892
893/**
894 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
895 * @test: The test context object.
896 * @left: an arbitrary expression that evaluates to a primitive C type.
897 * @right: an arbitrary expression that evaluates to a primitive C type.
898 *
899 * Sets an expectation that the values that @left and @right evaluate to are not
900 * equal. This is semantically equivalent to
901 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
902 * more information.
903 */
904#define KUNIT_EXPECT_NE(test, left, right) \
6709d0fe 905 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
73cda7bb
BH
906
907#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
908 KUNIT_BINARY_INT_ASSERTION(test, \
909 KUNIT_EXPECTATION, \
910 left, !=, right, \
911 fmt, \
912 ##__VA_ARGS__)
73cda7bb
BH
913
914/**
915 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
916 * @test: The test context object.
917 * @left: an arbitrary expression that evaluates to a pointer.
918 * @right: an arbitrary expression that evaluates to a pointer.
919 *
920 * Sets an expectation that the values that @left and @right evaluate to are not
921 * equal. This is semantically equivalent to
922 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
923 * more information.
924 */
925#define KUNIT_EXPECT_PTR_NE(test, left, right) \
6709d0fe 926 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
73cda7bb
BH
927
928#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
929 KUNIT_BINARY_PTR_ASSERTION(test, \
930 KUNIT_EXPECTATION, \
931 left, !=, right, \
932 fmt, \
933 ##__VA_ARGS__)
73cda7bb
BH
934
935/**
936 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
937 * @test: The test context object.
938 * @left: an arbitrary expression that evaluates to a primitive C type.
939 * @right: an arbitrary expression that evaluates to a primitive C type.
940 *
941 * Sets an expectation that the value that @left evaluates to is less than the
942 * value that @right evaluates to. This is semantically equivalent to
943 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
944 * more information.
945 */
946#define KUNIT_EXPECT_LT(test, left, right) \
6709d0fe 947 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
73cda7bb
BH
948
949#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
40f39777
DL
950 KUNIT_BINARY_INT_ASSERTION(test, \
951 KUNIT_EXPECTATION, \
952 left, <, right, \
953 fmt, \
954 ##__VA_ARGS__)
73cda7bb
BH
955
956/**
957 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
958 * @test: The test context object.
959 * @left: an arbitrary expression that evaluates to a primitive C type.
960 * @right: an arbitrary expression that evaluates to a primitive C type.
961 *
962 * Sets an expectation that the value that @left evaluates to is less than or
963 * equal to the value that @right evaluates to. Semantically this is equivalent
964 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
965 * more information.
966 */
967#define KUNIT_EXPECT_LE(test, left, right) \
6709d0fe 968 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
73cda7bb
BH
969
970#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
40f39777 971 KUNIT_BINARY_INT_ASSERTION(test, \
aded3cad 972 KUNIT_EXPECTATION, \
40f39777
DL
973 left, <=, right, \
974 fmt, \
975 ##__VA_ARGS__)
73cda7bb
BH
976
977/**
978 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
979 * @test: The test context object.
980 * @left: an arbitrary expression that evaluates to a primitive C type.
981 * @right: an arbitrary expression that evaluates to a primitive C type.
982 *
983 * Sets an expectation that the value that @left evaluates to is greater than
984 * the value that @right evaluates to. This is semantically equivalent to
985 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
986 * more information.
987 */
988#define KUNIT_EXPECT_GT(test, left, right) \
6709d0fe 989 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
73cda7bb
BH
990
991#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
40f39777
DL
992 KUNIT_BINARY_INT_ASSERTION(test, \
993 KUNIT_EXPECTATION, \
994 left, >, right, \
995 fmt, \
996 ##__VA_ARGS__)
73cda7bb
BH
997
998/**
999 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1000 * @test: The test context object.
1001 * @left: an arbitrary expression that evaluates to a primitive C type.
1002 * @right: an arbitrary expression that evaluates to a primitive C type.
1003 *
1004 * Sets an expectation that the value that @left evaluates to is greater than
1005 * the value that @right evaluates to. This is semantically equivalent to
1006 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1007 * more information.
1008 */
1009#define KUNIT_EXPECT_GE(test, left, right) \
6709d0fe 1010 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
73cda7bb
BH
1011
1012#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
40f39777
DL
1013 KUNIT_BINARY_INT_ASSERTION(test, \
1014 KUNIT_EXPECTATION, \
1015 left, >=, right, \
1016 fmt, \
1017 ##__VA_ARGS__)
73cda7bb
BH
1018
1019/**
1020 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1021 * @test: The test context object.
1022 * @left: an arbitrary expression that evaluates to a null terminated string.
1023 * @right: an arbitrary expression that evaluates to a null terminated string.
1024 *
1025 * Sets an expectation that the values that @left and @right evaluate to are
1026 * equal. This is semantically equivalent to
1027 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1028 * for more information.
1029 */
1030#define KUNIT_EXPECT_STREQ(test, left, right) \
6709d0fe 1031 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
73cda7bb
BH
1032
1033#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
955df7d8
DL
1034 KUNIT_BINARY_STR_ASSERTION(test, \
1035 KUNIT_EXPECTATION, \
1036 left, ==, right, \
1037 fmt, \
1038 ##__VA_ARGS__)
73cda7bb
BH
1039
1040/**
1041 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1042 * @test: The test context object.
1043 * @left: an arbitrary expression that evaluates to a null terminated string.
1044 * @right: an arbitrary expression that evaluates to a null terminated string.
1045 *
1046 * Sets an expectation that the values that @left and @right evaluate to are
1047 * not equal. This is semantically equivalent to
1048 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1049 * for more information.
1050 */
1051#define KUNIT_EXPECT_STRNEQ(test, left, right) \
6709d0fe 1052 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
73cda7bb
BH
1053
1054#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
955df7d8
DL
1055 KUNIT_BINARY_STR_ASSERTION(test, \
1056 KUNIT_EXPECTATION, \
1057 left, !=, right, \
1058 fmt, \
1059 ##__VA_ARGS__)
b8a926be
MC
1060
1061/**
1062 * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.
1063 * @test: The test context object.
1064 * @left: An arbitrary expression that evaluates to the specified size.
1065 * @right: An arbitrary expression that evaluates to the specified size.
1066 * @size: Number of bytes compared.
1067 *
1068 * Sets an expectation that the values that @left and @right evaluate to are
1069 * equal. This is semantically equivalent to
1070 * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
1071 * KUNIT_EXPECT_TRUE() for more information.
1072 *
1073 * Although this expectation works for any memory block, it is not recommended
1074 * for comparing more structured data, such as structs. This expectation is
1075 * recommended for comparing, for example, data arrays.
1076 */
1077#define KUNIT_EXPECT_MEMEQ(test, left, right, size) \
1078 KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)
1079
1080#define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
1081 KUNIT_MEM_ASSERTION(test, \
1082 KUNIT_EXPECTATION, \
1083 left, ==, right, \
1084 size, \
1085 fmt, \
1086 ##__VA_ARGS__)
1087
1088/**
1089 * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.
1090 * @test: The test context object.
1091 * @left: An arbitrary expression that evaluates to the specified size.
1092 * @right: An arbitrary expression that evaluates to the specified size.
1093 * @size: Number of bytes compared.
1094 *
1095 * Sets an expectation that the values that @left and @right evaluate to are
1096 * not equal. This is semantically equivalent to
1097 * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1098 * KUNIT_EXPECT_TRUE() for more information.
1099 *
1100 * Although this expectation works for any memory block, it is not recommended
1101 * for comparing more structured data, such as structs. This expectation is
1102 * recommended for comparing, for example, data arrays.
1103 */
1104#define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \
1105 KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)
1106
1107#define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1108 KUNIT_MEM_ASSERTION(test, \
1109 KUNIT_EXPECTATION, \
1110 left, !=, right, \
1111 size, \
1112 fmt, \
1113 ##__VA_ARGS__)
73cda7bb 1114
caae9458
RR
1115/**
1116 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
1117 * @test: The test context object.
1118 * @ptr: an arbitrary pointer.
1119 *
1120 * Sets an expectation that the value that @ptr evaluates to is null. This is
1121 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
1122 * See KUNIT_EXPECT_TRUE() for more information.
1123 */
1124#define KUNIT_EXPECT_NULL(test, ptr) \
1125 KUNIT_EXPECT_NULL_MSG(test, \
1126 ptr, \
1127 NULL)
1128
1129#define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
1130 KUNIT_BINARY_PTR_ASSERTION(test, \
1131 KUNIT_EXPECTATION, \
1132 ptr, ==, NULL, \
1133 fmt, \
1134 ##__VA_ARGS__)
1135
1136/**
1137 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
1138 * @test: The test context object.
1139 * @ptr: an arbitrary pointer.
1140 *
1141 * Sets an expectation that the value that @ptr evaluates to is not null. This
1142 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
1143 * See KUNIT_EXPECT_TRUE() for more information.
1144 */
1145#define KUNIT_EXPECT_NOT_NULL(test, ptr) \
1146 KUNIT_EXPECT_NOT_NULL_MSG(test, \
1147 ptr, \
1148 NULL)
1149
1150#define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1151 KUNIT_BINARY_PTR_ASSERTION(test, \
1152 KUNIT_EXPECTATION, \
1153 ptr, !=, NULL, \
1154 fmt, \
1155 ##__VA_ARGS__)
1156
73cda7bb
BH
1157/**
1158 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1159 * @test: The test context object.
1160 * @ptr: an arbitrary pointer.
1161 *
1162 * Sets an expectation that the value that @ptr evaluates to is not null and not
1163 * an errno stored in a pointer. This is semantically equivalent to
1164 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1165 * more information.
1166 */
1167#define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
6709d0fe 1168 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
73cda7bb
BH
1169
1170#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1171 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1172 KUNIT_EXPECTATION, \
1173 ptr, \
1174 fmt, \
1175 ##__VA_ARGS__)
1176
e4aea8f8
BH
1177#define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1178 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1179
1180/**
1181 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1182 * @test: The test context object.
1183 * @condition: an arbitrary boolean expression. The test fails and aborts when
1184 * this does not evaluate to true.
1185 *
1186 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1187 * fail *and immediately abort* when the specified condition is not met. Unlike
1188 * an expectation failure, it will prevent the test case from continuing to run;
1189 * this is otherwise known as an *assertion failure*.
1190 */
1191#define KUNIT_ASSERT_TRUE(test, condition) \
6709d0fe 1192 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
e4aea8f8
BH
1193
1194#define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1195 KUNIT_TRUE_MSG_ASSERTION(test, \
1196 KUNIT_ASSERTION, \
1197 condition, \
1198 fmt, \
1199 ##__VA_ARGS__)
1200
1201/**
1202 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1203 * @test: The test context object.
1204 * @condition: an arbitrary boolean expression.
1205 *
1206 * Sets an assertion that the value that @condition evaluates to is false. This
1207 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1208 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1209 */
1210#define KUNIT_ASSERT_FALSE(test, condition) \
6709d0fe 1211 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
e4aea8f8
BH
1212
1213#define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1214 KUNIT_FALSE_MSG_ASSERTION(test, \
1215 KUNIT_ASSERTION, \
1216 condition, \
1217 fmt, \
1218 ##__VA_ARGS__)
1219
1220/**
1221 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1222 * @test: The test context object.
1223 * @left: an arbitrary expression that evaluates to a primitive C type.
1224 * @right: an arbitrary expression that evaluates to a primitive C type.
1225 *
1226 * Sets an assertion that the values that @left and @right evaluate to are
1227 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1228 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1229 */
1230#define KUNIT_ASSERT_EQ(test, left, right) \
6709d0fe 1231 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
e4aea8f8
BH
1232
1233#define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
1234 KUNIT_BINARY_INT_ASSERTION(test, \
1235 KUNIT_ASSERTION, \
1236 left, ==, right, \
1237 fmt, \
1238 ##__VA_ARGS__)
e4aea8f8
BH
1239
1240/**
1241 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1242 * @test: The test context object.
1243 * @left: an arbitrary expression that evaluates to a pointer.
1244 * @right: an arbitrary expression that evaluates to a pointer.
1245 *
1246 * Sets an assertion that the values that @left and @right evaluate to are
1247 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1248 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1249 */
1250#define KUNIT_ASSERT_PTR_EQ(test, left, right) \
6709d0fe 1251 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
e4aea8f8
BH
1252
1253#define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
1254 KUNIT_BINARY_PTR_ASSERTION(test, \
1255 KUNIT_ASSERTION, \
1256 left, ==, right, \
1257 fmt, \
1258 ##__VA_ARGS__)
e4aea8f8
BH
1259
1260/**
1261 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1262 * @test: The test context object.
1263 * @left: an arbitrary expression that evaluates to a primitive C type.
1264 * @right: an arbitrary expression that evaluates to a primitive C type.
1265 *
1266 * Sets an assertion that the values that @left and @right evaluate to are not
1267 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1268 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1269 */
1270#define KUNIT_ASSERT_NE(test, left, right) \
6709d0fe 1271 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
e4aea8f8
BH
1272
1273#define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
1274 KUNIT_BINARY_INT_ASSERTION(test, \
1275 KUNIT_ASSERTION, \
1276 left, !=, right, \
1277 fmt, \
1278 ##__VA_ARGS__)
e4aea8f8
BH
1279
1280/**
1281 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1282 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1283 * @test: The test context object.
1284 * @left: an arbitrary expression that evaluates to a pointer.
1285 * @right: an arbitrary expression that evaluates to a pointer.
1286 *
1287 * Sets an assertion that the values that @left and @right evaluate to are not
1288 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1289 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1290 */
1291#define KUNIT_ASSERT_PTR_NE(test, left, right) \
6709d0fe 1292 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
e4aea8f8
BH
1293
1294#define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
1295 KUNIT_BINARY_PTR_ASSERTION(test, \
1296 KUNIT_ASSERTION, \
1297 left, !=, right, \
1298 fmt, \
1299 ##__VA_ARGS__)
e4aea8f8
BH
1300/**
1301 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1302 * @test: The test context object.
1303 * @left: an arbitrary expression that evaluates to a primitive C type.
1304 * @right: an arbitrary expression that evaluates to a primitive C type.
1305 *
1306 * Sets an assertion that the value that @left evaluates to is less than the
1307 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1308 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1309 * is not met.
1310 */
1311#define KUNIT_ASSERT_LT(test, left, right) \
6709d0fe 1312 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
e4aea8f8
BH
1313
1314#define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
40f39777 1315 KUNIT_BINARY_INT_ASSERTION(test, \
aded3cad 1316 KUNIT_ASSERTION, \
40f39777
DL
1317 left, <, right, \
1318 fmt, \
1319 ##__VA_ARGS__)
e4aea8f8
BH
1320/**
1321 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1322 * @test: The test context object.
1323 * @left: an arbitrary expression that evaluates to a primitive C type.
1324 * @right: an arbitrary expression that evaluates to a primitive C type.
1325 *
1326 * Sets an assertion that the value that @left evaluates to is less than or
1327 * equal to the value that @right evaluates to. This is the same as
1328 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1329 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1330 */
1331#define KUNIT_ASSERT_LE(test, left, right) \
6709d0fe 1332 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
e4aea8f8
BH
1333
1334#define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
40f39777
DL
1335 KUNIT_BINARY_INT_ASSERTION(test, \
1336 KUNIT_ASSERTION, \
1337 left, <=, right, \
1338 fmt, \
1339 ##__VA_ARGS__)
e4aea8f8
BH
1340
1341/**
1342 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1343 * @test: The test context object.
1344 * @left: an arbitrary expression that evaluates to a primitive C type.
1345 * @right: an arbitrary expression that evaluates to a primitive C type.
1346 *
1347 * Sets an assertion that the value that @left evaluates to is greater than the
1348 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1349 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1350 * is not met.
1351 */
1352#define KUNIT_ASSERT_GT(test, left, right) \
6709d0fe 1353 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
e4aea8f8
BH
1354
1355#define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
40f39777 1356 KUNIT_BINARY_INT_ASSERTION(test, \
aded3cad 1357 KUNIT_ASSERTION, \
40f39777
DL
1358 left, >, right, \
1359 fmt, \
1360 ##__VA_ARGS__)
e4aea8f8
BH
1361
1362/**
1363 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1364 * @test: The test context object.
1365 * @left: an arbitrary expression that evaluates to a primitive C type.
1366 * @right: an arbitrary expression that evaluates to a primitive C type.
1367 *
1368 * Sets an assertion that the value that @left evaluates to is greater than the
1369 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1370 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1371 * is not met.
1372 */
1373#define KUNIT_ASSERT_GE(test, left, right) \
6709d0fe 1374 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
e4aea8f8
BH
1375
1376#define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
40f39777
DL
1377 KUNIT_BINARY_INT_ASSERTION(test, \
1378 KUNIT_ASSERTION, \
1379 left, >=, right, \
1380 fmt, \
1381 ##__VA_ARGS__)
e4aea8f8
BH
1382
1383/**
1384 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1385 * @test: The test context object.
1386 * @left: an arbitrary expression that evaluates to a null terminated string.
1387 * @right: an arbitrary expression that evaluates to a null terminated string.
1388 *
1389 * Sets an assertion that the values that @left and @right evaluate to are
1390 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1391 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1392 */
1393#define KUNIT_ASSERT_STREQ(test, left, right) \
6709d0fe 1394 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
e4aea8f8
BH
1395
1396#define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
955df7d8
DL
1397 KUNIT_BINARY_STR_ASSERTION(test, \
1398 KUNIT_ASSERTION, \
1399 left, ==, right, \
1400 fmt, \
1401 ##__VA_ARGS__)
e4aea8f8
BH
1402
1403/**
1404 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1405 * @test: The test context object.
1406 * @left: an arbitrary expression that evaluates to a null terminated string.
1407 * @right: an arbitrary expression that evaluates to a null terminated string.
1408 *
1409 * Sets an expectation that the values that @left and @right evaluate to are
1410 * not equal. This is semantically equivalent to
1411 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1412 * for more information.
1413 */
1414#define KUNIT_ASSERT_STRNEQ(test, left, right) \
6709d0fe 1415 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
e4aea8f8
BH
1416
1417#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
955df7d8
DL
1418 KUNIT_BINARY_STR_ASSERTION(test, \
1419 KUNIT_ASSERTION, \
1420 left, !=, right, \
1421 fmt, \
1422 ##__VA_ARGS__)
e4aea8f8 1423
caae9458
RR
1424/**
1425 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1426 * @test: The test context object.
1427 * @ptr: an arbitrary pointer.
1428 *
1429 * Sets an assertion that the values that @ptr evaluates to is null. This is
1430 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1431 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1432 */
1433#define KUNIT_ASSERT_NULL(test, ptr) \
1434 KUNIT_ASSERT_NULL_MSG(test, \
1435 ptr, \
1436 NULL)
1437
1438#define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1439 KUNIT_BINARY_PTR_ASSERTION(test, \
1440 KUNIT_ASSERTION, \
1441 ptr, ==, NULL, \
1442 fmt, \
1443 ##__VA_ARGS__)
1444
1445/**
1446 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1447 * @test: The test context object.
1448 * @ptr: an arbitrary pointer.
1449 *
1450 * Sets an assertion that the values that @ptr evaluates to is not null. This
1451 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1452 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1453 */
1454#define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1455 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1456 ptr, \
1457 NULL)
1458
1459#define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1460 KUNIT_BINARY_PTR_ASSERTION(test, \
1461 KUNIT_ASSERTION, \
1462 ptr, !=, NULL, \
1463 fmt, \
1464 ##__VA_ARGS__)
1465
e4aea8f8
BH
1466/**
1467 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1468 * @test: The test context object.
1469 * @ptr: an arbitrary pointer.
1470 *
1471 * Sets an assertion that the value that @ptr evaluates to is not null and not
1472 * an errno stored in a pointer. This is the same as
1473 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1474 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1475 */
1476#define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
6709d0fe 1477 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
e4aea8f8
BH
1478
1479#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1480 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1481 KUNIT_ASSERTION, \
1482 ptr, \
1483 fmt, \
1484 ##__VA_ARGS__)
1485
fadb08e7
AR
1486/**
1487 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1488 * @name: prefix for the test parameter generator function.
1489 * @array: array of test parameters.
1490 * @get_desc: function to convert param to description; NULL to use default
1491 *
1492 * Define function @name_gen_params which uses @array to generate parameters.
1493 */
1494#define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1495 static const void *name##_gen_params(const void *prev, char *desc) \
1496 { \
1497 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1498 if (__next - (array) < ARRAY_SIZE((array))) { \
1499 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1500 if (__get_desc) \
1501 __get_desc(__next, desc); \
1502 return __next; \
1503 } \
1504 return NULL; \
1505 }
1506
61695f8c
DL
1507// TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1508// include resource.h themselves if they need it.
1509#include <kunit/resource.h>
1510
914cc63e 1511#endif /* _KUNIT_TEST_H */