Check of count or size is 0 in heapsort
This commit is contained in:
+9
-1
@@ -29,9 +29,17 @@ void
|
|||||||
uc_heapsort(void *base_, size_t count, size_t width,
|
uc_heapsort(void *base_, size_t count, size_t width,
|
||||||
uc_hs_cmp cmp, uc_hs_swp swap, void *cookie)
|
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_;
|
unsigned char *base = base_;
|
||||||
|
|
||||||
|
if (count == 0 || width == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
start = count / 2 - 1,
|
||||||
|
end = count - 1;
|
||||||
|
|
||||||
while (start >= 0) {
|
while (start >= 0) {
|
||||||
uc_sift(base, start, count, width, cmp, swap, cookie);
|
uc_sift(base, start, count, width, cmp, swap, cookie);
|
||||||
start--;
|
start--;
|
||||||
|
|||||||
+29
-1
@@ -81,16 +81,44 @@ START_TEST (test_heapsort_even)
|
|||||||
|
|
||||||
uc_heapsort(unordered, ARRAY_SIZE(unordered), sizeof(struct Stuff),
|
uc_heapsort(unordered, ARRAY_SIZE(unordered), sizeof(struct Stuff),
|
||||||
cmp_stuff, swap_stuff, NULL);
|
cmp_stuff, swap_stuff, NULL);
|
||||||
print_stuff(unordered, ARRAY_SIZE(unordered));
|
|
||||||
fail_if(memcmp(unordered, ordered, sizeof unordered) != 0);
|
fail_if(memcmp(unordered, ordered, sizeof unordered) != 0);
|
||||||
END_TEST
|
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 *heapsort_suite(void)
|
||||||
{
|
{
|
||||||
Suite *s = suite_create("heapsort");
|
Suite *s = suite_create("heapsort");
|
||||||
TCase *tc = tcase_create("heapsort tests");
|
TCase *tc = tcase_create("heapsort tests");
|
||||||
tcase_add_test(tc, test_heapsort_odd);
|
tcase_add_test(tc, test_heapsort_odd);
|
||||||
tcase_add_test(tc, test_heapsort_even);
|
tcase_add_test(tc, test_heapsort_even);
|
||||||
|
tcase_add_test(tc, test_heapsort_zero);
|
||||||
|
|
||||||
suite_add_tcase(s, tc);
|
suite_add_tcase(s, tc);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user