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