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