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