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