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