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