Commit b1e27118 authored by Chris Müller's avatar Chris Müller

test: set tests for red_black_trees (rename from map)

parent 0a3aab15
...@@ -4,6 +4,7 @@ set(SOURCES ...@@ -4,6 +4,7 @@ set(SOURCES
main.c main.c
array.c array.c
map.c map.c
red_black_tree.c
list.c list.c
structures.c structures.c
) )
......
...@@ -5,6 +5,7 @@ void cry_test_structures(); ...@@ -5,6 +5,7 @@ void cry_test_structures();
void cry_test_arrays(); void cry_test_arrays();
void cry_test_red_black_trees(); void cry_test_red_black_trees();
void cry_test_maps(); void cry_test_maps();
void cry_test_lists();
int int
main(int argc, char** argv) main(int argc, char** argv)
...@@ -14,6 +15,7 @@ main(int argc, char** argv) ...@@ -14,6 +15,7 @@ main(int argc, char** argv)
cry_test_structures(); cry_test_structures();
cry_test_arrays(); cry_test_arrays();
cry_test_red_black_trees(); cry_test_red_black_trees();
cry_test_lists();
cry_test_maps(); cry_test_maps();
cry_unittest_finalize(); cry_unittest_finalize();
......
#include "unittest.h" #include "unittest.h"
#include "structures/map.h"
#include <assert.h> #include <assert.h>
#include <string.h>
static void
test_map_initialization(const_pointer data)
{
struct CryMap* map = cry_map_new(cry_cast(cry_ordering_funptr, strcmp));
assert(cry_map_size(map) == 0);
assert(cry_map_lookup(map, "unknown_key") == 0);
cry_map_free(map, 0, 0);
}
static void
test_map_insertion(const_pointer data)
{
struct CryMap* map = cry_map_new(cry_int_compare);
const int* keys = cry_cast(const int*, data);
assert(cry_map_size(map) == 0);
while(*keys > 0) {
assert(cry_map_insert(map, keys, keys) == CRY_OKAY);
++keys;
}
assert(cry_map_size(map) == 10);
keys = cry_cast(const int*, data);
while(*keys > 0) {
assert(cry_map_lookup(map, keys) == keys);
++keys;
}
cry_map_free(map, 0, 0);
}
static void
test_map_removal(const_pointer data)
{
struct CryMap* map = cry_map_new(cry_int_compare);
const int* keys = cry_cast(const int*, data);
pointer value = 0;
assert(cry_map_size(map) == 0);
while(*keys > 0) {
assert(cry_map_insert(map, keys, keys) == CRY_OKAY);
++keys;
}
keys = cry_cast(const int*, data);
assert(cry_map_size(map) == 10);
// remove 3
value = cry_map_remove(map, keys + 4, 0);
assert(value != 0 && value == keys + 4);
// remove 12
value = cry_map_remove(map, keys + 2, 0);
assert(value != 0 && value == keys + 2);
// remove 17
value = cry_map_remove(map, keys + 9, 0);
assert(value != 0 && value == keys + 9);
// remove 18
value = cry_map_remove(map, keys + 7, 0);
assert(value != 0 && value == keys + 7);
// remove 15
value = cry_map_remove(map, keys + 3, 0);
assert(value != 0 && value == keys + 3);
// remove 16
value = cry_map_remove(map, keys + 8, 0);
assert(value != 0 && value == keys + 8);
// contains 4
assert(cry_map_lookup(map, keys + 0) == keys + 0);
// contains 7
assert(cry_map_lookup(map, keys + 1) == keys + 1);
// contains 5
assert(cry_map_lookup(map, keys + 5) == keys + 5);
// contains 14
assert(cry_map_lookup(map, keys + 6) == keys + 6);
cry_map_free(map, 0, 0);
}
void void
cry_test_maps(void) cry_test_maps(void)
{ {
int keys[] = {4, 7, 12, 15, 3, 5, 14, 18, 16, 17, -1 };
cry_unittest_run("structures.map.initialization", test_map_initialization, 0, 10);
cry_unittest_run("structures.map.insertion", test_map_insertion, keys, 10);
cry_unittest_run("structures.map.removal", test_map_removal, keys, 10);
} }
#include "unittest.h"
#include "structures/red_black_tree.h"
#include <assert.h>
#include <stdio.h>
static void
test_red_black_tree_initialization(const_pointer data)
{
struct RBTree* tree = rb_tree_new(cry_int_compare);
assert(tree->nodes == 0);
assert(rb_tree_lookup(tree, "unknown_key") == 0);
rb_tree_clear(tree, 0, 0);
cry_free(tree);
}
static void
test_red_black_tree_insertion(const_pointer data)
{
struct RBTree* tree = rb_tree_new(cry_int_compare);
const int* keys = cry_cast(const int*, data);
assert(tree->nodes == 0);
while(*keys > 0) {
assert(rb_tree_insert(tree, keys, cry_cast(int*, keys)) != 0);
++keys;
}
assert(tree->nodes == 10);
keys = cry_cast(const int*, data);
while(*keys > 0) {
struct RBNode* node = rb_tree_lookup(tree, keys);
assert(node != 0);
assert(node->key == keys);
++keys;
}
rb_tree_clear(tree, 0, 0);
cry_free(tree);
}
static void
test_red_black_tree_removal(const_pointer list)
{
struct RBTree* tree = rb_tree_new(cry_int_compare);
const int* keys = cry_cast(const int*, list);
struct RBNode* data = 0;
assert(tree->nodes == 0);
while(*keys > 0) {
assert(rb_tree_insert(tree, keys, cry_cast(int*, keys)) != 0);
++keys;
}
assert(tree->nodes == 10);
keys = cry_cast(const int*, list);
// remove 3
data = rb_tree_remove(tree, keys + 4);
assert(data != 0 && data->key == keys + 4);
rb_node_free(data, 0, 0);
// remove 12
data = rb_tree_remove(tree, keys + 2);
assert(data != 0 && data->key == keys + 2);
rb_node_free(data, 0, 0);
// remove 17
data = rb_tree_remove(tree, keys + 9);
assert(data != 0 && data->key == keys + 9);
rb_node_free(data, 0, 0);
// remove 18
data = rb_tree_remove(tree, keys + 7);
assert(data != 0 && data->key == keys + 7);
rb_node_free(data, 0, 0);
// remove 15
data = rb_tree_remove(tree, keys + 3);
assert(data != 0 && data->key == keys + 3);
rb_node_free(data, 0, 0);
// remove 16
data = rb_tree_remove(tree, keys + 8);
assert(data != 0 && data->key == keys + 8);
rb_node_free(data, 0, 0);
// contains 4
assert(rb_tree_lookup(tree, keys) != 0);
// contains 7
assert(rb_tree_lookup(tree, keys + 1) != 0);
// contains 5
assert(rb_tree_lookup(tree, keys + 5) != 0);
// contains 14
assert(rb_tree_lookup(tree, keys + 6) != 0);
rb_tree_clear(tree, 0, 0);
cry_free(tree);
}
void
cry_test_red_black_trees(void)
{
int keys[] = {4, 7, 12, 15, 3, 5, 14, 18, 16, 17, -1 };
cry_unittest_run("structures.red_black_tree.insertion", test_red_black_tree_insertion, keys, 10);
cry_unittest_run("structures.red_black_tree.removal", test_red_black_tree_removal, keys, 10);
cry_unittest_run("structures.red_black_tree.initialization", test_red_black_tree_initialization, 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