test/map_tests.c

Fri, 30 Nov 2012 13:10:58 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 30 Nov 2012 13:10:58 +0100
changeset 76
655020a30e77
parent 71
303dabadff1c
child 80
0125e4089f88
permissions
-rw-r--r--

fixed buffer

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

mercurial