olaf@20: /* olaf@20: * olaf@20: */ olaf@20: olaf@20: #include "map_tests.h" olaf@20: universe@33: UCX_TEST_IMPLEMENT(test_ucx_map_new) { olaf@20: UcxMap *map = ucx_map_new(16); universe@33: UCX_TEST_BEGIN universe@29: UCX_TEST_ASSERT(map->size == 16, "wrong size") universe@29: UCX_TEST_ASSERT(map->map != NULL, "failed") universe@29: universe@33: UCX_TEST_END universe@29: ucx_map_free(map); universe@29: } olaf@20: universe@33: UCX_TEST_IMPLEMENT(test_ucx_key) { universe@29: UcxKey key = ucx_key("This is a text.", 15); universe@33: UCX_TEST_BEGIN universe@29: UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed") universe@29: UCX_TEST_ASSERT(key.len == 15, "failed") universe@29: UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed") universe@29: universe@29: UCX_TEST_END universe@29: } olaf@20: universe@33: UCX_TEST_IMPLEMENT(test_ucx_map_put) { universe@29: universe@29: UcxMap *map = ucx_map_new(4); universe@29: universe@29: int td[5]; universe@29: td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; olaf@20: universe@33: UCX_TEST_BEGIN universe@29: ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */ universe@29: ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */ universe@29: ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */ universe@29: ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */ universe@29: ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */ universe@29: universe@29: UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0") universe@29: UCX_TEST_ASSERT(map->map[0]->next != NULL, "no list at slot 0") universe@29: UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], "failed Key2") universe@29: UCX_TEST_ASSERT(map->map[0]->next->next != NULL, "list corrupt at slot 0") universe@29: UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4], universe@29: "failed Key4") universe@29: UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, universe@29: "slot 0 not terminated") universe@29: universe@29: UCX_TEST_ASSERT(map->map[1] == NULL, "slot 1 not terminated") universe@29: universe@29: UCX_TEST_ASSERT(*((int*)map->map[2]->data) == td[3], "failed KeY3") universe@29: UCX_TEST_ASSERT(map->map[2]->next == NULL, "slot 2 not terminated") universe@29: universe@29: UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1") universe@29: universe@29: ucx_map_cstr_put(map, "Key0", &td[3]); /* 0 */ universe@29: universe@29: UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[3], "overwrite failed") universe@29: UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], universe@29: "overwrite failed") universe@29: UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4], universe@29: "overwrite failed") universe@29: UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed") universe@29: universe@33: UCX_TEST_END universe@29: ucx_map_free(map); universe@33: } universe@33: universe@33: UCX_TEST_IMPLEMENT(test_ucx_map_get) { universe@34: UcxMap *map = ucx_map_new(4); universe@34: universe@34: int td[5]; universe@34: td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; universe@34: universe@34: ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */ universe@34: ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */ universe@34: ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */ universe@34: ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */ universe@34: ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */ universe@33: UCX_TEST_BEGIN universe@34: universe@34: td[0] = *((int*)ucx_map_cstr_get(map, "Key0")); universe@34: td[1] = *((int*)ucx_map_cstr_get(map, "Key1")); universe@34: td[2] = *((int*)ucx_map_cstr_get(map, "Key2")); universe@34: td[3] = *((int*)ucx_map_cstr_get(map, "KeY3")); universe@34: td[4] = *((int*)ucx_map_cstr_get(map, "KEY4")); universe@34: UCX_TEST_ASSERT(td[0] == 10, "failed key 0") universe@34: UCX_TEST_ASSERT(td[1] == 42, "failed key 1") universe@34: UCX_TEST_ASSERT(td[2] == 70, "failed key 2") universe@34: UCX_TEST_ASSERT(td[3] == 11200, "failed key 3") universe@34: UCX_TEST_ASSERT(td[4] == 80000, "failed key 4") universe@34: universe@29: UCX_TEST_END universe@34: ucx_map_free(map); olaf@20: } universe@29: universe@33: UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) { universe@33: UcxMap *map = (UcxMap*) mapptr; olaf@31: int v1 = 10; olaf@31: int v2 = 15; olaf@31: int v3 = 7; olaf@31: int v4 = 9; universe@32: olaf@31: ucx_map_cstr_put(map, "v1", &v1); olaf@31: ucx_map_cstr_put(map, "v2", &v2); olaf@31: ucx_map_cstr_put(map, "v3", &v3); olaf@31: ucx_map_cstr_put(map, "v4", &v4); universe@32: olaf@31: UcxMapIterator i = ucx_map_iterator(map); olaf@31: int check = 0; olaf@31: int hit = 0; universe@32: olaf@31: UCX_MAP_FOREACH(int*, v, map, i) { olaf@31: check += *v; olaf@31: hit++; olaf@31: } universe@32: olaf@31: UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits"); olaf@31: UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result"); universe@33: } universe@32: universe@33: UCX_TEST_IMPLEMENT(test_ucx_map_iterator) { universe@33: UcxMap *map = ucx_map_new(16); universe@33: UCX_TEST_BEGIN universe@33: UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map) universe@33: UCX_TEST_END olaf@31: ucx_map_free(map); universe@33: } universe@33: universe@33: UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) { universe@33: UcxMap *map = ucx_map_new(1); universe@33: UCX_TEST_BEGIN universe@33: UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map) universe@33: UCX_TEST_END olaf@31: ucx_map_free(map); olaf@31: }