# Test API

_Peios Kernel / Kernel development / Development tools / kunit / api_

> This file documents all of the standard testing API.

<a id="rst-test-api"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

This file documents all of the standard testing API.


#### <a id="c-enum-kunit_status"></a>`enum kunit_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.

#### <a id="c-struct-kunit_case"></a>`struct kunit_case`

represents an individual test case.

**Definition**

```c
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 (*)(struct kunit *)`
that makes expectations and assertions (see [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) and
[`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)) about code under test. Each test case is associated
with a [`struct kunit_suite`](#c-struct-kunit_suite) and will be run after the suite's init
function and followed by the suite's exit function.

A test case should be static and should only be created with the
[`KUNIT_CASE()`](#c-KUNIT_CASE) macro; additionally, every array of test cases should be
terminated with an empty test case.

**Example**

```c
void add_test_basic(struct kunit *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));
}

static struct kunit_case example_test_cases[] = {
        KUNIT_CASE(add_test_basic),
        {}
};
```

#### <a id="c-KUNIT_CASE"></a>`KUNIT_CASE()`

```c
KUNIT_CASE(test_name)
```

A helper for creating a [`struct kunit_case`](#c-struct-kunit_case)

**Parameters**

- `test_name`: a reference to a test case function.

**Description**

Takes a symbol for a function representing a test case and creates a
[`struct kunit_case`](#c-struct-kunit_case) object from it. See the documentation for
[`struct kunit_case`](#c-struct-kunit_case) for an example on how to use it.

#### <a id="c-KUNIT_CASE_ATTR"></a>`KUNIT_CASE_ATTR()`

```c
KUNIT_CASE_ATTR(test_name, attributes)
```

A helper for creating a [`struct kunit_case`](#c-struct-kunit_case) with attributes

**Parameters**

- `test_name`: a reference to a test case function.
- `attributes`: a reference to a struct kunit_attributes object containing
  test attributes

#### <a id="c-KUNIT_CASE_SLOW"></a>`KUNIT_CASE_SLOW()`

```c
KUNIT_CASE_SLOW(test_name)
```

A helper for creating a [`struct kunit_case`](#c-struct-kunit_case) with the slow attribute

**Parameters**

- `test_name`: a reference to a test case function.

#### <a id="c-KUNIT_CASE_PARAM"></a>`KUNIT_CASE_PARAM()`

```c
KUNIT_CASE_PARAM(test_name, gen_params)
```

A helper for creation a parameterized [`struct kunit_case`](#c-struct-kunit_case)

**Parameters**

- `test_name`: a reference to a test case function.
- `gen_params`: a reference to a parameter generator function.

**Description**

The generator function:

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

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

#### <a id="c-KUNIT_CASE_PARAM_ATTR"></a>`KUNIT_CASE_PARAM_ATTR()`

```c
KUNIT_CASE_PARAM_ATTR(test_name, gen_params, attributes)
```

A helper for creating a parameterized [`struct kunit_case`](#c-struct-kunit_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 a struct kunit_attributes object containing
  test attributes

#### <a id="c-KUNIT_CASE_PARAM_WITH_INIT"></a>`KUNIT_CASE_PARAM_WITH_INIT()`

```c
KUNIT_CASE_PARAM_WITH_INIT(test_name, gen_params, init, exit)
```

Define a parameterized KUnit test case with custom `param_init()` and `param_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 the `param_init()` function to run before a parameterized test.
- `exit`: A reference to the `param_exit()` function to run after a parameterized test.

**Description**

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

**Note**

If you are registering a parameter array in `param_init()` with
`kunit_register_param_array()` then you need to pass `kunit_array_gen_params()`
to this as the generator function.

#### <a id="c-struct-kunit_suite"></a>`struct kunit_suite`

describes a related collection of [`struct kunit_case`](#c-struct-kunit_case)

**Definition**

```c
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 related [`struct kunit_case`](#c-struct-kunit_case) s, such that
**init** is called before every test case and **exit** is called after every
test case, similar to the notion of a *test fixture* or a *test class*
in other unit testing frameworks like JUnit or Googletest.

Note that **exit** and **suite_exit** will run even if **init** or **suite_init**
fail: make sure they can handle any inconsistent state which may result.

Every [`struct kunit_case`](#c-struct-kunit_case) must be associated with a kunit_suite for KUnit
to run it.

#### <a id="c-struct-kunit"></a>`struct kunit`

represents a running instance of a test.

**Definition**

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

**Members**

- `priv`: for user to store arbitrary data. Commonly used to pass data
  created in the init function (see [`struct kunit_suite`](#c-struct-kunit_suite)).
- `parent`: reference to the parent context of type struct kunit that can
  be used for storing shared resources.
- `params_array`: for storing the parameter array.

**Description**

Used to store information about the current context under which the test
is running. Most of this data is private and should only be accessed
indirectly via public functions; the exceptions are **priv**, **parent** and
**params_array** which can be used by the test writer to store arbitrary data,
access the parent context, and to store the parameter array, respectively.

#### <a id="c-kunit_test_suites"></a>`kunit_test_suites()`

```c
kunit_test_suites(__suites...)
```

used to register one or more [`struct kunit_suite`](#c-struct-kunit_suite) with KUnit.

**Parameters**

- `__suites...`: a statically allocated list of [`struct kunit_suite`](#c-struct-kunit_suite).

**Description**

Registers **suites** with the test framework.
This is done by placing the array of struct kunit_suite * in the
.kunit_test_suites ELF section.

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

#### <a id="c-kunit_test_init_section_suites"></a>`kunit_test_init_section_suites()`

```c
kunit_test_init_section_suites(__suites...)
```

used to register one or more [`struct kunit_suite`](#c-struct-kunit_suite) containing init functions or init data.

**Parameters**

- `__suites...`: a statically allocated list of [`struct kunit_suite`](#c-struct-kunit_suite).

**Description**

This functions similar to [`kunit_test_suites()`](#c-kunit_test_suites) except that it compiles the
list 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 data
for 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 because
they will be used after the init phase with debugfs.

#### <a id="c-kunit_kmalloc_array"></a>`kunit_kmalloc_array()`

```c
void * kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
```

Like [`kmalloc_array()`](/kernel/internals/core-api/mm-api.md#c-kmalloc_array) except the allocation is *test managed*.

**Parameters**

- `struct kunit *test`: The test context object.
- `size_t n`: number of elements.
- `size_t size`: The size in bytes of the desired memory.
- `gfp_t gfp`: flags passed to underlying [`kmalloc()`](/kernel/internals/core-api/mm-api.md#c-kmalloc).

**Description**

Just like `kmalloc_array(...)`, except the allocation is managed by the test case
and is automatically cleaned up after the test case concludes. See [`kunit_add_action()`](/kernel/development/dev-tools/kunit-api/resource.md#c-kunit_add_action)
for more information.

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

#### <a id="c-kunit_kmalloc"></a>`kunit_kmalloc()`

```c
void * kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
```

Like [`kmalloc()`](/kernel/internals/core-api/mm-api.md#c-kmalloc) except the allocation is *test managed*.

**Parameters**

- `struct kunit *test`: The test context object.
- `size_t size`: The size in bytes of the desired memory.
- `gfp_t gfp`: flags passed to underlying [`kmalloc()`](/kernel/internals/core-api/mm-api.md#c-kmalloc).

**Description**

See [`kmalloc()`](/kernel/internals/core-api/mm-api.md#c-kmalloc) and [`kunit_kmalloc_array()`](#c-kunit_kmalloc_array) for more information.

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

#### <a id="c-kunit_kfree"></a>`kunit_kfree()`

```c
void kunit_kfree(struct kunit *test, const void *ptr)
```

Like kfree except for allocations managed by KUnit.

**Parameters**

- `struct kunit *test`: The test case to which the resource belongs.
- `const void *ptr`: The memory allocation to free.

#### <a id="c-kunit_kzalloc"></a>`kunit_kzalloc()`

```c
void * kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
```

Just like [`kunit_kmalloc()`](#c-kunit_kmalloc), but zeroes the allocation.

**Parameters**

- `struct kunit *test`: The test context object.
- `size_t size`: The size in bytes of the desired memory.
- `gfp_t gfp`: flags passed to underlying [`kmalloc()`](/kernel/internals/core-api/mm-api.md#c-kmalloc).

**Description**

See [`kzalloc()`](/kernel/internals/core-api/mm-api.md#c-kzalloc) and [`kunit_kmalloc_array()`](#c-kunit_kmalloc_array) for more information.

#### <a id="c-kunit_kcalloc"></a>`kunit_kcalloc()`

```c
void * kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
```

Just like [`kunit_kmalloc_array()`](#c-kunit_kmalloc_array), but zeroes the allocation.

**Parameters**

- `struct kunit *test`: The test context object.
- `size_t n`: number of elements.
- `size_t size`: The size in bytes of the desired memory.
- `gfp_t gfp`: flags passed to underlying [`kmalloc()`](/kernel/internals/core-api/mm-api.md#c-kmalloc).

**Description**

See [`kcalloc()`](/kernel/internals/core-api/mm-api.md#c-kcalloc) and [`kunit_kmalloc_array()`](#c-kunit_kmalloc_array) for more information.

#### <a id="c-kunit_kfree_const"></a>`kunit_kfree_const()`

```c
void kunit_kfree_const(struct kunit *test, const void *x)
```

conditionally free test managed memory

**Parameters**

- `struct kunit *test`: The test context object.
- `const void *x`: pointer to the memory

**Description**

Calls [`kunit_kfree()`](#c-kunit_kfree) only if **x** is not in .rodata section.
See [`kunit_kstrdup_const()`](#c-kunit_kstrdup_const) for more information.

#### <a id="c-kunit_kstrdup"></a>`kunit_kstrdup()`

```c
char * kunit_kstrdup(struct kunit *test, const char *str, gfp_t gfp)
```

Duplicates a string into a test managed allocation.

**Parameters**

- `struct kunit *test`: The test context object.
- `const char *str`: The NULL-terminated string to duplicate.
- `gfp_t gfp`: flags passed to underlying [`kmalloc()`](/kernel/internals/core-api/mm-api.md#c-kmalloc).

**Description**

See [`kstrdup()`](/kernel/internals/core-api/kernel-api.md#c-kstrdup) and [`kunit_kmalloc_array()`](#c-kunit_kmalloc_array) for more information.

#### <a id="c-kunit_kstrdup_const"></a>`kunit_kstrdup_const()`

```c
const char * kunit_kstrdup_const(struct kunit *test, const char *str, gfp_t gfp)
```

Conditionally duplicates a string into a test managed allocation.

**Parameters**

- `struct kunit *test`: The test context object.
- `const char *str`: The NULL-terminated string to duplicate.
- `gfp_t gfp`: flags passed to underlying [`kmalloc()`](/kernel/internals/core-api/mm-api.md#c-kmalloc).

**Description**

Calls [`kunit_kstrdup()`](#c-kunit_kstrdup) only if **str** is not in the rodata section. Must be freed with
[`kunit_kfree_const()`](#c-kunit_kfree_const) -- not [`kunit_kfree()`](#c-kunit_kfree).
See [`kstrdup_const()`](/kernel/internals/core-api/kernel-api.md#c-kstrdup_const) and [`kunit_kmalloc_array()`](#c-kunit_kmalloc_array) for more information.

#### <a id="c-kunit_attach_mm"></a>`kunit_attach_mm()`

```c
int kunit_attach_mm(void)
```

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

**Parameters**

- `void`: no arguments

**Description**

Allocates a `struct mm_struct` and attaches it to **current**. In most cases, call
[`kunit_vm_mmap()`](#c-kunit_vm_mmap) without calling [`kunit_attach_mm()`](#c-kunit_attach_mm) directly. Only necessary when
code under test accesses the mm before executing the mmap (e.g., to perform
additional initialization beforehand).

**Return**

0 on success, -errno on failure.

#### <a id="c-kunit_vm_mmap"></a>`kunit_vm_mmap()`

```c
unsigned long kunit_vm_mmap(struct kunit *test, struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flag, unsigned long offset)
```

Allocate KUnit-tracked `vm_mmap()` area

**Parameters**

- `struct kunit *test`: The test context object.
- `struct file *file`: struct file pointer to map from, if any
- `unsigned long addr`: desired address, if any
- `unsigned long len`: how many bytes to allocate
- `unsigned long prot`: mmap PROT_* bits
- `unsigned long flag`: mmap flags
- `unsigned long offset`: offset into **file** to start mapping from.

**Description**

See `vm_mmap()` for more information.

#### <a id="c-kunit_mark_skipped"></a>`kunit_mark_skipped()`

```c
kunit_mark_skipped(test, fmt, ...)
```

Marks **test** as skipped

**Parameters**

- `test`: The test context object.
- `fmt`: A [`printk()`](/kernel/internals/core-api/printk-basics.md#c-printk) style format string.
- `...`: variable arguments

**Description**

Marks the test as skipped. **fmt** is given output as the test status
comment, typically the reason the test was skipped.

Test execution continues after [`kunit_mark_skipped()`](#c-kunit_mark_skipped) is called.

#### <a id="c-kunit_skip"></a>`kunit_skip()`

```c
kunit_skip(test, fmt, ...)
```

Marks **test** as skipped

**Parameters**

- `test`: The test context object.
- `fmt`: A [`printk()`](/kernel/internals/core-api/printk-basics.md#c-printk) style format string.
- `...`: variable arguments

**Description**

Skips the test. **fmt** is given output as the test status
comment, typically the reason the test was skipped.

Test execution is halted after [`kunit_skip()`](#c-kunit_skip) is called.

#### <a id="c-kunit_info"></a>`kunit_info()`

```c
kunit_info(test, fmt, ...)
```

Prints an INFO level message associated with **test**.

**Parameters**

- `test`: The test context object.
- `fmt`: A [`printk()`](/kernel/internals/core-api/printk-basics.md#c-printk) 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 like [`printk()`](/kernel/internals/core-api/printk-basics.md#c-printk).

#### <a id="c-kunit_warn"></a>`kunit_warn()`

```c
kunit_warn(test, fmt, ...)
```

Prints a WARN level message associated with **test**.

**Parameters**

- `test`: The test context object.
- `fmt`: A [`printk()`](/kernel/internals/core-api/printk-basics.md#c-printk) style format string.
- `...`: variable arguments

**Description**

Prints a warning level message.

#### <a id="c-kunit_err"></a>`kunit_err()`

```c
kunit_err(test, fmt, ...)
```

Prints an ERROR level message associated with **test**.

**Parameters**

- `test`: The test context object.
- `fmt`: A [`printk()`](/kernel/internals/core-api/printk-basics.md#c-printk) style format string.
- `...`: variable arguments

**Description**

Prints an error level message.

#### <a id="c-KUNIT_SUCCEED"></a>`KUNIT_SUCCEED()`

```c
KUNIT_SUCCEED(test)
```

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

**Parameters**

- `test`: The test context object.

**Description**

The opposite of [`KUNIT_FAIL()`](#c-KUNIT_FAIL), it is an expectation that cannot fail. In other
words, it does nothing and only exists for code clarity. See
[`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for more information.

#### <a id="c-KUNIT_FAIL"></a>`KUNIT_FAIL()`

```c
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 of [`KUNIT_SUCCEED()`](#c-KUNIT_SUCCEED), it is an expectation that always fails. In
other words, it always results in a failed expectation, and consequently
always causes the test case to fail when evaluated. See [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE)
for more information.

#### <a id="c-KUNIT_EXPECT_TRUE"></a>`KUNIT_EXPECT_TRUE()`

```c
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 does
  not evaluate to true.

**Description**

This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
to fail when the specified condition is not met; however, it will not prevent
the test case from continuing to run; this is otherwise known as an
*expectation failure*.

#### <a id="c-KUNIT_EXPECT_FALSE"></a>`KUNIT_EXPECT_FALSE()`

```c
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 does
  not evaluate to false.

**Description**

Sets an expectation that **condition** evaluates to false. See
[`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for more information.

#### <a id="c-KUNIT_EXPECT_EQ"></a>`KUNIT_EXPECT_EQ()`

```c
KUNIT_EXPECT_EQ(test, left, right)
```

Sets an expectation that **left** and **right** 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 that **left** and **right** evaluate to are
equal. This is semantically equivalent to
KUNIT_EXPECT_TRUE(**test**, (**left**) == (**right**)). See [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for
more information.

#### <a id="c-KUNIT_EXPECT_PTR_EQ"></a>`KUNIT_EXPECT_PTR_EQ()`

```c
KUNIT_EXPECT_PTR_EQ(test, left, right)
```

Expects that pointers **left** and **right** 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 that **left** and **right** evaluate to are
equal. This is semantically equivalent to
KUNIT_EXPECT_TRUE(**test**, (**left**) == (**right**)). See [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for
more information.

#### <a id="c-KUNIT_EXPECT_NE"></a>`KUNIT_EXPECT_NE()`

```c
KUNIT_EXPECT_NE(test, left, right)
```

An expectation that **left** and **right** 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 that **left** and **right** evaluate to are not
equal. This is semantically equivalent to
KUNIT_EXPECT_TRUE(**test**, (**left**) != (**right**)). See [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for
more information.

#### <a id="c-KUNIT_EXPECT_PTR_NE"></a>`KUNIT_EXPECT_PTR_NE()`

```c
KUNIT_EXPECT_PTR_NE(test, left, right)
```

Expects that pointers **left** and **right** 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 that **left** and **right** evaluate to are not
equal. This is semantically equivalent to
KUNIT_EXPECT_TRUE(**test**, (**left**) != (**right**)). See [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for
more information.

#### <a id="c-KUNIT_EXPECT_LT"></a>`KUNIT_EXPECT_LT()`

```c
KUNIT_EXPECT_LT(test, left, right)
```

An expectation that **left** is less than **right**.

**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 that **left** evaluates to is less than the
value that **right** evaluates to. This is semantically equivalent to
KUNIT_EXPECT_TRUE(**test**, (**left**) \< (**right**)). See [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for
more information.

#### <a id="c-KUNIT_EXPECT_LE"></a>`KUNIT_EXPECT_LE()`

```c
KUNIT_EXPECT_LE(test, left, right)
```

Expects that **left** is less than or equal to **right**.

**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 that **left** evaluates to is less than or
equal to the value that **right** evaluates to. Semantically this is equivalent
to KUNIT_EXPECT_TRUE(**test**, (**left**) \<= (**right**)). See [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for
more information.

#### <a id="c-KUNIT_EXPECT_GT"></a>`KUNIT_EXPECT_GT()`

```c
KUNIT_EXPECT_GT(test, left, right)
```

An expectation that **left** is greater than **right**.

**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 that **left** evaluates to is greater than
the value that **right** evaluates to. This is semantically equivalent to
KUNIT_EXPECT_TRUE(**test**, (**left**) > (**right**)). See [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for
more information.

#### <a id="c-KUNIT_EXPECT_GE"></a>`KUNIT_EXPECT_GE()`

```c
KUNIT_EXPECT_GE(test, left, right)
```

Expects that **left** is greater than or equal to **right**.

**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 that **left** evaluates to is greater than
the value that **right** evaluates to. This is semantically equivalent to
KUNIT_EXPECT_TRUE(**test**, (**left**) >= (**right**)). See [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for
more information.

#### <a id="c-KUNIT_EXPECT_STREQ"></a>`KUNIT_EXPECT_STREQ()`

```c
KUNIT_EXPECT_STREQ(test, left, right)
```

Expects that strings **left** and **right** 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 that **left** and **right** evaluate to are
equal. This is semantically equivalent to
KUNIT_EXPECT_TRUE(**test**, !strcmp((**left**), (**right**))). See [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE)
for more information.

#### <a id="c-KUNIT_EXPECT_STRNEQ"></a>`KUNIT_EXPECT_STRNEQ()`

```c
KUNIT_EXPECT_STRNEQ(test, left, right)
```

Expects that strings **left** and **right** 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 that **left** and **right** evaluate to are
not equal. This is semantically equivalent to
KUNIT_EXPECT_TRUE(**test**, strcmp((**left**), (**right**))). See [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE)
for more information.

#### <a id="c-KUNIT_EXPECT_MEMEQ"></a>`KUNIT_EXPECT_MEMEQ()`

```c
KUNIT_EXPECT_MEMEQ(test, left, right, size)
```

Expects that the first **size** bytes of **left** and **right** 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 that **left** and **right** evaluate to are
equal. This is semantically equivalent to
KUNIT_EXPECT_TRUE(**test**, !memcmp((**left**), (**right**), (**size**))). See
[`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for more information.

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

#### <a id="c-KUNIT_EXPECT_MEMNEQ"></a>`KUNIT_EXPECT_MEMNEQ()`

```c
KUNIT_EXPECT_MEMNEQ(test, left, right, size)
```

Expects that the first **size** bytes of **left** and **right** 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 that **left** and **right** evaluate to are
not equal. This is semantically equivalent to
KUNIT_EXPECT_TRUE(**test**, memcmp((**left**), (**right**), (**size**))). See
[`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for more information.

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

#### <a id="c-KUNIT_EXPECT_NULL"></a>`KUNIT_EXPECT_NULL()`

```c
KUNIT_EXPECT_NULL(test, ptr)
```

Expects that **ptr** is null.

**Parameters**

- `test`: The test context object.
- `ptr`: an arbitrary pointer.

**Description**

Sets an expectation that the value that **ptr** evaluates to is null. This is
semantically equivalent to KUNIT_EXPECT_PTR_EQ(**test**, ptr, NULL).
See [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for more information.

#### <a id="c-KUNIT_EXPECT_NOT_NULL"></a>`KUNIT_EXPECT_NOT_NULL()`

```c
KUNIT_EXPECT_NOT_NULL(test, ptr)
```

Expects that **ptr** is not null.

**Parameters**

- `test`: The test context object.
- `ptr`: an arbitrary pointer.

**Description**

Sets an expectation that the value that **ptr** evaluates to is not null. This
is semantically equivalent to KUNIT_EXPECT_PTR_NE(**test**, ptr, NULL).
See [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for more information.

#### <a id="c-KUNIT_EXPECT_NOT_ERR_OR_NULL"></a>`KUNIT_EXPECT_NOT_ERR_OR_NULL()`

```c
KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr)
```

Expects that **ptr** is not null and not err.

**Parameters**

- `test`: The test context object.
- `ptr`: an arbitrary pointer.

**Description**

Sets an expectation that the value that **ptr** evaluates to is not null and not
an errno stored in a pointer. This is semantically equivalent to
KUNIT_EXPECT_TRUE(**test**, !IS_ERR_OR_NULL(**ptr**)). See [`KUNIT_EXPECT_TRUE()`](#c-KUNIT_EXPECT_TRUE) for
more information.

#### <a id="c-KUNIT_FAIL_AND_ABORT"></a>`KUNIT_FAIL_AND_ABORT()`

```c
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 of [`KUNIT_SUCCEED()`](#c-KUNIT_SUCCEED), it is an assertion that always fails. In
other words, it always results in a failed assertion, and consequently
always causes the test case to fail and abort when evaluated.
See [`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE) for more information.

#### <a id="c-KUNIT_ASSERT_TRUE"></a>`KUNIT_ASSERT_TRUE()`

```c
KUNIT_ASSERT_TRUE(test, condition)
```

Sets an assertion that **condition** is true.

**Parameters**

- `test`: The test context object.
- `condition`: an arbitrary boolean expression. The test fails and aborts when
  this does not evaluate to true.

**Description**

This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
fail *and immediately abort* when the specified condition is not met. Unlike
an expectation failure, it will prevent the test case from continuing to run;
this is otherwise known as an *assertion failure*.

#### <a id="c-KUNIT_ASSERT_FALSE"></a>`KUNIT_ASSERT_FALSE()`

```c
KUNIT_ASSERT_FALSE(test, condition)
```

Sets an assertion that **condition** is false.

**Parameters**

- `test`: The test context object.
- `condition`: an arbitrary boolean expression.

**Description**

Sets an assertion that the value that **condition** evaluates to is false. This
is the same as [`KUNIT_EXPECT_FALSE()`](#c-KUNIT_EXPECT_FALSE), except it causes an assertion failure
(see [`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)) when the assertion is not met.

#### <a id="c-KUNIT_ASSERT_EQ"></a>`KUNIT_ASSERT_EQ()`

```c
KUNIT_ASSERT_EQ(test, left, right)
```

Sets an assertion that **left** and **right** 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 that **left** and **right** evaluate to are
equal. This is the same as [`KUNIT_EXPECT_EQ()`](#c-KUNIT_EXPECT_EQ), except it causes an assertion
failure (see [`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)) when the assertion is not met.

#### <a id="c-KUNIT_ASSERT_PTR_EQ"></a>`KUNIT_ASSERT_PTR_EQ()`

```c
KUNIT_ASSERT_PTR_EQ(test, left, right)
```

Asserts that pointers **left** and **right** 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 that **left** and **right** evaluate to are
equal. This is the same as [`KUNIT_EXPECT_EQ()`](#c-KUNIT_EXPECT_EQ), except it causes an assertion
failure (see [`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)) when the assertion is not met.

#### <a id="c-KUNIT_ASSERT_NE"></a>`KUNIT_ASSERT_NE()`

```c
KUNIT_ASSERT_NE(test, left, right)
```

An assertion that **left** and **right** 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 that **left** and **right** evaluate to are not
equal. This is the same as [`KUNIT_EXPECT_NE()`](#c-KUNIT_EXPECT_NE), except it causes an assertion
failure (see [`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)) when the assertion is not met.

#### <a id="c-KUNIT_ASSERT_PTR_NE"></a>`KUNIT_ASSERT_PTR_NE()`

```c
KUNIT_ASSERT_PTR_NE(test, left, right)
```

Asserts that pointers **left** and **right** are not equal. [`KUNIT_ASSERT_PTR_EQ()`](#c-KUNIT_ASSERT_PTR_EQ) - Asserts that pointers **left** and **right** 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 that **left** and **right** evaluate to are not
equal. This is the same as [`KUNIT_EXPECT_NE()`](#c-KUNIT_EXPECT_NE), except it causes an assertion
failure (see [`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)) when the assertion is not met.

#### <a id="c-KUNIT_ASSERT_LT"></a>`KUNIT_ASSERT_LT()`

```c
KUNIT_ASSERT_LT(test, left, right)
```

An assertion that **left** is less than **right**.

**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 that **left** evaluates to is less than the
value that **right** evaluates to. This is the same as [`KUNIT_EXPECT_LT()`](#c-KUNIT_EXPECT_LT), except
it causes an assertion failure (see [`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)) when the assertion
is not met.

#### <a id="c-KUNIT_ASSERT_LE"></a>`KUNIT_ASSERT_LE()`

```c
KUNIT_ASSERT_LE(test, left, right)
```

An assertion that **left** is less than or equal to **right**.

**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 that **left** evaluates to is less than or
equal to the value that **right** evaluates to. This is the same as
[`KUNIT_EXPECT_LE()`](#c-KUNIT_EXPECT_LE), except it causes an assertion failure (see
[`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)) when the assertion is not met.

#### <a id="c-KUNIT_ASSERT_GT"></a>`KUNIT_ASSERT_GT()`

```c
KUNIT_ASSERT_GT(test, left, right)
```

An assertion that **left** is greater than **right**.

**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 that **left** evaluates to is greater than the
value that **right** evaluates to. This is the same as [`KUNIT_EXPECT_GT()`](#c-KUNIT_EXPECT_GT), except
it causes an assertion failure (see [`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)) when the assertion
is not met.

#### <a id="c-KUNIT_ASSERT_GE"></a>`KUNIT_ASSERT_GE()`

```c
KUNIT_ASSERT_GE(test, left, right)
```

Assertion that **left** is greater than or equal to **right**.

**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 that **left** evaluates to is greater than the
value that **right** evaluates to. This is the same as [`KUNIT_EXPECT_GE()`](#c-KUNIT_EXPECT_GE), except
it causes an assertion failure (see [`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)) when the assertion
is not met.

#### <a id="c-KUNIT_ASSERT_STREQ"></a>`KUNIT_ASSERT_STREQ()`

```c
KUNIT_ASSERT_STREQ(test, left, right)
```

An assertion that strings **left** and **right** 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 that **left** and **right** evaluate to are
equal. This is the same as [`KUNIT_EXPECT_STREQ()`](#c-KUNIT_EXPECT_STREQ), except it causes an
assertion failure (see [`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)) when the assertion is not met.

#### <a id="c-KUNIT_ASSERT_STRNEQ"></a>`KUNIT_ASSERT_STRNEQ()`

```c
KUNIT_ASSERT_STRNEQ(test, left, right)
```

An assertion that strings **left** and **right** 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 that **left** and **right** evaluate to are
not equal. This is semantically equivalent to
KUNIT_ASSERT_TRUE(**test**, strcmp((**left**), (**right**))). See [`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)
for more information.

#### <a id="c-KUNIT_ASSERT_MEMEQ"></a>`KUNIT_ASSERT_MEMEQ()`

```c
KUNIT_ASSERT_MEMEQ(test, left, right, size)
```

Asserts that the first **size** bytes of **left** and **right** 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 that **left** and **right** evaluate to are
equal. This is semantically equivalent to
KUNIT_ASSERT_TRUE(**test**, !memcmp((**left**), (**right**), (**size**))). See
[`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE) for more information.

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

#### <a id="c-KUNIT_ASSERT_MEMNEQ"></a>`KUNIT_ASSERT_MEMNEQ()`

```c
KUNIT_ASSERT_MEMNEQ(test, left, right, size)
```

Asserts that the first **size** bytes of **left** and **right** 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 that **left** and **right** evaluate to are
not equal. This is semantically equivalent to
KUNIT_ASSERT_TRUE(**test**, memcmp((**left**), (**right**), (**size**))). See
[`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE) for more information.

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

#### <a id="c-KUNIT_ASSERT_NULL"></a>`KUNIT_ASSERT_NULL()`

```c
KUNIT_ASSERT_NULL(test, ptr)
```

Asserts that pointers **ptr** is null.

**Parameters**

- `test`: The test context object.
- `ptr`: an arbitrary pointer.

**Description**

Sets an assertion that the values that **ptr** evaluates to is null. This is
the same as [`KUNIT_EXPECT_NULL()`](#c-KUNIT_EXPECT_NULL), except it causes an assertion
failure (see [`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)) when the assertion is not met.

#### <a id="c-KUNIT_ASSERT_NOT_NULL"></a>`KUNIT_ASSERT_NOT_NULL()`

```c
KUNIT_ASSERT_NOT_NULL(test, ptr)
```

Asserts that pointers **ptr** is not null.

**Parameters**

- `test`: The test context object.
- `ptr`: an arbitrary pointer.

**Description**

Sets an assertion that the values that **ptr** evaluates to is not null. This
is the same as [`KUNIT_EXPECT_NOT_NULL()`](#c-KUNIT_EXPECT_NOT_NULL), except it causes an assertion
failure (see [`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)) when the assertion is not met.

#### <a id="c-KUNIT_ASSERT_NOT_ERR_OR_NULL"></a>`KUNIT_ASSERT_NOT_ERR_OR_NULL()`

```c
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr)
```

Assertion that **ptr** is not null and not err.

**Parameters**

- `test`: The test context object.
- `ptr`: an arbitrary pointer.

**Description**

Sets an assertion that the value that **ptr** evaluates to is not null and not
an errno stored in a pointer. This is the same as
[`KUNIT_EXPECT_NOT_ERR_OR_NULL()`](#c-KUNIT_EXPECT_NOT_ERR_OR_NULL), except it causes an assertion failure (see
[`KUNIT_ASSERT_TRUE()`](#c-KUNIT_ASSERT_TRUE)) when the assertion is not met.

#### <a id="c-KUNIT_ARRAY_PARAM"></a>`KUNIT_ARRAY_PARAM()`

```c
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 function **name_gen_params** which uses **array** to generate parameters.

#### <a id="c-KUNIT_ARRAY_PARAM_DESC"></a>`KUNIT_ARRAY_PARAM_DESC()`

```c
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 function **name_gen_params** which uses **array** to generate parameters.

#### <a id="c-kunit_register_params_array"></a>`kunit_register_params_array()`

```c
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 parameter
  element.

**Description**

This macro initializes the **test**'s parameter array data, storing information
including the parameter array, its count, the element size, and the parameter
description function within `test->params_array`.

**Note**

If using this macro in `param_init()`, `kunit_array_gen_params()`
will then need to be manually provided as the parameter generator function to
[`KUNIT_CASE_PARAM_WITH_INIT()`](#c-KUNIT_CASE_PARAM_WITH_INIT). `kunit_array_gen_params()` is a KUnit
function that uses the registered array to generate parameters
