test/map_tests.c

changeset 48
621a4430c404
parent 46
48ca036d7d9c
child 51
1c78cd19fb6b
     1.1 --- a/test/map_tests.c	Fri Oct 05 10:25:33 2012 +0200
     1.2 +++ b/test/map_tests.c	Fri Oct 05 11:52:53 2012 +0200
     1.3 @@ -141,7 +141,7 @@
     1.4      ucx_map_free(map);
     1.5  }
     1.6  
     1.7 -void* test_ucx_map_store_load_encdec(void *value, void *data) {
     1.8 +void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) {
     1.9      char *string = (char*) value;
    1.10      size_t n = strlen(string);
    1.11      char *encoded = malloc(n+1);
    1.12 @@ -149,6 +149,7 @@
    1.13          encoded[i] = string[n-1-i];
    1.14      }
    1.15      encoded[n] = 0;
    1.16 +    *size = n+1;
    1.17      return encoded;
    1.18  }
    1.19  
    1.20 @@ -173,7 +174,11 @@
    1.21      ucx_map_free(map);
    1.22      map = ucx_map_new(1);
    1.23      f = fopen("test_ucx_map_store", "r");
    1.24 -    r += ucx_map_load_enc(map, f, test_ucx_map_store_load_encdec, NULL);
    1.25 +    UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT;
    1.26 +    r += ucx_map_load_enc(map, f, allocator,
    1.27 +            test_ucx_map_store_load_encdec, NULL);
    1.28 +    fclose(f);
    1.29 +    unlink("test_ucx_map_store");
    1.30  
    1.31      UCX_TEST_BEGIN
    1.32      char *value;
    1.33 @@ -205,9 +210,43 @@
    1.34              "value error for key: simple");
    1.35  
    1.36      UCX_TEST_END
    1.37 +}
    1.38 +
    1.39 +UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) {
    1.40 +    UcxMap *map = ucx_map_new(4);
    1.41 +
    1.42 +    ucx_map_cstr_put(map, "test", "test");
    1.43 +    ucx_map_cstr_put(map, "key", "value");
    1.44 +    ucx_map_cstr_put(map, "testkey", "testvalue");
    1.45 +    ucx_map_cstr_put(map, "simple", "a simple value");
    1.46 +
    1.47 +    FILE *f = fopen("test_ucx_map_store", "w");
    1.48 +    int r;
    1.49 +    r = ucx_map_store_enc(map, f, NULL, NULL);
    1.50      fclose(f);
    1.51 +    ucx_map_free(map);
    1.52  
    1.53 +    UcxMempool *pool = ucx_mempool_new(4);
    1.54 +    map = ucx_map_new(4);
    1.55 +    f = fopen("test_ucx_map_store", "r");
    1.56 +    UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool);
    1.57 +    r += ucx_map_load_enc(map, f, allocator,
    1.58 +            test_ucx_map_store_load_encdec, NULL);
    1.59 +    fclose(f);
    1.60      unlink("test_ucx_map_store");
    1.61 +
    1.62 +    UCX_TEST_BEGIN
    1.63 +    UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
    1.64 +    UcxMapIterator iter = ucx_map_iterator(map);
    1.65 +    char *value; size_t n;
    1.66 +    UCX_MAP_FOREACH(value, iter) {
    1.67 +        n = strlen(value);
    1.68 +        UCX_TEST_ASSERT(strncmp(pool->data[iter.index], value, n),
    1.69 +                "values of map does not match pooled values");
    1.70 +    }
    1.71 +    UCX_TEST_END
    1.72 +
    1.73 +    ucx_mempool_free(pool);
    1.74  }
    1.75  
    1.76  UCX_TEST_IMPLEMENT(test_ucx_map_clone) {

mercurial