kunit: tool: make --json do nothing if --raw_ouput is set
[linux-2.6-block.git] / include / kunit / test.h
CommitLineData
914cc63e
BH
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Base unit test (KUnit) API.
4 *
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
7 */
8
9#ifndef _KUNIT_TEST_H
10#define _KUNIT_TEST_H
11
73cda7bb 12#include <kunit/assert.h>
5f3e0620 13#include <kunit/try-catch.h>
ec54c289 14
4fdacef8 15#include <linux/compiler.h>
ec54c289
AS
16#include <linux/container_of.h>
17#include <linux/err.h>
18#include <linux/init.h>
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
97d453bc
DL
479#define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
480 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
c1144e01 481 const struct assert_class __assertion = INITIALIZER; \
97d453bc
DL
482 kunit_do_failed_assertion(test, \
483 &__loc, \
484 assert_type, \
485 &__assertion.assert, \
486 assert_format, \
487 fmt, \
488 ##__VA_ARGS__); \
73cda7bb
BH
489} while (0)
490
491
492#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
97d453bc
DL
493 _KUNIT_FAILED(test, \
494 assert_type, \
495 kunit_fail_assert, \
496 kunit_fail_assert_format, \
497 {}, \
498 fmt, \
499 ##__VA_ARGS__)
73cda7bb
BH
500
501/**
502 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
503 * @test: The test context object.
504 * @fmt: an informational message to be printed when the assertion is made.
505 * @...: string format arguments.
506 *
507 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
508 * other words, it always results in a failed expectation, and consequently
509 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
510 * for more information.
511 */
512#define KUNIT_FAIL(test, fmt, ...) \
513 KUNIT_FAIL_ASSERTION(test, \
514 KUNIT_EXPECTATION, \
515 fmt, \
516 ##__VA_ARGS__)
517
697365c0
DL
518/* Helper to safely pass around an initializer list to other macros. */
519#define KUNIT_INIT_ASSERT(initializers...) { initializers }
520
73cda7bb
BH
521#define KUNIT_UNARY_ASSERTION(test, \
522 assert_type, \
697365c0
DL
523 condition_, \
524 expected_true_, \
73cda7bb
BH
525 fmt, \
526 ...) \
97d453bc 527do { \
697365c0 528 if (likely(!!(condition_) == !!expected_true_)) \
97d453bc
DL
529 break; \
530 \
531 _KUNIT_FAILED(test, \
532 assert_type, \
533 kunit_unary_assert, \
534 kunit_unary_assert_format, \
697365c0
DL
535 KUNIT_INIT_ASSERT(.condition = #condition_, \
536 .expected_true = expected_true_), \
97d453bc
DL
537 fmt, \
538 ##__VA_ARGS__); \
539} while (0)
73cda7bb
BH
540
541#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
542 KUNIT_UNARY_ASSERTION(test, \
543 assert_type, \
544 condition, \
545 true, \
546 fmt, \
547 ##__VA_ARGS__)
548
73cda7bb
BH
549#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
550 KUNIT_UNARY_ASSERTION(test, \
551 assert_type, \
552 condition, \
553 false, \
554 fmt, \
555 ##__VA_ARGS__)
556
73cda7bb
BH
557/*
558 * A factory macro for defining the assertions and expectations for the basic
559 * comparisons defined for the built in types.
560 *
561 * Unfortunately, there is no common type that all types can be promoted to for
562 * which all the binary operators behave the same way as for the actual types
563 * (for example, there is no type that long long and unsigned long long can
564 * both be cast to where the comparison result is preserved for all values). So
565 * the best we can do is do the comparison in the original types and then coerce
566 * everything to long long for printing; this way, the comparison behaves
567 * correctly and the printed out value usually makes sense without
568 * interpretation, but can always be interpreted to figure out the actual
569 * value.
570 */
571#define KUNIT_BASE_BINARY_ASSERTION(test, \
572 assert_class, \
064ff292 573 format_func, \
73cda7bb
BH
574 assert_type, \
575 left, \
576 op, \
577 right, \
578 fmt, \
579 ...) \
580do { \
c2741453
DL
581 const typeof(left) __left = (left); \
582 const typeof(right) __right = (right); \
2b6861e2
DL
583 static const struct kunit_binary_assert_text __text = { \
584 .operation = #op, \
585 .left_text = #left, \
586 .right_text = #right, \
587 }; \
73cda7bb 588 \
97d453bc
DL
589 if (likely(__left op __right)) \
590 break; \
591 \
592 _KUNIT_FAILED(test, \
593 assert_type, \
594 assert_class, \
595 format_func, \
697365c0
DL
596 KUNIT_INIT_ASSERT(.text = &__text, \
597 .left_value = __left, \
598 .right_value = __right), \
97d453bc
DL
599 fmt, \
600 ##__VA_ARGS__); \
73cda7bb
BH
601} while (0)
602
40f39777
DL
603#define KUNIT_BINARY_INT_ASSERTION(test, \
604 assert_type, \
605 left, \
606 op, \
607 right, \
608 fmt, \
73cda7bb
BH
609 ...) \
610 KUNIT_BASE_BINARY_ASSERTION(test, \
73cda7bb 611 kunit_binary_assert, \
064ff292 612 kunit_binary_assert_format, \
73cda7bb 613 assert_type, \
40f39777 614 left, op, right, \
73cda7bb
BH
615 fmt, \
616 ##__VA_ARGS__)
617
6125a5c7
DL
618#define KUNIT_BINARY_PTR_ASSERTION(test, \
619 assert_type, \
620 left, \
621 op, \
622 right, \
623 fmt, \
624 ...) \
625 KUNIT_BASE_BINARY_ASSERTION(test, \
73cda7bb 626 kunit_binary_ptr_assert, \
064ff292 627 kunit_binary_ptr_assert_format, \
73cda7bb 628 assert_type, \
6125a5c7 629 left, op, right, \
73cda7bb
BH
630 fmt, \
631 ##__VA_ARGS__)
632
73cda7bb
BH
633#define KUNIT_BINARY_STR_ASSERTION(test, \
634 assert_type, \
635 left, \
636 op, \
637 right, \
638 fmt, \
639 ...) \
640do { \
3747b5c0 641 const char *__left = (left); \
2b6861e2
DL
642 const char *__right = (right); \
643 static const struct kunit_binary_assert_text __text = { \
644 .operation = #op, \
645 .left_text = #left, \
646 .right_text = #right, \
647 }; \
73cda7bb 648 \
97d453bc
DL
649 if (likely(strcmp(__left, __right) op 0)) \
650 break; \
651 \
652 \
653 _KUNIT_FAILED(test, \
654 assert_type, \
655 kunit_binary_str_assert, \
656 kunit_binary_str_assert_format, \
697365c0
DL
657 KUNIT_INIT_ASSERT(.text = &__text, \
658 .left_value = __left, \
659 .right_value = __right), \
97d453bc
DL
660 fmt, \
661 ##__VA_ARGS__); \
73cda7bb
BH
662} while (0)
663
b8a926be
MC
664#define KUNIT_MEM_ASSERTION(test, \
665 assert_type, \
666 left, \
667 op, \
668 right, \
34c68f43 669 size_, \
b8a926be
MC
670 fmt, \
671 ...) \
672do { \
673 const void *__left = (left); \
674 const void *__right = (right); \
34c68f43 675 const size_t __size = (size_); \
b8a926be
MC
676 static const struct kunit_binary_assert_text __text = { \
677 .operation = #op, \
678 .left_text = #left, \
679 .right_text = #right, \
680 }; \
681 \
682 if (likely(memcmp(__left, __right, __size) op 0)) \
683 break; \
684 \
685 _KUNIT_FAILED(test, \
686 assert_type, \
687 kunit_mem_assert, \
688 kunit_mem_assert_format, \
34c68f43
DL
689 KUNIT_INIT_ASSERT(.text = &__text, \
690 .left_value = __left, \
691 .right_value = __right, \
692 .size = __size), \
b8a926be
MC
693 fmt, \
694 ##__VA_ARGS__); \
695} while (0)
696
73cda7bb
BH
697#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
698 assert_type, \
699 ptr, \
700 fmt, \
701 ...) \
702do { \
c2741453 703 const typeof(ptr) __ptr = (ptr); \
73cda7bb 704 \
97d453bc
DL
705 if (!IS_ERR_OR_NULL(__ptr)) \
706 break; \
707 \
708 _KUNIT_FAILED(test, \
709 assert_type, \
710 kunit_ptr_not_err_assert, \
711 kunit_ptr_not_err_assert_format, \
697365c0 712 KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \
97d453bc
DL
713 fmt, \
714 ##__VA_ARGS__); \
73cda7bb
BH
715} while (0)
716
73cda7bb
BH
717/**
718 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
719 * @test: The test context object.
720 * @condition: an arbitrary boolean expression. The test fails when this does
721 * not evaluate to true.
722 *
723 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
724 * to fail when the specified condition is not met; however, it will not prevent
725 * the test case from continuing to run; this is otherwise known as an
726 * *expectation failure*.
727 */
728#define KUNIT_EXPECT_TRUE(test, condition) \
6709d0fe 729 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
73cda7bb
BH
730
731#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
732 KUNIT_TRUE_MSG_ASSERTION(test, \
733 KUNIT_EXPECTATION, \
734 condition, \
735 fmt, \
736 ##__VA_ARGS__)
737
738/**
739 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
740 * @test: The test context object.
741 * @condition: an arbitrary boolean expression. The test fails when this does
742 * not evaluate to false.
743 *
744 * Sets an expectation that @condition evaluates to false. See
745 * KUNIT_EXPECT_TRUE() for more information.
746 */
747#define KUNIT_EXPECT_FALSE(test, condition) \
6709d0fe 748 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
73cda7bb
BH
749
750#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
751 KUNIT_FALSE_MSG_ASSERTION(test, \
752 KUNIT_EXPECTATION, \
753 condition, \
754 fmt, \
755 ##__VA_ARGS__)
756
757/**
758 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
759 * @test: The test context object.
760 * @left: an arbitrary expression that evaluates to a primitive C type.
761 * @right: an arbitrary expression that evaluates to a primitive C type.
762 *
763 * Sets an expectation that the values that @left and @right evaluate to are
764 * equal. This is semantically equivalent to
765 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
766 * more information.
767 */
768#define KUNIT_EXPECT_EQ(test, left, right) \
6709d0fe 769 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
73cda7bb
BH
770
771#define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
772 KUNIT_BINARY_INT_ASSERTION(test, \
773 KUNIT_EXPECTATION, \
774 left, ==, right, \
775 fmt, \
776 ##__VA_ARGS__)
73cda7bb
BH
777
778/**
779 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
780 * @test: The test context object.
781 * @left: an arbitrary expression that evaluates to a pointer.
782 * @right: an arbitrary expression that evaluates to a pointer.
783 *
784 * Sets an expectation that the values that @left and @right evaluate to are
785 * equal. This is semantically equivalent to
786 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
787 * more information.
788 */
789#define KUNIT_EXPECT_PTR_EQ(test, left, right) \
6709d0fe 790 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
73cda7bb
BH
791
792#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
793 KUNIT_BINARY_PTR_ASSERTION(test, \
794 KUNIT_EXPECTATION, \
795 left, ==, right, \
796 fmt, \
797 ##__VA_ARGS__)
73cda7bb
BH
798
799/**
800 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
801 * @test: The test context object.
802 * @left: an arbitrary expression that evaluates to a primitive C type.
803 * @right: an arbitrary expression that evaluates to a primitive C type.
804 *
805 * Sets an expectation that the values that @left and @right evaluate to are not
806 * equal. This is semantically equivalent to
807 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
808 * more information.
809 */
810#define KUNIT_EXPECT_NE(test, left, right) \
6709d0fe 811 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
73cda7bb
BH
812
813#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
814 KUNIT_BINARY_INT_ASSERTION(test, \
815 KUNIT_EXPECTATION, \
816 left, !=, right, \
817 fmt, \
818 ##__VA_ARGS__)
73cda7bb
BH
819
820/**
821 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
822 * @test: The test context object.
823 * @left: an arbitrary expression that evaluates to a pointer.
824 * @right: an arbitrary expression that evaluates to a pointer.
825 *
826 * Sets an expectation that the values that @left and @right evaluate to are not
827 * equal. This is semantically equivalent to
828 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
829 * more information.
830 */
831#define KUNIT_EXPECT_PTR_NE(test, left, right) \
6709d0fe 832 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
73cda7bb
BH
833
834#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
835 KUNIT_BINARY_PTR_ASSERTION(test, \
836 KUNIT_EXPECTATION, \
837 left, !=, right, \
838 fmt, \
839 ##__VA_ARGS__)
73cda7bb
BH
840
841/**
842 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
843 * @test: The test context object.
844 * @left: an arbitrary expression that evaluates to a primitive C type.
845 * @right: an arbitrary expression that evaluates to a primitive C type.
846 *
847 * Sets an expectation that the value that @left evaluates to is less than the
848 * value that @right evaluates to. This is semantically equivalent to
849 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
850 * more information.
851 */
852#define KUNIT_EXPECT_LT(test, left, right) \
6709d0fe 853 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
73cda7bb
BH
854
855#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
40f39777
DL
856 KUNIT_BINARY_INT_ASSERTION(test, \
857 KUNIT_EXPECTATION, \
858 left, <, right, \
859 fmt, \
860 ##__VA_ARGS__)
73cda7bb
BH
861
862/**
863 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
864 * @test: The test context object.
865 * @left: an arbitrary expression that evaluates to a primitive C type.
866 * @right: an arbitrary expression that evaluates to a primitive C type.
867 *
868 * Sets an expectation that the value that @left evaluates to is less than or
869 * equal to the value that @right evaluates to. Semantically this is equivalent
870 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
871 * more information.
872 */
873#define KUNIT_EXPECT_LE(test, left, right) \
6709d0fe 874 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
73cda7bb
BH
875
876#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
40f39777 877 KUNIT_BINARY_INT_ASSERTION(test, \
aded3cad 878 KUNIT_EXPECTATION, \
40f39777
DL
879 left, <=, right, \
880 fmt, \
881 ##__VA_ARGS__)
73cda7bb
BH
882
883/**
884 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
885 * @test: The test context object.
886 * @left: an arbitrary expression that evaluates to a primitive C type.
887 * @right: an arbitrary expression that evaluates to a primitive C type.
888 *
889 * Sets an expectation that the value that @left evaluates to is greater than
890 * the value that @right evaluates to. This is semantically equivalent to
891 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
892 * more information.
893 */
894#define KUNIT_EXPECT_GT(test, left, right) \
6709d0fe 895 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
73cda7bb
BH
896
897#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
40f39777
DL
898 KUNIT_BINARY_INT_ASSERTION(test, \
899 KUNIT_EXPECTATION, \
900 left, >, right, \
901 fmt, \
902 ##__VA_ARGS__)
73cda7bb
BH
903
904/**
905 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
906 * @test: The test context object.
907 * @left: an arbitrary expression that evaluates to a primitive C type.
908 * @right: an arbitrary expression that evaluates to a primitive C type.
909 *
910 * Sets an expectation that the value that @left evaluates to is greater than
911 * the value that @right evaluates to. This is semantically equivalent to
912 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
913 * more information.
914 */
915#define KUNIT_EXPECT_GE(test, left, right) \
6709d0fe 916 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
73cda7bb
BH
917
918#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
40f39777
DL
919 KUNIT_BINARY_INT_ASSERTION(test, \
920 KUNIT_EXPECTATION, \
921 left, >=, right, \
922 fmt, \
923 ##__VA_ARGS__)
73cda7bb
BH
924
925/**
926 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
927 * @test: The test context object.
928 * @left: an arbitrary expression that evaluates to a null terminated string.
929 * @right: an arbitrary expression that evaluates to a null terminated string.
930 *
931 * Sets an expectation that the values that @left and @right evaluate to are
932 * equal. This is semantically equivalent to
933 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
934 * for more information.
935 */
936#define KUNIT_EXPECT_STREQ(test, left, right) \
6709d0fe 937 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
73cda7bb
BH
938
939#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
955df7d8
DL
940 KUNIT_BINARY_STR_ASSERTION(test, \
941 KUNIT_EXPECTATION, \
942 left, ==, right, \
943 fmt, \
944 ##__VA_ARGS__)
73cda7bb
BH
945
946/**
947 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
948 * @test: The test context object.
949 * @left: an arbitrary expression that evaluates to a null terminated string.
950 * @right: an arbitrary expression that evaluates to a null terminated string.
951 *
952 * Sets an expectation that the values that @left and @right evaluate to are
953 * not equal. This is semantically equivalent to
954 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
955 * for more information.
956 */
957#define KUNIT_EXPECT_STRNEQ(test, left, right) \
6709d0fe 958 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
73cda7bb
BH
959
960#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
955df7d8
DL
961 KUNIT_BINARY_STR_ASSERTION(test, \
962 KUNIT_EXPECTATION, \
963 left, !=, right, \
964 fmt, \
965 ##__VA_ARGS__)
b8a926be
MC
966
967/**
968 * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.
969 * @test: The test context object.
970 * @left: An arbitrary expression that evaluates to the specified size.
971 * @right: An arbitrary expression that evaluates to the specified size.
972 * @size: Number of bytes compared.
973 *
974 * Sets an expectation that the values that @left and @right evaluate to are
975 * equal. This is semantically equivalent to
976 * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
977 * KUNIT_EXPECT_TRUE() for more information.
978 *
979 * Although this expectation works for any memory block, it is not recommended
980 * for comparing more structured data, such as structs. This expectation is
981 * recommended for comparing, for example, data arrays.
982 */
983#define KUNIT_EXPECT_MEMEQ(test, left, right, size) \
984 KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)
985
986#define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
987 KUNIT_MEM_ASSERTION(test, \
988 KUNIT_EXPECTATION, \
989 left, ==, right, \
990 size, \
991 fmt, \
992 ##__VA_ARGS__)
993
994/**
995 * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.
996 * @test: The test context object.
997 * @left: An arbitrary expression that evaluates to the specified size.
998 * @right: An arbitrary expression that evaluates to the specified size.
999 * @size: Number of bytes compared.
1000 *
1001 * Sets an expectation that the values that @left and @right evaluate to are
1002 * not equal. This is semantically equivalent to
1003 * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1004 * KUNIT_EXPECT_TRUE() for more information.
1005 *
1006 * Although this expectation works for any memory block, it is not recommended
1007 * for comparing more structured data, such as structs. This expectation is
1008 * recommended for comparing, for example, data arrays.
1009 */
1010#define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \
1011 KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)
1012
1013#define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1014 KUNIT_MEM_ASSERTION(test, \
1015 KUNIT_EXPECTATION, \
1016 left, !=, right, \
1017 size, \
1018 fmt, \
1019 ##__VA_ARGS__)
73cda7bb 1020
caae9458
RR
1021/**
1022 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
1023 * @test: The test context object.
1024 * @ptr: an arbitrary pointer.
1025 *
1026 * Sets an expectation that the value that @ptr evaluates to is null. This is
1027 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
1028 * See KUNIT_EXPECT_TRUE() for more information.
1029 */
1030#define KUNIT_EXPECT_NULL(test, ptr) \
1031 KUNIT_EXPECT_NULL_MSG(test, \
1032 ptr, \
1033 NULL)
1034
1035#define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
1036 KUNIT_BINARY_PTR_ASSERTION(test, \
1037 KUNIT_EXPECTATION, \
1038 ptr, ==, NULL, \
1039 fmt, \
1040 ##__VA_ARGS__)
1041
1042/**
1043 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
1044 * @test: The test context object.
1045 * @ptr: an arbitrary pointer.
1046 *
1047 * Sets an expectation that the value that @ptr evaluates to is not null. This
1048 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
1049 * See KUNIT_EXPECT_TRUE() for more information.
1050 */
1051#define KUNIT_EXPECT_NOT_NULL(test, ptr) \
1052 KUNIT_EXPECT_NOT_NULL_MSG(test, \
1053 ptr, \
1054 NULL)
1055
1056#define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1057 KUNIT_BINARY_PTR_ASSERTION(test, \
1058 KUNIT_EXPECTATION, \
1059 ptr, !=, NULL, \
1060 fmt, \
1061 ##__VA_ARGS__)
1062
73cda7bb
BH
1063/**
1064 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1065 * @test: The test context object.
1066 * @ptr: an arbitrary pointer.
1067 *
1068 * Sets an expectation that the value that @ptr evaluates to is not null and not
1069 * an errno stored in a pointer. This is semantically equivalent to
1070 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1071 * more information.
1072 */
1073#define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
6709d0fe 1074 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
73cda7bb
BH
1075
1076#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1077 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1078 KUNIT_EXPECTATION, \
1079 ptr, \
1080 fmt, \
1081 ##__VA_ARGS__)
1082
e4aea8f8
BH
1083#define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
1084 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1085
1086/**
1087 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1088 * @test: The test context object.
1089 * @condition: an arbitrary boolean expression. The test fails and aborts when
1090 * this does not evaluate to true.
1091 *
1092 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1093 * fail *and immediately abort* when the specified condition is not met. Unlike
1094 * an expectation failure, it will prevent the test case from continuing to run;
1095 * this is otherwise known as an *assertion failure*.
1096 */
1097#define KUNIT_ASSERT_TRUE(test, condition) \
6709d0fe 1098 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
e4aea8f8
BH
1099
1100#define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1101 KUNIT_TRUE_MSG_ASSERTION(test, \
1102 KUNIT_ASSERTION, \
1103 condition, \
1104 fmt, \
1105 ##__VA_ARGS__)
1106
1107/**
1108 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1109 * @test: The test context object.
1110 * @condition: an arbitrary boolean expression.
1111 *
1112 * Sets an assertion that the value that @condition evaluates to is false. This
1113 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1114 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1115 */
1116#define KUNIT_ASSERT_FALSE(test, condition) \
6709d0fe 1117 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
e4aea8f8
BH
1118
1119#define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1120 KUNIT_FALSE_MSG_ASSERTION(test, \
1121 KUNIT_ASSERTION, \
1122 condition, \
1123 fmt, \
1124 ##__VA_ARGS__)
1125
1126/**
1127 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1128 * @test: The test context object.
1129 * @left: an arbitrary expression that evaluates to a primitive C type.
1130 * @right: an arbitrary expression that evaluates to a primitive C type.
1131 *
1132 * Sets an assertion that the values that @left and @right evaluate to are
1133 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1134 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1135 */
1136#define KUNIT_ASSERT_EQ(test, left, right) \
6709d0fe 1137 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
e4aea8f8
BH
1138
1139#define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
1140 KUNIT_BINARY_INT_ASSERTION(test, \
1141 KUNIT_ASSERTION, \
1142 left, ==, right, \
1143 fmt, \
1144 ##__VA_ARGS__)
e4aea8f8
BH
1145
1146/**
1147 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1148 * @test: The test context object.
1149 * @left: an arbitrary expression that evaluates to a pointer.
1150 * @right: an arbitrary expression that evaluates to a pointer.
1151 *
1152 * Sets an assertion that the values that @left and @right evaluate to are
1153 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1154 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1155 */
1156#define KUNIT_ASSERT_PTR_EQ(test, left, right) \
6709d0fe 1157 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
e4aea8f8
BH
1158
1159#define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
1160 KUNIT_BINARY_PTR_ASSERTION(test, \
1161 KUNIT_ASSERTION, \
1162 left, ==, right, \
1163 fmt, \
1164 ##__VA_ARGS__)
e4aea8f8
BH
1165
1166/**
1167 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1168 * @test: The test context object.
1169 * @left: an arbitrary expression that evaluates to a primitive C type.
1170 * @right: an arbitrary expression that evaluates to a primitive C type.
1171 *
1172 * Sets an assertion that the values that @left and @right evaluate to are not
1173 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1174 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1175 */
1176#define KUNIT_ASSERT_NE(test, left, right) \
6709d0fe 1177 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
e4aea8f8
BH
1178
1179#define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
1180 KUNIT_BINARY_INT_ASSERTION(test, \
1181 KUNIT_ASSERTION, \
1182 left, !=, right, \
1183 fmt, \
1184 ##__VA_ARGS__)
e4aea8f8
BH
1185
1186/**
1187 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1188 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1189 * @test: The test context object.
1190 * @left: an arbitrary expression that evaluates to a pointer.
1191 * @right: an arbitrary expression that evaluates to a pointer.
1192 *
1193 * Sets an assertion that the values that @left and @right evaluate to are not
1194 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1195 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1196 */
1197#define KUNIT_ASSERT_PTR_NE(test, left, right) \
6709d0fe 1198 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
e4aea8f8
BH
1199
1200#define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
6125a5c7
DL
1201 KUNIT_BINARY_PTR_ASSERTION(test, \
1202 KUNIT_ASSERTION, \
1203 left, !=, right, \
1204 fmt, \
1205 ##__VA_ARGS__)
e4aea8f8
BH
1206/**
1207 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1208 * @test: The test context object.
1209 * @left: an arbitrary expression that evaluates to a primitive C type.
1210 * @right: an arbitrary expression that evaluates to a primitive C type.
1211 *
1212 * Sets an assertion that the value that @left evaluates to is less than the
1213 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1214 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1215 * is not met.
1216 */
1217#define KUNIT_ASSERT_LT(test, left, right) \
6709d0fe 1218 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
e4aea8f8
BH
1219
1220#define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
40f39777 1221 KUNIT_BINARY_INT_ASSERTION(test, \
aded3cad 1222 KUNIT_ASSERTION, \
40f39777
DL
1223 left, <, right, \
1224 fmt, \
1225 ##__VA_ARGS__)
e4aea8f8
BH
1226/**
1227 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1228 * @test: The test context object.
1229 * @left: an arbitrary expression that evaluates to a primitive C type.
1230 * @right: an arbitrary expression that evaluates to a primitive C type.
1231 *
1232 * Sets an assertion that the value that @left evaluates to is less than or
1233 * equal to the value that @right evaluates to. This is the same as
1234 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1235 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1236 */
1237#define KUNIT_ASSERT_LE(test, left, right) \
6709d0fe 1238 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
e4aea8f8
BH
1239
1240#define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
40f39777
DL
1241 KUNIT_BINARY_INT_ASSERTION(test, \
1242 KUNIT_ASSERTION, \
1243 left, <=, right, \
1244 fmt, \
1245 ##__VA_ARGS__)
e4aea8f8
BH
1246
1247/**
1248 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1249 * @test: The test context object.
1250 * @left: an arbitrary expression that evaluates to a primitive C type.
1251 * @right: an arbitrary expression that evaluates to a primitive C type.
1252 *
1253 * Sets an assertion that the value that @left evaluates to is greater than the
1254 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1255 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1256 * is not met.
1257 */
1258#define KUNIT_ASSERT_GT(test, left, right) \
6709d0fe 1259 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
e4aea8f8
BH
1260
1261#define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
40f39777 1262 KUNIT_BINARY_INT_ASSERTION(test, \
aded3cad 1263 KUNIT_ASSERTION, \
40f39777
DL
1264 left, >, right, \
1265 fmt, \
1266 ##__VA_ARGS__)
e4aea8f8
BH
1267
1268/**
1269 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1270 * @test: The test context object.
1271 * @left: an arbitrary expression that evaluates to a primitive C type.
1272 * @right: an arbitrary expression that evaluates to a primitive C type.
1273 *
1274 * Sets an assertion that the value that @left evaluates to is greater than the
1275 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1276 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1277 * is not met.
1278 */
1279#define KUNIT_ASSERT_GE(test, left, right) \
6709d0fe 1280 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
e4aea8f8
BH
1281
1282#define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
40f39777
DL
1283 KUNIT_BINARY_INT_ASSERTION(test, \
1284 KUNIT_ASSERTION, \
1285 left, >=, right, \
1286 fmt, \
1287 ##__VA_ARGS__)
e4aea8f8
BH
1288
1289/**
1290 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1291 * @test: The test context object.
1292 * @left: an arbitrary expression that evaluates to a null terminated string.
1293 * @right: an arbitrary expression that evaluates to a null terminated string.
1294 *
1295 * Sets an assertion that the values that @left and @right evaluate to are
1296 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1297 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1298 */
1299#define KUNIT_ASSERT_STREQ(test, left, right) \
6709d0fe 1300 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
e4aea8f8
BH
1301
1302#define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
955df7d8
DL
1303 KUNIT_BINARY_STR_ASSERTION(test, \
1304 KUNIT_ASSERTION, \
1305 left, ==, right, \
1306 fmt, \
1307 ##__VA_ARGS__)
e4aea8f8
BH
1308
1309/**
1310 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1311 * @test: The test context object.
1312 * @left: an arbitrary expression that evaluates to a null terminated string.
1313 * @right: an arbitrary expression that evaluates to a null terminated string.
1314 *
1315 * Sets an expectation that the values that @left and @right evaluate to are
1316 * not equal. This is semantically equivalent to
1317 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1318 * for more information.
1319 */
1320#define KUNIT_ASSERT_STRNEQ(test, left, right) \
6709d0fe 1321 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
e4aea8f8
BH
1322
1323#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
955df7d8
DL
1324 KUNIT_BINARY_STR_ASSERTION(test, \
1325 KUNIT_ASSERTION, \
1326 left, !=, right, \
1327 fmt, \
1328 ##__VA_ARGS__)
e4aea8f8 1329
caae9458
RR
1330/**
1331 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1332 * @test: The test context object.
1333 * @ptr: an arbitrary pointer.
1334 *
1335 * Sets an assertion that the values that @ptr evaluates to is null. This is
1336 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1337 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1338 */
1339#define KUNIT_ASSERT_NULL(test, ptr) \
1340 KUNIT_ASSERT_NULL_MSG(test, \
1341 ptr, \
1342 NULL)
1343
1344#define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1345 KUNIT_BINARY_PTR_ASSERTION(test, \
1346 KUNIT_ASSERTION, \
1347 ptr, ==, NULL, \
1348 fmt, \
1349 ##__VA_ARGS__)
1350
1351/**
1352 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1353 * @test: The test context object.
1354 * @ptr: an arbitrary pointer.
1355 *
1356 * Sets an assertion that the values that @ptr evaluates to is not null. This
1357 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1358 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1359 */
1360#define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1361 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1362 ptr, \
1363 NULL)
1364
1365#define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1366 KUNIT_BINARY_PTR_ASSERTION(test, \
1367 KUNIT_ASSERTION, \
1368 ptr, !=, NULL, \
1369 fmt, \
1370 ##__VA_ARGS__)
1371
e4aea8f8
BH
1372/**
1373 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1374 * @test: The test context object.
1375 * @ptr: an arbitrary pointer.
1376 *
1377 * Sets an assertion that the value that @ptr evaluates to is not null and not
1378 * an errno stored in a pointer. This is the same as
1379 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1380 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1381 */
1382#define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
6709d0fe 1383 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
e4aea8f8
BH
1384
1385#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1386 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1387 KUNIT_ASSERTION, \
1388 ptr, \
1389 fmt, \
1390 ##__VA_ARGS__)
1391
fadb08e7
AR
1392/**
1393 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1394 * @name: prefix for the test parameter generator function.
1395 * @array: array of test parameters.
1396 * @get_desc: function to convert param to description; NULL to use default
1397 *
1398 * Define function @name_gen_params which uses @array to generate parameters.
1399 */
1400#define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1401 static const void *name##_gen_params(const void *prev, char *desc) \
1402 { \
1403 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1404 if (__next - (array) < ARRAY_SIZE((array))) { \
1405 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1406 if (__get_desc) \
1407 __get_desc(__next, desc); \
1408 return __next; \
1409 } \
1410 return NULL; \
1411 }
1412
61695f8c
DL
1413// TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1414// include resource.h themselves if they need it.
1415#include <kunit/resource.h>
1416
914cc63e 1417#endif /* _KUNIT_TEST_H */