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