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