Skip to content

testsuite: ztest: Add validation of zassert strings #89482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 22, 2025
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
2 changes: 1 addition & 1 deletion submanifests/optional.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ manifest:
- optional
- name: zscilib
path: modules/lib/zscilib
revision: ee1b287d9dd07208d2cc52284240ac25bb66eae3
revision: ee3c0c405087e331aad16d167b6e4ec1c3452ba9
remote: upstream
groups:
- optional
Expand Down
8 changes: 8 additions & 0 deletions subsys/testsuite/ztest/include/zephyr/ztest_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ static inline bool z_zexpect(bool cond, const char *default_msg, const char *fil
_zassert_base(cond, default_msg, msg, ##__VA_ARGS__)

#define zassert(cond, default_msg, ...) \
IF_ENABLED(__VA_OPT__(1), ( \
if (0) { \
printf(__VA_ARGS__); \
})) \
_zassert_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))

/**
Expand Down Expand Up @@ -229,6 +233,10 @@ static inline bool z_zexpect(bool cond, const char *default_msg, const char *fil
_zassume_base(cond, default_msg, msg, ##__VA_ARGS__)

#define zassume(cond, default_msg, ...) \
IF_ENABLED(__VA_OPT__(1), ( \
if (0) { \
printf(__VA_ARGS__); \
})) \
_zassume_va(cond, default_msg, COND_CODE_1(__VA_OPT__(1), (__VA_ARGS__), (NULL)))

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ ZTEST(vector_table, test_arm_irq_vector_table)
}

zassert_true((k_sem_take(&sem[0], K_NO_WAIT) || k_sem_take(&sem[1], K_NO_WAIT) ||
k_sem_take(&sem[2], K_NO_WAIT)),
NULL);
k_sem_take(&sem[2], K_NO_WAIT)));

for (int ii = 0; ii < 3; ii++) {
#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE) || defined(CONFIG_SOC_TI_LM3S6965_QEMU)
Expand All @@ -129,8 +128,7 @@ ZTEST(vector_table, test_arm_irq_vector_table)
__ISB(); /* Ensure that IRQ is executed */

zassert_false((k_sem_take(&sem[0], K_NO_WAIT) || k_sem_take(&sem[1], K_NO_WAIT) ||
k_sem_take(&sem[2], K_NO_WAIT)),
NULL);
k_sem_take(&sem[2], K_NO_WAIT)));
}

typedef void (*vth)(void); /* Vector Table Handler */
Expand Down
6 changes: 2 additions & 4 deletions tests/arch/arm/arm_thread_swap/src/arm_thread_arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,8 @@ static void verify_fp_callee_saved(const struct _preempt_float *src,
(src->s27 == dst->s27) && (src->s28 == dst->s28) &&
(src->s29 == dst->s29) && (src->s30 == dst->s30) &&
(src->s31 == dst->s31),
" got: 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x"
" 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x\n"
" expected: 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x"
" 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x 0x%0x\n",
" got: %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n"
" expected: %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n",
(double)src->s16, (double)src->s17, (double)src->s18, (double)src->s19,
(double)src->s20, (double)src->s21, (double)src->s22, (double)src->s23,
(double)src->s24, (double)src->s25, (double)src->s26, (double)src->s27,
Expand Down
2 changes: 1 addition & 1 deletion tests/arch/arm/arm_tz_wrap_func/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ ZTEST(tz_wrap_func, test_tz_wrap_func)
msp1 = __get_MSP();
psp1 = __get_PSP();

zassert_equal(foo1_retval, wrap_foo1(foo1_arg1, foo1_arg2, foo1_arg3, foo1_arg4), NULL);
zassert_equal(foo1_retval, wrap_foo1(foo1_arg1, foo1_arg2, foo1_arg3, foo1_arg4));

zassert_equal(msp1, __get_MSP());
zassert_equal(psp1, __get_PSP());
Expand Down
8 changes: 4 additions & 4 deletions tests/arch/common/semihost/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ZTEST(semihost, test_file_ops)

/* Open in write mode */
fd = semihost_open(test_file, SEMIHOST_OPEN_WB);
zassert_true(fd > 0, "Bad handle (%d)", fd);
zassert_true(fd > 0, "Bad handle (%ld)", fd);
zassert_equal(semihost_flen(fd), 0, "File not empty");

/* Write some data */
Expand All @@ -35,12 +35,12 @@ ZTEST(semihost, test_file_ops)

/* Open the same file again for reading */
fd = semihost_open(test_file, SEMIHOST_OPEN_RB);
zassert_true(fd > 0, "Bad handle (%d)", fd);
zassert_true(fd > 0, "Bad handle (%ld)", fd);
zassert_equal(semihost_flen(fd), 2 * sizeof(w_buffer), "Data not preserved");

/* Check reading data */
read = semihost_read(fd, r_buffer, sizeof(r_buffer));
zassert_equal(read, sizeof(r_buffer), "Read failed %d", read);
zassert_equal(read, sizeof(r_buffer), "Read failed %ld", read);
zassert_mem_equal(r_buffer, w_buffer, sizeof(r_buffer), "Data not read");
read = semihost_read(fd, r_buffer, sizeof(r_buffer));
zassert_equal(read, sizeof(r_buffer), "Read failed");
Expand All @@ -63,7 +63,7 @@ ZTEST(semihost, test_file_ops)

/* Opening again in write mode should erase the file */
fd = semihost_open(test_file, SEMIHOST_OPEN_WB);
zassert_true(fd > 0, "Bad handle (%d)", fd);
zassert_true(fd > 0, "Bad handle (%ld)", fd);
zassert_equal(semihost_flen(fd), 0, "File not empty");
zassert_equal(semihost_close(fd), 0, "Close failed");
}
Expand Down
28 changes: 14 additions & 14 deletions tests/boards/intel_adsp/cache/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,46 @@ ZTEST(adsp_cache, test_adsp_cache_flush_inv_all)
*uncached = 40;

/* Just some sanity checks */
zassert_equal(*cached, 42, NULL);
zassert_equal(*uncached, 40, NULL);
zassert_equal(*cached, 42);
zassert_equal(*uncached, 40);

sys_cache_data_flush_and_invd_all();

/* After sys_cache_data_flush_and_invd_all(), uncached should be updated */
zassert_equal(*cached, 42, NULL);
zassert_equal(*uncached, 42, NULL);
zassert_equal(*cached, 42);
zassert_equal(*uncached, 42);

/* Flush and invalidate again, this time to check the invalidate part */
sys_cache_data_flush_and_invd_all();
*uncached = 80;

/* As cache is invalid, cached should be updated with uncached new value */
zassert_equal(*cached, 80, NULL);
zassert_equal(*uncached, 80, NULL);
zassert_equal(*cached, 80);
zassert_equal(*uncached, 80);

*cached = 82;

/* Only cached should have changed */
zassert_equal(*cached, 82, NULL);
zassert_equal(*uncached, 80, NULL);
zassert_equal(*cached, 82);
zassert_equal(*uncached, 80);

sys_cache_data_flush_all();

/* After sys_cache_data_flush_all(), uncached should be updated */
zassert_equal(*cached, 82, NULL);
zassert_equal(*uncached, 82, NULL);
zassert_equal(*cached, 82);
zassert_equal(*uncached, 82);

*uncached = 100;

/* As cache is not invalid, only uncached should be updated */
zassert_equal(*cached, 82, NULL);
zassert_equal(*uncached, 100, NULL);
zassert_equal(*cached, 82);
zassert_equal(*uncached, 100);

sys_cache_data_invd_all();

/* Now, cached should be updated */
zassert_equal(*cached, 100, NULL);
zassert_equal(*uncached, 100, NULL);
zassert_equal(*cached, 100);
zassert_equal(*uncached, 100);
}

ZTEST_SUITE(adsp_cache, NULL, NULL, NULL, NULL, NULL);
2 changes: 1 addition & 1 deletion tests/boards/intel_adsp/smoke/src/cpus.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static void core_smoke(void *arg)
int32_t diff = MAX(1, abs(clk_ratios[i] - clk_ratios[cpu]));

zassert_true((clk_ratios[cpu] / diff) > 100,
"clocks off by more than 1%");
"clocks off by more than 1%%");
}

/* Check tight loop performance to validate instruction cache */
Expand Down
46 changes: 18 additions & 28 deletions tests/boards/native_sim/cpu_wait/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ ZTEST(native_cpu_hold, test_cpu_hold_basic)
k_busy_wait(wait_times[i]);
time2 = posix_get_hw_cycle();
zassert_true(time2 - time1 == wait_times[i],
"k_busy_wait failed "
PRIu64"-"PRIu64"!="PRIu32"\n",
"k_busy_wait failed %" PRIu64 "-%" PRIu64 "!=%" PRIu32 "\n",
time2, time1, wait_times[i]);
time1 = time2;
}
Expand All @@ -39,8 +38,7 @@ ZTEST(native_cpu_hold, test_cpu_hold_basic)
posix_cpu_hold(wait_times[i]);
time2 = posix_get_hw_cycle();
zassert_true(time2 - time1 == wait_times[i],
"posix_cpu_hold failed "
PRIu64"-"PRIu64"!="PRIu32"\n",
"posix_cpu_hold failed %" PRIu64 "-%" PRIu64 "!=%" PRIu32 "\n",
time2, time1, wait_times[i]);
time1 = time2;
}
Expand Down Expand Up @@ -125,9 +123,8 @@ ZTEST(native_cpu_hold, test_cpu_hold_with_another_thread)
time2 = posix_get_hw_cycle();

zassert_true(time2 - time1 == TWO_TICKS_TIME + WASTED_TIME,
"k_busy_wait failed "
PRIu64"-"PRIu64"!="PRIu32"\n",
time2, time1, TWO_TICKS_TIME + WASTED_TIME);
"k_busy_wait failed %" PRIu64 "-%" PRIu64 "!=%" PRIu32 "\n",
time2, time1, (uint32_t)(TWO_TICKS_TIME + WASTED_TIME));

k_sem_take(&end_sema, K_FOREVER);

Expand All @@ -143,9 +140,8 @@ ZTEST(native_cpu_hold, test_cpu_hold_with_another_thread)
time2 = posix_get_hw_cycle();

zassert_true(time2 - time1 == TWO_AND_HALF_TICKS,
"k_busy_wait failed "
PRIu64"-"PRIu64"!="PRIu32"\n",
time2, time1, TWO_AND_HALF_TICKS);
"k_busy_wait failed %" PRIu64 "-%" PRIu64 "!=%" PRIu32"\n",
time2, time1, (uint32_t)TWO_AND_HALF_TICKS);

k_sem_take(&end_sema, K_FOREVER);

Expand All @@ -163,9 +159,8 @@ ZTEST(native_cpu_hold, test_cpu_hold_with_another_thread)
time2 = posix_get_hw_cycle();

zassert_true(time2 - time1 == TWO_TICKS_TIME + WASTED_TIME + 1,
"k_busy_wait failed "
PRIu64"-"PRIu64"!="PRIu32"\n",
time2, time1, TWO_TICKS_TIME + WASTED_TIME + 1);
"k_busy_wait failed %" PRIu64 "-%" PRIu64 "!=%" PRIu32 "\n",
time2, time1, (uint32_t)(TWO_TICKS_TIME + WASTED_TIME + 1));

k_sem_take(&end_sema, K_FOREVER);

Expand All @@ -183,9 +178,8 @@ ZTEST(native_cpu_hold, test_cpu_hold_with_another_thread)
time2 = posix_get_hw_cycle();

zassert_true(time2 - time1 == TWO_AND_HALF_TICKS + WASTED_TIME,
"k_busy_wait failed "
PRIu64"-"PRIu64"!="PRIu32"\n",
time2, time1, TWO_AND_HALF_TICKS + WASTED_TIME);
"k_busy_wait failed %" PRIu64 "-%" PRIu64 "!=%" PRIu32 "\n",
time2, time1, (uint32_t)(TWO_AND_HALF_TICKS + WASTED_TIME));

k_sem_take(&end_sema, K_FOREVER);
}
Expand Down Expand Up @@ -234,9 +228,8 @@ ZTEST(native_cpu_hold, test_cpu_hold_with_interrupts)
time2 = posix_get_hw_cycle();

zassert_true(time2 - time1 == ONE_TICK_TIME + WASTED_TIME,
"k_busy_wait failed "
PRIu64"-"PRIu64"!="PRIu32"\n",
time2, time1, ONE_TICK_TIME);
"k_busy_wait failed %" PRIu64 "-%" PRIu64 "!=%" PRIu32 "\n",
time2, time1, (uint32_t)ONE_TICK_TIME);


k_sleep(Z_TIMEOUT_TICKS(1)); /* Wait until tick boundary */
Expand All @@ -250,9 +243,8 @@ ZTEST(native_cpu_hold, test_cpu_hold_with_interrupts)
time2 = posix_get_hw_cycle();

zassert_true(time2 - time1 == ONE_AND_HALF_TICKS,
"k_busy_wait failed "
PRIu64"-"PRIu64"!="PRIu32"\n",
time2, time1, ONE_TICK_TIME);
"k_busy_wait failed %" PRIu64 "-%" PRIu64 "!=%" PRIu32 "\n",
time2, time1, (uint32_t)ONE_TICK_TIME);



Expand All @@ -267,9 +259,8 @@ ZTEST(native_cpu_hold, test_cpu_hold_with_interrupts)
time2 = posix_get_hw_cycle();

zassert_true(time2 - time1 == ONE_TICK_TIME + 1 + WASTED_TIME,
"k_busy_wait failed "
PRIu64"-"PRIu64"!="PRIu32"\n",
time2, time1, ONE_TICK_TIME);
"k_busy_wait failed %" PRIu64 "-%" PRIu64 "!=%" PRIu32 "\n",
time2, time1, (uint32_t)ONE_TICK_TIME);


k_sleep(Z_TIMEOUT_TICKS(1)); /* Wait until tick boundary */
Expand All @@ -283,9 +274,8 @@ ZTEST(native_cpu_hold, test_cpu_hold_with_interrupts)
time2 = posix_get_hw_cycle();

zassert_true(time2 - time1 == ONE_AND_HALF_TICKS + WASTED_TIME,
"k_busy_wait failed "
PRIu64"-"PRIu64"!="PRIu32"\n",
time2, time1, ONE_TICK_TIME);
"k_busy_wait failed %" PRIu64 "-%" PRIu64 "!=%" PRIu32 "\n",
time2, time1, (uint32_t)ONE_TICK_TIME);

#endif /* defined(CONFIG_BOARD_NATIVE_SIM) */
}
Expand Down
2 changes: 1 addition & 1 deletion tests/drivers/bbram/emul/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ZTEST(bbram, test_read_write)
/* Set and verify content. */
zassert_ok(bbram_write(dev, 0, BBRAM_SIZE, expected));
zassert_ok(bbram_read(dev, 0, BBRAM_SIZE, buffer));
zassert_mem_equal(buffer, expected, BBRAM_SIZE, NULL);
zassert_mem_equal(buffer, expected, BBRAM_SIZE);
}

ZTEST(bbram, test_set_invalid)
Expand Down
2 changes: 1 addition & 1 deletion tests/drivers/can/api/src/canfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ ZTEST_USER(canfd, test_invalid_sample_point)
int err;

err = can_calc_timing_data(can_dev, &timing, TEST_BITRATE_3, 1000);
zassert_equal(err, -EINVAL, "invalid sample point of 100.0% accepted (err %d)", err);
zassert_equal(err, -EINVAL, "invalid sample point of 100.0%% accepted (err %d)", err);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/drivers/can/api/src/classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ ZTEST_USER(can_classic, test_invalid_sample_point)
int err;

err = can_calc_timing(can_dev, &timing, TEST_BITRATE_1, 1000);
zassert_equal(err, -EINVAL, "invalid sample point of 100.0% accepted (err %d)", err);
zassert_equal(err, -EINVAL, "invalid sample point of 100.0%% accepted (err %d)", err);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ ZTEST(stm32_sysclck_config, test_sysclk_src)
#if STM32_SYSCLK_SRC_PLL
zassert_equal(RCC_SYSCLKSOURCE_STATUS_PLLCLK, sys_clk_src,
"Expected sysclk src: PLL (0x%x). Actual: 0x%x",
RCC_SYSCLKSOURCE_STATUS_PLLCLK, sys_clk_src);
(int)RCC_SYSCLKSOURCE_STATUS_PLLCLK, sys_clk_src);
#elif STM32_SYSCLK_SRC_HSE
zassert_equal(RCC_SYSCLKSOURCE_STATUS_HSE, sys_clk_src,
"Expected sysclk src: HSE (0x%x). Actual: 0x%x",
RCC_SYSCLKSOURCE_STATUS_HSE, sys_clk_src);
(int)RCC_SYSCLKSOURCE_STATUS_HSE, sys_clk_src);
#elif STM32_SYSCLK_SRC_HSI
zassert_equal(RCC_SYSCLKSOURCE_STATUS_HSI, sys_clk_src,
"Expected sysclk src: HSI (0x%x). Actual: 0x%x",
RCC_SYSCLKSOURCE_STATUS_HSI, sys_clk_src);
(int)RCC_SYSCLKSOURCE_STATUS_HSI, sys_clk_src);
#elif STM32_SYSCLK_SRC_MSI
zassert_equal(RCC_SYSCLKSOURCE_STATUS_MSI, sys_clk_src,
"Expected sysclk src: MSI (0x%x). Actual: 0x%x",
RCC_SYSCLKSOURCE_STATUS_MSI, sys_clk_src);
(int)RCC_SYSCLKSOURCE_STATUS_MSI, sys_clk_src);
#else
/* Case not expected */
zassert_true((STM32_SYSCLK_SRC_PLL ||
Expand All @@ -69,21 +69,21 @@ ZTEST(stm32_sysclck_config, test_pll_src)
#if STM32_PLL_SRC_HSE
zassert_equal(RCC_PLLSOURCE_HSE, pll_src,
"Expected PLL src: HSE (%d). Actual PLL src: %d",
RCC_PLLSOURCE_HSE, pll_src);
(uint32_t)RCC_PLLSOURCE_HSE, pll_src);
#elif STM32_PLL_SRC_HSI
#if defined(CONFIG_SOC_SERIES_STM32F1X)
zassert_equal(RCC_PLLSOURCE_HSI_DIV2, pll_src,
"Expected PLL src: HSI (%d). Actual PLL src: %d",
RCC_PLLSOURCE_HSI_DIV2, pll_src);
(uint32_t)RCC_PLLSOURCE_HSI_DIV2, pll_src);
#else
zassert_equal(RCC_PLLSOURCE_HSI, pll_src,
"Expected PLL src: HSI (%d). Actual PLL src: %d",
RCC_PLLSOURCE_HSI, pll_src);
(uint32_t)RCC_PLLSOURCE_HSI, pll_src);
#endif /* CONFIG_SOC_SERIES_STM32F1X */
#elif STM32_PLL_SRC_MSI
zassert_equal(RCC_PLLSOURCE_MSI, pll_src,
"Expected PLL src: MSI (%d). Actual PLL src: %d",
RCC_PLLSOURCE_MSI, pll_src);
(uint32_t)RCC_PLLSOURCE_MSI, pll_src);
#else /* --> RCC_PLLSOURCE_NONE */
#if defined(CONFIG_SOC_SERIES_STM32L0X) || defined(CONFIG_SOC_SERIES_STM32L1X) || \
defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32F1X) || \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ ZTEST(stm32_common_devices_clocks, test_adc_clk_config)
switch (pclken[1].bus) {
case STM32_SRC_SYSCLK:
zassert_equal(dev_actual_clk_src, ADC_SOURCE_SYSCLK,
"Expected ADC1 src: SYSCLK (0x%lx). Actual ADC1 src: 0x%x",
ADC_SOURCE_SYSCLK, dev_actual_clk_src);
"Expected ADC1 src: SYSCLK (0x%x). Actual ADC1 src: 0x%x",
(uint32_t)ADC_SOURCE_SYSCLK, dev_actual_clk_src);
break;
#if defined(STM32_SRC_PLL_P)
case STM32_SRC_PLL_P:
zassert_equal(dev_actual_clk_src, ADC_SOURCE_PLL,
"Expected ADC1 src: PLL (0x%lx). Actual ADC1 src: 0x%x",
ADC_SOURCE_PLL, dev_actual_clk_src);
"Expected ADC1 src: PLL (0x%x). Actual ADC1 src: 0x%x",
(uint32_t)ADC_SOURCE_PLL, dev_actual_clk_src);
break;
#endif /* STM32_SRC_PLL_P */
default:
Expand Down
Loading