162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Base unit test (KUnit) API.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2019, Google LLC.
662306a36Sopenharmony_ci * Author: Brendan Higgins <brendanhiggins@google.com>
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#ifndef _KUNIT_TEST_H
1062306a36Sopenharmony_ci#define _KUNIT_TEST_H
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <kunit/assert.h>
1362306a36Sopenharmony_ci#include <kunit/try-catch.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include <linux/args.h>
1662306a36Sopenharmony_ci#include <linux/compiler.h>
1762306a36Sopenharmony_ci#include <linux/container_of.h>
1862306a36Sopenharmony_ci#include <linux/err.h>
1962306a36Sopenharmony_ci#include <linux/init.h>
2062306a36Sopenharmony_ci#include <linux/jump_label.h>
2162306a36Sopenharmony_ci#include <linux/kconfig.h>
2262306a36Sopenharmony_ci#include <linux/kref.h>
2362306a36Sopenharmony_ci#include <linux/list.h>
2462306a36Sopenharmony_ci#include <linux/module.h>
2562306a36Sopenharmony_ci#include <linux/slab.h>
2662306a36Sopenharmony_ci#include <linux/spinlock.h>
2762306a36Sopenharmony_ci#include <linux/string.h>
2862306a36Sopenharmony_ci#include <linux/types.h>
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#include <asm/rwonce.h>
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci/* Static key: true if any KUnit tests are currently running */
3362306a36Sopenharmony_ciDECLARE_STATIC_KEY_FALSE(kunit_running);
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_cistruct kunit;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci/* Size of log associated with test. */
3862306a36Sopenharmony_ci#define KUNIT_LOG_SIZE 2048
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/* Maximum size of parameter description string. */
4162306a36Sopenharmony_ci#define KUNIT_PARAM_DESC_SIZE 128
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/* Maximum size of a status comment. */
4462306a36Sopenharmony_ci#define KUNIT_STATUS_COMMENT_SIZE 256
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci/*
4762306a36Sopenharmony_ci * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
4862306a36Sopenharmony_ci * sub-subtest.  See the "Subtests" section in
4962306a36Sopenharmony_ci * https://node-tap.org/tap-protocol/
5062306a36Sopenharmony_ci */
5162306a36Sopenharmony_ci#define KUNIT_INDENT_LEN		4
5262306a36Sopenharmony_ci#define KUNIT_SUBTEST_INDENT		"    "
5362306a36Sopenharmony_ci#define KUNIT_SUBSUBTEST_INDENT		"        "
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci/**
5662306a36Sopenharmony_ci * enum kunit_status - Type of result for a test or test suite
5762306a36Sopenharmony_ci * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
5862306a36Sopenharmony_ci * @KUNIT_FAILURE: Denotes the test has failed.
5962306a36Sopenharmony_ci * @KUNIT_SKIPPED: Denotes the test has been skipped.
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_cienum kunit_status {
6262306a36Sopenharmony_ci	KUNIT_SUCCESS,
6362306a36Sopenharmony_ci	KUNIT_FAILURE,
6462306a36Sopenharmony_ci	KUNIT_SKIPPED,
6562306a36Sopenharmony_ci};
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci/* Attribute struct/enum definitions */
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci/*
7062306a36Sopenharmony_ci * Speed Attribute is stored as an enum and separated into categories of
7162306a36Sopenharmony_ci * speed: very_slowm, slow, and normal. These speeds are relative to
7262306a36Sopenharmony_ci * other KUnit tests.
7362306a36Sopenharmony_ci *
7462306a36Sopenharmony_ci * Note: unset speed attribute acts as default of KUNIT_SPEED_NORMAL.
7562306a36Sopenharmony_ci */
7662306a36Sopenharmony_cienum kunit_speed {
7762306a36Sopenharmony_ci	KUNIT_SPEED_UNSET,
7862306a36Sopenharmony_ci	KUNIT_SPEED_VERY_SLOW,
7962306a36Sopenharmony_ci	KUNIT_SPEED_SLOW,
8062306a36Sopenharmony_ci	KUNIT_SPEED_NORMAL,
8162306a36Sopenharmony_ci	KUNIT_SPEED_MAX = KUNIT_SPEED_NORMAL,
8262306a36Sopenharmony_ci};
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci/* Holds attributes for each test case and suite */
8562306a36Sopenharmony_cistruct kunit_attributes {
8662306a36Sopenharmony_ci	enum kunit_speed speed;
8762306a36Sopenharmony_ci};
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci/**
9062306a36Sopenharmony_ci * struct kunit_case - represents an individual test case.
9162306a36Sopenharmony_ci *
9262306a36Sopenharmony_ci * @run_case: the function representing the actual test case.
9362306a36Sopenharmony_ci * @name:     the name of the test case.
9462306a36Sopenharmony_ci * @generate_params: the generator function for parameterized tests.
9562306a36Sopenharmony_ci * @attr:     the attributes associated with the test
9662306a36Sopenharmony_ci *
9762306a36Sopenharmony_ci * A test case is a function with the signature,
9862306a36Sopenharmony_ci * ``void (*)(struct kunit *)``
9962306a36Sopenharmony_ci * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
10062306a36Sopenharmony_ci * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
10162306a36Sopenharmony_ci * with a &struct kunit_suite and will be run after the suite's init
10262306a36Sopenharmony_ci * function and followed by the suite's exit function.
10362306a36Sopenharmony_ci *
10462306a36Sopenharmony_ci * A test case should be static and should only be created with the
10562306a36Sopenharmony_ci * KUNIT_CASE() macro; additionally, every array of test cases should be
10662306a36Sopenharmony_ci * terminated with an empty test case.
10762306a36Sopenharmony_ci *
10862306a36Sopenharmony_ci * Example:
10962306a36Sopenharmony_ci *
11062306a36Sopenharmony_ci * .. code-block:: c
11162306a36Sopenharmony_ci *
11262306a36Sopenharmony_ci *	void add_test_basic(struct kunit *test)
11362306a36Sopenharmony_ci *	{
11462306a36Sopenharmony_ci *		KUNIT_EXPECT_EQ(test, 1, add(1, 0));
11562306a36Sopenharmony_ci *		KUNIT_EXPECT_EQ(test, 2, add(1, 1));
11662306a36Sopenharmony_ci *		KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
11762306a36Sopenharmony_ci *		KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
11862306a36Sopenharmony_ci *		KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
11962306a36Sopenharmony_ci *	}
12062306a36Sopenharmony_ci *
12162306a36Sopenharmony_ci *	static struct kunit_case example_test_cases[] = {
12262306a36Sopenharmony_ci *		KUNIT_CASE(add_test_basic),
12362306a36Sopenharmony_ci *		{}
12462306a36Sopenharmony_ci *	};
12562306a36Sopenharmony_ci *
12662306a36Sopenharmony_ci */
12762306a36Sopenharmony_cistruct kunit_case {
12862306a36Sopenharmony_ci	void (*run_case)(struct kunit *test);
12962306a36Sopenharmony_ci	const char *name;
13062306a36Sopenharmony_ci	const void* (*generate_params)(const void *prev, char *desc);
13162306a36Sopenharmony_ci	struct kunit_attributes attr;
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	/* private: internal use only. */
13462306a36Sopenharmony_ci	enum kunit_status status;
13562306a36Sopenharmony_ci	char *module_name;
13662306a36Sopenharmony_ci	char *log;
13762306a36Sopenharmony_ci};
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_cistatic inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
14062306a36Sopenharmony_ci{
14162306a36Sopenharmony_ci	switch (status) {
14262306a36Sopenharmony_ci	case KUNIT_SKIPPED:
14362306a36Sopenharmony_ci	case KUNIT_SUCCESS:
14462306a36Sopenharmony_ci		return "ok";
14562306a36Sopenharmony_ci	case KUNIT_FAILURE:
14662306a36Sopenharmony_ci		return "not ok";
14762306a36Sopenharmony_ci	}
14862306a36Sopenharmony_ci	return "invalid";
14962306a36Sopenharmony_ci}
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci/**
15262306a36Sopenharmony_ci * KUNIT_CASE - A helper for creating a &struct kunit_case
15362306a36Sopenharmony_ci *
15462306a36Sopenharmony_ci * @test_name: a reference to a test case function.
15562306a36Sopenharmony_ci *
15662306a36Sopenharmony_ci * Takes a symbol for a function representing a test case and creates a
15762306a36Sopenharmony_ci * &struct kunit_case object from it. See the documentation for
15862306a36Sopenharmony_ci * &struct kunit_case for an example on how to use it.
15962306a36Sopenharmony_ci */
16062306a36Sopenharmony_ci#define KUNIT_CASE(test_name)			\
16162306a36Sopenharmony_ci		{ .run_case = test_name, .name = #test_name,	\
16262306a36Sopenharmony_ci		  .module_name = KBUILD_MODNAME}
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci/**
16562306a36Sopenharmony_ci * KUNIT_CASE_ATTR - A helper for creating a &struct kunit_case
16662306a36Sopenharmony_ci * with attributes
16762306a36Sopenharmony_ci *
16862306a36Sopenharmony_ci * @test_name: a reference to a test case function.
16962306a36Sopenharmony_ci * @attributes: a reference to a struct kunit_attributes object containing
17062306a36Sopenharmony_ci * test attributes
17162306a36Sopenharmony_ci */
17262306a36Sopenharmony_ci#define KUNIT_CASE_ATTR(test_name, attributes)			\
17362306a36Sopenharmony_ci		{ .run_case = test_name, .name = #test_name,	\
17462306a36Sopenharmony_ci		  .attr = attributes, .module_name = KBUILD_MODNAME}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci/**
17762306a36Sopenharmony_ci * KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case
17862306a36Sopenharmony_ci * with the slow attribute
17962306a36Sopenharmony_ci *
18062306a36Sopenharmony_ci * @test_name: a reference to a test case function.
18162306a36Sopenharmony_ci */
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci#define KUNIT_CASE_SLOW(test_name)			\
18462306a36Sopenharmony_ci		{ .run_case = test_name, .name = #test_name,	\
18562306a36Sopenharmony_ci		  .attr.speed = KUNIT_SPEED_SLOW, .module_name = KBUILD_MODNAME}
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci/**
18862306a36Sopenharmony_ci * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
18962306a36Sopenharmony_ci *
19062306a36Sopenharmony_ci * @test_name: a reference to a test case function.
19162306a36Sopenharmony_ci * @gen_params: a reference to a parameter generator function.
19262306a36Sopenharmony_ci *
19362306a36Sopenharmony_ci * The generator function::
19462306a36Sopenharmony_ci *
19562306a36Sopenharmony_ci *	const void* gen_params(const void *prev, char *desc)
19662306a36Sopenharmony_ci *
19762306a36Sopenharmony_ci * is used to lazily generate a series of arbitrarily typed values that fit into
19862306a36Sopenharmony_ci * a void*. The argument @prev is the previously returned value, which should be
19962306a36Sopenharmony_ci * used to derive the next value; @prev is set to NULL on the initial generator
20062306a36Sopenharmony_ci * call. When no more values are available, the generator must return NULL.
20162306a36Sopenharmony_ci * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
20262306a36Sopenharmony_ci * describing the parameter.
20362306a36Sopenharmony_ci */
20462306a36Sopenharmony_ci#define KUNIT_CASE_PARAM(test_name, gen_params)			\
20562306a36Sopenharmony_ci		{ .run_case = test_name, .name = #test_name,	\
20662306a36Sopenharmony_ci		  .generate_params = gen_params, .module_name = KBUILD_MODNAME}
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci/**
20962306a36Sopenharmony_ci * KUNIT_CASE_PARAM_ATTR - A helper for creating a parameterized &struct
21062306a36Sopenharmony_ci * kunit_case with attributes
21162306a36Sopenharmony_ci *
21262306a36Sopenharmony_ci * @test_name: a reference to a test case function.
21362306a36Sopenharmony_ci * @gen_params: a reference to a parameter generator function.
21462306a36Sopenharmony_ci * @attributes: a reference to a struct kunit_attributes object containing
21562306a36Sopenharmony_ci * test attributes
21662306a36Sopenharmony_ci */
21762306a36Sopenharmony_ci#define KUNIT_CASE_PARAM_ATTR(test_name, gen_params, attributes)	\
21862306a36Sopenharmony_ci		{ .run_case = test_name, .name = #test_name,	\
21962306a36Sopenharmony_ci		  .generate_params = gen_params,				\
22062306a36Sopenharmony_ci		  .attr = attributes, .module_name = KBUILD_MODNAME}
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci/**
22362306a36Sopenharmony_ci * struct kunit_suite - describes a related collection of &struct kunit_case
22462306a36Sopenharmony_ci *
22562306a36Sopenharmony_ci * @name:	the name of the test. Purely informational.
22662306a36Sopenharmony_ci * @suite_init:	called once per test suite before the test cases.
22762306a36Sopenharmony_ci * @suite_exit:	called once per test suite after all test cases.
22862306a36Sopenharmony_ci * @init:	called before every test case.
22962306a36Sopenharmony_ci * @exit:	called after every test case.
23062306a36Sopenharmony_ci * @test_cases:	a null terminated array of test cases.
23162306a36Sopenharmony_ci * @attr:	the attributes associated with the test suite
23262306a36Sopenharmony_ci *
23362306a36Sopenharmony_ci * A kunit_suite is a collection of related &struct kunit_case s, such that
23462306a36Sopenharmony_ci * @init is called before every test case and @exit is called after every
23562306a36Sopenharmony_ci * test case, similar to the notion of a *test fixture* or a *test class*
23662306a36Sopenharmony_ci * in other unit testing frameworks like JUnit or Googletest.
23762306a36Sopenharmony_ci *
23862306a36Sopenharmony_ci * Note that @exit and @suite_exit will run even if @init or @suite_init
23962306a36Sopenharmony_ci * fail: make sure they can handle any inconsistent state which may result.
24062306a36Sopenharmony_ci *
24162306a36Sopenharmony_ci * Every &struct kunit_case must be associated with a kunit_suite for KUnit
24262306a36Sopenharmony_ci * to run it.
24362306a36Sopenharmony_ci */
24462306a36Sopenharmony_cistruct kunit_suite {
24562306a36Sopenharmony_ci	const char name[256];
24662306a36Sopenharmony_ci	int (*suite_init)(struct kunit_suite *suite);
24762306a36Sopenharmony_ci	void (*suite_exit)(struct kunit_suite *suite);
24862306a36Sopenharmony_ci	int (*init)(struct kunit *test);
24962306a36Sopenharmony_ci	void (*exit)(struct kunit *test);
25062306a36Sopenharmony_ci	struct kunit_case *test_cases;
25162306a36Sopenharmony_ci	struct kunit_attributes attr;
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	/* private: internal use only */
25462306a36Sopenharmony_ci	char status_comment[KUNIT_STATUS_COMMENT_SIZE];
25562306a36Sopenharmony_ci	struct dentry *debugfs;
25662306a36Sopenharmony_ci	char *log;
25762306a36Sopenharmony_ci	int suite_init_err;
25862306a36Sopenharmony_ci};
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci/* Stores an array of suites, end points one past the end */
26162306a36Sopenharmony_cistruct kunit_suite_set {
26262306a36Sopenharmony_ci	struct kunit_suite * const *start;
26362306a36Sopenharmony_ci	struct kunit_suite * const *end;
26462306a36Sopenharmony_ci};
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci/**
26762306a36Sopenharmony_ci * struct kunit - represents a running instance of a test.
26862306a36Sopenharmony_ci *
26962306a36Sopenharmony_ci * @priv: for user to store arbitrary data. Commonly used to pass data
27062306a36Sopenharmony_ci *	  created in the init function (see &struct kunit_suite).
27162306a36Sopenharmony_ci *
27262306a36Sopenharmony_ci * Used to store information about the current context under which the test
27362306a36Sopenharmony_ci * is running. Most of this data is private and should only be accessed
27462306a36Sopenharmony_ci * indirectly via public functions; the one exception is @priv which can be
27562306a36Sopenharmony_ci * used by the test writer to store arbitrary data.
27662306a36Sopenharmony_ci */
27762306a36Sopenharmony_cistruct kunit {
27862306a36Sopenharmony_ci	void *priv;
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci	/* private: internal use only. */
28162306a36Sopenharmony_ci	const char *name; /* Read only after initialization! */
28262306a36Sopenharmony_ci	char *log; /* Points at case log after initialization */
28362306a36Sopenharmony_ci	struct kunit_try_catch try_catch;
28462306a36Sopenharmony_ci	/* param_value is the current parameter value for a test case. */
28562306a36Sopenharmony_ci	const void *param_value;
28662306a36Sopenharmony_ci	/* param_index stores the index of the parameter in parameterized tests. */
28762306a36Sopenharmony_ci	int param_index;
28862306a36Sopenharmony_ci	/*
28962306a36Sopenharmony_ci	 * success starts as true, and may only be set to false during a
29062306a36Sopenharmony_ci	 * test case; thus, it is safe to update this across multiple
29162306a36Sopenharmony_ci	 * threads using WRITE_ONCE; however, as a consequence, it may only
29262306a36Sopenharmony_ci	 * be read after the test case finishes once all threads associated
29362306a36Sopenharmony_ci	 * with the test case have terminated.
29462306a36Sopenharmony_ci	 */
29562306a36Sopenharmony_ci	spinlock_t lock; /* Guards all mutable test state. */
29662306a36Sopenharmony_ci	enum kunit_status status; /* Read only after test_case finishes! */
29762306a36Sopenharmony_ci	/*
29862306a36Sopenharmony_ci	 * Because resources is a list that may be updated multiple times (with
29962306a36Sopenharmony_ci	 * new resources) from any thread associated with a test case, we must
30062306a36Sopenharmony_ci	 * protect it with some type of lock.
30162306a36Sopenharmony_ci	 */
30262306a36Sopenharmony_ci	struct list_head resources; /* Protected by lock. */
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci	char status_comment[KUNIT_STATUS_COMMENT_SIZE];
30562306a36Sopenharmony_ci};
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_cistatic inline void kunit_set_failure(struct kunit *test)
30862306a36Sopenharmony_ci{
30962306a36Sopenharmony_ci	WRITE_ONCE(test->status, KUNIT_FAILURE);
31062306a36Sopenharmony_ci}
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_cibool kunit_enabled(void);
31362306a36Sopenharmony_ciconst char *kunit_action(void);
31462306a36Sopenharmony_ciconst char *kunit_filter_glob(void);
31562306a36Sopenharmony_cichar *kunit_filter(void);
31662306a36Sopenharmony_cichar *kunit_filter_action(void);
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_civoid kunit_init_test(struct kunit *test, const char *name, char *log);
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ciint kunit_run_tests(struct kunit_suite *suite);
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_cisize_t kunit_suite_num_test_cases(struct kunit_suite *suite);
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ciunsigned int kunit_test_case_num(struct kunit_suite *suite,
32562306a36Sopenharmony_ci				 struct kunit_case *test_case);
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_cistruct kunit_suite_set
32862306a36Sopenharmony_cikunit_filter_suites(const struct kunit_suite_set *suite_set,
32962306a36Sopenharmony_ci		    const char *filter_glob,
33062306a36Sopenharmony_ci		    char *filters,
33162306a36Sopenharmony_ci		    char *filter_action,
33262306a36Sopenharmony_ci		    int *err);
33362306a36Sopenharmony_civoid kunit_free_suite_set(struct kunit_suite_set suite_set);
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ciint __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_civoid __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_civoid kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin);
34062306a36Sopenharmony_civoid kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr);
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ci#if IS_BUILTIN(CONFIG_KUNIT)
34362306a36Sopenharmony_ciint kunit_run_all_tests(void);
34462306a36Sopenharmony_ci#else
34562306a36Sopenharmony_cistatic inline int kunit_run_all_tests(void)
34662306a36Sopenharmony_ci{
34762306a36Sopenharmony_ci	return 0;
34862306a36Sopenharmony_ci}
34962306a36Sopenharmony_ci#endif /* IS_BUILTIN(CONFIG_KUNIT) */
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci#define __kunit_test_suites(unique_array, ...)				       \
35262306a36Sopenharmony_ci	static struct kunit_suite *unique_array[]			       \
35362306a36Sopenharmony_ci	__aligned(sizeof(struct kunit_suite *))				       \
35462306a36Sopenharmony_ci	__used __section(".kunit_test_suites") = { __VA_ARGS__ }
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci/**
35762306a36Sopenharmony_ci * kunit_test_suites() - used to register one or more &struct kunit_suite
35862306a36Sopenharmony_ci *			 with KUnit.
35962306a36Sopenharmony_ci *
36062306a36Sopenharmony_ci * @__suites: a statically allocated list of &struct kunit_suite.
36162306a36Sopenharmony_ci *
36262306a36Sopenharmony_ci * Registers @suites with the test framework.
36362306a36Sopenharmony_ci * This is done by placing the array of struct kunit_suite * in the
36462306a36Sopenharmony_ci * .kunit_test_suites ELF section.
36562306a36Sopenharmony_ci *
36662306a36Sopenharmony_ci * When builtin, KUnit tests are all run via the executor at boot, and when
36762306a36Sopenharmony_ci * built as a module, they run on module load.
36862306a36Sopenharmony_ci *
36962306a36Sopenharmony_ci */
37062306a36Sopenharmony_ci#define kunit_test_suites(__suites...)						\
37162306a36Sopenharmony_ci	__kunit_test_suites(__UNIQUE_ID(array),				\
37262306a36Sopenharmony_ci			    ##__suites)
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci#define kunit_test_suite(suite)	kunit_test_suites(&suite)
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci/**
37762306a36Sopenharmony_ci * kunit_test_init_section_suites() - used to register one or more &struct
37862306a36Sopenharmony_ci *				      kunit_suite containing init functions or
37962306a36Sopenharmony_ci *				      init data.
38062306a36Sopenharmony_ci *
38162306a36Sopenharmony_ci * @__suites: a statically allocated list of &struct kunit_suite.
38262306a36Sopenharmony_ci *
38362306a36Sopenharmony_ci * This functions identically as kunit_test_suites() except that it suppresses
38462306a36Sopenharmony_ci * modpost warnings for referencing functions marked __init or data marked
38562306a36Sopenharmony_ci * __initdata; this is OK because currently KUnit only runs tests upon boot
38662306a36Sopenharmony_ci * during the init phase or upon loading a module during the init phase.
38762306a36Sopenharmony_ci *
38862306a36Sopenharmony_ci * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
38962306a36Sopenharmony_ci * tests must be excluded.
39062306a36Sopenharmony_ci *
39162306a36Sopenharmony_ci * The only thing this macro does that's different from kunit_test_suites is
39262306a36Sopenharmony_ci * that it suffixes the array and suite declarations it makes with _probe;
39362306a36Sopenharmony_ci * modpost suppresses warnings about referencing init data for symbols named in
39462306a36Sopenharmony_ci * this manner.
39562306a36Sopenharmony_ci */
39662306a36Sopenharmony_ci#define kunit_test_init_section_suites(__suites...)			\
39762306a36Sopenharmony_ci	__kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe),	\
39862306a36Sopenharmony_ci			    ##__suites)
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_ci#define kunit_test_init_section_suite(suite)	\
40162306a36Sopenharmony_ci	kunit_test_init_section_suites(&suite)
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci#define kunit_suite_for_each_test_case(suite, test_case)		\
40462306a36Sopenharmony_ci	for (test_case = suite->test_cases; test_case->run_case; test_case++)
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_cienum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci/**
40962306a36Sopenharmony_ci * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
41062306a36Sopenharmony_ci * @test: The test context object.
41162306a36Sopenharmony_ci * @n: number of elements.
41262306a36Sopenharmony_ci * @size: The size in bytes of the desired memory.
41362306a36Sopenharmony_ci * @gfp: flags passed to underlying kmalloc().
41462306a36Sopenharmony_ci *
41562306a36Sopenharmony_ci * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
41662306a36Sopenharmony_ci * and is automatically cleaned up after the test case concludes. See kunit_add_action()
41762306a36Sopenharmony_ci * for more information.
41862306a36Sopenharmony_ci *
41962306a36Sopenharmony_ci * Note that some internal context data is also allocated with GFP_KERNEL,
42062306a36Sopenharmony_ci * regardless of the gfp passed in.
42162306a36Sopenharmony_ci */
42262306a36Sopenharmony_civoid *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci/**
42562306a36Sopenharmony_ci * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
42662306a36Sopenharmony_ci * @test: The test context object.
42762306a36Sopenharmony_ci * @size: The size in bytes of the desired memory.
42862306a36Sopenharmony_ci * @gfp: flags passed to underlying kmalloc().
42962306a36Sopenharmony_ci *
43062306a36Sopenharmony_ci * See kmalloc() and kunit_kmalloc_array() for more information.
43162306a36Sopenharmony_ci *
43262306a36Sopenharmony_ci * Note that some internal context data is also allocated with GFP_KERNEL,
43362306a36Sopenharmony_ci * regardless of the gfp passed in.
43462306a36Sopenharmony_ci */
43562306a36Sopenharmony_cistatic inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
43662306a36Sopenharmony_ci{
43762306a36Sopenharmony_ci	return kunit_kmalloc_array(test, 1, size, gfp);
43862306a36Sopenharmony_ci}
43962306a36Sopenharmony_ci
44062306a36Sopenharmony_ci/**
44162306a36Sopenharmony_ci * kunit_kfree() - Like kfree except for allocations managed by KUnit.
44262306a36Sopenharmony_ci * @test: The test case to which the resource belongs.
44362306a36Sopenharmony_ci * @ptr: The memory allocation to free.
44462306a36Sopenharmony_ci */
44562306a36Sopenharmony_civoid kunit_kfree(struct kunit *test, const void *ptr);
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci/**
44862306a36Sopenharmony_ci * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
44962306a36Sopenharmony_ci * @test: The test context object.
45062306a36Sopenharmony_ci * @size: The size in bytes of the desired memory.
45162306a36Sopenharmony_ci * @gfp: flags passed to underlying kmalloc().
45262306a36Sopenharmony_ci *
45362306a36Sopenharmony_ci * See kzalloc() and kunit_kmalloc_array() for more information.
45462306a36Sopenharmony_ci */
45562306a36Sopenharmony_cistatic inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
45662306a36Sopenharmony_ci{
45762306a36Sopenharmony_ci	return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
45862306a36Sopenharmony_ci}
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci/**
46162306a36Sopenharmony_ci * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
46262306a36Sopenharmony_ci * @test: The test context object.
46362306a36Sopenharmony_ci * @n: number of elements.
46462306a36Sopenharmony_ci * @size: The size in bytes of the desired memory.
46562306a36Sopenharmony_ci * @gfp: flags passed to underlying kmalloc().
46662306a36Sopenharmony_ci *
46762306a36Sopenharmony_ci * See kcalloc() and kunit_kmalloc_array() for more information.
46862306a36Sopenharmony_ci */
46962306a36Sopenharmony_cistatic inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
47062306a36Sopenharmony_ci{
47162306a36Sopenharmony_ci	return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
47262306a36Sopenharmony_ci}
47362306a36Sopenharmony_ci
47462306a36Sopenharmony_civoid kunit_cleanup(struct kunit *test);
47562306a36Sopenharmony_ci
47662306a36Sopenharmony_civoid __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci/**
47962306a36Sopenharmony_ci * kunit_mark_skipped() - Marks @test_or_suite as skipped
48062306a36Sopenharmony_ci *
48162306a36Sopenharmony_ci * @test_or_suite: The test context object.
48262306a36Sopenharmony_ci * @fmt:  A printk() style format string.
48362306a36Sopenharmony_ci *
48462306a36Sopenharmony_ci * Marks the test as skipped. @fmt is given output as the test status
48562306a36Sopenharmony_ci * comment, typically the reason the test was skipped.
48662306a36Sopenharmony_ci *
48762306a36Sopenharmony_ci * Test execution continues after kunit_mark_skipped() is called.
48862306a36Sopenharmony_ci */
48962306a36Sopenharmony_ci#define kunit_mark_skipped(test_or_suite, fmt, ...)			\
49062306a36Sopenharmony_ci	do {								\
49162306a36Sopenharmony_ci		WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED);	\
49262306a36Sopenharmony_ci		scnprintf((test_or_suite)->status_comment,		\
49362306a36Sopenharmony_ci			  KUNIT_STATUS_COMMENT_SIZE,			\
49462306a36Sopenharmony_ci			  fmt, ##__VA_ARGS__);				\
49562306a36Sopenharmony_ci	} while (0)
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_ci/**
49862306a36Sopenharmony_ci * kunit_skip() - Marks @test_or_suite as skipped
49962306a36Sopenharmony_ci *
50062306a36Sopenharmony_ci * @test_or_suite: The test context object.
50162306a36Sopenharmony_ci * @fmt:  A printk() style format string.
50262306a36Sopenharmony_ci *
50362306a36Sopenharmony_ci * Skips the test. @fmt is given output as the test status
50462306a36Sopenharmony_ci * comment, typically the reason the test was skipped.
50562306a36Sopenharmony_ci *
50662306a36Sopenharmony_ci * Test execution is halted after kunit_skip() is called.
50762306a36Sopenharmony_ci */
50862306a36Sopenharmony_ci#define kunit_skip(test_or_suite, fmt, ...)				\
50962306a36Sopenharmony_ci	do {								\
51062306a36Sopenharmony_ci		kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
51162306a36Sopenharmony_ci		kunit_try_catch_throw(&((test_or_suite)->try_catch));	\
51262306a36Sopenharmony_ci	} while (0)
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_ci/*
51562306a36Sopenharmony_ci * printk and log to per-test or per-suite log buffer.  Logging only done
51662306a36Sopenharmony_ci * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
51762306a36Sopenharmony_ci */
51862306a36Sopenharmony_ci#define kunit_log(lvl, test_or_suite, fmt, ...)				\
51962306a36Sopenharmony_ci	do {								\
52062306a36Sopenharmony_ci		printk(lvl fmt, ##__VA_ARGS__);				\
52162306a36Sopenharmony_ci		kunit_log_append((test_or_suite)->log,	fmt,		\
52262306a36Sopenharmony_ci				 ##__VA_ARGS__);			\
52362306a36Sopenharmony_ci	} while (0)
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci#define kunit_printk(lvl, test, fmt, ...)				\
52662306a36Sopenharmony_ci	kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt,		\
52762306a36Sopenharmony_ci		  (test)->name,	##__VA_ARGS__)
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci/**
53062306a36Sopenharmony_ci * kunit_info() - Prints an INFO level message associated with @test.
53162306a36Sopenharmony_ci *
53262306a36Sopenharmony_ci * @test: The test context object.
53362306a36Sopenharmony_ci * @fmt:  A printk() style format string.
53462306a36Sopenharmony_ci *
53562306a36Sopenharmony_ci * Prints an info level message associated with the test suite being run.
53662306a36Sopenharmony_ci * Takes a variable number of format parameters just like printk().
53762306a36Sopenharmony_ci */
53862306a36Sopenharmony_ci#define kunit_info(test, fmt, ...) \
53962306a36Sopenharmony_ci	kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
54062306a36Sopenharmony_ci
54162306a36Sopenharmony_ci/**
54262306a36Sopenharmony_ci * kunit_warn() - Prints a WARN level message associated with @test.
54362306a36Sopenharmony_ci *
54462306a36Sopenharmony_ci * @test: The test context object.
54562306a36Sopenharmony_ci * @fmt:  A printk() style format string.
54662306a36Sopenharmony_ci *
54762306a36Sopenharmony_ci * Prints a warning level message.
54862306a36Sopenharmony_ci */
54962306a36Sopenharmony_ci#define kunit_warn(test, fmt, ...) \
55062306a36Sopenharmony_ci	kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
55162306a36Sopenharmony_ci
55262306a36Sopenharmony_ci/**
55362306a36Sopenharmony_ci * kunit_err() - Prints an ERROR level message associated with @test.
55462306a36Sopenharmony_ci *
55562306a36Sopenharmony_ci * @test: The test context object.
55662306a36Sopenharmony_ci * @fmt:  A printk() style format string.
55762306a36Sopenharmony_ci *
55862306a36Sopenharmony_ci * Prints an error level message.
55962306a36Sopenharmony_ci */
56062306a36Sopenharmony_ci#define kunit_err(test, fmt, ...) \
56162306a36Sopenharmony_ci	kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
56262306a36Sopenharmony_ci
56362306a36Sopenharmony_ci/**
56462306a36Sopenharmony_ci * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
56562306a36Sopenharmony_ci * @test: The test context object.
56662306a36Sopenharmony_ci *
56762306a36Sopenharmony_ci * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
56862306a36Sopenharmony_ci * words, it does nothing and only exists for code clarity. See
56962306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE() for more information.
57062306a36Sopenharmony_ci */
57162306a36Sopenharmony_ci#define KUNIT_SUCCEED(test) do {} while (0)
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_civoid __noreturn __kunit_abort(struct kunit *test);
57462306a36Sopenharmony_ci
57562306a36Sopenharmony_civoid __kunit_do_failed_assertion(struct kunit *test,
57662306a36Sopenharmony_ci			       const struct kunit_loc *loc,
57762306a36Sopenharmony_ci			       enum kunit_assert_type type,
57862306a36Sopenharmony_ci			       const struct kunit_assert *assert,
57962306a36Sopenharmony_ci			       assert_format_t assert_format,
58062306a36Sopenharmony_ci			       const char *fmt, ...);
58162306a36Sopenharmony_ci
58262306a36Sopenharmony_ci#define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
58362306a36Sopenharmony_ci	static const struct kunit_loc __loc = KUNIT_CURRENT_LOC;	       \
58462306a36Sopenharmony_ci	const struct assert_class __assertion = INITIALIZER;		       \
58562306a36Sopenharmony_ci	__kunit_do_failed_assertion(test,				       \
58662306a36Sopenharmony_ci				    &__loc,				       \
58762306a36Sopenharmony_ci				    assert_type,			       \
58862306a36Sopenharmony_ci				    &__assertion.assert,		       \
58962306a36Sopenharmony_ci				    assert_format,			       \
59062306a36Sopenharmony_ci				    fmt,				       \
59162306a36Sopenharmony_ci				    ##__VA_ARGS__);			       \
59262306a36Sopenharmony_ci	if (assert_type == KUNIT_ASSERTION)				       \
59362306a36Sopenharmony_ci		__kunit_abort(test);					       \
59462306a36Sopenharmony_ci} while (0)
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_ci
59762306a36Sopenharmony_ci#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...)		       \
59862306a36Sopenharmony_ci	_KUNIT_FAILED(test,						       \
59962306a36Sopenharmony_ci		      assert_type,					       \
60062306a36Sopenharmony_ci		      kunit_fail_assert,				       \
60162306a36Sopenharmony_ci		      kunit_fail_assert_format,				       \
60262306a36Sopenharmony_ci		      {},						       \
60362306a36Sopenharmony_ci		      fmt,						       \
60462306a36Sopenharmony_ci		      ##__VA_ARGS__)
60562306a36Sopenharmony_ci
60662306a36Sopenharmony_ci/**
60762306a36Sopenharmony_ci * KUNIT_FAIL() - Always causes a test to fail when evaluated.
60862306a36Sopenharmony_ci * @test: The test context object.
60962306a36Sopenharmony_ci * @fmt: an informational message to be printed when the assertion is made.
61062306a36Sopenharmony_ci * @...: string format arguments.
61162306a36Sopenharmony_ci *
61262306a36Sopenharmony_ci * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
61362306a36Sopenharmony_ci * other words, it always results in a failed expectation, and consequently
61462306a36Sopenharmony_ci * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
61562306a36Sopenharmony_ci * for more information.
61662306a36Sopenharmony_ci */
61762306a36Sopenharmony_ci#define KUNIT_FAIL(test, fmt, ...)					       \
61862306a36Sopenharmony_ci	KUNIT_FAIL_ASSERTION(test,					       \
61962306a36Sopenharmony_ci			     KUNIT_EXPECTATION,				       \
62062306a36Sopenharmony_ci			     fmt,					       \
62162306a36Sopenharmony_ci			     ##__VA_ARGS__)
62262306a36Sopenharmony_ci
62362306a36Sopenharmony_ci/* Helper to safely pass around an initializer list to other macros. */
62462306a36Sopenharmony_ci#define KUNIT_INIT_ASSERT(initializers...) { initializers }
62562306a36Sopenharmony_ci
62662306a36Sopenharmony_ci#define KUNIT_UNARY_ASSERTION(test,					       \
62762306a36Sopenharmony_ci			      assert_type,				       \
62862306a36Sopenharmony_ci			      condition_,				       \
62962306a36Sopenharmony_ci			      expected_true_,				       \
63062306a36Sopenharmony_ci			      fmt,					       \
63162306a36Sopenharmony_ci			      ...)					       \
63262306a36Sopenharmony_cido {									       \
63362306a36Sopenharmony_ci	if (likely(!!(condition_) == !!expected_true_))			       \
63462306a36Sopenharmony_ci		break;							       \
63562306a36Sopenharmony_ci									       \
63662306a36Sopenharmony_ci	_KUNIT_FAILED(test,						       \
63762306a36Sopenharmony_ci		      assert_type,					       \
63862306a36Sopenharmony_ci		      kunit_unary_assert,				       \
63962306a36Sopenharmony_ci		      kunit_unary_assert_format,			       \
64062306a36Sopenharmony_ci		      KUNIT_INIT_ASSERT(.condition = #condition_,	       \
64162306a36Sopenharmony_ci					.expected_true = expected_true_),      \
64262306a36Sopenharmony_ci		      fmt,						       \
64362306a36Sopenharmony_ci		      ##__VA_ARGS__);					       \
64462306a36Sopenharmony_ci} while (0)
64562306a36Sopenharmony_ci
64662306a36Sopenharmony_ci#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)       \
64762306a36Sopenharmony_ci	KUNIT_UNARY_ASSERTION(test,					       \
64862306a36Sopenharmony_ci			      assert_type,				       \
64962306a36Sopenharmony_ci			      condition,				       \
65062306a36Sopenharmony_ci			      true,					       \
65162306a36Sopenharmony_ci			      fmt,					       \
65262306a36Sopenharmony_ci			      ##__VA_ARGS__)
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)      \
65562306a36Sopenharmony_ci	KUNIT_UNARY_ASSERTION(test,					       \
65662306a36Sopenharmony_ci			      assert_type,				       \
65762306a36Sopenharmony_ci			      condition,				       \
65862306a36Sopenharmony_ci			      false,					       \
65962306a36Sopenharmony_ci			      fmt,					       \
66062306a36Sopenharmony_ci			      ##__VA_ARGS__)
66162306a36Sopenharmony_ci
66262306a36Sopenharmony_ci/*
66362306a36Sopenharmony_ci * A factory macro for defining the assertions and expectations for the basic
66462306a36Sopenharmony_ci * comparisons defined for the built in types.
66562306a36Sopenharmony_ci *
66662306a36Sopenharmony_ci * Unfortunately, there is no common type that all types can be promoted to for
66762306a36Sopenharmony_ci * which all the binary operators behave the same way as for the actual types
66862306a36Sopenharmony_ci * (for example, there is no type that long long and unsigned long long can
66962306a36Sopenharmony_ci * both be cast to where the comparison result is preserved for all values). So
67062306a36Sopenharmony_ci * the best we can do is do the comparison in the original types and then coerce
67162306a36Sopenharmony_ci * everything to long long for printing; this way, the comparison behaves
67262306a36Sopenharmony_ci * correctly and the printed out value usually makes sense without
67362306a36Sopenharmony_ci * interpretation, but can always be interpreted to figure out the actual
67462306a36Sopenharmony_ci * value.
67562306a36Sopenharmony_ci */
67662306a36Sopenharmony_ci#define KUNIT_BASE_BINARY_ASSERTION(test,				       \
67762306a36Sopenharmony_ci				    assert_class,			       \
67862306a36Sopenharmony_ci				    format_func,			       \
67962306a36Sopenharmony_ci				    assert_type,			       \
68062306a36Sopenharmony_ci				    left,				       \
68162306a36Sopenharmony_ci				    op,					       \
68262306a36Sopenharmony_ci				    right,				       \
68362306a36Sopenharmony_ci				    fmt,				       \
68462306a36Sopenharmony_ci				    ...)				       \
68562306a36Sopenharmony_cido {									       \
68662306a36Sopenharmony_ci	const typeof(left) __left = (left);				       \
68762306a36Sopenharmony_ci	const typeof(right) __right = (right);				       \
68862306a36Sopenharmony_ci	static const struct kunit_binary_assert_text __text = {		       \
68962306a36Sopenharmony_ci		.operation = #op,					       \
69062306a36Sopenharmony_ci		.left_text = #left,					       \
69162306a36Sopenharmony_ci		.right_text = #right,					       \
69262306a36Sopenharmony_ci	};								       \
69362306a36Sopenharmony_ci									       \
69462306a36Sopenharmony_ci	if (likely(__left op __right))					       \
69562306a36Sopenharmony_ci		break;							       \
69662306a36Sopenharmony_ci									       \
69762306a36Sopenharmony_ci	_KUNIT_FAILED(test,						       \
69862306a36Sopenharmony_ci		      assert_type,					       \
69962306a36Sopenharmony_ci		      assert_class,					       \
70062306a36Sopenharmony_ci		      format_func,					       \
70162306a36Sopenharmony_ci		      KUNIT_INIT_ASSERT(.text = &__text,		       \
70262306a36Sopenharmony_ci					.left_value = __left,		       \
70362306a36Sopenharmony_ci					.right_value = __right),	       \
70462306a36Sopenharmony_ci		      fmt,						       \
70562306a36Sopenharmony_ci		      ##__VA_ARGS__);					       \
70662306a36Sopenharmony_ci} while (0)
70762306a36Sopenharmony_ci
70862306a36Sopenharmony_ci#define KUNIT_BINARY_INT_ASSERTION(test,				       \
70962306a36Sopenharmony_ci				   assert_type,				       \
71062306a36Sopenharmony_ci				   left,				       \
71162306a36Sopenharmony_ci				   op,					       \
71262306a36Sopenharmony_ci				   right,				       \
71362306a36Sopenharmony_ci				   fmt,					       \
71462306a36Sopenharmony_ci				    ...)				       \
71562306a36Sopenharmony_ci	KUNIT_BASE_BINARY_ASSERTION(test,				       \
71662306a36Sopenharmony_ci				    kunit_binary_assert,		       \
71762306a36Sopenharmony_ci				    kunit_binary_assert_format,		       \
71862306a36Sopenharmony_ci				    assert_type,			       \
71962306a36Sopenharmony_ci				    left, op, right,			       \
72062306a36Sopenharmony_ci				    fmt,				       \
72162306a36Sopenharmony_ci				    ##__VA_ARGS__)
72262306a36Sopenharmony_ci
72362306a36Sopenharmony_ci#define KUNIT_BINARY_PTR_ASSERTION(test,				       \
72462306a36Sopenharmony_ci				   assert_type,				       \
72562306a36Sopenharmony_ci				   left,				       \
72662306a36Sopenharmony_ci				   op,					       \
72762306a36Sopenharmony_ci				   right,				       \
72862306a36Sopenharmony_ci				   fmt,					       \
72962306a36Sopenharmony_ci				    ...)				       \
73062306a36Sopenharmony_ci	KUNIT_BASE_BINARY_ASSERTION(test,				       \
73162306a36Sopenharmony_ci				    kunit_binary_ptr_assert,		       \
73262306a36Sopenharmony_ci				    kunit_binary_ptr_assert_format,	       \
73362306a36Sopenharmony_ci				    assert_type,			       \
73462306a36Sopenharmony_ci				    left, op, right,			       \
73562306a36Sopenharmony_ci				    fmt,				       \
73662306a36Sopenharmony_ci				    ##__VA_ARGS__)
73762306a36Sopenharmony_ci
73862306a36Sopenharmony_ci#define KUNIT_BINARY_STR_ASSERTION(test,				       \
73962306a36Sopenharmony_ci				   assert_type,				       \
74062306a36Sopenharmony_ci				   left,				       \
74162306a36Sopenharmony_ci				   op,					       \
74262306a36Sopenharmony_ci				   right,				       \
74362306a36Sopenharmony_ci				   fmt,					       \
74462306a36Sopenharmony_ci				   ...)					       \
74562306a36Sopenharmony_cido {									       \
74662306a36Sopenharmony_ci	const char *__left = (left);					       \
74762306a36Sopenharmony_ci	const char *__right = (right);					       \
74862306a36Sopenharmony_ci	static const struct kunit_binary_assert_text __text = {		       \
74962306a36Sopenharmony_ci		.operation = #op,					       \
75062306a36Sopenharmony_ci		.left_text = #left,					       \
75162306a36Sopenharmony_ci		.right_text = #right,					       \
75262306a36Sopenharmony_ci	};								       \
75362306a36Sopenharmony_ci									       \
75462306a36Sopenharmony_ci	if (likely(strcmp(__left, __right) op 0))			       \
75562306a36Sopenharmony_ci		break;							       \
75662306a36Sopenharmony_ci									       \
75762306a36Sopenharmony_ci									       \
75862306a36Sopenharmony_ci	_KUNIT_FAILED(test,						       \
75962306a36Sopenharmony_ci		      assert_type,					       \
76062306a36Sopenharmony_ci		      kunit_binary_str_assert,				       \
76162306a36Sopenharmony_ci		      kunit_binary_str_assert_format,			       \
76262306a36Sopenharmony_ci		      KUNIT_INIT_ASSERT(.text = &__text,		       \
76362306a36Sopenharmony_ci					.left_value = __left,		       \
76462306a36Sopenharmony_ci					.right_value = __right),	       \
76562306a36Sopenharmony_ci		      fmt,						       \
76662306a36Sopenharmony_ci		      ##__VA_ARGS__);					       \
76762306a36Sopenharmony_ci} while (0)
76862306a36Sopenharmony_ci
76962306a36Sopenharmony_ci#define KUNIT_MEM_ASSERTION(test,					       \
77062306a36Sopenharmony_ci			    assert_type,				       \
77162306a36Sopenharmony_ci			    left,					       \
77262306a36Sopenharmony_ci			    op,						       \
77362306a36Sopenharmony_ci			    right,					       \
77462306a36Sopenharmony_ci			    size_,					       \
77562306a36Sopenharmony_ci			    fmt,					       \
77662306a36Sopenharmony_ci			    ...)					       \
77762306a36Sopenharmony_cido {									       \
77862306a36Sopenharmony_ci	const void *__left = (left);					       \
77962306a36Sopenharmony_ci	const void *__right = (right);					       \
78062306a36Sopenharmony_ci	const size_t __size = (size_);					       \
78162306a36Sopenharmony_ci	static const struct kunit_binary_assert_text __text = {		       \
78262306a36Sopenharmony_ci		.operation = #op,					       \
78362306a36Sopenharmony_ci		.left_text = #left,					       \
78462306a36Sopenharmony_ci		.right_text = #right,					       \
78562306a36Sopenharmony_ci	};								       \
78662306a36Sopenharmony_ci									       \
78762306a36Sopenharmony_ci	if (likely(__left && __right))					       \
78862306a36Sopenharmony_ci		if (likely(memcmp(__left, __right, __size) op 0))	       \
78962306a36Sopenharmony_ci			break;						       \
79062306a36Sopenharmony_ci									       \
79162306a36Sopenharmony_ci	_KUNIT_FAILED(test,						       \
79262306a36Sopenharmony_ci		      assert_type,					       \
79362306a36Sopenharmony_ci		      kunit_mem_assert,					       \
79462306a36Sopenharmony_ci		      kunit_mem_assert_format,				       \
79562306a36Sopenharmony_ci		      KUNIT_INIT_ASSERT(.text = &__text,		       \
79662306a36Sopenharmony_ci					.left_value = __left,		       \
79762306a36Sopenharmony_ci					.right_value = __right,		       \
79862306a36Sopenharmony_ci					.size = __size),		       \
79962306a36Sopenharmony_ci		      fmt,						       \
80062306a36Sopenharmony_ci		      ##__VA_ARGS__);					       \
80162306a36Sopenharmony_ci} while (0)
80262306a36Sopenharmony_ci
80362306a36Sopenharmony_ci#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
80462306a36Sopenharmony_ci						assert_type,		       \
80562306a36Sopenharmony_ci						ptr,			       \
80662306a36Sopenharmony_ci						fmt,			       \
80762306a36Sopenharmony_ci						...)			       \
80862306a36Sopenharmony_cido {									       \
80962306a36Sopenharmony_ci	const typeof(ptr) __ptr = (ptr);				       \
81062306a36Sopenharmony_ci									       \
81162306a36Sopenharmony_ci	if (!IS_ERR_OR_NULL(__ptr))					       \
81262306a36Sopenharmony_ci		break;							       \
81362306a36Sopenharmony_ci									       \
81462306a36Sopenharmony_ci	_KUNIT_FAILED(test,						       \
81562306a36Sopenharmony_ci		      assert_type,					       \
81662306a36Sopenharmony_ci		      kunit_ptr_not_err_assert,				       \
81762306a36Sopenharmony_ci		      kunit_ptr_not_err_assert_format,			       \
81862306a36Sopenharmony_ci		      KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr),	       \
81962306a36Sopenharmony_ci		      fmt,						       \
82062306a36Sopenharmony_ci		      ##__VA_ARGS__);					       \
82162306a36Sopenharmony_ci} while (0)
82262306a36Sopenharmony_ci
82362306a36Sopenharmony_ci/**
82462306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
82562306a36Sopenharmony_ci * @test: The test context object.
82662306a36Sopenharmony_ci * @condition: an arbitrary boolean expression. The test fails when this does
82762306a36Sopenharmony_ci * not evaluate to true.
82862306a36Sopenharmony_ci *
82962306a36Sopenharmony_ci * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
83062306a36Sopenharmony_ci * to fail when the specified condition is not met; however, it will not prevent
83162306a36Sopenharmony_ci * the test case from continuing to run; this is otherwise known as an
83262306a36Sopenharmony_ci * *expectation failure*.
83362306a36Sopenharmony_ci */
83462306a36Sopenharmony_ci#define KUNIT_EXPECT_TRUE(test, condition) \
83562306a36Sopenharmony_ci	KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
83662306a36Sopenharmony_ci
83762306a36Sopenharmony_ci#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...)		       \
83862306a36Sopenharmony_ci	KUNIT_TRUE_MSG_ASSERTION(test,					       \
83962306a36Sopenharmony_ci				 KUNIT_EXPECTATION,			       \
84062306a36Sopenharmony_ci				 condition,				       \
84162306a36Sopenharmony_ci				 fmt,					       \
84262306a36Sopenharmony_ci				 ##__VA_ARGS__)
84362306a36Sopenharmony_ci
84462306a36Sopenharmony_ci/**
84562306a36Sopenharmony_ci * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
84662306a36Sopenharmony_ci * @test: The test context object.
84762306a36Sopenharmony_ci * @condition: an arbitrary boolean expression. The test fails when this does
84862306a36Sopenharmony_ci * not evaluate to false.
84962306a36Sopenharmony_ci *
85062306a36Sopenharmony_ci * Sets an expectation that @condition evaluates to false. See
85162306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE() for more information.
85262306a36Sopenharmony_ci */
85362306a36Sopenharmony_ci#define KUNIT_EXPECT_FALSE(test, condition) \
85462306a36Sopenharmony_ci	KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
85562306a36Sopenharmony_ci
85662306a36Sopenharmony_ci#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...)		       \
85762306a36Sopenharmony_ci	KUNIT_FALSE_MSG_ASSERTION(test,					       \
85862306a36Sopenharmony_ci				  KUNIT_EXPECTATION,			       \
85962306a36Sopenharmony_ci				  condition,				       \
86062306a36Sopenharmony_ci				  fmt,					       \
86162306a36Sopenharmony_ci				  ##__VA_ARGS__)
86262306a36Sopenharmony_ci
86362306a36Sopenharmony_ci/**
86462306a36Sopenharmony_ci * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
86562306a36Sopenharmony_ci * @test: The test context object.
86662306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a primitive C type.
86762306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a primitive C type.
86862306a36Sopenharmony_ci *
86962306a36Sopenharmony_ci * Sets an expectation that the values that @left and @right evaluate to are
87062306a36Sopenharmony_ci * equal. This is semantically equivalent to
87162306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
87262306a36Sopenharmony_ci * more information.
87362306a36Sopenharmony_ci */
87462306a36Sopenharmony_ci#define KUNIT_EXPECT_EQ(test, left, right) \
87562306a36Sopenharmony_ci	KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
87662306a36Sopenharmony_ci
87762306a36Sopenharmony_ci#define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...)		       \
87862306a36Sopenharmony_ci	KUNIT_BINARY_INT_ASSERTION(test,				       \
87962306a36Sopenharmony_ci				   KUNIT_EXPECTATION,			       \
88062306a36Sopenharmony_ci				   left, ==, right,			       \
88162306a36Sopenharmony_ci				   fmt,					       \
88262306a36Sopenharmony_ci				    ##__VA_ARGS__)
88362306a36Sopenharmony_ci
88462306a36Sopenharmony_ci/**
88562306a36Sopenharmony_ci * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
88662306a36Sopenharmony_ci * @test: The test context object.
88762306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a pointer.
88862306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a pointer.
88962306a36Sopenharmony_ci *
89062306a36Sopenharmony_ci * Sets an expectation that the values that @left and @right evaluate to are
89162306a36Sopenharmony_ci * equal. This is semantically equivalent to
89262306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
89362306a36Sopenharmony_ci * more information.
89462306a36Sopenharmony_ci */
89562306a36Sopenharmony_ci#define KUNIT_EXPECT_PTR_EQ(test, left, right)				       \
89662306a36Sopenharmony_ci	KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
89762306a36Sopenharmony_ci
89862306a36Sopenharmony_ci#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
89962306a36Sopenharmony_ci	KUNIT_BINARY_PTR_ASSERTION(test,				       \
90062306a36Sopenharmony_ci				   KUNIT_EXPECTATION,			       \
90162306a36Sopenharmony_ci				   left, ==, right,			       \
90262306a36Sopenharmony_ci				   fmt,					       \
90362306a36Sopenharmony_ci				   ##__VA_ARGS__)
90462306a36Sopenharmony_ci
90562306a36Sopenharmony_ci/**
90662306a36Sopenharmony_ci * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
90762306a36Sopenharmony_ci * @test: The test context object.
90862306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a primitive C type.
90962306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a primitive C type.
91062306a36Sopenharmony_ci *
91162306a36Sopenharmony_ci * Sets an expectation that the values that @left and @right evaluate to are not
91262306a36Sopenharmony_ci * equal. This is semantically equivalent to
91362306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
91462306a36Sopenharmony_ci * more information.
91562306a36Sopenharmony_ci */
91662306a36Sopenharmony_ci#define KUNIT_EXPECT_NE(test, left, right) \
91762306a36Sopenharmony_ci	KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
91862306a36Sopenharmony_ci
91962306a36Sopenharmony_ci#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...)		       \
92062306a36Sopenharmony_ci	KUNIT_BINARY_INT_ASSERTION(test,				       \
92162306a36Sopenharmony_ci				   KUNIT_EXPECTATION,			       \
92262306a36Sopenharmony_ci				   left, !=, right,			       \
92362306a36Sopenharmony_ci				   fmt,					       \
92462306a36Sopenharmony_ci				    ##__VA_ARGS__)
92562306a36Sopenharmony_ci
92662306a36Sopenharmony_ci/**
92762306a36Sopenharmony_ci * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
92862306a36Sopenharmony_ci * @test: The test context object.
92962306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a pointer.
93062306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a pointer.
93162306a36Sopenharmony_ci *
93262306a36Sopenharmony_ci * Sets an expectation that the values that @left and @right evaluate to are not
93362306a36Sopenharmony_ci * equal. This is semantically equivalent to
93462306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
93562306a36Sopenharmony_ci * more information.
93662306a36Sopenharmony_ci */
93762306a36Sopenharmony_ci#define KUNIT_EXPECT_PTR_NE(test, left, right)				       \
93862306a36Sopenharmony_ci	KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
93962306a36Sopenharmony_ci
94062306a36Sopenharmony_ci#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
94162306a36Sopenharmony_ci	KUNIT_BINARY_PTR_ASSERTION(test,				       \
94262306a36Sopenharmony_ci				   KUNIT_EXPECTATION,			       \
94362306a36Sopenharmony_ci				   left, !=, right,			       \
94462306a36Sopenharmony_ci				   fmt,					       \
94562306a36Sopenharmony_ci				   ##__VA_ARGS__)
94662306a36Sopenharmony_ci
94762306a36Sopenharmony_ci/**
94862306a36Sopenharmony_ci * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
94962306a36Sopenharmony_ci * @test: The test context object.
95062306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a primitive C type.
95162306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a primitive C type.
95262306a36Sopenharmony_ci *
95362306a36Sopenharmony_ci * Sets an expectation that the value that @left evaluates to is less than the
95462306a36Sopenharmony_ci * value that @right evaluates to. This is semantically equivalent to
95562306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
95662306a36Sopenharmony_ci * more information.
95762306a36Sopenharmony_ci */
95862306a36Sopenharmony_ci#define KUNIT_EXPECT_LT(test, left, right) \
95962306a36Sopenharmony_ci	KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
96062306a36Sopenharmony_ci
96162306a36Sopenharmony_ci#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...)		       \
96262306a36Sopenharmony_ci	KUNIT_BINARY_INT_ASSERTION(test,				       \
96362306a36Sopenharmony_ci				   KUNIT_EXPECTATION,			       \
96462306a36Sopenharmony_ci				   left, <, right,			       \
96562306a36Sopenharmony_ci				   fmt,					       \
96662306a36Sopenharmony_ci				    ##__VA_ARGS__)
96762306a36Sopenharmony_ci
96862306a36Sopenharmony_ci/**
96962306a36Sopenharmony_ci * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
97062306a36Sopenharmony_ci * @test: The test context object.
97162306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a primitive C type.
97262306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a primitive C type.
97362306a36Sopenharmony_ci *
97462306a36Sopenharmony_ci * Sets an expectation that the value that @left evaluates to is less than or
97562306a36Sopenharmony_ci * equal to the value that @right evaluates to. Semantically this is equivalent
97662306a36Sopenharmony_ci * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
97762306a36Sopenharmony_ci * more information.
97862306a36Sopenharmony_ci */
97962306a36Sopenharmony_ci#define KUNIT_EXPECT_LE(test, left, right) \
98062306a36Sopenharmony_ci	KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
98162306a36Sopenharmony_ci
98262306a36Sopenharmony_ci#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...)		       \
98362306a36Sopenharmony_ci	KUNIT_BINARY_INT_ASSERTION(test,				       \
98462306a36Sopenharmony_ci				   KUNIT_EXPECTATION,			       \
98562306a36Sopenharmony_ci				   left, <=, right,			       \
98662306a36Sopenharmony_ci				   fmt,					       \
98762306a36Sopenharmony_ci				    ##__VA_ARGS__)
98862306a36Sopenharmony_ci
98962306a36Sopenharmony_ci/**
99062306a36Sopenharmony_ci * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
99162306a36Sopenharmony_ci * @test: The test context object.
99262306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a primitive C type.
99362306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a primitive C type.
99462306a36Sopenharmony_ci *
99562306a36Sopenharmony_ci * Sets an expectation that the value that @left evaluates to is greater than
99662306a36Sopenharmony_ci * the value that @right evaluates to. This is semantically equivalent to
99762306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
99862306a36Sopenharmony_ci * more information.
99962306a36Sopenharmony_ci */
100062306a36Sopenharmony_ci#define KUNIT_EXPECT_GT(test, left, right) \
100162306a36Sopenharmony_ci	KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
100262306a36Sopenharmony_ci
100362306a36Sopenharmony_ci#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...)		       \
100462306a36Sopenharmony_ci	KUNIT_BINARY_INT_ASSERTION(test,				       \
100562306a36Sopenharmony_ci				   KUNIT_EXPECTATION,			       \
100662306a36Sopenharmony_ci				   left, >, right,			       \
100762306a36Sopenharmony_ci				   fmt,					       \
100862306a36Sopenharmony_ci				    ##__VA_ARGS__)
100962306a36Sopenharmony_ci
101062306a36Sopenharmony_ci/**
101162306a36Sopenharmony_ci * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
101262306a36Sopenharmony_ci * @test: The test context object.
101362306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a primitive C type.
101462306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a primitive C type.
101562306a36Sopenharmony_ci *
101662306a36Sopenharmony_ci * Sets an expectation that the value that @left evaluates to is greater than
101762306a36Sopenharmony_ci * the value that @right evaluates to. This is semantically equivalent to
101862306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
101962306a36Sopenharmony_ci * more information.
102062306a36Sopenharmony_ci */
102162306a36Sopenharmony_ci#define KUNIT_EXPECT_GE(test, left, right) \
102262306a36Sopenharmony_ci	KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
102362306a36Sopenharmony_ci
102462306a36Sopenharmony_ci#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...)		       \
102562306a36Sopenharmony_ci	KUNIT_BINARY_INT_ASSERTION(test,				       \
102662306a36Sopenharmony_ci				   KUNIT_EXPECTATION,			       \
102762306a36Sopenharmony_ci				   left, >=, right,			       \
102862306a36Sopenharmony_ci				   fmt,					       \
102962306a36Sopenharmony_ci				    ##__VA_ARGS__)
103062306a36Sopenharmony_ci
103162306a36Sopenharmony_ci/**
103262306a36Sopenharmony_ci * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
103362306a36Sopenharmony_ci * @test: The test context object.
103462306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a null terminated string.
103562306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a null terminated string.
103662306a36Sopenharmony_ci *
103762306a36Sopenharmony_ci * Sets an expectation that the values that @left and @right evaluate to are
103862306a36Sopenharmony_ci * equal. This is semantically equivalent to
103962306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
104062306a36Sopenharmony_ci * for more information.
104162306a36Sopenharmony_ci */
104262306a36Sopenharmony_ci#define KUNIT_EXPECT_STREQ(test, left, right) \
104362306a36Sopenharmony_ci	KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
104462306a36Sopenharmony_ci
104562306a36Sopenharmony_ci#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...)		       \
104662306a36Sopenharmony_ci	KUNIT_BINARY_STR_ASSERTION(test,				       \
104762306a36Sopenharmony_ci				   KUNIT_EXPECTATION,			       \
104862306a36Sopenharmony_ci				   left, ==, right,			       \
104962306a36Sopenharmony_ci				   fmt,					       \
105062306a36Sopenharmony_ci				   ##__VA_ARGS__)
105162306a36Sopenharmony_ci
105262306a36Sopenharmony_ci/**
105362306a36Sopenharmony_ci * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
105462306a36Sopenharmony_ci * @test: The test context object.
105562306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a null terminated string.
105662306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a null terminated string.
105762306a36Sopenharmony_ci *
105862306a36Sopenharmony_ci * Sets an expectation that the values that @left and @right evaluate to are
105962306a36Sopenharmony_ci * not equal. This is semantically equivalent to
106062306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
106162306a36Sopenharmony_ci * for more information.
106262306a36Sopenharmony_ci */
106362306a36Sopenharmony_ci#define KUNIT_EXPECT_STRNEQ(test, left, right) \
106462306a36Sopenharmony_ci	KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
106562306a36Sopenharmony_ci
106662306a36Sopenharmony_ci#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
106762306a36Sopenharmony_ci	KUNIT_BINARY_STR_ASSERTION(test,				       \
106862306a36Sopenharmony_ci				   KUNIT_EXPECTATION,			       \
106962306a36Sopenharmony_ci				   left, !=, right,			       \
107062306a36Sopenharmony_ci				   fmt,					       \
107162306a36Sopenharmony_ci				   ##__VA_ARGS__)
107262306a36Sopenharmony_ci
107362306a36Sopenharmony_ci/**
107462306a36Sopenharmony_ci * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.
107562306a36Sopenharmony_ci * @test: The test context object.
107662306a36Sopenharmony_ci * @left: An arbitrary expression that evaluates to the specified size.
107762306a36Sopenharmony_ci * @right: An arbitrary expression that evaluates to the specified size.
107862306a36Sopenharmony_ci * @size: Number of bytes compared.
107962306a36Sopenharmony_ci *
108062306a36Sopenharmony_ci * Sets an expectation that the values that @left and @right evaluate to are
108162306a36Sopenharmony_ci * equal. This is semantically equivalent to
108262306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
108362306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE() for more information.
108462306a36Sopenharmony_ci *
108562306a36Sopenharmony_ci * Although this expectation works for any memory block, it is not recommended
108662306a36Sopenharmony_ci * for comparing more structured data, such as structs. This expectation is
108762306a36Sopenharmony_ci * recommended for comparing, for example, data arrays.
108862306a36Sopenharmony_ci */
108962306a36Sopenharmony_ci#define KUNIT_EXPECT_MEMEQ(test, left, right, size) \
109062306a36Sopenharmony_ci	KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)
109162306a36Sopenharmony_ci
109262306a36Sopenharmony_ci#define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...)	       \
109362306a36Sopenharmony_ci	KUNIT_MEM_ASSERTION(test,					       \
109462306a36Sopenharmony_ci			    KUNIT_EXPECTATION,				       \
109562306a36Sopenharmony_ci			    left, ==, right,				       \
109662306a36Sopenharmony_ci			    size,					       \
109762306a36Sopenharmony_ci			    fmt,					       \
109862306a36Sopenharmony_ci			    ##__VA_ARGS__)
109962306a36Sopenharmony_ci
110062306a36Sopenharmony_ci/**
110162306a36Sopenharmony_ci * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.
110262306a36Sopenharmony_ci * @test: The test context object.
110362306a36Sopenharmony_ci * @left: An arbitrary expression that evaluates to the specified size.
110462306a36Sopenharmony_ci * @right: An arbitrary expression that evaluates to the specified size.
110562306a36Sopenharmony_ci * @size: Number of bytes compared.
110662306a36Sopenharmony_ci *
110762306a36Sopenharmony_ci * Sets an expectation that the values that @left and @right evaluate to are
110862306a36Sopenharmony_ci * not equal. This is semantically equivalent to
110962306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See
111062306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE() for more information.
111162306a36Sopenharmony_ci *
111262306a36Sopenharmony_ci * Although this expectation works for any memory block, it is not recommended
111362306a36Sopenharmony_ci * for comparing more structured data, such as structs. This expectation is
111462306a36Sopenharmony_ci * recommended for comparing, for example, data arrays.
111562306a36Sopenharmony_ci */
111662306a36Sopenharmony_ci#define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \
111762306a36Sopenharmony_ci	KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)
111862306a36Sopenharmony_ci
111962306a36Sopenharmony_ci#define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...)	       \
112062306a36Sopenharmony_ci	KUNIT_MEM_ASSERTION(test,					       \
112162306a36Sopenharmony_ci			    KUNIT_EXPECTATION,				       \
112262306a36Sopenharmony_ci			    left, !=, right,				       \
112362306a36Sopenharmony_ci			    size,					       \
112462306a36Sopenharmony_ci			    fmt,					       \
112562306a36Sopenharmony_ci			    ##__VA_ARGS__)
112662306a36Sopenharmony_ci
112762306a36Sopenharmony_ci/**
112862306a36Sopenharmony_ci * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
112962306a36Sopenharmony_ci * @test: The test context object.
113062306a36Sopenharmony_ci * @ptr: an arbitrary pointer.
113162306a36Sopenharmony_ci *
113262306a36Sopenharmony_ci * Sets an expectation that the value that @ptr evaluates to is null. This is
113362306a36Sopenharmony_ci * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
113462306a36Sopenharmony_ci * See KUNIT_EXPECT_TRUE() for more information.
113562306a36Sopenharmony_ci */
113662306a36Sopenharmony_ci#define KUNIT_EXPECT_NULL(test, ptr)				               \
113762306a36Sopenharmony_ci	KUNIT_EXPECT_NULL_MSG(test,					       \
113862306a36Sopenharmony_ci			      ptr,					       \
113962306a36Sopenharmony_ci			      NULL)
114062306a36Sopenharmony_ci
114162306a36Sopenharmony_ci#define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...)	                       \
114262306a36Sopenharmony_ci	KUNIT_BINARY_PTR_ASSERTION(test,				       \
114362306a36Sopenharmony_ci				   KUNIT_EXPECTATION,			       \
114462306a36Sopenharmony_ci				   ptr, ==, NULL,			       \
114562306a36Sopenharmony_ci				   fmt,					       \
114662306a36Sopenharmony_ci				   ##__VA_ARGS__)
114762306a36Sopenharmony_ci
114862306a36Sopenharmony_ci/**
114962306a36Sopenharmony_ci * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
115062306a36Sopenharmony_ci * @test: The test context object.
115162306a36Sopenharmony_ci * @ptr: an arbitrary pointer.
115262306a36Sopenharmony_ci *
115362306a36Sopenharmony_ci * Sets an expectation that the value that @ptr evaluates to is not null. This
115462306a36Sopenharmony_ci * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
115562306a36Sopenharmony_ci * See KUNIT_EXPECT_TRUE() for more information.
115662306a36Sopenharmony_ci */
115762306a36Sopenharmony_ci#define KUNIT_EXPECT_NOT_NULL(test, ptr)			               \
115862306a36Sopenharmony_ci	KUNIT_EXPECT_NOT_NULL_MSG(test,					       \
115962306a36Sopenharmony_ci				  ptr,					       \
116062306a36Sopenharmony_ci				  NULL)
116162306a36Sopenharmony_ci
116262306a36Sopenharmony_ci#define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...)	                       \
116362306a36Sopenharmony_ci	KUNIT_BINARY_PTR_ASSERTION(test,				       \
116462306a36Sopenharmony_ci				   KUNIT_EXPECTATION,			       \
116562306a36Sopenharmony_ci				   ptr, !=, NULL,			       \
116662306a36Sopenharmony_ci				   fmt,					       \
116762306a36Sopenharmony_ci				   ##__VA_ARGS__)
116862306a36Sopenharmony_ci
116962306a36Sopenharmony_ci/**
117062306a36Sopenharmony_ci * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
117162306a36Sopenharmony_ci * @test: The test context object.
117262306a36Sopenharmony_ci * @ptr: an arbitrary pointer.
117362306a36Sopenharmony_ci *
117462306a36Sopenharmony_ci * Sets an expectation that the value that @ptr evaluates to is not null and not
117562306a36Sopenharmony_ci * an errno stored in a pointer. This is semantically equivalent to
117662306a36Sopenharmony_ci * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
117762306a36Sopenharmony_ci * more information.
117862306a36Sopenharmony_ci */
117962306a36Sopenharmony_ci#define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
118062306a36Sopenharmony_ci	KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
118162306a36Sopenharmony_ci
118262306a36Sopenharmony_ci#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
118362306a36Sopenharmony_ci	KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
118462306a36Sopenharmony_ci						KUNIT_EXPECTATION,	       \
118562306a36Sopenharmony_ci						ptr,			       \
118662306a36Sopenharmony_ci						fmt,			       \
118762306a36Sopenharmony_ci						##__VA_ARGS__)
118862306a36Sopenharmony_ci
118962306a36Sopenharmony_ci#define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
119062306a36Sopenharmony_ci	KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
119162306a36Sopenharmony_ci
119262306a36Sopenharmony_ci/**
119362306a36Sopenharmony_ci * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
119462306a36Sopenharmony_ci * @test: The test context object.
119562306a36Sopenharmony_ci * @condition: an arbitrary boolean expression. The test fails and aborts when
119662306a36Sopenharmony_ci * this does not evaluate to true.
119762306a36Sopenharmony_ci *
119862306a36Sopenharmony_ci * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
119962306a36Sopenharmony_ci * fail *and immediately abort* when the specified condition is not met. Unlike
120062306a36Sopenharmony_ci * an expectation failure, it will prevent the test case from continuing to run;
120162306a36Sopenharmony_ci * this is otherwise known as an *assertion failure*.
120262306a36Sopenharmony_ci */
120362306a36Sopenharmony_ci#define KUNIT_ASSERT_TRUE(test, condition) \
120462306a36Sopenharmony_ci	KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
120562306a36Sopenharmony_ci
120662306a36Sopenharmony_ci#define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...)		       \
120762306a36Sopenharmony_ci	KUNIT_TRUE_MSG_ASSERTION(test,					       \
120862306a36Sopenharmony_ci				 KUNIT_ASSERTION,			       \
120962306a36Sopenharmony_ci				 condition,				       \
121062306a36Sopenharmony_ci				 fmt,					       \
121162306a36Sopenharmony_ci				 ##__VA_ARGS__)
121262306a36Sopenharmony_ci
121362306a36Sopenharmony_ci/**
121462306a36Sopenharmony_ci * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
121562306a36Sopenharmony_ci * @test: The test context object.
121662306a36Sopenharmony_ci * @condition: an arbitrary boolean expression.
121762306a36Sopenharmony_ci *
121862306a36Sopenharmony_ci * Sets an assertion that the value that @condition evaluates to is false. This
121962306a36Sopenharmony_ci * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
122062306a36Sopenharmony_ci * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
122162306a36Sopenharmony_ci */
122262306a36Sopenharmony_ci#define KUNIT_ASSERT_FALSE(test, condition) \
122362306a36Sopenharmony_ci	KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
122462306a36Sopenharmony_ci
122562306a36Sopenharmony_ci#define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...)		       \
122662306a36Sopenharmony_ci	KUNIT_FALSE_MSG_ASSERTION(test,					       \
122762306a36Sopenharmony_ci				  KUNIT_ASSERTION,			       \
122862306a36Sopenharmony_ci				  condition,				       \
122962306a36Sopenharmony_ci				  fmt,					       \
123062306a36Sopenharmony_ci				  ##__VA_ARGS__)
123162306a36Sopenharmony_ci
123262306a36Sopenharmony_ci/**
123362306a36Sopenharmony_ci * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
123462306a36Sopenharmony_ci * @test: The test context object.
123562306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a primitive C type.
123662306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a primitive C type.
123762306a36Sopenharmony_ci *
123862306a36Sopenharmony_ci * Sets an assertion that the values that @left and @right evaluate to are
123962306a36Sopenharmony_ci * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
124062306a36Sopenharmony_ci * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
124162306a36Sopenharmony_ci */
124262306a36Sopenharmony_ci#define KUNIT_ASSERT_EQ(test, left, right) \
124362306a36Sopenharmony_ci	KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
124462306a36Sopenharmony_ci
124562306a36Sopenharmony_ci#define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...)		       \
124662306a36Sopenharmony_ci	KUNIT_BINARY_INT_ASSERTION(test,				       \
124762306a36Sopenharmony_ci				   KUNIT_ASSERTION,			       \
124862306a36Sopenharmony_ci				   left, ==, right,			       \
124962306a36Sopenharmony_ci				   fmt,					       \
125062306a36Sopenharmony_ci				    ##__VA_ARGS__)
125162306a36Sopenharmony_ci
125262306a36Sopenharmony_ci/**
125362306a36Sopenharmony_ci * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
125462306a36Sopenharmony_ci * @test: The test context object.
125562306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a pointer.
125662306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a pointer.
125762306a36Sopenharmony_ci *
125862306a36Sopenharmony_ci * Sets an assertion that the values that @left and @right evaluate to are
125962306a36Sopenharmony_ci * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
126062306a36Sopenharmony_ci * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
126162306a36Sopenharmony_ci */
126262306a36Sopenharmony_ci#define KUNIT_ASSERT_PTR_EQ(test, left, right) \
126362306a36Sopenharmony_ci	KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
126462306a36Sopenharmony_ci
126562306a36Sopenharmony_ci#define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
126662306a36Sopenharmony_ci	KUNIT_BINARY_PTR_ASSERTION(test,				       \
126762306a36Sopenharmony_ci				   KUNIT_ASSERTION,			       \
126862306a36Sopenharmony_ci				   left, ==, right,			       \
126962306a36Sopenharmony_ci				   fmt,					       \
127062306a36Sopenharmony_ci				   ##__VA_ARGS__)
127162306a36Sopenharmony_ci
127262306a36Sopenharmony_ci/**
127362306a36Sopenharmony_ci * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
127462306a36Sopenharmony_ci * @test: The test context object.
127562306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a primitive C type.
127662306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a primitive C type.
127762306a36Sopenharmony_ci *
127862306a36Sopenharmony_ci * Sets an assertion that the values that @left and @right evaluate to are not
127962306a36Sopenharmony_ci * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
128062306a36Sopenharmony_ci * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
128162306a36Sopenharmony_ci */
128262306a36Sopenharmony_ci#define KUNIT_ASSERT_NE(test, left, right) \
128362306a36Sopenharmony_ci	KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
128462306a36Sopenharmony_ci
128562306a36Sopenharmony_ci#define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...)		       \
128662306a36Sopenharmony_ci	KUNIT_BINARY_INT_ASSERTION(test,				       \
128762306a36Sopenharmony_ci				   KUNIT_ASSERTION,			       \
128862306a36Sopenharmony_ci				   left, !=, right,			       \
128962306a36Sopenharmony_ci				   fmt,					       \
129062306a36Sopenharmony_ci				    ##__VA_ARGS__)
129162306a36Sopenharmony_ci
129262306a36Sopenharmony_ci/**
129362306a36Sopenharmony_ci * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
129462306a36Sopenharmony_ci * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
129562306a36Sopenharmony_ci * @test: The test context object.
129662306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a pointer.
129762306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a pointer.
129862306a36Sopenharmony_ci *
129962306a36Sopenharmony_ci * Sets an assertion that the values that @left and @right evaluate to are not
130062306a36Sopenharmony_ci * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
130162306a36Sopenharmony_ci * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
130262306a36Sopenharmony_ci */
130362306a36Sopenharmony_ci#define KUNIT_ASSERT_PTR_NE(test, left, right) \
130462306a36Sopenharmony_ci	KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
130562306a36Sopenharmony_ci
130662306a36Sopenharmony_ci#define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
130762306a36Sopenharmony_ci	KUNIT_BINARY_PTR_ASSERTION(test,				       \
130862306a36Sopenharmony_ci				   KUNIT_ASSERTION,			       \
130962306a36Sopenharmony_ci				   left, !=, right,			       \
131062306a36Sopenharmony_ci				   fmt,					       \
131162306a36Sopenharmony_ci				   ##__VA_ARGS__)
131262306a36Sopenharmony_ci/**
131362306a36Sopenharmony_ci * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
131462306a36Sopenharmony_ci * @test: The test context object.
131562306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a primitive C type.
131662306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a primitive C type.
131762306a36Sopenharmony_ci *
131862306a36Sopenharmony_ci * Sets an assertion that the value that @left evaluates to is less than the
131962306a36Sopenharmony_ci * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
132062306a36Sopenharmony_ci * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
132162306a36Sopenharmony_ci * is not met.
132262306a36Sopenharmony_ci */
132362306a36Sopenharmony_ci#define KUNIT_ASSERT_LT(test, left, right) \
132462306a36Sopenharmony_ci	KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
132562306a36Sopenharmony_ci
132662306a36Sopenharmony_ci#define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...)		       \
132762306a36Sopenharmony_ci	KUNIT_BINARY_INT_ASSERTION(test,				       \
132862306a36Sopenharmony_ci				   KUNIT_ASSERTION,			       \
132962306a36Sopenharmony_ci				   left, <, right,			       \
133062306a36Sopenharmony_ci				   fmt,					       \
133162306a36Sopenharmony_ci				    ##__VA_ARGS__)
133262306a36Sopenharmony_ci/**
133362306a36Sopenharmony_ci * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
133462306a36Sopenharmony_ci * @test: The test context object.
133562306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a primitive C type.
133662306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a primitive C type.
133762306a36Sopenharmony_ci *
133862306a36Sopenharmony_ci * Sets an assertion that the value that @left evaluates to is less than or
133962306a36Sopenharmony_ci * equal to the value that @right evaluates to. This is the same as
134062306a36Sopenharmony_ci * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
134162306a36Sopenharmony_ci * KUNIT_ASSERT_TRUE()) when the assertion is not met.
134262306a36Sopenharmony_ci */
134362306a36Sopenharmony_ci#define KUNIT_ASSERT_LE(test, left, right) \
134462306a36Sopenharmony_ci	KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
134562306a36Sopenharmony_ci
134662306a36Sopenharmony_ci#define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...)		       \
134762306a36Sopenharmony_ci	KUNIT_BINARY_INT_ASSERTION(test,				       \
134862306a36Sopenharmony_ci				   KUNIT_ASSERTION,			       \
134962306a36Sopenharmony_ci				   left, <=, right,			       \
135062306a36Sopenharmony_ci				   fmt,					       \
135162306a36Sopenharmony_ci				    ##__VA_ARGS__)
135262306a36Sopenharmony_ci
135362306a36Sopenharmony_ci/**
135462306a36Sopenharmony_ci * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
135562306a36Sopenharmony_ci * @test: The test context object.
135662306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a primitive C type.
135762306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a primitive C type.
135862306a36Sopenharmony_ci *
135962306a36Sopenharmony_ci * Sets an assertion that the value that @left evaluates to is greater than the
136062306a36Sopenharmony_ci * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
136162306a36Sopenharmony_ci * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
136262306a36Sopenharmony_ci * is not met.
136362306a36Sopenharmony_ci */
136462306a36Sopenharmony_ci#define KUNIT_ASSERT_GT(test, left, right) \
136562306a36Sopenharmony_ci	KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
136662306a36Sopenharmony_ci
136762306a36Sopenharmony_ci#define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...)		       \
136862306a36Sopenharmony_ci	KUNIT_BINARY_INT_ASSERTION(test,				       \
136962306a36Sopenharmony_ci				   KUNIT_ASSERTION,			       \
137062306a36Sopenharmony_ci				   left, >, right,			       \
137162306a36Sopenharmony_ci				   fmt,					       \
137262306a36Sopenharmony_ci				    ##__VA_ARGS__)
137362306a36Sopenharmony_ci
137462306a36Sopenharmony_ci/**
137562306a36Sopenharmony_ci * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
137662306a36Sopenharmony_ci * @test: The test context object.
137762306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a primitive C type.
137862306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a primitive C type.
137962306a36Sopenharmony_ci *
138062306a36Sopenharmony_ci * Sets an assertion that the value that @left evaluates to is greater than the
138162306a36Sopenharmony_ci * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
138262306a36Sopenharmony_ci * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
138362306a36Sopenharmony_ci * is not met.
138462306a36Sopenharmony_ci */
138562306a36Sopenharmony_ci#define KUNIT_ASSERT_GE(test, left, right) \
138662306a36Sopenharmony_ci	KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
138762306a36Sopenharmony_ci
138862306a36Sopenharmony_ci#define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...)		       \
138962306a36Sopenharmony_ci	KUNIT_BINARY_INT_ASSERTION(test,				       \
139062306a36Sopenharmony_ci				   KUNIT_ASSERTION,			       \
139162306a36Sopenharmony_ci				   left, >=, right,			       \
139262306a36Sopenharmony_ci				   fmt,					       \
139362306a36Sopenharmony_ci				    ##__VA_ARGS__)
139462306a36Sopenharmony_ci
139562306a36Sopenharmony_ci/**
139662306a36Sopenharmony_ci * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
139762306a36Sopenharmony_ci * @test: The test context object.
139862306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a null terminated string.
139962306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a null terminated string.
140062306a36Sopenharmony_ci *
140162306a36Sopenharmony_ci * Sets an assertion that the values that @left and @right evaluate to are
140262306a36Sopenharmony_ci * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
140362306a36Sopenharmony_ci * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
140462306a36Sopenharmony_ci */
140562306a36Sopenharmony_ci#define KUNIT_ASSERT_STREQ(test, left, right) \
140662306a36Sopenharmony_ci	KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
140762306a36Sopenharmony_ci
140862306a36Sopenharmony_ci#define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...)		       \
140962306a36Sopenharmony_ci	KUNIT_BINARY_STR_ASSERTION(test,				       \
141062306a36Sopenharmony_ci				   KUNIT_ASSERTION,			       \
141162306a36Sopenharmony_ci				   left, ==, right,			       \
141262306a36Sopenharmony_ci				   fmt,					       \
141362306a36Sopenharmony_ci				   ##__VA_ARGS__)
141462306a36Sopenharmony_ci
141562306a36Sopenharmony_ci/**
141662306a36Sopenharmony_ci * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
141762306a36Sopenharmony_ci * @test: The test context object.
141862306a36Sopenharmony_ci * @left: an arbitrary expression that evaluates to a null terminated string.
141962306a36Sopenharmony_ci * @right: an arbitrary expression that evaluates to a null terminated string.
142062306a36Sopenharmony_ci *
142162306a36Sopenharmony_ci * Sets an expectation that the values that @left and @right evaluate to are
142262306a36Sopenharmony_ci * not equal. This is semantically equivalent to
142362306a36Sopenharmony_ci * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
142462306a36Sopenharmony_ci * for more information.
142562306a36Sopenharmony_ci */
142662306a36Sopenharmony_ci#define KUNIT_ASSERT_STRNEQ(test, left, right) \
142762306a36Sopenharmony_ci	KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
142862306a36Sopenharmony_ci
142962306a36Sopenharmony_ci#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
143062306a36Sopenharmony_ci	KUNIT_BINARY_STR_ASSERTION(test,				       \
143162306a36Sopenharmony_ci				   KUNIT_ASSERTION,			       \
143262306a36Sopenharmony_ci				   left, !=, right,			       \
143362306a36Sopenharmony_ci				   fmt,					       \
143462306a36Sopenharmony_ci				   ##__VA_ARGS__)
143562306a36Sopenharmony_ci
143662306a36Sopenharmony_ci/**
143762306a36Sopenharmony_ci * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
143862306a36Sopenharmony_ci * @test: The test context object.
143962306a36Sopenharmony_ci * @ptr: an arbitrary pointer.
144062306a36Sopenharmony_ci *
144162306a36Sopenharmony_ci * Sets an assertion that the values that @ptr evaluates to is null. This is
144262306a36Sopenharmony_ci * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
144362306a36Sopenharmony_ci * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
144462306a36Sopenharmony_ci */
144562306a36Sopenharmony_ci#define KUNIT_ASSERT_NULL(test, ptr) \
144662306a36Sopenharmony_ci	KUNIT_ASSERT_NULL_MSG(test,					       \
144762306a36Sopenharmony_ci			      ptr,					       \
144862306a36Sopenharmony_ci			      NULL)
144962306a36Sopenharmony_ci
145062306a36Sopenharmony_ci#define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
145162306a36Sopenharmony_ci	KUNIT_BINARY_PTR_ASSERTION(test,				       \
145262306a36Sopenharmony_ci				   KUNIT_ASSERTION,			       \
145362306a36Sopenharmony_ci				   ptr, ==, NULL,			       \
145462306a36Sopenharmony_ci				   fmt,					       \
145562306a36Sopenharmony_ci				   ##__VA_ARGS__)
145662306a36Sopenharmony_ci
145762306a36Sopenharmony_ci/**
145862306a36Sopenharmony_ci * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
145962306a36Sopenharmony_ci * @test: The test context object.
146062306a36Sopenharmony_ci * @ptr: an arbitrary pointer.
146162306a36Sopenharmony_ci *
146262306a36Sopenharmony_ci * Sets an assertion that the values that @ptr evaluates to is not null. This
146362306a36Sopenharmony_ci * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
146462306a36Sopenharmony_ci * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
146562306a36Sopenharmony_ci */
146662306a36Sopenharmony_ci#define KUNIT_ASSERT_NOT_NULL(test, ptr) \
146762306a36Sopenharmony_ci	KUNIT_ASSERT_NOT_NULL_MSG(test,					       \
146862306a36Sopenharmony_ci				  ptr,					       \
146962306a36Sopenharmony_ci				  NULL)
147062306a36Sopenharmony_ci
147162306a36Sopenharmony_ci#define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
147262306a36Sopenharmony_ci	KUNIT_BINARY_PTR_ASSERTION(test,				       \
147362306a36Sopenharmony_ci				   KUNIT_ASSERTION,			       \
147462306a36Sopenharmony_ci				   ptr, !=, NULL,			       \
147562306a36Sopenharmony_ci				   fmt,					       \
147662306a36Sopenharmony_ci				   ##__VA_ARGS__)
147762306a36Sopenharmony_ci
147862306a36Sopenharmony_ci/**
147962306a36Sopenharmony_ci * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
148062306a36Sopenharmony_ci * @test: The test context object.
148162306a36Sopenharmony_ci * @ptr: an arbitrary pointer.
148262306a36Sopenharmony_ci *
148362306a36Sopenharmony_ci * Sets an assertion that the value that @ptr evaluates to is not null and not
148462306a36Sopenharmony_ci * an errno stored in a pointer. This is the same as
148562306a36Sopenharmony_ci * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
148662306a36Sopenharmony_ci * KUNIT_ASSERT_TRUE()) when the assertion is not met.
148762306a36Sopenharmony_ci */
148862306a36Sopenharmony_ci#define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
148962306a36Sopenharmony_ci	KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
149062306a36Sopenharmony_ci
149162306a36Sopenharmony_ci#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
149262306a36Sopenharmony_ci	KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
149362306a36Sopenharmony_ci						KUNIT_ASSERTION,	       \
149462306a36Sopenharmony_ci						ptr,			       \
149562306a36Sopenharmony_ci						fmt,			       \
149662306a36Sopenharmony_ci						##__VA_ARGS__)
149762306a36Sopenharmony_ci
149862306a36Sopenharmony_ci/**
149962306a36Sopenharmony_ci * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
150062306a36Sopenharmony_ci * @name:  prefix for the test parameter generator function.
150162306a36Sopenharmony_ci * @array: array of test parameters.
150262306a36Sopenharmony_ci * @get_desc: function to convert param to description; NULL to use default
150362306a36Sopenharmony_ci *
150462306a36Sopenharmony_ci * Define function @name_gen_params which uses @array to generate parameters.
150562306a36Sopenharmony_ci */
150662306a36Sopenharmony_ci#define KUNIT_ARRAY_PARAM(name, array, get_desc)						\
150762306a36Sopenharmony_ci	static const void *name##_gen_params(const void *prev, char *desc)			\
150862306a36Sopenharmony_ci	{											\
150962306a36Sopenharmony_ci		typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array);	\
151062306a36Sopenharmony_ci		if (__next - (array) < ARRAY_SIZE((array))) {					\
151162306a36Sopenharmony_ci			void (*__get_desc)(typeof(__next), char *) = get_desc;			\
151262306a36Sopenharmony_ci			if (__get_desc)								\
151362306a36Sopenharmony_ci				__get_desc(__next, desc);					\
151462306a36Sopenharmony_ci			return __next;								\
151562306a36Sopenharmony_ci		}										\
151662306a36Sopenharmony_ci		return NULL;									\
151762306a36Sopenharmony_ci	}
151862306a36Sopenharmony_ci
151962306a36Sopenharmony_ci// TODO(dlatypov@google.com): consider eventually migrating users to explicitly
152062306a36Sopenharmony_ci// include resource.h themselves if they need it.
152162306a36Sopenharmony_ci#include <kunit/resource.h>
152262306a36Sopenharmony_ci
152362306a36Sopenharmony_ci#endif /* _KUNIT_TEST_H */
1524