test/map_tests.c

Fri, 05 Oct 2012 11:55:36 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 05 Oct 2012 11:55:36 +0200
changeset 49
1771360b740d
parent 48
621a4430c404
child 51
1c78cd19fb6b
permissions
-rw-r--r--

Merge with 22fb97e9f58a0a8b18db7f81b7392dae0cd64571

/*
 *
 */

#include "map_tests.h"

#ifndef _WIN32
#include <unistd.h>
#endif /* not _WIN32 */

UCX_TEST_IMPLEMENT(test_ucx_map_new) {
    UcxMap *map = ucx_map_new(16);
    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(map->size == 16, "wrong size");
    UCX_TEST_ASSERT(map->map != NULL, "failed");
    
    UCX_TEST_END
    ucx_map_free(map);
}

UCX_TEST_IMPLEMENT(test_ucx_key) {
    UcxKey key = ucx_key("This is a text.", 15);
    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed");
    UCX_TEST_ASSERT(key.len == 15, "failed");
    UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed");
    
    UCX_TEST_END
}

UCX_TEST_IMPLEMENT(test_ucx_map_put) {
    
    UcxMap *map = ucx_map_new(4);
    
    int td[5];
    td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;

    UCX_TEST_BEGIN
    ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
    ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
    ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
    ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
    ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
    
    UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0");
    UCX_TEST_ASSERT(map->map[0]->next != NULL, "no list at slot 0");
    UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], "failed Key2");
    UCX_TEST_ASSERT(map->map[0]->next->next != NULL, "list corrupt at slot 0");
    UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4],
            "failed Key4")
    UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL,
            "slot 0 not terminated")

    UCX_TEST_ASSERT(map->map[1] == NULL, "slot 1 not terminated");

    UCX_TEST_ASSERT(*((int*)map->map[2]->data) == td[3], "failed KeY3");
    UCX_TEST_ASSERT(map->map[2]->next == NULL, "slot 2 not terminated");

    UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1");

    ucx_map_cstr_put(map, "Key0", &td[3]); /* 0 */
    
    UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[3], "overwrite failed");
    UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2],
            "overwrite failed")
    UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4], 
            "overwrite failed")
    UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed");
    
    UCX_TEST_END
    ucx_map_free(map);
}

UCX_TEST_IMPLEMENT(test_ucx_map_get) {
    UcxMap *map = ucx_map_new(4);

    int td[5];
    td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;

    ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
    ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
    ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
    ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
    ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
    UCX_TEST_BEGIN

    td[0] = *((int*)ucx_map_cstr_get(map, "Key0"));
    td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
    td[2] = *((int*)ucx_map_cstr_get(map, "Key2"));
    td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
    td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
    UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
    UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
    UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
    UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
    UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");

    UCX_TEST_END
    ucx_map_free(map);
}

UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) {
    UcxMap *map = (UcxMap*) mapptr;
    int v1 = 10;
    int v2 = 15;
    int v3 = 7;
    int v4 = 9;

    ucx_map_cstr_put(map, "v1", &v1);
    ucx_map_cstr_put(map, "v2", &v2);
    ucx_map_cstr_put(map, "v3", &v3);
    ucx_map_cstr_put(map, "v4", &v4);

    UcxMapIterator i = ucx_map_iterator(map);
    int check = 0;
    int hit = 0;

    int* v;
    UCX_MAP_FOREACH(v, i) {
        check += *v;
        hit++;
    }

    UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
    UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
}

UCX_TEST_IMPLEMENT(test_ucx_map_iterator) {
    UcxMap *map = ucx_map_new(16);
    UCX_TEST_BEGIN
    UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
    UCX_TEST_END
    ucx_map_free(map);
}

UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) {
    UcxMap *map = ucx_map_new(1);
    UCX_TEST_BEGIN
    UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
    UCX_TEST_END
    ucx_map_free(map);
}

void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) {
    char *string = (char*) value;
    size_t n = strlen(string);
    char *encoded = malloc(n+1);
    for (int i = 0 ; i < n ; i++) {
        encoded[i] = string[n-1-i];
    }
    encoded[n] = 0;
    *size = n+1;
    return encoded;
}

UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
    UcxMap *map = ucx_map_new(4);

    ucx_map_cstr_put(map, "test", "test");
    ucx_map_cstr_put(map, "key", "value");
    ucx_map_cstr_put(map, "other.very.long.key", "value");
    ucx_map_cstr_put(map, "testkey", "testvalue");
    ucx_map_cstr_put(map, "simple", "not a key but an extremely long value "
            "to test if the buffer extension works as designed");

    FILE *f = fopen("test_ucx_map_store", "w");
    int r;

    fwrite(" # comment test\n", 1, 16, f);
    r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL);
    fwrite("!discard this", 1, 13, f);

    fclose(f);
    ucx_map_free(map);
    map = ucx_map_new(1);
    f = fopen("test_ucx_map_store", "r");
    UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT;
    r += ucx_map_load_enc(map, f, allocator,
            test_ucx_map_store_load_encdec, NULL);
    fclose(f);
    unlink("test_ucx_map_store");

    UCX_TEST_BEGIN
    char *value;
    UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");

    value = ucx_map_cstr_get(map, "test");
    UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
    UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");

    value = ucx_map_cstr_get(map, "key");
    UCX_TEST_ASSERT(value != NULL, "value not found for key: key");
    UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");

    value = ucx_map_cstr_get(map, "other.very.long.key");
    UCX_TEST_ASSERT(value != NULL,
            "value not found for key: other.very.long.key");
    UCX_TEST_ASSERT(strcmp(value, "value") == 0,
            "value error for key: other.very.long.key");

    value = ucx_map_cstr_get(map, "testkey");
    UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey");
    UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
            "value error for key: testkey");

    value = ucx_map_cstr_get(map, "simple");
    UCX_TEST_ASSERT(value != NULL, "value not found for key: simple");
    UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
            "to test if the buffer extension works as designed") == 0,
            "value error for key: simple");

    UCX_TEST_END
}

UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) {
    UcxMap *map = ucx_map_new(4);

    ucx_map_cstr_put(map, "test", "test");
    ucx_map_cstr_put(map, "key", "value");
    ucx_map_cstr_put(map, "testkey", "testvalue");
    ucx_map_cstr_put(map, "simple", "a simple value");

    FILE *f = fopen("test_ucx_map_store", "w");
    int r;
    r = ucx_map_store_enc(map, f, NULL, NULL);
    fclose(f);
    ucx_map_free(map);

    UcxMempool *pool = ucx_mempool_new(4);
    map = ucx_map_new(4);
    f = fopen("test_ucx_map_store", "r");
    UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool);
    r += ucx_map_load_enc(map, f, allocator,
            test_ucx_map_store_load_encdec, NULL);
    fclose(f);
    unlink("test_ucx_map_store");

    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
    UcxMapIterator iter = ucx_map_iterator(map);
    char *value; size_t n;
    UCX_MAP_FOREACH(value, iter) {
        n = strlen(value);
        UCX_TEST_ASSERT(strncmp(pool->data[iter.index], value, n),
                "values of map does not match pooled values");
    }
    UCX_TEST_END

    ucx_mempool_free(pool);
}

UCX_TEST_IMPLEMENT(test_ucx_map_clone) {
    UcxMap *map = ucx_map_new(4);
    
    ucx_map_cstr_put(map, "key1", "value1");
    ucx_map_cstr_put(map, "key2", "value2");
    ucx_map_cstr_put(map, "key3", "value3");
    
    UcxMap *clone = ucx_map_clone(map, NULL, NULL);
    
    char *v1 = ucx_map_cstr_get(map, "key1");
    char *v2 = ucx_map_cstr_get(map, "key2");
    char *v3 = ucx_map_cstr_get(map, "key3");
    
    UCX_TEST_BEGIN
    
    UCX_TEST_ASSERT(v1 != NULL, "failed key 1");
    UCX_TEST_ASSERT(v2 != NULL, "failed key 2");
    UCX_TEST_ASSERT(v3 != NULL, "failed key 3");
    
    char *c1 = ucx_map_cstr_get(clone, "key1");
    char *c2 = ucx_map_cstr_get(clone, "key2");
    char *c3 = ucx_map_cstr_get(clone, "key3");
    
    UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)");
    UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)");
    UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)");
    
    UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1");
    UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2");
    UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3");
    
    UCX_TEST_END
    
    ucx_map_free(map);
    ucx_map_free(clone);
}

mercurial