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