Commit d5ae42aa authored by Christopher Friedt's avatar Christopher Friedt Committed by Carles Cufi
Browse files

tests: lib: c_lib: add coverage for qsort_r



A user previously reported that `qsort_r()` did not have test
coverage. Prior to 845a200c
`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: default avatarChristopher Friedt <chrisfriedt@gmail.com>
parent ed2ea2c7
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -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);
}
+24 −0
Original line number Diff line number Diff line
@@ -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");
}