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