/* SPDX-License-Identifier: GPL-2.0 */1/*2* Base unit test (KUnit) API.3*4* Copyright (C) 2019, Google LLC.5* Author: Brendan Higgins <[email protected]>6*/78#ifndef _KUNIT_TEST_H9#define _KUNIT_TEST_H1011#include <kunit/assert.h>12#include <kunit/try-catch.h>1314#include <linux/args.h>15#include <linux/compiler.h>16#include <linux/container_of.h>17#include <linux/err.h>18#include <linux/init.h>19#include <linux/jump_label.h>20#include <linux/kconfig.h>21#include <linux/kref.h>22#include <linux/list.h>23#include <linux/module.h>24#include <linux/slab.h>25#include <linux/spinlock.h>26#include <linux/string.h>27#include <linux/types.h>2829#include <asm/rwonce.h>30#include <asm/sections.h>3132/* Static key: true if any KUnit tests are currently running */33DECLARE_STATIC_KEY_FALSE(kunit_running);3435struct kunit;36struct string_stream;3738/* Maximum size of parameter description string. */39#define KUNIT_PARAM_DESC_SIZE 1284041/* Maximum size of a status comment. */42#define KUNIT_STATUS_COMMENT_SIZE 2564344/*45* TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a46* sub-subtest. See the "Subtests" section in47* https://node-tap.org/tap-protocol/48*/49#define KUNIT_INDENT_LEN 450#define KUNIT_SUBTEST_INDENT " "51#define KUNIT_SUBSUBTEST_INDENT " "5253/**54* enum kunit_status - Type of result for a test or test suite55* @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped56* @KUNIT_FAILURE: Denotes the test has failed.57* @KUNIT_SKIPPED: Denotes the test has been skipped.58*/59enum kunit_status {60KUNIT_SUCCESS,61KUNIT_FAILURE,62KUNIT_SKIPPED,63};6465/* Attribute struct/enum definitions */6667/*68* Speed Attribute is stored as an enum and separated into categories of69* speed: very_slow, slow, and normal. These speeds are relative to70* other KUnit tests.71*72* Note: unset speed attribute acts as default of KUNIT_SPEED_NORMAL.73*/74enum kunit_speed {75KUNIT_SPEED_UNSET,76KUNIT_SPEED_VERY_SLOW,77KUNIT_SPEED_SLOW,78KUNIT_SPEED_NORMAL,79KUNIT_SPEED_MAX = KUNIT_SPEED_NORMAL,80};8182/* Holds attributes for each test case and suite */83struct kunit_attributes {84enum kunit_speed speed;85};8687/**88* struct kunit_case - represents an individual test case.89*90* @run_case: the function representing the actual test case.91* @name: the name of the test case.92* @generate_params: the generator function for parameterized tests.93* @attr: the attributes associated with the test94*95* A test case is a function with the signature,96* ``void (*)(struct kunit *)``97* that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and98* KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated99* with a &struct kunit_suite and will be run after the suite's init100* function and followed by the suite's exit function.101*102* A test case should be static and should only be created with the103* KUNIT_CASE() macro; additionally, every array of test cases should be104* terminated with an empty test case.105*106* Example:107*108* .. code-block:: c109*110* void add_test_basic(struct kunit *test)111* {112* KUNIT_EXPECT_EQ(test, 1, add(1, 0));113* KUNIT_EXPECT_EQ(test, 2, add(1, 1));114* KUNIT_EXPECT_EQ(test, 0, add(-1, 1));115* KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));116* KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));117* }118*119* static struct kunit_case example_test_cases[] = {120* KUNIT_CASE(add_test_basic),121* {}122* };123*124*/125struct kunit_case {126void (*run_case)(struct kunit *test);127const char *name;128const void* (*generate_params)(const void *prev, char *desc);129struct kunit_attributes attr;130131/* private: internal use only. */132enum kunit_status status;133char *module_name;134struct string_stream *log;135};136137static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)138{139switch (status) {140case KUNIT_SKIPPED:141case KUNIT_SUCCESS:142return "ok";143case KUNIT_FAILURE:144return "not ok";145}146return "invalid";147}148149/**150* KUNIT_CASE - A helper for creating a &struct kunit_case151*152* @test_name: a reference to a test case function.153*154* Takes a symbol for a function representing a test case and creates a155* &struct kunit_case object from it. See the documentation for156* &struct kunit_case for an example on how to use it.157*/158#define KUNIT_CASE(test_name) \159{ .run_case = test_name, .name = #test_name, \160.module_name = KBUILD_MODNAME}161162/**163* KUNIT_CASE_ATTR - A helper for creating a &struct kunit_case164* with attributes165*166* @test_name: a reference to a test case function.167* @attributes: a reference to a struct kunit_attributes object containing168* test attributes169*/170#define KUNIT_CASE_ATTR(test_name, attributes) \171{ .run_case = test_name, .name = #test_name, \172.attr = attributes, .module_name = KBUILD_MODNAME}173174/**175* KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case176* with the slow attribute177*178* @test_name: a reference to a test case function.179*/180181#define KUNIT_CASE_SLOW(test_name) \182{ .run_case = test_name, .name = #test_name, \183.attr.speed = KUNIT_SPEED_SLOW, .module_name = KBUILD_MODNAME}184185/**186* KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case187*188* @test_name: a reference to a test case function.189* @gen_params: a reference to a parameter generator function.190*191* The generator function::192*193* const void* gen_params(const void *prev, char *desc)194*195* is used to lazily generate a series of arbitrarily typed values that fit into196* a void*. The argument @prev is the previously returned value, which should be197* used to derive the next value; @prev is set to NULL on the initial generator198* call. When no more values are available, the generator must return NULL.199* Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)200* describing the parameter.201*/202#define KUNIT_CASE_PARAM(test_name, gen_params) \203{ .run_case = test_name, .name = #test_name, \204.generate_params = gen_params, .module_name = KBUILD_MODNAME}205206/**207* KUNIT_CASE_PARAM_ATTR - A helper for creating a parameterized &struct208* kunit_case with attributes209*210* @test_name: a reference to a test case function.211* @gen_params: a reference to a parameter generator function.212* @attributes: a reference to a struct kunit_attributes object containing213* test attributes214*/215#define KUNIT_CASE_PARAM_ATTR(test_name, gen_params, attributes) \216{ .run_case = test_name, .name = #test_name, \217.generate_params = gen_params, \218.attr = attributes, .module_name = KBUILD_MODNAME}219220/**221* struct kunit_suite - describes a related collection of &struct kunit_case222*223* @name: the name of the test. Purely informational.224* @suite_init: called once per test suite before the test cases.225* @suite_exit: called once per test suite after all test cases.226* @init: called before every test case.227* @exit: called after every test case.228* @test_cases: a null terminated array of test cases.229* @attr: the attributes associated with the test suite230*231* A kunit_suite is a collection of related &struct kunit_case s, such that232* @init is called before every test case and @exit is called after every233* test case, similar to the notion of a *test fixture* or a *test class*234* in other unit testing frameworks like JUnit or Googletest.235*236* Note that @exit and @suite_exit will run even if @init or @suite_init237* fail: make sure they can handle any inconsistent state which may result.238*239* Every &struct kunit_case must be associated with a kunit_suite for KUnit240* to run it.241*/242struct kunit_suite {243const char name[256];244int (*suite_init)(struct kunit_suite *suite);245void (*suite_exit)(struct kunit_suite *suite);246int (*init)(struct kunit *test);247void (*exit)(struct kunit *test);248struct kunit_case *test_cases;249struct kunit_attributes attr;250251/* private: internal use only */252char status_comment[KUNIT_STATUS_COMMENT_SIZE];253struct dentry *debugfs;254struct string_stream *log;255int suite_init_err;256bool is_init;257};258259/* Stores an array of suites, end points one past the end */260struct kunit_suite_set {261struct kunit_suite * const *start;262struct kunit_suite * const *end;263};264265/**266* struct kunit - represents a running instance of a test.267*268* @priv: for user to store arbitrary data. Commonly used to pass data269* created in the init function (see &struct kunit_suite).270*271* Used to store information about the current context under which the test272* is running. Most of this data is private and should only be accessed273* indirectly via public functions; the one exception is @priv which can be274* used by the test writer to store arbitrary data.275*/276struct kunit {277void *priv;278279/* private: internal use only. */280const char *name; /* Read only after initialization! */281struct string_stream *log; /* Points at case log after initialization */282struct kunit_try_catch try_catch;283/* param_value is the current parameter value for a test case. */284const void *param_value;285/* param_index stores the index of the parameter in parameterized tests. */286int param_index;287/*288* success starts as true, and may only be set to false during a289* test case; thus, it is safe to update this across multiple290* threads using WRITE_ONCE; however, as a consequence, it may only291* be read after the test case finishes once all threads associated292* with the test case have terminated.293*/294spinlock_t lock; /* Guards all mutable test state. */295enum kunit_status status; /* Read only after test_case finishes! */296/*297* Because resources is a list that may be updated multiple times (with298* new resources) from any thread associated with a test case, we must299* protect it with some type of lock.300*/301struct list_head resources; /* Protected by lock. */302303char status_comment[KUNIT_STATUS_COMMENT_SIZE];304/* Saves the last seen test. Useful to help with faults. */305struct kunit_loc last_seen;306};307308static inline void kunit_set_failure(struct kunit *test)309{310WRITE_ONCE(test->status, KUNIT_FAILURE);311}312313bool kunit_enabled(void);314bool kunit_autorun(void);315const char *kunit_action(void);316const char *kunit_filter_glob(void);317char *kunit_filter(void);318char *kunit_filter_action(void);319320void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log);321322int kunit_run_tests(struct kunit_suite *suite);323324size_t kunit_suite_num_test_cases(struct kunit_suite *suite);325326unsigned int kunit_test_case_num(struct kunit_suite *suite,327struct kunit_case *test_case);328329struct kunit_suite_set330kunit_filter_suites(const struct kunit_suite_set *suite_set,331const char *filter_glob,332char *filters,333char *filter_action,334int *err);335void kunit_free_suite_set(struct kunit_suite_set suite_set);336337int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites,338bool run_tests);339340void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);341342void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin);343void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr);344345struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set,346struct kunit_suite_set suite_set);347348#if IS_BUILTIN(CONFIG_KUNIT)349int kunit_run_all_tests(void);350#else351static inline int kunit_run_all_tests(void)352{353return 0;354}355#endif /* IS_BUILTIN(CONFIG_KUNIT) */356357#define __kunit_test_suites(unique_array, ...) \358static struct kunit_suite *unique_array[] \359__aligned(sizeof(struct kunit_suite *)) \360__used __section(".kunit_test_suites") = { __VA_ARGS__ }361362/**363* kunit_test_suites() - used to register one or more &struct kunit_suite364* with KUnit.365*366* @__suites: a statically allocated list of &struct kunit_suite.367*368* Registers @suites with the test framework.369* This is done by placing the array of struct kunit_suite * in the370* .kunit_test_suites ELF section.371*372* When builtin, KUnit tests are all run via the executor at boot, and when373* built as a module, they run on module load.374*375*/376#define kunit_test_suites(__suites...) \377__kunit_test_suites(__UNIQUE_ID(array), \378##__suites)379380#define kunit_test_suite(suite) kunit_test_suites(&suite)381382#define __kunit_init_test_suites(unique_array, ...) \383static struct kunit_suite *unique_array[] \384__aligned(sizeof(struct kunit_suite *)) \385__used __section(".kunit_init_test_suites") = { __VA_ARGS__ }386387/**388* kunit_test_init_section_suites() - used to register one or more &struct389* kunit_suite containing init functions or390* init data.391*392* @__suites: a statically allocated list of &struct kunit_suite.393*394* This functions similar to kunit_test_suites() except that it compiles the395* list of suites during init phase.396*397* This macro also suffixes the array and suite declarations it makes with398* _probe; so that modpost suppresses warnings about referencing init data399* for symbols named in this manner.400*401* Note: these init tests are not able to be run after boot so there is no402* "run" debugfs file generated for these tests.403*404* Also, do not mark the suite or test case structs with __initdata because405* they will be used after the init phase with debugfs.406*/407#define kunit_test_init_section_suites(__suites...) \408__kunit_init_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \409##__suites)410411#define kunit_test_init_section_suite(suite) \412kunit_test_init_section_suites(&suite)413414#define kunit_suite_for_each_test_case(suite, test_case) \415for (test_case = suite->test_cases; test_case->run_case; test_case++)416417enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);418419/**420* kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.421* @test: The test context object.422* @n: number of elements.423* @size: The size in bytes of the desired memory.424* @gfp: flags passed to underlying kmalloc().425*426* Just like `kmalloc_array(...)`, except the allocation is managed by the test case427* and is automatically cleaned up after the test case concludes. See kunit_add_action()428* for more information.429*430* Note that some internal context data is also allocated with GFP_KERNEL,431* regardless of the gfp passed in.432*/433void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);434435/**436* kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.437* @test: The test context object.438* @size: The size in bytes of the desired memory.439* @gfp: flags passed to underlying kmalloc().440*441* See kmalloc() and kunit_kmalloc_array() for more information.442*443* Note that some internal context data is also allocated with GFP_KERNEL,444* regardless of the gfp passed in.445*/446static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)447{448return kunit_kmalloc_array(test, 1, size, gfp);449}450451/**452* kunit_kfree() - Like kfree except for allocations managed by KUnit.453* @test: The test case to which the resource belongs.454* @ptr: The memory allocation to free.455*/456void kunit_kfree(struct kunit *test, const void *ptr);457458/**459* kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.460* @test: The test context object.461* @size: The size in bytes of the desired memory.462* @gfp: flags passed to underlying kmalloc().463*464* See kzalloc() and kunit_kmalloc_array() for more information.465*/466static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)467{468return kunit_kmalloc(test, size, gfp | __GFP_ZERO);469}470471/**472* kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.473* @test: The test context object.474* @n: number of elements.475* @size: The size in bytes of the desired memory.476* @gfp: flags passed to underlying kmalloc().477*478* See kcalloc() and kunit_kmalloc_array() for more information.479*/480static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)481{482return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);483}484485486/**487* kunit_kfree_const() - conditionally free test managed memory488* @test: The test context object.489* @x: pointer to the memory490*491* Calls kunit_kfree() only if @x is not in .rodata section.492* See kunit_kstrdup_const() for more information.493*/494void kunit_kfree_const(struct kunit *test, const void *x);495496/**497* kunit_kstrdup() - Duplicates a string into a test managed allocation.498*499* @test: The test context object.500* @str: The NULL-terminated string to duplicate.501* @gfp: flags passed to underlying kmalloc().502*503* See kstrdup() and kunit_kmalloc_array() for more information.504*/505static inline char *kunit_kstrdup(struct kunit *test, const char *str, gfp_t gfp)506{507size_t len;508char *buf;509510if (!str)511return NULL;512513len = strlen(str) + 1;514buf = kunit_kmalloc(test, len, gfp);515if (buf)516memcpy(buf, str, len);517return buf;518}519520/**521* kunit_kstrdup_const() - Conditionally duplicates a string into a test managed allocation.522*523* @test: The test context object.524* @str: The NULL-terminated string to duplicate.525* @gfp: flags passed to underlying kmalloc().526*527* Calls kunit_kstrdup() only if @str is not in the rodata section. Must be freed with528* kunit_kfree_const() -- not kunit_kfree().529* See kstrdup_const() and kunit_kmalloc_array() for more information.530*/531const char *kunit_kstrdup_const(struct kunit *test, const char *str, gfp_t gfp);532533/**534* kunit_attach_mm() - Create and attach a new mm if it doesn't already exist.535*536* Allocates a &struct mm_struct and attaches it to @current. In most cases, call537* kunit_vm_mmap() without calling kunit_attach_mm() directly. Only necessary when538* code under test accesses the mm before executing the mmap (e.g., to perform539* additional initialization beforehand).540*541* Return: 0 on success, -errno on failure.542*/543int kunit_attach_mm(void);544545/**546* kunit_vm_mmap() - Allocate KUnit-tracked vm_mmap() area547* @test: The test context object.548* @file: struct file pointer to map from, if any549* @addr: desired address, if any550* @len: how many bytes to allocate551* @prot: mmap PROT_* bits552* @flag: mmap flags553* @offset: offset into @file to start mapping from.554*555* See vm_mmap() for more information.556*/557unsigned long kunit_vm_mmap(struct kunit *test, struct file *file,558unsigned long addr, unsigned long len,559unsigned long prot, unsigned long flag,560unsigned long offset);561562void kunit_cleanup(struct kunit *test);563564void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...);565566/**567* kunit_mark_skipped() - Marks @test as skipped568*569* @test: The test context object.570* @fmt: A printk() style format string.571*572* Marks the test as skipped. @fmt is given output as the test status573* comment, typically the reason the test was skipped.574*575* Test execution continues after kunit_mark_skipped() is called.576*/577#define kunit_mark_skipped(test, fmt, ...) \578do { \579WRITE_ONCE((test)->status, KUNIT_SKIPPED); \580scnprintf((test)->status_comment, \581KUNIT_STATUS_COMMENT_SIZE, \582fmt, ##__VA_ARGS__); \583} while (0)584585/**586* kunit_skip() - Marks @test as skipped587*588* @test: The test context object.589* @fmt: A printk() style format string.590*591* Skips the test. @fmt is given output as the test status592* comment, typically the reason the test was skipped.593*594* Test execution is halted after kunit_skip() is called.595*/596#define kunit_skip(test, fmt, ...) \597do { \598kunit_mark_skipped((test), fmt, ##__VA_ARGS__); \599kunit_try_catch_throw(&((test)->try_catch)); \600} while (0)601602/*603* printk and log to per-test or per-suite log buffer. Logging only done604* if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.605*/606#define kunit_log(lvl, test_or_suite, fmt, ...) \607do { \608printk(lvl fmt, ##__VA_ARGS__); \609kunit_log_append((test_or_suite)->log, fmt, \610##__VA_ARGS__); \611} while (0)612613#define kunit_printk(lvl, test, fmt, ...) \614kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \615(test)->name, ##__VA_ARGS__)616617/**618* kunit_info() - Prints an INFO level message associated with @test.619*620* @test: The test context object.621* @fmt: A printk() style format string.622*623* Prints an info level message associated with the test suite being run.624* Takes a variable number of format parameters just like printk().625*/626#define kunit_info(test, fmt, ...) \627kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)628629/**630* kunit_warn() - Prints a WARN level message associated with @test.631*632* @test: The test context object.633* @fmt: A printk() style format string.634*635* Prints a warning level message.636*/637#define kunit_warn(test, fmt, ...) \638kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)639640/**641* kunit_err() - Prints an ERROR level message associated with @test.642*643* @test: The test context object.644* @fmt: A printk() style format string.645*646* Prints an error level message.647*/648#define kunit_err(test, fmt, ...) \649kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)650651/*652* Must be called at the beginning of each KUNIT_*_ASSERTION().653* Cf. KUNIT_CURRENT_LOC.654*/655#define _KUNIT_SAVE_LOC(test) do { \656WRITE_ONCE(test->last_seen.file, __FILE__); \657WRITE_ONCE(test->last_seen.line, __LINE__); \658} while (0)659660/**661* KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.662* @test: The test context object.663*664* The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other665* words, it does nothing and only exists for code clarity. See666* KUNIT_EXPECT_TRUE() for more information.667*/668#define KUNIT_SUCCEED(test) _KUNIT_SAVE_LOC(test)669670void __noreturn __kunit_abort(struct kunit *test);671672void __printf(6, 7) __kunit_do_failed_assertion(struct kunit *test,673const struct kunit_loc *loc,674enum kunit_assert_type type,675const struct kunit_assert *assert,676assert_format_t assert_format,677const char *fmt, ...);678679#define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \680static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \681const struct assert_class __assertion = INITIALIZER; \682__kunit_do_failed_assertion(test, \683&__loc, \684assert_type, \685&__assertion.assert, \686assert_format, \687fmt, \688##__VA_ARGS__); \689if (assert_type == KUNIT_ASSERTION) \690__kunit_abort(test); \691} while (0)692693694#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) do { \695_KUNIT_SAVE_LOC(test); \696_KUNIT_FAILED(test, \697assert_type, \698kunit_fail_assert, \699kunit_fail_assert_format, \700{}, \701fmt, \702##__VA_ARGS__); \703} while (0)704705/**706* KUNIT_FAIL() - Always causes a test to fail when evaluated.707* @test: The test context object.708* @fmt: an informational message to be printed when the assertion is made.709* @...: string format arguments.710*711* The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In712* other words, it always results in a failed expectation, and consequently713* always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()714* for more information.715*/716#define KUNIT_FAIL(test, fmt, ...) \717KUNIT_FAIL_ASSERTION(test, \718KUNIT_EXPECTATION, \719fmt, \720##__VA_ARGS__)721722/* Helper to safely pass around an initializer list to other macros. */723#define KUNIT_INIT_ASSERT(initializers...) { initializers }724725#define KUNIT_UNARY_ASSERTION(test, \726assert_type, \727condition_, \728expected_true_, \729fmt, \730...) \731do { \732_KUNIT_SAVE_LOC(test); \733if (likely(!!(condition_) == !!expected_true_)) \734break; \735\736_KUNIT_FAILED(test, \737assert_type, \738kunit_unary_assert, \739kunit_unary_assert_format, \740KUNIT_INIT_ASSERT(.condition = #condition_, \741.expected_true = expected_true_), \742fmt, \743##__VA_ARGS__); \744} while (0)745746#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \747KUNIT_UNARY_ASSERTION(test, \748assert_type, \749condition, \750true, \751fmt, \752##__VA_ARGS__)753754#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \755KUNIT_UNARY_ASSERTION(test, \756assert_type, \757condition, \758false, \759fmt, \760##__VA_ARGS__)761762/*763* A factory macro for defining the assertions and expectations for the basic764* comparisons defined for the built in types.765*766* Unfortunately, there is no common type that all types can be promoted to for767* which all the binary operators behave the same way as for the actual types768* (for example, there is no type that long long and unsigned long long can769* both be cast to where the comparison result is preserved for all values). So770* the best we can do is do the comparison in the original types and then coerce771* everything to long long for printing; this way, the comparison behaves772* correctly and the printed out value usually makes sense without773* interpretation, but can always be interpreted to figure out the actual774* value.775*/776#define KUNIT_BASE_BINARY_ASSERTION(test, \777assert_class, \778format_func, \779assert_type, \780left, \781op, \782right, \783fmt, \784...) \785do { \786const typeof(left) __left = (left); \787const typeof(right) __right = (right); \788static const struct kunit_binary_assert_text __text = { \789.operation = #op, \790.left_text = #left, \791.right_text = #right, \792}; \793\794_KUNIT_SAVE_LOC(test); \795if (likely(__left op __right)) \796break; \797\798_KUNIT_FAILED(test, \799assert_type, \800assert_class, \801format_func, \802KUNIT_INIT_ASSERT(.text = &__text, \803.left_value = __left, \804.right_value = __right), \805fmt, \806##__VA_ARGS__); \807} while (0)808809#define KUNIT_BINARY_INT_ASSERTION(test, \810assert_type, \811left, \812op, \813right, \814fmt, \815...) \816KUNIT_BASE_BINARY_ASSERTION(test, \817kunit_binary_assert, \818kunit_binary_assert_format, \819assert_type, \820left, op, right, \821fmt, \822##__VA_ARGS__)823824#define KUNIT_BINARY_PTR_ASSERTION(test, \825assert_type, \826left, \827op, \828right, \829fmt, \830...) \831KUNIT_BASE_BINARY_ASSERTION(test, \832kunit_binary_ptr_assert, \833kunit_binary_ptr_assert_format, \834assert_type, \835left, op, right, \836fmt, \837##__VA_ARGS__)838839#define KUNIT_BINARY_STR_ASSERTION(test, \840assert_type, \841left, \842op, \843right, \844fmt, \845...) \846do { \847const char *__left = (left); \848const char *__right = (right); \849static const struct kunit_binary_assert_text __text = { \850.operation = #op, \851.left_text = #left, \852.right_text = #right, \853}; \854\855_KUNIT_SAVE_LOC(test); \856if (likely((__left) && (__right) && (strcmp(__left, __right) op 0))) \857break; \858\859\860_KUNIT_FAILED(test, \861assert_type, \862kunit_binary_str_assert, \863kunit_binary_str_assert_format, \864KUNIT_INIT_ASSERT(.text = &__text, \865.left_value = __left, \866.right_value = __right), \867fmt, \868##__VA_ARGS__); \869} while (0)870871#define KUNIT_MEM_ASSERTION(test, \872assert_type, \873left, \874op, \875right, \876size_, \877fmt, \878...) \879do { \880const void *__left = (left); \881const void *__right = (right); \882const size_t __size = (size_); \883static const struct kunit_binary_assert_text __text = { \884.operation = #op, \885.left_text = #left, \886.right_text = #right, \887}; \888\889_KUNIT_SAVE_LOC(test); \890if (likely(__left && __right)) \891if (likely(memcmp(__left, __right, __size) op 0)) \892break; \893\894_KUNIT_FAILED(test, \895assert_type, \896kunit_mem_assert, \897kunit_mem_assert_format, \898KUNIT_INIT_ASSERT(.text = &__text, \899.left_value = __left, \900.right_value = __right, \901.size = __size), \902fmt, \903##__VA_ARGS__); \904} while (0)905906#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \907assert_type, \908ptr, \909fmt, \910...) \911do { \912const typeof(ptr) __ptr = (ptr); \913\914_KUNIT_SAVE_LOC(test); \915if (!IS_ERR_OR_NULL(__ptr)) \916break; \917\918_KUNIT_FAILED(test, \919assert_type, \920kunit_ptr_not_err_assert, \921kunit_ptr_not_err_assert_format, \922KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \923fmt, \924##__VA_ARGS__); \925} while (0)926927/**928* KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.929* @test: The test context object.930* @condition: an arbitrary boolean expression. The test fails when this does931* not evaluate to true.932*933* This and expectations of the form `KUNIT_EXPECT_*` will cause the test case934* to fail when the specified condition is not met; however, it will not prevent935* the test case from continuing to run; this is otherwise known as an936* *expectation failure*.937*/938#define KUNIT_EXPECT_TRUE(test, condition) \939KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)940941#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \942KUNIT_TRUE_MSG_ASSERTION(test, \943KUNIT_EXPECTATION, \944condition, \945fmt, \946##__VA_ARGS__)947948/**949* KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.950* @test: The test context object.951* @condition: an arbitrary boolean expression. The test fails when this does952* not evaluate to false.953*954* Sets an expectation that @condition evaluates to false. See955* KUNIT_EXPECT_TRUE() for more information.956*/957#define KUNIT_EXPECT_FALSE(test, condition) \958KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)959960#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \961KUNIT_FALSE_MSG_ASSERTION(test, \962KUNIT_EXPECTATION, \963condition, \964fmt, \965##__VA_ARGS__)966967/**968* KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.969* @test: The test context object.970* @left: an arbitrary expression that evaluates to a primitive C type.971* @right: an arbitrary expression that evaluates to a primitive C type.972*973* Sets an expectation that the values that @left and @right evaluate to are974* equal. This is semantically equivalent to975* KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for976* more information.977*/978#define KUNIT_EXPECT_EQ(test, left, right) \979KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)980981#define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \982KUNIT_BINARY_INT_ASSERTION(test, \983KUNIT_EXPECTATION, \984left, ==, right, \985fmt, \986##__VA_ARGS__)987988/**989* KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.990* @test: The test context object.991* @left: an arbitrary expression that evaluates to a pointer.992* @right: an arbitrary expression that evaluates to a pointer.993*994* Sets an expectation that the values that @left and @right evaluate to are995* equal. This is semantically equivalent to996* KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for997* more information.998*/999#define KUNIT_EXPECT_PTR_EQ(test, left, right) \1000KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)10011002#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \1003KUNIT_BINARY_PTR_ASSERTION(test, \1004KUNIT_EXPECTATION, \1005left, ==, right, \1006fmt, \1007##__VA_ARGS__)10081009/**1010* KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.1011* @test: The test context object.1012* @left: an arbitrary expression that evaluates to a primitive C type.1013* @right: an arbitrary expression that evaluates to a primitive C type.1014*1015* Sets an expectation that the values that @left and @right evaluate to are not1016* equal. This is semantically equivalent to1017* KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for1018* more information.1019*/1020#define KUNIT_EXPECT_NE(test, left, right) \1021KUNIT_EXPECT_NE_MSG(test, left, right, NULL)10221023#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \1024KUNIT_BINARY_INT_ASSERTION(test, \1025KUNIT_EXPECTATION, \1026left, !=, right, \1027fmt, \1028##__VA_ARGS__)10291030/**1031* KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.1032* @test: The test context object.1033* @left: an arbitrary expression that evaluates to a pointer.1034* @right: an arbitrary expression that evaluates to a pointer.1035*1036* Sets an expectation that the values that @left and @right evaluate to are not1037* equal. This is semantically equivalent to1038* KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for1039* more information.1040*/1041#define KUNIT_EXPECT_PTR_NE(test, left, right) \1042KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)10431044#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \1045KUNIT_BINARY_PTR_ASSERTION(test, \1046KUNIT_EXPECTATION, \1047left, !=, right, \1048fmt, \1049##__VA_ARGS__)10501051/**1052* KUNIT_EXPECT_LT() - An expectation that @left is less than @right.1053* @test: The test context object.1054* @left: an arbitrary expression that evaluates to a primitive C type.1055* @right: an arbitrary expression that evaluates to a primitive C type.1056*1057* Sets an expectation that the value that @left evaluates to is less than the1058* value that @right evaluates to. This is semantically equivalent to1059* KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for1060* more information.1061*/1062#define KUNIT_EXPECT_LT(test, left, right) \1063KUNIT_EXPECT_LT_MSG(test, left, right, NULL)10641065#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \1066KUNIT_BINARY_INT_ASSERTION(test, \1067KUNIT_EXPECTATION, \1068left, <, right, \1069fmt, \1070##__VA_ARGS__)10711072/**1073* KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.1074* @test: The test context object.1075* @left: an arbitrary expression that evaluates to a primitive C type.1076* @right: an arbitrary expression that evaluates to a primitive C type.1077*1078* Sets an expectation that the value that @left evaluates to is less than or1079* equal to the value that @right evaluates to. Semantically this is equivalent1080* to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for1081* more information.1082*/1083#define KUNIT_EXPECT_LE(test, left, right) \1084KUNIT_EXPECT_LE_MSG(test, left, right, NULL)10851086#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \1087KUNIT_BINARY_INT_ASSERTION(test, \1088KUNIT_EXPECTATION, \1089left, <=, right, \1090fmt, \1091##__VA_ARGS__)10921093/**1094* KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.1095* @test: The test context object.1096* @left: an arbitrary expression that evaluates to a primitive C type.1097* @right: an arbitrary expression that evaluates to a primitive C type.1098*1099* Sets an expectation that the value that @left evaluates to is greater than1100* the value that @right evaluates to. This is semantically equivalent to1101* KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for1102* more information.1103*/1104#define KUNIT_EXPECT_GT(test, left, right) \1105KUNIT_EXPECT_GT_MSG(test, left, right, NULL)11061107#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \1108KUNIT_BINARY_INT_ASSERTION(test, \1109KUNIT_EXPECTATION, \1110left, >, right, \1111fmt, \1112##__VA_ARGS__)11131114/**1115* KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.1116* @test: The test context object.1117* @left: an arbitrary expression that evaluates to a primitive C type.1118* @right: an arbitrary expression that evaluates to a primitive C type.1119*1120* Sets an expectation that the value that @left evaluates to is greater than1121* the value that @right evaluates to. This is semantically equivalent to1122* KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for1123* more information.1124*/1125#define KUNIT_EXPECT_GE(test, left, right) \1126KUNIT_EXPECT_GE_MSG(test, left, right, NULL)11271128#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \1129KUNIT_BINARY_INT_ASSERTION(test, \1130KUNIT_EXPECTATION, \1131left, >=, right, \1132fmt, \1133##__VA_ARGS__)11341135/**1136* KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.1137* @test: The test context object.1138* @left: an arbitrary expression that evaluates to a null terminated string.1139* @right: an arbitrary expression that evaluates to a null terminated string.1140*1141* Sets an expectation that the values that @left and @right evaluate to are1142* equal. This is semantically equivalent to1143* KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()1144* for more information.1145*/1146#define KUNIT_EXPECT_STREQ(test, left, right) \1147KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)11481149#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \1150KUNIT_BINARY_STR_ASSERTION(test, \1151KUNIT_EXPECTATION, \1152left, ==, right, \1153fmt, \1154##__VA_ARGS__)11551156/**1157* KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.1158* @test: The test context object.1159* @left: an arbitrary expression that evaluates to a null terminated string.1160* @right: an arbitrary expression that evaluates to a null terminated string.1161*1162* Sets an expectation that the values that @left and @right evaluate to are1163* not equal. This is semantically equivalent to1164* KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()1165* for more information.1166*/1167#define KUNIT_EXPECT_STRNEQ(test, left, right) \1168KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)11691170#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \1171KUNIT_BINARY_STR_ASSERTION(test, \1172KUNIT_EXPECTATION, \1173left, !=, right, \1174fmt, \1175##__VA_ARGS__)11761177/**1178* KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.1179* @test: The test context object.1180* @left: An arbitrary expression that evaluates to the specified size.1181* @right: An arbitrary expression that evaluates to the specified size.1182* @size: Number of bytes compared.1183*1184* Sets an expectation that the values that @left and @right evaluate to are1185* equal. This is semantically equivalent to1186* KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See1187* KUNIT_EXPECT_TRUE() for more information.1188*1189* Although this expectation works for any memory block, it is not recommended1190* for comparing more structured data, such as structs. This expectation is1191* recommended for comparing, for example, data arrays.1192*/1193#define KUNIT_EXPECT_MEMEQ(test, left, right, size) \1194KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)11951196#define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \1197KUNIT_MEM_ASSERTION(test, \1198KUNIT_EXPECTATION, \1199left, ==, right, \1200size, \1201fmt, \1202##__VA_ARGS__)12031204/**1205* KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.1206* @test: The test context object.1207* @left: An arbitrary expression that evaluates to the specified size.1208* @right: An arbitrary expression that evaluates to the specified size.1209* @size: Number of bytes compared.1210*1211* Sets an expectation that the values that @left and @right evaluate to are1212* not equal. This is semantically equivalent to1213* KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See1214* KUNIT_EXPECT_TRUE() for more information.1215*1216* Although this expectation works for any memory block, it is not recommended1217* for comparing more structured data, such as structs. This expectation is1218* recommended for comparing, for example, data arrays.1219*/1220#define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \1221KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)12221223#define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \1224KUNIT_MEM_ASSERTION(test, \1225KUNIT_EXPECTATION, \1226left, !=, right, \1227size, \1228fmt, \1229##__VA_ARGS__)12301231/**1232* KUNIT_EXPECT_NULL() - Expects that @ptr is null.1233* @test: The test context object.1234* @ptr: an arbitrary pointer.1235*1236* Sets an expectation that the value that @ptr evaluates to is null. This is1237* semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).1238* See KUNIT_EXPECT_TRUE() for more information.1239*/1240#define KUNIT_EXPECT_NULL(test, ptr) \1241KUNIT_EXPECT_NULL_MSG(test, \1242ptr, \1243NULL)12441245#define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \1246KUNIT_BINARY_PTR_ASSERTION(test, \1247KUNIT_EXPECTATION, \1248ptr, ==, NULL, \1249fmt, \1250##__VA_ARGS__)12511252/**1253* KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.1254* @test: The test context object.1255* @ptr: an arbitrary pointer.1256*1257* Sets an expectation that the value that @ptr evaluates to is not null. This1258* is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).1259* See KUNIT_EXPECT_TRUE() for more information.1260*/1261#define KUNIT_EXPECT_NOT_NULL(test, ptr) \1262KUNIT_EXPECT_NOT_NULL_MSG(test, \1263ptr, \1264NULL)12651266#define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \1267KUNIT_BINARY_PTR_ASSERTION(test, \1268KUNIT_EXPECTATION, \1269ptr, !=, NULL, \1270fmt, \1271##__VA_ARGS__)12721273/**1274* KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.1275* @test: The test context object.1276* @ptr: an arbitrary pointer.1277*1278* Sets an expectation that the value that @ptr evaluates to is not null and not1279* an errno stored in a pointer. This is semantically equivalent to1280* KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for1281* more information.1282*/1283#define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \1284KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)12851286#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \1287KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \1288KUNIT_EXPECTATION, \1289ptr, \1290fmt, \1291##__VA_ARGS__)12921293/**1294* KUNIT_FAIL_AND_ABORT() - Always causes a test to fail and abort when evaluated.1295* @test: The test context object.1296* @fmt: an informational message to be printed when the assertion is made.1297* @...: string format arguments.1298*1299* The opposite of KUNIT_SUCCEED(), it is an assertion that always fails. In1300* other words, it always results in a failed assertion, and consequently1301* always causes the test case to fail and abort when evaluated.1302* See KUNIT_ASSERT_TRUE() for more information.1303*/1304#define KUNIT_FAIL_AND_ABORT(test, fmt, ...) \1305KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)13061307/**1308* KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.1309* @test: The test context object.1310* @condition: an arbitrary boolean expression. The test fails and aborts when1311* this does not evaluate to true.1312*1313* This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to1314* fail *and immediately abort* when the specified condition is not met. Unlike1315* an expectation failure, it will prevent the test case from continuing to run;1316* this is otherwise known as an *assertion failure*.1317*/1318#define KUNIT_ASSERT_TRUE(test, condition) \1319KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)13201321#define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \1322KUNIT_TRUE_MSG_ASSERTION(test, \1323KUNIT_ASSERTION, \1324condition, \1325fmt, \1326##__VA_ARGS__)13271328/**1329* KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.1330* @test: The test context object.1331* @condition: an arbitrary boolean expression.1332*1333* Sets an assertion that the value that @condition evaluates to is false. This1334* is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure1335* (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1336*/1337#define KUNIT_ASSERT_FALSE(test, condition) \1338KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)13391340#define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \1341KUNIT_FALSE_MSG_ASSERTION(test, \1342KUNIT_ASSERTION, \1343condition, \1344fmt, \1345##__VA_ARGS__)13461347/**1348* KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.1349* @test: The test context object.1350* @left: an arbitrary expression that evaluates to a primitive C type.1351* @right: an arbitrary expression that evaluates to a primitive C type.1352*1353* Sets an assertion that the values that @left and @right evaluate to are1354* equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion1355* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1356*/1357#define KUNIT_ASSERT_EQ(test, left, right) \1358KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)13591360#define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \1361KUNIT_BINARY_INT_ASSERTION(test, \1362KUNIT_ASSERTION, \1363left, ==, right, \1364fmt, \1365##__VA_ARGS__)13661367/**1368* KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.1369* @test: The test context object.1370* @left: an arbitrary expression that evaluates to a pointer.1371* @right: an arbitrary expression that evaluates to a pointer.1372*1373* Sets an assertion that the values that @left and @right evaluate to are1374* equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion1375* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1376*/1377#define KUNIT_ASSERT_PTR_EQ(test, left, right) \1378KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)13791380#define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \1381KUNIT_BINARY_PTR_ASSERTION(test, \1382KUNIT_ASSERTION, \1383left, ==, right, \1384fmt, \1385##__VA_ARGS__)13861387/**1388* KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.1389* @test: The test context object.1390* @left: an arbitrary expression that evaluates to a primitive C type.1391* @right: an arbitrary expression that evaluates to a primitive C type.1392*1393* Sets an assertion that the values that @left and @right evaluate to are not1394* equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion1395* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1396*/1397#define KUNIT_ASSERT_NE(test, left, right) \1398KUNIT_ASSERT_NE_MSG(test, left, right, NULL)13991400#define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \1401KUNIT_BINARY_INT_ASSERTION(test, \1402KUNIT_ASSERTION, \1403left, !=, right, \1404fmt, \1405##__VA_ARGS__)14061407/**1408* KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.1409* KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.1410* @test: The test context object.1411* @left: an arbitrary expression that evaluates to a pointer.1412* @right: an arbitrary expression that evaluates to a pointer.1413*1414* Sets an assertion that the values that @left and @right evaluate to are not1415* equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion1416* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1417*/1418#define KUNIT_ASSERT_PTR_NE(test, left, right) \1419KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)14201421#define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \1422KUNIT_BINARY_PTR_ASSERTION(test, \1423KUNIT_ASSERTION, \1424left, !=, right, \1425fmt, \1426##__VA_ARGS__)1427/**1428* KUNIT_ASSERT_LT() - An assertion that @left is less than @right.1429* @test: The test context object.1430* @left: an arbitrary expression that evaluates to a primitive C type.1431* @right: an arbitrary expression that evaluates to a primitive C type.1432*1433* Sets an assertion that the value that @left evaluates to is less than the1434* value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except1435* it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion1436* is not met.1437*/1438#define KUNIT_ASSERT_LT(test, left, right) \1439KUNIT_ASSERT_LT_MSG(test, left, right, NULL)14401441#define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \1442KUNIT_BINARY_INT_ASSERTION(test, \1443KUNIT_ASSERTION, \1444left, <, right, \1445fmt, \1446##__VA_ARGS__)1447/**1448* KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.1449* @test: The test context object.1450* @left: an arbitrary expression that evaluates to a primitive C type.1451* @right: an arbitrary expression that evaluates to a primitive C type.1452*1453* Sets an assertion that the value that @left evaluates to is less than or1454* equal to the value that @right evaluates to. This is the same as1455* KUNIT_EXPECT_LE(), except it causes an assertion failure (see1456* KUNIT_ASSERT_TRUE()) when the assertion is not met.1457*/1458#define KUNIT_ASSERT_LE(test, left, right) \1459KUNIT_ASSERT_LE_MSG(test, left, right, NULL)14601461#define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \1462KUNIT_BINARY_INT_ASSERTION(test, \1463KUNIT_ASSERTION, \1464left, <=, right, \1465fmt, \1466##__VA_ARGS__)14671468/**1469* KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.1470* @test: The test context object.1471* @left: an arbitrary expression that evaluates to a primitive C type.1472* @right: an arbitrary expression that evaluates to a primitive C type.1473*1474* Sets an assertion that the value that @left evaluates to is greater than the1475* value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except1476* it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion1477* is not met.1478*/1479#define KUNIT_ASSERT_GT(test, left, right) \1480KUNIT_ASSERT_GT_MSG(test, left, right, NULL)14811482#define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \1483KUNIT_BINARY_INT_ASSERTION(test, \1484KUNIT_ASSERTION, \1485left, >, right, \1486fmt, \1487##__VA_ARGS__)14881489/**1490* KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.1491* @test: The test context object.1492* @left: an arbitrary expression that evaluates to a primitive C type.1493* @right: an arbitrary expression that evaluates to a primitive C type.1494*1495* Sets an assertion that the value that @left evaluates to is greater than the1496* value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except1497* it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion1498* is not met.1499*/1500#define KUNIT_ASSERT_GE(test, left, right) \1501KUNIT_ASSERT_GE_MSG(test, left, right, NULL)15021503#define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \1504KUNIT_BINARY_INT_ASSERTION(test, \1505KUNIT_ASSERTION, \1506left, >=, right, \1507fmt, \1508##__VA_ARGS__)15091510/**1511* KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.1512* @test: The test context object.1513* @left: an arbitrary expression that evaluates to a null terminated string.1514* @right: an arbitrary expression that evaluates to a null terminated string.1515*1516* Sets an assertion that the values that @left and @right evaluate to are1517* equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an1518* assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1519*/1520#define KUNIT_ASSERT_STREQ(test, left, right) \1521KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)15221523#define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \1524KUNIT_BINARY_STR_ASSERTION(test, \1525KUNIT_ASSERTION, \1526left, ==, right, \1527fmt, \1528##__VA_ARGS__)15291530/**1531* KUNIT_ASSERT_STRNEQ() - An assertion that strings @left and @right are not equal.1532* @test: The test context object.1533* @left: an arbitrary expression that evaluates to a null terminated string.1534* @right: an arbitrary expression that evaluates to a null terminated string.1535*1536* Sets an assertion that the values that @left and @right evaluate to are1537* not equal. This is semantically equivalent to1538* KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()1539* for more information.1540*/1541#define KUNIT_ASSERT_STRNEQ(test, left, right) \1542KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)15431544#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \1545KUNIT_BINARY_STR_ASSERTION(test, \1546KUNIT_ASSERTION, \1547left, !=, right, \1548fmt, \1549##__VA_ARGS__)15501551/**1552* KUNIT_ASSERT_MEMEQ() - Asserts that the first @size bytes of @left and @right are equal.1553* @test: The test context object.1554* @left: An arbitrary expression that evaluates to the specified size.1555* @right: An arbitrary expression that evaluates to the specified size.1556* @size: Number of bytes compared.1557*1558* Sets an assertion that the values that @left and @right evaluate to are1559* equal. This is semantically equivalent to1560* KUNIT_ASSERT_TRUE(@test, !memcmp((@left), (@right), (@size))). See1561* KUNIT_ASSERT_TRUE() for more information.1562*1563* Although this assertion works for any memory block, it is not recommended1564* for comparing more structured data, such as structs. This assertion is1565* recommended for comparing, for example, data arrays.1566*/1567#define KUNIT_ASSERT_MEMEQ(test, left, right, size) \1568KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, NULL)15691570#define KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, fmt, ...) \1571KUNIT_MEM_ASSERTION(test, \1572KUNIT_ASSERTION, \1573left, ==, right, \1574size, \1575fmt, \1576##__VA_ARGS__)15771578/**1579* KUNIT_ASSERT_MEMNEQ() - Asserts that the first @size bytes of @left and @right are not equal.1580* @test: The test context object.1581* @left: An arbitrary expression that evaluates to the specified size.1582* @right: An arbitrary expression that evaluates to the specified size.1583* @size: Number of bytes compared.1584*1585* Sets an assertion that the values that @left and @right evaluate to are1586* not equal. This is semantically equivalent to1587* KUNIT_ASSERT_TRUE(@test, memcmp((@left), (@right), (@size))). See1588* KUNIT_ASSERT_TRUE() for more information.1589*1590* Although this assertion works for any memory block, it is not recommended1591* for comparing more structured data, such as structs. This assertion is1592* recommended for comparing, for example, data arrays.1593*/1594#define KUNIT_ASSERT_MEMNEQ(test, left, right, size) \1595KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, NULL)15961597#define KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \1598KUNIT_MEM_ASSERTION(test, \1599KUNIT_ASSERTION, \1600left, !=, right, \1601size, \1602fmt, \1603##__VA_ARGS__)16041605/**1606* KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.1607* @test: The test context object.1608* @ptr: an arbitrary pointer.1609*1610* Sets an assertion that the values that @ptr evaluates to is null. This is1611* the same as KUNIT_EXPECT_NULL(), except it causes an assertion1612* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1613*/1614#define KUNIT_ASSERT_NULL(test, ptr) \1615KUNIT_ASSERT_NULL_MSG(test, \1616ptr, \1617NULL)16181619#define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \1620KUNIT_BINARY_PTR_ASSERTION(test, \1621KUNIT_ASSERTION, \1622ptr, ==, NULL, \1623fmt, \1624##__VA_ARGS__)16251626/**1627* KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.1628* @test: The test context object.1629* @ptr: an arbitrary pointer.1630*1631* Sets an assertion that the values that @ptr evaluates to is not null. This1632* is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion1633* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1634*/1635#define KUNIT_ASSERT_NOT_NULL(test, ptr) \1636KUNIT_ASSERT_NOT_NULL_MSG(test, \1637ptr, \1638NULL)16391640#define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \1641KUNIT_BINARY_PTR_ASSERTION(test, \1642KUNIT_ASSERTION, \1643ptr, !=, NULL, \1644fmt, \1645##__VA_ARGS__)16461647/**1648* KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.1649* @test: The test context object.1650* @ptr: an arbitrary pointer.1651*1652* Sets an assertion that the value that @ptr evaluates to is not null and not1653* an errno stored in a pointer. This is the same as1654* KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see1655* KUNIT_ASSERT_TRUE()) when the assertion is not met.1656*/1657#define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \1658KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)16591660#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \1661KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \1662KUNIT_ASSERTION, \1663ptr, \1664fmt, \1665##__VA_ARGS__)16661667/**1668* KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.1669* @name: prefix for the test parameter generator function.1670* @array: array of test parameters.1671* @get_desc: function to convert param to description; NULL to use default1672*1673* Define function @name_gen_params which uses @array to generate parameters.1674*/1675#define KUNIT_ARRAY_PARAM(name, array, get_desc) \1676static const void *name##_gen_params(const void *prev, char *desc) \1677{ \1678typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \1679if (__next - (array) < ARRAY_SIZE((array))) { \1680void (*__get_desc)(typeof(__next), char *) = get_desc; \1681if (__get_desc) \1682__get_desc(__next, desc); \1683return __next; \1684} \1685return NULL; \1686}16871688/**1689* KUNIT_ARRAY_PARAM_DESC() - Define test parameter generator from an array.1690* @name: prefix for the test parameter generator function.1691* @array: array of test parameters.1692* @desc_member: structure member from array element to use as description1693*1694* Define function @name_gen_params which uses @array to generate parameters.1695*/1696#define KUNIT_ARRAY_PARAM_DESC(name, array, desc_member) \1697static const void *name##_gen_params(const void *prev, char *desc) \1698{ \1699typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \1700if (__next - (array) < ARRAY_SIZE((array))) { \1701strscpy(desc, __next->desc_member, KUNIT_PARAM_DESC_SIZE); \1702return __next; \1703} \1704return NULL; \1705}17061707// TODO([email protected]): consider eventually migrating users to explicitly1708// include resource.h themselves if they need it.1709#include <kunit/resource.h>17101711#endif /* _KUNIT_TEST_H */171217131714