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