Zephyr API Documentation  3.6.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
Ztest assertion macros

This module provides assertions when using Ztest. More...

Macros

#define zassert(cond, default_msg, ...)    _zassert_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
 
#define zassume(cond, default_msg, ...)    _zassume_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
 
#define zexpect(cond, default_msg, ...)    _zexpect_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))
 
#define zassert_unreachable(...)   zassert(0, "Reached unreachable code", ##__VA_ARGS__)
 Assert that this function call won't be reached.
 
#define zassert_true(cond, ...)   zassert(cond, #cond " is false", ##__VA_ARGS__)
 Assert that cond is true.
 
#define zassert_false(cond, ...)   zassert(!(cond), #cond " is true", ##__VA_ARGS__)
 Assert that cond is false.
 
#define zassert_ok(cond, ...)   zassert(!(cond), #cond " is non-zero", ##__VA_ARGS__)
 Assert that cond is 0 (success)
 
#define zassert_not_ok(cond, ...)   zassert(!!(cond), #cond " is zero", ##__VA_ARGS__)
 Assert that cond is not 0 (failure)
 
#define zassert_is_null(ptr, ...)   zassert((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)
 Assert that ptr is NULL.
 
#define zassert_not_null(ptr, ...)   zassert((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)
 Assert that ptr is not NULL.
 
#define zassert_equal(a, b, ...)   zassert((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)
 Assert that a equals b.
 
#define zassert_not_equal(a, b, ...)   zassert((a) != (b), #a " equal to " #b, ##__VA_ARGS__)
 Assert that a does not equal b.
 
#define zassert_equal_ptr(a, b, ...)    zassert((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)
 Assert that a equals b.
 
#define zassert_within(a, b, d, ...)
 Assert that a is within b with delta d.
 
#define zassert_between_inclusive(a, l, u, ...)
 Assert that a is greater than or equal to l and less than or equal to u.
 
#define zassert_mem_equal(...)   zassert_mem_equal__(__VA_ARGS__)
 Assert that 2 memory buffers have the same contents.
 
#define zassert_mem_equal__(buf, exp, size, ...)    zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)
 Internal assert that 2 memory buffers have the same contents.
 
#define zassert_str_equal(s1, s2, ...)    zassert(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)
 Assert that 2 strings have the same contents.
 

Detailed Description

This module provides assertions when using Ztest.

Macro Definition Documentation

◆ zassert

#define zassert (   cond,
  default_msg,
  ... 
)     _zassert_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))

◆ zassert_between_inclusive

#define zassert_between_inclusive (   a,
  l,
  u,
  ... 
)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Value:
zassert(((a) >= (l)) && ((a) <= (u)), #a " not between " #l " and " #u " inclusive", \
##__VA_ARGS__)
#define zassert(cond, default_msg,...)
Definition: ztest_assert.h:188

Assert that a is greater than or equal to l and less than or equal to u.

Parameters
aValue to compare
lLower limit
uUpper limit
...Optional message and variables to print if the assertion fails

◆ zassert_equal

#define zassert_equal (   a,
  b,
  ... 
)    zassert((a) == (b), #a " not equal to " #b, ##__VA_ARGS__)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Assert that a equals b.

a and b won't be converted and will be compared directly.

Parameters
aValue to compare
bValue to compare
...Optional message and variables to print if the assertion fails

◆ zassert_equal_ptr

#define zassert_equal_ptr (   a,
  b,
  ... 
)     zassert((void *)(a) == (void *)(b), #a " not equal to " #b, ##__VA_ARGS__)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Assert that a equals b.

a and b will be converted to void * before comparing.

Parameters
aValue to compare
bValue to compare
...Optional message and variables to print if the assertion fails

◆ zassert_false

#define zassert_false (   cond,
  ... 
)    zassert(!(cond), #cond " is true", ##__VA_ARGS__)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Assert that cond is false.

Parameters
condCondition to check
...Optional message and variables to print if the assertion fails

◆ zassert_is_null

#define zassert_is_null (   ptr,
  ... 
)    zassert((ptr) == NULL, #ptr " is not NULL", ##__VA_ARGS__)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Assert that ptr is NULL.

Parameters
ptrPointer to compare
...Optional message and variables to print if the assertion fails

◆ zassert_mem_equal

#define zassert_mem_equal (   ...)    zassert_mem_equal__(__VA_ARGS__)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Assert that 2 memory buffers have the same contents.

This macro calls the final memory comparison assertion macro. Using double expansion allows providing some arguments by macros that would expand to more than one values (ANSI-C99 defines that all the macro arguments have to be expanded before macro call).

Parameters
...Arguments, see zassert_mem_equal__ for real arguments accepted.

◆ zassert_mem_equal__

#define zassert_mem_equal__ (   buf,
  exp,
  size,
  ... 
)     zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, ##__VA_ARGS__)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Internal assert that 2 memory buffers have the same contents.

Note
This is internal macro, to be used as a second expansion. See zassert_mem_equal.
Parameters
bufBuffer to compare
expBuffer with expected contents
sizeSize of buffers
...Optional message and variables to print if the assertion fails

◆ zassert_not_equal

#define zassert_not_equal (   a,
  b,
  ... 
)    zassert((a) != (b), #a " equal to " #b, ##__VA_ARGS__)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Assert that a does not equal b.

a and b won't be converted and will be compared directly.

Parameters
aValue to compare
bValue to compare
...Optional message and variables to print if the assertion fails

◆ zassert_not_null

#define zassert_not_null (   ptr,
  ... 
)    zassert((ptr) != NULL, #ptr " is NULL", ##__VA_ARGS__)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Assert that ptr is not NULL.

Parameters
ptrPointer to compare
...Optional message and variables to print if the assertion fails

◆ zassert_not_ok

#define zassert_not_ok (   cond,
  ... 
)    zassert(!!(cond), #cond " is zero", ##__VA_ARGS__)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Assert that cond is not 0 (failure)

Parameters
condCondition to check
...Optional message and variables to print if the assertion fails

◆ zassert_ok

#define zassert_ok (   cond,
  ... 
)    zassert(!(cond), #cond " is non-zero", ##__VA_ARGS__)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Assert that cond is 0 (success)

Parameters
condCondition to check
...Optional message and variables to print if the assertion fails

◆ zassert_str_equal

#define zassert_str_equal (   s1,
  s2,
  ... 
)     zassert(strcmp(s1, s2) == 0, #s1 " not equal to " #s2, ##__VA_ARGS__)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Assert that 2 strings have the same contents.

Parameters
s1The first string
s2The second string
...Optional message and variables to print if the expectation fails

◆ zassert_true

#define zassert_true (   cond,
  ... 
)    zassert(cond, #cond " is false", ##__VA_ARGS__)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Assert that cond is true.

Parameters
condCondition to check
...Optional message and variables to print if the assertion fails

◆ zassert_unreachable

#define zassert_unreachable (   ...)    zassert(0, "Reached unreachable code", ##__VA_ARGS__)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Assert that this function call won't be reached.

Parameters
...Optional message and variables to print if the assertion fails

◆ zassert_within

#define zassert_within (   a,
  b,
  d,
  ... 
)

#include </home/runner/work/sdk-nrf/sdk-nrf/ncs/zephyr/subsys/testsuite/ztest/include/zephyr/ztest_assert.h>

Value:
zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), #a " not within " #b " +/- " #d, \
##__VA_ARGS__)
irp nz macro MOVR cc d
Definition: asm-macro-32-bit-gnu.h:11

Assert that a is within b with delta d.

Parameters
aValue to compare
bValue to compare
dDelta
...Optional message and variables to print if the assertion fails

◆ zassume

#define zassume (   cond,
  default_msg,
  ... 
)     _zassume_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))

◆ zexpect

#define zexpect (   cond,
  default_msg,
  ... 
)     _zexpect_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))