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 15 #include <linux/args.h> 16 #include <linux/compiler.h> 17 #include <linux/container_of.h> 18 #include <linux/err.h> 19 #include <linux/init.h> 20 #include <linux/jump_label.h> 21 #include <linux/kconfig.h> 22 #include <linux/kref.h> 23 #include <linux/list.h> 24 #include <linux/module.h> 25 #include <linux/slab.h> 26 #include <linux/spinlock.h> 27 #include <linux/string.h> 28 #include <linux/types.h> 29 30 #include <asm/rwonce.h> 31 32 /* Static key: true if any KUnit tests are currently running */ 33 DECLARE_STATIC_KEY_FALSE(kunit_running); 34 35 struct kunit; 36 37 /* Size of log associated with test. */ 38 #define KUNIT_LOG_SIZE 2048 39 40 /* Maximum size of parameter description string. */ 41 #define KUNIT_PARAM_DESC_SIZE 128 42 43 /* Maximum size of a status comment. */ 44 #define KUNIT_STATUS_COMMENT_SIZE 256 45 46 /* 47 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a 48 * sub-subtest. See the "Subtests" section in 49 * https://node-tap.org/tap-protocol/ 50 */ 51 #define KUNIT_INDENT_LEN 4 52 #define KUNIT_SUBTEST_INDENT " " 53 #define KUNIT_SUBSUBTEST_INDENT " " 54 55 /** 56 * enum kunit_status - Type of result for a test or test suite 57 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped 58 * @KUNIT_FAILURE: Denotes the test has failed. 59 * @KUNIT_SKIPPED: Denotes the test has been skipped. 60 */ 61 enum kunit_status { 62 KUNIT_SUCCESS, 63 KUNIT_FAILURE, 64 KUNIT_SKIPPED, 65 }; 66 67 /** 68 * struct kunit_case - represents an individual test case. 69 * 70 * @run_case: the function representing the actual test case. 71 * @name: the name of the test case. 72 * @generate_params: the generator function for parameterized tests. 73 * 74 * A test case is a function with the signature, 75 * ``void (*)(struct kunit *)`` 76 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and 77 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated 78 * with a &struct kunit_suite and will be run after the suite's init 79 * function and followed by the suite's exit function. 80 * 81 * A test case should be static and should only be created with the 82 * KUNIT_CASE() macro; additionally, every array of test cases should be 83 * terminated with an empty test case. 84 * 85 * Example: 86 * 87 * .. code-block:: c 88 * 89 * void add_test_basic(struct kunit *test) 90 * { 91 * KUNIT_EXPECT_EQ(test, 1, add(1, 0)); 92 * KUNIT_EXPECT_EQ(test, 2, add(1, 1)); 93 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1)); 94 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX)); 95 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN)); 96 * } 97 * 98 * static struct kunit_case example_test_cases[] = { 99 * KUNIT_CASE(add_test_basic), 100 * {} 101 * }; 102 * 103 */ 104 struct kunit_case { 105 void (*run_case)(struct kunit *test); 106 const char *name; 107 const void* (*generate_params)(const void *prev, char *desc); 108 109 /* private: internal use only. */ 110 enum kunit_status status; 111 char *log; 112 }; 113 114 static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) 115 { 116 switch (status) { 117 case KUNIT_SKIPPED: 118 case KUNIT_SUCCESS: 119 return "ok"; 120 case KUNIT_FAILURE: 121 return "not ok"; 122 } 123 return "invalid"; 124 } 125 126 /** 127 * KUNIT_CASE - A helper for creating a &struct kunit_case 128 * 129 * @test_name: a reference to a test case function. 130 * 131 * Takes a symbol for a function representing a test case and creates a 132 * &struct kunit_case object from it. See the documentation for 133 * &struct kunit_case for an example on how to use it. 134 */ 135 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name } 136 137 /** 138 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case 139 * 140 * @test_name: a reference to a test case function. 141 * @gen_params: a reference to a parameter generator function. 142 * 143 * The generator function:: 144 * 145 * const void* gen_params(const void *prev, char *desc) 146 * 147 * is used to lazily generate a series of arbitrarily typed values that fit into 148 * a void*. The argument @prev is the previously returned value, which should be 149 * used to derive the next value; @prev is set to NULL on the initial generator 150 * call. When no more values are available, the generator must return NULL. 151 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE) 152 * describing the parameter. 153 */ 154 #define KUNIT_CASE_PARAM(test_name, gen_params) \ 155 { .run_case = test_name, .name = #test_name, \ 156 .generate_params = gen_params } 157 158 /** 159 * struct kunit_suite - describes a related collection of &struct kunit_case 160 * 161 * @name: the name of the test. Purely informational. 162 * @suite_init: called once per test suite before the test cases. 163 * @suite_exit: called once per test suite after all test cases. 164 * @init: called before every test case. 165 * @exit: called after every test case. 166 * @test_cases: a null terminated array of test cases. 167 * 168 * A kunit_suite is a collection of related &struct kunit_case s, such that 169 * @init is called before every test case and @exit is called after every 170 * test case, similar to the notion of a *test fixture* or a *test class* 171 * in other unit testing frameworks like JUnit or Googletest. 172 * 173 * Note that @exit and @suite_exit will run even if @init or @suite_init 174 * fail: make sure they can handle any inconsistent state which may result. 175 * 176 * Every &struct kunit_case must be associated with a kunit_suite for KUnit 177 * to run it. 178 */ 179 struct kunit_suite { 180 const char name[256]; 181 int (*suite_init)(struct kunit_suite *suite); 182 void (*suite_exit)(struct kunit_suite *suite); 183 int (*init)(struct kunit *test); 184 void (*exit)(struct kunit *test); 185 struct kunit_case *test_cases; 186 187 /* private: internal use only */ 188 char status_comment[KUNIT_STATUS_COMMENT_SIZE]; 189 struct dentry *debugfs; 190 char *log; 191 int suite_init_err; 192 }; 193 194 /** 195 * struct kunit - represents a running instance of a test. 196 * 197 * @priv: for user to store arbitrary data. Commonly used to pass data 198 * created in the init function (see &struct kunit_suite). 199 * 200 * Used to store information about the current context under which the test 201 * is running. Most of this data is private and should only be accessed 202 * indirectly via public functions; the one exception is @priv which can be 203 * used by the test writer to store arbitrary data. 204 */ 205 struct kunit { 206 void *priv; 207 208 /* private: internal use only. */ 209 const char *name; /* Read only after initialization! */ 210 char *log; /* Points at case log after initialization */ 211 struct kunit_try_catch try_catch; 212 /* param_value is the current parameter value for a test case. */ 213 const void *param_value; 214 /* param_index stores the index of the parameter in parameterized tests. */ 215 int param_index; 216 /* 217 * success starts as true, and may only be set to false during a 218 * test case; thus, it is safe to update this across multiple 219 * threads using WRITE_ONCE; however, as a consequence, it may only 220 * be read after the test case finishes once all threads associated 221 * with the test case have terminated. 222 */ 223 spinlock_t lock; /* Guards all mutable test state. */ 224 enum kunit_status status; /* Read only after test_case finishes! */ 225 /* 226 * Because resources is a list that may be updated multiple times (with 227 * new resources) from any thread associated with a test case, we must 228 * protect it with some type of lock. 229 */ 230 struct list_head resources; /* Protected by lock. */ 231 232 char status_comment[KUNIT_STATUS_COMMENT_SIZE]; 233 }; 234 235 static inline void kunit_set_failure(struct kunit *test) 236 { 237 WRITE_ONCE(test->status, KUNIT_FAILURE); 238 } 239 240 bool kunit_enabled(void); 241 242 void kunit_init_test(struct kunit *test, const char *name, char *log); 243 244 int kunit_run_tests(struct kunit_suite *suite); 245 246 size_t kunit_suite_num_test_cases(struct kunit_suite *suite); 247 248 unsigned int kunit_test_case_num(struct kunit_suite *suite, 249 struct kunit_case *test_case); 250 251 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites); 252 253 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites); 254 255 #if IS_BUILTIN(CONFIG_KUNIT) 256 int kunit_run_all_tests(void); 257 #else 258 static inline int kunit_run_all_tests(void) 259 { 260 return 0; 261 } 262 #endif /* IS_BUILTIN(CONFIG_KUNIT) */ 263 264 #define __kunit_test_suites(unique_array, ...) \ 265 static struct kunit_suite *unique_array[] \ 266 __aligned(sizeof(struct kunit_suite *)) \ 267 __used __section(".kunit_test_suites") = { __VA_ARGS__ } 268 269 /** 270 * kunit_test_suites() - used to register one or more &struct kunit_suite 271 * with KUnit. 272 * 273 * @__suites: a statically allocated list of &struct kunit_suite. 274 * 275 * Registers @suites with the test framework. 276 * This is done by placing the array of struct kunit_suite * in the 277 * .kunit_test_suites ELF section. 278 * 279 * When builtin, KUnit tests are all run via the executor at boot, and when 280 * built as a module, they run on module load. 281 * 282 */ 283 #define kunit_test_suites(__suites...) \ 284 __kunit_test_suites(__UNIQUE_ID(array), \ 285 ##__suites) 286 287 #define kunit_test_suite(suite) kunit_test_suites(&suite) 288 289 /** 290 * kunit_test_init_section_suites() - used to register one or more &struct 291 * kunit_suite containing init functions or 292 * init data. 293 * 294 * @__suites: a statically allocated list of &struct kunit_suite. 295 * 296 * This functions identically as kunit_test_suites() except that it suppresses 297 * modpost warnings for referencing functions marked __init or data marked 298 * __initdata; this is OK because currently KUnit only runs tests upon boot 299 * during the init phase or upon loading a module during the init phase. 300 * 301 * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these 302 * tests must be excluded. 303 * 304 * The only thing this macro does that's different from kunit_test_suites is 305 * that it suffixes the array and suite declarations it makes with _probe; 306 * modpost suppresses warnings about referencing init data for symbols named in 307 * this manner. 308 */ 309 #define kunit_test_init_section_suites(__suites...) \ 310 __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \ 311 ##__suites) 312 313 #define kunit_test_init_section_suite(suite) \ 314 kunit_test_init_section_suites(&suite) 315 316 #define kunit_suite_for_each_test_case(suite, test_case) \ 317 for (test_case = suite->test_cases; test_case->run_case; test_case++) 318 319 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite); 320 321 /** 322 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*. 323 * @test: The test context object. 324 * @n: number of elements. 325 * @size: The size in bytes of the desired memory. 326 * @gfp: flags passed to underlying kmalloc(). 327 * 328 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case 329 * and is automatically cleaned up after the test case concludes. See kunit_add_action() 330 * for more information. 331 * 332 * Note that some internal context data is also allocated with GFP_KERNEL, 333 * regardless of the gfp passed in. 334 */ 335 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp); 336 337 /** 338 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*. 339 * @test: The test context object. 340 * @size: The size in bytes of the desired memory. 341 * @gfp: flags passed to underlying kmalloc(). 342 * 343 * See kmalloc() and kunit_kmalloc_array() for more information. 344 * 345 * Note that some internal context data is also allocated with GFP_KERNEL, 346 * regardless of the gfp passed in. 347 */ 348 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) 349 { 350 return kunit_kmalloc_array(test, 1, size, gfp); 351 } 352 353 /** 354 * kunit_kfree() - Like kfree except for allocations managed by KUnit. 355 * @test: The test case to which the resource belongs. 356 * @ptr: The memory allocation to free. 357 */ 358 void kunit_kfree(struct kunit *test, const void *ptr); 359 360 /** 361 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation. 362 * @test: The test context object. 363 * @size: The size in bytes of the desired memory. 364 * @gfp: flags passed to underlying kmalloc(). 365 * 366 * See kzalloc() and kunit_kmalloc_array() for more information. 367 */ 368 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp) 369 { 370 return kunit_kmalloc(test, size, gfp | __GFP_ZERO); 371 } 372 373 /** 374 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation. 375 * @test: The test context object. 376 * @n: number of elements. 377 * @size: The size in bytes of the desired memory. 378 * @gfp: flags passed to underlying kmalloc(). 379 * 380 * See kcalloc() and kunit_kmalloc_array() for more information. 381 */ 382 static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp) 383 { 384 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO); 385 } 386 387 void kunit_cleanup(struct kunit *test); 388 389 void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...); 390 391 /** 392 * kunit_mark_skipped() - Marks @test_or_suite as skipped 393 * 394 * @test_or_suite: The test context object. 395 * @fmt: A printk() style format string. 396 * 397 * Marks the test as skipped. @fmt is given output as the test status 398 * comment, typically the reason the test was skipped. 399 * 400 * Test execution continues after kunit_mark_skipped() is called. 401 */ 402 #define kunit_mark_skipped(test_or_suite, fmt, ...) \ 403 do { \ 404 WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \ 405 scnprintf((test_or_suite)->status_comment, \ 406 KUNIT_STATUS_COMMENT_SIZE, \ 407 fmt, ##__VA_ARGS__); \ 408 } while (0) 409 410 /** 411 * kunit_skip() - Marks @test_or_suite as skipped 412 * 413 * @test_or_suite: The test context object. 414 * @fmt: A printk() style format string. 415 * 416 * Skips the test. @fmt is given output as the test status 417 * comment, typically the reason the test was skipped. 418 * 419 * Test execution is halted after kunit_skip() is called. 420 */ 421 #define kunit_skip(test_or_suite, fmt, ...) \ 422 do { \ 423 kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\ 424 kunit_try_catch_throw(&((test_or_suite)->try_catch)); \ 425 } while (0) 426 427 /* 428 * printk and log to per-test or per-suite log buffer. Logging only done 429 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used. 430 */ 431 #define kunit_log(lvl, test_or_suite, fmt, ...) \ 432 do { \ 433 printk(lvl fmt, ##__VA_ARGS__); \ 434 kunit_log_append((test_or_suite)->log, fmt, \ 435 ##__VA_ARGS__); \ 436 } while (0) 437 438 #define kunit_printk(lvl, test, fmt, ...) \ 439 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \ 440 (test)->name, ##__VA_ARGS__) 441 442 /** 443 * kunit_info() - Prints an INFO level message associated with @test. 444 * 445 * @test: The test context object. 446 * @fmt: A printk() style format string. 447 * 448 * Prints an info level message associated with the test suite being run. 449 * Takes a variable number of format parameters just like printk(). 450 */ 451 #define kunit_info(test, fmt, ...) \ 452 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) 453 454 /** 455 * kunit_warn() - Prints a WARN level message associated with @test. 456 * 457 * @test: The test context object. 458 * @fmt: A printk() style format string. 459 * 460 * Prints a warning level message. 461 */ 462 #define kunit_warn(test, fmt, ...) \ 463 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__) 464 465 /** 466 * kunit_err() - Prints an ERROR level message associated with @test. 467 * 468 * @test: The test context object. 469 * @fmt: A printk() style format string. 470 * 471 * Prints an error level message. 472 */ 473 #define kunit_err(test, fmt, ...) \ 474 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) 475 476 /** 477 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity. 478 * @test: The test context object. 479 * 480 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other 481 * words, it does nothing and only exists for code clarity. See 482 * KUNIT_EXPECT_TRUE() for more information. 483 */ 484 #define KUNIT_SUCCEED(test) do {} while (0) 485 486 void __noreturn __kunit_abort(struct kunit *test); 487 488 void __kunit_do_failed_assertion(struct kunit *test, 489 const struct kunit_loc *loc, 490 enum kunit_assert_type type, 491 const struct kunit_assert *assert, 492 assert_format_t assert_format, 493 const char *fmt, ...); 494 495 #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \ 496 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \ 497 const struct assert_class __assertion = INITIALIZER; \ 498 __kunit_do_failed_assertion(test, \ 499 &__loc, \ 500 assert_type, \ 501 &__assertion.assert, \ 502 assert_format, \ 503 fmt, \ 504 ##__VA_ARGS__); \ 505 if (assert_type == KUNIT_ASSERTION) \ 506 __kunit_abort(test); \ 507 } while (0) 508 509 510 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \ 511 _KUNIT_FAILED(test, \ 512 assert_type, \ 513 kunit_fail_assert, \ 514 kunit_fail_assert_format, \ 515 {}, \ 516 fmt, \ 517 ##__VA_ARGS__) 518 519 /** 520 * KUNIT_FAIL() - Always causes a test to fail when evaluated. 521 * @test: The test context object. 522 * @fmt: an informational message to be printed when the assertion is made. 523 * @...: string format arguments. 524 * 525 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In 526 * other words, it always results in a failed expectation, and consequently 527 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE() 528 * for more information. 529 */ 530 #define KUNIT_FAIL(test, fmt, ...) \ 531 KUNIT_FAIL_ASSERTION(test, \ 532 KUNIT_EXPECTATION, \ 533 fmt, \ 534 ##__VA_ARGS__) 535 536 /* Helper to safely pass around an initializer list to other macros. */ 537 #define KUNIT_INIT_ASSERT(initializers...) { initializers } 538 539 #define KUNIT_UNARY_ASSERTION(test, \ 540 assert_type, \ 541 condition_, \ 542 expected_true_, \ 543 fmt, \ 544 ...) \ 545 do { \ 546 if (likely(!!(condition_) == !!expected_true_)) \ 547 break; \ 548 \ 549 _KUNIT_FAILED(test, \ 550 assert_type, \ 551 kunit_unary_assert, \ 552 kunit_unary_assert_format, \ 553 KUNIT_INIT_ASSERT(.condition = #condition_, \ 554 .expected_true = expected_true_), \ 555 fmt, \ 556 ##__VA_ARGS__); \ 557 } while (0) 558 559 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \ 560 KUNIT_UNARY_ASSERTION(test, \ 561 assert_type, \ 562 condition, \ 563 true, \ 564 fmt, \ 565 ##__VA_ARGS__) 566 567 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \ 568 KUNIT_UNARY_ASSERTION(test, \ 569 assert_type, \ 570 condition, \ 571 false, \ 572 fmt, \ 573 ##__VA_ARGS__) 574 575 /* 576 * A factory macro for defining the assertions and expectations for the basic 577 * comparisons defined for the built in types. 578 * 579 * Unfortunately, there is no common type that all types can be promoted to for 580 * which all the binary operators behave the same way as for the actual types 581 * (for example, there is no type that long long and unsigned long long can 582 * both be cast to where the comparison result is preserved for all values). So 583 * the best we can do is do the comparison in the original types and then coerce 584 * everything to long long for printing; this way, the comparison behaves 585 * correctly and the printed out value usually makes sense without 586 * interpretation, but can always be interpreted to figure out the actual 587 * value. 588 */ 589 #define KUNIT_BASE_BINARY_ASSERTION(test, \ 590 assert_class, \ 591 format_func, \ 592 assert_type, \ 593 left, \ 594 op, \ 595 right, \ 596 fmt, \ 597 ...) \ 598 do { \ 599 const typeof(left) __left = (left); \ 600 const typeof(right) __right = (right); \ 601 static const struct kunit_binary_assert_text __text = { \ 602 .operation = #op, \ 603 .left_text = #left, \ 604 .right_text = #right, \ 605 }; \ 606 \ 607 if (likely(__left op __right)) \ 608 break; \ 609 \ 610 _KUNIT_FAILED(test, \ 611 assert_type, \ 612 assert_class, \ 613 format_func, \ 614 KUNIT_INIT_ASSERT(.text = &__text, \ 615 .left_value = __left, \ 616 .right_value = __right), \ 617 fmt, \ 618 ##__VA_ARGS__); \ 619 } while (0) 620 621 #define KUNIT_BINARY_INT_ASSERTION(test, \ 622 assert_type, \ 623 left, \ 624 op, \ 625 right, \ 626 fmt, \ 627 ...) \ 628 KUNIT_BASE_BINARY_ASSERTION(test, \ 629 kunit_binary_assert, \ 630 kunit_binary_assert_format, \ 631 assert_type, \ 632 left, op, right, \ 633 fmt, \ 634 ##__VA_ARGS__) 635 636 #define KUNIT_BINARY_PTR_ASSERTION(test, \ 637 assert_type, \ 638 left, \ 639 op, \ 640 right, \ 641 fmt, \ 642 ...) \ 643 KUNIT_BASE_BINARY_ASSERTION(test, \ 644 kunit_binary_ptr_assert, \ 645 kunit_binary_ptr_assert_format, \ 646 assert_type, \ 647 left, op, right, \ 648 fmt, \ 649 ##__VA_ARGS__) 650 651 #define KUNIT_BINARY_STR_ASSERTION(test, \ 652 assert_type, \ 653 left, \ 654 op, \ 655 right, \ 656 fmt, \ 657 ...) \ 658 do { \ 659 const char *__left = (left); \ 660 const char *__right = (right); \ 661 static const struct kunit_binary_assert_text __text = { \ 662 .operation = #op, \ 663 .left_text = #left, \ 664 .right_text = #right, \ 665 }; \ 666 \ 667 if (likely(strcmp(__left, __right) op 0)) \ 668 break; \ 669 \ 670 \ 671 _KUNIT_FAILED(test, \ 672 assert_type, \ 673 kunit_binary_str_assert, \ 674 kunit_binary_str_assert_format, \ 675 KUNIT_INIT_ASSERT(.text = &__text, \ 676 .left_value = __left, \ 677 .right_value = __right), \ 678 fmt, \ 679 ##__VA_ARGS__); \ 680 } while (0) 681 682 #define KUNIT_MEM_ASSERTION(test, \ 683 assert_type, \ 684 left, \ 685 op, \ 686 right, \ 687 size_, \ 688 fmt, \ 689 ...) \ 690 do { \ 691 const void *__left = (left); \ 692 const void *__right = (right); \ 693 const size_t __size = (size_); \ 694 static const struct kunit_binary_assert_text __text = { \ 695 .operation = #op, \ 696 .left_text = #left, \ 697 .right_text = #right, \ 698 }; \ 699 \ 700 if (likely(__left && __right)) \ 701 if (likely(memcmp(__left, __right, __size) op 0)) \ 702 break; \ 703 \ 704 _KUNIT_FAILED(test, \ 705 assert_type, \ 706 kunit_mem_assert, \ 707 kunit_mem_assert_format, \ 708 KUNIT_INIT_ASSERT(.text = &__text, \ 709 .left_value = __left, \ 710 .right_value = __right, \ 711 .size = __size), \ 712 fmt, \ 713 ##__VA_ARGS__); \ 714 } while (0) 715 716 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 717 assert_type, \ 718 ptr, \ 719 fmt, \ 720 ...) \ 721 do { \ 722 const typeof(ptr) __ptr = (ptr); \ 723 \ 724 if (!IS_ERR_OR_NULL(__ptr)) \ 725 break; \ 726 \ 727 _KUNIT_FAILED(test, \ 728 assert_type, \ 729 kunit_ptr_not_err_assert, \ 730 kunit_ptr_not_err_assert_format, \ 731 KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \ 732 fmt, \ 733 ##__VA_ARGS__); \ 734 } while (0) 735 736 /** 737 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true. 738 * @test: The test context object. 739 * @condition: an arbitrary boolean expression. The test fails when this does 740 * not evaluate to true. 741 * 742 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case 743 * to fail when the specified condition is not met; however, it will not prevent 744 * the test case from continuing to run; this is otherwise known as an 745 * *expectation failure*. 746 */ 747 #define KUNIT_EXPECT_TRUE(test, condition) \ 748 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL) 749 750 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \ 751 KUNIT_TRUE_MSG_ASSERTION(test, \ 752 KUNIT_EXPECTATION, \ 753 condition, \ 754 fmt, \ 755 ##__VA_ARGS__) 756 757 /** 758 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false. 759 * @test: The test context object. 760 * @condition: an arbitrary boolean expression. The test fails when this does 761 * not evaluate to false. 762 * 763 * Sets an expectation that @condition evaluates to false. See 764 * KUNIT_EXPECT_TRUE() for more information. 765 */ 766 #define KUNIT_EXPECT_FALSE(test, condition) \ 767 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL) 768 769 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \ 770 KUNIT_FALSE_MSG_ASSERTION(test, \ 771 KUNIT_EXPECTATION, \ 772 condition, \ 773 fmt, \ 774 ##__VA_ARGS__) 775 776 /** 777 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal. 778 * @test: The test context object. 779 * @left: an arbitrary expression that evaluates to a primitive C type. 780 * @right: an arbitrary expression that evaluates to a primitive C type. 781 * 782 * Sets an expectation that the values that @left and @right evaluate to are 783 * equal. This is semantically equivalent to 784 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for 785 * more information. 786 */ 787 #define KUNIT_EXPECT_EQ(test, left, right) \ 788 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL) 789 790 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \ 791 KUNIT_BINARY_INT_ASSERTION(test, \ 792 KUNIT_EXPECTATION, \ 793 left, ==, right, \ 794 fmt, \ 795 ##__VA_ARGS__) 796 797 /** 798 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal. 799 * @test: The test context object. 800 * @left: an arbitrary expression that evaluates to a pointer. 801 * @right: an arbitrary expression that evaluates to a pointer. 802 * 803 * Sets an expectation that the values that @left and @right evaluate to are 804 * equal. This is semantically equivalent to 805 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for 806 * more information. 807 */ 808 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \ 809 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL) 810 811 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \ 812 KUNIT_BINARY_PTR_ASSERTION(test, \ 813 KUNIT_EXPECTATION, \ 814 left, ==, right, \ 815 fmt, \ 816 ##__VA_ARGS__) 817 818 /** 819 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal. 820 * @test: The test context object. 821 * @left: an arbitrary expression that evaluates to a primitive C type. 822 * @right: an arbitrary expression that evaluates to a primitive C type. 823 * 824 * Sets an expectation that the values that @left and @right evaluate to are not 825 * equal. This is semantically equivalent to 826 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for 827 * more information. 828 */ 829 #define KUNIT_EXPECT_NE(test, left, right) \ 830 KUNIT_EXPECT_NE_MSG(test, left, right, NULL) 831 832 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \ 833 KUNIT_BINARY_INT_ASSERTION(test, \ 834 KUNIT_EXPECTATION, \ 835 left, !=, right, \ 836 fmt, \ 837 ##__VA_ARGS__) 838 839 /** 840 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal. 841 * @test: The test context object. 842 * @left: an arbitrary expression that evaluates to a pointer. 843 * @right: an arbitrary expression that evaluates to a pointer. 844 * 845 * Sets an expectation that the values that @left and @right evaluate to are not 846 * equal. This is semantically equivalent to 847 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for 848 * more information. 849 */ 850 #define KUNIT_EXPECT_PTR_NE(test, left, right) \ 851 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL) 852 853 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \ 854 KUNIT_BINARY_PTR_ASSERTION(test, \ 855 KUNIT_EXPECTATION, \ 856 left, !=, right, \ 857 fmt, \ 858 ##__VA_ARGS__) 859 860 /** 861 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right. 862 * @test: The test context object. 863 * @left: an arbitrary expression that evaluates to a primitive C type. 864 * @right: an arbitrary expression that evaluates to a primitive C type. 865 * 866 * Sets an expectation that the value that @left evaluates to is less than the 867 * value that @right evaluates to. This is semantically equivalent to 868 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for 869 * more information. 870 */ 871 #define KUNIT_EXPECT_LT(test, left, right) \ 872 KUNIT_EXPECT_LT_MSG(test, left, right, NULL) 873 874 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \ 875 KUNIT_BINARY_INT_ASSERTION(test, \ 876 KUNIT_EXPECTATION, \ 877 left, <, right, \ 878 fmt, \ 879 ##__VA_ARGS__) 880 881 /** 882 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right. 883 * @test: The test context object. 884 * @left: an arbitrary expression that evaluates to a primitive C type. 885 * @right: an arbitrary expression that evaluates to a primitive C type. 886 * 887 * Sets an expectation that the value that @left evaluates to is less than or 888 * equal to the value that @right evaluates to. Semantically this is equivalent 889 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for 890 * more information. 891 */ 892 #define KUNIT_EXPECT_LE(test, left, right) \ 893 KUNIT_EXPECT_LE_MSG(test, left, right, NULL) 894 895 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \ 896 KUNIT_BINARY_INT_ASSERTION(test, \ 897 KUNIT_EXPECTATION, \ 898 left, <=, right, \ 899 fmt, \ 900 ##__VA_ARGS__) 901 902 /** 903 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right. 904 * @test: The test context object. 905 * @left: an arbitrary expression that evaluates to a primitive C type. 906 * @right: an arbitrary expression that evaluates to a primitive C type. 907 * 908 * Sets an expectation that the value that @left evaluates to is greater than 909 * the value that @right evaluates to. This is semantically equivalent to 910 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for 911 * more information. 912 */ 913 #define KUNIT_EXPECT_GT(test, left, right) \ 914 KUNIT_EXPECT_GT_MSG(test, left, right, NULL) 915 916 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \ 917 KUNIT_BINARY_INT_ASSERTION(test, \ 918 KUNIT_EXPECTATION, \ 919 left, >, right, \ 920 fmt, \ 921 ##__VA_ARGS__) 922 923 /** 924 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right. 925 * @test: The test context object. 926 * @left: an arbitrary expression that evaluates to a primitive C type. 927 * @right: an arbitrary expression that evaluates to a primitive C type. 928 * 929 * Sets an expectation that the value that @left evaluates to is greater than 930 * the value that @right evaluates to. This is semantically equivalent to 931 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for 932 * more information. 933 */ 934 #define KUNIT_EXPECT_GE(test, left, right) \ 935 KUNIT_EXPECT_GE_MSG(test, left, right, NULL) 936 937 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \ 938 KUNIT_BINARY_INT_ASSERTION(test, \ 939 KUNIT_EXPECTATION, \ 940 left, >=, right, \ 941 fmt, \ 942 ##__VA_ARGS__) 943 944 /** 945 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal. 946 * @test: The test context object. 947 * @left: an arbitrary expression that evaluates to a null terminated string. 948 * @right: an arbitrary expression that evaluates to a null terminated string. 949 * 950 * Sets an expectation that the values that @left and @right evaluate to are 951 * equal. This is semantically equivalent to 952 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE() 953 * for more information. 954 */ 955 #define KUNIT_EXPECT_STREQ(test, left, right) \ 956 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL) 957 958 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \ 959 KUNIT_BINARY_STR_ASSERTION(test, \ 960 KUNIT_EXPECTATION, \ 961 left, ==, right, \ 962 fmt, \ 963 ##__VA_ARGS__) 964 965 /** 966 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal. 967 * @test: The test context object. 968 * @left: an arbitrary expression that evaluates to a null terminated string. 969 * @right: an arbitrary expression that evaluates to a null terminated string. 970 * 971 * Sets an expectation that the values that @left and @right evaluate to are 972 * not equal. This is semantically equivalent to 973 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE() 974 * for more information. 975 */ 976 #define KUNIT_EXPECT_STRNEQ(test, left, right) \ 977 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL) 978 979 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \ 980 KUNIT_BINARY_STR_ASSERTION(test, \ 981 KUNIT_EXPECTATION, \ 982 left, !=, right, \ 983 fmt, \ 984 ##__VA_ARGS__) 985 986 /** 987 * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal. 988 * @test: The test context object. 989 * @left: An arbitrary expression that evaluates to the specified size. 990 * @right: An arbitrary expression that evaluates to the specified size. 991 * @size: Number of bytes compared. 992 * 993 * Sets an expectation that the values that @left and @right evaluate to are 994 * equal. This is semantically equivalent to 995 * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See 996 * KUNIT_EXPECT_TRUE() for more information. 997 * 998 * Although this expectation works for any memory block, it is not recommended 999 * for comparing more structured data, such as structs. This expectation is 1000 * recommended for comparing, for example, data arrays. 1001 */ 1002 #define KUNIT_EXPECT_MEMEQ(test, left, right, size) \ 1003 KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL) 1004 1005 #define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \ 1006 KUNIT_MEM_ASSERTION(test, \ 1007 KUNIT_EXPECTATION, \ 1008 left, ==, right, \ 1009 size, \ 1010 fmt, \ 1011 ##__VA_ARGS__) 1012 1013 /** 1014 * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal. 1015 * @test: The test context object. 1016 * @left: An arbitrary expression that evaluates to the specified size. 1017 * @right: An arbitrary expression that evaluates to the specified size. 1018 * @size: Number of bytes compared. 1019 * 1020 * Sets an expectation that the values that @left and @right evaluate to are 1021 * not equal. This is semantically equivalent to 1022 * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See 1023 * KUNIT_EXPECT_TRUE() for more information. 1024 * 1025 * Although this expectation works for any memory block, it is not recommended 1026 * for comparing more structured data, such as structs. This expectation is 1027 * recommended for comparing, for example, data arrays. 1028 */ 1029 #define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \ 1030 KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL) 1031 1032 #define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \ 1033 KUNIT_MEM_ASSERTION(test, \ 1034 KUNIT_EXPECTATION, \ 1035 left, !=, right, \ 1036 size, \ 1037 fmt, \ 1038 ##__VA_ARGS__) 1039 1040 /** 1041 * KUNIT_EXPECT_NULL() - Expects that @ptr is null. 1042 * @test: The test context object. 1043 * @ptr: an arbitrary pointer. 1044 * 1045 * Sets an expectation that the value that @ptr evaluates to is null. This is 1046 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL). 1047 * See KUNIT_EXPECT_TRUE() for more information. 1048 */ 1049 #define KUNIT_EXPECT_NULL(test, ptr) \ 1050 KUNIT_EXPECT_NULL_MSG(test, \ 1051 ptr, \ 1052 NULL) 1053 1054 #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \ 1055 KUNIT_BINARY_PTR_ASSERTION(test, \ 1056 KUNIT_EXPECTATION, \ 1057 ptr, ==, NULL, \ 1058 fmt, \ 1059 ##__VA_ARGS__) 1060 1061 /** 1062 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null. 1063 * @test: The test context object. 1064 * @ptr: an arbitrary pointer. 1065 * 1066 * Sets an expectation that the value that @ptr evaluates to is not null. This 1067 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL). 1068 * See KUNIT_EXPECT_TRUE() for more information. 1069 */ 1070 #define KUNIT_EXPECT_NOT_NULL(test, ptr) \ 1071 KUNIT_EXPECT_NOT_NULL_MSG(test, \ 1072 ptr, \ 1073 NULL) 1074 1075 #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \ 1076 KUNIT_BINARY_PTR_ASSERTION(test, \ 1077 KUNIT_EXPECTATION, \ 1078 ptr, !=, NULL, \ 1079 fmt, \ 1080 ##__VA_ARGS__) 1081 1082 /** 1083 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err. 1084 * @test: The test context object. 1085 * @ptr: an arbitrary pointer. 1086 * 1087 * Sets an expectation that the value that @ptr evaluates to is not null and not 1088 * an errno stored in a pointer. This is semantically equivalent to 1089 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for 1090 * more information. 1091 */ 1092 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \ 1093 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL) 1094 1095 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \ 1096 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 1097 KUNIT_EXPECTATION, \ 1098 ptr, \ 1099 fmt, \ 1100 ##__VA_ARGS__) 1101 1102 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \ 1103 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__) 1104 1105 /** 1106 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true. 1107 * @test: The test context object. 1108 * @condition: an arbitrary boolean expression. The test fails and aborts when 1109 * this does not evaluate to true. 1110 * 1111 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to 1112 * fail *and immediately abort* when the specified condition is not met. Unlike 1113 * an expectation failure, it will prevent the test case from continuing to run; 1114 * this is otherwise known as an *assertion failure*. 1115 */ 1116 #define KUNIT_ASSERT_TRUE(test, condition) \ 1117 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL) 1118 1119 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \ 1120 KUNIT_TRUE_MSG_ASSERTION(test, \ 1121 KUNIT_ASSERTION, \ 1122 condition, \ 1123 fmt, \ 1124 ##__VA_ARGS__) 1125 1126 /** 1127 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false. 1128 * @test: The test context object. 1129 * @condition: an arbitrary boolean expression. 1130 * 1131 * Sets an assertion that the value that @condition evaluates to is false. This 1132 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure 1133 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1134 */ 1135 #define KUNIT_ASSERT_FALSE(test, condition) \ 1136 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL) 1137 1138 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \ 1139 KUNIT_FALSE_MSG_ASSERTION(test, \ 1140 KUNIT_ASSERTION, \ 1141 condition, \ 1142 fmt, \ 1143 ##__VA_ARGS__) 1144 1145 /** 1146 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal. 1147 * @test: The test context object. 1148 * @left: an arbitrary expression that evaluates to a primitive C type. 1149 * @right: an arbitrary expression that evaluates to a primitive C type. 1150 * 1151 * Sets an assertion that the values that @left and @right evaluate to are 1152 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion 1153 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1154 */ 1155 #define KUNIT_ASSERT_EQ(test, left, right) \ 1156 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL) 1157 1158 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \ 1159 KUNIT_BINARY_INT_ASSERTION(test, \ 1160 KUNIT_ASSERTION, \ 1161 left, ==, right, \ 1162 fmt, \ 1163 ##__VA_ARGS__) 1164 1165 /** 1166 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal. 1167 * @test: The test context object. 1168 * @left: an arbitrary expression that evaluates to a pointer. 1169 * @right: an arbitrary expression that evaluates to a pointer. 1170 * 1171 * Sets an assertion that the values that @left and @right evaluate to are 1172 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion 1173 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1174 */ 1175 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \ 1176 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL) 1177 1178 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \ 1179 KUNIT_BINARY_PTR_ASSERTION(test, \ 1180 KUNIT_ASSERTION, \ 1181 left, ==, right, \ 1182 fmt, \ 1183 ##__VA_ARGS__) 1184 1185 /** 1186 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal. 1187 * @test: The test context object. 1188 * @left: an arbitrary expression that evaluates to a primitive C type. 1189 * @right: an arbitrary expression that evaluates to a primitive C type. 1190 * 1191 * Sets an assertion that the values that @left and @right evaluate to are not 1192 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion 1193 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1194 */ 1195 #define KUNIT_ASSERT_NE(test, left, right) \ 1196 KUNIT_ASSERT_NE_MSG(test, left, right, NULL) 1197 1198 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \ 1199 KUNIT_BINARY_INT_ASSERTION(test, \ 1200 KUNIT_ASSERTION, \ 1201 left, !=, right, \ 1202 fmt, \ 1203 ##__VA_ARGS__) 1204 1205 /** 1206 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal. 1207 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal. 1208 * @test: The test context object. 1209 * @left: an arbitrary expression that evaluates to a pointer. 1210 * @right: an arbitrary expression that evaluates to a pointer. 1211 * 1212 * Sets an assertion that the values that @left and @right evaluate to are not 1213 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion 1214 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1215 */ 1216 #define KUNIT_ASSERT_PTR_NE(test, left, right) \ 1217 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL) 1218 1219 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \ 1220 KUNIT_BINARY_PTR_ASSERTION(test, \ 1221 KUNIT_ASSERTION, \ 1222 left, !=, right, \ 1223 fmt, \ 1224 ##__VA_ARGS__) 1225 /** 1226 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right. 1227 * @test: The test context object. 1228 * @left: an arbitrary expression that evaluates to a primitive C type. 1229 * @right: an arbitrary expression that evaluates to a primitive C type. 1230 * 1231 * Sets an assertion that the value that @left evaluates to is less than the 1232 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except 1233 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1234 * is not met. 1235 */ 1236 #define KUNIT_ASSERT_LT(test, left, right) \ 1237 KUNIT_ASSERT_LT_MSG(test, left, right, NULL) 1238 1239 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \ 1240 KUNIT_BINARY_INT_ASSERTION(test, \ 1241 KUNIT_ASSERTION, \ 1242 left, <, right, \ 1243 fmt, \ 1244 ##__VA_ARGS__) 1245 /** 1246 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right. 1247 * @test: The test context object. 1248 * @left: an arbitrary expression that evaluates to a primitive C type. 1249 * @right: an arbitrary expression that evaluates to a primitive C type. 1250 * 1251 * Sets an assertion that the value that @left evaluates to is less than or 1252 * equal to the value that @right evaluates to. This is the same as 1253 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see 1254 * KUNIT_ASSERT_TRUE()) when the assertion is not met. 1255 */ 1256 #define KUNIT_ASSERT_LE(test, left, right) \ 1257 KUNIT_ASSERT_LE_MSG(test, left, right, NULL) 1258 1259 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \ 1260 KUNIT_BINARY_INT_ASSERTION(test, \ 1261 KUNIT_ASSERTION, \ 1262 left, <=, right, \ 1263 fmt, \ 1264 ##__VA_ARGS__) 1265 1266 /** 1267 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right. 1268 * @test: The test context object. 1269 * @left: an arbitrary expression that evaluates to a primitive C type. 1270 * @right: an arbitrary expression that evaluates to a primitive C type. 1271 * 1272 * Sets an assertion that the value that @left evaluates to is greater than the 1273 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except 1274 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1275 * is not met. 1276 */ 1277 #define KUNIT_ASSERT_GT(test, left, right) \ 1278 KUNIT_ASSERT_GT_MSG(test, left, right, NULL) 1279 1280 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \ 1281 KUNIT_BINARY_INT_ASSERTION(test, \ 1282 KUNIT_ASSERTION, \ 1283 left, >, right, \ 1284 fmt, \ 1285 ##__VA_ARGS__) 1286 1287 /** 1288 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right. 1289 * @test: The test context object. 1290 * @left: an arbitrary expression that evaluates to a primitive C type. 1291 * @right: an arbitrary expression that evaluates to a primitive C type. 1292 * 1293 * Sets an assertion that the value that @left evaluates to is greater than the 1294 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except 1295 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1296 * is not met. 1297 */ 1298 #define KUNIT_ASSERT_GE(test, left, right) \ 1299 KUNIT_ASSERT_GE_MSG(test, left, right, NULL) 1300 1301 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \ 1302 KUNIT_BINARY_INT_ASSERTION(test, \ 1303 KUNIT_ASSERTION, \ 1304 left, >=, right, \ 1305 fmt, \ 1306 ##__VA_ARGS__) 1307 1308 /** 1309 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal. 1310 * @test: The test context object. 1311 * @left: an arbitrary expression that evaluates to a null terminated string. 1312 * @right: an arbitrary expression that evaluates to a null terminated string. 1313 * 1314 * Sets an assertion that the values that @left and @right evaluate to are 1315 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an 1316 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1317 */ 1318 #define KUNIT_ASSERT_STREQ(test, left, right) \ 1319 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL) 1320 1321 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \ 1322 KUNIT_BINARY_STR_ASSERTION(test, \ 1323 KUNIT_ASSERTION, \ 1324 left, ==, right, \ 1325 fmt, \ 1326 ##__VA_ARGS__) 1327 1328 /** 1329 * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal. 1330 * @test: The test context object. 1331 * @left: an arbitrary expression that evaluates to a null terminated string. 1332 * @right: an arbitrary expression that evaluates to a null terminated string. 1333 * 1334 * Sets an expectation that the values that @left and @right evaluate to are 1335 * not equal. This is semantically equivalent to 1336 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE() 1337 * for more information. 1338 */ 1339 #define KUNIT_ASSERT_STRNEQ(test, left, right) \ 1340 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL) 1341 1342 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \ 1343 KUNIT_BINARY_STR_ASSERTION(test, \ 1344 KUNIT_ASSERTION, \ 1345 left, !=, right, \ 1346 fmt, \ 1347 ##__VA_ARGS__) 1348 1349 /** 1350 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null. 1351 * @test: The test context object. 1352 * @ptr: an arbitrary pointer. 1353 * 1354 * Sets an assertion that the values that @ptr evaluates to is null. This is 1355 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion 1356 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1357 */ 1358 #define KUNIT_ASSERT_NULL(test, ptr) \ 1359 KUNIT_ASSERT_NULL_MSG(test, \ 1360 ptr, \ 1361 NULL) 1362 1363 #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \ 1364 KUNIT_BINARY_PTR_ASSERTION(test, \ 1365 KUNIT_ASSERTION, \ 1366 ptr, ==, NULL, \ 1367 fmt, \ 1368 ##__VA_ARGS__) 1369 1370 /** 1371 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null. 1372 * @test: The test context object. 1373 * @ptr: an arbitrary pointer. 1374 * 1375 * Sets an assertion that the values that @ptr evaluates to is not null. This 1376 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion 1377 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1378 */ 1379 #define KUNIT_ASSERT_NOT_NULL(test, ptr) \ 1380 KUNIT_ASSERT_NOT_NULL_MSG(test, \ 1381 ptr, \ 1382 NULL) 1383 1384 #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \ 1385 KUNIT_BINARY_PTR_ASSERTION(test, \ 1386 KUNIT_ASSERTION, \ 1387 ptr, !=, NULL, \ 1388 fmt, \ 1389 ##__VA_ARGS__) 1390 1391 /** 1392 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err. 1393 * @test: The test context object. 1394 * @ptr: an arbitrary pointer. 1395 * 1396 * Sets an assertion that the value that @ptr evaluates to is not null and not 1397 * an errno stored in a pointer. This is the same as 1398 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see 1399 * KUNIT_ASSERT_TRUE()) when the assertion is not met. 1400 */ 1401 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \ 1402 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL) 1403 1404 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \ 1405 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 1406 KUNIT_ASSERTION, \ 1407 ptr, \ 1408 fmt, \ 1409 ##__VA_ARGS__) 1410 1411 /** 1412 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array. 1413 * @name: prefix for the test parameter generator function. 1414 * @array: array of test parameters. 1415 * @get_desc: function to convert param to description; NULL to use default 1416 * 1417 * Define function @name_gen_params which uses @array to generate parameters. 1418 */ 1419 #define KUNIT_ARRAY_PARAM(name, array, get_desc) \ 1420 static const void *name##_gen_params(const void *prev, char *desc) \ 1421 { \ 1422 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \ 1423 if (__next - (array) < ARRAY_SIZE((array))) { \ 1424 void (*__get_desc)(typeof(__next), char *) = get_desc; \ 1425 if (__get_desc) \ 1426 __get_desc(__next, desc); \ 1427 return __next; \ 1428 } \ 1429 return NULL; \ 1430 } 1431 1432 // TODO(dlatypov@google.com): consider eventually migrating users to explicitly 1433 // include resource.h themselves if they need it. 1434 #include <kunit/resource.h> 1435 1436 #endif /* _KUNIT_TEST_H */ 1437