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 /* 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 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. 113 * @generate_params: the generator function for parameterized tests. 114 * 115 * A test case is a function with the signature, 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 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; 148 const void* (*generate_params)(const void *prev, char *desc); 149 150 /* private: internal use only. */ 151 bool success; 152 char *log; 153 }; 154 155 static inline char *kunit_status_to_string(bool status) 156 { 157 return status ? "ok" : "not ok"; 158 } 159 160 /** 161 * KUNIT_CASE - A helper for creating a &struct kunit_case 162 * 163 * @test_name: a reference to a test case function. 164 * 165 * Takes a symbol for a function representing a test case and creates a 166 * &struct kunit_case object from it. See the documentation for 167 * &struct kunit_case for an example on how to use it. 168 */ 169 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name } 170 171 /** 172 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case 173 * 174 * @test_name: a reference to a test case function. 175 * @gen_params: a reference to a parameter generator function. 176 * 177 * The generator function:: 178 * 179 * const void* gen_params(const void *prev, char *desc) 180 * 181 * is used to lazily generate a series of arbitrarily typed values that fit into 182 * a void*. The argument @prev is the previously returned value, which should be 183 * used to derive the next value; @prev is set to NULL on the initial generator 184 * call. When no more values are available, the generator must return NULL. 185 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE) 186 * describing the parameter. 187 */ 188 #define KUNIT_CASE_PARAM(test_name, gen_params) \ 189 { .run_case = test_name, .name = #test_name, \ 190 .generate_params = gen_params } 191 192 /** 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; 213 214 /* private: internal use only */ 215 struct dentry *debugfs; 216 char *log; 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! */ 235 char *log; /* Points at case log after initialization */ 236 struct kunit_try_catch try_catch; 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; 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! */ 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. */ 256 }; 257 258 static inline void kunit_set_failure(struct kunit *test) 259 { 260 WRITE_ONCE(test->success, false); 261 } 262 263 void kunit_init_test(struct kunit *test, const char *name, char *log); 264 265 int kunit_run_tests(struct kunit_suite *suite); 266 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 272 int __kunit_test_suites_init(struct kunit_suite * const * const suites); 273 274 void __kunit_test_suites_exit(struct kunit_suite **suites); 275 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 285 #ifdef MODULE 286 /** 287 * kunit_test_suites_for_module() - used to register one or more 288 * &struct kunit_suite with KUnit. 289 * 290 * @__suites: a statically allocated list of &struct kunit_suite. 291 * 292 * Registers @__suites with the test framework. See &struct kunit_suite for 293 * more information. 294 * 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. 300 */ 301 #define kunit_test_suites_for_module(__suites) \ 302 static int __init kunit_test_suites_init(void) \ 303 { \ 304 return __kunit_test_suites_init(__suites); \ 305 } \ 306 module_init(kunit_test_suites_init); \ 307 \ 308 static void __exit kunit_test_suites_exit(void) \ 309 { \ 310 return __kunit_test_suites_exit(__suites); \ 311 } \ 312 module_exit(kunit_test_suites_exit) 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 \ 321 __used __section(".kunit_test_suites") = unique_array 322 323 /** 324 * kunit_test_suites() - used to register one or more &struct kunit_suite 325 * with KUnit. 326 * 327 * @__suites: a statically allocated list of &struct kunit_suite. 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 */ 341 #define kunit_test_suites(__suites...) \ 342 __kunit_test_suites(__UNIQUE_ID(array), \ 343 __UNIQUE_ID(suites), \ 344 ##__suites) 345 346 #define kunit_test_suite(suite) kunit_test_suites(&suite) 347 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 353 /* 354 * Like kunit_alloc_resource() below, but returns the struct kunit_resource 355 * object that contains the allocation. This is mostly for testing purposes. 356 */ 357 struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test, 358 kunit_resource_init_t init, 359 kunit_resource_free_t free, 360 gfp_t internal_gfp, 361 void *context); 362 363 /** 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). 414 * @res: The resource. 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); 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. 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. 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 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 * 451 * Note: KUnit needs to allocate memory for a kunit_resource object. You must 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 463 res = kzalloc(sizeof(*res), internal_gfp); 464 if (!res) 465 return NULL; 466 467 if (!kunit_add_resource(test, init, free, res, context)) 468 return res->data; 469 470 return NULL; 471 } 472 473 typedef bool (*kunit_resource_match_t)(struct kunit *test, 474 struct kunit_resource *res, 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. 480 * @res: The resource. 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, 487 struct kunit_resource *res, 488 void *match_data) 489 { 490 return res->data == match_data; 491 } 492 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 506 /** 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 unsigned long flags; 519 520 spin_lock_irqsave(&test->lock, flags); 521 522 list_for_each_entry_reverse(res, &test->resources, node) { 523 if (match(test, res, (void *)match_data)) { 524 found = res; 525 kunit_get_resource(found); 526 break; 527 } 528 } 529 530 spin_unlock_irqrestore(&test->lock, flags); 531 532 return found; 533 } 534 535 /** 536 * kunit_find_named_resource() - Find a resource using match name. 537 * @test: Test case to which the resource belongs. 538 * @name: match name. 539 */ 540 static inline struct kunit_resource * 541 kunit_find_named_resource(struct kunit *test, 542 const char *name) 543 { 544 return kunit_find_resource(test, kunit_resource_name_match, 545 (void *)name); 546 } 547 548 /** 549 * kunit_destroy_resource() - Find a kunit_resource and destroy it. 550 * @test: Test case to which the resource belongs. 551 * @match: Match function. Returns whether a given resource matches @match_data. 552 * @match_data: Data passed into @match. 553 * 554 * RETURNS: 555 * 0 if kunit_resource is found and freed, -ENOENT if not found. 556 */ 557 int kunit_destroy_resource(struct kunit *test, 558 kunit_resource_match_t match, 559 void *match_data); 560 561 static inline int kunit_destroy_named_resource(struct kunit *test, 562 const char *name) 563 { 564 return kunit_destroy_resource(test, kunit_resource_name_match, 565 (void *)name); 566 } 567 568 /** 569 * kunit_remove_resource() - remove resource from resource list associated with 570 * test. 571 * @test: The test context object. 572 * @res: The resource to be removed. 573 * 574 * Note that the resource will not be immediately freed since it is likely 575 * the caller has a reference to it via alloc_and_get() or find(); 576 * in this case a final call to kunit_put_resource() is required. 577 */ 578 void kunit_remove_resource(struct kunit *test, struct kunit_resource *res); 579 580 /** 581 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*. 582 * @test: The test context object. 583 * @size: The size in bytes of the desired memory. 584 * @gfp: flags passed to underlying kmalloc(). 585 * 586 * Just like `kmalloc(...)`, except the allocation is managed by the test case 587 * and is automatically cleaned up after the test case concludes. See &struct 588 * kunit_resource for more information. 589 */ 590 void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp); 591 592 /** 593 * kunit_kfree() - Like kfree except for allocations managed by KUnit. 594 * @test: The test case to which the resource belongs. 595 * @ptr: The memory allocation to free. 596 */ 597 void kunit_kfree(struct kunit *test, const void *ptr); 598 599 /** 600 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation. 601 * @test: The test context object. 602 * @size: The size in bytes of the desired memory. 603 * @gfp: flags passed to underlying kmalloc(). 604 * 605 * See kzalloc() and kunit_kmalloc() for more information. 606 */ 607 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp) 608 { 609 return kunit_kmalloc(test, size, gfp | __GFP_ZERO); 610 } 611 612 void kunit_cleanup(struct kunit *test); 613 614 void kunit_log_append(char *log, const char *fmt, ...); 615 616 /* 617 * printk and log to per-test or per-suite log buffer. Logging only done 618 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used. 619 */ 620 #define kunit_log(lvl, test_or_suite, fmt, ...) \ 621 do { \ 622 printk(lvl fmt, ##__VA_ARGS__); \ 623 kunit_log_append((test_or_suite)->log, fmt "\n", \ 624 ##__VA_ARGS__); \ 625 } while (0) 626 627 #define kunit_printk(lvl, test, fmt, ...) \ 628 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \ 629 (test)->name, ##__VA_ARGS__) 630 631 /** 632 * kunit_info() - Prints an INFO level message associated with @test. 633 * 634 * @test: The test context object. 635 * @fmt: A printk() style format string. 636 * 637 * Prints an info level message associated with the test suite being run. 638 * Takes a variable number of format parameters just like printk(). 639 */ 640 #define kunit_info(test, fmt, ...) \ 641 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) 642 643 /** 644 * kunit_warn() - Prints a WARN level message associated with @test. 645 * 646 * @test: The test context object. 647 * @fmt: A printk() style format string. 648 * 649 * Prints a warning level message. 650 */ 651 #define kunit_warn(test, fmt, ...) \ 652 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__) 653 654 /** 655 * kunit_err() - Prints an ERROR level message associated with @test. 656 * 657 * @test: The test context object. 658 * @fmt: A printk() style format string. 659 * 660 * Prints an error level message. 661 */ 662 #define kunit_err(test, fmt, ...) \ 663 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) 664 665 /** 666 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity. 667 * @test: The test context object. 668 * 669 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other 670 * words, it does nothing and only exists for code clarity. See 671 * KUNIT_EXPECT_TRUE() for more information. 672 */ 673 #define KUNIT_SUCCEED(test) do {} while (0) 674 675 void kunit_do_assertion(struct kunit *test, 676 struct kunit_assert *assert, 677 bool pass, 678 const char *fmt, ...); 679 680 #define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do { \ 681 struct assert_class __assertion = INITIALIZER; \ 682 kunit_do_assertion(test, \ 683 &__assertion.assert, \ 684 pass, \ 685 fmt, \ 686 ##__VA_ARGS__); \ 687 } while (0) 688 689 690 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \ 691 KUNIT_ASSERTION(test, \ 692 false, \ 693 kunit_fail_assert, \ 694 KUNIT_INIT_FAIL_ASSERT_STRUCT(test, assert_type), \ 695 fmt, \ 696 ##__VA_ARGS__) 697 698 /** 699 * KUNIT_FAIL() - Always causes a test to fail when evaluated. 700 * @test: The test context object. 701 * @fmt: an informational message to be printed when the assertion is made. 702 * @...: string format arguments. 703 * 704 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In 705 * other words, it always results in a failed expectation, and consequently 706 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE() 707 * for more information. 708 */ 709 #define KUNIT_FAIL(test, fmt, ...) \ 710 KUNIT_FAIL_ASSERTION(test, \ 711 KUNIT_EXPECTATION, \ 712 fmt, \ 713 ##__VA_ARGS__) 714 715 #define KUNIT_UNARY_ASSERTION(test, \ 716 assert_type, \ 717 condition, \ 718 expected_true, \ 719 fmt, \ 720 ...) \ 721 KUNIT_ASSERTION(test, \ 722 !!(condition) == !!expected_true, \ 723 kunit_unary_assert, \ 724 KUNIT_INIT_UNARY_ASSERT_STRUCT(test, \ 725 assert_type, \ 726 #condition, \ 727 expected_true), \ 728 fmt, \ 729 ##__VA_ARGS__) 730 731 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \ 732 KUNIT_UNARY_ASSERTION(test, \ 733 assert_type, \ 734 condition, \ 735 true, \ 736 fmt, \ 737 ##__VA_ARGS__) 738 739 #define KUNIT_TRUE_ASSERTION(test, assert_type, condition) \ 740 KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, NULL) 741 742 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \ 743 KUNIT_UNARY_ASSERTION(test, \ 744 assert_type, \ 745 condition, \ 746 false, \ 747 fmt, \ 748 ##__VA_ARGS__) 749 750 #define KUNIT_FALSE_ASSERTION(test, assert_type, condition) \ 751 KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, NULL) 752 753 /* 754 * A factory macro for defining the assertions and expectations for the basic 755 * comparisons defined for the built in types. 756 * 757 * Unfortunately, there is no common type that all types can be promoted to for 758 * which all the binary operators behave the same way as for the actual types 759 * (for example, there is no type that long long and unsigned long long can 760 * both be cast to where the comparison result is preserved for all values). So 761 * the best we can do is do the comparison in the original types and then coerce 762 * everything to long long for printing; this way, the comparison behaves 763 * correctly and the printed out value usually makes sense without 764 * interpretation, but can always be interpreted to figure out the actual 765 * value. 766 */ 767 #define KUNIT_BASE_BINARY_ASSERTION(test, \ 768 assert_class, \ 769 ASSERT_CLASS_INIT, \ 770 assert_type, \ 771 left, \ 772 op, \ 773 right, \ 774 fmt, \ 775 ...) \ 776 do { \ 777 typeof(left) __left = (left); \ 778 typeof(right) __right = (right); \ 779 ((void)__typecheck(__left, __right)); \ 780 \ 781 KUNIT_ASSERTION(test, \ 782 __left op __right, \ 783 assert_class, \ 784 ASSERT_CLASS_INIT(test, \ 785 assert_type, \ 786 #op, \ 787 #left, \ 788 __left, \ 789 #right, \ 790 __right), \ 791 fmt, \ 792 ##__VA_ARGS__); \ 793 } while (0) 794 795 #define KUNIT_BASE_EQ_MSG_ASSERTION(test, \ 796 assert_class, \ 797 ASSERT_CLASS_INIT, \ 798 assert_type, \ 799 left, \ 800 right, \ 801 fmt, \ 802 ...) \ 803 KUNIT_BASE_BINARY_ASSERTION(test, \ 804 assert_class, \ 805 ASSERT_CLASS_INIT, \ 806 assert_type, \ 807 left, ==, right, \ 808 fmt, \ 809 ##__VA_ARGS__) 810 811 #define KUNIT_BASE_NE_MSG_ASSERTION(test, \ 812 assert_class, \ 813 ASSERT_CLASS_INIT, \ 814 assert_type, \ 815 left, \ 816 right, \ 817 fmt, \ 818 ...) \ 819 KUNIT_BASE_BINARY_ASSERTION(test, \ 820 assert_class, \ 821 ASSERT_CLASS_INIT, \ 822 assert_type, \ 823 left, !=, right, \ 824 fmt, \ 825 ##__VA_ARGS__) 826 827 #define KUNIT_BASE_LT_MSG_ASSERTION(test, \ 828 assert_class, \ 829 ASSERT_CLASS_INIT, \ 830 assert_type, \ 831 left, \ 832 right, \ 833 fmt, \ 834 ...) \ 835 KUNIT_BASE_BINARY_ASSERTION(test, \ 836 assert_class, \ 837 ASSERT_CLASS_INIT, \ 838 assert_type, \ 839 left, <, right, \ 840 fmt, \ 841 ##__VA_ARGS__) 842 843 #define KUNIT_BASE_LE_MSG_ASSERTION(test, \ 844 assert_class, \ 845 ASSERT_CLASS_INIT, \ 846 assert_type, \ 847 left, \ 848 right, \ 849 fmt, \ 850 ...) \ 851 KUNIT_BASE_BINARY_ASSERTION(test, \ 852 assert_class, \ 853 ASSERT_CLASS_INIT, \ 854 assert_type, \ 855 left, <=, right, \ 856 fmt, \ 857 ##__VA_ARGS__) 858 859 #define KUNIT_BASE_GT_MSG_ASSERTION(test, \ 860 assert_class, \ 861 ASSERT_CLASS_INIT, \ 862 assert_type, \ 863 left, \ 864 right, \ 865 fmt, \ 866 ...) \ 867 KUNIT_BASE_BINARY_ASSERTION(test, \ 868 assert_class, \ 869 ASSERT_CLASS_INIT, \ 870 assert_type, \ 871 left, >, right, \ 872 fmt, \ 873 ##__VA_ARGS__) 874 875 #define KUNIT_BASE_GE_MSG_ASSERTION(test, \ 876 assert_class, \ 877 ASSERT_CLASS_INIT, \ 878 assert_type, \ 879 left, \ 880 right, \ 881 fmt, \ 882 ...) \ 883 KUNIT_BASE_BINARY_ASSERTION(test, \ 884 assert_class, \ 885 ASSERT_CLASS_INIT, \ 886 assert_type, \ 887 left, >=, right, \ 888 fmt, \ 889 ##__VA_ARGS__) 890 891 #define KUNIT_BINARY_EQ_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\ 892 KUNIT_BASE_EQ_MSG_ASSERTION(test, \ 893 kunit_binary_assert, \ 894 KUNIT_INIT_BINARY_ASSERT_STRUCT, \ 895 assert_type, \ 896 left, \ 897 right, \ 898 fmt, \ 899 ##__VA_ARGS__) 900 901 #define KUNIT_BINARY_EQ_ASSERTION(test, assert_type, left, right) \ 902 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \ 903 assert_type, \ 904 left, \ 905 right, \ 906 NULL) 907 908 #define KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \ 909 assert_type, \ 910 left, \ 911 right, \ 912 fmt, \ 913 ...) \ 914 KUNIT_BASE_EQ_MSG_ASSERTION(test, \ 915 kunit_binary_ptr_assert, \ 916 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \ 917 assert_type, \ 918 left, \ 919 right, \ 920 fmt, \ 921 ##__VA_ARGS__) 922 923 #define KUNIT_BINARY_PTR_EQ_ASSERTION(test, assert_type, left, right) \ 924 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \ 925 assert_type, \ 926 left, \ 927 right, \ 928 NULL) 929 930 #define KUNIT_BINARY_NE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\ 931 KUNIT_BASE_NE_MSG_ASSERTION(test, \ 932 kunit_binary_assert, \ 933 KUNIT_INIT_BINARY_ASSERT_STRUCT, \ 934 assert_type, \ 935 left, \ 936 right, \ 937 fmt, \ 938 ##__VA_ARGS__) 939 940 #define KUNIT_BINARY_NE_ASSERTION(test, assert_type, left, right) \ 941 KUNIT_BINARY_NE_MSG_ASSERTION(test, \ 942 assert_type, \ 943 left, \ 944 right, \ 945 NULL) 946 947 #define KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \ 948 assert_type, \ 949 left, \ 950 right, \ 951 fmt, \ 952 ...) \ 953 KUNIT_BASE_NE_MSG_ASSERTION(test, \ 954 kunit_binary_ptr_assert, \ 955 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \ 956 assert_type, \ 957 left, \ 958 right, \ 959 fmt, \ 960 ##__VA_ARGS__) 961 962 #define KUNIT_BINARY_PTR_NE_ASSERTION(test, assert_type, left, right) \ 963 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \ 964 assert_type, \ 965 left, \ 966 right, \ 967 NULL) 968 969 #define KUNIT_BINARY_LT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\ 970 KUNIT_BASE_LT_MSG_ASSERTION(test, \ 971 kunit_binary_assert, \ 972 KUNIT_INIT_BINARY_ASSERT_STRUCT, \ 973 assert_type, \ 974 left, \ 975 right, \ 976 fmt, \ 977 ##__VA_ARGS__) 978 979 #define KUNIT_BINARY_LT_ASSERTION(test, assert_type, left, right) \ 980 KUNIT_BINARY_LT_MSG_ASSERTION(test, \ 981 assert_type, \ 982 left, \ 983 right, \ 984 NULL) 985 986 #define KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \ 987 assert_type, \ 988 left, \ 989 right, \ 990 fmt, \ 991 ...) \ 992 KUNIT_BASE_LT_MSG_ASSERTION(test, \ 993 kunit_binary_ptr_assert, \ 994 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \ 995 assert_type, \ 996 left, \ 997 right, \ 998 fmt, \ 999 ##__VA_ARGS__) 1000 1001 #define KUNIT_BINARY_PTR_LT_ASSERTION(test, assert_type, left, right) \ 1002 KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \ 1003 assert_type, \ 1004 left, \ 1005 right, \ 1006 NULL) 1007 1008 #define KUNIT_BINARY_LE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\ 1009 KUNIT_BASE_LE_MSG_ASSERTION(test, \ 1010 kunit_binary_assert, \ 1011 KUNIT_INIT_BINARY_ASSERT_STRUCT, \ 1012 assert_type, \ 1013 left, \ 1014 right, \ 1015 fmt, \ 1016 ##__VA_ARGS__) 1017 1018 #define KUNIT_BINARY_LE_ASSERTION(test, assert_type, left, right) \ 1019 KUNIT_BINARY_LE_MSG_ASSERTION(test, \ 1020 assert_type, \ 1021 left, \ 1022 right, \ 1023 NULL) 1024 1025 #define KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \ 1026 assert_type, \ 1027 left, \ 1028 right, \ 1029 fmt, \ 1030 ...) \ 1031 KUNIT_BASE_LE_MSG_ASSERTION(test, \ 1032 kunit_binary_ptr_assert, \ 1033 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \ 1034 assert_type, \ 1035 left, \ 1036 right, \ 1037 fmt, \ 1038 ##__VA_ARGS__) 1039 1040 #define KUNIT_BINARY_PTR_LE_ASSERTION(test, assert_type, left, right) \ 1041 KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \ 1042 assert_type, \ 1043 left, \ 1044 right, \ 1045 NULL) 1046 1047 #define KUNIT_BINARY_GT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\ 1048 KUNIT_BASE_GT_MSG_ASSERTION(test, \ 1049 kunit_binary_assert, \ 1050 KUNIT_INIT_BINARY_ASSERT_STRUCT, \ 1051 assert_type, \ 1052 left, \ 1053 right, \ 1054 fmt, \ 1055 ##__VA_ARGS__) 1056 1057 #define KUNIT_BINARY_GT_ASSERTION(test, assert_type, left, right) \ 1058 KUNIT_BINARY_GT_MSG_ASSERTION(test, \ 1059 assert_type, \ 1060 left, \ 1061 right, \ 1062 NULL) 1063 1064 #define KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \ 1065 assert_type, \ 1066 left, \ 1067 right, \ 1068 fmt, \ 1069 ...) \ 1070 KUNIT_BASE_GT_MSG_ASSERTION(test, \ 1071 kunit_binary_ptr_assert, \ 1072 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \ 1073 assert_type, \ 1074 left, \ 1075 right, \ 1076 fmt, \ 1077 ##__VA_ARGS__) 1078 1079 #define KUNIT_BINARY_PTR_GT_ASSERTION(test, assert_type, left, right) \ 1080 KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \ 1081 assert_type, \ 1082 left, \ 1083 right, \ 1084 NULL) 1085 1086 #define KUNIT_BINARY_GE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\ 1087 KUNIT_BASE_GE_MSG_ASSERTION(test, \ 1088 kunit_binary_assert, \ 1089 KUNIT_INIT_BINARY_ASSERT_STRUCT, \ 1090 assert_type, \ 1091 left, \ 1092 right, \ 1093 fmt, \ 1094 ##__VA_ARGS__) 1095 1096 #define KUNIT_BINARY_GE_ASSERTION(test, assert_type, left, right) \ 1097 KUNIT_BINARY_GE_MSG_ASSERTION(test, \ 1098 assert_type, \ 1099 left, \ 1100 right, \ 1101 NULL) 1102 1103 #define KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \ 1104 assert_type, \ 1105 left, \ 1106 right, \ 1107 fmt, \ 1108 ...) \ 1109 KUNIT_BASE_GE_MSG_ASSERTION(test, \ 1110 kunit_binary_ptr_assert, \ 1111 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \ 1112 assert_type, \ 1113 left, \ 1114 right, \ 1115 fmt, \ 1116 ##__VA_ARGS__) 1117 1118 #define KUNIT_BINARY_PTR_GE_ASSERTION(test, assert_type, left, right) \ 1119 KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \ 1120 assert_type, \ 1121 left, \ 1122 right, \ 1123 NULL) 1124 1125 #define KUNIT_BINARY_STR_ASSERTION(test, \ 1126 assert_type, \ 1127 left, \ 1128 op, \ 1129 right, \ 1130 fmt, \ 1131 ...) \ 1132 do { \ 1133 typeof(left) __left = (left); \ 1134 typeof(right) __right = (right); \ 1135 \ 1136 KUNIT_ASSERTION(test, \ 1137 strcmp(__left, __right) op 0, \ 1138 kunit_binary_str_assert, \ 1139 KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(test, \ 1140 assert_type, \ 1141 #op, \ 1142 #left, \ 1143 __left, \ 1144 #right, \ 1145 __right), \ 1146 fmt, \ 1147 ##__VA_ARGS__); \ 1148 } while (0) 1149 1150 #define KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \ 1151 assert_type, \ 1152 left, \ 1153 right, \ 1154 fmt, \ 1155 ...) \ 1156 KUNIT_BINARY_STR_ASSERTION(test, \ 1157 assert_type, \ 1158 left, ==, right, \ 1159 fmt, \ 1160 ##__VA_ARGS__) 1161 1162 #define KUNIT_BINARY_STR_EQ_ASSERTION(test, assert_type, left, right) \ 1163 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \ 1164 assert_type, \ 1165 left, \ 1166 right, \ 1167 NULL) 1168 1169 #define KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \ 1170 assert_type, \ 1171 left, \ 1172 right, \ 1173 fmt, \ 1174 ...) \ 1175 KUNIT_BINARY_STR_ASSERTION(test, \ 1176 assert_type, \ 1177 left, !=, right, \ 1178 fmt, \ 1179 ##__VA_ARGS__) 1180 1181 #define KUNIT_BINARY_STR_NE_ASSERTION(test, assert_type, left, right) \ 1182 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \ 1183 assert_type, \ 1184 left, \ 1185 right, \ 1186 NULL) 1187 1188 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 1189 assert_type, \ 1190 ptr, \ 1191 fmt, \ 1192 ...) \ 1193 do { \ 1194 typeof(ptr) __ptr = (ptr); \ 1195 \ 1196 KUNIT_ASSERTION(test, \ 1197 !IS_ERR_OR_NULL(__ptr), \ 1198 kunit_ptr_not_err_assert, \ 1199 KUNIT_INIT_PTR_NOT_ERR_STRUCT(test, \ 1200 assert_type, \ 1201 #ptr, \ 1202 __ptr), \ 1203 fmt, \ 1204 ##__VA_ARGS__); \ 1205 } while (0) 1206 1207 #define KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, assert_type, ptr) \ 1208 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 1209 assert_type, \ 1210 ptr, \ 1211 NULL) 1212 1213 /** 1214 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true. 1215 * @test: The test context object. 1216 * @condition: an arbitrary boolean expression. The test fails when this does 1217 * not evaluate to true. 1218 * 1219 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case 1220 * to fail when the specified condition is not met; however, it will not prevent 1221 * the test case from continuing to run; this is otherwise known as an 1222 * *expectation failure*. 1223 */ 1224 #define KUNIT_EXPECT_TRUE(test, condition) \ 1225 KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition) 1226 1227 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \ 1228 KUNIT_TRUE_MSG_ASSERTION(test, \ 1229 KUNIT_EXPECTATION, \ 1230 condition, \ 1231 fmt, \ 1232 ##__VA_ARGS__) 1233 1234 /** 1235 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false. 1236 * @test: The test context object. 1237 * @condition: an arbitrary boolean expression. The test fails when this does 1238 * not evaluate to false. 1239 * 1240 * Sets an expectation that @condition evaluates to false. See 1241 * KUNIT_EXPECT_TRUE() for more information. 1242 */ 1243 #define KUNIT_EXPECT_FALSE(test, condition) \ 1244 KUNIT_FALSE_ASSERTION(test, KUNIT_EXPECTATION, condition) 1245 1246 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \ 1247 KUNIT_FALSE_MSG_ASSERTION(test, \ 1248 KUNIT_EXPECTATION, \ 1249 condition, \ 1250 fmt, \ 1251 ##__VA_ARGS__) 1252 1253 /** 1254 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal. 1255 * @test: The test context object. 1256 * @left: an arbitrary expression that evaluates to a primitive C type. 1257 * @right: an arbitrary expression that evaluates to a primitive C type. 1258 * 1259 * Sets an expectation that the values that @left and @right evaluate to are 1260 * equal. This is semantically equivalent to 1261 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for 1262 * more information. 1263 */ 1264 #define KUNIT_EXPECT_EQ(test, left, right) \ 1265 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1266 1267 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \ 1268 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \ 1269 KUNIT_EXPECTATION, \ 1270 left, \ 1271 right, \ 1272 fmt, \ 1273 ##__VA_ARGS__) 1274 1275 /** 1276 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal. 1277 * @test: The test context object. 1278 * @left: an arbitrary expression that evaluates to a pointer. 1279 * @right: an arbitrary expression that evaluates to a pointer. 1280 * 1281 * Sets an expectation that the values that @left and @right evaluate to are 1282 * equal. This is semantically equivalent to 1283 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for 1284 * more information. 1285 */ 1286 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \ 1287 KUNIT_BINARY_PTR_EQ_ASSERTION(test, \ 1288 KUNIT_EXPECTATION, \ 1289 left, \ 1290 right) 1291 1292 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \ 1293 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \ 1294 KUNIT_EXPECTATION, \ 1295 left, \ 1296 right, \ 1297 fmt, \ 1298 ##__VA_ARGS__) 1299 1300 /** 1301 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal. 1302 * @test: The test context object. 1303 * @left: an arbitrary expression that evaluates to a primitive C type. 1304 * @right: an arbitrary expression that evaluates to a primitive C type. 1305 * 1306 * Sets an expectation that the values that @left and @right evaluate to are not 1307 * equal. This is semantically equivalent to 1308 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for 1309 * more information. 1310 */ 1311 #define KUNIT_EXPECT_NE(test, left, right) \ 1312 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1313 1314 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \ 1315 KUNIT_BINARY_NE_MSG_ASSERTION(test, \ 1316 KUNIT_EXPECTATION, \ 1317 left, \ 1318 right, \ 1319 fmt, \ 1320 ##__VA_ARGS__) 1321 1322 /** 1323 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal. 1324 * @test: The test context object. 1325 * @left: an arbitrary expression that evaluates to a pointer. 1326 * @right: an arbitrary expression that evaluates to a pointer. 1327 * 1328 * Sets an expectation that the values that @left and @right evaluate to are not 1329 * equal. This is semantically equivalent to 1330 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for 1331 * more information. 1332 */ 1333 #define KUNIT_EXPECT_PTR_NE(test, left, right) \ 1334 KUNIT_BINARY_PTR_NE_ASSERTION(test, \ 1335 KUNIT_EXPECTATION, \ 1336 left, \ 1337 right) 1338 1339 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \ 1340 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \ 1341 KUNIT_EXPECTATION, \ 1342 left, \ 1343 right, \ 1344 fmt, \ 1345 ##__VA_ARGS__) 1346 1347 /** 1348 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right. 1349 * @test: The test context object. 1350 * @left: an arbitrary expression that evaluates to a primitive C type. 1351 * @right: an arbitrary expression that evaluates to a primitive C type. 1352 * 1353 * Sets an expectation that the value that @left evaluates to is less than the 1354 * value that @right evaluates to. This is semantically equivalent to 1355 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for 1356 * more information. 1357 */ 1358 #define KUNIT_EXPECT_LT(test, left, right) \ 1359 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1360 1361 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \ 1362 KUNIT_BINARY_LT_MSG_ASSERTION(test, \ 1363 KUNIT_EXPECTATION, \ 1364 left, \ 1365 right, \ 1366 fmt, \ 1367 ##__VA_ARGS__) 1368 1369 /** 1370 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right. 1371 * @test: The test context object. 1372 * @left: an arbitrary expression that evaluates to a primitive C type. 1373 * @right: an arbitrary expression that evaluates to a primitive C type. 1374 * 1375 * Sets an expectation that the value that @left evaluates to is less than or 1376 * equal to the value that @right evaluates to. Semantically this is equivalent 1377 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for 1378 * more information. 1379 */ 1380 #define KUNIT_EXPECT_LE(test, left, right) \ 1381 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1382 1383 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \ 1384 KUNIT_BINARY_LE_MSG_ASSERTION(test, \ 1385 KUNIT_EXPECTATION, \ 1386 left, \ 1387 right, \ 1388 fmt, \ 1389 ##__VA_ARGS__) 1390 1391 /** 1392 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right. 1393 * @test: The test context object. 1394 * @left: an arbitrary expression that evaluates to a primitive C type. 1395 * @right: an arbitrary expression that evaluates to a primitive C type. 1396 * 1397 * Sets an expectation that the value that @left evaluates to is greater than 1398 * the value that @right evaluates to. This is semantically equivalent to 1399 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for 1400 * more information. 1401 */ 1402 #define KUNIT_EXPECT_GT(test, left, right) \ 1403 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1404 1405 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \ 1406 KUNIT_BINARY_GT_MSG_ASSERTION(test, \ 1407 KUNIT_EXPECTATION, \ 1408 left, \ 1409 right, \ 1410 fmt, \ 1411 ##__VA_ARGS__) 1412 1413 /** 1414 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right. 1415 * @test: The test context object. 1416 * @left: an arbitrary expression that evaluates to a primitive C type. 1417 * @right: an arbitrary expression that evaluates to a primitive C type. 1418 * 1419 * Sets an expectation that the value that @left evaluates to is greater than 1420 * the value that @right evaluates to. This is semantically equivalent to 1421 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for 1422 * more information. 1423 */ 1424 #define KUNIT_EXPECT_GE(test, left, right) \ 1425 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1426 1427 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \ 1428 KUNIT_BINARY_GE_MSG_ASSERTION(test, \ 1429 KUNIT_EXPECTATION, \ 1430 left, \ 1431 right, \ 1432 fmt, \ 1433 ##__VA_ARGS__) 1434 1435 /** 1436 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal. 1437 * @test: The test context object. 1438 * @left: an arbitrary expression that evaluates to a null terminated string. 1439 * @right: an arbitrary expression that evaluates to a null terminated string. 1440 * 1441 * Sets an expectation that the values that @left and @right evaluate to are 1442 * equal. This is semantically equivalent to 1443 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE() 1444 * for more information. 1445 */ 1446 #define KUNIT_EXPECT_STREQ(test, left, right) \ 1447 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1448 1449 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \ 1450 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \ 1451 KUNIT_EXPECTATION, \ 1452 left, \ 1453 right, \ 1454 fmt, \ 1455 ##__VA_ARGS__) 1456 1457 /** 1458 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal. 1459 * @test: The test context object. 1460 * @left: an arbitrary expression that evaluates to a null terminated string. 1461 * @right: an arbitrary expression that evaluates to a null terminated string. 1462 * 1463 * Sets an expectation that the values that @left and @right evaluate to are 1464 * not equal. This is semantically equivalent to 1465 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE() 1466 * for more information. 1467 */ 1468 #define KUNIT_EXPECT_STRNEQ(test, left, right) \ 1469 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1470 1471 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \ 1472 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \ 1473 KUNIT_EXPECTATION, \ 1474 left, \ 1475 right, \ 1476 fmt, \ 1477 ##__VA_ARGS__) 1478 1479 /** 1480 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err. 1481 * @test: The test context object. 1482 * @ptr: an arbitrary pointer. 1483 * 1484 * Sets an expectation that the value that @ptr evaluates to is not null and not 1485 * an errno stored in a pointer. This is semantically equivalent to 1486 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for 1487 * more information. 1488 */ 1489 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \ 1490 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_EXPECTATION, ptr) 1491 1492 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \ 1493 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 1494 KUNIT_EXPECTATION, \ 1495 ptr, \ 1496 fmt, \ 1497 ##__VA_ARGS__) 1498 1499 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \ 1500 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__) 1501 1502 /** 1503 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true. 1504 * @test: The test context object. 1505 * @condition: an arbitrary boolean expression. The test fails and aborts when 1506 * this does not evaluate to true. 1507 * 1508 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to 1509 * fail *and immediately abort* when the specified condition is not met. Unlike 1510 * an expectation failure, it will prevent the test case from continuing to run; 1511 * this is otherwise known as an *assertion failure*. 1512 */ 1513 #define KUNIT_ASSERT_TRUE(test, condition) \ 1514 KUNIT_TRUE_ASSERTION(test, KUNIT_ASSERTION, condition) 1515 1516 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \ 1517 KUNIT_TRUE_MSG_ASSERTION(test, \ 1518 KUNIT_ASSERTION, \ 1519 condition, \ 1520 fmt, \ 1521 ##__VA_ARGS__) 1522 1523 /** 1524 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false. 1525 * @test: The test context object. 1526 * @condition: an arbitrary boolean expression. 1527 * 1528 * Sets an assertion that the value that @condition evaluates to is false. This 1529 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure 1530 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1531 */ 1532 #define KUNIT_ASSERT_FALSE(test, condition) \ 1533 KUNIT_FALSE_ASSERTION(test, KUNIT_ASSERTION, condition) 1534 1535 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \ 1536 KUNIT_FALSE_MSG_ASSERTION(test, \ 1537 KUNIT_ASSERTION, \ 1538 condition, \ 1539 fmt, \ 1540 ##__VA_ARGS__) 1541 1542 /** 1543 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal. 1544 * @test: The test context object. 1545 * @left: an arbitrary expression that evaluates to a primitive C type. 1546 * @right: an arbitrary expression that evaluates to a primitive C type. 1547 * 1548 * Sets an assertion that the values that @left and @right evaluate to are 1549 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion 1550 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1551 */ 1552 #define KUNIT_ASSERT_EQ(test, left, right) \ 1553 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right) 1554 1555 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \ 1556 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \ 1557 KUNIT_ASSERTION, \ 1558 left, \ 1559 right, \ 1560 fmt, \ 1561 ##__VA_ARGS__) 1562 1563 /** 1564 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal. 1565 * @test: The test context object. 1566 * @left: an arbitrary expression that evaluates to a pointer. 1567 * @right: an arbitrary expression that evaluates to a pointer. 1568 * 1569 * Sets an assertion that the values that @left and @right evaluate to are 1570 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion 1571 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1572 */ 1573 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \ 1574 KUNIT_BINARY_PTR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right) 1575 1576 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \ 1577 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \ 1578 KUNIT_ASSERTION, \ 1579 left, \ 1580 right, \ 1581 fmt, \ 1582 ##__VA_ARGS__) 1583 1584 /** 1585 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal. 1586 * @test: The test context object. 1587 * @left: an arbitrary expression that evaluates to a primitive C type. 1588 * @right: an arbitrary expression that evaluates to a primitive C type. 1589 * 1590 * Sets an assertion that the values that @left and @right evaluate to are not 1591 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion 1592 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1593 */ 1594 #define KUNIT_ASSERT_NE(test, left, right) \ 1595 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1596 1597 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \ 1598 KUNIT_BINARY_NE_MSG_ASSERTION(test, \ 1599 KUNIT_ASSERTION, \ 1600 left, \ 1601 right, \ 1602 fmt, \ 1603 ##__VA_ARGS__) 1604 1605 /** 1606 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal. 1607 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal. 1608 * @test: The test context object. 1609 * @left: an arbitrary expression that evaluates to a pointer. 1610 * @right: an arbitrary expression that evaluates to a pointer. 1611 * 1612 * Sets an assertion that the values that @left and @right evaluate to are not 1613 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion 1614 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1615 */ 1616 #define KUNIT_ASSERT_PTR_NE(test, left, right) \ 1617 KUNIT_BINARY_PTR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1618 1619 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \ 1620 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \ 1621 KUNIT_ASSERTION, \ 1622 left, \ 1623 right, \ 1624 fmt, \ 1625 ##__VA_ARGS__) 1626 /** 1627 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right. 1628 * @test: The test context object. 1629 * @left: an arbitrary expression that evaluates to a primitive C type. 1630 * @right: an arbitrary expression that evaluates to a primitive C type. 1631 * 1632 * Sets an assertion that the value that @left evaluates to is less than the 1633 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except 1634 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1635 * is not met. 1636 */ 1637 #define KUNIT_ASSERT_LT(test, left, right) \ 1638 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_ASSERTION, left, right) 1639 1640 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \ 1641 KUNIT_BINARY_LT_MSG_ASSERTION(test, \ 1642 KUNIT_ASSERTION, \ 1643 left, \ 1644 right, \ 1645 fmt, \ 1646 ##__VA_ARGS__) 1647 /** 1648 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right. 1649 * @test: The test context object. 1650 * @left: an arbitrary expression that evaluates to a primitive C type. 1651 * @right: an arbitrary expression that evaluates to a primitive C type. 1652 * 1653 * Sets an assertion that the value that @left evaluates to is less than or 1654 * equal to the value that @right evaluates to. This is the same as 1655 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see 1656 * KUNIT_ASSERT_TRUE()) when the assertion is not met. 1657 */ 1658 #define KUNIT_ASSERT_LE(test, left, right) \ 1659 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1660 1661 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \ 1662 KUNIT_BINARY_LE_MSG_ASSERTION(test, \ 1663 KUNIT_ASSERTION, \ 1664 left, \ 1665 right, \ 1666 fmt, \ 1667 ##__VA_ARGS__) 1668 1669 /** 1670 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right. 1671 * @test: The test context object. 1672 * @left: an arbitrary expression that evaluates to a primitive C type. 1673 * @right: an arbitrary expression that evaluates to a primitive C type. 1674 * 1675 * Sets an assertion that the value that @left evaluates to is greater than the 1676 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except 1677 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1678 * is not met. 1679 */ 1680 #define KUNIT_ASSERT_GT(test, left, right) \ 1681 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_ASSERTION, left, right) 1682 1683 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \ 1684 KUNIT_BINARY_GT_MSG_ASSERTION(test, \ 1685 KUNIT_ASSERTION, \ 1686 left, \ 1687 right, \ 1688 fmt, \ 1689 ##__VA_ARGS__) 1690 1691 /** 1692 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right. 1693 * @test: The test context object. 1694 * @left: an arbitrary expression that evaluates to a primitive C type. 1695 * @right: an arbitrary expression that evaluates to a primitive C type. 1696 * 1697 * Sets an assertion that the value that @left evaluates to is greater than the 1698 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except 1699 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1700 * is not met. 1701 */ 1702 #define KUNIT_ASSERT_GE(test, left, right) \ 1703 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1704 1705 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \ 1706 KUNIT_BINARY_GE_MSG_ASSERTION(test, \ 1707 KUNIT_ASSERTION, \ 1708 left, \ 1709 right, \ 1710 fmt, \ 1711 ##__VA_ARGS__) 1712 1713 /** 1714 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal. 1715 * @test: The test context object. 1716 * @left: an arbitrary expression that evaluates to a null terminated string. 1717 * @right: an arbitrary expression that evaluates to a null terminated string. 1718 * 1719 * Sets an assertion that the values that @left and @right evaluate to are 1720 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an 1721 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1722 */ 1723 #define KUNIT_ASSERT_STREQ(test, left, right) \ 1724 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right) 1725 1726 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \ 1727 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \ 1728 KUNIT_ASSERTION, \ 1729 left, \ 1730 right, \ 1731 fmt, \ 1732 ##__VA_ARGS__) 1733 1734 /** 1735 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal. 1736 * @test: The test context object. 1737 * @left: an arbitrary expression that evaluates to a null terminated string. 1738 * @right: an arbitrary expression that evaluates to a null terminated string. 1739 * 1740 * Sets an expectation that the values that @left and @right evaluate to are 1741 * not equal. This is semantically equivalent to 1742 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE() 1743 * for more information. 1744 */ 1745 #define KUNIT_ASSERT_STRNEQ(test, left, right) \ 1746 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1747 1748 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \ 1749 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \ 1750 KUNIT_ASSERTION, \ 1751 left, \ 1752 right, \ 1753 fmt, \ 1754 ##__VA_ARGS__) 1755 1756 /** 1757 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err. 1758 * @test: The test context object. 1759 * @ptr: an arbitrary pointer. 1760 * 1761 * Sets an assertion that the value that @ptr evaluates to is not null and not 1762 * an errno stored in a pointer. This is the same as 1763 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see 1764 * KUNIT_ASSERT_TRUE()) when the assertion is not met. 1765 */ 1766 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \ 1767 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_ASSERTION, ptr) 1768 1769 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \ 1770 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 1771 KUNIT_ASSERTION, \ 1772 ptr, \ 1773 fmt, \ 1774 ##__VA_ARGS__) 1775 1776 /** 1777 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array. 1778 * @name: prefix for the test parameter generator function. 1779 * @array: array of test parameters. 1780 * @get_desc: function to convert param to description; NULL to use default 1781 * 1782 * Define function @name_gen_params which uses @array to generate parameters. 1783 */ 1784 #define KUNIT_ARRAY_PARAM(name, array, get_desc) \ 1785 static const void *name##_gen_params(const void *prev, char *desc) \ 1786 { \ 1787 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \ 1788 if (__next - (array) < ARRAY_SIZE((array))) { \ 1789 void (*__get_desc)(typeof(__next), char *) = get_desc; \ 1790 if (__get_desc) \ 1791 __get_desc(__next, desc); \ 1792 return __next; \ 1793 } \ 1794 return NULL; \ 1795 } 1796 1797 #endif /* _KUNIT_TEST_H */ 1798