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