Test API

This file documents all of the standard testing API.

enumkunit_status

Type of result for a test or test suite

Constants

KUNIT_SUCCESS

Denotes the test suite has not failed nor been skipped

KUNIT_FAILURE

Denotes the test has failed.

KUNIT_SKIPPED

Denotes the test has been skipped.

structkunit_case

represents an individual test case.

Definition:

struct kunit_case {    void (*run_case)(struct kunit *test);    const char *name;    const void* (*generate_params)(struct kunit *test, const void *prev, char *desc);    struct kunit_attributes attr;    int (*param_init)(struct kunit *test);    void (*param_exit)(struct kunit *test);};

Members

run_case

the function representing the actual test case.

name

the name of the test case.

generate_params

the generator function for parameterized tests.

attr

the attributes associated with the test

param_init

The init function to run before a parameterized test.

param_exit

The exit function to run after a parameterized test.

Description

A test case is a function with the signature,void(*)(structkunit*)that makes expectations and assertions (seeKUNIT_EXPECT_TRUE() andKUNIT_ASSERT_TRUE()) about code under test. Each test case is associatedwith astructkunit_suite and will be run after the suite’s initfunction and followed by the suite’s exit function.

A test case should be static and should only be created with theKUNIT_CASE() macro; additionally, every array of test cases should beterminated with an empty test case.

Example

voidadd_test_basic(structkunit*test){KUNIT_EXPECT_EQ(test,1,add(1,0));KUNIT_EXPECT_EQ(test,2,add(1,1));KUNIT_EXPECT_EQ(test,0,add(-1,1));KUNIT_EXPECT_EQ(test,INT_MAX,add(0,INT_MAX));KUNIT_EXPECT_EQ(test,-1,add(INT_MAX,INT_MIN));}staticstructkunit_caseexample_test_cases[]={KUNIT_CASE(add_test_basic),{}};
KUNIT_CASE

KUNIT_CASE(test_name)

A helper for creating astructkunit_case

Parameters

test_name

a reference to a test case function.

Description

Takes a symbol for a function representing a test case and creates astructkunit_case object from it. See the documentation forstructkunit_case for an example on how to use it.

KUNIT_CASE_ATTR

KUNIT_CASE_ATTR(test_name,attributes)

A helper for creating astructkunit_case with attributes

Parameters

test_name

a reference to a test case function.

attributes

a reference to astructkunit_attributes object containingtest attributes

KUNIT_CASE_SLOW

KUNIT_CASE_SLOW(test_name)

A helper for creating astructkunit_case with the slow attribute

Parameters

test_name

a reference to a test case function.

KUNIT_CASE_PARAM

KUNIT_CASE_PARAM(test_name,gen_params)

A helper for creation a parameterizedstructkunit_case

Parameters

test_name

a reference to a test case function.

gen_params

a reference to a parameter generator function.

Description

The generator function:

const void* gen_params(const void *prev, char *desc)

is used to lazily generate a series of arbitrarily typed values that fit intoa void*. The argumentprev is the previously returned value, which should beused to derive the next value;prev is set to NULL on the initial generatorcall. When no more values are available, the generator must return NULL.Optionally write a string intodesc (size of KUNIT_PARAM_DESC_SIZE)describing the parameter.

KUNIT_CASE_PARAM_ATTR

KUNIT_CASE_PARAM_ATTR(test_name,gen_params,attributes)

A helper for creating a parameterizedstructkunit_case with attributes

Parameters

test_name

a reference to a test case function.

gen_params

a reference to a parameter generator function.

attributes

a reference to astructkunit_attributes object containingtest attributes

KUNIT_CASE_PARAM_WITH_INIT

KUNIT_CASE_PARAM_WITH_INIT(test_name,gen_params,init,exit)

Define a parameterized KUnit test case with customparam_init() andparam_exit() functions.

Parameters

test_name

The function implementing the test case.

gen_params

The function to generate parameters for the test case.

init

A reference to theparam_init() function to run before a parameterized test.

exit

A reference to theparam_exit() function to run after a parameterized test.

Description

Provides the option to registerparam_init() andparam_exit() functions.param_init/exit will be passed the parameterized test context and run oncebefore and once after the parameterized test. The init function can be usedto add resources to share between parameter runs, pass parameter arrays,and any other setup logic. The exit function can be used to clean up resourcesthat were not managed by the parameterized test, and any other teardown logic.

Note

If you are registering a parameter array inparam_init() withkunit_register_param_array() then you need to passkunit_array_gen_params()to this as the generator function.

structkunit_suite

describes a related collection ofstructkunit_case

Definition:

struct kunit_suite {    const char name[256];    int (*suite_init)(struct kunit_suite *suite);    void (*suite_exit)(struct kunit_suite *suite);    int (*init)(struct kunit *test);    void (*exit)(struct kunit *test);    struct kunit_case *test_cases;    struct kunit_attributes attr;};

Members

name

the name of the test. Purely informational.

suite_init

called once per test suite before the test cases.

suite_exit

called once per test suite after all test cases.

init

called before every test case.

exit

called after every test case.

test_cases

a null terminated array of test cases.

attr

the attributes associated with the test suite

Description

A kunit_suite is a collection of relatedstructkunit_case s, such thatinit is called before every test case andexit is called after everytest case, similar to the notion of atest fixture or atest classin other unit testing frameworks like JUnit or Googletest.

Note thatexit andsuite_exit will run even ifinit orsuite_initfail: make sure they can handle any inconsistent state which may result.

Everystructkunit_case must be associated with a kunit_suite for KUnitto run it.

structkunit

represents a running instance of a test.

Definition:

struct kunit {    void *priv;    struct kunit *parent;    struct kunit_params params_array;};

Members

priv

for user to store arbitrary data. Commonly used to pass datacreated in the init function (seestructkunit_suite).

parent

reference to the parent context of typestructkunit that canbe used for storing shared resources.

params_array

for storing the parameter array.

Description

Used to store information about the current context under which the testis running. Most of this data is private and should only be accessedindirectly via public functions; the exceptions arepriv,parent andparams_array which can be used by the test writer to store arbitrary data,access the parent context, and to store the parameter array, respectively.

kunit_test_suites

kunit_test_suites(__suites...)

used to register one or morestructkunit_suite with KUnit.

Parameters

__suites...

a statically allocated list ofstructkunit_suite.

Description

Registerssuites with the test framework.This is done by placing the array ofstructkunit_suite * in the.kunit_test_suites ELF section.

When builtin, KUnit tests are all run via the executor at boot, and whenbuilt as a module, they run on module load.

kunit_test_init_section_suites

kunit_test_init_section_suites(__suites...)

used to register one or morestructkunit_suite containing init functions or init data.

Parameters

__suites...

a statically allocated list ofstructkunit_suite.

Description

This functions similar tokunit_test_suites() except that it compiles thelist of suites during init phase.

This macro also suffixes the array and suite declarations it makes with_probe; so that modpost suppresses warnings about referencing init datafor symbols named in this manner.

Note

these init tests are not able to be run after boot so there is no“run” debugfs file generated for these tests.

Also, do not mark the suite or test case structs with __initdata becausethey will be used after the init phase with debugfs.

void*kunit_kmalloc_array(structkunit*test,size_tn,size_tsize,gfp_tgfp)

Likekmalloc_array() except the allocation istest managed.

Parameters

structkunit*test

The test context object.

size_tn

number of elements.

size_tsize

The size in bytes of the desired memory.

gfp_tgfp

flags passed to underlyingkmalloc().

Description

Just likekmalloc_array(...), except the allocation is managed by the test caseand is automatically cleaned up after the test case concludes. Seekunit_add_action()for more information.

Note that some internal context data is also allocated with GFP_KERNEL,regardless of the gfp passed in.

void*kunit_kmalloc(structkunit*test,size_tsize,gfp_tgfp)

Likekmalloc() except the allocation istest managed.

Parameters

structkunit*test

The test context object.

size_tsize

The size in bytes of the desired memory.

gfp_tgfp

flags passed to underlyingkmalloc().

Description

Seekmalloc() andkunit_kmalloc_array() for more information.

Note that some internal context data is also allocated with GFP_KERNEL,regardless of the gfp passed in.

voidkunit_kfree(structkunit*test,constvoid*ptr)

Like kfree except for allocations managed by KUnit.

Parameters

structkunit*test

The test case to which the resource belongs.

constvoid*ptr

The memory allocation to free.

void*kunit_kzalloc(structkunit*test,size_tsize,gfp_tgfp)

Just likekunit_kmalloc(), but zeroes the allocation.

Parameters

structkunit*test

The test context object.

size_tsize

The size in bytes of the desired memory.

gfp_tgfp

flags passed to underlyingkmalloc().

Description

Seekzalloc() andkunit_kmalloc_array() for more information.

void*kunit_kcalloc(structkunit*test,size_tn,size_tsize,gfp_tgfp)

Just likekunit_kmalloc_array(), but zeroes the allocation.

Parameters

structkunit*test

The test context object.

size_tn

number of elements.

size_tsize

The size in bytes of the desired memory.

gfp_tgfp

flags passed to underlyingkmalloc().

Description

Seekcalloc() andkunit_kmalloc_array() for more information.

voidkunit_kfree_const(structkunit*test,constvoid*x)

conditionally free test managed memory

Parameters

structkunit*test

The test context object.

constvoid*x

pointer to the memory

Description

Callskunit_kfree() only ifx is not in .rodata section.Seekunit_kstrdup_const() for more information.

char*kunit_kstrdup(structkunit*test,constchar*str,gfp_tgfp)

Duplicates a string into a test managed allocation.

Parameters

structkunit*test

The test context object.

constchar*str

The NULL-terminated string to duplicate.

gfp_tgfp

flags passed to underlyingkmalloc().

Description

Seekstrdup() andkunit_kmalloc_array() for more information.

constchar*kunit_kstrdup_const(structkunit*test,constchar*str,gfp_tgfp)

Conditionally duplicates a string into a test managed allocation.

Parameters

structkunit*test

The test context object.

constchar*str

The NULL-terminated string to duplicate.

gfp_tgfp

flags passed to underlyingkmalloc().

Description

Callskunit_kstrdup() only ifstr is not in the rodata section. Must be freed withkunit_kfree_const() -- notkunit_kfree().Seekstrdup_const() andkunit_kmalloc_array() for more information.

intkunit_attach_mm(void)

Create and attach a new mm if it doesn’t already exist.

Parameters

void

no arguments

Description

Allocates astructmm_struct and attaches it tocurrent. In most cases, callkunit_vm_mmap() without callingkunit_attach_mm() directly. Only necessary whencode under test accesses the mm before executing the mmap (e.g., to performadditional initialization beforehand).

Return

0 on success, -errno on failure.

unsignedlongkunit_vm_mmap(structkunit*test,structfile*file,unsignedlongaddr,unsignedlonglen,unsignedlongprot,unsignedlongflag,unsignedlongoffset)

Allocate KUnit-trackedvm_mmap() area

Parameters

structkunit*test

The test context object.

structfile*file

structfile pointer to map from, if any

unsignedlongaddr

desired address, if any

unsignedlonglen

how many bytes to allocate

unsignedlongprot

mmap PROT_* bits

unsignedlongflag

mmap flags

unsignedlongoffset

offset intofile to start mapping from.

Description

Seevm_mmap() for more information.

kunit_mark_skipped

kunit_mark_skipped(test,fmt,...)

Markstest as skipped

Parameters

test

The test context object.

fmt

Aprintk() style format string.

...

variable arguments

Description

Marks the test as skipped.fmt is given output as the test statuscomment, typically the reason the test was skipped.

Test execution continues afterkunit_mark_skipped() is called.

kunit_skip

kunit_skip(test,fmt,...)

Markstest as skipped

Parameters

test

The test context object.

fmt

Aprintk() style format string.

...

variable arguments

Description

Skips the test.fmt is given output as the test statuscomment, typically the reason the test was skipped.

Test execution is halted afterkunit_skip() is called.

kunit_info

kunit_info(test,fmt,...)

Prints an INFO level message associated withtest.

Parameters

test

The test context object.

fmt

Aprintk() style format string.

...

variable arguments

Description

Prints an info level message associated with the test suite being run.Takes a variable number of format parameters just likeprintk().

kunit_warn

kunit_warn(test,fmt,...)

Prints a WARN level message associated withtest.

Parameters

test

The test context object.

fmt

Aprintk() style format string.

...

variable arguments

Description

Prints a warning level message.

kunit_err

kunit_err(test,fmt,...)

Prints an ERROR level message associated withtest.

Parameters

test

The test context object.

fmt

Aprintk() style format string.

...

variable arguments

Description

Prints an error level message.

KUNIT_SUCCEED

KUNIT_SUCCEED(test)

A no-op expectation. Only exists for code clarity.

Parameters

test

The test context object.

Description

The opposite ofKUNIT_FAIL(), it is an expectation that cannot fail. In otherwords, it does nothing and only exists for code clarity. SeeKUNIT_EXPECT_TRUE() for more information.

KUNIT_FAIL

KUNIT_FAIL(test,fmt,...)

Always causes a test to fail when evaluated.

Parameters

test

The test context object.

fmt

an informational message to be printed when the assertion is made.

...

string format arguments.

Description

The opposite ofKUNIT_SUCCEED(), it is an expectation that always fails. Inother words, it always results in a failed expectation, and consequentlyalways causes the test case to fail when evaluated. SeeKUNIT_EXPECT_TRUE()for more information.

KUNIT_EXPECT_TRUE

KUNIT_EXPECT_TRUE(test,condition)

Causes a test failure when the expression is not true.

Parameters

test

The test context object.

condition

an arbitrary boolean expression. The test fails when this doesnot evaluate to true.

Description

This and expectations of the formKUNIT_EXPECT_* will cause the test caseto fail when the specified condition is not met; however, it will not preventthe test case from continuing to run; this is otherwise known as anexpectation failure.

KUNIT_EXPECT_FALSE

KUNIT_EXPECT_FALSE(test,condition)

Makes a test failure when the expression is not false.

Parameters

test

The test context object.

condition

an arbitrary boolean expression. The test fails when this doesnot evaluate to false.

Description

Sets an expectation thatcondition evaluates to false. SeeKUNIT_EXPECT_TRUE() for more information.

KUNIT_EXPECT_EQ

KUNIT_EXPECT_EQ(test,left,right)

Sets an expectation thatleft andright are equal.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a primitive C type.

right

an arbitrary expression that evaluates to a primitive C type.

Description

Sets an expectation that the values thatleft andright evaluate to areequal. This is semantically equivalent toKUNIT_EXPECT_TRUE(test, (left) == (right)). SeeKUNIT_EXPECT_TRUE() formore information.

KUNIT_EXPECT_PTR_EQ

KUNIT_EXPECT_PTR_EQ(test,left,right)

Expects that pointersleft andright are equal.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a pointer.

right

an arbitrary expression that evaluates to a pointer.

Description

Sets an expectation that the values thatleft andright evaluate to areequal. This is semantically equivalent toKUNIT_EXPECT_TRUE(test, (left) == (right)). SeeKUNIT_EXPECT_TRUE() formore information.

KUNIT_EXPECT_NE

KUNIT_EXPECT_NE(test,left,right)

An expectation thatleft andright are not equal.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a primitive C type.

right

an arbitrary expression that evaluates to a primitive C type.

Description

Sets an expectation that the values thatleft andright evaluate to are notequal. This is semantically equivalent toKUNIT_EXPECT_TRUE(test, (left) != (right)). SeeKUNIT_EXPECT_TRUE() formore information.

KUNIT_EXPECT_PTR_NE

KUNIT_EXPECT_PTR_NE(test,left,right)

Expects that pointersleft andright are not equal.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a pointer.

right

an arbitrary expression that evaluates to a pointer.

Description

Sets an expectation that the values thatleft andright evaluate to are notequal. This is semantically equivalent toKUNIT_EXPECT_TRUE(test, (left) != (right)). SeeKUNIT_EXPECT_TRUE() formore information.

KUNIT_EXPECT_LT

KUNIT_EXPECT_LT(test,left,right)

An expectation thatleft is less thanright.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a primitive C type.

right

an arbitrary expression that evaluates to a primitive C type.

Description

Sets an expectation that the value thatleft evaluates to is less than thevalue thatright evaluates to. This is semantically equivalent toKUNIT_EXPECT_TRUE(test, (left) < (right)). SeeKUNIT_EXPECT_TRUE() formore information.

KUNIT_EXPECT_LE

KUNIT_EXPECT_LE(test,left,right)

Expects thatleft is less than or equal toright.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a primitive C type.

right

an arbitrary expression that evaluates to a primitive C type.

Description

Sets an expectation that the value thatleft evaluates to is less than orequal to the value thatright evaluates to. Semantically this is equivalentto KUNIT_EXPECT_TRUE(test, (left) <= (right)). SeeKUNIT_EXPECT_TRUE() formore information.

KUNIT_EXPECT_GT

KUNIT_EXPECT_GT(test,left,right)

An expectation thatleft is greater thanright.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a primitive C type.

right

an arbitrary expression that evaluates to a primitive C type.

Description

Sets an expectation that the value thatleft evaluates to is greater thanthe value thatright evaluates to. This is semantically equivalent toKUNIT_EXPECT_TRUE(test, (left) > (right)). SeeKUNIT_EXPECT_TRUE() formore information.

KUNIT_EXPECT_GE

KUNIT_EXPECT_GE(test,left,right)

Expects thatleft is greater than or equal toright.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a primitive C type.

right

an arbitrary expression that evaluates to a primitive C type.

Description

Sets an expectation that the value thatleft evaluates to is greater thanthe value thatright evaluates to. This is semantically equivalent toKUNIT_EXPECT_TRUE(test, (left) >= (right)). SeeKUNIT_EXPECT_TRUE() formore information.

KUNIT_EXPECT_STREQ

KUNIT_EXPECT_STREQ(test,left,right)

Expects that stringsleft andright are equal.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a null terminated string.

right

an arbitrary expression that evaluates to a null terminated string.

Description

Sets an expectation that the values thatleft andright evaluate to areequal. This is semantically equivalent toKUNIT_EXPECT_TRUE(test, !strcmp((left), (right))). SeeKUNIT_EXPECT_TRUE()for more information.

KUNIT_EXPECT_STRNEQ

KUNIT_EXPECT_STRNEQ(test,left,right)

Expects that stringsleft andright are not equal.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a null terminated string.

right

an arbitrary expression that evaluates to a null terminated string.

Description

Sets an expectation that the values thatleft andright evaluate to arenot equal. This is semantically equivalent toKUNIT_EXPECT_TRUE(test, strcmp((left), (right))). SeeKUNIT_EXPECT_TRUE()for more information.

KUNIT_EXPECT_MEMEQ

KUNIT_EXPECT_MEMEQ(test,left,right,size)

Expects that the firstsize bytes ofleft andright are equal.

Parameters

test

The test context object.

left

An arbitrary expression that evaluates to the specified size.

right

An arbitrary expression that evaluates to the specified size.

size

Number of bytes compared.

Description

Sets an expectation that the values thatleft andright evaluate to areequal. This is semantically equivalent toKUNIT_EXPECT_TRUE(test, !memcmp((left), (right), (size))). SeeKUNIT_EXPECT_TRUE() for more information.

Although this expectation works for any memory block, it is not recommendedfor comparing more structured data, such as structs. This expectation isrecommended for comparing, for example, data arrays.

KUNIT_EXPECT_MEMNEQ

KUNIT_EXPECT_MEMNEQ(test,left,right,size)

Expects that the firstsize bytes ofleft andright are not equal.

Parameters

test

The test context object.

left

An arbitrary expression that evaluates to the specified size.

right

An arbitrary expression that evaluates to the specified size.

size

Number of bytes compared.

Description

Sets an expectation that the values thatleft andright evaluate to arenot equal. This is semantically equivalent toKUNIT_EXPECT_TRUE(test, memcmp((left), (right), (size))). SeeKUNIT_EXPECT_TRUE() for more information.

Although this expectation works for any memory block, it is not recommendedfor comparing more structured data, such as structs. This expectation isrecommended for comparing, for example, data arrays.

KUNIT_EXPECT_NULL

KUNIT_EXPECT_NULL(test,ptr)

Expects thatptr is null.

Parameters

test

The test context object.

ptr

an arbitrary pointer.

Description

Sets an expectation that the value thatptr evaluates to is null. This issemantically equivalent to KUNIT_EXPECT_PTR_EQ(test, ptr, NULL).SeeKUNIT_EXPECT_TRUE() for more information.

KUNIT_EXPECT_NOT_NULL

KUNIT_EXPECT_NOT_NULL(test,ptr)

Expects thatptr is not null.

Parameters

test

The test context object.

ptr

an arbitrary pointer.

Description

Sets an expectation that the value thatptr evaluates to is not null. Thisis semantically equivalent to KUNIT_EXPECT_PTR_NE(test, ptr, NULL).SeeKUNIT_EXPECT_TRUE() for more information.

KUNIT_EXPECT_NOT_ERR_OR_NULL

KUNIT_EXPECT_NOT_ERR_OR_NULL(test,ptr)

Expects thatptr is not null and not err.

Parameters

test

The test context object.

ptr

an arbitrary pointer.

Description

Sets an expectation that the value thatptr evaluates to is not null and notan errno stored in a pointer. This is semantically equivalent toKUNIT_EXPECT_TRUE(test, !IS_ERR_OR_NULL(ptr)). SeeKUNIT_EXPECT_TRUE() formore information.

KUNIT_FAIL_AND_ABORT

KUNIT_FAIL_AND_ABORT(test,fmt,...)

Always causes a test to fail and abort when evaluated.

Parameters

test

The test context object.

fmt

an informational message to be printed when the assertion is made.

...

string format arguments.

Description

The opposite ofKUNIT_SUCCEED(), it is an assertion that always fails. Inother words, it always results in a failed assertion, and consequentlyalways causes the test case to fail and abort when evaluated.SeeKUNIT_ASSERT_TRUE() for more information.

KUNIT_ASSERT_TRUE

KUNIT_ASSERT_TRUE(test,condition)

Sets an assertion thatcondition is true.

Parameters

test

The test context object.

condition

an arbitrary boolean expression. The test fails and aborts whenthis does not evaluate to true.

Description

This and assertions of the formKUNIT_ASSERT_* will cause the test case tofailand immediately abort when the specified condition is not met. Unlikean expectation failure, it will prevent the test case from continuing to run;this is otherwise known as anassertion failure.

KUNIT_ASSERT_FALSE

KUNIT_ASSERT_FALSE(test,condition)

Sets an assertion thatcondition is false.

Parameters

test

The test context object.

condition

an arbitrary boolean expression.

Description

Sets an assertion that the value thatcondition evaluates to is false. Thisis the same asKUNIT_EXPECT_FALSE(), except it causes an assertion failure(seeKUNIT_ASSERT_TRUE()) when the assertion is not met.

KUNIT_ASSERT_EQ

KUNIT_ASSERT_EQ(test,left,right)

Sets an assertion thatleft andright are equal.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a primitive C type.

right

an arbitrary expression that evaluates to a primitive C type.

Description

Sets an assertion that the values thatleft andright evaluate to areequal. This is the same asKUNIT_EXPECT_EQ(), except it causes an assertionfailure (seeKUNIT_ASSERT_TRUE()) when the assertion is not met.

KUNIT_ASSERT_PTR_EQ

KUNIT_ASSERT_PTR_EQ(test,left,right)

Asserts that pointersleft andright are equal.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a pointer.

right

an arbitrary expression that evaluates to a pointer.

Description

Sets an assertion that the values thatleft andright evaluate to areequal. This is the same asKUNIT_EXPECT_EQ(), except it causes an assertionfailure (seeKUNIT_ASSERT_TRUE()) when the assertion is not met.

KUNIT_ASSERT_NE

KUNIT_ASSERT_NE(test,left,right)

An assertion thatleft andright are not equal.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a primitive C type.

right

an arbitrary expression that evaluates to a primitive C type.

Description

Sets an assertion that the values thatleft andright evaluate to are notequal. This is the same asKUNIT_EXPECT_NE(), except it causes an assertionfailure (seeKUNIT_ASSERT_TRUE()) when the assertion is not met.

KUNIT_ASSERT_PTR_NE

KUNIT_ASSERT_PTR_NE(test,left,right)

Asserts that pointersleft andright are not equal.KUNIT_ASSERT_PTR_EQ() - Asserts that pointersleft andright are equal.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a pointer.

right

an arbitrary expression that evaluates to a pointer.

Description

Sets an assertion that the values thatleft andright evaluate to are notequal. This is the same asKUNIT_EXPECT_NE(), except it causes an assertionfailure (seeKUNIT_ASSERT_TRUE()) when the assertion is not met.

KUNIT_ASSERT_LT

KUNIT_ASSERT_LT(test,left,right)

An assertion thatleft is less thanright.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a primitive C type.

right

an arbitrary expression that evaluates to a primitive C type.

Description

Sets an assertion that the value thatleft evaluates to is less than thevalue thatright evaluates to. This is the same asKUNIT_EXPECT_LT(), exceptit causes an assertion failure (seeKUNIT_ASSERT_TRUE()) when the assertionis not met.

KUNIT_ASSERT_LE

KUNIT_ASSERT_LE(test,left,right)

An assertion thatleft is less than or equal toright.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a primitive C type.

right

an arbitrary expression that evaluates to a primitive C type.

Description

Sets an assertion that the value thatleft evaluates to is less than orequal to the value thatright evaluates to. This is the same asKUNIT_EXPECT_LE(), except it causes an assertion failure (seeKUNIT_ASSERT_TRUE()) when the assertion is not met.

KUNIT_ASSERT_GT

KUNIT_ASSERT_GT(test,left,right)

An assertion thatleft is greater thanright.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a primitive C type.

right

an arbitrary expression that evaluates to a primitive C type.

Description

Sets an assertion that the value thatleft evaluates to is greater than thevalue thatright evaluates to. This is the same asKUNIT_EXPECT_GT(), exceptit causes an assertion failure (seeKUNIT_ASSERT_TRUE()) when the assertionis not met.

KUNIT_ASSERT_GE

KUNIT_ASSERT_GE(test,left,right)

Assertion thatleft is greater than or equal toright.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a primitive C type.

right

an arbitrary expression that evaluates to a primitive C type.

Description

Sets an assertion that the value thatleft evaluates to is greater than thevalue thatright evaluates to. This is the same asKUNIT_EXPECT_GE(), exceptit causes an assertion failure (seeKUNIT_ASSERT_TRUE()) when the assertionis not met.

KUNIT_ASSERT_STREQ

KUNIT_ASSERT_STREQ(test,left,right)

An assertion that stringsleft andright are equal.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a null terminated string.

right

an arbitrary expression that evaluates to a null terminated string.

Description

Sets an assertion that the values thatleft andright evaluate to areequal. This is the same asKUNIT_EXPECT_STREQ(), except it causes anassertion failure (seeKUNIT_ASSERT_TRUE()) when the assertion is not met.

KUNIT_ASSERT_STRNEQ

KUNIT_ASSERT_STRNEQ(test,left,right)

An assertion that stringsleft andright are not equal.

Parameters

test

The test context object.

left

an arbitrary expression that evaluates to a null terminated string.

right

an arbitrary expression that evaluates to a null terminated string.

Description

Sets an assertion that the values thatleft andright evaluate to arenot equal. This is semantically equivalent toKUNIT_ASSERT_TRUE(test, strcmp((left), (right))). SeeKUNIT_ASSERT_TRUE()for more information.

KUNIT_ASSERT_MEMEQ

KUNIT_ASSERT_MEMEQ(test,left,right,size)

Asserts that the firstsize bytes ofleft andright are equal.

Parameters

test

The test context object.

left

An arbitrary expression that evaluates to the specified size.

right

An arbitrary expression that evaluates to the specified size.

size

Number of bytes compared.

Description

Sets an assertion that the values thatleft andright evaluate to areequal. This is semantically equivalent toKUNIT_ASSERT_TRUE(test, !memcmp((left), (right), (size))). SeeKUNIT_ASSERT_TRUE() for more information.

Although this assertion works for any memory block, it is not recommendedfor comparing more structured data, such as structs. This assertion isrecommended for comparing, for example, data arrays.

KUNIT_ASSERT_MEMNEQ

KUNIT_ASSERT_MEMNEQ(test,left,right,size)

Asserts that the firstsize bytes ofleft andright are not equal.

Parameters

test

The test context object.

left

An arbitrary expression that evaluates to the specified size.

right

An arbitrary expression that evaluates to the specified size.

size

Number of bytes compared.

Description

Sets an assertion that the values thatleft andright evaluate to arenot equal. This is semantically equivalent toKUNIT_ASSERT_TRUE(test, memcmp((left), (right), (size))). SeeKUNIT_ASSERT_TRUE() for more information.

Although this assertion works for any memory block, it is not recommendedfor comparing more structured data, such as structs. This assertion isrecommended for comparing, for example, data arrays.

KUNIT_ASSERT_NULL

KUNIT_ASSERT_NULL(test,ptr)

Asserts that pointersptr is null.

Parameters

test

The test context object.

ptr

an arbitrary pointer.

Description

Sets an assertion that the values thatptr evaluates to is null. This isthe same asKUNIT_EXPECT_NULL(), except it causes an assertionfailure (seeKUNIT_ASSERT_TRUE()) when the assertion is not met.

KUNIT_ASSERT_NOT_NULL

KUNIT_ASSERT_NOT_NULL(test,ptr)

Asserts that pointersptr is not null.

Parameters

test

The test context object.

ptr

an arbitrary pointer.

Description

Sets an assertion that the values thatptr evaluates to is not null. Thisis the same asKUNIT_EXPECT_NOT_NULL(), except it causes an assertionfailure (seeKUNIT_ASSERT_TRUE()) when the assertion is not met.

KUNIT_ASSERT_NOT_ERR_OR_NULL

KUNIT_ASSERT_NOT_ERR_OR_NULL(test,ptr)

Assertion thatptr is not null and not err.

Parameters

test

The test context object.

ptr

an arbitrary pointer.

Description

Sets an assertion that the value thatptr evaluates to is not null and notan errno stored in a pointer. This is the same asKUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (seeKUNIT_ASSERT_TRUE()) when the assertion is not met.

KUNIT_ARRAY_PARAM

KUNIT_ARRAY_PARAM(name,array,get_desc)

Define test parameter generator from an array.

Parameters

name

prefix for the test parameter generator function.

array

array of test parameters.

get_desc

function to convert param to description; NULL to use default

Description

Define functionname_gen_params which usesarray to generate parameters.

KUNIT_ARRAY_PARAM_DESC

KUNIT_ARRAY_PARAM_DESC(name,array,desc_member)

Define test parameter generator from an array.

Parameters

name

prefix for the test parameter generator function.

array

array of test parameters.

desc_member

structure member from array element to use as description

Description

Define functionname_gen_params which usesarray to generate parameters.

kunit_register_params_array

kunit_register_params_array(test,array,param_count,get_desc)

Register parameter array for a KUnit test.

Parameters

test

The KUnit test structure to which parameters will be added.

array

An array of test parameters.

param_count

Number of parameters.

get_desc

Function that generates a string description for a given parameterelement.

Description

This macro initializes thetest’s parameter array data, storing informationincluding the parameter array, its count, the element size, and the parameterdescription function withintest->params_array.

Note

If using this macro inparam_init(),kunit_array_gen_params()will then need to be manually provided as the parameter generator function toKUNIT_CASE_PARAM_WITH_INIT().kunit_array_gen_params() is a KUnitfunction that uses the registered array to generate parameters