Linux Audio

Check our new training course

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