Skip to content

Commit b27c5d7

Browse files
asemjonovsnashif
authored andcommitted
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 <[email protected]>
1 parent 28bf1aa commit b27c5d7

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

subsys/testsuite/ztest/src/ztest_new.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -610,17 +610,22 @@ struct ztest_unit_test *z_ztest_get_next_test(const char *suite, struct ztest_un
610610
#ifdef CONFIG_ZTEST_SHUFFLE
611611
static void z_ztest_shuffle(void *dest[], intptr_t start, size_t num_items, size_t element_size)
612612
{
613+
void *tmp;
614+
615+
/* Initialize dest array */
613616
for (size_t i = 0; i < num_items; ++i) {
614-
int pos = sys_rand32_get() % num_items;
615-
const int start_pos = pos;
617+
dest[i] = (void *)(start + (i * element_size));
618+
}
616619

617-
/* Get the next valid position */
618-
while (dest[pos] != NULL) {
619-
pos = (pos + 1) % num_items;
620-
__ASSERT_NO_MSG(pos != start_pos);
621-
}
620+
/* Shuffle dest array */
621+
for (size_t i = num_items - 1; i > 0; i--) {
622+
int j = sys_rand32_get() % (i + 1);
622623

623-
dest[pos] = (void *)(start + (i * element_size));
624+
if (i != j) {
625+
tmp = dest[j];
626+
dest[j] = dest[i];
627+
dest[i] = tmp;
628+
}
624629
}
625630
}
626631
#endif /* CONFIG_ZTEST_SHUFFLE */

tests/ztest/base/prj_verbose_1.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ CONFIG_ZTEST_ASSERT_VERBOSE=1
55
CONFIG_ZTEST_SHUFFLE=y
66
CONFIG_ZTEST_SHUFFLE_SUITE_REPEAT_COUNT=2
77
CONFIG_ZTEST_SHUFFLE_TEST_REPEAT_COUNT=2
8+
CONFIG_ENTROPY_GENERATOR=y

0 commit comments

Comments
 (0)