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