olaf@20: /* olaf@20: * olaf@20: */ olaf@20: olaf@20: #include "map_tests.h" olaf@20: olaf@44: #ifndef _WIN32 olaf@44: #include olaf@44: #endif /* not _WIN32 */ olaf@44: universe@33: UCX_TEST_IMPLEMENT(test_ucx_map_new) { olaf@20: UcxMap *map = ucx_map_new(16); universe@33: UCX_TEST_BEGIN universe@40: UCX_TEST_ASSERT(map->size == 16, "wrong size"); universe@40: 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@40: UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed"); universe@40: UCX_TEST_ASSERT(key.len == 15, "failed"); universe@40: 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@40: UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0"); universe@40: UCX_TEST_ASSERT(map->map[0]->next != NULL, "no list at slot 0"); universe@40: UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], "failed Key2"); universe@40: 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@40: UCX_TEST_ASSERT(map->map[1] == NULL, "slot 1 not terminated"); universe@29: universe@40: UCX_TEST_ASSERT(*((int*)map->map[2]->data) == td[3], "failed KeY3"); universe@40: UCX_TEST_ASSERT(map->map[2]->next == NULL, "slot 2 not terminated"); universe@29: universe@40: 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@40: 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@40: 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@40: UCX_TEST_ASSERT(td[0] == 10, "failed key 0"); universe@40: UCX_TEST_ASSERT(td[1] == 42, "failed key 1"); universe@40: UCX_TEST_ASSERT(td[2] == 70, "failed key 2"); universe@40: UCX_TEST_ASSERT(td[3] == 11200, "failed key 3"); universe@40: 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: universe@41: int* v; universe@41: UCX_MAP_FOREACH(v, 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: } universe@42: universe@48: void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) { universe@46: char *string = (char*) value; universe@46: size_t n = strlen(string); universe@46: char *encoded = malloc(n+1); universe@46: for (int i = 0 ; i < n ; i++) { universe@46: encoded[i] = string[n-1-i]; universe@46: } universe@46: encoded[n] = 0; universe@48: *size = n+1; universe@46: return encoded; universe@46: } universe@46: universe@42: UCX_TEST_IMPLEMENT(test_ucx_map_store_load) { universe@42: UcxMap *map = ucx_map_new(4); universe@42: universe@42: ucx_map_cstr_put(map, "test", "test"); universe@42: ucx_map_cstr_put(map, "key", "value"); universe@42: ucx_map_cstr_put(map, "other.very.long.key", "value"); universe@42: ucx_map_cstr_put(map, "testkey", "testvalue"); universe@42: ucx_map_cstr_put(map, "simple", "not a key but an extremely long value " universe@42: "to test if the buffer extension works as designed"); universe@42: universe@42: FILE *f = fopen("test_ucx_map_store", "w"); universe@42: int r; universe@42: universe@43: fwrite(" # comment test\n", 1, 16, f); universe@46: r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL); universe@43: fwrite("!discard this", 1, 13, f); universe@42: universe@42: fclose(f); universe@42: ucx_map_free(map); universe@42: map = ucx_map_new(1); universe@42: f = fopen("test_ucx_map_store", "r"); universe@48: UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT; universe@48: r += ucx_map_load_enc(map, f, allocator, universe@48: test_ucx_map_store_load_encdec, NULL); universe@48: fclose(f); universe@48: unlink("test_ucx_map_store"); universe@42: universe@42: UCX_TEST_BEGIN universe@42: char *value; universe@42: UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); universe@42: universe@42: value = ucx_map_cstr_get(map, "test"); universe@43: UCX_TEST_ASSERT(value != NULL, "value not found for key: test"); universe@42: UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test"); universe@42: universe@42: value = ucx_map_cstr_get(map, "key"); universe@43: UCX_TEST_ASSERT(value != NULL, "value not found for key: key"); universe@42: UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key"); universe@42: universe@42: value = ucx_map_cstr_get(map, "other.very.long.key"); universe@43: UCX_TEST_ASSERT(value != NULL, universe@43: "value not found for key: other.very.long.key"); universe@42: UCX_TEST_ASSERT(strcmp(value, "value") == 0, universe@42: "value error for key: other.very.long.key"); universe@42: universe@42: value = ucx_map_cstr_get(map, "testkey"); universe@43: UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey"); universe@42: UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0, universe@42: "value error for key: testkey"); universe@42: universe@42: value = ucx_map_cstr_get(map, "simple"); universe@43: UCX_TEST_ASSERT(value != NULL, "value not found for key: simple"); universe@42: UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value " universe@42: "to test if the buffer extension works as designed") == 0, universe@42: "value error for key: simple"); universe@42: universe@42: UCX_TEST_END universe@48: } universe@48: universe@48: UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) { universe@48: UcxMap *map = ucx_map_new(4); universe@48: universe@48: ucx_map_cstr_put(map, "test", "test"); universe@48: ucx_map_cstr_put(map, "key", "value"); universe@48: ucx_map_cstr_put(map, "testkey", "testvalue"); universe@48: ucx_map_cstr_put(map, "simple", "a simple value"); universe@48: universe@48: FILE *f = fopen("test_ucx_map_store", "w"); universe@48: int r; universe@48: r = ucx_map_store_enc(map, f, NULL, NULL); universe@42: fclose(f); universe@48: ucx_map_free(map); universe@42: universe@48: UcxMempool *pool = ucx_mempool_new(4); universe@48: map = ucx_map_new(4); universe@48: f = fopen("test_ucx_map_store", "r"); universe@48: UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool); universe@48: r += ucx_map_load_enc(map, f, allocator, universe@48: test_ucx_map_store_load_encdec, NULL); universe@48: fclose(f); universe@42: unlink("test_ucx_map_store"); universe@48: universe@48: UCX_TEST_BEGIN universe@48: UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); universe@48: UcxMapIterator iter = ucx_map_iterator(map); universe@48: char *value; size_t n; universe@48: UCX_MAP_FOREACH(value, iter) { universe@48: n = strlen(value); universe@48: UCX_TEST_ASSERT(strncmp(pool->data[iter.index], value, n), universe@48: "values of map does not match pooled values"); universe@48: } universe@48: UCX_TEST_END universe@48: universe@48: ucx_mempool_free(pool); universe@42: } olaf@44: olaf@44: UCX_TEST_IMPLEMENT(test_ucx_map_clone) { olaf@44: UcxMap *map = ucx_map_new(4); olaf@44: olaf@44: ucx_map_cstr_put(map, "key1", "value1"); olaf@44: ucx_map_cstr_put(map, "key2", "value2"); olaf@44: ucx_map_cstr_put(map, "key3", "value3"); olaf@44: olaf@44: UcxMap *clone = ucx_map_clone(map, NULL, NULL); olaf@44: olaf@44: char *v1 = ucx_map_cstr_get(map, "key1"); olaf@44: char *v2 = ucx_map_cstr_get(map, "key2"); olaf@44: char *v3 = ucx_map_cstr_get(map, "key3"); olaf@44: olaf@44: UCX_TEST_BEGIN olaf@44: olaf@44: UCX_TEST_ASSERT(v1 != NULL, "failed key 1"); olaf@44: UCX_TEST_ASSERT(v2 != NULL, "failed key 2"); olaf@44: UCX_TEST_ASSERT(v3 != NULL, "failed key 3"); olaf@44: olaf@44: char *c1 = ucx_map_cstr_get(clone, "key1"); olaf@44: char *c2 = ucx_map_cstr_get(clone, "key2"); olaf@44: char *c3 = ucx_map_cstr_get(clone, "key3"); olaf@44: olaf@44: UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)"); olaf@44: UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)"); olaf@44: UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)"); olaf@44: olaf@44: UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1"); olaf@44: UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2"); olaf@44: UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3"); olaf@44: olaf@44: UCX_TEST_END olaf@44: olaf@44: ucx_map_free(map); olaf@44: ucx_map_free(clone); olaf@44: } universe@51: universe@51: UCX_TEST_IMPLEMENT(test_ucx_map_rehash) { universe@51: UcxMap *map = ucx_map_new(4); universe@51: universe@51: char keys[10][5]; universe@51: char values[10][7]; universe@51: for (int i = 0 ; i < 10 ; i++) { universe@51: strcpy(keys[i], "key"); universe@51: keys[i][3] = 48+i; keys[i][4] = 0; universe@51: strcpy(values[i], "value"); universe@51: values[i][5] = 48+i; values[i][6] = 0; universe@51: universe@51: ucx_map_cstr_put(map, keys[i], values[i]); universe@51: } universe@51: universe@51: map = ucx_map_rehash(map); universe@51: universe@51: UCX_TEST_BEGIN universe@51: UCX_TEST_ASSERT(map->size == 25, "new capacity shall be 2.5 * count"); universe@51: UCX_TEST_ASSERT(map->count == 10, "new map element count incorrect"); universe@51: for (int i = 0 ; i < 10 ; i++) { universe@51: char *value = ucx_map_cstr_get(map, keys[i]); universe@51: UCX_TEST_ASSERT(value != NULL, "new map is missing old keys"); universe@51: UCX_TEST_ASSERT(strncmp(value, values[i], 6) == 0, universe@51: "new map contains incorrect values"); universe@51: } universe@51: UcxMap *samemap = ucx_map_rehash(map); universe@51: UCX_TEST_ASSERT(samemap == map, universe@51: "subsequent rehashing call shall do nothing"); universe@51: UCX_TEST_ASSERT(samemap->size == 25, universe@51: "subsequent rehashing call shall not change size"); universe@51: UCX_TEST_END universe@51: universe@51: ucx_map_free(map); universe@51: }