Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[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
b229baa3 15#include <linux/args.h>
4fdacef8 16#include <linux/compiler.h>
ec54c289
AS
17#include <linux/container_of.h>
18#include <linux/err.h>
19#include <linux/init.h>
908d0c17 20#include <linux/jump_label.h>
ec54c289
AS
21#include <linux/kconfig.h>
22#include <linux/kref.h>
23#include <linux/list.h>
c475c77d 24#include <linux/module.h>
0a756853 25#include <linux/slab.h>
ec54c289
AS
26#include <linux/spinlock.h>
27#include <linux/string.h>
914cc63e 28#include <linux/types.h>
ec54c289
AS
29
30#include <asm/rwonce.h>
f2c6dbd2 31#include <asm/sections.h>
914cc63e 32
908d0c17
DG
33/* Static key: true if any KUnit tests are currently running */
34DECLARE_STATIC_KEY_FALSE(kunit_running);
35
914cc63e 36struct kunit;
05e2006c 37struct string_stream;
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
d1be0cf3 70 * speed: very_slow, slow, and normal. These speeds are relative to
02c2d0c2
RM
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;
05e2006c 135 struct string_stream *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 254 struct dentry *debugfs;
05e2006c 255 struct string_stream *log;
1cdba21d 256 int suite_init_err;
6c4ea2f4 257 bool is_init;
914cc63e
BH
258};
259
c95e7c05
JK
260/* Stores an array of suites, end points one past the end */
261struct kunit_suite_set {
262 struct kunit_suite * const *start;
263 struct kunit_suite * const *end;
264};
265
914cc63e
BH
266/**
267 * struct kunit - represents a running instance of a test.
268 *
269 * @priv: for user to store arbitrary data. Commonly used to pass data
270 * created in the init function (see &struct kunit_suite).
271 *
272 * Used to store information about the current context under which the test
273 * is running. Most of this data is private and should only be accessed
274 * indirectly via public functions; the one exception is @priv which can be
275 * used by the test writer to store arbitrary data.
276 */
277struct kunit {
278 void *priv;
279
280 /* private: internal use only. */
281 const char *name; /* Read only after initialization! */
05e2006c 282 struct string_stream *log; /* Points at case log after initialization */
5f3e0620 283 struct kunit_try_catch try_catch;
fadb08e7
AR
284 /* param_value is the current parameter value for a test case. */
285 const void *param_value;
286 /* param_index stores the index of the parameter in parameterized tests. */
287 int param_index;
914cc63e
BH
288 /*
289 * success starts as true, and may only be set to false during a
290 * test case; thus, it is safe to update this across multiple
291 * threads using WRITE_ONCE; however, as a consequence, it may only
292 * be read after the test case finishes once all threads associated
293 * with the test case have terminated.
294 */
0a756853 295 spinlock_t lock; /* Guards all mutable test state. */
6d2426b2 296 enum kunit_status status; /* Read only after test_case finishes! */
0a756853
BH
297 /*
298 * Because resources is a list that may be updated multiple times (with
299 * new resources) from any thread associated with a test case, we must
300 * protect it with some type of lock.
301 */
302 struct list_head resources; /* Protected by lock. */
6d2426b2
DG
303
304 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
8bd5d74b
MS
305 /* Saves the last seen test. Useful to help with faults. */
306 struct kunit_loc last_seen;
914cc63e
BH
307};
308
83c4e7a0
PA
309static inline void kunit_set_failure(struct kunit *test)
310{
6d2426b2 311 WRITE_ONCE(test->status, KUNIT_FAILURE);
83c4e7a0
PA
312}
313
d20a6ba5 314bool kunit_enabled(void);
31691914 315bool kunit_autorun(void);
18258c60 316const char *kunit_action(void);
b67abaad
JK
317const char *kunit_filter_glob(void);
318char *kunit_filter(void);
319char *kunit_filter_action(void);
d20a6ba5 320
05e2006c 321void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log);
914cc63e
BH
322
323int kunit_run_tests(struct kunit_suite *suite);
324
e2219db2
AM
325size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
326
327unsigned int kunit_test_case_num(struct kunit_suite *suite,
328 struct kunit_case *test_case);
329
b67abaad
JK
330struct kunit_suite_set
331kunit_filter_suites(const struct kunit_suite_set *suite_set,
332 const char *filter_glob,
333 char *filters,
334 char *filter_action,
335 int *err);
336void kunit_free_suite_set(struct kunit_suite_set suite_set);
337
31691914
SK
338int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites,
339 bool run_tests);
e2219db2 340
e5857d39 341void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
e2219db2 342
c95e7c05 343void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin);
18258c60 344void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr);
c95e7c05 345
d81f0d7b
RM
346struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set,
347 struct kunit_suite_set suite_set);
348
8c0d8849
BH
349#if IS_BUILTIN(CONFIG_KUNIT)
350int kunit_run_all_tests(void);
351#else
352static inline int kunit_run_all_tests(void)
353{
354 return 0;
355}
356#endif /* IS_BUILTIN(CONFIG_KUNIT) */
357
e5857d39 358#define __kunit_test_suites(unique_array, ...) \
e5857d39
DL
359 static struct kunit_suite *unique_array[] \
360 __aligned(sizeof(struct kunit_suite *)) \
361 __used __section(".kunit_test_suites") = { __VA_ARGS__ }
aac35468
AM
362
363/**
364 * kunit_test_suites() - used to register one or more &struct kunit_suite
365 * with KUnit.
366 *
7f32b10c 367 * @__suites: a statically allocated list of &struct kunit_suite.
aac35468 368 *
3d6e4462
JK
369 * Registers @suites with the test framework.
370 * This is done by placing the array of struct kunit_suite * in the
371 * .kunit_test_suites ELF section.
aac35468 372 *
3d6e4462
JK
373 * When builtin, KUnit tests are all run via the executor at boot, and when
374 * built as a module, they run on module load.
aac35468
AM
375 *
376 */
7f32b10c 377#define kunit_test_suites(__suites...) \
aac35468 378 __kunit_test_suites(__UNIQUE_ID(array), \
7f32b10c 379 ##__suites)
c475c77d
AM
380
381#define kunit_test_suite(suite) kunit_test_suites(&suite)
914cc63e 382
d81f0d7b
RM
383#define __kunit_init_test_suites(unique_array, ...) \
384 static struct kunit_suite *unique_array[] \
385 __aligned(sizeof(struct kunit_suite *)) \
386 __used __section(".kunit_init_test_suites") = { __VA_ARGS__ }
387
9bf2eed9
BH
388/**
389 * kunit_test_init_section_suites() - used to register one or more &struct
390 * kunit_suite containing init functions or
391 * init data.
392 *
393 * @__suites: a statically allocated list of &struct kunit_suite.
394 *
d81f0d7b
RM
395 * This functions similar to kunit_test_suites() except that it compiles the
396 * list of suites during init phase.
397 *
398 * This macro also suffixes the array and suite declarations it makes with
399 * _probe; so that modpost suppresses warnings about referencing init data
400 * for symbols named in this manner.
9bf2eed9 401 *
d81f0d7b
RM
402 * Note: these init tests are not able to be run after boot so there is no
403 * "run" debugfs file generated for these tests.
9bf2eed9 404 *
d81f0d7b
RM
405 * Also, do not mark the suite or test case structs with __initdata because
406 * they will be used after the init phase with debugfs.
9bf2eed9
BH
407 */
408#define kunit_test_init_section_suites(__suites...) \
d81f0d7b 409 __kunit_init_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
9bf2eed9
BH
410 ##__suites)
411
412#define kunit_test_init_section_suite(suite) \
413 kunit_test_init_section_suites(&suite)
414
e2219db2
AM
415#define kunit_suite_for_each_test_case(suite, test_case) \
416 for (test_case = suite->test_cases; test_case->run_case; test_case++)
417
6d2426b2 418enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
e2219db2 419
0a756853 420/**
7122debb 421 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
0a756853 422 * @test: The test context object.
7122debb 423 * @n: number of elements.
0a756853
BH
424 * @size: The size in bytes of the desired memory.
425 * @gfp: flags passed to underlying kmalloc().
426 *
7122debb 427 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
57e3cded
DG
428 * and is automatically cleaned up after the test case concludes. See kunit_add_action()
429 * for more information.
430 *
431 * Note that some internal context data is also allocated with GFP_KERNEL,
432 * regardless of the gfp passed in.
0a756853 433 */
361b57df 434void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
7122debb
DL
435
436/**
437 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
438 * @test: The test context object.
439 * @size: The size in bytes of the desired memory.
440 * @gfp: flags passed to underlying kmalloc().
441 *
442 * See kmalloc() and kunit_kmalloc_array() for more information.
57e3cded
DG
443 *
444 * Note that some internal context data is also allocated with GFP_KERNEL,
445 * regardless of the gfp passed in.
7122debb
DL
446 */
447static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
448{
449 return kunit_kmalloc_array(test, 1, size, gfp);
450}
0a756853
BH
451
452/**
453 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
454 * @test: The test case to which the resource belongs.
455 * @ptr: The memory allocation to free.
456 */
457void kunit_kfree(struct kunit *test, const void *ptr);
458
459/**
460 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
461 * @test: The test context object.
462 * @size: The size in bytes of the desired memory.
463 * @gfp: flags passed to underlying kmalloc().
464 *
7122debb 465 * See kzalloc() and kunit_kmalloc_array() for more information.
0a756853
BH
466 */
467static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
468{
469 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
470}
471
7122debb
DL
472/**
473 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
474 * @test: The test context object.
475 * @n: number of elements.
476 * @size: The size in bytes of the desired memory.
477 * @gfp: flags passed to underlying kmalloc().
478 *
479 * See kcalloc() and kunit_kmalloc_array() for more information.
480 */
361b57df 481static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
7122debb 482{
361b57df 483 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
7122debb
DL
484}
485
f2c6dbd2
DG
486
487/**
488 * kunit_kfree_const() - conditionally free test managed memory
12cb32a5 489 * @test: The test context object.
f2c6dbd2
DG
490 * @x: pointer to the memory
491 *
492 * Calls kunit_kfree() only if @x is not in .rodata section.
493 * See kunit_kstrdup_const() for more information.
494 */
495void kunit_kfree_const(struct kunit *test, const void *x);
496
497/**
498 * kunit_kstrdup() - Duplicates a string into a test managed allocation.
499 *
500 * @test: The test context object.
501 * @str: The NULL-terminated string to duplicate.
502 * @gfp: flags passed to underlying kmalloc().
503 *
504 * See kstrdup() and kunit_kmalloc_array() for more information.
505 */
506static inline char *kunit_kstrdup(struct kunit *test, const char *str, gfp_t gfp)
507{
508 size_t len;
509 char *buf;
510
511 if (!str)
512 return NULL;
513
514 len = strlen(str) + 1;
515 buf = kunit_kmalloc(test, len, gfp);
516 if (buf)
517 memcpy(buf, str, len);
518 return buf;
519}
520
521/**
522 * kunit_kstrdup_const() - Conditionally duplicates a string into a test managed allocation.
523 *
524 * @test: The test context object.
525 * @str: The NULL-terminated string to duplicate.
526 * @gfp: flags passed to underlying kmalloc().
527 *
528 * Calls kunit_kstrdup() only if @str is not in the rodata section. Must be freed with
529 * kunit_kfree_const() -- not kunit_kfree().
530 * See kstrdup_const() and kunit_kmalloc_array() for more information.
531 */
532const char *kunit_kstrdup_const(struct kunit *test, const char *str, gfp_t gfp);
533
51104c19
KC
534/**
535 * kunit_vm_mmap() - Allocate KUnit-tracked vm_mmap() area
536 * @test: The test context object.
537 * @file: struct file pointer to map from, if any
538 * @addr: desired address, if any
539 * @len: how many bytes to allocate
540 * @prot: mmap PROT_* bits
541 * @flag: mmap flags
542 * @offset: offset into @file to start mapping from.
543 *
544 * See vm_mmap() for more information.
545 */
546unsigned long kunit_vm_mmap(struct kunit *test, struct file *file,
547 unsigned long addr, unsigned long len,
548 unsigned long prot, unsigned long flag,
549 unsigned long offset);
550
0a756853
BH
551void kunit_cleanup(struct kunit *test);
552
05e2006c 553void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...);
e2219db2 554
6d2426b2 555/**
0619a486 556 * kunit_mark_skipped() - Marks @test as skipped
6d2426b2 557 *
0619a486 558 * @test: The test context object.
6d2426b2
DG
559 * @fmt: A printk() style format string.
560 *
561 * Marks the test as skipped. @fmt is given output as the test status
562 * comment, typically the reason the test was skipped.
563 *
564 * Test execution continues after kunit_mark_skipped() is called.
565 */
0619a486 566#define kunit_mark_skipped(test, fmt, ...) \
6d2426b2 567 do { \
0619a486
KB
568 WRITE_ONCE((test)->status, KUNIT_SKIPPED); \
569 scnprintf((test)->status_comment, \
6d2426b2
DG
570 KUNIT_STATUS_COMMENT_SIZE, \
571 fmt, ##__VA_ARGS__); \
572 } while (0)
573
574/**
0619a486 575 * kunit_skip() - Marks @test as skipped
6d2426b2 576 *
0619a486 577 * @test: The test context object.
6d2426b2
DG
578 * @fmt: A printk() style format string.
579 *
580 * Skips the test. @fmt is given output as the test status
581 * comment, typically the reason the test was skipped.
582 *
583 * Test execution is halted after kunit_skip() is called.
584 */
0619a486 585#define kunit_skip(test, fmt, ...) \
6d2426b2 586 do { \
0619a486
KB
587 kunit_mark_skipped((test), fmt, ##__VA_ARGS__); \
588 kunit_try_catch_throw(&((test)->try_catch)); \
6d2426b2 589 } while (0)
e2219db2
AM
590
591/*
592 * printk and log to per-test or per-suite log buffer. Logging only done
593 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
594 */
595#define kunit_log(lvl, test_or_suite, fmt, ...) \
596 do { \
597 printk(lvl fmt, ##__VA_ARGS__); \
2c6a96da 598 kunit_log_append((test_or_suite)->log, fmt, \
e2219db2
AM
599 ##__VA_ARGS__); \
600 } while (0)
601
602#define kunit_printk(lvl, test, fmt, ...) \
c3bba690
AM
603 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
604 (test)->name, ##__VA_ARGS__)
914cc63e
BH
605
606/**
607 * kunit_info() - Prints an INFO level message associated with @test.
608 *
609 * @test: The test context object.
610 * @fmt: A printk() style format string.
611 *
612 * Prints an info level message associated with the test suite being run.
613 * Takes a variable number of format parameters just like printk().
614 */
615#define kunit_info(test, fmt, ...) \
616 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
617
618/**
619 * kunit_warn() - Prints a WARN level message associated with @test.
620 *
621 * @test: The test context object.
622 * @fmt: A printk() style format string.
623 *
624 * Prints a warning level message.
625 */
626#define kunit_warn(test, fmt, ...) \
627 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
628
629/**
630 * kunit_err() - Prints an ERROR level message associated with @test.
631 *
632 * @test: The test context object.
633 * @fmt: A printk() style format string.
634 *
635 * Prints an error level message.
636 */
637#define kunit_err(test, fmt, ...) \
638 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
639
8bd5d74b
MS
640/*
641 * Must be called at the beginning of each KUNIT_*_ASSERTION().
642 * Cf. KUNIT_CURRENT_LOC.
643 */
644#define _KUNIT_SAVE_LOC(test) do { \
645 WRITE_ONCE(test->last_seen.file, __FILE__); \
646 WRITE_ONCE(test->last_seen.line, __LINE__); \
647} while (0)
648
73cda7bb
BH
649/**
650 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
651 * @test: The test context object.
652 *
653 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
654 * words, it does nothing and only exists for code clarity. See
655 * KUNIT_EXPECT_TRUE() for more information.
656 */
8bd5d74b 657#define KUNIT_SUCCEED(test) _KUNIT_SAVE_LOC(test)
73cda7bb 658
26075518
DG
659void __noreturn __kunit_abort(struct kunit *test);
660
806cb227
DG
661void __printf(6, 7) __kunit_do_failed_assertion(struct kunit *test,
662 const struct kunit_loc *loc,
663 enum kunit_assert_type type,
664 const struct kunit_assert *assert,
665 assert_format_t assert_format,
666 const char *fmt, ...);
73cda7bb 667
97d453bc
DL
668#define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
669 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
c1144e01 670 const struct assert_class __assertion = INITIALIZER; \
26075518
DG
671 __kunit_do_failed_assertion(test, \
672 &__loc, \
673 assert_type, \
674 &__assertion.assert, \
675 assert_format, \
676 fmt, \
677 ##__VA_ARGS__); \
678 if (assert_type == KUNIT_ASSERTION) \
679 __kunit_abort(test); \
73cda7bb
BH
680} while (0)
681
682
8bd5d74b
MS
683#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) do { \
684 _KUNIT_SAVE_LOC(test); \
97d453bc
DL
685 _KUNIT_FAILED(test, \
686 assert_type, \
687 kunit_fail_assert, \
688 kunit_fail_assert_format, \
689 {}, \
690 fmt, \
8bd5d74b
MS
691 ##__VA_ARGS__); \
692} while (0)
73cda7bb
BH
693
694/**
695 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
696 * @test: The test context object.
697 * @fmt: an informational message to be printed when the assertion is made.
698 * @...: string format arguments.
699 *
700 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
701 * other words, it always results in a failed expectation, and consequently
702 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
703 * for more information.
704 */
705#define KUNIT_FAIL(test, fmt, ...) \
706 KUNIT_FAIL_ASSERTION(test, \
707 KUNIT_EXPECTATION, \
708 fmt, \
709 ##__VA_ARGS__)
710
697365c0
DL
711/* Helper to safely pass around an initializer list to other macros. */
712#define KUNIT_INIT_ASSERT(initializers...) { initializers }
713
73cda7bb
BH
714#define KUNIT_UNARY_ASSERTION(test, \
715 assert_type, \
697365c0
DL
716 condition_, \
717 expected_true_, \
73cda7bb
BH
718 fmt, \
719 ...) \
97d453bc 720do { \
8bd5d74b 721 _KUNIT_SAVE_LOC(test); \
697365c0 722 if (likely(!!(condition_) == !!expected_true_)) \
97d453bc
DL
723 break; \
724 \
725 _KUNIT_FAILED(test, \
726 assert_type, \
727 kunit_unary_assert, \
728 kunit_unary_assert_format, \
697365c0
DL
729 KUNIT_INIT_ASSERT(.condition = #condition_, \
730 .expected_true = expected_true_), \
97d453bc
DL
731 fmt, \
732 ##__VA_ARGS__); \
733} while (0)
73cda7bb
BH
734
735#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
736 KUNIT_UNARY_ASSERTION(test, \
737 assert_type, \
738 condition, \
739 true, \
740 fmt, \
741 ##__VA_ARGS__)
742
73cda7bb
BH
743#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
744 KUNIT_UNARY_ASSERTION(test, \
745 assert_type, \
746 condition, \
747 false, \
748 fmt, \
749 ##__VA_ARGS__)
750
73cda7bb
BH
751/*
752 * A factory macro for defining the assertions and expectations for the basic
753 * comparisons defined for the built in types.
754 *
755 * Unfortunately, there is no common type that all types can be promoted to for
756 * which all the binary operators behave the same way as for the actual types
757 * (for example, there is no type that long long and unsigned long long can
758 * both be cast to where the comparison result is preserved for all values). So
759 * the best we can do is do the comparison in the original types and then coerce
760 * everything to long long for printing; this way, the comparison behaves
761 * correctly and the printed out value usually makes sense without
762 * interpretation, but can always be interpreted to figure out the actual
763 * value.
764 */
765#define KUNIT_BASE_BINARY_ASSERTION(test, \
766 assert_class, \
064ff292 767 format_func, \
73cda7bb
BH
768 assert_type, \
769 left, \
770 op, \
771 right, \
772 fmt, \
773 ...) \
774do { \
c2741453
DL
775 const typeof(left) __left = (left); \
776 const typeof(right) __right = (right); \
2b6861e2
DL
777 static const struct kunit_binary_assert_text __text = { \
778 .operation = #op, \
779 .left_text = #left, \
780 .right_text = #right, \
781 }; \
73cda7bb 782 \
8bd5d74b 783 _KUNIT_SAVE_LOC(test); \
97d453bc
DL
784 if (likely(__left op __right)) \
785 break; \
786 \
787 _KUNIT_FAILED(test, \
788 assert_type, \
789 assert_class, \
790 format_func, \
697365c0
DL
791 KUNIT_INIT_ASSERT(.text = &__text, \
792 .left_value = __left, \
793 .right_value = __right), \
97d453bc
DL
794 fmt, \
795 ##__VA_ARGS__); \
73cda7bb
BH
796} while (0)
797
40f39777
DL
798#define KUNIT_BINARY_INT_ASSERTION(test, \
799 assert_type, \
800 left, \
801 op, \
802 right, \
803 fmt, \
73cda7bb
BH
804 ...) \
805 KUNIT_BASE_BINARY_ASSERTION(test, \
73cda7bb 806 kunit_binary_assert, \
064ff292 807 kunit_binary_assert_format, \
73cda7bb 808 assert_type, \
40f39777 809 left, op, right, \
73cda7bb
BH
810 fmt, \
811 ##__VA_ARGS__)
812
6125a5c7
DL
813#define KUNIT_BINARY_PTR_ASSERTION(test, \
814 assert_type, \
815 left, \
816 op, \
817 right, \
818 fmt, \
819 ...) \
820 KUNIT_BASE_BINARY_ASSERTION(test, \
73cda7bb 821 kunit_binary_ptr_assert, \
064ff292 822 kunit_binary_ptr_assert_format, \
73cda7bb 823 assert_type, \
6125a5c7 824 left, op, right, \
73cda7bb
BH
825 fmt, \
826 ##__VA_ARGS__)
827
73cda7bb
BH
828#define KUNIT_BINARY_STR_ASSERTION(test, \
829 assert_type, \
830 left, \
831 op, \
832 right, \
833 fmt, \
834 ...) \
835do { \
3747b5c0 836 const char *__left = (left); \
2b6861e2
DL
837 const char *__right = (right); \
838 static const struct kunit_binary_assert_text __text = { \
839 .operation = #op, \
840 .left_text = #left, \
841 .right_text = #right, \
842 }; \
73cda7bb 843 \
8bd5d74b 844 _KUNIT_SAVE_LOC(test); \
7ece381a 845 if (likely((__left) && (__right) && (strcmp(__left, __right) op 0))) \
97d453bc
DL
846 break; \
847 \
848 \
849 _KUNIT_FAILED(test, \
850 assert_type, \
851 kunit_binary_str_assert, \
852 kunit_binary_str_assert_format, \
697365c0
DL
853 KUNIT_INIT_ASSERT(.text = &__text, \
854 .left_value = __left, \
855 .right_value = __right), \
97d453bc
DL
856 fmt, \
857 ##__VA_ARGS__); \
73cda7bb
BH
858} while (0)
859
b8a926be
MC
860#define KUNIT_MEM_ASSERTION(test, \
861 assert_type, \
862 left, \
863 op, \
864 right, \
34c68f43 865 size_, \
b8a926be
MC
866 fmt, \
867 ...) \
868do { \
869 const void *__left = (left); \
870 const void *__right = (right); \
34c68f43 871 const size_t __size = (size_); \
b8a926be
MC
872 static const struct kunit_binary_assert_text __text = { \
873 .operation = #op, \
874 .left_text = #left, \
875 .right_text = #right, \
876 }; \
877 \
8bd5d74b 878 _KUNIT_SAVE_LOC(test); \
dd2f0a0a
RM
879 if (likely(__left && __right)) \
880 if (likely(memcmp(__left, __right, __size) op 0)) \
881 break; \
b8a926be
MC
882 \
883 _KUNIT_FAILED(test, \
884 assert_type, \
885 kunit_mem_assert, \
886 kunit_mem_assert_format, \
34c68f43
DL
887 KUNIT_INIT_ASSERT(.text = &__text, \
888 .left_value = __left, \
889 .right_value = __right, \
890 .size = __size), \
b8a926be
MC
891 fmt, \
892 ##__VA_ARGS__); \
893} while (0)
894
73cda7bb
BH
895#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
896 assert_type, \
897 ptr, \
898 fmt, \
899 ...) \
900do { \
c2741453 901 const typeof(ptr) __ptr = (ptr); \
73cda7bb 902 \
8bd5d74b 903 _KUNIT_SAVE_LOC(test); \
97d453bc
DL
904 if (!IS_ERR_OR_NULL(__ptr)) \
905 break; \
906 \
907 _KUNIT_FAILED(test, \
908 assert_type, \
909 kunit_ptr_not_err_assert, \
910 kunit_ptr_not_err_assert_format, \
697365c0 911 KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \
97d453bc
DL
912 fmt, \
913 ##__VA_ARGS__); \
73cda7bb
BH
914} while (0)
915
73cda7bb
BH
916/**
917 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
918 * @test: The test context object.
919 * @condition: an arbitrary boolean expression. The test fails when this does
920 * not evaluate to true.
921 *
922 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
923 * to fail when the specified condition is not met; however, it will not prevent
924 * the test case from continuing to run; this is otherwise known as an
925 * *expectation failure*.
926 */
927#define KUNIT_EXPECT_TRUE(test, condition) \
6709d0fe 928 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
73cda7bb
BH
929
930#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
931 KUNIT_TRUE_MSG_ASSERTION(test, \
932 KUNIT_EXPECTATION, \
933 condition, \
934 fmt, \
935 ##__VA_ARGS__)
936
937/**
938 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
939 * @test: The test context object.
940 * @condition: an arbitrary boolean expression. The test fails when this does
941 * not evaluate to false.
942 *
943 * Sets an expectation that @condition evaluates to false. See
944 * KUNIT_EXPECT_TRUE() for more information.
945 */
946#define KUNIT_EXPECT_FALSE(test, condition) \
6709d0fe 947 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
73cda7bb
BH
948
949#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
950 KUNIT_FALSE_MSG_ASSERTION(test, \
951 KUNIT_EXPECTATION, \
952 condition, \
953 fmt, \
954 ##__VA_ARGS__)
955
956/**
957 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
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 values that @left and @right evaluate to are
963 * equal. This is semantically equivalent to
964 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
965 * more information.
966 */
967#define KUNIT_EXPECT_EQ(test, left, right) \
6709d0fe 968 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
73cda7bb
BH
969
970#define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
971 KUNIT_BINARY_INT_ASSERTION(test, \
972 KUNIT_EXPECTATION, \
973 left, ==, right, \
974 fmt, \
975 ##__VA_ARGS__)
73cda7bb
BH
976
977/**
978 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
979 * @test: The test context object.
980 * @left: an arbitrary expression that evaluates to a pointer.
981 * @right: an arbitrary expression that evaluates to a pointer.
982 *
983 * Sets an expectation that the values that @left and @right evaluate to are
984 * equal. 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_PTR_EQ(test, left, right) \
6709d0fe 989 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
73cda7bb
BH
990
991#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
992 KUNIT_BINARY_PTR_ASSERTION(test, \
993 KUNIT_EXPECTATION, \
994 left, ==, right, \
995 fmt, \
996 ##__VA_ARGS__)
73cda7bb
BH
997
998/**
999 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
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 values that @left and @right evaluate to are not
1005 * equal. 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_NE(test, left, right) \
6709d0fe 1010 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
73cda7bb
BH
1011
1012#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
6125a5c7
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_PTR_NE() - Expects that pointers @left and @right are not equal.
1021 * @test: The test context object.
1022 * @left: an arbitrary expression that evaluates to a pointer.
1023 * @right: an arbitrary expression that evaluates to a pointer.
1024 *
1025 * Sets an expectation that the values that @left and @right evaluate to are not
1026 * equal. This is semantically equivalent to
1027 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1028 * more information.
1029 */
1030#define KUNIT_EXPECT_PTR_NE(test, left, right) \
6709d0fe 1031 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
73cda7bb
BH
1032
1033#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
1034 KUNIT_BINARY_PTR_ASSERTION(test, \
1035 KUNIT_EXPECTATION, \
1036 left, !=, right, \
1037 fmt, \
1038 ##__VA_ARGS__)
73cda7bb
BH
1039
1040/**
1041 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
1042 * @test: The test context object.
1043 * @left: an arbitrary expression that evaluates to a primitive C type.
1044 * @right: an arbitrary expression that evaluates to a primitive C type.
1045 *
1046 * Sets an expectation that the value that @left evaluates to is less than the
1047 * value that @right evaluates to. This is semantically equivalent to
1048 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
1049 * more information.
1050 */
1051#define KUNIT_EXPECT_LT(test, left, right) \
6709d0fe 1052 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
73cda7bb
BH
1053
1054#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
40f39777
DL
1055 KUNIT_BINARY_INT_ASSERTION(test, \
1056 KUNIT_EXPECTATION, \
1057 left, <, right, \
1058 fmt, \
1059 ##__VA_ARGS__)
73cda7bb
BH
1060
1061/**
1062 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
1063 * @test: The test context object.
1064 * @left: an arbitrary expression that evaluates to a primitive C type.
1065 * @right: an arbitrary expression that evaluates to a primitive C type.
1066 *
1067 * Sets an expectation that the value that @left evaluates to is less than or
1068 * equal to the value that @right evaluates to. Semantically this is equivalent
1069 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
1070 * more information.
1071 */
1072#define KUNIT_EXPECT_LE(test, left, right) \
6709d0fe 1073 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
73cda7bb
BH
1074
1075#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
40f39777 1076 KUNIT_BINARY_INT_ASSERTION(test, \
aded3cad 1077 KUNIT_EXPECTATION, \
40f39777
DL
1078 left, <=, right, \
1079 fmt, \
1080 ##__VA_ARGS__)
73cda7bb
BH
1081
1082/**
1083 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
1084 * @test: The test context object.
1085 * @left: an arbitrary expression that evaluates to a primitive C type.
1086 * @right: an arbitrary expression that evaluates to a primitive C type.
1087 *
1088 * Sets an expectation that the value that @left evaluates to is greater than
1089 * the value that @right evaluates to. This is semantically equivalent to
1090 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
1091 * more information.
1092 */
1093#define KUNIT_EXPECT_GT(test, left, right) \
6709d0fe 1094 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
73cda7bb
BH
1095
1096#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
40f39777
DL
1097 KUNIT_BINARY_INT_ASSERTION(test, \
1098 KUNIT_EXPECTATION, \
1099 left, >, right, \
1100 fmt, \
1101 ##__VA_ARGS__)
73cda7bb
BH
1102
1103/**
1104 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1105 * @test: The test context object.
1106 * @left: an arbitrary expression that evaluates to a primitive C type.
1107 * @right: an arbitrary expression that evaluates to a primitive C type.
1108 *
1109 * Sets an expectation that the value that @left evaluates to is greater than
1110 * the value that @right evaluates to. This is semantically equivalent to
1111 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1112 * more information.
1113 */
1114#define KUNIT_EXPECT_GE(test, left, right) \
6709d0fe 1115 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
73cda7bb
BH
1116
1117#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
40f39777
DL
1118 KUNIT_BINARY_INT_ASSERTION(test, \
1119 KUNIT_EXPECTATION, \
1120 left, >=, right, \
1121 fmt, \
1122 ##__VA_ARGS__)
73cda7bb
BH
1123
1124/**
1125 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1126 * @test: The test context object.
1127 * @left: an arbitrary expression that evaluates to a null terminated string.
1128 * @right: an arbitrary expression that evaluates to a null terminated string.
1129 *
1130 * Sets an expectation that the values that @left and @right evaluate to are
1131 * equal. This is semantically equivalent to
1132 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1133 * for more information.
1134 */
1135#define KUNIT_EXPECT_STREQ(test, left, right) \
6709d0fe 1136 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
73cda7bb
BH
1137
1138#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
955df7d8
DL
1139 KUNIT_BINARY_STR_ASSERTION(test, \
1140 KUNIT_EXPECTATION, \
1141 left, ==, right, \
1142 fmt, \
1143 ##__VA_ARGS__)
73cda7bb
BH
1144
1145/**
1146 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1147 * @test: The test context object.
1148 * @left: an arbitrary expression that evaluates to a null terminated string.
1149 * @right: an arbitrary expression that evaluates to a null terminated string.
1150 *
1151 * Sets an expectation that the values that @left and @right evaluate to are
1152 * not equal. This is semantically equivalent to
1153 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1154 * for more information.
1155 */
1156#define KUNIT_EXPECT_STRNEQ(test, left, right) \
6709d0fe 1157 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
73cda7bb
BH
1158
1159#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
955df7d8
DL
1160 KUNIT_BINARY_STR_ASSERTION(test, \
1161 KUNIT_EXPECTATION, \
1162 left, !=, right, \
1163 fmt, \
1164 ##__VA_ARGS__)
b8a926be
MC
1165
1166/**
1167 * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.
1168 * @test: The test context object.
1169 * @left: An arbitrary expression that evaluates to the specified size.
1170 * @right: An arbitrary expression that evaluates to the specified size.
1171 * @size: Number of bytes compared.
1172 *
1173 * Sets an expectation that the values that @left and @right evaluate to are
1174 * equal. This is semantically equivalent to
1175 * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
1176 * KUNIT_EXPECT_TRUE() for more information.
1177 *
1178 * Although this expectation works for any memory block, it is not recommended
1179 * for comparing more structured data, such as structs. This expectation is
1180 * recommended for comparing, for example, data arrays.
1181 */
1182#define KUNIT_EXPECT_MEMEQ(test, left, right, size) \
1183 KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)
1184
1185#define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
1186 KUNIT_MEM_ASSERTION(test, \
1187 KUNIT_EXPECTATION, \
1188 left, ==, right, \
1189 size, \
1190 fmt, \
1191 ##__VA_ARGS__)
1192
1193/**
1194 * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.
1195 * @test: The test context object.
1196 * @left: An arbitrary expression that evaluates to the specified size.
1197 * @right: An arbitrary expression that evaluates to the specified size.
1198 * @size: Number of bytes compared.
1199 *
1200 * Sets an expectation that the values that @left and @right evaluate to are
1201 * not equal. This is semantically equivalent to
1202 * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1203 * KUNIT_EXPECT_TRUE() for more information.
1204 *
1205 * Although this expectation works for any memory block, it is not recommended
1206 * for comparing more structured data, such as structs. This expectation is
1207 * recommended for comparing, for example, data arrays.
1208 */
1209#define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \
1210 KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)
1211
1212#define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1213 KUNIT_MEM_ASSERTION(test, \
1214 KUNIT_EXPECTATION, \
1215 left, !=, right, \
1216 size, \
1217 fmt, \
1218 ##__VA_ARGS__)
73cda7bb 1219
caae9458
RR
1220/**
1221 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
1222 * @test: The test context object.
1223 * @ptr: an arbitrary pointer.
1224 *
1225 * Sets an expectation that the value that @ptr evaluates to is null. This is
1226 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
1227 * See KUNIT_EXPECT_TRUE() for more information.
1228 */
1229#define KUNIT_EXPECT_NULL(test, ptr) \
1230 KUNIT_EXPECT_NULL_MSG(test, \
1231 ptr, \
1232 NULL)
1233
1234#define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
1235 KUNIT_BINARY_PTR_ASSERTION(test, \
1236 KUNIT_EXPECTATION, \
1237 ptr, ==, NULL, \
1238 fmt, \
1239 ##__VA_ARGS__)
1240
1241/**
1242 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
1243 * @test: The test context object.
1244 * @ptr: an arbitrary pointer.
1245 *
1246 * Sets an expectation that the value that @ptr evaluates to is not null. This
1247 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
1248 * See KUNIT_EXPECT_TRUE() for more information.
1249 */
1250#define KUNIT_EXPECT_NOT_NULL(test, ptr) \
1251 KUNIT_EXPECT_NOT_NULL_MSG(test, \
1252 ptr, \
1253 NULL)
1254
1255#define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1256 KUNIT_BINARY_PTR_ASSERTION(test, \
1257 KUNIT_EXPECTATION, \
1258 ptr, !=, NULL, \
1259 fmt, \
1260 ##__VA_ARGS__)
1261
73cda7bb
BH
1262/**
1263 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1264 * @test: The test context object.
1265 * @ptr: an arbitrary pointer.
1266 *
1267 * Sets an expectation that the value that @ptr evaluates to is not null and not
1268 * an errno stored in a pointer. This is semantically equivalent to
1269 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1270 * more information.
1271 */
1272#define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
6709d0fe 1273 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
73cda7bb
BH
1274
1275#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1276 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1277 KUNIT_EXPECTATION, \
1278 ptr, \
1279 fmt, \
1280 ##__VA_ARGS__)
1281
7d4087b0
EC
1282/**
1283 * KUNIT_FAIL_AND_ABORT() - Always causes a test to fail and abort when evaluated.
1284 * @test: The test context object.
1285 * @fmt: an informational message to be printed when the assertion is made.
1286 * @...: string format arguments.
1287 *
1288 * The opposite of KUNIT_SUCCEED(), it is an assertion that always fails. In
1289 * other words, it always results in a failed assertion, and consequently
1290 * always causes the test case to fail and abort when evaluated.
1291 * See KUNIT_ASSERT_TRUE() for more information.
1292 */
1293#define KUNIT_FAIL_AND_ABORT(test, fmt, ...) \
e4aea8f8
BH
1294 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1295
1296/**
1297 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1298 * @test: The test context object.
1299 * @condition: an arbitrary boolean expression. The test fails and aborts when
1300 * this does not evaluate to true.
1301 *
1302 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1303 * fail *and immediately abort* when the specified condition is not met. Unlike
1304 * an expectation failure, it will prevent the test case from continuing to run;
1305 * this is otherwise known as an *assertion failure*.
1306 */
1307#define KUNIT_ASSERT_TRUE(test, condition) \
6709d0fe 1308 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
e4aea8f8
BH
1309
1310#define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1311 KUNIT_TRUE_MSG_ASSERTION(test, \
1312 KUNIT_ASSERTION, \
1313 condition, \
1314 fmt, \
1315 ##__VA_ARGS__)
1316
1317/**
1318 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1319 * @test: The test context object.
1320 * @condition: an arbitrary boolean expression.
1321 *
1322 * Sets an assertion that the value that @condition evaluates to is false. This
1323 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1324 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1325 */
1326#define KUNIT_ASSERT_FALSE(test, condition) \
6709d0fe 1327 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
e4aea8f8
BH
1328
1329#define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1330 KUNIT_FALSE_MSG_ASSERTION(test, \
1331 KUNIT_ASSERTION, \
1332 condition, \
1333 fmt, \
1334 ##__VA_ARGS__)
1335
1336/**
1337 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1338 * @test: The test context object.
1339 * @left: an arbitrary expression that evaluates to a primitive C type.
1340 * @right: an arbitrary expression that evaluates to a primitive C type.
1341 *
1342 * Sets an assertion that the values that @left and @right evaluate to are
1343 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1344 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1345 */
1346#define KUNIT_ASSERT_EQ(test, left, right) \
6709d0fe 1347 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
e4aea8f8
BH
1348
1349#define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
1350 KUNIT_BINARY_INT_ASSERTION(test, \
1351 KUNIT_ASSERTION, \
1352 left, ==, right, \
1353 fmt, \
1354 ##__VA_ARGS__)
e4aea8f8
BH
1355
1356/**
1357 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1358 * @test: The test context object.
1359 * @left: an arbitrary expression that evaluates to a pointer.
1360 * @right: an arbitrary expression that evaluates to a pointer.
1361 *
1362 * Sets an assertion that the values that @left and @right evaluate to are
1363 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1364 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1365 */
1366#define KUNIT_ASSERT_PTR_EQ(test, left, right) \
6709d0fe 1367 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
e4aea8f8
BH
1368
1369#define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
1370 KUNIT_BINARY_PTR_ASSERTION(test, \
1371 KUNIT_ASSERTION, \
1372 left, ==, right, \
1373 fmt, \
1374 ##__VA_ARGS__)
e4aea8f8
BH
1375
1376/**
1377 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1378 * @test: The test context object.
1379 * @left: an arbitrary expression that evaluates to a primitive C type.
1380 * @right: an arbitrary expression that evaluates to a primitive C type.
1381 *
1382 * Sets an assertion that the values that @left and @right evaluate to are not
1383 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1384 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1385 */
1386#define KUNIT_ASSERT_NE(test, left, right) \
6709d0fe 1387 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
e4aea8f8
BH
1388
1389#define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
1390 KUNIT_BINARY_INT_ASSERTION(test, \
1391 KUNIT_ASSERTION, \
1392 left, !=, right, \
1393 fmt, \
1394 ##__VA_ARGS__)
e4aea8f8
BH
1395
1396/**
1397 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1398 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1399 * @test: The test context object.
1400 * @left: an arbitrary expression that evaluates to a pointer.
1401 * @right: an arbitrary expression that evaluates to a pointer.
1402 *
1403 * Sets an assertion that the values that @left and @right evaluate to are not
1404 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1405 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1406 */
1407#define KUNIT_ASSERT_PTR_NE(test, left, right) \
6709d0fe 1408 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
e4aea8f8
BH
1409
1410#define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
1411 KUNIT_BINARY_PTR_ASSERTION(test, \
1412 KUNIT_ASSERTION, \
1413 left, !=, right, \
1414 fmt, \
1415 ##__VA_ARGS__)
e4aea8f8
BH
1416/**
1417 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1418 * @test: The test context object.
1419 * @left: an arbitrary expression that evaluates to a primitive C type.
1420 * @right: an arbitrary expression that evaluates to a primitive C type.
1421 *
1422 * Sets an assertion that the value that @left evaluates to is less than the
1423 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1424 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1425 * is not met.
1426 */
1427#define KUNIT_ASSERT_LT(test, left, right) \
6709d0fe 1428 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
e4aea8f8
BH
1429
1430#define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
40f39777 1431 KUNIT_BINARY_INT_ASSERTION(test, \
aded3cad 1432 KUNIT_ASSERTION, \
40f39777
DL
1433 left, <, right, \
1434 fmt, \
1435 ##__VA_ARGS__)
e4aea8f8
BH
1436/**
1437 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1438 * @test: The test context object.
1439 * @left: an arbitrary expression that evaluates to a primitive C type.
1440 * @right: an arbitrary expression that evaluates to a primitive C type.
1441 *
1442 * Sets an assertion that the value that @left evaluates to is less than or
1443 * equal to the value that @right evaluates to. This is the same as
1444 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1445 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1446 */
1447#define KUNIT_ASSERT_LE(test, left, right) \
6709d0fe 1448 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
e4aea8f8
BH
1449
1450#define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
40f39777
DL
1451 KUNIT_BINARY_INT_ASSERTION(test, \
1452 KUNIT_ASSERTION, \
1453 left, <=, right, \
1454 fmt, \
1455 ##__VA_ARGS__)
e4aea8f8
BH
1456
1457/**
1458 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1459 * @test: The test context object.
1460 * @left: an arbitrary expression that evaluates to a primitive C type.
1461 * @right: an arbitrary expression that evaluates to a primitive C type.
1462 *
1463 * Sets an assertion that the value that @left evaluates to is greater than the
1464 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1465 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1466 * is not met.
1467 */
1468#define KUNIT_ASSERT_GT(test, left, right) \
6709d0fe 1469 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
e4aea8f8
BH
1470
1471#define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
40f39777 1472 KUNIT_BINARY_INT_ASSERTION(test, \
aded3cad 1473 KUNIT_ASSERTION, \
40f39777
DL
1474 left, >, right, \
1475 fmt, \
1476 ##__VA_ARGS__)
e4aea8f8
BH
1477
1478/**
1479 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1480 * @test: The test context object.
1481 * @left: an arbitrary expression that evaluates to a primitive C type.
1482 * @right: an arbitrary expression that evaluates to a primitive C type.
1483 *
1484 * Sets an assertion that the value that @left evaluates to is greater than the
1485 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1486 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1487 * is not met.
1488 */
1489#define KUNIT_ASSERT_GE(test, left, right) \
6709d0fe 1490 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
e4aea8f8
BH
1491
1492#define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
40f39777
DL
1493 KUNIT_BINARY_INT_ASSERTION(test, \
1494 KUNIT_ASSERTION, \
1495 left, >=, right, \
1496 fmt, \
1497 ##__VA_ARGS__)
e4aea8f8
BH
1498
1499/**
1500 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1501 * @test: The test context object.
1502 * @left: an arbitrary expression that evaluates to a null terminated string.
1503 * @right: an arbitrary expression that evaluates to a null terminated string.
1504 *
1505 * Sets an assertion that the values that @left and @right evaluate to are
1506 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1507 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1508 */
1509#define KUNIT_ASSERT_STREQ(test, left, right) \
6709d0fe 1510 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
e4aea8f8
BH
1511
1512#define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
955df7d8
DL
1513 KUNIT_BINARY_STR_ASSERTION(test, \
1514 KUNIT_ASSERTION, \
1515 left, ==, right, \
1516 fmt, \
1517 ##__VA_ARGS__)
e4aea8f8
BH
1518
1519/**
2be32bbe 1520 * KUNIT_ASSERT_STRNEQ() - An assertion that strings @left and @right are not equal.
e4aea8f8
BH
1521 * @test: The test context object.
1522 * @left: an arbitrary expression that evaluates to a null terminated string.
1523 * @right: an arbitrary expression that evaluates to a null terminated string.
1524 *
2be32bbe 1525 * Sets an assertion that the values that @left and @right evaluate to are
e4aea8f8
BH
1526 * not equal. This is semantically equivalent to
1527 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1528 * for more information.
1529 */
1530#define KUNIT_ASSERT_STRNEQ(test, left, right) \
6709d0fe 1531 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
e4aea8f8
BH
1532
1533#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
955df7d8
DL
1534 KUNIT_BINARY_STR_ASSERTION(test, \
1535 KUNIT_ASSERTION, \
1536 left, !=, right, \
1537 fmt, \
1538 ##__VA_ARGS__)
e4aea8f8 1539
ebf51e46
EC
1540/**
1541 * KUNIT_ASSERT_MEMEQ() - Asserts that the first @size bytes of @left and @right are equal.
1542 * @test: The test context object.
1543 * @left: An arbitrary expression that evaluates to the specified size.
1544 * @right: An arbitrary expression that evaluates to the specified size.
1545 * @size: Number of bytes compared.
1546 *
1547 * Sets an assertion that the values that @left and @right evaluate to are
1548 * equal. This is semantically equivalent to
1549 * KUNIT_ASSERT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
1550 * KUNIT_ASSERT_TRUE() for more information.
1551 *
1552 * Although this assertion works for any memory block, it is not recommended
1553 * for comparing more structured data, such as structs. This assertion is
1554 * recommended for comparing, for example, data arrays.
1555 */
1556#define KUNIT_ASSERT_MEMEQ(test, left, right, size) \
1557 KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, NULL)
1558
1559#define KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
1560 KUNIT_MEM_ASSERTION(test, \
1561 KUNIT_ASSERTION, \
1562 left, ==, right, \
1563 size, \
1564 fmt, \
1565 ##__VA_ARGS__)
1566
1567/**
1568 * KUNIT_ASSERT_MEMNEQ() - Asserts that the first @size bytes of @left and @right are not equal.
1569 * @test: The test context object.
1570 * @left: An arbitrary expression that evaluates to the specified size.
1571 * @right: An arbitrary expression that evaluates to the specified size.
1572 * @size: Number of bytes compared.
1573 *
1574 * Sets an assertion that the values that @left and @right evaluate to are
1575 * not equal. This is semantically equivalent to
1576 * KUNIT_ASSERT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1577 * KUNIT_ASSERT_TRUE() for more information.
1578 *
1579 * Although this assertion works for any memory block, it is not recommended
1580 * for comparing more structured data, such as structs. This assertion is
1581 * recommended for comparing, for example, data arrays.
1582 */
1583#define KUNIT_ASSERT_MEMNEQ(test, left, right, size) \
1584 KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, NULL)
1585
1586#define KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1587 KUNIT_MEM_ASSERTION(test, \
1588 KUNIT_ASSERTION, \
1589 left, !=, right, \
1590 size, \
1591 fmt, \
1592 ##__VA_ARGS__)
1593
caae9458
RR
1594/**
1595 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1596 * @test: The test context object.
1597 * @ptr: an arbitrary pointer.
1598 *
1599 * Sets an assertion that the values that @ptr evaluates to is null. This is
1600 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1601 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1602 */
1603#define KUNIT_ASSERT_NULL(test, ptr) \
1604 KUNIT_ASSERT_NULL_MSG(test, \
1605 ptr, \
1606 NULL)
1607
1608#define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1609 KUNIT_BINARY_PTR_ASSERTION(test, \
1610 KUNIT_ASSERTION, \
1611 ptr, ==, NULL, \
1612 fmt, \
1613 ##__VA_ARGS__)
1614
1615/**
1616 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1617 * @test: The test context object.
1618 * @ptr: an arbitrary pointer.
1619 *
1620 * Sets an assertion that the values that @ptr evaluates to is not null. This
1621 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1622 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1623 */
1624#define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1625 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1626 ptr, \
1627 NULL)
1628
1629#define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1630 KUNIT_BINARY_PTR_ASSERTION(test, \
1631 KUNIT_ASSERTION, \
1632 ptr, !=, NULL, \
1633 fmt, \
1634 ##__VA_ARGS__)
1635
e4aea8f8
BH
1636/**
1637 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1638 * @test: The test context object.
1639 * @ptr: an arbitrary pointer.
1640 *
1641 * Sets an assertion that the value that @ptr evaluates to is not null and not
1642 * an errno stored in a pointer. This is the same as
1643 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1644 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1645 */
1646#define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
6709d0fe 1647 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
e4aea8f8
BH
1648
1649#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1650 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1651 KUNIT_ASSERTION, \
1652 ptr, \
1653 fmt, \
1654 ##__VA_ARGS__)
1655
fadb08e7
AR
1656/**
1657 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1658 * @name: prefix for the test parameter generator function.
1659 * @array: array of test parameters.
1660 * @get_desc: function to convert param to description; NULL to use default
1661 *
1662 * Define function @name_gen_params which uses @array to generate parameters.
1663 */
1664#define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1665 static const void *name##_gen_params(const void *prev, char *desc) \
1666 { \
1667 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1668 if (__next - (array) < ARRAY_SIZE((array))) { \
1669 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1670 if (__get_desc) \
1671 __get_desc(__next, desc); \
1672 return __next; \
1673 } \
1674 return NULL; \
1675 }
1676
292010ee
BB
1677/**
1678 * KUNIT_ARRAY_PARAM_DESC() - Define test parameter generator from an array.
1679 * @name: prefix for the test parameter generator function.
1680 * @array: array of test parameters.
1681 * @desc_member: structure member from array element to use as description
1682 *
1683 * Define function @name_gen_params which uses @array to generate parameters.
1684 */
1685#define KUNIT_ARRAY_PARAM_DESC(name, array, desc_member) \
1686 static const void *name##_gen_params(const void *prev, char *desc) \
1687 { \
1688 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1689 if (__next - (array) < ARRAY_SIZE((array))) { \
1690 strscpy(desc, __next->desc_member, KUNIT_PARAM_DESC_SIZE); \
1691 return __next; \
1692 } \
1693 return NULL; \
1694 }
1695
61695f8c
DL
1696// TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1697// include resource.h themselves if they need it.
1698#include <kunit/resource.h>
1699
914cc63e 1700#endif /* _KUNIT_TEST_H */