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