test/map_tests.c

Fri, 05 Oct 2012 11:52:53 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 05 Oct 2012 11:52:53 +0200
changeset 48
621a4430c404
parent 46
48ca036d7d9c
child 51
1c78cd19fb6b
permissions
-rw-r--r--

map can now load values from file into pooled memory

use with care when using a decoder that also allocates memory

     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_END
    99     ucx_map_free(map);
   100 }
   102 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) {
   103     UcxMap *map = (UcxMap*) mapptr;
   104     int v1 = 10;
   105     int v2 = 15;
   106     int v3 = 7;
   107     int v4 = 9;
   109     ucx_map_cstr_put(map, "v1", &v1);
   110     ucx_map_cstr_put(map, "v2", &v2);
   111     ucx_map_cstr_put(map, "v3", &v3);
   112     ucx_map_cstr_put(map, "v4", &v4);
   114     UcxMapIterator i = ucx_map_iterator(map);
   115     int check = 0;
   116     int hit = 0;
   118     int* v;
   119     UCX_MAP_FOREACH(v, i) {
   120         check += *v;
   121         hit++;
   122     }
   124     UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
   125     UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
   126 }
   128 UCX_TEST_IMPLEMENT(test_ucx_map_iterator) {
   129     UcxMap *map = ucx_map_new(16);
   130     UCX_TEST_BEGIN
   131     UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
   132     UCX_TEST_END
   133     ucx_map_free(map);
   134 }
   136 UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) {
   137     UcxMap *map = ucx_map_new(1);
   138     UCX_TEST_BEGIN
   139     UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
   140     UCX_TEST_END
   141     ucx_map_free(map);
   142 }
   144 void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) {
   145     char *string = (char*) value;
   146     size_t n = strlen(string);
   147     char *encoded = malloc(n+1);
   148     for (int i = 0 ; i < n ; i++) {
   149         encoded[i] = string[n-1-i];
   150     }
   151     encoded[n] = 0;
   152     *size = n+1;
   153     return encoded;
   154 }
   156 UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
   157     UcxMap *map = ucx_map_new(4);
   159     ucx_map_cstr_put(map, "test", "test");
   160     ucx_map_cstr_put(map, "key", "value");
   161     ucx_map_cstr_put(map, "other.very.long.key", "value");
   162     ucx_map_cstr_put(map, "testkey", "testvalue");
   163     ucx_map_cstr_put(map, "simple", "not a key but an extremely long value "
   164             "to test if the buffer extension works as designed");
   166     FILE *f = fopen("test_ucx_map_store", "w");
   167     int r;
   169     fwrite(" # comment test\n", 1, 16, f);
   170     r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL);
   171     fwrite("!discard this", 1, 13, f);
   173     fclose(f);
   174     ucx_map_free(map);
   175     map = ucx_map_new(1);
   176     f = fopen("test_ucx_map_store", "r");
   177     UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT;
   178     r += ucx_map_load_enc(map, f, allocator,
   179             test_ucx_map_store_load_encdec, NULL);
   180     fclose(f);
   181     unlink("test_ucx_map_store");
   183     UCX_TEST_BEGIN
   184     char *value;
   185     UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
   187     value = ucx_map_cstr_get(map, "test");
   188     UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
   189     UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");
   191     value = ucx_map_cstr_get(map, "key");
   192     UCX_TEST_ASSERT(value != NULL, "value not found for key: key");
   193     UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");
   195     value = ucx_map_cstr_get(map, "other.very.long.key");
   196     UCX_TEST_ASSERT(value != NULL,
   197             "value not found for key: other.very.long.key");
   198     UCX_TEST_ASSERT(strcmp(value, "value") == 0,
   199             "value error for key: other.very.long.key");
   201     value = ucx_map_cstr_get(map, "testkey");
   202     UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey");
   203     UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
   204             "value error for key: testkey");
   206     value = ucx_map_cstr_get(map, "simple");
   207     UCX_TEST_ASSERT(value != NULL, "value not found for key: simple");
   208     UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
   209             "to test if the buffer extension works as designed") == 0,
   210             "value error for key: simple");
   212     UCX_TEST_END
   213 }
   215 UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) {
   216     UcxMap *map = ucx_map_new(4);
   218     ucx_map_cstr_put(map, "test", "test");
   219     ucx_map_cstr_put(map, "key", "value");
   220     ucx_map_cstr_put(map, "testkey", "testvalue");
   221     ucx_map_cstr_put(map, "simple", "a simple value");
   223     FILE *f = fopen("test_ucx_map_store", "w");
   224     int r;
   225     r = ucx_map_store_enc(map, f, NULL, NULL);
   226     fclose(f);
   227     ucx_map_free(map);
   229     UcxMempool *pool = ucx_mempool_new(4);
   230     map = ucx_map_new(4);
   231     f = fopen("test_ucx_map_store", "r");
   232     UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool);
   233     r += ucx_map_load_enc(map, f, allocator,
   234             test_ucx_map_store_load_encdec, NULL);
   235     fclose(f);
   236     unlink("test_ucx_map_store");
   238     UCX_TEST_BEGIN
   239     UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
   240     UcxMapIterator iter = ucx_map_iterator(map);
   241     char *value; size_t n;
   242     UCX_MAP_FOREACH(value, iter) {
   243         n = strlen(value);
   244         UCX_TEST_ASSERT(strncmp(pool->data[iter.index], value, n),
   245                 "values of map does not match pooled values");
   246     }
   247     UCX_TEST_END
   249     ucx_mempool_free(pool);
   250 }
   252 UCX_TEST_IMPLEMENT(test_ucx_map_clone) {
   253     UcxMap *map = ucx_map_new(4);
   255     ucx_map_cstr_put(map, "key1", "value1");
   256     ucx_map_cstr_put(map, "key2", "value2");
   257     ucx_map_cstr_put(map, "key3", "value3");
   259     UcxMap *clone = ucx_map_clone(map, NULL, NULL);
   261     char *v1 = ucx_map_cstr_get(map, "key1");
   262     char *v2 = ucx_map_cstr_get(map, "key2");
   263     char *v3 = ucx_map_cstr_get(map, "key3");
   265     UCX_TEST_BEGIN
   267     UCX_TEST_ASSERT(v1 != NULL, "failed key 1");
   268     UCX_TEST_ASSERT(v2 != NULL, "failed key 2");
   269     UCX_TEST_ASSERT(v3 != NULL, "failed key 3");
   271     char *c1 = ucx_map_cstr_get(clone, "key1");
   272     char *c2 = ucx_map_cstr_get(clone, "key2");
   273     char *c3 = ucx_map_cstr_get(clone, "key3");
   275     UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)");
   276     UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)");
   277     UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)");
   279     UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1");
   280     UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2");
   281     UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3");
   283     UCX_TEST_END
   285     ucx_map_free(map);
   286     ucx_map_free(clone);
   287 }

mercurial