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