Commit 00696d87 authored by Chris Müller's avatar Chris Müller

test: uses Goodrich & Tamassia examples for Red-Black-Trees unittests.

parent b1340495
......@@ -19,40 +19,95 @@ test_map_initialization(const_pointer data)
static void
test_map_insertion(const_pointer data)
{
struct CryMap* map = cry_map_new(cry_cast(cry_ordering_funptr, strcmp));
const char** keys = cry_cast(const char**, 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);
while(*keys > 0) {
assert(cry_map_insert(map, keys, keys) == CRY_OKAY);
++keys;
}
assert(cry_map_size(map) == 7);
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);
assert(cry_map_lookup(map, "A") != 0);
assert(strcmp(cry_cast(const char*, cry_map_lookup(map, "A")), "A") == 0);
assert(cry_map_lookup(map, "B") != 0);
assert(strcmp(cry_cast(const char*, cry_map_lookup(map, "B")), "B") == 0);
assert(cry_map_lookup(map, "S") != 0);
assert(strcmp(cry_cast(const char*, cry_map_lookup(map, "S")), "S") == 0);
assert(cry_map_lookup(map, "F") == 0);
// 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
cry_test_maps(void)
{
const char* keys[] = { "A", "B", "C", "Z", "D", "S", "E", 0 };
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);
}
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