Commit 321f424a authored by Chris Müller's avatar Chris Müller

test: add testcode for min- and max heaps.

parent 9ce59569
#include "unittest.h"
#include <assert.h>
#include "structures/list.h"
#include <stdio.h>
#include "structures/stack.h"
#include "structures/queue.h"
#include "structures/array.h"
#include "structures/heap.h"
static void
......@@ -91,6 +91,50 @@ test_queue(const_pointer data)
cry_queue_free(queue, 0);
}
static void
test_heap(const_pointer data)
{
int items[] = { 7, 5, 2, 10, 3 };
struct CryHeap* min_heap = cry_heap_new(5, cry_int_compare, CRY_MIN);
struct CryHeap* max_heap = cry_heap_new(5, cry_int_compare, CRY_MAX);
assert(cry_heap_size(min_heap) == 0);
assert(cry_heap_size(max_heap) == 0);
cry_heap_push(min_heap, items);
cry_heap_push(min_heap, items + 1);
cry_heap_push(min_heap, items + 2);
cry_heap_push(min_heap, items + 3);
cry_heap_push(min_heap, items + 4);
cry_heap_push(max_heap, items);
cry_heap_push(max_heap, items + 1);
cry_heap_push(max_heap, items + 2);
cry_heap_push(max_heap, items + 3);
cry_heap_push(max_heap, items + 4);
assert(cry_heap_size(min_heap) == 5);
assert(cry_heap_size(max_heap) == 5);
assert(*cry_cast(int*, cry_heap_pop(min_heap)) == 2);
assert(*cry_cast(int*, cry_heap_pop(min_heap)) == 3);
assert(*cry_cast(int*, cry_heap_pop(min_heap)) == 5);
assert(*cry_cast(int*, cry_heap_pop(min_heap)) == 7);
assert(*cry_cast(int*, cry_heap_pop(min_heap)) == 10);
assert(*cry_cast(int*, cry_heap_pop(max_heap)) == 10);
assert(*cry_cast(int*, cry_heap_pop(max_heap)) == 7);
assert(*cry_cast(int*, cry_heap_pop(max_heap)) == 5);
assert(*cry_cast(int*, cry_heap_pop(max_heap)) == 3);
assert(*cry_cast(int*, cry_heap_pop(max_heap)) == 2);
assert(cry_heap_size(min_heap) == 0);
assert(cry_heap_size(max_heap) == 0);
cry_heap_free(min_heap);
cry_heap_free(max_heap);
}
void
......@@ -98,4 +142,5 @@ cry_test_structures(void)
{
cry_unittest_run("structures.stack", test_stack, 0, 10);
cry_unittest_run("structures.queue", test_queue, 0, 10);
cry_unittest_run("structures.heap", test_heap, 0, 10);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment