test/map_tests.c

Wed, 06 Feb 2013 14:35:15 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 06 Feb 2013 14:35:15 +0100
changeset 80
0125e4089f88
parent 76
655020a30e77
child 88
18823857ce79
permissions
-rw-r--r--

Fixed map tests + added some formatting options to logger

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

mercurial