33 return *((
const int *)
ob);
39 return *((
const int *)
a) - *((
const int *)
b);
45 fail_unless(
test_heap != NULL,
"Failed to allocate minheap of size 1.");
46 fail_unless(
test_heap->
len == 0,
"Initialized empty heap has nonzero length.");
56 fail_unless(
result == 0,
"Minheap should have allowed 1 element, but allowed 0.");
58 fail_unless(
result == -1,
"Minheap of size 1 allowed 2 elements to be added.");
65 fail_unless(
result == NULL,
"Empty heap should not return a non-null value on remove.");
66 int vals[3] = {1, 2, 3};
69 fail_unless(
result == &vals[0],
"Heap should have returned the only value it was given (%d)", vals[0]);
71 fail_unless(
result == NULL,
"Heap should be empty, yet we returned a value on removal (len=%d)",
test_heap->
len);
74 fail_unless(insresult == -1,
"Heap should have been full, but insert was allowed (len=%d)",
test_heap->
len);
79 int vals[30] = {4, 6, 23, 7, 343, 12, 1, 1, 33, 76, 4, 34, 8, 7, 90, 123, 2, 3, 55, 65, 4, 6, 77, 65, 5, 7, 6, 5, 4, 1};
80 int expected[30], actual[30];
82 memcpy(expected, vals,
sizeof(
int)*30);
87 for (
int i = 0; i < 30; ++i) {
89 fail_unless(
result == 0,
"Failed to insert to the heap for index %d (%d).", i, vals[i]);
92 for (
int i = 0; i < 30; ++i) {
94 fail_unless(
result != NULL,
"Failed to remove from the heap after %d elements.", i);
96 fail_unless(expected[i] == actual[i],
"Heap did not gather data correctly. (%d != %d)", expected[i], actual[i]);
111 return diag * 3 + rem * 2;
120 Pos vals[400], expected[400], actual[400];
123 for (
int i = 0; i < 400; ++i) {
124 vals[i].
x = rand() & 16535;
125 vals[i].
y = rand() & 16535;
128 memcpy(expected, vals, 400 *
sizeof(
Pos));
132 for (
int i = 0; i < 400; ++i) {
134 fail_unless(
res == 0,
"Could not insert into minheap at size %d.",
test_heap->
len);
137 for (
int i = 0; i < 400; ++i) {
139 fail_unless(
res != NULL,
"Minheap emptied before it should have: %d items should be left.", 400-i);
143 "Minheap retrieved wrong value. Expected (%d, %d): %d, got (%d, %d): %d", expected[i].
x, expected[i].
y,
159 vals[i] = rand() & 32767;
162 memcpy(expected, vals,
HEAP_SIZE *
sizeof(
int));
168 fail_unless(
res == 0,
"Could not insert into minheap at size %d.", heap.
len);
174 fail_unless(ref != NULL,
"Minheap emptied before it should have: %d items should be left.",
HEAP_SIZE - i);
175 fail_unless(*ref == expected[i],
"Minheap retrieved wrong value. Expected %d, got %d.", expected[i], *ref);
181 Suite *s = suite_create(
"minheap");
182 TCase *tc_core = tcase_create(
"Core");
183 tcase_set_timeout(tc_core, 60);
188 suite_add_tcase(s, tc_core);
189 tcase_add_test(tc_core, test_minheap_allocate);
190 tcase_add_test(tc_core, test_minheap_overfill);
191 tcase_add_test(tc_core, test_minheap_empty_remove);
192 tcase_add_test(tc_core, test_minheap_insert_remove);
193 tcase_add_test(tc_core, test_minheap_with_struct);
194 tcase_add_test(tc_core, test_minheap_static_alloc);
202 SRunner *sr = srunner_create(s);
207 srunner_set_xml(sr, LOGDIR
"/unit/common/minheap.xml");
208 srunner_set_log(sr, LOGDIR
"/unit/common/minheap.out");
209 srunner_run_all(sr, CK_ENV);
210 nf = srunner_ntests_failed(sr);
212 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;