test/map_tests.c

Mon, 08 Oct 2012 12:29:27 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 08 Oct 2012 12:29:27 +0200
changeset 53
e533c170bfb8
parent 52
34f50d0bada4
child 55
180bc6b18fec
permissions
-rw-r--r--

added ucx_map_remove

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

mercurial