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