Commit ff90c24e authored by Chris Müller's avatar Chris Müller
Browse files

Add testcases for stack and queue.

parent c4811ff5
......@@ -2,7 +2,7 @@ set(TEST crystal_unittests)
set(SOURCES
main.c
matrix.c
structures.c
)
......
#include "unittest.h"
void cry_test_matrix();
void cry_test_structures();
int
main(int argc, char** argv)
{
cry_unittest_initialize(argc, argv);
cry_test_matrix();
cry_test_structures();
cry_unittest_finalize();
......
#include "unittest.h"
#include <assert.h>
#include "structures/stack.h"
#include "structures/queue.h"
static void
test_stack(const_pointer data)
{
int items[] = { 1, 2, 3 };
struct CryStack* stack = cry_stack_new();
assert(cry_stack_size(stack) == 0);
assert(cry_stack_top(stack) == 0);
// pushing items to stack
cry_stack_push(stack, items);
assert(cry_stack_size(stack) == 1);
assert(cry_stack_top(stack) == items);
cry_stack_push(stack, items + 1);
assert(cry_stack_size(stack) == 2);
assert(cry_stack_top(stack) == items + 1);
cry_stack_push(stack, items + 2);
// popping items from stack
assert(cry_stack_size(stack) == 3);
assert(cry_stack_top(stack) == items + 2);
assert(cry_stack_pop(stack) == items + 2);
assert(cry_stack_size(stack) == 2);
assert(cry_stack_top(stack) == items + 1);
assert(cry_stack_pop(stack) == items + 1);
assert(cry_stack_size(stack) == 1);
assert(cry_stack_top(stack) == items);
assert(cry_stack_pop(stack) == items);
assert(cry_stack_size(stack) == 0);
assert(cry_stack_top(stack) == 0);
// cleanup
cry_stack_free(stack, 0);
}
static void
test_queue(const_pointer data)
{
int items[] = { 1, 2, 3 };
struct CryQueue* queue = cry_queue_new();
assert(cry_queue_size(queue) == 0);
assert(cry_queue_front(queue) == 0);
// add items to queue
cry_queue_enqueue(queue, items);
assert(cry_queue_front(queue) == items);
assert(cry_queue_size(queue) == 1);
cry_queue_enqueue(queue, items + 1);
assert(cry_queue_front(queue) == items);
assert(cry_queue_size(queue) == 2);
cry_queue_enqueue(queue, items + 2);
assert(cry_queue_front(queue) == items);
assert(cry_queue_size(queue) == 3);
// remove items from queue
assert(cry_queue_dequeue(queue) == items);
assert(cry_queue_size(queue) == 2);
assert(cry_queue_front(queue) == items + 1);
assert(cry_queue_dequeue(queue) == items + 1);
assert(cry_queue_size(queue) == 1);
assert(cry_queue_front(queue) == items + 2);
assert(cry_queue_dequeue(queue) == items + 2);
assert(cry_queue_size(queue) == 0);
assert(cry_queue_front(queue) == 0);
cry_queue_free(queue, 0);
}
cry_test_structures(void)
{
cry_unittest_run("structures.stack", test_stack, 0, 10);
cry_unittest_run("structures.queue", test_queue, 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