xref: /linux-6.15/include/kunit/test.h (revision d1be0cf3)
1914cc63eSBrendan Higgins /* SPDX-License-Identifier: GPL-2.0 */
2914cc63eSBrendan Higgins /*
3914cc63eSBrendan Higgins  * Base unit test (KUnit) API.
4914cc63eSBrendan Higgins  *
5914cc63eSBrendan Higgins  * Copyright (C) 2019, Google LLC.
6914cc63eSBrendan Higgins  * Author: Brendan Higgins <[email protected]>
7914cc63eSBrendan Higgins  */
8914cc63eSBrendan Higgins 
9914cc63eSBrendan Higgins #ifndef _KUNIT_TEST_H
10914cc63eSBrendan Higgins #define _KUNIT_TEST_H
11914cc63eSBrendan Higgins 
1273cda7bbSBrendan Higgins #include <kunit/assert.h>
135f3e0620SBrendan Higgins #include <kunit/try-catch.h>
14ec54c289SAndy Shevchenko 
15b229baa3SAndy Shevchenko #include <linux/args.h>
164fdacef8SDaniel Latypov #include <linux/compiler.h>
17ec54c289SAndy Shevchenko #include <linux/container_of.h>
18ec54c289SAndy Shevchenko #include <linux/err.h>
19ec54c289SAndy Shevchenko #include <linux/init.h>
20908d0c17SDavid Gow #include <linux/jump_label.h>
21ec54c289SAndy Shevchenko #include <linux/kconfig.h>
22ec54c289SAndy Shevchenko #include <linux/kref.h>
23ec54c289SAndy Shevchenko #include <linux/list.h>
24c475c77dSAlan Maguire #include <linux/module.h>
250a756853SBrendan Higgins #include <linux/slab.h>
26ec54c289SAndy Shevchenko #include <linux/spinlock.h>
27ec54c289SAndy Shevchenko #include <linux/string.h>
28914cc63eSBrendan Higgins #include <linux/types.h>
29ec54c289SAndy Shevchenko 
30ec54c289SAndy Shevchenko #include <asm/rwonce.h>
31f2c6dbd2SDavid Gow #include <asm/sections.h>
32914cc63eSBrendan Higgins 
33908d0c17SDavid Gow /* Static key: true if any KUnit tests are currently running */
34908d0c17SDavid Gow DECLARE_STATIC_KEY_FALSE(kunit_running);
35908d0c17SDavid Gow 
36914cc63eSBrendan Higgins struct kunit;
3705e2006cSRichard Fitzgerald struct string_stream;
38e2219db2SAlan Maguire 
39fadb08e7SArpitha Raghunandan /* Maximum size of parameter description string. */
40fadb08e7SArpitha Raghunandan #define KUNIT_PARAM_DESC_SIZE 128
41fadb08e7SArpitha Raghunandan 
426d2426b2SDavid Gow /* Maximum size of a status comment. */
436d2426b2SDavid Gow #define KUNIT_STATUS_COMMENT_SIZE 256
446d2426b2SDavid Gow 
45c3bba690SAlan Maguire /*
46c3bba690SAlan Maguire  * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
47c3bba690SAlan Maguire  * sub-subtest.  See the "Subtests" section in
48c3bba690SAlan Maguire  * https://node-tap.org/tap-protocol/
49c3bba690SAlan Maguire  */
50b1eaa8b2SMichal Wajdeczko #define KUNIT_INDENT_LEN		4
51c3bba690SAlan Maguire #define KUNIT_SUBTEST_INDENT		"    "
52c3bba690SAlan Maguire #define KUNIT_SUBSUBTEST_INDENT		"        "
53c3bba690SAlan Maguire 
54914cc63eSBrendan Higgins /**
556d2426b2SDavid Gow  * enum kunit_status - Type of result for a test or test suite
566d2426b2SDavid Gow  * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
576d2426b2SDavid Gow  * @KUNIT_FAILURE: Denotes the test has failed.
586d2426b2SDavid Gow  * @KUNIT_SKIPPED: Denotes the test has been skipped.
596d2426b2SDavid Gow  */
606d2426b2SDavid Gow enum kunit_status {
616d2426b2SDavid Gow 	KUNIT_SUCCESS,
626d2426b2SDavid Gow 	KUNIT_FAILURE,
636d2426b2SDavid Gow 	KUNIT_SKIPPED,
646d2426b2SDavid Gow };
656d2426b2SDavid Gow 
6602c2d0c2SRae Moar /* Attribute struct/enum definitions */
6702c2d0c2SRae Moar 
6802c2d0c2SRae Moar /*
6902c2d0c2SRae Moar  * Speed Attribute is stored as an enum and separated into categories of
70*d1be0cf3SGeert Uytterhoeven  * speed: very_slow, slow, and normal. These speeds are relative to
7102c2d0c2SRae Moar  * other KUnit tests.
7202c2d0c2SRae Moar  *
7302c2d0c2SRae Moar  * Note: unset speed attribute acts as default of KUNIT_SPEED_NORMAL.
7402c2d0c2SRae Moar  */
7502c2d0c2SRae Moar enum kunit_speed {
7602c2d0c2SRae Moar 	KUNIT_SPEED_UNSET,
7702c2d0c2SRae Moar 	KUNIT_SPEED_VERY_SLOW,
7802c2d0c2SRae Moar 	KUNIT_SPEED_SLOW,
7902c2d0c2SRae Moar 	KUNIT_SPEED_NORMAL,
8002c2d0c2SRae Moar 	KUNIT_SPEED_MAX = KUNIT_SPEED_NORMAL,
8102c2d0c2SRae Moar };
8202c2d0c2SRae Moar 
8339e92cb1SRae Moar /* Holds attributes for each test case and suite */
8402c2d0c2SRae Moar struct kunit_attributes {
8502c2d0c2SRae Moar 	enum kunit_speed speed;
8602c2d0c2SRae Moar };
8739e92cb1SRae Moar 
886d2426b2SDavid Gow /**
89914cc63eSBrendan Higgins  * struct kunit_case - represents an individual test case.
90914cc63eSBrendan Higgins  *
91914cc63eSBrendan Higgins  * @run_case: the function representing the actual test case.
92914cc63eSBrendan Higgins  * @name:     the name of the test case.
93fadb08e7SArpitha Raghunandan  * @generate_params: the generator function for parameterized tests.
9439e92cb1SRae Moar  * @attr:     the attributes associated with the test
95914cc63eSBrendan Higgins  *
96914cc63eSBrendan Higgins  * A test case is a function with the signature,
97e4aea8f8SBrendan Higgins  * ``void (*)(struct kunit *)``
98e4aea8f8SBrendan Higgins  * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
99e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
100914cc63eSBrendan Higgins  * with a &struct kunit_suite and will be run after the suite's init
101914cc63eSBrendan Higgins  * function and followed by the suite's exit function.
102914cc63eSBrendan Higgins  *
103914cc63eSBrendan Higgins  * A test case should be static and should only be created with the
104914cc63eSBrendan Higgins  * KUNIT_CASE() macro; additionally, every array of test cases should be
105914cc63eSBrendan Higgins  * terminated with an empty test case.
106914cc63eSBrendan Higgins  *
107914cc63eSBrendan Higgins  * Example:
108914cc63eSBrendan Higgins  *
109914cc63eSBrendan Higgins  * .. code-block:: c
110914cc63eSBrendan Higgins  *
111914cc63eSBrendan Higgins  *	void add_test_basic(struct kunit *test)
112914cc63eSBrendan Higgins  *	{
113914cc63eSBrendan Higgins  *		KUNIT_EXPECT_EQ(test, 1, add(1, 0));
114914cc63eSBrendan Higgins  *		KUNIT_EXPECT_EQ(test, 2, add(1, 1));
115914cc63eSBrendan Higgins  *		KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
116914cc63eSBrendan Higgins  *		KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
117914cc63eSBrendan Higgins  *		KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
118914cc63eSBrendan Higgins  *	}
119914cc63eSBrendan Higgins  *
120914cc63eSBrendan Higgins  *	static struct kunit_case example_test_cases[] = {
121914cc63eSBrendan Higgins  *		KUNIT_CASE(add_test_basic),
122914cc63eSBrendan Higgins  *		{}
123914cc63eSBrendan Higgins  *	};
124914cc63eSBrendan Higgins  *
125914cc63eSBrendan Higgins  */
126914cc63eSBrendan Higgins struct kunit_case {
127914cc63eSBrendan Higgins 	void (*run_case)(struct kunit *test);
128914cc63eSBrendan Higgins 	const char *name;
129fadb08e7SArpitha Raghunandan 	const void* (*generate_params)(const void *prev, char *desc);
13039e92cb1SRae Moar 	struct kunit_attributes attr;
131914cc63eSBrendan Higgins 
132914cc63eSBrendan Higgins 	/* private: internal use only. */
1336d2426b2SDavid Gow 	enum kunit_status status;
134a00a7270SRae Moar 	char *module_name;
13505e2006cSRichard Fitzgerald 	struct string_stream *log;
136914cc63eSBrendan Higgins };
137914cc63eSBrendan Higgins 
kunit_status_to_ok_not_ok(enum kunit_status status)1386d2426b2SDavid Gow static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
139e2219db2SAlan Maguire {
1406d2426b2SDavid Gow 	switch (status) {
1416d2426b2SDavid Gow 	case KUNIT_SKIPPED:
1426d2426b2SDavid Gow 	case KUNIT_SUCCESS:
1436d2426b2SDavid Gow 		return "ok";
1446d2426b2SDavid Gow 	case KUNIT_FAILURE:
1456d2426b2SDavid Gow 		return "not ok";
1466d2426b2SDavid Gow 	}
1476d2426b2SDavid Gow 	return "invalid";
148e2219db2SAlan Maguire }
149e2219db2SAlan Maguire 
150914cc63eSBrendan Higgins /**
151914cc63eSBrendan Higgins  * KUNIT_CASE - A helper for creating a &struct kunit_case
152914cc63eSBrendan Higgins  *
153914cc63eSBrendan Higgins  * @test_name: a reference to a test case function.
154914cc63eSBrendan Higgins  *
155914cc63eSBrendan Higgins  * Takes a symbol for a function representing a test case and creates a
156914cc63eSBrendan Higgins  * &struct kunit_case object from it. See the documentation for
157914cc63eSBrendan Higgins  * &struct kunit_case for an example on how to use it.
158914cc63eSBrendan Higgins  */
159a00a7270SRae Moar #define KUNIT_CASE(test_name)			\
160a00a7270SRae Moar 		{ .run_case = test_name, .name = #test_name,	\
161a00a7270SRae Moar 		  .module_name = KBUILD_MODNAME}
162914cc63eSBrendan Higgins 
163914cc63eSBrendan Higgins /**
16439e92cb1SRae Moar  * KUNIT_CASE_ATTR - A helper for creating a &struct kunit_case
16539e92cb1SRae Moar  * with attributes
16639e92cb1SRae Moar  *
16739e92cb1SRae Moar  * @test_name: a reference to a test case function.
16839e92cb1SRae Moar  * @attributes: a reference to a struct kunit_attributes object containing
16939e92cb1SRae Moar  * test attributes
17039e92cb1SRae Moar  */
17139e92cb1SRae Moar #define KUNIT_CASE_ATTR(test_name, attributes)			\
17239e92cb1SRae Moar 		{ .run_case = test_name, .name = #test_name,	\
173a00a7270SRae Moar 		  .attr = attributes, .module_name = KBUILD_MODNAME}
17439e92cb1SRae Moar 
17539e92cb1SRae Moar /**
17602c2d0c2SRae Moar  * KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case
17702c2d0c2SRae Moar  * with the slow attribute
17802c2d0c2SRae Moar  *
17902c2d0c2SRae Moar  * @test_name: a reference to a test case function.
18002c2d0c2SRae Moar  */
18102c2d0c2SRae Moar 
18202c2d0c2SRae Moar #define KUNIT_CASE_SLOW(test_name)			\
18302c2d0c2SRae Moar 		{ .run_case = test_name, .name = #test_name,	\
184a00a7270SRae Moar 		  .attr.speed = KUNIT_SPEED_SLOW, .module_name = KBUILD_MODNAME}
18502c2d0c2SRae Moar 
18602c2d0c2SRae Moar /**
187fadb08e7SArpitha Raghunandan  * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
188fadb08e7SArpitha Raghunandan  *
189fadb08e7SArpitha Raghunandan  * @test_name: a reference to a test case function.
190fadb08e7SArpitha Raghunandan  * @gen_params: a reference to a parameter generator function.
191fadb08e7SArpitha Raghunandan  *
192fadb08e7SArpitha Raghunandan  * The generator function::
193fadb08e7SArpitha Raghunandan  *
194fadb08e7SArpitha Raghunandan  *	const void* gen_params(const void *prev, char *desc)
195fadb08e7SArpitha Raghunandan  *
196fadb08e7SArpitha Raghunandan  * is used to lazily generate a series of arbitrarily typed values that fit into
197fadb08e7SArpitha Raghunandan  * a void*. The argument @prev is the previously returned value, which should be
198fadb08e7SArpitha Raghunandan  * used to derive the next value; @prev is set to NULL on the initial generator
199fadb08e7SArpitha Raghunandan  * call. When no more values are available, the generator must return NULL.
200fadb08e7SArpitha Raghunandan  * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
201fadb08e7SArpitha Raghunandan  * describing the parameter.
202fadb08e7SArpitha Raghunandan  */
203fadb08e7SArpitha Raghunandan #define KUNIT_CASE_PARAM(test_name, gen_params)			\
204fadb08e7SArpitha Raghunandan 		{ .run_case = test_name, .name = #test_name,	\
205a00a7270SRae Moar 		  .generate_params = gen_params, .module_name = KBUILD_MODNAME}
206fadb08e7SArpitha Raghunandan 
207fadb08e7SArpitha Raghunandan /**
20839e92cb1SRae Moar  * KUNIT_CASE_PARAM_ATTR - A helper for creating a parameterized &struct
20939e92cb1SRae Moar  * kunit_case with attributes
21039e92cb1SRae Moar  *
21139e92cb1SRae Moar  * @test_name: a reference to a test case function.
21239e92cb1SRae Moar  * @gen_params: a reference to a parameter generator function.
21339e92cb1SRae Moar  * @attributes: a reference to a struct kunit_attributes object containing
21439e92cb1SRae Moar  * test attributes
21539e92cb1SRae Moar  */
21639e92cb1SRae Moar #define KUNIT_CASE_PARAM_ATTR(test_name, gen_params, attributes)	\
21739e92cb1SRae Moar 		{ .run_case = test_name, .name = #test_name,	\
21839e92cb1SRae Moar 		  .generate_params = gen_params,				\
219a00a7270SRae Moar 		  .attr = attributes, .module_name = KBUILD_MODNAME}
22039e92cb1SRae Moar 
22139e92cb1SRae Moar /**
222914cc63eSBrendan Higgins  * struct kunit_suite - describes a related collection of &struct kunit_case
223914cc63eSBrendan Higgins  *
224914cc63eSBrendan Higgins  * @name:	the name of the test. Purely informational.
2251cdba21dSDaniel Latypov  * @suite_init:	called once per test suite before the test cases.
2261cdba21dSDaniel Latypov  * @suite_exit:	called once per test suite after all test cases.
227914cc63eSBrendan Higgins  * @init:	called before every test case.
228914cc63eSBrendan Higgins  * @exit:	called after every test case.
229914cc63eSBrendan Higgins  * @test_cases:	a null terminated array of test cases.
23039e92cb1SRae Moar  * @attr:	the attributes associated with the test suite
231914cc63eSBrendan Higgins  *
232914cc63eSBrendan Higgins  * A kunit_suite is a collection of related &struct kunit_case s, such that
233914cc63eSBrendan Higgins  * @init is called before every test case and @exit is called after every
234914cc63eSBrendan Higgins  * test case, similar to the notion of a *test fixture* or a *test class*
235914cc63eSBrendan Higgins  * in other unit testing frameworks like JUnit or Googletest.
236914cc63eSBrendan Higgins  *
237cdc87bdaSDavid Gow  * Note that @exit and @suite_exit will run even if @init or @suite_init
238cdc87bdaSDavid Gow  * fail: make sure they can handle any inconsistent state which may result.
239cdc87bdaSDavid Gow  *
240914cc63eSBrendan Higgins  * Every &struct kunit_case must be associated with a kunit_suite for KUnit
241914cc63eSBrendan Higgins  * to run it.
242914cc63eSBrendan Higgins  */
243914cc63eSBrendan Higgins struct kunit_suite {
244914cc63eSBrendan Higgins 	const char name[256];
2451cdba21dSDaniel Latypov 	int (*suite_init)(struct kunit_suite *suite);
2461cdba21dSDaniel Latypov 	void (*suite_exit)(struct kunit_suite *suite);
247914cc63eSBrendan Higgins 	int (*init)(struct kunit *test);
248914cc63eSBrendan Higgins 	void (*exit)(struct kunit *test);
249914cc63eSBrendan Higgins 	struct kunit_case *test_cases;
25039e92cb1SRae Moar 	struct kunit_attributes attr;
251e2219db2SAlan Maguire 
252c4714b00SLothar Rubusch 	/* private: internal use only */
2536d2426b2SDavid Gow 	char status_comment[KUNIT_STATUS_COMMENT_SIZE];
254e2219db2SAlan Maguire 	struct dentry *debugfs;
25505e2006cSRichard Fitzgerald 	struct string_stream *log;
2561cdba21dSDaniel Latypov 	int suite_init_err;
2576c4ea2f4SRae Moar 	bool is_init;
258914cc63eSBrendan Higgins };
259914cc63eSBrendan Higgins 
260c95e7c05SJanusz Krzysztofik /* Stores an array of suites, end points one past the end */
261c95e7c05SJanusz Krzysztofik struct kunit_suite_set {
262c95e7c05SJanusz Krzysztofik 	struct kunit_suite * const *start;
263c95e7c05SJanusz Krzysztofik 	struct kunit_suite * const *end;
264c95e7c05SJanusz Krzysztofik };
265c95e7c05SJanusz Krzysztofik 
266914cc63eSBrendan Higgins /**
267914cc63eSBrendan Higgins  * struct kunit - represents a running instance of a test.
268914cc63eSBrendan Higgins  *
269914cc63eSBrendan Higgins  * @priv: for user to store arbitrary data. Commonly used to pass data
270914cc63eSBrendan Higgins  *	  created in the init function (see &struct kunit_suite).
271914cc63eSBrendan Higgins  *
272914cc63eSBrendan Higgins  * Used to store information about the current context under which the test
273914cc63eSBrendan Higgins  * is running. Most of this data is private and should only be accessed
274914cc63eSBrendan Higgins  * indirectly via public functions; the one exception is @priv which can be
275914cc63eSBrendan Higgins  * used by the test writer to store arbitrary data.
276914cc63eSBrendan Higgins  */
277914cc63eSBrendan Higgins struct kunit {
278914cc63eSBrendan Higgins 	void *priv;
279914cc63eSBrendan Higgins 
280914cc63eSBrendan Higgins 	/* private: internal use only. */
281914cc63eSBrendan Higgins 	const char *name; /* Read only after initialization! */
28205e2006cSRichard Fitzgerald 	struct string_stream *log; /* Points at case log after initialization */
2835f3e0620SBrendan Higgins 	struct kunit_try_catch try_catch;
284fadb08e7SArpitha Raghunandan 	/* param_value is the current parameter value for a test case. */
285fadb08e7SArpitha Raghunandan 	const void *param_value;
286fadb08e7SArpitha Raghunandan 	/* param_index stores the index of the parameter in parameterized tests. */
287fadb08e7SArpitha Raghunandan 	int param_index;
288914cc63eSBrendan Higgins 	/*
289914cc63eSBrendan Higgins 	 * success starts as true, and may only be set to false during a
290914cc63eSBrendan Higgins 	 * test case; thus, it is safe to update this across multiple
291914cc63eSBrendan Higgins 	 * threads using WRITE_ONCE; however, as a consequence, it may only
292914cc63eSBrendan Higgins 	 * be read after the test case finishes once all threads associated
293914cc63eSBrendan Higgins 	 * with the test case have terminated.
294914cc63eSBrendan Higgins 	 */
2950a756853SBrendan Higgins 	spinlock_t lock; /* Guards all mutable test state. */
2966d2426b2SDavid Gow 	enum kunit_status status; /* Read only after test_case finishes! */
2970a756853SBrendan Higgins 	/*
2980a756853SBrendan Higgins 	 * Because resources is a list that may be updated multiple times (with
2990a756853SBrendan Higgins 	 * new resources) from any thread associated with a test case, we must
3000a756853SBrendan Higgins 	 * protect it with some type of lock.
3010a756853SBrendan Higgins 	 */
3020a756853SBrendan Higgins 	struct list_head resources; /* Protected by lock. */
3036d2426b2SDavid Gow 
3046d2426b2SDavid Gow 	char status_comment[KUNIT_STATUS_COMMENT_SIZE];
3058bd5d74bSMickaël Salaün 	/* Saves the last seen test. Useful to help with faults. */
3068bd5d74bSMickaël Salaün 	struct kunit_loc last_seen;
307914cc63eSBrendan Higgins };
308914cc63eSBrendan Higgins 
kunit_set_failure(struct kunit * test)30983c4e7a0SPatricia Alfonso static inline void kunit_set_failure(struct kunit *test)
31083c4e7a0SPatricia Alfonso {
3116d2426b2SDavid Gow 	WRITE_ONCE(test->status, KUNIT_FAILURE);
31283c4e7a0SPatricia Alfonso }
31383c4e7a0SPatricia Alfonso 
314d20a6ba5SJoe Fradley bool kunit_enabled(void);
31531691914SStanislav Kinsburskii bool kunit_autorun(void);
31618258c60SJanusz Krzysztofik const char *kunit_action(void);
317b67abaadSJanusz Krzysztofik const char *kunit_filter_glob(void);
318b67abaadSJanusz Krzysztofik char *kunit_filter(void);
319b67abaadSJanusz Krzysztofik char *kunit_filter_action(void);
320d20a6ba5SJoe Fradley 
32105e2006cSRichard Fitzgerald void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log);
322914cc63eSBrendan Higgins 
323914cc63eSBrendan Higgins int kunit_run_tests(struct kunit_suite *suite);
324914cc63eSBrendan Higgins 
325e2219db2SAlan Maguire size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
326e2219db2SAlan Maguire 
327e2219db2SAlan Maguire unsigned int kunit_test_case_num(struct kunit_suite *suite,
328e2219db2SAlan Maguire 				 struct kunit_case *test_case);
329e2219db2SAlan Maguire 
330b67abaadSJanusz Krzysztofik struct kunit_suite_set
331b67abaadSJanusz Krzysztofik kunit_filter_suites(const struct kunit_suite_set *suite_set,
332b67abaadSJanusz Krzysztofik 		    const char *filter_glob,
333b67abaadSJanusz Krzysztofik 		    char *filters,
334b67abaadSJanusz Krzysztofik 		    char *filter_action,
335b67abaadSJanusz Krzysztofik 		    int *err);
336b67abaadSJanusz Krzysztofik void kunit_free_suite_set(struct kunit_suite_set suite_set);
337b67abaadSJanusz Krzysztofik 
33831691914SStanislav Kinsburskii int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites,
33931691914SStanislav Kinsburskii 			     bool run_tests);
340e2219db2SAlan Maguire 
341e5857d39SDaniel Latypov void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
342e2219db2SAlan Maguire 
343c95e7c05SJanusz Krzysztofik void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin);
34418258c60SJanusz Krzysztofik void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr);
345c95e7c05SJanusz Krzysztofik 
346d81f0d7bSRae Moar struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set,
347d81f0d7bSRae Moar 		struct kunit_suite_set suite_set);
348d81f0d7bSRae Moar 
3498c0d8849SBrendan Higgins #if IS_BUILTIN(CONFIG_KUNIT)
3508c0d8849SBrendan Higgins int kunit_run_all_tests(void);
3518c0d8849SBrendan Higgins #else
kunit_run_all_tests(void)3528c0d8849SBrendan Higgins static inline int kunit_run_all_tests(void)
3538c0d8849SBrendan Higgins {
3548c0d8849SBrendan Higgins 	return 0;
3558c0d8849SBrendan Higgins }
3568c0d8849SBrendan Higgins #endif /* IS_BUILTIN(CONFIG_KUNIT) */
3578c0d8849SBrendan Higgins 
358e5857d39SDaniel Latypov #define __kunit_test_suites(unique_array, ...)				       \
359e5857d39SDaniel Latypov 	static struct kunit_suite *unique_array[]			       \
360e5857d39SDaniel Latypov 	__aligned(sizeof(struct kunit_suite *))				       \
361e5857d39SDaniel Latypov 	__used __section(".kunit_test_suites") = { __VA_ARGS__ }
362aac35468SAlan Maguire 
363aac35468SAlan Maguire /**
364aac35468SAlan Maguire  * kunit_test_suites() - used to register one or more &struct kunit_suite
365aac35468SAlan Maguire  *			 with KUnit.
366aac35468SAlan Maguire  *
3677f32b10cSMauro Carvalho Chehab  * @__suites: a statically allocated list of &struct kunit_suite.
368aac35468SAlan Maguire  *
3693d6e4462SJeremy Kerr  * Registers @suites with the test framework.
3703d6e4462SJeremy Kerr  * This is done by placing the array of struct kunit_suite * in the
3713d6e4462SJeremy Kerr  * .kunit_test_suites ELF section.
372aac35468SAlan Maguire  *
3733d6e4462SJeremy Kerr  * When builtin, KUnit tests are all run via the executor at boot, and when
3743d6e4462SJeremy Kerr  * built as a module, they run on module load.
375aac35468SAlan Maguire  *
376aac35468SAlan Maguire  */
3777f32b10cSMauro Carvalho Chehab #define kunit_test_suites(__suites...)						\
378aac35468SAlan Maguire 	__kunit_test_suites(__UNIQUE_ID(array),				\
3797f32b10cSMauro Carvalho Chehab 			    ##__suites)
380c475c77dSAlan Maguire 
381c475c77dSAlan Maguire #define kunit_test_suite(suite)	kunit_test_suites(&suite)
382914cc63eSBrendan Higgins 
383d81f0d7bSRae Moar #define __kunit_init_test_suites(unique_array, ...)			       \
384d81f0d7bSRae Moar 	static struct kunit_suite *unique_array[]			       \
385d81f0d7bSRae Moar 	__aligned(sizeof(struct kunit_suite *))				       \
386d81f0d7bSRae Moar 	__used __section(".kunit_init_test_suites") = { __VA_ARGS__ }
387d81f0d7bSRae Moar 
3889bf2eed9SBrendan Higgins /**
3899bf2eed9SBrendan Higgins  * kunit_test_init_section_suites() - used to register one or more &struct
3909bf2eed9SBrendan Higgins  *				      kunit_suite containing init functions or
3919bf2eed9SBrendan Higgins  *				      init data.
3929bf2eed9SBrendan Higgins  *
3939bf2eed9SBrendan Higgins  * @__suites: a statically allocated list of &struct kunit_suite.
3949bf2eed9SBrendan Higgins  *
395d81f0d7bSRae Moar  * This functions similar to kunit_test_suites() except that it compiles the
396d81f0d7bSRae Moar  * list of suites during init phase.
3979bf2eed9SBrendan Higgins  *
398d81f0d7bSRae Moar  * This macro also suffixes the array and suite declarations it makes with
399d81f0d7bSRae Moar  * _probe; so that modpost suppresses warnings about referencing init data
400d81f0d7bSRae Moar  * for symbols named in this manner.
4019bf2eed9SBrendan Higgins  *
402d81f0d7bSRae Moar  * Note: these init tests are not able to be run after boot so there is no
403d81f0d7bSRae Moar  * "run" debugfs file generated for these tests.
404d81f0d7bSRae Moar  *
405d81f0d7bSRae Moar  * Also, do not mark the suite or test case structs with __initdata because
406d81f0d7bSRae Moar  * they will be used after the init phase with debugfs.
4079bf2eed9SBrendan Higgins  */
4089bf2eed9SBrendan Higgins #define kunit_test_init_section_suites(__suites...)			\
409d81f0d7bSRae Moar 	__kunit_init_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
4109bf2eed9SBrendan Higgins 			    ##__suites)
4119bf2eed9SBrendan Higgins 
4129bf2eed9SBrendan Higgins #define kunit_test_init_section_suite(suite)	\
4139bf2eed9SBrendan Higgins 	kunit_test_init_section_suites(&suite)
4149bf2eed9SBrendan Higgins 
415e2219db2SAlan Maguire #define kunit_suite_for_each_test_case(suite, test_case)		\
416e2219db2SAlan Maguire 	for (test_case = suite->test_cases; test_case->run_case; test_case++)
417e2219db2SAlan Maguire 
4186d2426b2SDavid Gow enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
419e2219db2SAlan Maguire 
420d4cdd146SAlan Maguire /**
4217122debbSDaniel Latypov  * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
4227122debbSDaniel Latypov  * @test: The test context object.
4237122debbSDaniel Latypov  * @n: number of elements.
4247122debbSDaniel Latypov  * @size: The size in bytes of the desired memory.
4257122debbSDaniel Latypov  * @gfp: flags passed to underlying kmalloc().
4267122debbSDaniel Latypov  *
4277122debbSDaniel Latypov  * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
42857e3cdedSDavid Gow  * and is automatically cleaned up after the test case concludes. See kunit_add_action()
42957e3cdedSDavid Gow  * for more information.
43057e3cdedSDavid Gow  *
43157e3cdedSDavid Gow  * Note that some internal context data is also allocated with GFP_KERNEL,
43257e3cdedSDavid Gow  * regardless of the gfp passed in.
4337122debbSDaniel Latypov  */
434361b57dfSDaniel Latypov void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
4357122debbSDaniel Latypov 
4367122debbSDaniel Latypov /**
4370a756853SBrendan Higgins  * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
4380a756853SBrendan Higgins  * @test: The test context object.
4390a756853SBrendan Higgins  * @size: The size in bytes of the desired memory.
4400a756853SBrendan Higgins  * @gfp: flags passed to underlying kmalloc().
4410a756853SBrendan Higgins  *
4427122debbSDaniel Latypov  * See kmalloc() and kunit_kmalloc_array() for more information.
44357e3cdedSDavid Gow  *
44457e3cdedSDavid Gow  * Note that some internal context data is also allocated with GFP_KERNEL,
44557e3cdedSDavid Gow  * regardless of the gfp passed in.
4460a756853SBrendan Higgins  */
kunit_kmalloc(struct kunit * test,size_t size,gfp_t gfp)4477122debbSDaniel Latypov static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
4487122debbSDaniel Latypov {
4497122debbSDaniel Latypov 	return kunit_kmalloc_array(test, 1, size, gfp);
4507122debbSDaniel Latypov }
4510a756853SBrendan Higgins 
4520a756853SBrendan Higgins /**
4530a756853SBrendan Higgins  * kunit_kfree() - Like kfree except for allocations managed by KUnit.
4540a756853SBrendan Higgins  * @test: The test case to which the resource belongs.
4550a756853SBrendan Higgins  * @ptr: The memory allocation to free.
4560a756853SBrendan Higgins  */
4570a756853SBrendan Higgins void kunit_kfree(struct kunit *test, const void *ptr);
4580a756853SBrendan Higgins 
4590a756853SBrendan Higgins /**
4600a756853SBrendan Higgins  * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
4610a756853SBrendan Higgins  * @test: The test context object.
4620a756853SBrendan Higgins  * @size: The size in bytes of the desired memory.
4630a756853SBrendan Higgins  * @gfp: flags passed to underlying kmalloc().
4640a756853SBrendan Higgins  *
4657122debbSDaniel Latypov  * See kzalloc() and kunit_kmalloc_array() for more information.
4660a756853SBrendan Higgins  */
kunit_kzalloc(struct kunit * test,size_t size,gfp_t gfp)4670a756853SBrendan Higgins static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
4680a756853SBrendan Higgins {
4690a756853SBrendan Higgins 	return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
4700a756853SBrendan Higgins }
4710a756853SBrendan Higgins 
4727122debbSDaniel Latypov /**
4737122debbSDaniel Latypov  * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
4747122debbSDaniel Latypov  * @test: The test context object.
4757122debbSDaniel Latypov  * @n: number of elements.
4767122debbSDaniel Latypov  * @size: The size in bytes of the desired memory.
4777122debbSDaniel Latypov  * @gfp: flags passed to underlying kmalloc().
4787122debbSDaniel Latypov  *
4797122debbSDaniel Latypov  * See kcalloc() and kunit_kmalloc_array() for more information.
4807122debbSDaniel Latypov  */
kunit_kcalloc(struct kunit * test,size_t n,size_t size,gfp_t gfp)481361b57dfSDaniel Latypov static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
4827122debbSDaniel Latypov {
483361b57dfSDaniel Latypov 	return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
4847122debbSDaniel Latypov }
4857122debbSDaniel Latypov 
486f2c6dbd2SDavid Gow 
487f2c6dbd2SDavid Gow /**
488f2c6dbd2SDavid Gow  * kunit_kfree_const() - conditionally free test managed memory
48912cb32a5SDavid Gow  * @test: The test context object.
490f2c6dbd2SDavid Gow  * @x: pointer to the memory
491f2c6dbd2SDavid Gow  *
492f2c6dbd2SDavid Gow  * Calls kunit_kfree() only if @x is not in .rodata section.
493f2c6dbd2SDavid Gow  * See kunit_kstrdup_const() for more information.
494f2c6dbd2SDavid Gow  */
495f2c6dbd2SDavid Gow void kunit_kfree_const(struct kunit *test, const void *x);
496f2c6dbd2SDavid Gow 
497f2c6dbd2SDavid Gow /**
498f2c6dbd2SDavid Gow  * kunit_kstrdup() - Duplicates a string into a test managed allocation.
499f2c6dbd2SDavid Gow  *
500f2c6dbd2SDavid Gow  * @test: The test context object.
501f2c6dbd2SDavid Gow  * @str: The NULL-terminated string to duplicate.
502f2c6dbd2SDavid Gow  * @gfp: flags passed to underlying kmalloc().
503f2c6dbd2SDavid Gow  *
504f2c6dbd2SDavid Gow  * See kstrdup() and kunit_kmalloc_array() for more information.
505f2c6dbd2SDavid Gow  */
kunit_kstrdup(struct kunit * test,const char * str,gfp_t gfp)506f2c6dbd2SDavid Gow static inline char *kunit_kstrdup(struct kunit *test, const char *str, gfp_t gfp)
507f2c6dbd2SDavid Gow {
508f2c6dbd2SDavid Gow 	size_t len;
509f2c6dbd2SDavid Gow 	char *buf;
510f2c6dbd2SDavid Gow 
511f2c6dbd2SDavid Gow 	if (!str)
512f2c6dbd2SDavid Gow 		return NULL;
513f2c6dbd2SDavid Gow 
514f2c6dbd2SDavid Gow 	len = strlen(str) + 1;
515f2c6dbd2SDavid Gow 	buf = kunit_kmalloc(test, len, gfp);
516f2c6dbd2SDavid Gow 	if (buf)
517f2c6dbd2SDavid Gow 		memcpy(buf, str, len);
518f2c6dbd2SDavid Gow 	return buf;
519f2c6dbd2SDavid Gow }
520f2c6dbd2SDavid Gow 
521f2c6dbd2SDavid Gow /**
522f2c6dbd2SDavid Gow  * kunit_kstrdup_const() - Conditionally duplicates a string into a test managed allocation.
523f2c6dbd2SDavid Gow  *
524f2c6dbd2SDavid Gow  * @test: The test context object.
525f2c6dbd2SDavid Gow  * @str: The NULL-terminated string to duplicate.
526f2c6dbd2SDavid Gow  * @gfp: flags passed to underlying kmalloc().
527f2c6dbd2SDavid Gow  *
528f2c6dbd2SDavid Gow  * Calls kunit_kstrdup() only if @str is not in the rodata section. Must be freed with
529f2c6dbd2SDavid Gow  * kunit_kfree_const() -- not kunit_kfree().
530f2c6dbd2SDavid Gow  * See kstrdup_const() and kunit_kmalloc_array() for more information.
531f2c6dbd2SDavid Gow  */
532f2c6dbd2SDavid Gow const char *kunit_kstrdup_const(struct kunit *test, const char *str, gfp_t gfp);
533f2c6dbd2SDavid Gow 
53451104c19SKees Cook /**
53551104c19SKees Cook  * kunit_vm_mmap() - Allocate KUnit-tracked vm_mmap() area
53651104c19SKees Cook  * @test: The test context object.
53751104c19SKees Cook  * @file: struct file pointer to map from, if any
53851104c19SKees Cook  * @addr: desired address, if any
53951104c19SKees Cook  * @len: how many bytes to allocate
54051104c19SKees Cook  * @prot: mmap PROT_* bits
54151104c19SKees Cook  * @flag: mmap flags
54251104c19SKees Cook  * @offset: offset into @file to start mapping from.
54351104c19SKees Cook  *
54451104c19SKees Cook  * See vm_mmap() for more information.
54551104c19SKees Cook  */
54651104c19SKees Cook unsigned long kunit_vm_mmap(struct kunit *test, struct file *file,
54751104c19SKees Cook 			    unsigned long addr, unsigned long len,
54851104c19SKees Cook 			    unsigned long prot, unsigned long flag,
54951104c19SKees Cook 			    unsigned long offset);
55051104c19SKees Cook 
5510a756853SBrendan Higgins void kunit_cleanup(struct kunit *test);
5520a756853SBrendan Higgins 
55305e2006cSRichard Fitzgerald void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...);
554e2219db2SAlan Maguire 
5556d2426b2SDavid Gow /**
5560619a486SKevin Brodsky  * kunit_mark_skipped() - Marks @test as skipped
5576d2426b2SDavid Gow  *
5580619a486SKevin Brodsky  * @test: The test context object.
5596d2426b2SDavid Gow  * @fmt:  A printk() style format string.
5606d2426b2SDavid Gow  *
5616d2426b2SDavid Gow  * Marks the test as skipped. @fmt is given output as the test status
5626d2426b2SDavid Gow  * comment, typically the reason the test was skipped.
5636d2426b2SDavid Gow  *
5646d2426b2SDavid Gow  * Test execution continues after kunit_mark_skipped() is called.
5656d2426b2SDavid Gow  */
5660619a486SKevin Brodsky #define kunit_mark_skipped(test, fmt, ...)				\
5676d2426b2SDavid Gow 	do {								\
5680619a486SKevin Brodsky 		WRITE_ONCE((test)->status, KUNIT_SKIPPED);		\
5690619a486SKevin Brodsky 		scnprintf((test)->status_comment,			\
5706d2426b2SDavid Gow 			  KUNIT_STATUS_COMMENT_SIZE,			\
5716d2426b2SDavid Gow 			  fmt, ##__VA_ARGS__);				\
5726d2426b2SDavid Gow 	} while (0)
5736d2426b2SDavid Gow 
5746d2426b2SDavid Gow /**
5750619a486SKevin Brodsky  * kunit_skip() - Marks @test as skipped
5766d2426b2SDavid Gow  *
5770619a486SKevin Brodsky  * @test: The test context object.
5786d2426b2SDavid Gow  * @fmt:  A printk() style format string.
5796d2426b2SDavid Gow  *
5806d2426b2SDavid Gow  * Skips the test. @fmt is given output as the test status
5816d2426b2SDavid Gow  * comment, typically the reason the test was skipped.
5826d2426b2SDavid Gow  *
5836d2426b2SDavid Gow  * Test execution is halted after kunit_skip() is called.
5846d2426b2SDavid Gow  */
5850619a486SKevin Brodsky #define kunit_skip(test, fmt, ...)					\
5866d2426b2SDavid Gow 	do {								\
5870619a486SKevin Brodsky 		kunit_mark_skipped((test), fmt, ##__VA_ARGS__);		\
5880619a486SKevin Brodsky 		kunit_try_catch_throw(&((test)->try_catch));		\
5896d2426b2SDavid Gow 	} while (0)
590e2219db2SAlan Maguire 
591e2219db2SAlan Maguire /*
592e2219db2SAlan Maguire  * printk and log to per-test or per-suite log buffer.  Logging only done
593e2219db2SAlan Maguire  * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
594e2219db2SAlan Maguire  */
595e2219db2SAlan Maguire #define kunit_log(lvl, test_or_suite, fmt, ...)				\
596e2219db2SAlan Maguire 	do {								\
597e2219db2SAlan Maguire 		printk(lvl fmt, ##__VA_ARGS__);				\
5982c6a96daSRae Moar 		kunit_log_append((test_or_suite)->log,	fmt,		\
599e2219db2SAlan Maguire 				 ##__VA_ARGS__);			\
600e2219db2SAlan Maguire 	} while (0)
601e2219db2SAlan Maguire 
602741a98d0SBrendan Higgins #define kunit_printk(lvl, test, fmt, ...)				\
603c3bba690SAlan Maguire 	kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt,		\
604c3bba690SAlan Maguire 		  (test)->name,	##__VA_ARGS__)
605914cc63eSBrendan Higgins 
606914cc63eSBrendan Higgins /**
607914cc63eSBrendan Higgins  * kunit_info() - Prints an INFO level message associated with @test.
608914cc63eSBrendan Higgins  *
609914cc63eSBrendan Higgins  * @test: The test context object.
610914cc63eSBrendan Higgins  * @fmt:  A printk() style format string.
611914cc63eSBrendan Higgins  *
612914cc63eSBrendan Higgins  * Prints an info level message associated with the test suite being run.
613914cc63eSBrendan Higgins  * Takes a variable number of format parameters just like printk().
614914cc63eSBrendan Higgins  */
615914cc63eSBrendan Higgins #define kunit_info(test, fmt, ...) \
616914cc63eSBrendan Higgins 	kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
617914cc63eSBrendan Higgins 
618914cc63eSBrendan Higgins /**
619914cc63eSBrendan Higgins  * kunit_warn() - Prints a WARN level message associated with @test.
620914cc63eSBrendan Higgins  *
621914cc63eSBrendan Higgins  * @test: The test context object.
622914cc63eSBrendan Higgins  * @fmt:  A printk() style format string.
623914cc63eSBrendan Higgins  *
624914cc63eSBrendan Higgins  * Prints a warning level message.
625914cc63eSBrendan Higgins  */
626914cc63eSBrendan Higgins #define kunit_warn(test, fmt, ...) \
627914cc63eSBrendan Higgins 	kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
628914cc63eSBrendan Higgins 
629914cc63eSBrendan Higgins /**
630914cc63eSBrendan Higgins  * kunit_err() - Prints an ERROR level message associated with @test.
631914cc63eSBrendan Higgins  *
632914cc63eSBrendan Higgins  * @test: The test context object.
633914cc63eSBrendan Higgins  * @fmt:  A printk() style format string.
634914cc63eSBrendan Higgins  *
635914cc63eSBrendan Higgins  * Prints an error level message.
636914cc63eSBrendan Higgins  */
637914cc63eSBrendan Higgins #define kunit_err(test, fmt, ...) \
638914cc63eSBrendan Higgins 	kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
639914cc63eSBrendan Higgins 
6408bd5d74bSMickaël Salaün /*
6418bd5d74bSMickaël Salaün  * Must be called at the beginning of each KUNIT_*_ASSERTION().
6428bd5d74bSMickaël Salaün  * Cf. KUNIT_CURRENT_LOC.
6438bd5d74bSMickaël Salaün  */
6448bd5d74bSMickaël Salaün #define _KUNIT_SAVE_LOC(test) do {					       \
6458bd5d74bSMickaël Salaün 	WRITE_ONCE(test->last_seen.file, __FILE__);			       \
6468bd5d74bSMickaël Salaün 	WRITE_ONCE(test->last_seen.line, __LINE__);			       \
6478bd5d74bSMickaël Salaün } while (0)
6488bd5d74bSMickaël Salaün 
64973cda7bbSBrendan Higgins /**
65073cda7bbSBrendan Higgins  * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
65173cda7bbSBrendan Higgins  * @test: The test context object.
65273cda7bbSBrendan Higgins  *
65373cda7bbSBrendan Higgins  * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
65473cda7bbSBrendan Higgins  * words, it does nothing and only exists for code clarity. See
65573cda7bbSBrendan Higgins  * KUNIT_EXPECT_TRUE() for more information.
65673cda7bbSBrendan Higgins  */
6578bd5d74bSMickaël Salaün #define KUNIT_SUCCEED(test) _KUNIT_SAVE_LOC(test)
65873cda7bbSBrendan Higgins 
65926075518SDavid Gow void __noreturn __kunit_abort(struct kunit *test);
66026075518SDavid Gow 
661806cb227SDavid Gow void __printf(6, 7) __kunit_do_failed_assertion(struct kunit *test,
66221957f90SDaniel Latypov 						const struct kunit_loc *loc,
66321957f90SDaniel Latypov 						enum kunit_assert_type type,
6647466886bSMiguel Ojeda 						const struct kunit_assert *assert,
665a8495ad8SDaniel Latypov 						assert_format_t assert_format,
66673cda7bbSBrendan Higgins 						const char *fmt, ...);
66773cda7bbSBrendan Higgins 
66897d453bcSDaniel Latypov #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
669c2741453SDaniel Latypov 	static const struct kunit_loc __loc = KUNIT_CURRENT_LOC;	       \
670c1144e01SDaniel Latypov 	const struct assert_class __assertion = INITIALIZER;		       \
67126075518SDavid Gow 	__kunit_do_failed_assertion(test,				       \
672c2741453SDaniel Latypov 				    &__loc,				       \
67321957f90SDaniel Latypov 				    assert_type,			       \
67473cda7bbSBrendan Higgins 				    &__assertion.assert,		       \
675a8495ad8SDaniel Latypov 				    assert_format,			       \
67673cda7bbSBrendan Higgins 				    fmt,				       \
67773cda7bbSBrendan Higgins 				    ##__VA_ARGS__);			       \
67826075518SDavid Gow 	if (assert_type == KUNIT_ASSERTION)				       \
67926075518SDavid Gow 		__kunit_abort(test);					       \
68073cda7bbSBrendan Higgins } while (0)
68173cda7bbSBrendan Higgins 
68273cda7bbSBrendan Higgins 
6838bd5d74bSMickaël Salaün #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) do {		       \
6848bd5d74bSMickaël Salaün 	_KUNIT_SAVE_LOC(test);						       \
68597d453bcSDaniel Latypov 	_KUNIT_FAILED(test,						       \
68621957f90SDaniel Latypov 		      assert_type,					       \
68773cda7bbSBrendan Higgins 		      kunit_fail_assert,				       \
688a8495ad8SDaniel Latypov 		      kunit_fail_assert_format,				       \
689a8495ad8SDaniel Latypov 		      {},						       \
69073cda7bbSBrendan Higgins 		      fmt,						       \
6918bd5d74bSMickaël Salaün 		      ##__VA_ARGS__);					       \
6928bd5d74bSMickaël Salaün } while (0)
69373cda7bbSBrendan Higgins 
69473cda7bbSBrendan Higgins /**
69573cda7bbSBrendan Higgins  * KUNIT_FAIL() - Always causes a test to fail when evaluated.
69673cda7bbSBrendan Higgins  * @test: The test context object.
69773cda7bbSBrendan Higgins  * @fmt: an informational message to be printed when the assertion is made.
69873cda7bbSBrendan Higgins  * @...: string format arguments.
69973cda7bbSBrendan Higgins  *
70073cda7bbSBrendan Higgins  * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
70173cda7bbSBrendan Higgins  * other words, it always results in a failed expectation, and consequently
70273cda7bbSBrendan Higgins  * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
70373cda7bbSBrendan Higgins  * for more information.
70473cda7bbSBrendan Higgins  */
70573cda7bbSBrendan Higgins #define KUNIT_FAIL(test, fmt, ...)					       \
70673cda7bbSBrendan Higgins 	KUNIT_FAIL_ASSERTION(test,					       \
70773cda7bbSBrendan Higgins 			     KUNIT_EXPECTATION,				       \
70873cda7bbSBrendan Higgins 			     fmt,					       \
70973cda7bbSBrendan Higgins 			     ##__VA_ARGS__)
71073cda7bbSBrendan Higgins 
711697365c0SDaniel Latypov /* Helper to safely pass around an initializer list to other macros. */
712697365c0SDaniel Latypov #define KUNIT_INIT_ASSERT(initializers...) { initializers }
713697365c0SDaniel Latypov 
71473cda7bbSBrendan Higgins #define KUNIT_UNARY_ASSERTION(test,					       \
71573cda7bbSBrendan Higgins 			      assert_type,				       \
716697365c0SDaniel Latypov 			      condition_,				       \
717697365c0SDaniel Latypov 			      expected_true_,				       \
71873cda7bbSBrendan Higgins 			      fmt,					       \
71973cda7bbSBrendan Higgins 			      ...)					       \
72097d453bcSDaniel Latypov do {									       \
7218bd5d74bSMickaël Salaün 	_KUNIT_SAVE_LOC(test);						       \
722697365c0SDaniel Latypov 	if (likely(!!(condition_) == !!expected_true_))			       \
72397d453bcSDaniel Latypov 		break;							       \
72497d453bcSDaniel Latypov 									       \
72597d453bcSDaniel Latypov 	_KUNIT_FAILED(test,						       \
72621957f90SDaniel Latypov 		      assert_type,					       \
72773cda7bbSBrendan Higgins 		      kunit_unary_assert,				       \
728a8495ad8SDaniel Latypov 		      kunit_unary_assert_format,			       \
729697365c0SDaniel Latypov 		      KUNIT_INIT_ASSERT(.condition = #condition_,	       \
730697365c0SDaniel Latypov 					.expected_true = expected_true_),      \
73173cda7bbSBrendan Higgins 		      fmt,						       \
73297d453bcSDaniel Latypov 		      ##__VA_ARGS__);					       \
73397d453bcSDaniel Latypov } while (0)
73473cda7bbSBrendan Higgins 
73573cda7bbSBrendan Higgins #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)       \
73673cda7bbSBrendan Higgins 	KUNIT_UNARY_ASSERTION(test,					       \
73773cda7bbSBrendan Higgins 			      assert_type,				       \
73873cda7bbSBrendan Higgins 			      condition,				       \
73973cda7bbSBrendan Higgins 			      true,					       \
74073cda7bbSBrendan Higgins 			      fmt,					       \
74173cda7bbSBrendan Higgins 			      ##__VA_ARGS__)
74273cda7bbSBrendan Higgins 
74373cda7bbSBrendan Higgins #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)      \
74473cda7bbSBrendan Higgins 	KUNIT_UNARY_ASSERTION(test,					       \
74573cda7bbSBrendan Higgins 			      assert_type,				       \
74673cda7bbSBrendan Higgins 			      condition,				       \
74773cda7bbSBrendan Higgins 			      false,					       \
74873cda7bbSBrendan Higgins 			      fmt,					       \
74973cda7bbSBrendan Higgins 			      ##__VA_ARGS__)
75073cda7bbSBrendan Higgins 
75173cda7bbSBrendan Higgins /*
75273cda7bbSBrendan Higgins  * A factory macro for defining the assertions and expectations for the basic
75373cda7bbSBrendan Higgins  * comparisons defined for the built in types.
75473cda7bbSBrendan Higgins  *
75573cda7bbSBrendan Higgins  * Unfortunately, there is no common type that all types can be promoted to for
75673cda7bbSBrendan Higgins  * which all the binary operators behave the same way as for the actual types
75773cda7bbSBrendan Higgins  * (for example, there is no type that long long and unsigned long long can
75873cda7bbSBrendan Higgins  * both be cast to where the comparison result is preserved for all values). So
75973cda7bbSBrendan Higgins  * the best we can do is do the comparison in the original types and then coerce
76073cda7bbSBrendan Higgins  * everything to long long for printing; this way, the comparison behaves
76173cda7bbSBrendan Higgins  * correctly and the printed out value usually makes sense without
76273cda7bbSBrendan Higgins  * interpretation, but can always be interpreted to figure out the actual
76373cda7bbSBrendan Higgins  * value.
76473cda7bbSBrendan Higgins  */
76573cda7bbSBrendan Higgins #define KUNIT_BASE_BINARY_ASSERTION(test,				       \
76673cda7bbSBrendan Higgins 				    assert_class,			       \
767064ff292SDaniel Latypov 				    format_func,			       \
76873cda7bbSBrendan Higgins 				    assert_type,			       \
76973cda7bbSBrendan Higgins 				    left,				       \
77073cda7bbSBrendan Higgins 				    op,					       \
77173cda7bbSBrendan Higgins 				    right,				       \
77273cda7bbSBrendan Higgins 				    fmt,				       \
77373cda7bbSBrendan Higgins 				    ...)				       \
77473cda7bbSBrendan Higgins do {									       \
775c2741453SDaniel Latypov 	const typeof(left) __left = (left);				       \
776c2741453SDaniel Latypov 	const typeof(right) __right = (right);				       \
7772b6861e2SDaniel Latypov 	static const struct kunit_binary_assert_text __text = {		       \
7782b6861e2SDaniel Latypov 		.operation = #op,					       \
7792b6861e2SDaniel Latypov 		.left_text = #left,					       \
7802b6861e2SDaniel Latypov 		.right_text = #right,					       \
7812b6861e2SDaniel Latypov 	};								       \
78273cda7bbSBrendan Higgins 									       \
7838bd5d74bSMickaël Salaün 	_KUNIT_SAVE_LOC(test);						       \
78497d453bcSDaniel Latypov 	if (likely(__left op __right))					       \
78597d453bcSDaniel Latypov 		break;							       \
78697d453bcSDaniel Latypov 									       \
78797d453bcSDaniel Latypov 	_KUNIT_FAILED(test,						       \
78821957f90SDaniel Latypov 		      assert_type,					       \
78973cda7bbSBrendan Higgins 		      assert_class,					       \
790a8495ad8SDaniel Latypov 		      format_func,					       \
791697365c0SDaniel Latypov 		      KUNIT_INIT_ASSERT(.text = &__text,		       \
792697365c0SDaniel Latypov 					.left_value = __left,		       \
793697365c0SDaniel Latypov 					.right_value = __right),	       \
79473cda7bbSBrendan Higgins 		      fmt,						       \
79573cda7bbSBrendan Higgins 		      ##__VA_ARGS__);					       \
79673cda7bbSBrendan Higgins } while (0)
79773cda7bbSBrendan Higgins 
79840f39777SDaniel Latypov #define KUNIT_BINARY_INT_ASSERTION(test,				       \
79940f39777SDaniel Latypov 				   assert_type,				       \
80040f39777SDaniel Latypov 				   left,				       \
80140f39777SDaniel Latypov 				   op,					       \
80240f39777SDaniel Latypov 				   right,				       \
80340f39777SDaniel Latypov 				   fmt,					       \
80440f39777SDaniel Latypov 				    ...)				       \
80540f39777SDaniel Latypov 	KUNIT_BASE_BINARY_ASSERTION(test,				       \
80640f39777SDaniel Latypov 				    kunit_binary_assert,		       \
807064ff292SDaniel Latypov 				    kunit_binary_assert_format,		       \
80840f39777SDaniel Latypov 				    assert_type,			       \
80940f39777SDaniel Latypov 				    left, op, right,			       \
81040f39777SDaniel Latypov 				    fmt,				       \
81140f39777SDaniel Latypov 				    ##__VA_ARGS__)
81240f39777SDaniel Latypov 
8136125a5c7SDaniel Latypov #define KUNIT_BINARY_PTR_ASSERTION(test,				       \
81473cda7bbSBrendan Higgins 				   assert_type,				       \
81573cda7bbSBrendan Higgins 				   left,				       \
8166125a5c7SDaniel Latypov 				   op,					       \
81773cda7bbSBrendan Higgins 				   right,				       \
81873cda7bbSBrendan Higgins 				   fmt,					       \
81973cda7bbSBrendan Higgins 				    ...)				       \
8206125a5c7SDaniel Latypov 	KUNIT_BASE_BINARY_ASSERTION(test,				       \
82173cda7bbSBrendan Higgins 				    kunit_binary_ptr_assert,		       \
822064ff292SDaniel Latypov 				    kunit_binary_ptr_assert_format,	       \
82373cda7bbSBrendan Higgins 				    assert_type,			       \
8246125a5c7SDaniel Latypov 				    left, op, right,			       \
82573cda7bbSBrendan Higgins 				    fmt,				       \
82673cda7bbSBrendan Higgins 				    ##__VA_ARGS__)
82773cda7bbSBrendan Higgins 
82873cda7bbSBrendan Higgins #define KUNIT_BINARY_STR_ASSERTION(test,				       \
82973cda7bbSBrendan Higgins 				   assert_type,				       \
83073cda7bbSBrendan Higgins 				   left,				       \
83173cda7bbSBrendan Higgins 				   op,					       \
83273cda7bbSBrendan Higgins 				   right,				       \
83373cda7bbSBrendan Higgins 				   fmt,					       \
83473cda7bbSBrendan Higgins 				   ...)					       \
83573cda7bbSBrendan Higgins do {									       \
8363747b5c0SDavid Gow 	const char *__left = (left);					       \
8373747b5c0SDavid Gow 	const char *__right = (right);					       \
8382b6861e2SDaniel Latypov 	static const struct kunit_binary_assert_text __text = {		       \
8392b6861e2SDaniel Latypov 		.operation = #op,					       \
8402b6861e2SDaniel Latypov 		.left_text = #left,					       \
8412b6861e2SDaniel Latypov 		.right_text = #right,					       \
8422b6861e2SDaniel Latypov 	};								       \
84373cda7bbSBrendan Higgins 									       \
8448bd5d74bSMickaël Salaün 	_KUNIT_SAVE_LOC(test);						       \
8457ece381aSRichard Fitzgerald 	if (likely((__left) && (__right) && (strcmp(__left, __right) op 0)))   \
84697d453bcSDaniel Latypov 		break;							       \
84797d453bcSDaniel Latypov 									       \
84897d453bcSDaniel Latypov 									       \
84997d453bcSDaniel Latypov 	_KUNIT_FAILED(test,						       \
85021957f90SDaniel Latypov 		      assert_type,					       \
85173cda7bbSBrendan Higgins 		      kunit_binary_str_assert,				       \
852a8495ad8SDaniel Latypov 		      kunit_binary_str_assert_format,			       \
853697365c0SDaniel Latypov 		      KUNIT_INIT_ASSERT(.text = &__text,		       \
854697365c0SDaniel Latypov 					.left_value = __left,		       \
855697365c0SDaniel Latypov 					.right_value = __right),	       \
85673cda7bbSBrendan Higgins 		      fmt,						       \
85773cda7bbSBrendan Higgins 		      ##__VA_ARGS__);					       \
85873cda7bbSBrendan Higgins } while (0)
85973cda7bbSBrendan Higgins 
860b8a926beSMaíra Canal #define KUNIT_MEM_ASSERTION(test,					       \
861b8a926beSMaíra Canal 			    assert_type,				       \
862b8a926beSMaíra Canal 			    left,					       \
863b8a926beSMaíra Canal 			    op,						       \
864b8a926beSMaíra Canal 			    right,					       \
86534c68f43SDaniel Latypov 			    size_,					       \
866b8a926beSMaíra Canal 			    fmt,					       \
867b8a926beSMaíra Canal 			    ...)					       \
868b8a926beSMaíra Canal do {									       \
869b8a926beSMaíra Canal 	const void *__left = (left);					       \
870b8a926beSMaíra Canal 	const void *__right = (right);					       \
87134c68f43SDaniel Latypov 	const size_t __size = (size_);					       \
872b8a926beSMaíra Canal 	static const struct kunit_binary_assert_text __text = {		       \
873b8a926beSMaíra Canal 		.operation = #op,					       \
874b8a926beSMaíra Canal 		.left_text = #left,					       \
875b8a926beSMaíra Canal 		.right_text = #right,					       \
876b8a926beSMaíra Canal 	};								       \
877b8a926beSMaíra Canal 									       \
8788bd5d74bSMickaël Salaün 	_KUNIT_SAVE_LOC(test);						       \
879dd2f0a0aSRae Moar 	if (likely(__left && __right))					       \
880b8a926beSMaíra Canal 		if (likely(memcmp(__left, __right, __size) op 0))	       \
881b8a926beSMaíra Canal 			break;						       \
882b8a926beSMaíra Canal 									       \
883b8a926beSMaíra Canal 	_KUNIT_FAILED(test,						       \
884b8a926beSMaíra Canal 		      assert_type,					       \
885b8a926beSMaíra Canal 		      kunit_mem_assert,					       \
886b8a926beSMaíra Canal 		      kunit_mem_assert_format,				       \
88734c68f43SDaniel Latypov 		      KUNIT_INIT_ASSERT(.text = &__text,		       \
88834c68f43SDaniel Latypov 					.left_value = __left,		       \
88934c68f43SDaniel Latypov 					.right_value = __right,		       \
89034c68f43SDaniel Latypov 					.size = __size),		       \
891b8a926beSMaíra Canal 		      fmt,						       \
892b8a926beSMaíra Canal 		      ##__VA_ARGS__);					       \
893b8a926beSMaíra Canal } while (0)
894b8a926beSMaíra Canal 
89573cda7bbSBrendan Higgins #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
89673cda7bbSBrendan Higgins 						assert_type,		       \
89773cda7bbSBrendan Higgins 						ptr,			       \
89873cda7bbSBrendan Higgins 						fmt,			       \
89973cda7bbSBrendan Higgins 						...)			       \
90073cda7bbSBrendan Higgins do {									       \
901c2741453SDaniel Latypov 	const typeof(ptr) __ptr = (ptr);				       \
90273cda7bbSBrendan Higgins 									       \
9038bd5d74bSMickaël Salaün 	_KUNIT_SAVE_LOC(test);						       \
90497d453bcSDaniel Latypov 	if (!IS_ERR_OR_NULL(__ptr))					       \
90597d453bcSDaniel Latypov 		break;							       \
90697d453bcSDaniel Latypov 									       \
90797d453bcSDaniel Latypov 	_KUNIT_FAILED(test,						       \
90821957f90SDaniel Latypov 		      assert_type,					       \
90973cda7bbSBrendan Higgins 		      kunit_ptr_not_err_assert,				       \
910a8495ad8SDaniel Latypov 		      kunit_ptr_not_err_assert_format,			       \
911697365c0SDaniel Latypov 		      KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr),	       \
91273cda7bbSBrendan Higgins 		      fmt,						       \
91373cda7bbSBrendan Higgins 		      ##__VA_ARGS__);					       \
91473cda7bbSBrendan Higgins } while (0)
91573cda7bbSBrendan Higgins 
91673cda7bbSBrendan Higgins /**
91773cda7bbSBrendan Higgins  * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
91873cda7bbSBrendan Higgins  * @test: The test context object.
91973cda7bbSBrendan Higgins  * @condition: an arbitrary boolean expression. The test fails when this does
92073cda7bbSBrendan Higgins  * not evaluate to true.
92173cda7bbSBrendan Higgins  *
92273cda7bbSBrendan Higgins  * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
92373cda7bbSBrendan Higgins  * to fail when the specified condition is not met; however, it will not prevent
92473cda7bbSBrendan Higgins  * the test case from continuing to run; this is otherwise known as an
92573cda7bbSBrendan Higgins  * *expectation failure*.
92673cda7bbSBrendan Higgins  */
92773cda7bbSBrendan Higgins #define KUNIT_EXPECT_TRUE(test, condition) \
9286709d0feSDaniel Latypov 	KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
92973cda7bbSBrendan Higgins 
93073cda7bbSBrendan Higgins #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...)		       \
93173cda7bbSBrendan Higgins 	KUNIT_TRUE_MSG_ASSERTION(test,					       \
93273cda7bbSBrendan Higgins 				 KUNIT_EXPECTATION,			       \
93373cda7bbSBrendan Higgins 				 condition,				       \
93473cda7bbSBrendan Higgins 				 fmt,					       \
93573cda7bbSBrendan Higgins 				 ##__VA_ARGS__)
93673cda7bbSBrendan Higgins 
93773cda7bbSBrendan Higgins /**
93873cda7bbSBrendan Higgins  * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
93973cda7bbSBrendan Higgins  * @test: The test context object.
94073cda7bbSBrendan Higgins  * @condition: an arbitrary boolean expression. The test fails when this does
94173cda7bbSBrendan Higgins  * not evaluate to false.
94273cda7bbSBrendan Higgins  *
94373cda7bbSBrendan Higgins  * Sets an expectation that @condition evaluates to false. See
94473cda7bbSBrendan Higgins  * KUNIT_EXPECT_TRUE() for more information.
94573cda7bbSBrendan Higgins  */
94673cda7bbSBrendan Higgins #define KUNIT_EXPECT_FALSE(test, condition) \
9476709d0feSDaniel Latypov 	KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
94873cda7bbSBrendan Higgins 
94973cda7bbSBrendan Higgins #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...)		       \
95073cda7bbSBrendan Higgins 	KUNIT_FALSE_MSG_ASSERTION(test,					       \
95173cda7bbSBrendan Higgins 				  KUNIT_EXPECTATION,			       \
95273cda7bbSBrendan Higgins 				  condition,				       \
95373cda7bbSBrendan Higgins 				  fmt,					       \
95473cda7bbSBrendan Higgins 				  ##__VA_ARGS__)
95573cda7bbSBrendan Higgins 
95673cda7bbSBrendan Higgins /**
95773cda7bbSBrendan Higgins  * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
95873cda7bbSBrendan Higgins  * @test: The test context object.
95973cda7bbSBrendan Higgins  * @left: an arbitrary expression that evaluates to a primitive C type.
96073cda7bbSBrendan Higgins  * @right: an arbitrary expression that evaluates to a primitive C type.
96173cda7bbSBrendan Higgins  *
96273cda7bbSBrendan Higgins  * Sets an expectation that the values that @left and @right evaluate to are
96373cda7bbSBrendan Higgins  * equal. This is semantically equivalent to
96473cda7bbSBrendan Higgins  * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
96573cda7bbSBrendan Higgins  * more information.
96673cda7bbSBrendan Higgins  */
96773cda7bbSBrendan Higgins #define KUNIT_EXPECT_EQ(test, left, right) \
9686709d0feSDaniel Latypov 	KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
96973cda7bbSBrendan Higgins 
97073cda7bbSBrendan Higgins #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...)		       \
9716125a5c7SDaniel Latypov 	KUNIT_BINARY_INT_ASSERTION(test,				       \
97273cda7bbSBrendan Higgins 				   KUNIT_EXPECTATION,			       \
9736125a5c7SDaniel Latypov 				   left, ==, right,			       \
97473cda7bbSBrendan Higgins 				   fmt,					       \
97573cda7bbSBrendan Higgins 				    ##__VA_ARGS__)
97673cda7bbSBrendan Higgins 
97773cda7bbSBrendan Higgins /**
97873cda7bbSBrendan Higgins  * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
97973cda7bbSBrendan Higgins  * @test: The test context object.
98073cda7bbSBrendan Higgins  * @left: an arbitrary expression that evaluates to a pointer.
98173cda7bbSBrendan Higgins  * @right: an arbitrary expression that evaluates to a pointer.
98273cda7bbSBrendan Higgins  *
98373cda7bbSBrendan Higgins  * Sets an expectation that the values that @left and @right evaluate to are
98473cda7bbSBrendan Higgins  * equal. This is semantically equivalent to
98573cda7bbSBrendan Higgins  * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
98673cda7bbSBrendan Higgins  * more information.
98773cda7bbSBrendan Higgins  */
98873cda7bbSBrendan Higgins #define KUNIT_EXPECT_PTR_EQ(test, left, right)				       \
9896709d0feSDaniel Latypov 	KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
99073cda7bbSBrendan Higgins 
99173cda7bbSBrendan Higgins #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
9926125a5c7SDaniel Latypov 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
99373cda7bbSBrendan Higgins 				   KUNIT_EXPECTATION,			       \
9946125a5c7SDaniel Latypov 				   left, ==, right,			       \
99573cda7bbSBrendan Higgins 				   fmt,					       \
99673cda7bbSBrendan Higgins 				   ##__VA_ARGS__)
99773cda7bbSBrendan Higgins 
99873cda7bbSBrendan Higgins /**
99973cda7bbSBrendan Higgins  * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
100073cda7bbSBrendan Higgins  * @test: The test context object.
100173cda7bbSBrendan Higgins  * @left: an arbitrary expression that evaluates to a primitive C type.
100273cda7bbSBrendan Higgins  * @right: an arbitrary expression that evaluates to a primitive C type.
100373cda7bbSBrendan Higgins  *
100473cda7bbSBrendan Higgins  * Sets an expectation that the values that @left and @right evaluate to are not
100573cda7bbSBrendan Higgins  * equal. This is semantically equivalent to
100673cda7bbSBrendan Higgins  * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
100773cda7bbSBrendan Higgins  * more information.
100873cda7bbSBrendan Higgins  */
100973cda7bbSBrendan Higgins #define KUNIT_EXPECT_NE(test, left, right) \
10106709d0feSDaniel Latypov 	KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
101173cda7bbSBrendan Higgins 
101273cda7bbSBrendan Higgins #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...)		       \
10136125a5c7SDaniel Latypov 	KUNIT_BINARY_INT_ASSERTION(test,				       \
101473cda7bbSBrendan Higgins 				   KUNIT_EXPECTATION,			       \
10156125a5c7SDaniel Latypov 				   left, !=, right,			       \
101673cda7bbSBrendan Higgins 				   fmt,					       \
101773cda7bbSBrendan Higgins 				    ##__VA_ARGS__)
101873cda7bbSBrendan Higgins 
101973cda7bbSBrendan Higgins /**
102073cda7bbSBrendan Higgins  * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
102173cda7bbSBrendan Higgins  * @test: The test context object.
102273cda7bbSBrendan Higgins  * @left: an arbitrary expression that evaluates to a pointer.
102373cda7bbSBrendan Higgins  * @right: an arbitrary expression that evaluates to a pointer.
102473cda7bbSBrendan Higgins  *
102573cda7bbSBrendan Higgins  * Sets an expectation that the values that @left and @right evaluate to are not
102673cda7bbSBrendan Higgins  * equal. This is semantically equivalent to
102773cda7bbSBrendan Higgins  * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
102873cda7bbSBrendan Higgins  * more information.
102973cda7bbSBrendan Higgins  */
103073cda7bbSBrendan Higgins #define KUNIT_EXPECT_PTR_NE(test, left, right)				       \
10316709d0feSDaniel Latypov 	KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
103273cda7bbSBrendan Higgins 
103373cda7bbSBrendan Higgins #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
10346125a5c7SDaniel Latypov 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
103573cda7bbSBrendan Higgins 				   KUNIT_EXPECTATION,			       \
10366125a5c7SDaniel Latypov 				   left, !=, right,			       \
103773cda7bbSBrendan Higgins 				   fmt,					       \
103873cda7bbSBrendan Higgins 				   ##__VA_ARGS__)
103973cda7bbSBrendan Higgins 
104073cda7bbSBrendan Higgins /**
104173cda7bbSBrendan Higgins  * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
104273cda7bbSBrendan Higgins  * @test: The test context object.
104373cda7bbSBrendan Higgins  * @left: an arbitrary expression that evaluates to a primitive C type.
104473cda7bbSBrendan Higgins  * @right: an arbitrary expression that evaluates to a primitive C type.
104573cda7bbSBrendan Higgins  *
104673cda7bbSBrendan Higgins  * Sets an expectation that the value that @left evaluates to is less than the
104773cda7bbSBrendan Higgins  * value that @right evaluates to. This is semantically equivalent to
104873cda7bbSBrendan Higgins  * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
104973cda7bbSBrendan Higgins  * more information.
105073cda7bbSBrendan Higgins  */
105173cda7bbSBrendan Higgins #define KUNIT_EXPECT_LT(test, left, right) \
10526709d0feSDaniel Latypov 	KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
105373cda7bbSBrendan Higgins 
105473cda7bbSBrendan Higgins #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...)		       \
105540f39777SDaniel Latypov 	KUNIT_BINARY_INT_ASSERTION(test,				       \
105673cda7bbSBrendan Higgins 				   KUNIT_EXPECTATION,			       \
105740f39777SDaniel Latypov 				   left, <, right,			       \
105873cda7bbSBrendan Higgins 				   fmt,					       \
105973cda7bbSBrendan Higgins 				    ##__VA_ARGS__)
106073cda7bbSBrendan Higgins 
106173cda7bbSBrendan Higgins /**
106273cda7bbSBrendan Higgins  * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
106373cda7bbSBrendan Higgins  * @test: The test context object.
106473cda7bbSBrendan Higgins  * @left: an arbitrary expression that evaluates to a primitive C type.
106573cda7bbSBrendan Higgins  * @right: an arbitrary expression that evaluates to a primitive C type.
106673cda7bbSBrendan Higgins  *
106773cda7bbSBrendan Higgins  * Sets an expectation that the value that @left evaluates to is less than or
106873cda7bbSBrendan Higgins  * equal to the value that @right evaluates to. Semantically this is equivalent
106973cda7bbSBrendan Higgins  * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
107073cda7bbSBrendan Higgins  * more information.
107173cda7bbSBrendan Higgins  */
107273cda7bbSBrendan Higgins #define KUNIT_EXPECT_LE(test, left, right) \
10736709d0feSDaniel Latypov 	KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
107473cda7bbSBrendan Higgins 
107573cda7bbSBrendan Higgins #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...)		       \
107640f39777SDaniel Latypov 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1077aded3cadSSander Vanheule 				   KUNIT_EXPECTATION,			       \
107840f39777SDaniel Latypov 				   left, <=, right,			       \
107973cda7bbSBrendan Higgins 				   fmt,					       \
108073cda7bbSBrendan Higgins 				    ##__VA_ARGS__)
108173cda7bbSBrendan Higgins 
108273cda7bbSBrendan Higgins /**
108373cda7bbSBrendan Higgins  * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
108473cda7bbSBrendan Higgins  * @test: The test context object.
108573cda7bbSBrendan Higgins  * @left: an arbitrary expression that evaluates to a primitive C type.
108673cda7bbSBrendan Higgins  * @right: an arbitrary expression that evaluates to a primitive C type.
108773cda7bbSBrendan Higgins  *
108873cda7bbSBrendan Higgins  * Sets an expectation that the value that @left evaluates to is greater than
108973cda7bbSBrendan Higgins  * the value that @right evaluates to. This is semantically equivalent to
109073cda7bbSBrendan Higgins  * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
109173cda7bbSBrendan Higgins  * more information.
109273cda7bbSBrendan Higgins  */
109373cda7bbSBrendan Higgins #define KUNIT_EXPECT_GT(test, left, right) \
10946709d0feSDaniel Latypov 	KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
109573cda7bbSBrendan Higgins 
109673cda7bbSBrendan Higgins #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...)		       \
109740f39777SDaniel Latypov 	KUNIT_BINARY_INT_ASSERTION(test,				       \
109873cda7bbSBrendan Higgins 				   KUNIT_EXPECTATION,			       \
109940f39777SDaniel Latypov 				   left, >, right,			       \
110073cda7bbSBrendan Higgins 				   fmt,					       \
110173cda7bbSBrendan Higgins 				    ##__VA_ARGS__)
110273cda7bbSBrendan Higgins 
110373cda7bbSBrendan Higgins /**
110473cda7bbSBrendan Higgins  * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
110573cda7bbSBrendan Higgins  * @test: The test context object.
110673cda7bbSBrendan Higgins  * @left: an arbitrary expression that evaluates to a primitive C type.
110773cda7bbSBrendan Higgins  * @right: an arbitrary expression that evaluates to a primitive C type.
110873cda7bbSBrendan Higgins  *
110973cda7bbSBrendan Higgins  * Sets an expectation that the value that @left evaluates to is greater than
111073cda7bbSBrendan Higgins  * the value that @right evaluates to. This is semantically equivalent to
111173cda7bbSBrendan Higgins  * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
111273cda7bbSBrendan Higgins  * more information.
111373cda7bbSBrendan Higgins  */
111473cda7bbSBrendan Higgins #define KUNIT_EXPECT_GE(test, left, right) \
11156709d0feSDaniel Latypov 	KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
111673cda7bbSBrendan Higgins 
111773cda7bbSBrendan Higgins #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...)		       \
111840f39777SDaniel Latypov 	KUNIT_BINARY_INT_ASSERTION(test,				       \
111973cda7bbSBrendan Higgins 				   KUNIT_EXPECTATION,			       \
112040f39777SDaniel Latypov 				   left, >=, right,			       \
112173cda7bbSBrendan Higgins 				   fmt,					       \
112273cda7bbSBrendan Higgins 				    ##__VA_ARGS__)
112373cda7bbSBrendan Higgins 
112473cda7bbSBrendan Higgins /**
112573cda7bbSBrendan Higgins  * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
112673cda7bbSBrendan Higgins  * @test: The test context object.
112773cda7bbSBrendan Higgins  * @left: an arbitrary expression that evaluates to a null terminated string.
112873cda7bbSBrendan Higgins  * @right: an arbitrary expression that evaluates to a null terminated string.
112973cda7bbSBrendan Higgins  *
113073cda7bbSBrendan Higgins  * Sets an expectation that the values that @left and @right evaluate to are
113173cda7bbSBrendan Higgins  * equal. This is semantically equivalent to
113273cda7bbSBrendan Higgins  * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
113373cda7bbSBrendan Higgins  * for more information.
113473cda7bbSBrendan Higgins  */
113573cda7bbSBrendan Higgins #define KUNIT_EXPECT_STREQ(test, left, right) \
11366709d0feSDaniel Latypov 	KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
113773cda7bbSBrendan Higgins 
113873cda7bbSBrendan Higgins #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...)		       \
1139955df7d8SDaniel Latypov 	KUNIT_BINARY_STR_ASSERTION(test,				       \
114073cda7bbSBrendan Higgins 				   KUNIT_EXPECTATION,			       \
1141955df7d8SDaniel Latypov 				   left, ==, right,			       \
114273cda7bbSBrendan Higgins 				   fmt,					       \
114373cda7bbSBrendan Higgins 				   ##__VA_ARGS__)
114473cda7bbSBrendan Higgins 
114573cda7bbSBrendan Higgins /**
114673cda7bbSBrendan Higgins  * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
114773cda7bbSBrendan Higgins  * @test: The test context object.
114873cda7bbSBrendan Higgins  * @left: an arbitrary expression that evaluates to a null terminated string.
114973cda7bbSBrendan Higgins  * @right: an arbitrary expression that evaluates to a null terminated string.
115073cda7bbSBrendan Higgins  *
115173cda7bbSBrendan Higgins  * Sets an expectation that the values that @left and @right evaluate to are
115273cda7bbSBrendan Higgins  * not equal. This is semantically equivalent to
115373cda7bbSBrendan Higgins  * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
115473cda7bbSBrendan Higgins  * for more information.
115573cda7bbSBrendan Higgins  */
115673cda7bbSBrendan Higgins #define KUNIT_EXPECT_STRNEQ(test, left, right) \
11576709d0feSDaniel Latypov 	KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
115873cda7bbSBrendan Higgins 
115973cda7bbSBrendan Higgins #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
1160955df7d8SDaniel Latypov 	KUNIT_BINARY_STR_ASSERTION(test,				       \
116173cda7bbSBrendan Higgins 				   KUNIT_EXPECTATION,			       \
1162955df7d8SDaniel Latypov 				   left, !=, right,			       \
116373cda7bbSBrendan Higgins 				   fmt,					       \
116473cda7bbSBrendan Higgins 				   ##__VA_ARGS__)
116573cda7bbSBrendan Higgins 
116673cda7bbSBrendan Higgins /**
1167b8a926beSMaíra Canal  * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.
1168b8a926beSMaíra Canal  * @test: The test context object.
1169b8a926beSMaíra Canal  * @left: An arbitrary expression that evaluates to the specified size.
1170b8a926beSMaíra Canal  * @right: An arbitrary expression that evaluates to the specified size.
1171b8a926beSMaíra Canal  * @size: Number of bytes compared.
1172b8a926beSMaíra Canal  *
1173b8a926beSMaíra Canal  * Sets an expectation that the values that @left and @right evaluate to are
1174b8a926beSMaíra Canal  * equal. This is semantically equivalent to
1175b8a926beSMaíra Canal  * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
1176b8a926beSMaíra Canal  * KUNIT_EXPECT_TRUE() for more information.
1177b8a926beSMaíra Canal  *
1178b8a926beSMaíra Canal  * Although this expectation works for any memory block, it is not recommended
1179b8a926beSMaíra Canal  * for comparing more structured data, such as structs. This expectation is
1180b8a926beSMaíra Canal  * recommended for comparing, for example, data arrays.
1181b8a926beSMaíra Canal  */
1182b8a926beSMaíra Canal #define KUNIT_EXPECT_MEMEQ(test, left, right, size) \
1183b8a926beSMaíra Canal 	KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)
1184b8a926beSMaíra Canal 
1185b8a926beSMaíra Canal #define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...)	       \
1186b8a926beSMaíra Canal 	KUNIT_MEM_ASSERTION(test,					       \
1187b8a926beSMaíra Canal 			    KUNIT_EXPECTATION,				       \
1188b8a926beSMaíra Canal 			    left, ==, right,				       \
1189b8a926beSMaíra Canal 			    size,					       \
1190b8a926beSMaíra Canal 			    fmt,					       \
1191b8a926beSMaíra Canal 			    ##__VA_ARGS__)
1192b8a926beSMaíra Canal 
1193b8a926beSMaíra Canal /**
1194b8a926beSMaíra Canal  * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.
1195b8a926beSMaíra Canal  * @test: The test context object.
1196b8a926beSMaíra Canal  * @left: An arbitrary expression that evaluates to the specified size.
1197b8a926beSMaíra Canal  * @right: An arbitrary expression that evaluates to the specified size.
1198b8a926beSMaíra Canal  * @size: Number of bytes compared.
1199b8a926beSMaíra Canal  *
1200b8a926beSMaíra Canal  * Sets an expectation that the values that @left and @right evaluate to are
1201b8a926beSMaíra Canal  * not equal. This is semantically equivalent to
1202b8a926beSMaíra Canal  * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1203b8a926beSMaíra Canal  * KUNIT_EXPECT_TRUE() for more information.
1204b8a926beSMaíra Canal  *
1205b8a926beSMaíra Canal  * Although this expectation works for any memory block, it is not recommended
1206b8a926beSMaíra Canal  * for comparing more structured data, such as structs. This expectation is
1207b8a926beSMaíra Canal  * recommended for comparing, for example, data arrays.
1208b8a926beSMaíra Canal  */
1209b8a926beSMaíra Canal #define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \
1210b8a926beSMaíra Canal 	KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)
1211b8a926beSMaíra Canal 
1212b8a926beSMaíra Canal #define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...)	       \
1213b8a926beSMaíra Canal 	KUNIT_MEM_ASSERTION(test,					       \
1214b8a926beSMaíra Canal 			    KUNIT_EXPECTATION,				       \
1215b8a926beSMaíra Canal 			    left, !=, right,				       \
1216b8a926beSMaíra Canal 			    size,					       \
1217b8a926beSMaíra Canal 			    fmt,					       \
1218b8a926beSMaíra Canal 			    ##__VA_ARGS__)
1219b8a926beSMaíra Canal 
1220b8a926beSMaíra Canal /**
1221caae9458SRicardo Ribalda  * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
1222caae9458SRicardo Ribalda  * @test: The test context object.
1223caae9458SRicardo Ribalda  * @ptr: an arbitrary pointer.
1224caae9458SRicardo Ribalda  *
1225caae9458SRicardo Ribalda  * Sets an expectation that the value that @ptr evaluates to is null. This is
1226caae9458SRicardo Ribalda  * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
1227caae9458SRicardo Ribalda  * See KUNIT_EXPECT_TRUE() for more information.
1228caae9458SRicardo Ribalda  */
1229caae9458SRicardo Ribalda #define KUNIT_EXPECT_NULL(test, ptr)				               \
1230caae9458SRicardo Ribalda 	KUNIT_EXPECT_NULL_MSG(test,					       \
1231caae9458SRicardo Ribalda 			      ptr,					       \
1232caae9458SRicardo Ribalda 			      NULL)
1233caae9458SRicardo Ribalda 
1234caae9458SRicardo Ribalda #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...)	                       \
1235caae9458SRicardo Ribalda 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1236caae9458SRicardo Ribalda 				   KUNIT_EXPECTATION,			       \
1237caae9458SRicardo Ribalda 				   ptr, ==, NULL,			       \
1238caae9458SRicardo Ribalda 				   fmt,					       \
1239caae9458SRicardo Ribalda 				   ##__VA_ARGS__)
1240caae9458SRicardo Ribalda 
1241caae9458SRicardo Ribalda /**
1242caae9458SRicardo Ribalda  * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
1243caae9458SRicardo Ribalda  * @test: The test context object.
1244caae9458SRicardo Ribalda  * @ptr: an arbitrary pointer.
1245caae9458SRicardo Ribalda  *
1246caae9458SRicardo Ribalda  * Sets an expectation that the value that @ptr evaluates to is not null. This
1247caae9458SRicardo Ribalda  * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
1248caae9458SRicardo Ribalda  * See KUNIT_EXPECT_TRUE() for more information.
1249caae9458SRicardo Ribalda  */
1250caae9458SRicardo Ribalda #define KUNIT_EXPECT_NOT_NULL(test, ptr)			               \
1251caae9458SRicardo Ribalda 	KUNIT_EXPECT_NOT_NULL_MSG(test,					       \
1252caae9458SRicardo Ribalda 				  ptr,					       \
1253caae9458SRicardo Ribalda 				  NULL)
1254caae9458SRicardo Ribalda 
1255caae9458SRicardo Ribalda #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...)	                       \
1256caae9458SRicardo Ribalda 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1257caae9458SRicardo Ribalda 				   KUNIT_EXPECTATION,			       \
1258caae9458SRicardo Ribalda 				   ptr, !=, NULL,			       \
1259caae9458SRicardo Ribalda 				   fmt,					       \
1260caae9458SRicardo Ribalda 				   ##__VA_ARGS__)
1261caae9458SRicardo Ribalda 
1262caae9458SRicardo Ribalda /**
126373cda7bbSBrendan Higgins  * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
126473cda7bbSBrendan Higgins  * @test: The test context object.
126573cda7bbSBrendan Higgins  * @ptr: an arbitrary pointer.
126673cda7bbSBrendan Higgins  *
126773cda7bbSBrendan Higgins  * Sets an expectation that the value that @ptr evaluates to is not null and not
126873cda7bbSBrendan Higgins  * an errno stored in a pointer. This is semantically equivalent to
126973cda7bbSBrendan Higgins  * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
127073cda7bbSBrendan Higgins  * more information.
127173cda7bbSBrendan Higgins  */
127273cda7bbSBrendan Higgins #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
12736709d0feSDaniel Latypov 	KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
127473cda7bbSBrendan Higgins 
127573cda7bbSBrendan Higgins #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
127673cda7bbSBrendan Higgins 	KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
127773cda7bbSBrendan Higgins 						KUNIT_EXPECTATION,	       \
127873cda7bbSBrendan Higgins 						ptr,			       \
127973cda7bbSBrendan Higgins 						fmt,			       \
128073cda7bbSBrendan Higgins 						##__VA_ARGS__)
128173cda7bbSBrendan Higgins 
12827d4087b0SEric Chan /**
12837d4087b0SEric Chan  * KUNIT_FAIL_AND_ABORT() - Always causes a test to fail and abort when evaluated.
12847d4087b0SEric Chan  * @test: The test context object.
12857d4087b0SEric Chan  * @fmt: an informational message to be printed when the assertion is made.
12867d4087b0SEric Chan  * @...: string format arguments.
12877d4087b0SEric Chan  *
12887d4087b0SEric Chan  * The opposite of KUNIT_SUCCEED(), it is an assertion that always fails. In
12897d4087b0SEric Chan  * other words, it always results in a failed assertion, and consequently
12907d4087b0SEric Chan  * always causes the test case to fail and abort when evaluated.
12917d4087b0SEric Chan  * See KUNIT_ASSERT_TRUE() for more information.
12927d4087b0SEric Chan  */
12937d4087b0SEric Chan #define KUNIT_FAIL_AND_ABORT(test, fmt, ...) \
1294e4aea8f8SBrendan Higgins 	KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1295e4aea8f8SBrendan Higgins 
1296e4aea8f8SBrendan Higgins /**
1297e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1298e4aea8f8SBrendan Higgins  * @test: The test context object.
1299e4aea8f8SBrendan Higgins  * @condition: an arbitrary boolean expression. The test fails and aborts when
1300e4aea8f8SBrendan Higgins  * this does not evaluate to true.
1301e4aea8f8SBrendan Higgins  *
1302e4aea8f8SBrendan Higgins  * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1303e4aea8f8SBrendan Higgins  * fail *and immediately abort* when the specified condition is not met. Unlike
1304e4aea8f8SBrendan Higgins  * an expectation failure, it will prevent the test case from continuing to run;
1305e4aea8f8SBrendan Higgins  * this is otherwise known as an *assertion failure*.
1306e4aea8f8SBrendan Higgins  */
1307e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_TRUE(test, condition) \
13086709d0feSDaniel Latypov 	KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
1309e4aea8f8SBrendan Higgins 
1310e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...)		       \
1311e4aea8f8SBrendan Higgins 	KUNIT_TRUE_MSG_ASSERTION(test,					       \
1312e4aea8f8SBrendan Higgins 				 KUNIT_ASSERTION,			       \
1313e4aea8f8SBrendan Higgins 				 condition,				       \
1314e4aea8f8SBrendan Higgins 				 fmt,					       \
1315e4aea8f8SBrendan Higgins 				 ##__VA_ARGS__)
1316e4aea8f8SBrendan Higgins 
1317e4aea8f8SBrendan Higgins /**
1318e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1319e4aea8f8SBrendan Higgins  * @test: The test context object.
1320e4aea8f8SBrendan Higgins  * @condition: an arbitrary boolean expression.
1321e4aea8f8SBrendan Higgins  *
1322e4aea8f8SBrendan Higgins  * Sets an assertion that the value that @condition evaluates to is false. This
1323e4aea8f8SBrendan Higgins  * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1324e4aea8f8SBrendan Higgins  * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1325e4aea8f8SBrendan Higgins  */
1326e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_FALSE(test, condition) \
13276709d0feSDaniel Latypov 	KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
1328e4aea8f8SBrendan Higgins 
1329e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...)		       \
1330e4aea8f8SBrendan Higgins 	KUNIT_FALSE_MSG_ASSERTION(test,					       \
1331e4aea8f8SBrendan Higgins 				  KUNIT_ASSERTION,			       \
1332e4aea8f8SBrendan Higgins 				  condition,				       \
1333e4aea8f8SBrendan Higgins 				  fmt,					       \
1334e4aea8f8SBrendan Higgins 				  ##__VA_ARGS__)
1335e4aea8f8SBrendan Higgins 
1336e4aea8f8SBrendan Higgins /**
1337e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1338e4aea8f8SBrendan Higgins  * @test: The test context object.
1339e4aea8f8SBrendan Higgins  * @left: an arbitrary expression that evaluates to a primitive C type.
1340e4aea8f8SBrendan Higgins  * @right: an arbitrary expression that evaluates to a primitive C type.
1341e4aea8f8SBrendan Higgins  *
1342e4aea8f8SBrendan Higgins  * Sets an assertion that the values that @left and @right evaluate to are
1343e4aea8f8SBrendan Higgins  * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1344e4aea8f8SBrendan Higgins  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1345e4aea8f8SBrendan Higgins  */
1346e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_EQ(test, left, right) \
13476709d0feSDaniel Latypov 	KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
1348e4aea8f8SBrendan Higgins 
1349e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...)		       \
13506125a5c7SDaniel Latypov 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1351e4aea8f8SBrendan Higgins 				   KUNIT_ASSERTION,			       \
13526125a5c7SDaniel Latypov 				   left, ==, right,			       \
1353e4aea8f8SBrendan Higgins 				   fmt,					       \
1354e4aea8f8SBrendan Higgins 				    ##__VA_ARGS__)
1355e4aea8f8SBrendan Higgins 
1356e4aea8f8SBrendan Higgins /**
1357e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1358e4aea8f8SBrendan Higgins  * @test: The test context object.
1359e4aea8f8SBrendan Higgins  * @left: an arbitrary expression that evaluates to a pointer.
1360e4aea8f8SBrendan Higgins  * @right: an arbitrary expression that evaluates to a pointer.
1361e4aea8f8SBrendan Higgins  *
1362e4aea8f8SBrendan Higgins  * Sets an assertion that the values that @left and @right evaluate to are
1363e4aea8f8SBrendan Higgins  * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1364e4aea8f8SBrendan Higgins  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1365e4aea8f8SBrendan Higgins  */
1366e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
13676709d0feSDaniel Latypov 	KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
1368e4aea8f8SBrendan Higgins 
1369e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...)		       \
13706125a5c7SDaniel Latypov 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1371e4aea8f8SBrendan Higgins 				   KUNIT_ASSERTION,			       \
13726125a5c7SDaniel Latypov 				   left, ==, right,			       \
1373e4aea8f8SBrendan Higgins 				   fmt,					       \
1374e4aea8f8SBrendan Higgins 				   ##__VA_ARGS__)
1375e4aea8f8SBrendan Higgins 
1376e4aea8f8SBrendan Higgins /**
1377e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1378e4aea8f8SBrendan Higgins  * @test: The test context object.
1379e4aea8f8SBrendan Higgins  * @left: an arbitrary expression that evaluates to a primitive C type.
1380e4aea8f8SBrendan Higgins  * @right: an arbitrary expression that evaluates to a primitive C type.
1381e4aea8f8SBrendan Higgins  *
1382e4aea8f8SBrendan Higgins  * Sets an assertion that the values that @left and @right evaluate to are not
1383e4aea8f8SBrendan Higgins  * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1384e4aea8f8SBrendan Higgins  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1385e4aea8f8SBrendan Higgins  */
1386e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_NE(test, left, right) \
13876709d0feSDaniel Latypov 	KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
1388e4aea8f8SBrendan Higgins 
1389e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...)		       \
13906125a5c7SDaniel Latypov 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1391e4aea8f8SBrendan Higgins 				   KUNIT_ASSERTION,			       \
13926125a5c7SDaniel Latypov 				   left, !=, right,			       \
1393e4aea8f8SBrendan Higgins 				   fmt,					       \
1394e4aea8f8SBrendan Higgins 				    ##__VA_ARGS__)
1395e4aea8f8SBrendan Higgins 
1396e4aea8f8SBrendan Higgins /**
1397e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1398e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1399e4aea8f8SBrendan Higgins  * @test: The test context object.
1400e4aea8f8SBrendan Higgins  * @left: an arbitrary expression that evaluates to a pointer.
1401e4aea8f8SBrendan Higgins  * @right: an arbitrary expression that evaluates to a pointer.
1402e4aea8f8SBrendan Higgins  *
1403e4aea8f8SBrendan Higgins  * Sets an assertion that the values that @left and @right evaluate to are not
1404e4aea8f8SBrendan Higgins  * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1405e4aea8f8SBrendan Higgins  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1406e4aea8f8SBrendan Higgins  */
1407e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_PTR_NE(test, left, right) \
14086709d0feSDaniel Latypov 	KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1409e4aea8f8SBrendan Higgins 
1410e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...)		       \
14116125a5c7SDaniel Latypov 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1412e4aea8f8SBrendan Higgins 				   KUNIT_ASSERTION,			       \
14136125a5c7SDaniel Latypov 				   left, !=, right,			       \
1414e4aea8f8SBrendan Higgins 				   fmt,					       \
1415e4aea8f8SBrendan Higgins 				   ##__VA_ARGS__)
1416e4aea8f8SBrendan Higgins /**
1417e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1418e4aea8f8SBrendan Higgins  * @test: The test context object.
1419e4aea8f8SBrendan Higgins  * @left: an arbitrary expression that evaluates to a primitive C type.
1420e4aea8f8SBrendan Higgins  * @right: an arbitrary expression that evaluates to a primitive C type.
1421e4aea8f8SBrendan Higgins  *
1422e4aea8f8SBrendan Higgins  * Sets an assertion that the value that @left evaluates to is less than the
1423e4aea8f8SBrendan Higgins  * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1424e4aea8f8SBrendan Higgins  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1425e4aea8f8SBrendan Higgins  * is not met.
1426e4aea8f8SBrendan Higgins  */
1427e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_LT(test, left, right) \
14286709d0feSDaniel Latypov 	KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1429e4aea8f8SBrendan Higgins 
1430e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...)		       \
143140f39777SDaniel Latypov 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1432aded3cadSSander Vanheule 				   KUNIT_ASSERTION,			       \
143340f39777SDaniel Latypov 				   left, <, right,			       \
1434e4aea8f8SBrendan Higgins 				   fmt,					       \
1435e4aea8f8SBrendan Higgins 				    ##__VA_ARGS__)
1436e4aea8f8SBrendan Higgins /**
1437e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1438e4aea8f8SBrendan Higgins  * @test: The test context object.
1439e4aea8f8SBrendan Higgins  * @left: an arbitrary expression that evaluates to a primitive C type.
1440e4aea8f8SBrendan Higgins  * @right: an arbitrary expression that evaluates to a primitive C type.
1441e4aea8f8SBrendan Higgins  *
1442e4aea8f8SBrendan Higgins  * Sets an assertion that the value that @left evaluates to is less than or
1443e4aea8f8SBrendan Higgins  * equal to the value that @right evaluates to. This is the same as
1444e4aea8f8SBrendan Higgins  * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1445e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1446e4aea8f8SBrendan Higgins  */
1447e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_LE(test, left, right) \
14486709d0feSDaniel Latypov 	KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1449e4aea8f8SBrendan Higgins 
1450e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...)		       \
145140f39777SDaniel Latypov 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1452e4aea8f8SBrendan Higgins 				   KUNIT_ASSERTION,			       \
145340f39777SDaniel Latypov 				   left, <=, right,			       \
1454e4aea8f8SBrendan Higgins 				   fmt,					       \
1455e4aea8f8SBrendan Higgins 				    ##__VA_ARGS__)
1456e4aea8f8SBrendan Higgins 
1457e4aea8f8SBrendan Higgins /**
1458e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1459e4aea8f8SBrendan Higgins  * @test: The test context object.
1460e4aea8f8SBrendan Higgins  * @left: an arbitrary expression that evaluates to a primitive C type.
1461e4aea8f8SBrendan Higgins  * @right: an arbitrary expression that evaluates to a primitive C type.
1462e4aea8f8SBrendan Higgins  *
1463e4aea8f8SBrendan Higgins  * Sets an assertion that the value that @left evaluates to is greater than the
1464e4aea8f8SBrendan Higgins  * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1465e4aea8f8SBrendan Higgins  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1466e4aea8f8SBrendan Higgins  * is not met.
1467e4aea8f8SBrendan Higgins  */
1468e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_GT(test, left, right) \
14696709d0feSDaniel Latypov 	KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1470e4aea8f8SBrendan Higgins 
1471e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...)		       \
147240f39777SDaniel Latypov 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1473aded3cadSSander Vanheule 				   KUNIT_ASSERTION,			       \
147440f39777SDaniel Latypov 				   left, >, right,			       \
1475e4aea8f8SBrendan Higgins 				   fmt,					       \
1476e4aea8f8SBrendan Higgins 				    ##__VA_ARGS__)
1477e4aea8f8SBrendan Higgins 
1478e4aea8f8SBrendan Higgins /**
1479e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1480e4aea8f8SBrendan Higgins  * @test: The test context object.
1481e4aea8f8SBrendan Higgins  * @left: an arbitrary expression that evaluates to a primitive C type.
1482e4aea8f8SBrendan Higgins  * @right: an arbitrary expression that evaluates to a primitive C type.
1483e4aea8f8SBrendan Higgins  *
1484e4aea8f8SBrendan Higgins  * Sets an assertion that the value that @left evaluates to is greater than the
1485e4aea8f8SBrendan Higgins  * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1486e4aea8f8SBrendan Higgins  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1487e4aea8f8SBrendan Higgins  * is not met.
1488e4aea8f8SBrendan Higgins  */
1489e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_GE(test, left, right) \
14906709d0feSDaniel Latypov 	KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1491e4aea8f8SBrendan Higgins 
1492e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...)		       \
149340f39777SDaniel Latypov 	KUNIT_BINARY_INT_ASSERTION(test,				       \
1494e4aea8f8SBrendan Higgins 				   KUNIT_ASSERTION,			       \
149540f39777SDaniel Latypov 				   left, >=, right,			       \
1496e4aea8f8SBrendan Higgins 				   fmt,					       \
1497e4aea8f8SBrendan Higgins 				    ##__VA_ARGS__)
1498e4aea8f8SBrendan Higgins 
1499e4aea8f8SBrendan Higgins /**
1500e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1501e4aea8f8SBrendan Higgins  * @test: The test context object.
1502e4aea8f8SBrendan Higgins  * @left: an arbitrary expression that evaluates to a null terminated string.
1503e4aea8f8SBrendan Higgins  * @right: an arbitrary expression that evaluates to a null terminated string.
1504e4aea8f8SBrendan Higgins  *
1505e4aea8f8SBrendan Higgins  * Sets an assertion that the values that @left and @right evaluate to are
1506e4aea8f8SBrendan Higgins  * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1507e4aea8f8SBrendan Higgins  * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1508e4aea8f8SBrendan Higgins  */
1509e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_STREQ(test, left, right) \
15106709d0feSDaniel Latypov 	KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1511e4aea8f8SBrendan Higgins 
1512e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...)		       \
1513955df7d8SDaniel Latypov 	KUNIT_BINARY_STR_ASSERTION(test,				       \
1514e4aea8f8SBrendan Higgins 				   KUNIT_ASSERTION,			       \
1515955df7d8SDaniel Latypov 				   left, ==, right,			       \
1516e4aea8f8SBrendan Higgins 				   fmt,					       \
1517e4aea8f8SBrendan Higgins 				   ##__VA_ARGS__)
1518e4aea8f8SBrendan Higgins 
1519e4aea8f8SBrendan Higgins /**
15202be32bbeSEric Chan  * KUNIT_ASSERT_STRNEQ() - An assertion that strings @left and @right are not equal.
1521e4aea8f8SBrendan Higgins  * @test: The test context object.
1522e4aea8f8SBrendan Higgins  * @left: an arbitrary expression that evaluates to a null terminated string.
1523e4aea8f8SBrendan Higgins  * @right: an arbitrary expression that evaluates to a null terminated string.
1524e4aea8f8SBrendan Higgins  *
15252be32bbeSEric Chan  * Sets an assertion that the values that @left and @right evaluate to are
1526e4aea8f8SBrendan Higgins  * not equal. This is semantically equivalent to
1527e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1528e4aea8f8SBrendan Higgins  * for more information.
1529e4aea8f8SBrendan Higgins  */
1530e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_STRNEQ(test, left, right) \
15316709d0feSDaniel Latypov 	KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1532e4aea8f8SBrendan Higgins 
1533e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...)		       \
1534955df7d8SDaniel Latypov 	KUNIT_BINARY_STR_ASSERTION(test,				       \
1535e4aea8f8SBrendan Higgins 				   KUNIT_ASSERTION,			       \
1536955df7d8SDaniel Latypov 				   left, !=, right,			       \
1537e4aea8f8SBrendan Higgins 				   fmt,					       \
1538e4aea8f8SBrendan Higgins 				   ##__VA_ARGS__)
1539e4aea8f8SBrendan Higgins 
1540e4aea8f8SBrendan Higgins /**
1541ebf51e46SEric Chan  * KUNIT_ASSERT_MEMEQ() - Asserts that the first @size bytes of @left and @right are equal.
1542ebf51e46SEric Chan  * @test: The test context object.
1543ebf51e46SEric Chan  * @left: An arbitrary expression that evaluates to the specified size.
1544ebf51e46SEric Chan  * @right: An arbitrary expression that evaluates to the specified size.
1545ebf51e46SEric Chan  * @size: Number of bytes compared.
1546ebf51e46SEric Chan  *
1547ebf51e46SEric Chan  * Sets an assertion that the values that @left and @right evaluate to are
1548ebf51e46SEric Chan  * equal. This is semantically equivalent to
1549ebf51e46SEric Chan  * KUNIT_ASSERT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
1550ebf51e46SEric Chan  * KUNIT_ASSERT_TRUE() for more information.
1551ebf51e46SEric Chan  *
1552ebf51e46SEric Chan  * Although this assertion works for any memory block, it is not recommended
1553ebf51e46SEric Chan  * for comparing more structured data, such as structs. This assertion is
1554ebf51e46SEric Chan  * recommended for comparing, for example, data arrays.
1555ebf51e46SEric Chan  */
1556ebf51e46SEric Chan #define KUNIT_ASSERT_MEMEQ(test, left, right, size) \
1557ebf51e46SEric Chan 	KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, NULL)
1558ebf51e46SEric Chan 
1559ebf51e46SEric Chan #define KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, fmt, ...)	       \
1560ebf51e46SEric Chan 	KUNIT_MEM_ASSERTION(test,					       \
1561ebf51e46SEric Chan 			    KUNIT_ASSERTION,				       \
1562ebf51e46SEric Chan 			    left, ==, right,				       \
1563ebf51e46SEric Chan 			    size,					       \
1564ebf51e46SEric Chan 			    fmt,					       \
1565ebf51e46SEric Chan 			    ##__VA_ARGS__)
1566ebf51e46SEric Chan 
1567ebf51e46SEric Chan /**
1568ebf51e46SEric Chan  * KUNIT_ASSERT_MEMNEQ() - Asserts that the first @size bytes of @left and @right are not equal.
1569ebf51e46SEric Chan  * @test: The test context object.
1570ebf51e46SEric Chan  * @left: An arbitrary expression that evaluates to the specified size.
1571ebf51e46SEric Chan  * @right: An arbitrary expression that evaluates to the specified size.
1572ebf51e46SEric Chan  * @size: Number of bytes compared.
1573ebf51e46SEric Chan  *
1574ebf51e46SEric Chan  * Sets an assertion that the values that @left and @right evaluate to are
1575ebf51e46SEric Chan  * not equal. This is semantically equivalent to
1576ebf51e46SEric Chan  * KUNIT_ASSERT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1577ebf51e46SEric Chan  * KUNIT_ASSERT_TRUE() for more information.
1578ebf51e46SEric Chan  *
1579ebf51e46SEric Chan  * Although this assertion works for any memory block, it is not recommended
1580ebf51e46SEric Chan  * for comparing more structured data, such as structs. This assertion is
1581ebf51e46SEric Chan  * recommended for comparing, for example, data arrays.
1582ebf51e46SEric Chan  */
1583ebf51e46SEric Chan #define KUNIT_ASSERT_MEMNEQ(test, left, right, size) \
1584ebf51e46SEric Chan 	KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, NULL)
1585ebf51e46SEric Chan 
1586ebf51e46SEric Chan #define KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, fmt, ...)	       \
1587ebf51e46SEric Chan 	KUNIT_MEM_ASSERTION(test,					       \
1588ebf51e46SEric Chan 			    KUNIT_ASSERTION,				       \
1589ebf51e46SEric Chan 			    left, !=, right,				       \
1590ebf51e46SEric Chan 			    size,					       \
1591ebf51e46SEric Chan 			    fmt,					       \
1592ebf51e46SEric Chan 			    ##__VA_ARGS__)
1593ebf51e46SEric Chan 
1594ebf51e46SEric Chan /**
1595caae9458SRicardo Ribalda  * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1596caae9458SRicardo Ribalda  * @test: The test context object.
1597caae9458SRicardo Ribalda  * @ptr: an arbitrary pointer.
1598caae9458SRicardo Ribalda  *
1599caae9458SRicardo Ribalda  * Sets an assertion that the values that @ptr evaluates to is null. This is
1600caae9458SRicardo Ribalda  * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1601caae9458SRicardo Ribalda  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1602caae9458SRicardo Ribalda  */
1603caae9458SRicardo Ribalda #define KUNIT_ASSERT_NULL(test, ptr) \
1604caae9458SRicardo Ribalda 	KUNIT_ASSERT_NULL_MSG(test,					       \
1605caae9458SRicardo Ribalda 			      ptr,					       \
1606caae9458SRicardo Ribalda 			      NULL)
1607caae9458SRicardo Ribalda 
1608caae9458SRicardo Ribalda #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1609caae9458SRicardo Ribalda 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1610caae9458SRicardo Ribalda 				   KUNIT_ASSERTION,			       \
1611caae9458SRicardo Ribalda 				   ptr, ==, NULL,			       \
1612caae9458SRicardo Ribalda 				   fmt,					       \
1613caae9458SRicardo Ribalda 				   ##__VA_ARGS__)
1614caae9458SRicardo Ribalda 
1615caae9458SRicardo Ribalda /**
1616caae9458SRicardo Ribalda  * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1617caae9458SRicardo Ribalda  * @test: The test context object.
1618caae9458SRicardo Ribalda  * @ptr: an arbitrary pointer.
1619caae9458SRicardo Ribalda  *
1620caae9458SRicardo Ribalda  * Sets an assertion that the values that @ptr evaluates to is not null. This
1621caae9458SRicardo Ribalda  * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1622caae9458SRicardo Ribalda  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1623caae9458SRicardo Ribalda  */
1624caae9458SRicardo Ribalda #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1625caae9458SRicardo Ribalda 	KUNIT_ASSERT_NOT_NULL_MSG(test,					       \
1626caae9458SRicardo Ribalda 				  ptr,					       \
1627caae9458SRicardo Ribalda 				  NULL)
1628caae9458SRicardo Ribalda 
1629caae9458SRicardo Ribalda #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1630caae9458SRicardo Ribalda 	KUNIT_BINARY_PTR_ASSERTION(test,				       \
1631caae9458SRicardo Ribalda 				   KUNIT_ASSERTION,			       \
1632caae9458SRicardo Ribalda 				   ptr, !=, NULL,			       \
1633caae9458SRicardo Ribalda 				   fmt,					       \
1634caae9458SRicardo Ribalda 				   ##__VA_ARGS__)
1635caae9458SRicardo Ribalda 
1636caae9458SRicardo Ribalda /**
1637e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1638e4aea8f8SBrendan Higgins  * @test: The test context object.
1639e4aea8f8SBrendan Higgins  * @ptr: an arbitrary pointer.
1640e4aea8f8SBrendan Higgins  *
1641e4aea8f8SBrendan Higgins  * Sets an assertion that the value that @ptr evaluates to is not null and not
1642e4aea8f8SBrendan Higgins  * an errno stored in a pointer. This is the same as
1643e4aea8f8SBrendan Higgins  * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1644e4aea8f8SBrendan Higgins  * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1645e4aea8f8SBrendan Higgins  */
1646e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
16476709d0feSDaniel Latypov 	KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1648e4aea8f8SBrendan Higgins 
1649e4aea8f8SBrendan Higgins #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)		       \
1650e4aea8f8SBrendan Higgins 	KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,			       \
1651e4aea8f8SBrendan Higgins 						KUNIT_ASSERTION,	       \
1652e4aea8f8SBrendan Higgins 						ptr,			       \
1653e4aea8f8SBrendan Higgins 						fmt,			       \
1654e4aea8f8SBrendan Higgins 						##__VA_ARGS__)
1655e4aea8f8SBrendan Higgins 
1656fadb08e7SArpitha Raghunandan /**
1657fadb08e7SArpitha Raghunandan  * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1658fadb08e7SArpitha Raghunandan  * @name:  prefix for the test parameter generator function.
1659fadb08e7SArpitha Raghunandan  * @array: array of test parameters.
1660fadb08e7SArpitha Raghunandan  * @get_desc: function to convert param to description; NULL to use default
1661fadb08e7SArpitha Raghunandan  *
1662fadb08e7SArpitha Raghunandan  * Define function @name_gen_params which uses @array to generate parameters.
1663fadb08e7SArpitha Raghunandan  */
1664fadb08e7SArpitha Raghunandan #define KUNIT_ARRAY_PARAM(name, array, get_desc)						\
1665fadb08e7SArpitha Raghunandan 	static const void *name##_gen_params(const void *prev, char *desc)			\
1666fadb08e7SArpitha Raghunandan 	{											\
1667fadb08e7SArpitha Raghunandan 		typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array);	\
1668fadb08e7SArpitha Raghunandan 		if (__next - (array) < ARRAY_SIZE((array))) {					\
1669fadb08e7SArpitha Raghunandan 			void (*__get_desc)(typeof(__next), char *) = get_desc;			\
1670fadb08e7SArpitha Raghunandan 			if (__get_desc)								\
1671fadb08e7SArpitha Raghunandan 				__get_desc(__next, desc);					\
1672fadb08e7SArpitha Raghunandan 			return __next;								\
1673fadb08e7SArpitha Raghunandan 		}										\
1674fadb08e7SArpitha Raghunandan 		return NULL;									\
1675fadb08e7SArpitha Raghunandan 	}
1676fadb08e7SArpitha Raghunandan 
1677292010eeSBenjamin Berg /**
1678292010eeSBenjamin Berg  * KUNIT_ARRAY_PARAM_DESC() - Define test parameter generator from an array.
1679292010eeSBenjamin Berg  * @name:  prefix for the test parameter generator function.
1680292010eeSBenjamin Berg  * @array: array of test parameters.
1681292010eeSBenjamin Berg  * @desc_member: structure member from array element to use as description
1682292010eeSBenjamin Berg  *
1683292010eeSBenjamin Berg  * Define function @name_gen_params which uses @array to generate parameters.
1684292010eeSBenjamin Berg  */
1685292010eeSBenjamin Berg #define KUNIT_ARRAY_PARAM_DESC(name, array, desc_member)					\
1686292010eeSBenjamin Berg 	static const void *name##_gen_params(const void *prev, char *desc)			\
1687292010eeSBenjamin Berg 	{											\
1688292010eeSBenjamin Berg 		typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array);	\
1689292010eeSBenjamin Berg 		if (__next - (array) < ARRAY_SIZE((array))) {					\
1690292010eeSBenjamin Berg 			strscpy(desc, __next->desc_member, KUNIT_PARAM_DESC_SIZE);		\
1691292010eeSBenjamin Berg 			return __next;								\
1692292010eeSBenjamin Berg 		}										\
1693292010eeSBenjamin Berg 		return NULL;									\
1694292010eeSBenjamin Berg 	}
1695292010eeSBenjamin Berg 
169661695f8cSDaniel Latypov // TODO([email protected]): consider eventually migrating users to explicitly
169761695f8cSDaniel Latypov // include resource.h themselves if they need it.
169861695f8cSDaniel Latypov #include <kunit/resource.h>
169961695f8cSDaniel Latypov 
1700914cc63eSBrendan Higgins #endif /* _KUNIT_TEST_H */
1701