test/map_tests.c

Wed, 27 Feb 2013 13:30:21 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Feb 2013 13:30:21 +0100
changeset 95
ecfdc1c4a552
parent 88
18823857ce79
child 103
08018864fb91
permissions
-rw-r--r--

added gnu++11 support

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@88 142 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, UcxMap *map) {
olaf@31 143 int v1 = 10;
olaf@31 144 int v2 = 15;
olaf@31 145 int v3 = 7;
olaf@31 146 int v4 = 9;
universe@32 147
olaf@31 148 ucx_map_cstr_put(map, "v1", &v1);
olaf@31 149 ucx_map_cstr_put(map, "v2", &v2);
olaf@31 150 ucx_map_cstr_put(map, "v3", &v3);
olaf@31 151 ucx_map_cstr_put(map, "v4", &v4);
universe@32 152
olaf@31 153 UcxMapIterator i = ucx_map_iterator(map);
olaf@31 154 int check = 0;
olaf@31 155 int hit = 0;
universe@32 156
universe@41 157 int* v;
universe@41 158 UCX_MAP_FOREACH(v, i) {
olaf@31 159 check += *v;
olaf@31 160 hit++;
olaf@31 161 }
universe@32 162
olaf@31 163 UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
olaf@31 164 UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
universe@33 165 }
universe@32 166
universe@33 167 UCX_TEST_IMPLEMENT(test_ucx_map_iterator) {
universe@33 168 UcxMap *map = ucx_map_new(16);
universe@33 169 UCX_TEST_BEGIN
universe@33 170 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
universe@33 171 UCX_TEST_END
olaf@31 172 ucx_map_free(map);
universe@33 173 }
universe@33 174
universe@33 175 UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) {
universe@33 176 UcxMap *map = ucx_map_new(1);
universe@33 177 UCX_TEST_BEGIN
universe@33 178 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
universe@33 179 UCX_TEST_END
olaf@31 180 ucx_map_free(map);
olaf@31 181 }
universe@42 182
universe@48 183 void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) {
universe@69 184 const char *string = (const char*) value;
universe@46 185 size_t n = strlen(string);
universe@69 186 char *encoded = (char*) malloc(n+1);
universe@95 187 for (size_t i = 0 ; i < n ; i++) {
universe@46 188 encoded[i] = string[n-1-i];
universe@46 189 }
universe@46 190 encoded[n] = 0;
universe@48 191 *size = n+1;
universe@46 192 return encoded;
universe@46 193 }
universe@46 194
universe@42 195 UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
universe@42 196 UcxMap *map = ucx_map_new(4);
universe@42 197
universe@71 198 ucx_map_cstr_put(map, "test", (void*)"test");
universe@71 199 ucx_map_cstr_put(map, "key", (void*)"value");
universe@71 200 ucx_map_cstr_put(map, "other.very.long.key", (void*)"value");
universe@71 201 ucx_map_cstr_put(map, "testkey", (void*)"testvalue");
universe@71 202 ucx_map_cstr_put(map, "simple", (void*)"not a key but an extremely long "
universe@71 203 "value to test if the buffer extension works as designed");
universe@42 204
universe@55 205 UCX_TEST_BEGIN
universe@55 206 FILE *f = tmpfile();
universe@55 207 UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted")
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@55 213 fflush(f);
universe@42 214
universe@42 215 ucx_map_free(map);
universe@42 216 map = ucx_map_new(1);
universe@55 217 fseek(f, 0, SEEK_SET);
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@42 222
universe@69 223 const char *value;
universe@42 224 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
universe@42 225
universe@69 226 value = (const char *) ucx_map_cstr_get(map, "test");
universe@43 227 UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
universe@42 228 UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");
universe@42 229
universe@69 230 value = (const char *) ucx_map_cstr_get(map, "key");
universe@43 231 UCX_TEST_ASSERT(value != NULL, "value not found for key: key");
universe@42 232 UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");
universe@42 233
universe@69 234 value = (const char *) ucx_map_cstr_get(map, "other.very.long.key");
universe@43 235 UCX_TEST_ASSERT(value != NULL,
universe@43 236 "value not found for key: other.very.long.key");
universe@42 237 UCX_TEST_ASSERT(strcmp(value, "value") == 0,
universe@42 238 "value error for key: other.very.long.key");
universe@42 239
universe@69 240 value = (const char *) ucx_map_cstr_get(map, "testkey");
universe@43 241 UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey");
universe@42 242 UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
universe@42 243 "value error for key: testkey");
universe@42 244
universe@69 245 value = (const char *) ucx_map_cstr_get(map, "simple");
universe@43 246 UCX_TEST_ASSERT(value != NULL, "value not found for key: simple");
universe@42 247 UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
universe@42 248 "to test if the buffer extension works as designed") == 0,
universe@42 249 "value error for key: simple");
universe@42 250
universe@55 251 void *d;
universe@55 252 UcxMapIterator iter = ucx_map_iterator(map);
universe@55 253 UCX_MAP_FOREACH(d, iter) {
universe@55 254 free(d);
universe@55 255 }
universe@55 256 ucx_map_free(map);
universe@42 257 UCX_TEST_END
universe@48 258 }
universe@48 259
universe@48 260 UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) {
universe@48 261 UcxMap *map = ucx_map_new(4);
universe@48 262
universe@71 263 ucx_map_cstr_put(map, "test", (void*)"test");
universe@71 264 ucx_map_cstr_put(map, "key", (void*)"value");
universe@71 265 ucx_map_cstr_put(map, "testkey", (void*)"testvalue");
universe@71 266 ucx_map_cstr_put(map, "simple", (void*)"a simple value");
universe@48 267
universe@55 268 UCX_TEST_BEGIN
universe@55 269 FILE *f = tmpfile();
olaf@76 270 UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted");
universe@48 271 int r;
universe@48 272 r = ucx_map_store_enc(map, f, NULL, NULL);
universe@48 273 ucx_map_free(map);
universe@55 274 fflush(f);
universe@42 275
universe@48 276 UcxMempool *pool = ucx_mempool_new(4);
universe@48 277 map = ucx_map_new(4);
universe@55 278 fseek(f, 0, SEEK_SET);
universe@48 279 UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool);
universe@48 280 r += ucx_map_load_enc(map, f, allocator,
universe@48 281 test_ucx_map_store_load_encdec, NULL);
universe@48 282 fclose(f);
universe@48 283
universe@48 284 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
universe@48 285 UcxMapIterator iter = ucx_map_iterator(map);
universe@69 286 const char *value; size_t n;
universe@48 287 UCX_MAP_FOREACH(value, iter) {
universe@48 288 n = strlen(value);
universe@69 289 UCX_TEST_ASSERT(strncmp((const char*) pool->data[iter.index], value, n),
universe@48 290 "values of map does not match pooled values");
universe@48 291 }
universe@48 292
universe@48 293 ucx_mempool_free(pool);
universe@55 294 ucx_map_free(map);
universe@55 295 UCX_TEST_END
universe@42 296 }
olaf@44 297
olaf@44 298 UCX_TEST_IMPLEMENT(test_ucx_map_clone) {
olaf@44 299 UcxMap *map = ucx_map_new(4);
olaf@44 300
universe@71 301 ucx_map_cstr_put(map, "key1", (void*)"value1");
universe@71 302 ucx_map_cstr_put(map, "key2", (void*)"value2");
universe@71 303 ucx_map_cstr_put(map, "key3", (void*)"value3");
olaf@44 304
olaf@44 305 UcxMap *clone = ucx_map_clone(map, NULL, NULL);
olaf@44 306
universe@69 307 const char *v1 = (const char *) ucx_map_cstr_get(map, "key1");
universe@69 308 const char *v2 = (const char *) ucx_map_cstr_get(map, "key2");
universe@69 309 const char *v3 = (const char *) ucx_map_cstr_get(map, "key3");
olaf@44 310
olaf@44 311 UCX_TEST_BEGIN
olaf@44 312
olaf@44 313 UCX_TEST_ASSERT(v1 != NULL, "failed key 1");
olaf@44 314 UCX_TEST_ASSERT(v2 != NULL, "failed key 2");
olaf@44 315 UCX_TEST_ASSERT(v3 != NULL, "failed key 3");
olaf@44 316
universe@69 317 const char *c1 = (const char *) ucx_map_cstr_get(clone, "key1");
universe@69 318 const char *c2 = (const char *) ucx_map_cstr_get(clone, "key2");
universe@69 319 const char *c3 = (const char *) ucx_map_cstr_get(clone, "key3");
olaf@44 320
olaf@44 321 UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)");
olaf@44 322 UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)");
olaf@44 323 UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)");
olaf@44 324
olaf@44 325 UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1");
olaf@44 326 UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2");
olaf@44 327 UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3");
olaf@44 328
olaf@44 329 UCX_TEST_END
olaf@44 330
olaf@44 331 ucx_map_free(map);
olaf@44 332 ucx_map_free(clone);
olaf@44 333 }
universe@51 334
universe@51 335 UCX_TEST_IMPLEMENT(test_ucx_map_rehash) {
universe@51 336 UcxMap *map = ucx_map_new(4);
universe@51 337
universe@51 338 char keys[10][5];
universe@51 339 char values[10][7];
universe@51 340 for (int i = 0 ; i < 10 ; i++) {
universe@51 341 strcpy(keys[i], "key");
universe@51 342 keys[i][3] = 48+i; keys[i][4] = 0;
universe@51 343 strcpy(values[i], "value");
universe@51 344 values[i][5] = 48+i; values[i][6] = 0;
universe@51 345
universe@51 346 ucx_map_cstr_put(map, keys[i], values[i]);
universe@51 347 }
universe@51 348
olaf@52 349 ucx_map_rehash(map);
universe@51 350
universe@51 351 UCX_TEST_BEGIN
universe@51 352 UCX_TEST_ASSERT(map->size == 25, "new capacity shall be 2.5 * count");
universe@51 353 UCX_TEST_ASSERT(map->count == 10, "new map element count incorrect");
universe@51 354 for (int i = 0 ; i < 10 ; i++) {
universe@69 355 const char *value = (const char *) ucx_map_cstr_get(map, keys[i]);
universe@51 356 UCX_TEST_ASSERT(value != NULL, "new map is missing old keys");
universe@51 357 UCX_TEST_ASSERT(strncmp(value, values[i], 6) == 0,
universe@51 358 "new map contains incorrect values");
universe@51 359 }
olaf@52 360 ucx_map_rehash(map);
olaf@52 361 UCX_TEST_ASSERT(map->size == 25,
universe@51 362 "subsequent rehashing call shall not change size");
universe@51 363 UCX_TEST_END
universe@51 364
universe@51 365 ucx_map_free(map);
universe@51 366 }

mercurial