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