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 void __printf(3, 4) kunit_printk(const char *level, 343 const struct kunit *test, 344 const char *fmt, ...); 345 346 /** 347 * kunit_info() - Prints an INFO level message associated with @test. 348 * 349 * @test: The test context object. 350 * @fmt: A printk() style format string. 351 * 352 * Prints an info level message associated with the test suite being run. 353 * Takes a variable number of format parameters just like printk(). 354 */ 355 #define kunit_info(test, fmt, ...) \ 356 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) 357 358 /** 359 * kunit_warn() - Prints a WARN level message associated with @test. 360 * 361 * @test: The test context object. 362 * @fmt: A printk() style format string. 363 * 364 * Prints a warning level message. 365 */ 366 #define kunit_warn(test, fmt, ...) \ 367 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__) 368 369 /** 370 * kunit_err() - Prints an ERROR level message associated with @test. 371 * 372 * @test: The test context object. 373 * @fmt: A printk() style format string. 374 * 375 * Prints an error level message. 376 */ 377 #define kunit_err(test, fmt, ...) \ 378 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) 379 380 /** 381 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity. 382 * @test: The test context object. 383 * 384 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other 385 * words, it does nothing and only exists for code clarity. See 386 * KUNIT_EXPECT_TRUE() for more information. 387 */ 388 #define KUNIT_SUCCEED(test) do {} while (0) 389 390 void kunit_do_assertion(struct kunit *test, 391 struct kunit_assert *assert, 392 bool pass, 393 const char *fmt, ...); 394 395 #define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do { \ 396 struct assert_class __assertion = INITIALIZER; \ 397 kunit_do_assertion(test, \ 398 &__assertion.assert, \ 399 pass, \ 400 fmt, \ 401 ##__VA_ARGS__); \ 402 } while (0) 403 404 405 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \ 406 KUNIT_ASSERTION(test, \ 407 false, \ 408 kunit_fail_assert, \ 409 KUNIT_INIT_FAIL_ASSERT_STRUCT(test, assert_type), \ 410 fmt, \ 411 ##__VA_ARGS__) 412 413 /** 414 * KUNIT_FAIL() - Always causes a test to fail when evaluated. 415 * @test: The test context object. 416 * @fmt: an informational message to be printed when the assertion is made. 417 * @...: string format arguments. 418 * 419 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In 420 * other words, it always results in a failed expectation, and consequently 421 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE() 422 * for more information. 423 */ 424 #define KUNIT_FAIL(test, fmt, ...) \ 425 KUNIT_FAIL_ASSERTION(test, \ 426 KUNIT_EXPECTATION, \ 427 fmt, \ 428 ##__VA_ARGS__) 429 430 #define KUNIT_UNARY_ASSERTION(test, \ 431 assert_type, \ 432 condition, \ 433 expected_true, \ 434 fmt, \ 435 ...) \ 436 KUNIT_ASSERTION(test, \ 437 !!(condition) == !!expected_true, \ 438 kunit_unary_assert, \ 439 KUNIT_INIT_UNARY_ASSERT_STRUCT(test, \ 440 assert_type, \ 441 #condition, \ 442 expected_true), \ 443 fmt, \ 444 ##__VA_ARGS__) 445 446 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \ 447 KUNIT_UNARY_ASSERTION(test, \ 448 assert_type, \ 449 condition, \ 450 true, \ 451 fmt, \ 452 ##__VA_ARGS__) 453 454 #define KUNIT_TRUE_ASSERTION(test, assert_type, condition) \ 455 KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, NULL) 456 457 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \ 458 KUNIT_UNARY_ASSERTION(test, \ 459 assert_type, \ 460 condition, \ 461 false, \ 462 fmt, \ 463 ##__VA_ARGS__) 464 465 #define KUNIT_FALSE_ASSERTION(test, assert_type, condition) \ 466 KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, NULL) 467 468 /* 469 * A factory macro for defining the assertions and expectations for the basic 470 * comparisons defined for the built in types. 471 * 472 * Unfortunately, there is no common type that all types can be promoted to for 473 * which all the binary operators behave the same way as for the actual types 474 * (for example, there is no type that long long and unsigned long long can 475 * both be cast to where the comparison result is preserved for all values). So 476 * the best we can do is do the comparison in the original types and then coerce 477 * everything to long long for printing; this way, the comparison behaves 478 * correctly and the printed out value usually makes sense without 479 * interpretation, but can always be interpreted to figure out the actual 480 * value. 481 */ 482 #define KUNIT_BASE_BINARY_ASSERTION(test, \ 483 assert_class, \ 484 ASSERT_CLASS_INIT, \ 485 assert_type, \ 486 left, \ 487 op, \ 488 right, \ 489 fmt, \ 490 ...) \ 491 do { \ 492 typeof(left) __left = (left); \ 493 typeof(right) __right = (right); \ 494 ((void)__typecheck(__left, __right)); \ 495 \ 496 KUNIT_ASSERTION(test, \ 497 __left op __right, \ 498 assert_class, \ 499 ASSERT_CLASS_INIT(test, \ 500 assert_type, \ 501 #op, \ 502 #left, \ 503 __left, \ 504 #right, \ 505 __right), \ 506 fmt, \ 507 ##__VA_ARGS__); \ 508 } while (0) 509 510 #define KUNIT_BASE_EQ_MSG_ASSERTION(test, \ 511 assert_class, \ 512 ASSERT_CLASS_INIT, \ 513 assert_type, \ 514 left, \ 515 right, \ 516 fmt, \ 517 ...) \ 518 KUNIT_BASE_BINARY_ASSERTION(test, \ 519 assert_class, \ 520 ASSERT_CLASS_INIT, \ 521 assert_type, \ 522 left, ==, right, \ 523 fmt, \ 524 ##__VA_ARGS__) 525 526 #define KUNIT_BASE_NE_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_LT_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_LE_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_GT_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_GE_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_BINARY_EQ_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\ 607 KUNIT_BASE_EQ_MSG_ASSERTION(test, \ 608 kunit_binary_assert, \ 609 KUNIT_INIT_BINARY_ASSERT_STRUCT, \ 610 assert_type, \ 611 left, \ 612 right, \ 613 fmt, \ 614 ##__VA_ARGS__) 615 616 #define KUNIT_BINARY_EQ_ASSERTION(test, assert_type, left, right) \ 617 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \ 618 assert_type, \ 619 left, \ 620 right, \ 621 NULL) 622 623 #define KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \ 624 assert_type, \ 625 left, \ 626 right, \ 627 fmt, \ 628 ...) \ 629 KUNIT_BASE_EQ_MSG_ASSERTION(test, \ 630 kunit_binary_ptr_assert, \ 631 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \ 632 assert_type, \ 633 left, \ 634 right, \ 635 fmt, \ 636 ##__VA_ARGS__) 637 638 #define KUNIT_BINARY_PTR_EQ_ASSERTION(test, assert_type, left, right) \ 639 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \ 640 assert_type, \ 641 left, \ 642 right, \ 643 NULL) 644 645 #define KUNIT_BINARY_NE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\ 646 KUNIT_BASE_NE_MSG_ASSERTION(test, \ 647 kunit_binary_assert, \ 648 KUNIT_INIT_BINARY_ASSERT_STRUCT, \ 649 assert_type, \ 650 left, \ 651 right, \ 652 fmt, \ 653 ##__VA_ARGS__) 654 655 #define KUNIT_BINARY_NE_ASSERTION(test, assert_type, left, right) \ 656 KUNIT_BINARY_NE_MSG_ASSERTION(test, \ 657 assert_type, \ 658 left, \ 659 right, \ 660 NULL) 661 662 #define KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \ 663 assert_type, \ 664 left, \ 665 right, \ 666 fmt, \ 667 ...) \ 668 KUNIT_BASE_NE_MSG_ASSERTION(test, \ 669 kunit_binary_ptr_assert, \ 670 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \ 671 assert_type, \ 672 left, \ 673 right, \ 674 fmt, \ 675 ##__VA_ARGS__) 676 677 #define KUNIT_BINARY_PTR_NE_ASSERTION(test, assert_type, left, right) \ 678 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \ 679 assert_type, \ 680 left, \ 681 right, \ 682 NULL) 683 684 #define KUNIT_BINARY_LT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\ 685 KUNIT_BASE_LT_MSG_ASSERTION(test, \ 686 kunit_binary_assert, \ 687 KUNIT_INIT_BINARY_ASSERT_STRUCT, \ 688 assert_type, \ 689 left, \ 690 right, \ 691 fmt, \ 692 ##__VA_ARGS__) 693 694 #define KUNIT_BINARY_LT_ASSERTION(test, assert_type, left, right) \ 695 KUNIT_BINARY_LT_MSG_ASSERTION(test, \ 696 assert_type, \ 697 left, \ 698 right, \ 699 NULL) 700 701 #define KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \ 702 assert_type, \ 703 left, \ 704 right, \ 705 fmt, \ 706 ...) \ 707 KUNIT_BASE_LT_MSG_ASSERTION(test, \ 708 kunit_binary_ptr_assert, \ 709 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \ 710 assert_type, \ 711 left, \ 712 right, \ 713 fmt, \ 714 ##__VA_ARGS__) 715 716 #define KUNIT_BINARY_PTR_LT_ASSERTION(test, assert_type, left, right) \ 717 KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \ 718 assert_type, \ 719 left, \ 720 right, \ 721 NULL) 722 723 #define KUNIT_BINARY_LE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\ 724 KUNIT_BASE_LE_MSG_ASSERTION(test, \ 725 kunit_binary_assert, \ 726 KUNIT_INIT_BINARY_ASSERT_STRUCT, \ 727 assert_type, \ 728 left, \ 729 right, \ 730 fmt, \ 731 ##__VA_ARGS__) 732 733 #define KUNIT_BINARY_LE_ASSERTION(test, assert_type, left, right) \ 734 KUNIT_BINARY_LE_MSG_ASSERTION(test, \ 735 assert_type, \ 736 left, \ 737 right, \ 738 NULL) 739 740 #define KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \ 741 assert_type, \ 742 left, \ 743 right, \ 744 fmt, \ 745 ...) \ 746 KUNIT_BASE_LE_MSG_ASSERTION(test, \ 747 kunit_binary_ptr_assert, \ 748 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \ 749 assert_type, \ 750 left, \ 751 right, \ 752 fmt, \ 753 ##__VA_ARGS__) 754 755 #define KUNIT_BINARY_PTR_LE_ASSERTION(test, assert_type, left, right) \ 756 KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \ 757 assert_type, \ 758 left, \ 759 right, \ 760 NULL) 761 762 #define KUNIT_BINARY_GT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\ 763 KUNIT_BASE_GT_MSG_ASSERTION(test, \ 764 kunit_binary_assert, \ 765 KUNIT_INIT_BINARY_ASSERT_STRUCT, \ 766 assert_type, \ 767 left, \ 768 right, \ 769 fmt, \ 770 ##__VA_ARGS__) 771 772 #define KUNIT_BINARY_GT_ASSERTION(test, assert_type, left, right) \ 773 KUNIT_BINARY_GT_MSG_ASSERTION(test, \ 774 assert_type, \ 775 left, \ 776 right, \ 777 NULL) 778 779 #define KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \ 780 assert_type, \ 781 left, \ 782 right, \ 783 fmt, \ 784 ...) \ 785 KUNIT_BASE_GT_MSG_ASSERTION(test, \ 786 kunit_binary_ptr_assert, \ 787 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \ 788 assert_type, \ 789 left, \ 790 right, \ 791 fmt, \ 792 ##__VA_ARGS__) 793 794 #define KUNIT_BINARY_PTR_GT_ASSERTION(test, assert_type, left, right) \ 795 KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \ 796 assert_type, \ 797 left, \ 798 right, \ 799 NULL) 800 801 #define KUNIT_BINARY_GE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\ 802 KUNIT_BASE_GE_MSG_ASSERTION(test, \ 803 kunit_binary_assert, \ 804 KUNIT_INIT_BINARY_ASSERT_STRUCT, \ 805 assert_type, \ 806 left, \ 807 right, \ 808 fmt, \ 809 ##__VA_ARGS__) 810 811 #define KUNIT_BINARY_GE_ASSERTION(test, assert_type, left, right) \ 812 KUNIT_BINARY_GE_MSG_ASSERTION(test, \ 813 assert_type, \ 814 left, \ 815 right, \ 816 NULL) 817 818 #define KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \ 819 assert_type, \ 820 left, \ 821 right, \ 822 fmt, \ 823 ...) \ 824 KUNIT_BASE_GE_MSG_ASSERTION(test, \ 825 kunit_binary_ptr_assert, \ 826 KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \ 827 assert_type, \ 828 left, \ 829 right, \ 830 fmt, \ 831 ##__VA_ARGS__) 832 833 #define KUNIT_BINARY_PTR_GE_ASSERTION(test, assert_type, left, right) \ 834 KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \ 835 assert_type, \ 836 left, \ 837 right, \ 838 NULL) 839 840 #define KUNIT_BINARY_STR_ASSERTION(test, \ 841 assert_type, \ 842 left, \ 843 op, \ 844 right, \ 845 fmt, \ 846 ...) \ 847 do { \ 848 typeof(left) __left = (left); \ 849 typeof(right) __right = (right); \ 850 \ 851 KUNIT_ASSERTION(test, \ 852 strcmp(__left, __right) op 0, \ 853 kunit_binary_str_assert, \ 854 KUNIT_INIT_BINARY_ASSERT_STRUCT(test, \ 855 assert_type, \ 856 #op, \ 857 #left, \ 858 __left, \ 859 #right, \ 860 __right), \ 861 fmt, \ 862 ##__VA_ARGS__); \ 863 } while (0) 864 865 #define KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \ 866 assert_type, \ 867 left, \ 868 right, \ 869 fmt, \ 870 ...) \ 871 KUNIT_BINARY_STR_ASSERTION(test, \ 872 assert_type, \ 873 left, ==, right, \ 874 fmt, \ 875 ##__VA_ARGS__) 876 877 #define KUNIT_BINARY_STR_EQ_ASSERTION(test, assert_type, left, right) \ 878 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \ 879 assert_type, \ 880 left, \ 881 right, \ 882 NULL) 883 884 #define KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \ 885 assert_type, \ 886 left, \ 887 right, \ 888 fmt, \ 889 ...) \ 890 KUNIT_BINARY_STR_ASSERTION(test, \ 891 assert_type, \ 892 left, !=, right, \ 893 fmt, \ 894 ##__VA_ARGS__) 895 896 #define KUNIT_BINARY_STR_NE_ASSERTION(test, assert_type, left, right) \ 897 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \ 898 assert_type, \ 899 left, \ 900 right, \ 901 NULL) 902 903 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 904 assert_type, \ 905 ptr, \ 906 fmt, \ 907 ...) \ 908 do { \ 909 typeof(ptr) __ptr = (ptr); \ 910 \ 911 KUNIT_ASSERTION(test, \ 912 !IS_ERR_OR_NULL(__ptr), \ 913 kunit_ptr_not_err_assert, \ 914 KUNIT_INIT_PTR_NOT_ERR_STRUCT(test, \ 915 assert_type, \ 916 #ptr, \ 917 __ptr), \ 918 fmt, \ 919 ##__VA_ARGS__); \ 920 } while (0) 921 922 #define KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, assert_type, ptr) \ 923 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 924 assert_type, \ 925 ptr, \ 926 NULL) 927 928 /** 929 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true. 930 * @test: The test context object. 931 * @condition: an arbitrary boolean expression. The test fails when this does 932 * not evaluate to true. 933 * 934 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case 935 * to fail when the specified condition is not met; however, it will not prevent 936 * the test case from continuing to run; this is otherwise known as an 937 * *expectation failure*. 938 */ 939 #define KUNIT_EXPECT_TRUE(test, condition) \ 940 KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition) 941 942 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \ 943 KUNIT_TRUE_MSG_ASSERTION(test, \ 944 KUNIT_EXPECTATION, \ 945 condition, \ 946 fmt, \ 947 ##__VA_ARGS__) 948 949 /** 950 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false. 951 * @test: The test context object. 952 * @condition: an arbitrary boolean expression. The test fails when this does 953 * not evaluate to false. 954 * 955 * Sets an expectation that @condition evaluates to false. See 956 * KUNIT_EXPECT_TRUE() for more information. 957 */ 958 #define KUNIT_EXPECT_FALSE(test, condition) \ 959 KUNIT_FALSE_ASSERTION(test, KUNIT_EXPECTATION, condition) 960 961 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \ 962 KUNIT_FALSE_MSG_ASSERTION(test, \ 963 KUNIT_EXPECTATION, \ 964 condition, \ 965 fmt, \ 966 ##__VA_ARGS__) 967 968 /** 969 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal. 970 * @test: The test context object. 971 * @left: an arbitrary expression that evaluates to a primitive C type. 972 * @right: an arbitrary expression that evaluates to a primitive C type. 973 * 974 * Sets an expectation that the values that @left and @right evaluate to are 975 * equal. This is semantically equivalent to 976 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for 977 * more information. 978 */ 979 #define KUNIT_EXPECT_EQ(test, left, right) \ 980 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right) 981 982 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \ 983 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \ 984 KUNIT_EXPECTATION, \ 985 left, \ 986 right, \ 987 fmt, \ 988 ##__VA_ARGS__) 989 990 /** 991 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal. 992 * @test: The test context object. 993 * @left: an arbitrary expression that evaluates to a pointer. 994 * @right: an arbitrary expression that evaluates to a pointer. 995 * 996 * Sets an expectation that the values that @left and @right evaluate to are 997 * equal. This is semantically equivalent to 998 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for 999 * more information. 1000 */ 1001 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \ 1002 KUNIT_BINARY_PTR_EQ_ASSERTION(test, \ 1003 KUNIT_EXPECTATION, \ 1004 left, \ 1005 right) 1006 1007 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \ 1008 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \ 1009 KUNIT_EXPECTATION, \ 1010 left, \ 1011 right, \ 1012 fmt, \ 1013 ##__VA_ARGS__) 1014 1015 /** 1016 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal. 1017 * @test: The test context object. 1018 * @left: an arbitrary expression that evaluates to a primitive C type. 1019 * @right: an arbitrary expression that evaluates to a primitive C type. 1020 * 1021 * Sets an expectation that the values that @left and @right evaluate to are not 1022 * equal. This is semantically equivalent to 1023 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for 1024 * more information. 1025 */ 1026 #define KUNIT_EXPECT_NE(test, left, right) \ 1027 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1028 1029 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \ 1030 KUNIT_BINARY_NE_MSG_ASSERTION(test, \ 1031 KUNIT_EXPECTATION, \ 1032 left, \ 1033 right, \ 1034 fmt, \ 1035 ##__VA_ARGS__) 1036 1037 /** 1038 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal. 1039 * @test: The test context object. 1040 * @left: an arbitrary expression that evaluates to a pointer. 1041 * @right: an arbitrary expression that evaluates to a pointer. 1042 * 1043 * Sets an expectation that the values that @left and @right evaluate to are not 1044 * equal. This is semantically equivalent to 1045 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for 1046 * more information. 1047 */ 1048 #define KUNIT_EXPECT_PTR_NE(test, left, right) \ 1049 KUNIT_BINARY_PTR_NE_ASSERTION(test, \ 1050 KUNIT_EXPECTATION, \ 1051 left, \ 1052 right) 1053 1054 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \ 1055 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \ 1056 KUNIT_EXPECTATION, \ 1057 left, \ 1058 right, \ 1059 fmt, \ 1060 ##__VA_ARGS__) 1061 1062 /** 1063 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right. 1064 * @test: The test context object. 1065 * @left: an arbitrary expression that evaluates to a primitive C type. 1066 * @right: an arbitrary expression that evaluates to a primitive C type. 1067 * 1068 * Sets an expectation that the value that @left evaluates to is less than the 1069 * value that @right evaluates to. This is semantically equivalent to 1070 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for 1071 * more information. 1072 */ 1073 #define KUNIT_EXPECT_LT(test, left, right) \ 1074 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1075 1076 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \ 1077 KUNIT_BINARY_LT_MSG_ASSERTION(test, \ 1078 KUNIT_EXPECTATION, \ 1079 left, \ 1080 right, \ 1081 fmt, \ 1082 ##__VA_ARGS__) 1083 1084 /** 1085 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right. 1086 * @test: The test context object. 1087 * @left: an arbitrary expression that evaluates to a primitive C type. 1088 * @right: an arbitrary expression that evaluates to a primitive C type. 1089 * 1090 * Sets an expectation that the value that @left evaluates to is less than or 1091 * equal to the value that @right evaluates to. Semantically this is equivalent 1092 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for 1093 * more information. 1094 */ 1095 #define KUNIT_EXPECT_LE(test, left, right) \ 1096 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1097 1098 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \ 1099 KUNIT_BINARY_LE_MSG_ASSERTION(test, \ 1100 KUNIT_EXPECTATION, \ 1101 left, \ 1102 right, \ 1103 fmt, \ 1104 ##__VA_ARGS__) 1105 1106 /** 1107 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right. 1108 * @test: The test context object. 1109 * @left: an arbitrary expression that evaluates to a primitive C type. 1110 * @right: an arbitrary expression that evaluates to a primitive C type. 1111 * 1112 * Sets an expectation that the value that @left evaluates to is greater than 1113 * the value that @right evaluates to. This is semantically equivalent to 1114 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for 1115 * more information. 1116 */ 1117 #define KUNIT_EXPECT_GT(test, left, right) \ 1118 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1119 1120 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \ 1121 KUNIT_BINARY_GT_MSG_ASSERTION(test, \ 1122 KUNIT_EXPECTATION, \ 1123 left, \ 1124 right, \ 1125 fmt, \ 1126 ##__VA_ARGS__) 1127 1128 /** 1129 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right. 1130 * @test: The test context object. 1131 * @left: an arbitrary expression that evaluates to a primitive C type. 1132 * @right: an arbitrary expression that evaluates to a primitive C type. 1133 * 1134 * Sets an expectation that the value that @left evaluates to is greater than 1135 * the value that @right evaluates to. This is semantically equivalent to 1136 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for 1137 * more information. 1138 */ 1139 #define KUNIT_EXPECT_GE(test, left, right) \ 1140 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1141 1142 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \ 1143 KUNIT_BINARY_GE_MSG_ASSERTION(test, \ 1144 KUNIT_EXPECTATION, \ 1145 left, \ 1146 right, \ 1147 fmt, \ 1148 ##__VA_ARGS__) 1149 1150 /** 1151 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal. 1152 * @test: The test context object. 1153 * @left: an arbitrary expression that evaluates to a null terminated string. 1154 * @right: an arbitrary expression that evaluates to a null terminated string. 1155 * 1156 * Sets an expectation that the values that @left and @right evaluate to are 1157 * equal. This is semantically equivalent to 1158 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE() 1159 * for more information. 1160 */ 1161 #define KUNIT_EXPECT_STREQ(test, left, right) \ 1162 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1163 1164 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \ 1165 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \ 1166 KUNIT_EXPECTATION, \ 1167 left, \ 1168 right, \ 1169 fmt, \ 1170 ##__VA_ARGS__) 1171 1172 /** 1173 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal. 1174 * @test: The test context object. 1175 * @left: an arbitrary expression that evaluates to a null terminated string. 1176 * @right: an arbitrary expression that evaluates to a null terminated string. 1177 * 1178 * Sets an expectation that the values that @left and @right evaluate to are 1179 * not equal. This is semantically equivalent to 1180 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE() 1181 * for more information. 1182 */ 1183 #define KUNIT_EXPECT_STRNEQ(test, left, right) \ 1184 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right) 1185 1186 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \ 1187 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \ 1188 KUNIT_EXPECTATION, \ 1189 left, \ 1190 right, \ 1191 fmt, \ 1192 ##__VA_ARGS__) 1193 1194 /** 1195 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err. 1196 * @test: The test context object. 1197 * @ptr: an arbitrary pointer. 1198 * 1199 * Sets an expectation that the value that @ptr evaluates to is not null and not 1200 * an errno stored in a pointer. This is semantically equivalent to 1201 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for 1202 * more information. 1203 */ 1204 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \ 1205 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_EXPECTATION, ptr) 1206 1207 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \ 1208 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 1209 KUNIT_EXPECTATION, \ 1210 ptr, \ 1211 fmt, \ 1212 ##__VA_ARGS__) 1213 1214 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \ 1215 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__) 1216 1217 /** 1218 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true. 1219 * @test: The test context object. 1220 * @condition: an arbitrary boolean expression. The test fails and aborts when 1221 * this does not evaluate to true. 1222 * 1223 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to 1224 * fail *and immediately abort* when the specified condition is not met. Unlike 1225 * an expectation failure, it will prevent the test case from continuing to run; 1226 * this is otherwise known as an *assertion failure*. 1227 */ 1228 #define KUNIT_ASSERT_TRUE(test, condition) \ 1229 KUNIT_TRUE_ASSERTION(test, KUNIT_ASSERTION, condition) 1230 1231 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \ 1232 KUNIT_TRUE_MSG_ASSERTION(test, \ 1233 KUNIT_ASSERTION, \ 1234 condition, \ 1235 fmt, \ 1236 ##__VA_ARGS__) 1237 1238 /** 1239 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false. 1240 * @test: The test context object. 1241 * @condition: an arbitrary boolean expression. 1242 * 1243 * Sets an assertion that the value that @condition evaluates to is false. This 1244 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure 1245 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1246 */ 1247 #define KUNIT_ASSERT_FALSE(test, condition) \ 1248 KUNIT_FALSE_ASSERTION(test, KUNIT_ASSERTION, condition) 1249 1250 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \ 1251 KUNIT_FALSE_MSG_ASSERTION(test, \ 1252 KUNIT_ASSERTION, \ 1253 condition, \ 1254 fmt, \ 1255 ##__VA_ARGS__) 1256 1257 /** 1258 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal. 1259 * @test: The test context object. 1260 * @left: an arbitrary expression that evaluates to a primitive C type. 1261 * @right: an arbitrary expression that evaluates to a primitive C type. 1262 * 1263 * Sets an assertion that the values that @left and @right evaluate to are 1264 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion 1265 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1266 */ 1267 #define KUNIT_ASSERT_EQ(test, left, right) \ 1268 KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right) 1269 1270 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \ 1271 KUNIT_BINARY_EQ_MSG_ASSERTION(test, \ 1272 KUNIT_ASSERTION, \ 1273 left, \ 1274 right, \ 1275 fmt, \ 1276 ##__VA_ARGS__) 1277 1278 /** 1279 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal. 1280 * @test: The test context object. 1281 * @left: an arbitrary expression that evaluates to a pointer. 1282 * @right: an arbitrary expression that evaluates to a pointer. 1283 * 1284 * Sets an assertion that the values that @left and @right evaluate to are 1285 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion 1286 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1287 */ 1288 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \ 1289 KUNIT_BINARY_PTR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right) 1290 1291 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \ 1292 KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \ 1293 KUNIT_ASSERTION, \ 1294 left, \ 1295 right, \ 1296 fmt, \ 1297 ##__VA_ARGS__) 1298 1299 /** 1300 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal. 1301 * @test: The test context object. 1302 * @left: an arbitrary expression that evaluates to a primitive C type. 1303 * @right: an arbitrary expression that evaluates to a primitive C type. 1304 * 1305 * Sets an assertion that the values that @left and @right evaluate to are not 1306 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion 1307 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1308 */ 1309 #define KUNIT_ASSERT_NE(test, left, right) \ 1310 KUNIT_BINARY_NE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1311 1312 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \ 1313 KUNIT_BINARY_NE_MSG_ASSERTION(test, \ 1314 KUNIT_ASSERTION, \ 1315 left, \ 1316 right, \ 1317 fmt, \ 1318 ##__VA_ARGS__) 1319 1320 /** 1321 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal. 1322 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal. 1323 * @test: The test context object. 1324 * @left: an arbitrary expression that evaluates to a pointer. 1325 * @right: an arbitrary expression that evaluates to a pointer. 1326 * 1327 * Sets an assertion that the values that @left and @right evaluate to are not 1328 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion 1329 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1330 */ 1331 #define KUNIT_ASSERT_PTR_NE(test, left, right) \ 1332 KUNIT_BINARY_PTR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1333 1334 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \ 1335 KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \ 1336 KUNIT_ASSERTION, \ 1337 left, \ 1338 right, \ 1339 fmt, \ 1340 ##__VA_ARGS__) 1341 /** 1342 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right. 1343 * @test: The test context object. 1344 * @left: an arbitrary expression that evaluates to a primitive C type. 1345 * @right: an arbitrary expression that evaluates to a primitive C type. 1346 * 1347 * Sets an assertion that the value that @left evaluates to is less than the 1348 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except 1349 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1350 * is not met. 1351 */ 1352 #define KUNIT_ASSERT_LT(test, left, right) \ 1353 KUNIT_BINARY_LT_ASSERTION(test, KUNIT_ASSERTION, left, right) 1354 1355 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \ 1356 KUNIT_BINARY_LT_MSG_ASSERTION(test, \ 1357 KUNIT_ASSERTION, \ 1358 left, \ 1359 right, \ 1360 fmt, \ 1361 ##__VA_ARGS__) 1362 /** 1363 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right. 1364 * @test: The test context object. 1365 * @left: an arbitrary expression that evaluates to a primitive C type. 1366 * @right: an arbitrary expression that evaluates to a primitive C type. 1367 * 1368 * Sets an assertion that the value that @left evaluates to is less than or 1369 * equal to the value that @right evaluates to. This is the same as 1370 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see 1371 * KUNIT_ASSERT_TRUE()) when the assertion is not met. 1372 */ 1373 #define KUNIT_ASSERT_LE(test, left, right) \ 1374 KUNIT_BINARY_LE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1375 1376 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \ 1377 KUNIT_BINARY_LE_MSG_ASSERTION(test, \ 1378 KUNIT_ASSERTION, \ 1379 left, \ 1380 right, \ 1381 fmt, \ 1382 ##__VA_ARGS__) 1383 1384 /** 1385 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right. 1386 * @test: The test context object. 1387 * @left: an arbitrary expression that evaluates to a primitive C type. 1388 * @right: an arbitrary expression that evaluates to a primitive C type. 1389 * 1390 * Sets an assertion that the value that @left evaluates to is greater than the 1391 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except 1392 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1393 * is not met. 1394 */ 1395 #define KUNIT_ASSERT_GT(test, left, right) \ 1396 KUNIT_BINARY_GT_ASSERTION(test, KUNIT_ASSERTION, left, right) 1397 1398 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \ 1399 KUNIT_BINARY_GT_MSG_ASSERTION(test, \ 1400 KUNIT_ASSERTION, \ 1401 left, \ 1402 right, \ 1403 fmt, \ 1404 ##__VA_ARGS__) 1405 1406 /** 1407 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right. 1408 * @test: The test context object. 1409 * @left: an arbitrary expression that evaluates to a primitive C type. 1410 * @right: an arbitrary expression that evaluates to a primitive C type. 1411 * 1412 * Sets an assertion that the value that @left evaluates to is greater than the 1413 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except 1414 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1415 * is not met. 1416 */ 1417 #define KUNIT_ASSERT_GE(test, left, right) \ 1418 KUNIT_BINARY_GE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1419 1420 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \ 1421 KUNIT_BINARY_GE_MSG_ASSERTION(test, \ 1422 KUNIT_ASSERTION, \ 1423 left, \ 1424 right, \ 1425 fmt, \ 1426 ##__VA_ARGS__) 1427 1428 /** 1429 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal. 1430 * @test: The test context object. 1431 * @left: an arbitrary expression that evaluates to a null terminated string. 1432 * @right: an arbitrary expression that evaluates to a null terminated string. 1433 * 1434 * Sets an assertion that the values that @left and @right evaluate to are 1435 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an 1436 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1437 */ 1438 #define KUNIT_ASSERT_STREQ(test, left, right) \ 1439 KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right) 1440 1441 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \ 1442 KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \ 1443 KUNIT_ASSERTION, \ 1444 left, \ 1445 right, \ 1446 fmt, \ 1447 ##__VA_ARGS__) 1448 1449 /** 1450 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal. 1451 * @test: The test context object. 1452 * @left: an arbitrary expression that evaluates to a null terminated string. 1453 * @right: an arbitrary expression that evaluates to a null terminated string. 1454 * 1455 * Sets an expectation that the values that @left and @right evaluate to are 1456 * not equal. This is semantically equivalent to 1457 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE() 1458 * for more information. 1459 */ 1460 #define KUNIT_ASSERT_STRNEQ(test, left, right) \ 1461 KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right) 1462 1463 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \ 1464 KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \ 1465 KUNIT_ASSERTION, \ 1466 left, \ 1467 right, \ 1468 fmt, \ 1469 ##__VA_ARGS__) 1470 1471 /** 1472 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err. 1473 * @test: The test context object. 1474 * @ptr: an arbitrary pointer. 1475 * 1476 * Sets an assertion that the value that @ptr evaluates to is not null and not 1477 * an errno stored in a pointer. This is the same as 1478 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see 1479 * KUNIT_ASSERT_TRUE()) when the assertion is not met. 1480 */ 1481 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \ 1482 KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_ASSERTION, ptr) 1483 1484 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \ 1485 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 1486 KUNIT_ASSERTION, \ 1487 ptr, \ 1488 fmt, \ 1489 ##__VA_ARGS__) 1490 1491 #endif /* _KUNIT_TEST_H */ 1492