tests: lib: c_lib: add coverage for qsort_r

A user previously reported that `qsort_r()` did not have test
coverage. Prior to 845a200c1b
`qsort()` was actually just calling `qsort_r()` inline.

There is still virtually no difference between the two
sorting routines, but it would be good to add coverage.

Relates-to #44218

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
This commit is contained in:
Christopher Friedt 2022-03-25 10:46:26 -04:00 committed by Carles Cufí
parent ed2ea2c7df
commit d5ae42aa1d
2 changed files with 34 additions and 1 deletions

View file

@ -1206,6 +1206,14 @@ void test_exit(void)
*/
extern void test_qsort(void);
/**
*
* @brief Test qsort_r function
*
* @see qsort_r()
*/
extern void test_qsort_r(void);
/**
* @}
*/
@ -1247,7 +1255,8 @@ void test_main(void)
ztest_unit_test(test_str_operate),
ztest_unit_test(test_tolower_toupper),
ztest_unit_test(test_strtok_r),
ztest_unit_test(test_qsort)
ztest_unit_test(test_qsort),
ztest_unit_test(test_qsort_r)
);
ztest_run_test_suite(test_c_lib);
}

View file

@ -121,3 +121,27 @@ void test_qsort(void)
"size 93 not sorted");
}
}
static int compare_ints_with_boolp_arg(const void *a, const void *b, void *argp)
{
int aa = *(const int *)a;
int bb = *(const int *)b;
*(bool *)argp = true;
return (aa > bb) - (aa < bb);
}
void test_qsort_r(void)
{
bool arg = false;
const int expect_int[] = { 1, 5, 7 };
int actual_int[] = { 1, 7, 5 };
qsort_r(actual_int, ARRAY_SIZE(actual_int), sizeof(actual_int[0]),
compare_ints_with_boolp_arg, &arg);
zassert_mem_equal(actual_int, expect_int, sizeof(expect_int), "array not sorted");
zassert_true(arg, "arg not modified");
}