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