Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/zephyr/sys/util_macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ extern "C" {
*
* @return Nth argument.
*/
#define GET_ARG_N(N, ...) Z_GET_ARG_##N(__VA_ARGS__)
#define GET_ARG_N(N, ...) UTIL_CAT(Z_GET_ARG_, N)(__VA_ARGS__)

/**
* @brief Strips n first arguments from the argument list.
Expand All @@ -398,7 +398,7 @@ extern "C" {
*
* @return argument list without N first arguments.
*/
#define GET_ARGS_LESS_N(N, ...) Z_GET_ARGS_LESS_##N(__VA_ARGS__)
#define GET_ARGS_LESS_N(N, ...) UTIL_CAT(Z_GET_ARGS_LESS_, N)(__VA_ARGS__)

/**
* @brief Like <tt>a || b</tt>, but does evaluation and
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/util/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,20 +534,25 @@ ZTEST(util, test_nested_FOR_EACH) {
zassert_equal(a2, 2);
}

#define TWO 2 /* to showcase that GET_ARG_N and GET_ARGS_LESS_N also work with macros */

ZTEST(util, test_GET_ARG_N) {
int a = GET_ARG_N(1, 10, 100, 1000);
int b = GET_ARG_N(2, 10, 100, 1000);
int c = GET_ARG_N(3, 10, 100, 1000);
int d = GET_ARG_N(TWO, 10, 100, 1000);

zassert_equal(a, 10);
zassert_equal(b, 100);
zassert_equal(c, 1000);
zassert_equal(d, 100);
}

ZTEST(util, test_GET_ARGS_LESS_N) {
uint8_t a[] = { GET_ARGS_LESS_N(0, 1, 2, 3) };
uint8_t b[] = { GET_ARGS_LESS_N(1, 1, 2, 3) };
uint8_t c[] = { GET_ARGS_LESS_N(2, 1, 2, 3) };
uint8_t d[] = { GET_ARGS_LESS_N(TWO, 1, 2, 3) };

zassert_equal(sizeof(a), 3);

Expand All @@ -557,6 +562,9 @@ ZTEST(util, test_GET_ARGS_LESS_N) {

zassert_equal(sizeof(c), 1);
zassert_equal(c[0], 3);

zassert_equal(sizeof(d), 1);
zassert_equal(d[0], 3);
}

ZTEST(util, test_mixing_GET_ARG_and_FOR_EACH) {
Expand Down