Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * Base unit test (KUnit) API. |
| 4 | * |
| 5 | * Copyright (C) 2019, Google LLC. |
| 6 | * Author: Brendan Higgins <[email protected]> |
| 7 | */ |
| 8 | |
| 9 | #ifndef _KUNIT_TEST_H |
| 10 | #define _KUNIT_TEST_H |
| 11 | |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 12 | #include <kunit/assert.h> |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 13 | #include <kunit/try-catch.h> |
Andy Shevchenko | ec54c28 | 2021-11-08 18:32:15 -0800 | [diff] [blame] | 14 | |
Daniel Latypov | 4fdacef | 2022-01-13 08:59:27 -0800 | [diff] [blame] | 15 | #include <linux/compiler.h> |
Andy Shevchenko | ec54c28 | 2021-11-08 18:32:15 -0800 | [diff] [blame] | 16 | #include <linux/container_of.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/kconfig.h> |
| 20 | #include <linux/kref.h> |
| 21 | #include <linux/list.h> |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 22 | #include <linux/module.h> |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 23 | #include <linux/slab.h> |
Andy Shevchenko | ec54c28 | 2021-11-08 18:32:15 -0800 | [diff] [blame] | 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/string.h> |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 26 | #include <linux/types.h> |
Andy Shevchenko | ec54c28 | 2021-11-08 18:32:15 -0800 | [diff] [blame] | 27 | |
| 28 | #include <asm/rwonce.h> |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 29 | |
| 30 | struct kunit; |
| 31 | |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 32 | /* Size of log associated with test. */ |
| 33 | #define KUNIT_LOG_SIZE 512 |
| 34 | |
Arpitha Raghunandan | fadb08e7 | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 35 | /* Maximum size of parameter description string. */ |
| 36 | #define KUNIT_PARAM_DESC_SIZE 128 |
| 37 | |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 38 | /* Maximum size of a status comment. */ |
| 39 | #define KUNIT_STATUS_COMMENT_SIZE 256 |
| 40 | |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 41 | /* |
| 42 | * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a |
| 43 | * sub-subtest. See the "Subtests" section in |
| 44 | * https://node-tap.org/tap-protocol/ |
| 45 | */ |
| 46 | #define KUNIT_SUBTEST_INDENT " " |
| 47 | #define KUNIT_SUBSUBTEST_INDENT " " |
| 48 | |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 49 | /** |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 50 | * enum kunit_status - Type of result for a test or test suite |
| 51 | * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped |
| 52 | * @KUNIT_FAILURE: Denotes the test has failed. |
| 53 | * @KUNIT_SKIPPED: Denotes the test has been skipped. |
| 54 | */ |
| 55 | enum kunit_status { |
| 56 | KUNIT_SUCCESS, |
| 57 | KUNIT_FAILURE, |
| 58 | KUNIT_SKIPPED, |
| 59 | }; |
| 60 | |
| 61 | /** |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 62 | * struct kunit_case - represents an individual test case. |
| 63 | * |
| 64 | * @run_case: the function representing the actual test case. |
| 65 | * @name: the name of the test case. |
Arpitha Raghunandan | fadb08e7 | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 66 | * @generate_params: the generator function for parameterized tests. |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 67 | * |
| 68 | * A test case is a function with the signature, |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 69 | * ``void (*)(struct kunit *)`` |
| 70 | * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and |
| 71 | * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 72 | * with a &struct kunit_suite and will be run after the suite's init |
| 73 | * function and followed by the suite's exit function. |
| 74 | * |
| 75 | * A test case should be static and should only be created with the |
| 76 | * KUNIT_CASE() macro; additionally, every array of test cases should be |
| 77 | * terminated with an empty test case. |
| 78 | * |
| 79 | * Example: |
| 80 | * |
| 81 | * .. code-block:: c |
| 82 | * |
| 83 | * void add_test_basic(struct kunit *test) |
| 84 | * { |
| 85 | * KUNIT_EXPECT_EQ(test, 1, add(1, 0)); |
| 86 | * KUNIT_EXPECT_EQ(test, 2, add(1, 1)); |
| 87 | * KUNIT_EXPECT_EQ(test, 0, add(-1, 1)); |
| 88 | * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX)); |
| 89 | * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN)); |
| 90 | * } |
| 91 | * |
| 92 | * static struct kunit_case example_test_cases[] = { |
| 93 | * KUNIT_CASE(add_test_basic), |
| 94 | * {} |
| 95 | * }; |
| 96 | * |
| 97 | */ |
| 98 | struct kunit_case { |
| 99 | void (*run_case)(struct kunit *test); |
| 100 | const char *name; |
Arpitha Raghunandan | fadb08e7 | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 101 | const void* (*generate_params)(const void *prev, char *desc); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 102 | |
| 103 | /* private: internal use only. */ |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 104 | enum kunit_status status; |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 105 | char *log; |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 108 | static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 109 | { |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 110 | switch (status) { |
| 111 | case KUNIT_SKIPPED: |
| 112 | case KUNIT_SUCCESS: |
| 113 | return "ok"; |
| 114 | case KUNIT_FAILURE: |
| 115 | return "not ok"; |
| 116 | } |
| 117 | return "invalid"; |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 120 | /** |
| 121 | * KUNIT_CASE - A helper for creating a &struct kunit_case |
| 122 | * |
| 123 | * @test_name: a reference to a test case function. |
| 124 | * |
| 125 | * Takes a symbol for a function representing a test case and creates a |
| 126 | * &struct kunit_case object from it. See the documentation for |
| 127 | * &struct kunit_case for an example on how to use it. |
| 128 | */ |
| 129 | #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name } |
| 130 | |
| 131 | /** |
Arpitha Raghunandan | fadb08e7 | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 132 | * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case |
| 133 | * |
| 134 | * @test_name: a reference to a test case function. |
| 135 | * @gen_params: a reference to a parameter generator function. |
| 136 | * |
| 137 | * The generator function:: |
| 138 | * |
| 139 | * const void* gen_params(const void *prev, char *desc) |
| 140 | * |
| 141 | * is used to lazily generate a series of arbitrarily typed values that fit into |
| 142 | * a void*. The argument @prev is the previously returned value, which should be |
| 143 | * used to derive the next value; @prev is set to NULL on the initial generator |
| 144 | * call. When no more values are available, the generator must return NULL. |
| 145 | * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE) |
| 146 | * describing the parameter. |
| 147 | */ |
| 148 | #define KUNIT_CASE_PARAM(test_name, gen_params) \ |
| 149 | { .run_case = test_name, .name = #test_name, \ |
| 150 | .generate_params = gen_params } |
| 151 | |
| 152 | /** |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 153 | * struct kunit_suite - describes a related collection of &struct kunit_case |
| 154 | * |
| 155 | * @name: the name of the test. Purely informational. |
Daniel Latypov | 1cdba21 | 2022-04-29 11:12:57 -0700 | [diff] [blame] | 156 | * @suite_init: called once per test suite before the test cases. |
| 157 | * @suite_exit: called once per test suite after all test cases. |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 158 | * @init: called before every test case. |
| 159 | * @exit: called after every test case. |
| 160 | * @test_cases: a null terminated array of test cases. |
| 161 | * |
| 162 | * A kunit_suite is a collection of related &struct kunit_case s, such that |
| 163 | * @init is called before every test case and @exit is called after every |
| 164 | * test case, similar to the notion of a *test fixture* or a *test class* |
| 165 | * in other unit testing frameworks like JUnit or Googletest. |
| 166 | * |
| 167 | * Every &struct kunit_case must be associated with a kunit_suite for KUnit |
| 168 | * to run it. |
| 169 | */ |
| 170 | struct kunit_suite { |
| 171 | const char name[256]; |
Daniel Latypov | 1cdba21 | 2022-04-29 11:12:57 -0700 | [diff] [blame] | 172 | int (*suite_init)(struct kunit_suite *suite); |
| 173 | void (*suite_exit)(struct kunit_suite *suite); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 174 | int (*init)(struct kunit *test); |
| 175 | void (*exit)(struct kunit *test); |
| 176 | struct kunit_case *test_cases; |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 177 | |
Lothar Rubusch | c4714b0 | 2020-04-15 20:16:53 +0000 | [diff] [blame] | 178 | /* private: internal use only */ |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 179 | char status_comment[KUNIT_STATUS_COMMENT_SIZE]; |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 180 | struct dentry *debugfs; |
| 181 | char *log; |
Daniel Latypov | 1cdba21 | 2022-04-29 11:12:57 -0700 | [diff] [blame] | 182 | int suite_init_err; |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | /** |
| 186 | * struct kunit - represents a running instance of a test. |
| 187 | * |
| 188 | * @priv: for user to store arbitrary data. Commonly used to pass data |
| 189 | * created in the init function (see &struct kunit_suite). |
| 190 | * |
| 191 | * Used to store information about the current context under which the test |
| 192 | * is running. Most of this data is private and should only be accessed |
| 193 | * indirectly via public functions; the one exception is @priv which can be |
| 194 | * used by the test writer to store arbitrary data. |
| 195 | */ |
| 196 | struct kunit { |
| 197 | void *priv; |
| 198 | |
| 199 | /* private: internal use only. */ |
| 200 | const char *name; /* Read only after initialization! */ |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 201 | char *log; /* Points at case log after initialization */ |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 202 | struct kunit_try_catch try_catch; |
Arpitha Raghunandan | fadb08e7 | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 203 | /* param_value is the current parameter value for a test case. */ |
| 204 | const void *param_value; |
| 205 | /* param_index stores the index of the parameter in parameterized tests. */ |
| 206 | int param_index; |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 207 | /* |
| 208 | * success starts as true, and may only be set to false during a |
| 209 | * test case; thus, it is safe to update this across multiple |
| 210 | * threads using WRITE_ONCE; however, as a consequence, it may only |
| 211 | * be read after the test case finishes once all threads associated |
| 212 | * with the test case have terminated. |
| 213 | */ |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 214 | spinlock_t lock; /* Guards all mutable test state. */ |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 215 | enum kunit_status status; /* Read only after test_case finishes! */ |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 216 | /* |
| 217 | * Because resources is a list that may be updated multiple times (with |
| 218 | * new resources) from any thread associated with a test case, we must |
| 219 | * protect it with some type of lock. |
| 220 | */ |
| 221 | struct list_head resources; /* Protected by lock. */ |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 222 | |
| 223 | char status_comment[KUNIT_STATUS_COMMENT_SIZE]; |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 224 | }; |
| 225 | |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 226 | static inline void kunit_set_failure(struct kunit *test) |
| 227 | { |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 228 | WRITE_ONCE(test->status, KUNIT_FAILURE); |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Joe Fradley | d20a6ba | 2022-08-23 07:24:54 -0700 | [diff] [blame] | 231 | bool kunit_enabled(void); |
| 232 | |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 233 | void kunit_init_test(struct kunit *test, const char *name, char *log); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 234 | |
| 235 | int kunit_run_tests(struct kunit_suite *suite); |
| 236 | |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 237 | size_t kunit_suite_num_test_cases(struct kunit_suite *suite); |
| 238 | |
| 239 | unsigned int kunit_test_case_num(struct kunit_suite *suite, |
| 240 | struct kunit_case *test_case); |
| 241 | |
Daniel Latypov | e5857d3 | 2022-07-09 11:19:58 +0800 | [diff] [blame] | 242 | int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites); |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 243 | |
Daniel Latypov | e5857d3 | 2022-07-09 11:19:58 +0800 | [diff] [blame] | 244 | void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites); |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 245 | |
Brendan Higgins | 8c0d884 | 2020-08-04 13:47:43 -0700 | [diff] [blame] | 246 | #if IS_BUILTIN(CONFIG_KUNIT) |
| 247 | int kunit_run_all_tests(void); |
| 248 | #else |
| 249 | static inline int kunit_run_all_tests(void) |
| 250 | { |
| 251 | return 0; |
| 252 | } |
| 253 | #endif /* IS_BUILTIN(CONFIG_KUNIT) */ |
| 254 | |
Daniel Latypov | e5857d3 | 2022-07-09 11:19:58 +0800 | [diff] [blame] | 255 | #define __kunit_test_suites(unique_array, ...) \ |
Daniel Latypov | e5857d3 | 2022-07-09 11:19:58 +0800 | [diff] [blame] | 256 | static struct kunit_suite *unique_array[] \ |
| 257 | __aligned(sizeof(struct kunit_suite *)) \ |
| 258 | __used __section(".kunit_test_suites") = { __VA_ARGS__ } |
Alan Maguire | aac3546 | 2020-08-04 13:47:42 -0700 | [diff] [blame] | 259 | |
| 260 | /** |
| 261 | * kunit_test_suites() - used to register one or more &struct kunit_suite |
| 262 | * with KUnit. |
| 263 | * |
Mauro Carvalho Chehab | 7f32b10 | 2020-10-21 14:17:26 +0200 | [diff] [blame] | 264 | * @__suites: a statically allocated list of &struct kunit_suite. |
Alan Maguire | aac3546 | 2020-08-04 13:47:42 -0700 | [diff] [blame] | 265 | * |
Jeremy Kerr | 3d6e446 | 2022-07-09 11:19:57 +0800 | [diff] [blame] | 266 | * Registers @suites with the test framework. |
| 267 | * This is done by placing the array of struct kunit_suite * in the |
| 268 | * .kunit_test_suites ELF section. |
Alan Maguire | aac3546 | 2020-08-04 13:47:42 -0700 | [diff] [blame] | 269 | * |
Jeremy Kerr | 3d6e446 | 2022-07-09 11:19:57 +0800 | [diff] [blame] | 270 | * When builtin, KUnit tests are all run via the executor at boot, and when |
| 271 | * built as a module, they run on module load. |
Alan Maguire | aac3546 | 2020-08-04 13:47:42 -0700 | [diff] [blame] | 272 | * |
| 273 | */ |
Mauro Carvalho Chehab | 7f32b10 | 2020-10-21 14:17:26 +0200 | [diff] [blame] | 274 | #define kunit_test_suites(__suites...) \ |
Alan Maguire | aac3546 | 2020-08-04 13:47:42 -0700 | [diff] [blame] | 275 | __kunit_test_suites(__UNIQUE_ID(array), \ |
Mauro Carvalho Chehab | 7f32b10 | 2020-10-21 14:17:26 +0200 | [diff] [blame] | 276 | ##__suites) |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 277 | |
| 278 | #define kunit_test_suite(suite) kunit_test_suites(&suite) |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 279 | |
Brendan Higgins | 9bf2eed | 2022-04-18 21:05:15 -0700 | [diff] [blame] | 280 | /** |
| 281 | * kunit_test_init_section_suites() - used to register one or more &struct |
| 282 | * kunit_suite containing init functions or |
| 283 | * init data. |
| 284 | * |
| 285 | * @__suites: a statically allocated list of &struct kunit_suite. |
| 286 | * |
Mauro Carvalho Chehab | 7b23794 | 2022-07-02 12:07:40 +0100 | [diff] [blame] | 287 | * This functions identically as kunit_test_suites() except that it suppresses |
Brendan Higgins | 9bf2eed | 2022-04-18 21:05:15 -0700 | [diff] [blame] | 288 | * modpost warnings for referencing functions marked __init or data marked |
| 289 | * __initdata; this is OK because currently KUnit only runs tests upon boot |
| 290 | * during the init phase or upon loading a module during the init phase. |
| 291 | * |
| 292 | * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these |
| 293 | * tests must be excluded. |
| 294 | * |
| 295 | * The only thing this macro does that's different from kunit_test_suites is |
| 296 | * that it suffixes the array and suite declarations it makes with _probe; |
| 297 | * modpost suppresses warnings about referencing init data for symbols named in |
| 298 | * this manner. |
| 299 | */ |
| 300 | #define kunit_test_init_section_suites(__suites...) \ |
| 301 | __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \ |
Brendan Higgins | 9bf2eed | 2022-04-18 21:05:15 -0700 | [diff] [blame] | 302 | ##__suites) |
| 303 | |
| 304 | #define kunit_test_init_section_suite(suite) \ |
| 305 | kunit_test_init_section_suites(&suite) |
| 306 | |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 307 | #define kunit_suite_for_each_test_case(suite, test_case) \ |
| 308 | for (test_case = suite->test_cases; test_case->run_case; test_case++) |
| 309 | |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 310 | enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite); |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 311 | |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 312 | /** |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 313 | * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*. |
| 314 | * @test: The test context object. |
| 315 | * @n: number of elements. |
| 316 | * @size: The size in bytes of the desired memory. |
| 317 | * @gfp: flags passed to underlying kmalloc(). |
| 318 | * |
| 319 | * Just like `kmalloc_array(...)`, except the allocation is managed by the test case |
| 320 | * and is automatically cleaned up after the test case concludes. See &struct |
| 321 | * kunit_resource for more information. |
| 322 | */ |
Daniel Latypov | 361b57d | 2021-10-05 13:46:32 -0700 | [diff] [blame] | 323 | void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp); |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 324 | |
| 325 | /** |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 326 | * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*. |
| 327 | * @test: The test context object. |
| 328 | * @size: The size in bytes of the desired memory. |
| 329 | * @gfp: flags passed to underlying kmalloc(). |
| 330 | * |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 331 | * See kmalloc() and kunit_kmalloc_array() for more information. |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 332 | */ |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 333 | static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) |
| 334 | { |
| 335 | return kunit_kmalloc_array(test, 1, size, gfp); |
| 336 | } |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 337 | |
| 338 | /** |
| 339 | * kunit_kfree() - Like kfree except for allocations managed by KUnit. |
| 340 | * @test: The test case to which the resource belongs. |
| 341 | * @ptr: The memory allocation to free. |
| 342 | */ |
| 343 | void kunit_kfree(struct kunit *test, const void *ptr); |
| 344 | |
| 345 | /** |
| 346 | * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation. |
| 347 | * @test: The test context object. |
| 348 | * @size: The size in bytes of the desired memory. |
| 349 | * @gfp: flags passed to underlying kmalloc(). |
| 350 | * |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 351 | * See kzalloc() and kunit_kmalloc_array() for more information. |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 352 | */ |
| 353 | static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp) |
| 354 | { |
| 355 | return kunit_kmalloc(test, size, gfp | __GFP_ZERO); |
| 356 | } |
| 357 | |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 358 | /** |
| 359 | * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation. |
| 360 | * @test: The test context object. |
| 361 | * @n: number of elements. |
| 362 | * @size: The size in bytes of the desired memory. |
| 363 | * @gfp: flags passed to underlying kmalloc(). |
| 364 | * |
| 365 | * See kcalloc() and kunit_kmalloc_array() for more information. |
| 366 | */ |
Daniel Latypov | 361b57d | 2021-10-05 13:46:32 -0700 | [diff] [blame] | 367 | static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp) |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 368 | { |
Daniel Latypov | 361b57d | 2021-10-05 13:46:32 -0700 | [diff] [blame] | 369 | return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO); |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 372 | void kunit_cleanup(struct kunit *test); |
| 373 | |
David Gow | 44acdbb | 2021-05-13 13:03:50 -0700 | [diff] [blame] | 374 | void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...); |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 375 | |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 376 | /** |
| 377 | * kunit_mark_skipped() - Marks @test_or_suite as skipped |
| 378 | * |
| 379 | * @test_or_suite: The test context object. |
| 380 | * @fmt: A printk() style format string. |
| 381 | * |
| 382 | * Marks the test as skipped. @fmt is given output as the test status |
| 383 | * comment, typically the reason the test was skipped. |
| 384 | * |
| 385 | * Test execution continues after kunit_mark_skipped() is called. |
| 386 | */ |
| 387 | #define kunit_mark_skipped(test_or_suite, fmt, ...) \ |
| 388 | do { \ |
| 389 | WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \ |
| 390 | scnprintf((test_or_suite)->status_comment, \ |
| 391 | KUNIT_STATUS_COMMENT_SIZE, \ |
| 392 | fmt, ##__VA_ARGS__); \ |
| 393 | } while (0) |
| 394 | |
| 395 | /** |
| 396 | * kunit_skip() - Marks @test_or_suite as skipped |
| 397 | * |
| 398 | * @test_or_suite: The test context object. |
| 399 | * @fmt: A printk() style format string. |
| 400 | * |
| 401 | * Skips the test. @fmt is given output as the test status |
| 402 | * comment, typically the reason the test was skipped. |
| 403 | * |
| 404 | * Test execution is halted after kunit_skip() is called. |
| 405 | */ |
| 406 | #define kunit_skip(test_or_suite, fmt, ...) \ |
| 407 | do { \ |
| 408 | kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\ |
| 409 | kunit_try_catch_throw(&((test_or_suite)->try_catch)); \ |
| 410 | } while (0) |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * printk and log to per-test or per-suite log buffer. Logging only done |
| 414 | * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used. |
| 415 | */ |
| 416 | #define kunit_log(lvl, test_or_suite, fmt, ...) \ |
| 417 | do { \ |
| 418 | printk(lvl fmt, ##__VA_ARGS__); \ |
| 419 | kunit_log_append((test_or_suite)->log, fmt "\n", \ |
| 420 | ##__VA_ARGS__); \ |
| 421 | } while (0) |
| 422 | |
| 423 | #define kunit_printk(lvl, test, fmt, ...) \ |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 424 | kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \ |
| 425 | (test)->name, ##__VA_ARGS__) |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 426 | |
| 427 | /** |
| 428 | * kunit_info() - Prints an INFO level message associated with @test. |
| 429 | * |
| 430 | * @test: The test context object. |
| 431 | * @fmt: A printk() style format string. |
| 432 | * |
| 433 | * Prints an info level message associated with the test suite being run. |
| 434 | * Takes a variable number of format parameters just like printk(). |
| 435 | */ |
| 436 | #define kunit_info(test, fmt, ...) \ |
| 437 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) |
| 438 | |
| 439 | /** |
| 440 | * kunit_warn() - Prints a WARN level message associated with @test. |
| 441 | * |
| 442 | * @test: The test context object. |
| 443 | * @fmt: A printk() style format string. |
| 444 | * |
| 445 | * Prints a warning level message. |
| 446 | */ |
| 447 | #define kunit_warn(test, fmt, ...) \ |
| 448 | kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__) |
| 449 | |
| 450 | /** |
| 451 | * kunit_err() - Prints an ERROR level message associated with @test. |
| 452 | * |
| 453 | * @test: The test context object. |
| 454 | * @fmt: A printk() style format string. |
| 455 | * |
| 456 | * Prints an error level message. |
| 457 | */ |
| 458 | #define kunit_err(test, fmt, ...) \ |
| 459 | kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) |
| 460 | |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 461 | /** |
| 462 | * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity. |
| 463 | * @test: The test context object. |
| 464 | * |
| 465 | * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other |
| 466 | * words, it does nothing and only exists for code clarity. See |
| 467 | * KUNIT_EXPECT_TRUE() for more information. |
| 468 | */ |
| 469 | #define KUNIT_SUCCEED(test) do {} while (0) |
| 470 | |
Daniel Latypov | 4fdacef | 2022-01-13 08:59:27 -0800 | [diff] [blame] | 471 | void kunit_do_failed_assertion(struct kunit *test, |
Daniel Latypov | 21957f9 | 2022-01-13 08:59:30 -0800 | [diff] [blame] | 472 | const struct kunit_loc *loc, |
| 473 | enum kunit_assert_type type, |
Miguel Ojeda | 7466886 | 2022-05-02 11:36:25 +0200 | [diff] [blame] | 474 | const struct kunit_assert *assert, |
Daniel Latypov | a8495ad | 2022-09-30 17:26:35 -0700 | [diff] [blame] | 475 | assert_format_t assert_format, |
Daniel Latypov | 4fdacef | 2022-01-13 08:59:27 -0800 | [diff] [blame] | 476 | const char *fmt, ...); |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 477 | |
Daniel Latypov | 97d453b | 2022-09-30 17:26:36 -0700 | [diff] [blame] | 478 | #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \ |
| 479 | static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \ |
Daniel Latypov | c1144e0 | 2022-09-30 17:26:38 -0700 | [diff] [blame] | 480 | const struct assert_class __assertion = INITIALIZER; \ |
Daniel Latypov | 97d453b | 2022-09-30 17:26:36 -0700 | [diff] [blame] | 481 | kunit_do_failed_assertion(test, \ |
| 482 | &__loc, \ |
| 483 | assert_type, \ |
| 484 | &__assertion.assert, \ |
| 485 | assert_format, \ |
| 486 | fmt, \ |
| 487 | ##__VA_ARGS__); \ |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 488 | } while (0) |
| 489 | |
| 490 | |
| 491 | #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \ |
Daniel Latypov | 97d453b | 2022-09-30 17:26:36 -0700 | [diff] [blame] | 492 | _KUNIT_FAILED(test, \ |
| 493 | assert_type, \ |
| 494 | kunit_fail_assert, \ |
| 495 | kunit_fail_assert_format, \ |
| 496 | {}, \ |
| 497 | fmt, \ |
| 498 | ##__VA_ARGS__) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 499 | |
| 500 | /** |
| 501 | * KUNIT_FAIL() - Always causes a test to fail when evaluated. |
| 502 | * @test: The test context object. |
| 503 | * @fmt: an informational message to be printed when the assertion is made. |
| 504 | * @...: string format arguments. |
| 505 | * |
| 506 | * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In |
| 507 | * other words, it always results in a failed expectation, and consequently |
| 508 | * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE() |
| 509 | * for more information. |
| 510 | */ |
| 511 | #define KUNIT_FAIL(test, fmt, ...) \ |
| 512 | KUNIT_FAIL_ASSERTION(test, \ |
| 513 | KUNIT_EXPECTATION, \ |
| 514 | fmt, \ |
| 515 | ##__VA_ARGS__) |
| 516 | |
| 517 | #define KUNIT_UNARY_ASSERTION(test, \ |
| 518 | assert_type, \ |
| 519 | condition, \ |
| 520 | expected_true, \ |
| 521 | fmt, \ |
| 522 | ...) \ |
Daniel Latypov | 97d453b | 2022-09-30 17:26:36 -0700 | [diff] [blame] | 523 | do { \ |
| 524 | if (likely(!!(condition) == !!expected_true)) \ |
| 525 | break; \ |
| 526 | \ |
| 527 | _KUNIT_FAILED(test, \ |
| 528 | assert_type, \ |
| 529 | kunit_unary_assert, \ |
| 530 | kunit_unary_assert_format, \ |
| 531 | KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \ |
| 532 | expected_true), \ |
| 533 | fmt, \ |
| 534 | ##__VA_ARGS__); \ |
| 535 | } while (0) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 536 | |
| 537 | #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \ |
| 538 | KUNIT_UNARY_ASSERTION(test, \ |
| 539 | assert_type, \ |
| 540 | condition, \ |
| 541 | true, \ |
| 542 | fmt, \ |
| 543 | ##__VA_ARGS__) |
| 544 | |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 545 | #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \ |
| 546 | KUNIT_UNARY_ASSERTION(test, \ |
| 547 | assert_type, \ |
| 548 | condition, \ |
| 549 | false, \ |
| 550 | fmt, \ |
| 551 | ##__VA_ARGS__) |
| 552 | |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 553 | /* |
| 554 | * A factory macro for defining the assertions and expectations for the basic |
| 555 | * comparisons defined for the built in types. |
| 556 | * |
| 557 | * Unfortunately, there is no common type that all types can be promoted to for |
| 558 | * which all the binary operators behave the same way as for the actual types |
| 559 | * (for example, there is no type that long long and unsigned long long can |
| 560 | * both be cast to where the comparison result is preserved for all values). So |
| 561 | * the best we can do is do the comparison in the original types and then coerce |
| 562 | * everything to long long for printing; this way, the comparison behaves |
| 563 | * correctly and the printed out value usually makes sense without |
| 564 | * interpretation, but can always be interpreted to figure out the actual |
| 565 | * value. |
| 566 | */ |
| 567 | #define KUNIT_BASE_BINARY_ASSERTION(test, \ |
| 568 | assert_class, \ |
Daniel Latypov | 064ff29 | 2022-01-25 13:00:10 -0800 | [diff] [blame] | 569 | format_func, \ |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 570 | assert_type, \ |
| 571 | left, \ |
| 572 | op, \ |
| 573 | right, \ |
| 574 | fmt, \ |
| 575 | ...) \ |
| 576 | do { \ |
Daniel Latypov | c274145 | 2022-01-27 13:52:22 -0800 | [diff] [blame] | 577 | const typeof(left) __left = (left); \ |
| 578 | const typeof(right) __right = (right); \ |
Daniel Latypov | 2b6861e | 2022-01-25 13:00:11 -0800 | [diff] [blame] | 579 | static const struct kunit_binary_assert_text __text = { \ |
| 580 | .operation = #op, \ |
| 581 | .left_text = #left, \ |
| 582 | .right_text = #right, \ |
| 583 | }; \ |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 584 | \ |
Daniel Latypov | 97d453b | 2022-09-30 17:26:36 -0700 | [diff] [blame] | 585 | if (likely(__left op __right)) \ |
| 586 | break; \ |
| 587 | \ |
| 588 | _KUNIT_FAILED(test, \ |
| 589 | assert_type, \ |
| 590 | assert_class, \ |
| 591 | format_func, \ |
| 592 | KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ |
| 593 | __left, \ |
| 594 | __right), \ |
| 595 | fmt, \ |
| 596 | ##__VA_ARGS__); \ |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 597 | } while (0) |
| 598 | |
Daniel Latypov | 40f39777 | 2022-01-18 14:35:05 -0800 | [diff] [blame] | 599 | #define KUNIT_BINARY_INT_ASSERTION(test, \ |
| 600 | assert_type, \ |
| 601 | left, \ |
| 602 | op, \ |
| 603 | right, \ |
| 604 | fmt, \ |
| 605 | ...) \ |
| 606 | KUNIT_BASE_BINARY_ASSERTION(test, \ |
| 607 | kunit_binary_assert, \ |
Daniel Latypov | 064ff29 | 2022-01-25 13:00:10 -0800 | [diff] [blame] | 608 | kunit_binary_assert_format, \ |
Daniel Latypov | 40f39777 | 2022-01-18 14:35:05 -0800 | [diff] [blame] | 609 | assert_type, \ |
| 610 | left, op, right, \ |
| 611 | fmt, \ |
| 612 | ##__VA_ARGS__) |
| 613 | |
Daniel Latypov | 6125a5c | 2022-01-18 14:35:06 -0800 | [diff] [blame] | 614 | #define KUNIT_BINARY_PTR_ASSERTION(test, \ |
| 615 | assert_type, \ |
| 616 | left, \ |
| 617 | op, \ |
| 618 | right, \ |
| 619 | fmt, \ |
| 620 | ...) \ |
| 621 | KUNIT_BASE_BINARY_ASSERTION(test, \ |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 622 | kunit_binary_ptr_assert, \ |
Daniel Latypov | 064ff29 | 2022-01-25 13:00:10 -0800 | [diff] [blame] | 623 | kunit_binary_ptr_assert_format, \ |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 624 | assert_type, \ |
Daniel Latypov | 6125a5c | 2022-01-18 14:35:06 -0800 | [diff] [blame] | 625 | left, op, right, \ |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 626 | fmt, \ |
| 627 | ##__VA_ARGS__) |
| 628 | |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 629 | #define KUNIT_BINARY_STR_ASSERTION(test, \ |
| 630 | assert_type, \ |
| 631 | left, \ |
| 632 | op, \ |
| 633 | right, \ |
| 634 | fmt, \ |
| 635 | ...) \ |
| 636 | do { \ |
David Gow | 3747b5c | 2021-05-13 12:31:56 -0700 | [diff] [blame] | 637 | const char *__left = (left); \ |
Daniel Latypov | 2b6861e | 2022-01-25 13:00:11 -0800 | [diff] [blame] | 638 | const char *__right = (right); \ |
| 639 | static const struct kunit_binary_assert_text __text = { \ |
| 640 | .operation = #op, \ |
| 641 | .left_text = #left, \ |
| 642 | .right_text = #right, \ |
| 643 | }; \ |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 644 | \ |
Daniel Latypov | 97d453b | 2022-09-30 17:26:36 -0700 | [diff] [blame] | 645 | if (likely(strcmp(__left, __right) op 0)) \ |
| 646 | break; \ |
| 647 | \ |
| 648 | \ |
| 649 | _KUNIT_FAILED(test, \ |
| 650 | assert_type, \ |
| 651 | kunit_binary_str_assert, \ |
| 652 | kunit_binary_str_assert_format, \ |
| 653 | KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ |
| 654 | __left, \ |
| 655 | __right), \ |
| 656 | fmt, \ |
| 657 | ##__VA_ARGS__); \ |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 658 | } while (0) |
| 659 | |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 660 | #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ |
| 661 | assert_type, \ |
| 662 | ptr, \ |
| 663 | fmt, \ |
| 664 | ...) \ |
| 665 | do { \ |
Daniel Latypov | c274145 | 2022-01-27 13:52:22 -0800 | [diff] [blame] | 666 | const typeof(ptr) __ptr = (ptr); \ |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 667 | \ |
Daniel Latypov | 97d453b | 2022-09-30 17:26:36 -0700 | [diff] [blame] | 668 | if (!IS_ERR_OR_NULL(__ptr)) \ |
| 669 | break; \ |
| 670 | \ |
| 671 | _KUNIT_FAILED(test, \ |
| 672 | assert_type, \ |
| 673 | kunit_ptr_not_err_assert, \ |
| 674 | kunit_ptr_not_err_assert_format, \ |
| 675 | KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr), \ |
| 676 | fmt, \ |
| 677 | ##__VA_ARGS__); \ |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 678 | } while (0) |
| 679 | |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 680 | /** |
| 681 | * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true. |
| 682 | * @test: The test context object. |
| 683 | * @condition: an arbitrary boolean expression. The test fails when this does |
| 684 | * not evaluate to true. |
| 685 | * |
| 686 | * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case |
| 687 | * to fail when the specified condition is not met; however, it will not prevent |
| 688 | * the test case from continuing to run; this is otherwise known as an |
| 689 | * *expectation failure*. |
| 690 | */ |
| 691 | #define KUNIT_EXPECT_TRUE(test, condition) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 692 | KUNIT_EXPECT_TRUE_MSG(test, condition, NULL) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 693 | |
| 694 | #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \ |
| 695 | KUNIT_TRUE_MSG_ASSERTION(test, \ |
| 696 | KUNIT_EXPECTATION, \ |
| 697 | condition, \ |
| 698 | fmt, \ |
| 699 | ##__VA_ARGS__) |
| 700 | |
| 701 | /** |
| 702 | * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false. |
| 703 | * @test: The test context object. |
| 704 | * @condition: an arbitrary boolean expression. The test fails when this does |
| 705 | * not evaluate to false. |
| 706 | * |
| 707 | * Sets an expectation that @condition evaluates to false. See |
| 708 | * KUNIT_EXPECT_TRUE() for more information. |
| 709 | */ |
| 710 | #define KUNIT_EXPECT_FALSE(test, condition) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 711 | KUNIT_EXPECT_FALSE_MSG(test, condition, NULL) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 712 | |
| 713 | #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \ |
| 714 | KUNIT_FALSE_MSG_ASSERTION(test, \ |
| 715 | KUNIT_EXPECTATION, \ |
| 716 | condition, \ |
| 717 | fmt, \ |
| 718 | ##__VA_ARGS__) |
| 719 | |
| 720 | /** |
| 721 | * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal. |
| 722 | * @test: The test context object. |
| 723 | * @left: an arbitrary expression that evaluates to a primitive C type. |
| 724 | * @right: an arbitrary expression that evaluates to a primitive C type. |
| 725 | * |
| 726 | * Sets an expectation that the values that @left and @right evaluate to are |
| 727 | * equal. This is semantically equivalent to |
| 728 | * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for |
| 729 | * more information. |
| 730 | */ |
| 731 | #define KUNIT_EXPECT_EQ(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 732 | KUNIT_EXPECT_EQ_MSG(test, left, right, NULL) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 733 | |
| 734 | #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 6125a5c | 2022-01-18 14:35:06 -0800 | [diff] [blame] | 735 | KUNIT_BINARY_INT_ASSERTION(test, \ |
| 736 | KUNIT_EXPECTATION, \ |
| 737 | left, ==, right, \ |
| 738 | fmt, \ |
| 739 | ##__VA_ARGS__) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 740 | |
| 741 | /** |
| 742 | * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal. |
| 743 | * @test: The test context object. |
| 744 | * @left: an arbitrary expression that evaluates to a pointer. |
| 745 | * @right: an arbitrary expression that evaluates to a pointer. |
| 746 | * |
| 747 | * Sets an expectation that the values that @left and @right evaluate to are |
| 748 | * equal. This is semantically equivalent to |
| 749 | * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for |
| 750 | * more information. |
| 751 | */ |
| 752 | #define KUNIT_EXPECT_PTR_EQ(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 753 | KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 754 | |
| 755 | #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 6125a5c | 2022-01-18 14:35:06 -0800 | [diff] [blame] | 756 | KUNIT_BINARY_PTR_ASSERTION(test, \ |
| 757 | KUNIT_EXPECTATION, \ |
| 758 | left, ==, right, \ |
| 759 | fmt, \ |
| 760 | ##__VA_ARGS__) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 761 | |
| 762 | /** |
| 763 | * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal. |
| 764 | * @test: The test context object. |
| 765 | * @left: an arbitrary expression that evaluates to a primitive C type. |
| 766 | * @right: an arbitrary expression that evaluates to a primitive C type. |
| 767 | * |
| 768 | * Sets an expectation that the values that @left and @right evaluate to are not |
| 769 | * equal. This is semantically equivalent to |
| 770 | * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for |
| 771 | * more information. |
| 772 | */ |
| 773 | #define KUNIT_EXPECT_NE(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 774 | KUNIT_EXPECT_NE_MSG(test, left, right, NULL) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 775 | |
| 776 | #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 6125a5c | 2022-01-18 14:35:06 -0800 | [diff] [blame] | 777 | KUNIT_BINARY_INT_ASSERTION(test, \ |
| 778 | KUNIT_EXPECTATION, \ |
| 779 | left, !=, right, \ |
| 780 | fmt, \ |
| 781 | ##__VA_ARGS__) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 782 | |
| 783 | /** |
| 784 | * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal. |
| 785 | * @test: The test context object. |
| 786 | * @left: an arbitrary expression that evaluates to a pointer. |
| 787 | * @right: an arbitrary expression that evaluates to a pointer. |
| 788 | * |
| 789 | * Sets an expectation that the values that @left and @right evaluate to are not |
| 790 | * equal. This is semantically equivalent to |
| 791 | * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for |
| 792 | * more information. |
| 793 | */ |
| 794 | #define KUNIT_EXPECT_PTR_NE(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 795 | KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 796 | |
| 797 | #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 6125a5c | 2022-01-18 14:35:06 -0800 | [diff] [blame] | 798 | KUNIT_BINARY_PTR_ASSERTION(test, \ |
| 799 | KUNIT_EXPECTATION, \ |
| 800 | left, !=, right, \ |
| 801 | fmt, \ |
| 802 | ##__VA_ARGS__) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 803 | |
| 804 | /** |
| 805 | * KUNIT_EXPECT_LT() - An expectation that @left is less than @right. |
| 806 | * @test: The test context object. |
| 807 | * @left: an arbitrary expression that evaluates to a primitive C type. |
| 808 | * @right: an arbitrary expression that evaluates to a primitive C type. |
| 809 | * |
| 810 | * Sets an expectation that the value that @left evaluates to is less than the |
| 811 | * value that @right evaluates to. This is semantically equivalent to |
| 812 | * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for |
| 813 | * more information. |
| 814 | */ |
| 815 | #define KUNIT_EXPECT_LT(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 816 | KUNIT_EXPECT_LT_MSG(test, left, right, NULL) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 817 | |
| 818 | #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 40f39777 | 2022-01-18 14:35:05 -0800 | [diff] [blame] | 819 | KUNIT_BINARY_INT_ASSERTION(test, \ |
| 820 | KUNIT_EXPECTATION, \ |
| 821 | left, <, right, \ |
| 822 | fmt, \ |
| 823 | ##__VA_ARGS__) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 824 | |
| 825 | /** |
| 826 | * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right. |
| 827 | * @test: The test context object. |
| 828 | * @left: an arbitrary expression that evaluates to a primitive C type. |
| 829 | * @right: an arbitrary expression that evaluates to a primitive C type. |
| 830 | * |
| 831 | * Sets an expectation that the value that @left evaluates to is less than or |
| 832 | * equal to the value that @right evaluates to. Semantically this is equivalent |
| 833 | * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for |
| 834 | * more information. |
| 835 | */ |
| 836 | #define KUNIT_EXPECT_LE(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 837 | KUNIT_EXPECT_LE_MSG(test, left, right, NULL) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 838 | |
| 839 | #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 40f39777 | 2022-01-18 14:35:05 -0800 | [diff] [blame] | 840 | KUNIT_BINARY_INT_ASSERTION(test, \ |
Sander Vanheule | aded3ca | 2022-08-21 17:01:47 +0200 | [diff] [blame] | 841 | KUNIT_EXPECTATION, \ |
Daniel Latypov | 40f39777 | 2022-01-18 14:35:05 -0800 | [diff] [blame] | 842 | left, <=, right, \ |
| 843 | fmt, \ |
| 844 | ##__VA_ARGS__) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 845 | |
| 846 | /** |
| 847 | * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right. |
| 848 | * @test: The test context object. |
| 849 | * @left: an arbitrary expression that evaluates to a primitive C type. |
| 850 | * @right: an arbitrary expression that evaluates to a primitive C type. |
| 851 | * |
| 852 | * Sets an expectation that the value that @left evaluates to is greater than |
| 853 | * the value that @right evaluates to. This is semantically equivalent to |
| 854 | * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for |
| 855 | * more information. |
| 856 | */ |
| 857 | #define KUNIT_EXPECT_GT(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 858 | KUNIT_EXPECT_GT_MSG(test, left, right, NULL) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 859 | |
| 860 | #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 40f39777 | 2022-01-18 14:35:05 -0800 | [diff] [blame] | 861 | KUNIT_BINARY_INT_ASSERTION(test, \ |
| 862 | KUNIT_EXPECTATION, \ |
| 863 | left, >, right, \ |
| 864 | fmt, \ |
| 865 | ##__VA_ARGS__) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 866 | |
| 867 | /** |
| 868 | * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right. |
| 869 | * @test: The test context object. |
| 870 | * @left: an arbitrary expression that evaluates to a primitive C type. |
| 871 | * @right: an arbitrary expression that evaluates to a primitive C type. |
| 872 | * |
| 873 | * Sets an expectation that the value that @left evaluates to is greater than |
| 874 | * the value that @right evaluates to. This is semantically equivalent to |
| 875 | * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for |
| 876 | * more information. |
| 877 | */ |
| 878 | #define KUNIT_EXPECT_GE(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 879 | KUNIT_EXPECT_GE_MSG(test, left, right, NULL) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 880 | |
| 881 | #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 40f39777 | 2022-01-18 14:35:05 -0800 | [diff] [blame] | 882 | KUNIT_BINARY_INT_ASSERTION(test, \ |
| 883 | KUNIT_EXPECTATION, \ |
| 884 | left, >=, right, \ |
| 885 | fmt, \ |
| 886 | ##__VA_ARGS__) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 887 | |
| 888 | /** |
| 889 | * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal. |
| 890 | * @test: The test context object. |
| 891 | * @left: an arbitrary expression that evaluates to a null terminated string. |
| 892 | * @right: an arbitrary expression that evaluates to a null terminated string. |
| 893 | * |
| 894 | * Sets an expectation that the values that @left and @right evaluate to are |
| 895 | * equal. This is semantically equivalent to |
| 896 | * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE() |
| 897 | * for more information. |
| 898 | */ |
| 899 | #define KUNIT_EXPECT_STREQ(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 900 | KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 901 | |
| 902 | #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 955df7d | 2022-01-18 14:35:04 -0800 | [diff] [blame] | 903 | KUNIT_BINARY_STR_ASSERTION(test, \ |
| 904 | KUNIT_EXPECTATION, \ |
| 905 | left, ==, right, \ |
| 906 | fmt, \ |
| 907 | ##__VA_ARGS__) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 908 | |
| 909 | /** |
| 910 | * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal. |
| 911 | * @test: The test context object. |
| 912 | * @left: an arbitrary expression that evaluates to a null terminated string. |
| 913 | * @right: an arbitrary expression that evaluates to a null terminated string. |
| 914 | * |
| 915 | * Sets an expectation that the values that @left and @right evaluate to are |
| 916 | * not equal. This is semantically equivalent to |
| 917 | * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE() |
| 918 | * for more information. |
| 919 | */ |
| 920 | #define KUNIT_EXPECT_STRNEQ(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 921 | KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 922 | |
| 923 | #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 955df7d | 2022-01-18 14:35:04 -0800 | [diff] [blame] | 924 | KUNIT_BINARY_STR_ASSERTION(test, \ |
| 925 | KUNIT_EXPECTATION, \ |
| 926 | left, !=, right, \ |
| 927 | fmt, \ |
| 928 | ##__VA_ARGS__) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 929 | |
| 930 | /** |
Ricardo Ribalda | caae945 | 2022-02-11 17:42:41 +0100 | [diff] [blame] | 931 | * KUNIT_EXPECT_NULL() - Expects that @ptr is null. |
| 932 | * @test: The test context object. |
| 933 | * @ptr: an arbitrary pointer. |
| 934 | * |
| 935 | * Sets an expectation that the value that @ptr evaluates to is null. This is |
| 936 | * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL). |
| 937 | * See KUNIT_EXPECT_TRUE() for more information. |
| 938 | */ |
| 939 | #define KUNIT_EXPECT_NULL(test, ptr) \ |
| 940 | KUNIT_EXPECT_NULL_MSG(test, \ |
| 941 | ptr, \ |
| 942 | NULL) |
| 943 | |
| 944 | #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \ |
| 945 | KUNIT_BINARY_PTR_ASSERTION(test, \ |
| 946 | KUNIT_EXPECTATION, \ |
| 947 | ptr, ==, NULL, \ |
| 948 | fmt, \ |
| 949 | ##__VA_ARGS__) |
| 950 | |
| 951 | /** |
| 952 | * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null. |
| 953 | * @test: The test context object. |
| 954 | * @ptr: an arbitrary pointer. |
| 955 | * |
| 956 | * Sets an expectation that the value that @ptr evaluates to is not null. This |
| 957 | * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL). |
| 958 | * See KUNIT_EXPECT_TRUE() for more information. |
| 959 | */ |
| 960 | #define KUNIT_EXPECT_NOT_NULL(test, ptr) \ |
| 961 | KUNIT_EXPECT_NOT_NULL_MSG(test, \ |
| 962 | ptr, \ |
| 963 | NULL) |
| 964 | |
| 965 | #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \ |
| 966 | KUNIT_BINARY_PTR_ASSERTION(test, \ |
| 967 | KUNIT_EXPECTATION, \ |
| 968 | ptr, !=, NULL, \ |
| 969 | fmt, \ |
| 970 | ##__VA_ARGS__) |
| 971 | |
| 972 | /** |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 973 | * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err. |
| 974 | * @test: The test context object. |
| 975 | * @ptr: an arbitrary pointer. |
| 976 | * |
| 977 | * Sets an expectation that the value that @ptr evaluates to is not null and not |
| 978 | * an errno stored in a pointer. This is semantically equivalent to |
| 979 | * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for |
| 980 | * more information. |
| 981 | */ |
| 982 | #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 983 | KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL) |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 984 | |
| 985 | #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \ |
| 986 | KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ |
| 987 | KUNIT_EXPECTATION, \ |
| 988 | ptr, \ |
| 989 | fmt, \ |
| 990 | ##__VA_ARGS__) |
| 991 | |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 992 | #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \ |
| 993 | KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__) |
| 994 | |
| 995 | /** |
| 996 | * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true. |
| 997 | * @test: The test context object. |
| 998 | * @condition: an arbitrary boolean expression. The test fails and aborts when |
| 999 | * this does not evaluate to true. |
| 1000 | * |
| 1001 | * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to |
| 1002 | * fail *and immediately abort* when the specified condition is not met. Unlike |
| 1003 | * an expectation failure, it will prevent the test case from continuing to run; |
| 1004 | * this is otherwise known as an *assertion failure*. |
| 1005 | */ |
| 1006 | #define KUNIT_ASSERT_TRUE(test, condition) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 1007 | KUNIT_ASSERT_TRUE_MSG(test, condition, NULL) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1008 | |
| 1009 | #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \ |
| 1010 | KUNIT_TRUE_MSG_ASSERTION(test, \ |
| 1011 | KUNIT_ASSERTION, \ |
| 1012 | condition, \ |
| 1013 | fmt, \ |
| 1014 | ##__VA_ARGS__) |
| 1015 | |
| 1016 | /** |
| 1017 | * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false. |
| 1018 | * @test: The test context object. |
| 1019 | * @condition: an arbitrary boolean expression. |
| 1020 | * |
| 1021 | * Sets an assertion that the value that @condition evaluates to is false. This |
| 1022 | * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure |
| 1023 | * (see KUNIT_ASSERT_TRUE()) when the assertion is not met. |
| 1024 | */ |
| 1025 | #define KUNIT_ASSERT_FALSE(test, condition) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 1026 | KUNIT_ASSERT_FALSE_MSG(test, condition, NULL) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1027 | |
| 1028 | #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \ |
| 1029 | KUNIT_FALSE_MSG_ASSERTION(test, \ |
| 1030 | KUNIT_ASSERTION, \ |
| 1031 | condition, \ |
| 1032 | fmt, \ |
| 1033 | ##__VA_ARGS__) |
| 1034 | |
| 1035 | /** |
| 1036 | * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal. |
| 1037 | * @test: The test context object. |
| 1038 | * @left: an arbitrary expression that evaluates to a primitive C type. |
| 1039 | * @right: an arbitrary expression that evaluates to a primitive C type. |
| 1040 | * |
| 1041 | * Sets an assertion that the values that @left and @right evaluate to are |
| 1042 | * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion |
| 1043 | * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. |
| 1044 | */ |
| 1045 | #define KUNIT_ASSERT_EQ(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 1046 | KUNIT_ASSERT_EQ_MSG(test, left, right, NULL) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1047 | |
| 1048 | #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 6125a5c | 2022-01-18 14:35:06 -0800 | [diff] [blame] | 1049 | KUNIT_BINARY_INT_ASSERTION(test, \ |
| 1050 | KUNIT_ASSERTION, \ |
| 1051 | left, ==, right, \ |
| 1052 | fmt, \ |
| 1053 | ##__VA_ARGS__) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1054 | |
| 1055 | /** |
| 1056 | * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal. |
| 1057 | * @test: The test context object. |
| 1058 | * @left: an arbitrary expression that evaluates to a pointer. |
| 1059 | * @right: an arbitrary expression that evaluates to a pointer. |
| 1060 | * |
| 1061 | * Sets an assertion that the values that @left and @right evaluate to are |
| 1062 | * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion |
| 1063 | * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. |
| 1064 | */ |
| 1065 | #define KUNIT_ASSERT_PTR_EQ(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 1066 | KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1067 | |
| 1068 | #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 6125a5c | 2022-01-18 14:35:06 -0800 | [diff] [blame] | 1069 | KUNIT_BINARY_PTR_ASSERTION(test, \ |
| 1070 | KUNIT_ASSERTION, \ |
| 1071 | left, ==, right, \ |
| 1072 | fmt, \ |
| 1073 | ##__VA_ARGS__) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1074 | |
| 1075 | /** |
| 1076 | * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal. |
| 1077 | * @test: The test context object. |
| 1078 | * @left: an arbitrary expression that evaluates to a primitive C type. |
| 1079 | * @right: an arbitrary expression that evaluates to a primitive C type. |
| 1080 | * |
| 1081 | * Sets an assertion that the values that @left and @right evaluate to are not |
| 1082 | * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion |
| 1083 | * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. |
| 1084 | */ |
| 1085 | #define KUNIT_ASSERT_NE(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 1086 | KUNIT_ASSERT_NE_MSG(test, left, right, NULL) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1087 | |
| 1088 | #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 6125a5c | 2022-01-18 14:35:06 -0800 | [diff] [blame] | 1089 | KUNIT_BINARY_INT_ASSERTION(test, \ |
| 1090 | KUNIT_ASSERTION, \ |
| 1091 | left, !=, right, \ |
| 1092 | fmt, \ |
| 1093 | ##__VA_ARGS__) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1094 | |
| 1095 | /** |
| 1096 | * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal. |
| 1097 | * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal. |
| 1098 | * @test: The test context object. |
| 1099 | * @left: an arbitrary expression that evaluates to a pointer. |
| 1100 | * @right: an arbitrary expression that evaluates to a pointer. |
| 1101 | * |
| 1102 | * Sets an assertion that the values that @left and @right evaluate to are not |
| 1103 | * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion |
| 1104 | * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. |
| 1105 | */ |
| 1106 | #define KUNIT_ASSERT_PTR_NE(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 1107 | KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1108 | |
| 1109 | #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 6125a5c | 2022-01-18 14:35:06 -0800 | [diff] [blame] | 1110 | KUNIT_BINARY_PTR_ASSERTION(test, \ |
| 1111 | KUNIT_ASSERTION, \ |
| 1112 | left, !=, right, \ |
| 1113 | fmt, \ |
| 1114 | ##__VA_ARGS__) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1115 | /** |
| 1116 | * KUNIT_ASSERT_LT() - An assertion that @left is less than @right. |
| 1117 | * @test: The test context object. |
| 1118 | * @left: an arbitrary expression that evaluates to a primitive C type. |
| 1119 | * @right: an arbitrary expression that evaluates to a primitive C type. |
| 1120 | * |
| 1121 | * Sets an assertion that the value that @left evaluates to is less than the |
| 1122 | * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except |
| 1123 | * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion |
| 1124 | * is not met. |
| 1125 | */ |
| 1126 | #define KUNIT_ASSERT_LT(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 1127 | KUNIT_ASSERT_LT_MSG(test, left, right, NULL) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1128 | |
| 1129 | #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 40f39777 | 2022-01-18 14:35:05 -0800 | [diff] [blame] | 1130 | KUNIT_BINARY_INT_ASSERTION(test, \ |
Sander Vanheule | aded3ca | 2022-08-21 17:01:47 +0200 | [diff] [blame] | 1131 | KUNIT_ASSERTION, \ |
Daniel Latypov | 40f39777 | 2022-01-18 14:35:05 -0800 | [diff] [blame] | 1132 | left, <, right, \ |
| 1133 | fmt, \ |
| 1134 | ##__VA_ARGS__) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1135 | /** |
| 1136 | * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right. |
| 1137 | * @test: The test context object. |
| 1138 | * @left: an arbitrary expression that evaluates to a primitive C type. |
| 1139 | * @right: an arbitrary expression that evaluates to a primitive C type. |
| 1140 | * |
| 1141 | * Sets an assertion that the value that @left evaluates to is less than or |
| 1142 | * equal to the value that @right evaluates to. This is the same as |
| 1143 | * KUNIT_EXPECT_LE(), except it causes an assertion failure (see |
| 1144 | * KUNIT_ASSERT_TRUE()) when the assertion is not met. |
| 1145 | */ |
| 1146 | #define KUNIT_ASSERT_LE(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 1147 | KUNIT_ASSERT_LE_MSG(test, left, right, NULL) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1148 | |
| 1149 | #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 40f39777 | 2022-01-18 14:35:05 -0800 | [diff] [blame] | 1150 | KUNIT_BINARY_INT_ASSERTION(test, \ |
| 1151 | KUNIT_ASSERTION, \ |
| 1152 | left, <=, right, \ |
| 1153 | fmt, \ |
| 1154 | ##__VA_ARGS__) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1155 | |
| 1156 | /** |
| 1157 | * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right. |
| 1158 | * @test: The test context object. |
| 1159 | * @left: an arbitrary expression that evaluates to a primitive C type. |
| 1160 | * @right: an arbitrary expression that evaluates to a primitive C type. |
| 1161 | * |
| 1162 | * Sets an assertion that the value that @left evaluates to is greater than the |
| 1163 | * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except |
| 1164 | * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion |
| 1165 | * is not met. |
| 1166 | */ |
| 1167 | #define KUNIT_ASSERT_GT(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 1168 | KUNIT_ASSERT_GT_MSG(test, left, right, NULL) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1169 | |
| 1170 | #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 40f39777 | 2022-01-18 14:35:05 -0800 | [diff] [blame] | 1171 | KUNIT_BINARY_INT_ASSERTION(test, \ |
Sander Vanheule | aded3ca | 2022-08-21 17:01:47 +0200 | [diff] [blame] | 1172 | KUNIT_ASSERTION, \ |
Daniel Latypov | 40f39777 | 2022-01-18 14:35:05 -0800 | [diff] [blame] | 1173 | left, >, right, \ |
| 1174 | fmt, \ |
| 1175 | ##__VA_ARGS__) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1176 | |
| 1177 | /** |
| 1178 | * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right. |
| 1179 | * @test: The test context object. |
| 1180 | * @left: an arbitrary expression that evaluates to a primitive C type. |
| 1181 | * @right: an arbitrary expression that evaluates to a primitive C type. |
| 1182 | * |
| 1183 | * Sets an assertion that the value that @left evaluates to is greater than the |
| 1184 | * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except |
| 1185 | * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion |
| 1186 | * is not met. |
| 1187 | */ |
| 1188 | #define KUNIT_ASSERT_GE(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 1189 | KUNIT_ASSERT_GE_MSG(test, left, right, NULL) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1190 | |
| 1191 | #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 40f39777 | 2022-01-18 14:35:05 -0800 | [diff] [blame] | 1192 | KUNIT_BINARY_INT_ASSERTION(test, \ |
| 1193 | KUNIT_ASSERTION, \ |
| 1194 | left, >=, right, \ |
| 1195 | fmt, \ |
| 1196 | ##__VA_ARGS__) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1197 | |
| 1198 | /** |
| 1199 | * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal. |
| 1200 | * @test: The test context object. |
| 1201 | * @left: an arbitrary expression that evaluates to a null terminated string. |
| 1202 | * @right: an arbitrary expression that evaluates to a null terminated string. |
| 1203 | * |
| 1204 | * Sets an assertion that the values that @left and @right evaluate to are |
| 1205 | * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an |
| 1206 | * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. |
| 1207 | */ |
| 1208 | #define KUNIT_ASSERT_STREQ(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 1209 | KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1210 | |
| 1211 | #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 955df7d | 2022-01-18 14:35:04 -0800 | [diff] [blame] | 1212 | KUNIT_BINARY_STR_ASSERTION(test, \ |
| 1213 | KUNIT_ASSERTION, \ |
| 1214 | left, ==, right, \ |
| 1215 | fmt, \ |
| 1216 | ##__VA_ARGS__) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1217 | |
| 1218 | /** |
| 1219 | * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal. |
| 1220 | * @test: The test context object. |
| 1221 | * @left: an arbitrary expression that evaluates to a null terminated string. |
| 1222 | * @right: an arbitrary expression that evaluates to a null terminated string. |
| 1223 | * |
| 1224 | * Sets an expectation that the values that @left and @right evaluate to are |
| 1225 | * not equal. This is semantically equivalent to |
| 1226 | * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE() |
| 1227 | * for more information. |
| 1228 | */ |
| 1229 | #define KUNIT_ASSERT_STRNEQ(test, left, right) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 1230 | KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1231 | |
| 1232 | #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \ |
Daniel Latypov | 955df7d | 2022-01-18 14:35:04 -0800 | [diff] [blame] | 1233 | KUNIT_BINARY_STR_ASSERTION(test, \ |
| 1234 | KUNIT_ASSERTION, \ |
| 1235 | left, !=, right, \ |
| 1236 | fmt, \ |
| 1237 | ##__VA_ARGS__) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1238 | |
| 1239 | /** |
Ricardo Ribalda | caae945 | 2022-02-11 17:42:41 +0100 | [diff] [blame] | 1240 | * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null. |
| 1241 | * @test: The test context object. |
| 1242 | * @ptr: an arbitrary pointer. |
| 1243 | * |
| 1244 | * Sets an assertion that the values that @ptr evaluates to is null. This is |
| 1245 | * the same as KUNIT_EXPECT_NULL(), except it causes an assertion |
| 1246 | * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. |
| 1247 | */ |
| 1248 | #define KUNIT_ASSERT_NULL(test, ptr) \ |
| 1249 | KUNIT_ASSERT_NULL_MSG(test, \ |
| 1250 | ptr, \ |
| 1251 | NULL) |
| 1252 | |
| 1253 | #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \ |
| 1254 | KUNIT_BINARY_PTR_ASSERTION(test, \ |
| 1255 | KUNIT_ASSERTION, \ |
| 1256 | ptr, ==, NULL, \ |
| 1257 | fmt, \ |
| 1258 | ##__VA_ARGS__) |
| 1259 | |
| 1260 | /** |
| 1261 | * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null. |
| 1262 | * @test: The test context object. |
| 1263 | * @ptr: an arbitrary pointer. |
| 1264 | * |
| 1265 | * Sets an assertion that the values that @ptr evaluates to is not null. This |
| 1266 | * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion |
| 1267 | * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. |
| 1268 | */ |
| 1269 | #define KUNIT_ASSERT_NOT_NULL(test, ptr) \ |
| 1270 | KUNIT_ASSERT_NOT_NULL_MSG(test, \ |
| 1271 | ptr, \ |
| 1272 | NULL) |
| 1273 | |
| 1274 | #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \ |
| 1275 | KUNIT_BINARY_PTR_ASSERTION(test, \ |
| 1276 | KUNIT_ASSERTION, \ |
| 1277 | ptr, !=, NULL, \ |
| 1278 | fmt, \ |
| 1279 | ##__VA_ARGS__) |
| 1280 | |
| 1281 | /** |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1282 | * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err. |
| 1283 | * @test: The test context object. |
| 1284 | * @ptr: an arbitrary pointer. |
| 1285 | * |
| 1286 | * Sets an assertion that the value that @ptr evaluates to is not null and not |
| 1287 | * an errno stored in a pointer. This is the same as |
| 1288 | * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see |
| 1289 | * KUNIT_ASSERT_TRUE()) when the assertion is not met. |
| 1290 | */ |
| 1291 | #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \ |
Daniel Latypov | 6709d0f | 2022-01-18 14:35:02 -0800 | [diff] [blame] | 1292 | KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL) |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 1293 | |
| 1294 | #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \ |
| 1295 | KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ |
| 1296 | KUNIT_ASSERTION, \ |
| 1297 | ptr, \ |
| 1298 | fmt, \ |
| 1299 | ##__VA_ARGS__) |
| 1300 | |
Arpitha Raghunandan | fadb08e7 | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 1301 | /** |
| 1302 | * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array. |
| 1303 | * @name: prefix for the test parameter generator function. |
| 1304 | * @array: array of test parameters. |
| 1305 | * @get_desc: function to convert param to description; NULL to use default |
| 1306 | * |
| 1307 | * Define function @name_gen_params which uses @array to generate parameters. |
| 1308 | */ |
| 1309 | #define KUNIT_ARRAY_PARAM(name, array, get_desc) \ |
| 1310 | static const void *name##_gen_params(const void *prev, char *desc) \ |
| 1311 | { \ |
| 1312 | typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \ |
| 1313 | if (__next - (array) < ARRAY_SIZE((array))) { \ |
| 1314 | void (*__get_desc)(typeof(__next), char *) = get_desc; \ |
| 1315 | if (__get_desc) \ |
| 1316 | __get_desc(__next, desc); \ |
| 1317 | return __next; \ |
| 1318 | } \ |
| 1319 | return NULL; \ |
| 1320 | } |
| 1321 | |
Daniel Latypov | 61695f8 | 2022-03-28 10:41:42 -0700 | [diff] [blame] | 1322 | // TODO([email protected]): consider eventually migrating users to explicitly |
| 1323 | // include resource.h themselves if they need it. |
| 1324 | #include <kunit/resource.h> |
| 1325 | |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 1326 | #endif /* _KUNIT_TEST_H */ |