test/map_tests.c

Mon, 08 Oct 2012 12:29:27 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 08 Oct 2012 12:29:27 +0200
changeset 53
e533c170bfb8
parent 52
34f50d0bada4
child 55
180bc6b18fec
permissions
-rw-r--r--

added ucx_map_remove

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@53 98 UCX_TEST_ASSERT(map->count == 5, "expected 5 remaining values");
universe@53 99 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0") != NULL, "element removed");
universe@53 100
universe@53 101 UCX_TEST_END
universe@53 102 ucx_map_free(map);
universe@53 103 }
universe@53 104
universe@53 105 UCX_TEST_IMPLEMENT(test_ucx_map_remove) {
universe@53 106 UcxMap *map = ucx_map_new(4);
universe@53 107
universe@53 108 int td[5];
universe@53 109 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
universe@53 110
universe@53 111 ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
universe@53 112 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
universe@53 113 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
universe@53 114 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
universe@53 115 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
universe@53 116 UCX_TEST_BEGIN
universe@53 117
universe@53 118 td[0] = *((int*)ucx_map_cstr_remove(map, "Key0"));
universe@53 119 td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
universe@53 120 td[2] = *((int*)ucx_map_cstr_remove(map, "Key2"));
universe@53 121 td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
universe@53 122 td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
universe@53 123 UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
universe@53 124 UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
universe@53 125 UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
universe@53 126 UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
universe@53 127 UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
universe@53 128
universe@53 129 UCX_TEST_ASSERT(map->count == 3, "expected 3 remaining values");
universe@53 130 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0")==NULL, "element not removed");
universe@53 131 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key1")!=NULL, "element removed");
universe@53 132 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key2")==NULL, "element not removed");
universe@53 133 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KeY3")!=NULL, "element removed");
universe@53 134 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KEY4")!=NULL, "element removed");
universe@53 135
universe@53 136 UCX_TEST_ASSERT(ucx_map_cstr_remove(map, "Key2") == NULL,
universe@53 137 "subsequent remove call shall return NULL");
universe@53 138
universe@29 139 UCX_TEST_END
universe@34 140 ucx_map_free(map);
olaf@20 141 }
universe@29 142
universe@33 143 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) {
universe@33 144 UcxMap *map = (UcxMap*) mapptr;
olaf@31 145 int v1 = 10;
olaf@31 146 int v2 = 15;
olaf@31 147 int v3 = 7;
olaf@31 148 int v4 = 9;
universe@32 149
olaf@31 150 ucx_map_cstr_put(map, "v1", &v1);
olaf@31 151 ucx_map_cstr_put(map, "v2", &v2);
olaf@31 152 ucx_map_cstr_put(map, "v3", &v3);
olaf@31 153 ucx_map_cstr_put(map, "v4", &v4);
universe@32 154
olaf@31 155 UcxMapIterator i = ucx_map_iterator(map);
olaf@31 156 int check = 0;
olaf@31 157 int hit = 0;
universe@32 158
universe@41 159 int* v;
universe@41 160 UCX_MAP_FOREACH(v, i) {
olaf@31 161 check += *v;
olaf@31 162 hit++;
olaf@31 163 }
universe@32 164
olaf@31 165 UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
olaf@31 166 UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
universe@33 167 }
universe@32 168
universe@33 169 UCX_TEST_IMPLEMENT(test_ucx_map_iterator) {
universe@33 170 UcxMap *map = ucx_map_new(16);
universe@33 171 UCX_TEST_BEGIN
universe@33 172 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
universe@33 173 UCX_TEST_END
olaf@31 174 ucx_map_free(map);
universe@33 175 }
universe@33 176
universe@33 177 UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) {
universe@33 178 UcxMap *map = ucx_map_new(1);
universe@33 179 UCX_TEST_BEGIN
universe@33 180 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
universe@33 181 UCX_TEST_END
olaf@31 182 ucx_map_free(map);
olaf@31 183 }
universe@42 184
universe@48 185 void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) {
universe@46 186 char *string = (char*) value;
universe@46 187 size_t n = strlen(string);
universe@46 188 char *encoded = malloc(n+1);
universe@46 189 for (int i = 0 ; i < n ; i++) {
universe@46 190 encoded[i] = string[n-1-i];
universe@46 191 }
universe@46 192 encoded[n] = 0;
universe@48 193 *size = n+1;
universe@46 194 return encoded;
universe@46 195 }
universe@46 196
universe@42 197 UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
universe@42 198 UcxMap *map = ucx_map_new(4);
universe@42 199
universe@42 200 ucx_map_cstr_put(map, "test", "test");
universe@42 201 ucx_map_cstr_put(map, "key", "value");
universe@42 202 ucx_map_cstr_put(map, "other.very.long.key", "value");
universe@42 203 ucx_map_cstr_put(map, "testkey", "testvalue");
universe@42 204 ucx_map_cstr_put(map, "simple", "not a key but an extremely long value "
universe@42 205 "to test if the buffer extension works as designed");
universe@42 206
universe@42 207 FILE *f = fopen("test_ucx_map_store", "w");
universe@42 208 int r;
universe@42 209
universe@43 210 fwrite(" # comment test\n", 1, 16, f);
universe@46 211 r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL);
universe@43 212 fwrite("!discard this", 1, 13, f);
universe@42 213
universe@42 214 fclose(f);
universe@42 215 ucx_map_free(map);
universe@42 216 map = ucx_map_new(1);
universe@42 217 f = fopen("test_ucx_map_store", "r");
universe@48 218 UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT;
universe@48 219 r += ucx_map_load_enc(map, f, allocator,
universe@48 220 test_ucx_map_store_load_encdec, NULL);
universe@48 221 fclose(f);
universe@48 222 unlink("test_ucx_map_store");
universe@42 223
universe@42 224 UCX_TEST_BEGIN
universe@42 225 char *value;
universe@42 226 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
universe@42 227
universe@42 228 value = ucx_map_cstr_get(map, "test");
universe@43 229 UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
universe@42 230 UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");
universe@42 231
universe@42 232 value = ucx_map_cstr_get(map, "key");
universe@43 233 UCX_TEST_ASSERT(value != NULL, "value not found for key: key");
universe@42 234 UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");
universe@42 235
universe@42 236 value = ucx_map_cstr_get(map, "other.very.long.key");
universe@43 237 UCX_TEST_ASSERT(value != NULL,
universe@43 238 "value not found for key: other.very.long.key");
universe@42 239 UCX_TEST_ASSERT(strcmp(value, "value") == 0,
universe@42 240 "value error for key: other.very.long.key");
universe@42 241
universe@42 242 value = ucx_map_cstr_get(map, "testkey");
universe@43 243 UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey");
universe@42 244 UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
universe@42 245 "value error for key: testkey");
universe@42 246
universe@42 247 value = ucx_map_cstr_get(map, "simple");
universe@43 248 UCX_TEST_ASSERT(value != NULL, "value not found for key: simple");
universe@42 249 UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
universe@42 250 "to test if the buffer extension works as designed") == 0,
universe@42 251 "value error for key: simple");
universe@42 252
universe@42 253 UCX_TEST_END
universe@48 254 }
universe@48 255
universe@48 256 UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) {
universe@48 257 UcxMap *map = ucx_map_new(4);
universe@48 258
universe@48 259 ucx_map_cstr_put(map, "test", "test");
universe@48 260 ucx_map_cstr_put(map, "key", "value");
universe@48 261 ucx_map_cstr_put(map, "testkey", "testvalue");
universe@48 262 ucx_map_cstr_put(map, "simple", "a simple value");
universe@48 263
universe@48 264 FILE *f = fopen("test_ucx_map_store", "w");
universe@48 265 int r;
universe@48 266 r = ucx_map_store_enc(map, f, NULL, NULL);
universe@42 267 fclose(f);
universe@48 268 ucx_map_free(map);
universe@42 269
universe@48 270 UcxMempool *pool = ucx_mempool_new(4);
universe@48 271 map = ucx_map_new(4);
universe@48 272 f = fopen("test_ucx_map_store", "r");
universe@48 273 UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool);
universe@48 274 r += ucx_map_load_enc(map, f, allocator,
universe@48 275 test_ucx_map_store_load_encdec, NULL);
universe@48 276 fclose(f);
universe@42 277 unlink("test_ucx_map_store");
universe@48 278
universe@48 279 UCX_TEST_BEGIN
universe@48 280 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
universe@48 281 UcxMapIterator iter = ucx_map_iterator(map);
universe@48 282 char *value; size_t n;
universe@48 283 UCX_MAP_FOREACH(value, iter) {
universe@48 284 n = strlen(value);
universe@48 285 UCX_TEST_ASSERT(strncmp(pool->data[iter.index], value, n),
universe@48 286 "values of map does not match pooled values");
universe@48 287 }
universe@48 288 UCX_TEST_END
universe@48 289
universe@48 290 ucx_mempool_free(pool);
universe@42 291 }
olaf@44 292
olaf@44 293 UCX_TEST_IMPLEMENT(test_ucx_map_clone) {
olaf@44 294 UcxMap *map = ucx_map_new(4);
olaf@44 295
olaf@44 296 ucx_map_cstr_put(map, "key1", "value1");
olaf@44 297 ucx_map_cstr_put(map, "key2", "value2");
olaf@44 298 ucx_map_cstr_put(map, "key3", "value3");
olaf@44 299
olaf@44 300 UcxMap *clone = ucx_map_clone(map, NULL, NULL);
olaf@44 301
olaf@44 302 char *v1 = ucx_map_cstr_get(map, "key1");
olaf@44 303 char *v2 = ucx_map_cstr_get(map, "key2");
olaf@44 304 char *v3 = ucx_map_cstr_get(map, "key3");
olaf@44 305
olaf@44 306 UCX_TEST_BEGIN
olaf@44 307
olaf@44 308 UCX_TEST_ASSERT(v1 != NULL, "failed key 1");
olaf@44 309 UCX_TEST_ASSERT(v2 != NULL, "failed key 2");
olaf@44 310 UCX_TEST_ASSERT(v3 != NULL, "failed key 3");
olaf@44 311
olaf@44 312 char *c1 = ucx_map_cstr_get(clone, "key1");
olaf@44 313 char *c2 = ucx_map_cstr_get(clone, "key2");
olaf@44 314 char *c3 = ucx_map_cstr_get(clone, "key3");
olaf@44 315
olaf@44 316 UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)");
olaf@44 317 UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)");
olaf@44 318 UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)");
olaf@44 319
olaf@44 320 UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1");
olaf@44 321 UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2");
olaf@44 322 UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3");
olaf@44 323
olaf@44 324 UCX_TEST_END
olaf@44 325
olaf@44 326 ucx_map_free(map);
olaf@44 327 ucx_map_free(clone);
olaf@44 328 }
universe@51 329
universe@51 330 UCX_TEST_IMPLEMENT(test_ucx_map_rehash) {
universe@51 331 UcxMap *map = ucx_map_new(4);
universe@51 332
universe@51 333 char keys[10][5];
universe@51 334 char values[10][7];
universe@51 335 for (int i = 0 ; i < 10 ; i++) {
universe@51 336 strcpy(keys[i], "key");
universe@51 337 keys[i][3] = 48+i; keys[i][4] = 0;
universe@51 338 strcpy(values[i], "value");
universe@51 339 values[i][5] = 48+i; values[i][6] = 0;
universe@51 340
universe@51 341 ucx_map_cstr_put(map, keys[i], values[i]);
universe@51 342 }
universe@51 343
olaf@52 344 ucx_map_rehash(map);
universe@51 345
universe@51 346 UCX_TEST_BEGIN
universe@51 347 UCX_TEST_ASSERT(map->size == 25, "new capacity shall be 2.5 * count");
universe@51 348 UCX_TEST_ASSERT(map->count == 10, "new map element count incorrect");
universe@51 349 for (int i = 0 ; i < 10 ; i++) {
universe@51 350 char *value = ucx_map_cstr_get(map, keys[i]);
universe@51 351 UCX_TEST_ASSERT(value != NULL, "new map is missing old keys");
universe@51 352 UCX_TEST_ASSERT(strncmp(value, values[i], 6) == 0,
universe@51 353 "new map contains incorrect values");
universe@51 354 }
olaf@52 355 ucx_map_rehash(map);
olaf@52 356 UCX_TEST_ASSERT(map->size == 25,
universe@51 357 "subsequent rehashing call shall not change size");
universe@51 358 UCX_TEST_END
universe@51 359
universe@51 360 ucx_map_free(map);
universe@51 361 }

mercurial