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