Check of count or size is 0 in heapsort

This commit is contained in:
Nils O. Selåsdal
2013-12-03 14:29:57 +01:00
parent 70fb352656
commit 4caf2573fb
2 changed files with 38 additions and 2 deletions
+9 -1
View File
@@ -29,9 +29,17 @@ void
uc_heapsort(void *base_, size_t count, size_t width,
uc_hs_cmp cmp, uc_hs_swp swap, void *cookie)
{
int start = count / 2 - 1, end = count - 1;
long start;
long end;
unsigned char *base = base_;
if (count == 0 || width == 0) {
return;
}
start = count / 2 - 1,
end = count - 1;
while (start >= 0) {
uc_sift(base, start, count, width, cmp, swap, cookie);
start--;
+29 -1
View File
@@ -81,16 +81,44 @@ START_TEST (test_heapsort_even)
uc_heapsort(unordered, ARRAY_SIZE(unordered), sizeof(struct Stuff),
cmp_stuff, swap_stuff, NULL);
print_stuff(unordered, ARRAY_SIZE(unordered));
fail_if(memcmp(unordered, ordered, sizeof unordered) != 0);
END_TEST
START_TEST (test_heapsort_zero)
struct Stuff unordered[] = {
{"1", 1},
{"7", 7},
{"3", 3},
{"4", 4},
{"6", 6},
{"2", 2},
};
struct Stuff same[] = {
{"1", 1},
{"7", 7},
{"3", 3},
{"4", 4},
{"6", 6},
{"2", 2},
};
uc_heapsort(unordered, 0, sizeof(struct Stuff),
cmp_stuff, swap_stuff, NULL);
fail_if(memcmp(unordered, same, sizeof unordered) != 0);
uc_heapsort(unordered, ARRAY_SIZE(unordered), 0,
cmp_stuff, swap_stuff, NULL);
fail_if(memcmp(unordered, same, sizeof unordered) != 0);
END_TEST
Suite *heapsort_suite(void)
{
Suite *s = suite_create("heapsort");
TCase *tc = tcase_create("heapsort tests");
tcase_add_test(tc, test_heapsort_odd);
tcase_add_test(tc, test_heapsort_even);
tcase_add_test(tc, test_heapsort_zero);
suite_add_tcase(s, tc);