test/map_tests.c

Wed, 27 Feb 2013 09:41:17 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Feb 2013 09:41:17 +0100
changeset 88
18823857ce79
parent 80
0125e4089f88
child 95
ecfdc1c4a552
permissions
-rw-r--r--

variadic test subroutines

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

mercurial