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