ztest: Fix unused variable compile error in shuffle function

When CONFIG_ZTEST_SHUFFLE is enabled and ASSERTS are disabled
`start_pos` becomes an unused variable leading to a compile error.
Cleaned-up shuffling algorithm to not need a `start_pos` check.

Signed-off-by: Al Semjonovs <asemjonovs@google.com>
This commit is contained in:
Al Semjonovs 2022-08-17 09:52:15 -06:00 committed by Anas Nashif
commit b27c5d73ef
2 changed files with 14 additions and 8 deletions

View file

@ -610,17 +610,22 @@ struct ztest_unit_test *z_ztest_get_next_test(const char *suite, struct ztest_un
#ifdef CONFIG_ZTEST_SHUFFLE
static void z_ztest_shuffle(void *dest[], intptr_t start, size_t num_items, size_t element_size)
{
void *tmp;
/* Initialize dest array */
for (size_t i = 0; i < num_items; ++i) {
int pos = sys_rand32_get() % num_items;
const int start_pos = pos;
dest[i] = (void *)(start + (i * element_size));
}
/* Get the next valid position */
while (dest[pos] != NULL) {
pos = (pos + 1) % num_items;
__ASSERT_NO_MSG(pos != start_pos);
/* Shuffle dest array */
for (size_t i = num_items - 1; i > 0; i--) {
int j = sys_rand32_get() % (i + 1);
if (i != j) {
tmp = dest[j];
dest[j] = dest[i];
dest[i] = tmp;
}
dest[pos] = (void *)(start + (i * element_size));
}
}
#endif /* CONFIG_ZTEST_SHUFFLE */

View file

@ -5,3 +5,4 @@ CONFIG_ZTEST_ASSERT_VERBOSE=1
CONFIG_ZTEST_SHUFFLE=y
CONFIG_ZTEST_SHUFFLE_SUITE_REPEAT_COUNT=2
CONFIG_ZTEST_SHUFFLE_TEST_REPEAT_COUNT=2
CONFIG_ENTROPY_GENERATOR=y