Fri, 05 Oct 2012 14:06:40 +0200
added rehashing to maps by using clone function
olaf@20 | 1 | /* |
olaf@20 | 2 | * |
olaf@20 | 3 | */ |
olaf@20 | 4 | |
olaf@20 | 5 | #include "map_tests.h" |
olaf@20 | 6 | |
olaf@44 | 7 | #ifndef _WIN32 |
olaf@44 | 8 | #include <unistd.h> |
olaf@44 | 9 | #endif /* not _WIN32 */ |
olaf@44 | 10 | |
universe@33 | 11 | UCX_TEST_IMPLEMENT(test_ucx_map_new) { |
olaf@20 | 12 | UcxMap *map = ucx_map_new(16); |
universe@33 | 13 | UCX_TEST_BEGIN |
universe@40 | 14 | UCX_TEST_ASSERT(map->size == 16, "wrong size"); |
universe@40 | 15 | UCX_TEST_ASSERT(map->map != NULL, "failed"); |
universe@29 | 16 | |
universe@33 | 17 | UCX_TEST_END |
universe@29 | 18 | ucx_map_free(map); |
universe@29 | 19 | } |
olaf@20 | 20 | |
universe@33 | 21 | UCX_TEST_IMPLEMENT(test_ucx_key) { |
universe@29 | 22 | UcxKey key = ucx_key("This is a text.", 15); |
universe@33 | 23 | UCX_TEST_BEGIN |
universe@40 | 24 | UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed"); |
universe@40 | 25 | UCX_TEST_ASSERT(key.len == 15, "failed"); |
universe@40 | 26 | UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed"); |
universe@29 | 27 | |
universe@29 | 28 | UCX_TEST_END |
universe@29 | 29 | } |
olaf@20 | 30 | |
universe@33 | 31 | UCX_TEST_IMPLEMENT(test_ucx_map_put) { |
universe@29 | 32 | |
universe@29 | 33 | UcxMap *map = ucx_map_new(4); |
universe@29 | 34 | |
universe@29 | 35 | int td[5]; |
universe@29 | 36 | td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; |
olaf@20 | 37 | |
universe@33 | 38 | UCX_TEST_BEGIN |
universe@29 | 39 | ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */ |
universe@29 | 40 | ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */ |
universe@29 | 41 | ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */ |
universe@29 | 42 | ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */ |
universe@29 | 43 | ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */ |
universe@29 | 44 | |
universe@40 | 45 | UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0"); |
universe@40 | 46 | UCX_TEST_ASSERT(map->map[0]->next != NULL, "no list at slot 0"); |
universe@40 | 47 | UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], "failed Key2"); |
universe@40 | 48 | UCX_TEST_ASSERT(map->map[0]->next->next != NULL, "list corrupt at slot 0"); |
universe@29 | 49 | UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4], |
universe@29 | 50 | "failed Key4") |
universe@29 | 51 | UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, |
universe@29 | 52 | "slot 0 not terminated") |
universe@29 | 53 | |
universe@40 | 54 | UCX_TEST_ASSERT(map->map[1] == NULL, "slot 1 not terminated"); |
universe@29 | 55 | |
universe@40 | 56 | UCX_TEST_ASSERT(*((int*)map->map[2]->data) == td[3], "failed KeY3"); |
universe@40 | 57 | UCX_TEST_ASSERT(map->map[2]->next == NULL, "slot 2 not terminated"); |
universe@29 | 58 | |
universe@40 | 59 | UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1"); |
universe@29 | 60 | |
universe@29 | 61 | ucx_map_cstr_put(map, "Key0", &td[3]); /* 0 */ |
universe@29 | 62 | |
universe@40 | 63 | UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[3], "overwrite failed"); |
universe@29 | 64 | UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], |
universe@29 | 65 | "overwrite failed") |
universe@29 | 66 | UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4], |
universe@29 | 67 | "overwrite failed") |
universe@40 | 68 | UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed"); |
universe@29 | 69 | |
universe@33 | 70 | UCX_TEST_END |
universe@29 | 71 | ucx_map_free(map); |
universe@33 | 72 | } |
universe@33 | 73 | |
universe@33 | 74 | UCX_TEST_IMPLEMENT(test_ucx_map_get) { |
universe@34 | 75 | UcxMap *map = ucx_map_new(4); |
universe@34 | 76 | |
universe@34 | 77 | int td[5]; |
universe@34 | 78 | td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; |
universe@34 | 79 | |
universe@34 | 80 | ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */ |
universe@34 | 81 | ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */ |
universe@34 | 82 | ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */ |
universe@34 | 83 | ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */ |
universe@34 | 84 | ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */ |
universe@33 | 85 | UCX_TEST_BEGIN |
universe@34 | 86 | |
universe@34 | 87 | td[0] = *((int*)ucx_map_cstr_get(map, "Key0")); |
universe@34 | 88 | td[1] = *((int*)ucx_map_cstr_get(map, "Key1")); |
universe@34 | 89 | td[2] = *((int*)ucx_map_cstr_get(map, "Key2")); |
universe@34 | 90 | td[3] = *((int*)ucx_map_cstr_get(map, "KeY3")); |
universe@34 | 91 | td[4] = *((int*)ucx_map_cstr_get(map, "KEY4")); |
universe@40 | 92 | UCX_TEST_ASSERT(td[0] == 10, "failed key 0"); |
universe@40 | 93 | UCX_TEST_ASSERT(td[1] == 42, "failed key 1"); |
universe@40 | 94 | UCX_TEST_ASSERT(td[2] == 70, "failed key 2"); |
universe@40 | 95 | UCX_TEST_ASSERT(td[3] == 11200, "failed key 3"); |
universe@40 | 96 | UCX_TEST_ASSERT(td[4] == 80000, "failed key 4"); |
universe@34 | 97 | |
universe@29 | 98 | UCX_TEST_END |
universe@34 | 99 | ucx_map_free(map); |
olaf@20 | 100 | } |
universe@29 | 101 | |
universe@33 | 102 | UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) { |
universe@33 | 103 | UcxMap *map = (UcxMap*) mapptr; |
olaf@31 | 104 | int v1 = 10; |
olaf@31 | 105 | int v2 = 15; |
olaf@31 | 106 | int v3 = 7; |
olaf@31 | 107 | int v4 = 9; |
universe@32 | 108 | |
olaf@31 | 109 | ucx_map_cstr_put(map, "v1", &v1); |
olaf@31 | 110 | ucx_map_cstr_put(map, "v2", &v2); |
olaf@31 | 111 | ucx_map_cstr_put(map, "v3", &v3); |
olaf@31 | 112 | ucx_map_cstr_put(map, "v4", &v4); |
universe@32 | 113 | |
olaf@31 | 114 | UcxMapIterator i = ucx_map_iterator(map); |
olaf@31 | 115 | int check = 0; |
olaf@31 | 116 | int hit = 0; |
universe@32 | 117 | |
universe@41 | 118 | int* v; |
universe@41 | 119 | UCX_MAP_FOREACH(v, i) { |
olaf@31 | 120 | check += *v; |
olaf@31 | 121 | hit++; |
olaf@31 | 122 | } |
universe@32 | 123 | |
olaf@31 | 124 | UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits"); |
olaf@31 | 125 | UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result"); |
universe@33 | 126 | } |
universe@32 | 127 | |
universe@33 | 128 | UCX_TEST_IMPLEMENT(test_ucx_map_iterator) { |
universe@33 | 129 | UcxMap *map = ucx_map_new(16); |
universe@33 | 130 | UCX_TEST_BEGIN |
universe@33 | 131 | UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map) |
universe@33 | 132 | UCX_TEST_END |
olaf@31 | 133 | ucx_map_free(map); |
universe@33 | 134 | } |
universe@33 | 135 | |
universe@33 | 136 | UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) { |
universe@33 | 137 | UcxMap *map = ucx_map_new(1); |
universe@33 | 138 | UCX_TEST_BEGIN |
universe@33 | 139 | UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map) |
universe@33 | 140 | UCX_TEST_END |
olaf@31 | 141 | ucx_map_free(map); |
olaf@31 | 142 | } |
universe@42 | 143 | |
universe@48 | 144 | void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) { |
universe@46 | 145 | char *string = (char*) value; |
universe@46 | 146 | size_t n = strlen(string); |
universe@46 | 147 | char *encoded = malloc(n+1); |
universe@46 | 148 | for (int i = 0 ; i < n ; i++) { |
universe@46 | 149 | encoded[i] = string[n-1-i]; |
universe@46 | 150 | } |
universe@46 | 151 | encoded[n] = 0; |
universe@48 | 152 | *size = n+1; |
universe@46 | 153 | return encoded; |
universe@46 | 154 | } |
universe@46 | 155 | |
universe@42 | 156 | UCX_TEST_IMPLEMENT(test_ucx_map_store_load) { |
universe@42 | 157 | UcxMap *map = ucx_map_new(4); |
universe@42 | 158 | |
universe@42 | 159 | ucx_map_cstr_put(map, "test", "test"); |
universe@42 | 160 | ucx_map_cstr_put(map, "key", "value"); |
universe@42 | 161 | ucx_map_cstr_put(map, "other.very.long.key", "value"); |
universe@42 | 162 | ucx_map_cstr_put(map, "testkey", "testvalue"); |
universe@42 | 163 | ucx_map_cstr_put(map, "simple", "not a key but an extremely long value " |
universe@42 | 164 | "to test if the buffer extension works as designed"); |
universe@42 | 165 | |
universe@42 | 166 | FILE *f = fopen("test_ucx_map_store", "w"); |
universe@42 | 167 | int r; |
universe@42 | 168 | |
universe@43 | 169 | fwrite(" # comment test\n", 1, 16, f); |
universe@46 | 170 | r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL); |
universe@43 | 171 | fwrite("!discard this", 1, 13, f); |
universe@42 | 172 | |
universe@42 | 173 | fclose(f); |
universe@42 | 174 | ucx_map_free(map); |
universe@42 | 175 | map = ucx_map_new(1); |
universe@42 | 176 | f = fopen("test_ucx_map_store", "r"); |
universe@48 | 177 | UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT; |
universe@48 | 178 | r += ucx_map_load_enc(map, f, allocator, |
universe@48 | 179 | test_ucx_map_store_load_encdec, NULL); |
universe@48 | 180 | fclose(f); |
universe@48 | 181 | unlink("test_ucx_map_store"); |
universe@42 | 182 | |
universe@42 | 183 | UCX_TEST_BEGIN |
universe@42 | 184 | char *value; |
universe@42 | 185 | UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); |
universe@42 | 186 | |
universe@42 | 187 | value = ucx_map_cstr_get(map, "test"); |
universe@43 | 188 | UCX_TEST_ASSERT(value != NULL, "value not found for key: test"); |
universe@42 | 189 | UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test"); |
universe@42 | 190 | |
universe@42 | 191 | value = ucx_map_cstr_get(map, "key"); |
universe@43 | 192 | UCX_TEST_ASSERT(value != NULL, "value not found for key: key"); |
universe@42 | 193 | UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key"); |
universe@42 | 194 | |
universe@42 | 195 | value = ucx_map_cstr_get(map, "other.very.long.key"); |
universe@43 | 196 | UCX_TEST_ASSERT(value != NULL, |
universe@43 | 197 | "value not found for key: other.very.long.key"); |
universe@42 | 198 | UCX_TEST_ASSERT(strcmp(value, "value") == 0, |
universe@42 | 199 | "value error for key: other.very.long.key"); |
universe@42 | 200 | |
universe@42 | 201 | value = ucx_map_cstr_get(map, "testkey"); |
universe@43 | 202 | UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey"); |
universe@42 | 203 | UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0, |
universe@42 | 204 | "value error for key: testkey"); |
universe@42 | 205 | |
universe@42 | 206 | value = ucx_map_cstr_get(map, "simple"); |
universe@43 | 207 | UCX_TEST_ASSERT(value != NULL, "value not found for key: simple"); |
universe@42 | 208 | UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value " |
universe@42 | 209 | "to test if the buffer extension works as designed") == 0, |
universe@42 | 210 | "value error for key: simple"); |
universe@42 | 211 | |
universe@42 | 212 | UCX_TEST_END |
universe@48 | 213 | } |
universe@48 | 214 | |
universe@48 | 215 | UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) { |
universe@48 | 216 | UcxMap *map = ucx_map_new(4); |
universe@48 | 217 | |
universe@48 | 218 | ucx_map_cstr_put(map, "test", "test"); |
universe@48 | 219 | ucx_map_cstr_put(map, "key", "value"); |
universe@48 | 220 | ucx_map_cstr_put(map, "testkey", "testvalue"); |
universe@48 | 221 | ucx_map_cstr_put(map, "simple", "a simple value"); |
universe@48 | 222 | |
universe@48 | 223 | FILE *f = fopen("test_ucx_map_store", "w"); |
universe@48 | 224 | int r; |
universe@48 | 225 | r = ucx_map_store_enc(map, f, NULL, NULL); |
universe@42 | 226 | fclose(f); |
universe@48 | 227 | ucx_map_free(map); |
universe@42 | 228 | |
universe@48 | 229 | UcxMempool *pool = ucx_mempool_new(4); |
universe@48 | 230 | map = ucx_map_new(4); |
universe@48 | 231 | f = fopen("test_ucx_map_store", "r"); |
universe@48 | 232 | UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool); |
universe@48 | 233 | r += ucx_map_load_enc(map, f, allocator, |
universe@48 | 234 | test_ucx_map_store_load_encdec, NULL); |
universe@48 | 235 | fclose(f); |
universe@42 | 236 | unlink("test_ucx_map_store"); |
universe@48 | 237 | |
universe@48 | 238 | UCX_TEST_BEGIN |
universe@48 | 239 | UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); |
universe@48 | 240 | UcxMapIterator iter = ucx_map_iterator(map); |
universe@48 | 241 | char *value; size_t n; |
universe@48 | 242 | UCX_MAP_FOREACH(value, iter) { |
universe@48 | 243 | n = strlen(value); |
universe@48 | 244 | UCX_TEST_ASSERT(strncmp(pool->data[iter.index], value, n), |
universe@48 | 245 | "values of map does not match pooled values"); |
universe@48 | 246 | } |
universe@48 | 247 | UCX_TEST_END |
universe@48 | 248 | |
universe@48 | 249 | ucx_mempool_free(pool); |
universe@42 | 250 | } |
olaf@44 | 251 | |
olaf@44 | 252 | UCX_TEST_IMPLEMENT(test_ucx_map_clone) { |
olaf@44 | 253 | UcxMap *map = ucx_map_new(4); |
olaf@44 | 254 | |
olaf@44 | 255 | ucx_map_cstr_put(map, "key1", "value1"); |
olaf@44 | 256 | ucx_map_cstr_put(map, "key2", "value2"); |
olaf@44 | 257 | ucx_map_cstr_put(map, "key3", "value3"); |
olaf@44 | 258 | |
olaf@44 | 259 | UcxMap *clone = ucx_map_clone(map, NULL, NULL); |
olaf@44 | 260 | |
olaf@44 | 261 | char *v1 = ucx_map_cstr_get(map, "key1"); |
olaf@44 | 262 | char *v2 = ucx_map_cstr_get(map, "key2"); |
olaf@44 | 263 | char *v3 = ucx_map_cstr_get(map, "key3"); |
olaf@44 | 264 | |
olaf@44 | 265 | UCX_TEST_BEGIN |
olaf@44 | 266 | |
olaf@44 | 267 | UCX_TEST_ASSERT(v1 != NULL, "failed key 1"); |
olaf@44 | 268 | UCX_TEST_ASSERT(v2 != NULL, "failed key 2"); |
olaf@44 | 269 | UCX_TEST_ASSERT(v3 != NULL, "failed key 3"); |
olaf@44 | 270 | |
olaf@44 | 271 | char *c1 = ucx_map_cstr_get(clone, "key1"); |
olaf@44 | 272 | char *c2 = ucx_map_cstr_get(clone, "key2"); |
olaf@44 | 273 | char *c3 = ucx_map_cstr_get(clone, "key3"); |
olaf@44 | 274 | |
olaf@44 | 275 | UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)"); |
olaf@44 | 276 | UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)"); |
olaf@44 | 277 | UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)"); |
olaf@44 | 278 | |
olaf@44 | 279 | UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1"); |
olaf@44 | 280 | UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2"); |
olaf@44 | 281 | UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3"); |
olaf@44 | 282 | |
olaf@44 | 283 | UCX_TEST_END |
olaf@44 | 284 | |
olaf@44 | 285 | ucx_map_free(map); |
olaf@44 | 286 | ucx_map_free(clone); |
olaf@44 | 287 | } |
universe@51 | 288 | |
universe@51 | 289 | UCX_TEST_IMPLEMENT(test_ucx_map_rehash) { |
universe@51 | 290 | UcxMap *map = ucx_map_new(4); |
universe@51 | 291 | |
universe@51 | 292 | char keys[10][5]; |
universe@51 | 293 | char values[10][7]; |
universe@51 | 294 | for (int i = 0 ; i < 10 ; i++) { |
universe@51 | 295 | strcpy(keys[i], "key"); |
universe@51 | 296 | keys[i][3] = 48+i; keys[i][4] = 0; |
universe@51 | 297 | strcpy(values[i], "value"); |
universe@51 | 298 | values[i][5] = 48+i; values[i][6] = 0; |
universe@51 | 299 | |
universe@51 | 300 | ucx_map_cstr_put(map, keys[i], values[i]); |
universe@51 | 301 | } |
universe@51 | 302 | |
universe@51 | 303 | map = ucx_map_rehash(map); |
universe@51 | 304 | |
universe@51 | 305 | UCX_TEST_BEGIN |
universe@51 | 306 | UCX_TEST_ASSERT(map->size == 25, "new capacity shall be 2.5 * count"); |
universe@51 | 307 | UCX_TEST_ASSERT(map->count == 10, "new map element count incorrect"); |
universe@51 | 308 | for (int i = 0 ; i < 10 ; i++) { |
universe@51 | 309 | char *value = ucx_map_cstr_get(map, keys[i]); |
universe@51 | 310 | UCX_TEST_ASSERT(value != NULL, "new map is missing old keys"); |
universe@51 | 311 | UCX_TEST_ASSERT(strncmp(value, values[i], 6) == 0, |
universe@51 | 312 | "new map contains incorrect values"); |
universe@51 | 313 | } |
universe@51 | 314 | UcxMap *samemap = ucx_map_rehash(map); |
universe@51 | 315 | UCX_TEST_ASSERT(samemap == map, |
universe@51 | 316 | "subsequent rehashing call shall do nothing"); |
universe@51 | 317 | UCX_TEST_ASSERT(samemap->size == 25, |
universe@51 | 318 | "subsequent rehashing call shall not change size"); |
universe@51 | 319 | UCX_TEST_END |
universe@51 | 320 | |
universe@51 | 321 | ucx_map_free(map); |
universe@51 | 322 | } |