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