test/map_tests.c

Tue, 09 Oct 2012 10:21:18 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 09 Oct 2012 10:21:18 +0200
changeset 55
180bc6b18fec
parent 53
e533c170bfb8
child 69
fb59270b1de3
permissions
-rw-r--r--

fixed map tests + used tmpfiles in tests

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

mercurial