--- a/test/map_tests.c Mon Feb 20 15:30:45 2012 +0100 +++ b/test/map_tests.c Tue Feb 21 01:13:17 2012 +0100 @@ -2,43 +2,72 @@ * */ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - #include "map_tests.h" -int map_tests() { - printf(" Test ucx_map_new\n"); +UCX_TEST_BEGIN(test_ucx_map_new) { UcxMap *map = ucx_map_new(16); - if(map == NULL) { - return -1; - } + + UCX_TEST_ASSERT(map->size == 16, "wrong size") + UCX_TEST_ASSERT(map->map != NULL, "failed") + + ucx_map_free(map); + + UCX_TEST_END +} - printf(" Test ucx_map_put\n"); - char *txt = "text/plain"; - char *xml = "text/xml"; - ucx_map_cstr_put(map, "txt", txt); - ucx_map_cstr_put(map, "xml", xml); +UCX_TEST_BEGIN(test_ucx_key) { + + UcxKey key = ucx_key("This is a text.", 15); + UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed") + UCX_TEST_ASSERT(key.len == 15, "failed") + UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed") + + UCX_TEST_END +} + +UCX_TEST_BEGIN(test_ucx_map_put) { + + UcxMap *map = ucx_map_new(4); + + int td[5]; + td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; - printf(" Test ucx_map_get\n"); - if(ucx_map_cstr_get(map, "txt") != txt) { - fprintf(stderr, "ucx_map_get failed\n"); - return -1; - } - char xmlkey[4]; - xmlkey[0] = 'x'; - xmlkey[1] = 'm'; - xmlkey[2] = 'l'; - xmlkey[3] = 0; - if(ucx_map_cstr_get(map, xmlkey) != xml) { - fprintf(stderr, "ucx_map_get failed\n"); - return -1; - } - if(ucx_map_cstr_get(map, "nokey") != NULL) { - fprintf(stderr, "ucx_map_get failed\n"); - return -1; - } + ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */ + ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */ + ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */ + ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */ + ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */ + + UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0") + UCX_TEST_ASSERT(map->map[0]->next != NULL, "no list at slot 0") + UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], "failed Key2") + UCX_TEST_ASSERT(map->map[0]->next->next != NULL, "list corrupt at slot 0") + UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4], + "failed Key4") + UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, + "slot 0 not terminated") + + UCX_TEST_ASSERT(map->map[1] == NULL, "slot 1 not terminated") + + UCX_TEST_ASSERT(*((int*)map->map[2]->data) == td[3], "failed KeY3") + UCX_TEST_ASSERT(map->map[2]->next == NULL, "slot 2 not terminated") - return 0; + UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1") + + ucx_map_cstr_put(map, "Key0", &td[3]); /* 0 */ + + UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[3], "overwrite failed") + UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], + "overwrite failed") + UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4], + "overwrite failed") + UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed") + + ucx_map_free(map); + + UCX_TEST_END } + +UCX_TEST_BEGIN(test_ucx_map_get) { + UCX_TEST_END +}