test/map_tests.c

Thu, 04 Oct 2012 16:03:18 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 04 Oct 2012 16:03:18 +0200
changeset 42
ff3dd1ee7dee
parent 41
7f90a03e186e
child 43
02f38adea013
permissions
-rw-r--r--

(broken-commit) - added load and store functions, tests failing

some evil crash happens when executing the test - remove the strcmp calls in the test case for the store and load function and everything "works"

the error must be somewhere else - maybe something that should not be freed is freed during the test

     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("This is a text.", 15);
    19     UCX_TEST_BEGIN
    20     UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed");
    21     UCX_TEST_ASSERT(key.len == 15, "failed");
    22     UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed");
    24     UCX_TEST_END
    25 }
    27 UCX_TEST_IMPLEMENT(test_ucx_map_put) {
    29     UcxMap *map = ucx_map_new(4);
    31     int td[5];
    32     td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
    34     UCX_TEST_BEGIN
    35     ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
    36     ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
    37     ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
    38     ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
    39     ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
    41     UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0");
    42     UCX_TEST_ASSERT(map->map[0]->next != NULL, "no list at slot 0");
    43     UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], "failed Key2");
    44     UCX_TEST_ASSERT(map->map[0]->next->next != NULL, "list corrupt at slot 0");
    45     UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4],
    46             "failed Key4")
    47     UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL,
    48             "slot 0 not terminated")
    50     UCX_TEST_ASSERT(map->map[1] == NULL, "slot 1 not terminated");
    52     UCX_TEST_ASSERT(*((int*)map->map[2]->data) == td[3], "failed KeY3");
    53     UCX_TEST_ASSERT(map->map[2]->next == NULL, "slot 2 not terminated");
    55     UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1");
    57     ucx_map_cstr_put(map, "Key0", &td[3]); /* 0 */
    59     UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[3], "overwrite failed");
    60     UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2],
    61             "overwrite failed")
    62     UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4], 
    63             "overwrite failed")
    64     UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed");
    66     UCX_TEST_END
    67     ucx_map_free(map);
    68 }
    70 UCX_TEST_IMPLEMENT(test_ucx_map_get) {
    71     UcxMap *map = ucx_map_new(4);
    73     int td[5];
    74     td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
    76     ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
    77     ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
    78     ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
    79     ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
    80     ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
    81     UCX_TEST_BEGIN
    83     td[0] = *((int*)ucx_map_cstr_get(map, "Key0"));
    84     td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
    85     td[2] = *((int*)ucx_map_cstr_get(map, "Key2"));
    86     td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
    87     td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
    88     UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
    89     UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
    90     UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
    91     UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
    92     UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
    94     UCX_TEST_END
    95     ucx_map_free(map);
    96 }
    98 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) {
    99     UcxMap *map = (UcxMap*) mapptr;
   100     int v1 = 10;
   101     int v2 = 15;
   102     int v3 = 7;
   103     int v4 = 9;
   105     ucx_map_cstr_put(map, "v1", &v1);
   106     ucx_map_cstr_put(map, "v2", &v2);
   107     ucx_map_cstr_put(map, "v3", &v3);
   108     ucx_map_cstr_put(map, "v4", &v4);
   110     UcxMapIterator i = ucx_map_iterator(map);
   111     int check = 0;
   112     int hit = 0;
   114     int* v;
   115     UCX_MAP_FOREACH(v, i) {
   116         check += *v;
   117         hit++;
   118     }
   120     UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
   121     UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
   122 }
   124 UCX_TEST_IMPLEMENT(test_ucx_map_iterator) {
   125     UcxMap *map = ucx_map_new(16);
   126     UCX_TEST_BEGIN
   127     UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
   128     UCX_TEST_END
   129     ucx_map_free(map);
   130 }
   132 UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) {
   133     UcxMap *map = ucx_map_new(1);
   134     UCX_TEST_BEGIN
   135     UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
   136     UCX_TEST_END
   137     ucx_map_free(map);
   138 }
   140 UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
   141     UcxMap *map = ucx_map_new(4);
   143     ucx_map_cstr_put(map, "test", "test");
   144     ucx_map_cstr_put(map, "key", "value");
   145     ucx_map_cstr_put(map, "other.very.long.key", "value");
   146     ucx_map_cstr_put(map, "testkey", "testvalue");
   147     ucx_map_cstr_put(map, "simple", "not a key but an extremely long value "
   148             "to test if the buffer extension works as designed");
   150     FILE *f = fopen("test_ucx_map_store", "w");
   151     int r;
   153     fwrite(" # comment test", 1, 15, f);
   154     r = ucx_map_store(map, f);
   155     fwrite("#discard this", 1, 13, f);
   157     fclose(f);
   158     ucx_map_free(map);
   159     map = ucx_map_new(1);
   160     f = fopen("test_ucx_map_store", "r");
   161     r += ucx_map_load(map, f);
   163     UCX_TEST_BEGIN
   164     char *value;
   165     UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
   167     value = ucx_map_cstr_get(map, "test");
   168     UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");
   170     value = ucx_map_cstr_get(map, "key");
   171     UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");
   173     value = ucx_map_cstr_get(map, "other.very.long.key");
   174     UCX_TEST_ASSERT(strcmp(value, "value") == 0,
   175             "value error for key: other.very.long.key");
   177     value = ucx_map_cstr_get(map, "testkey");
   178     UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
   179             "value error for key: testkey");
   181     value = ucx_map_cstr_get(map, "simple");
   182     UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
   183             "to test if the buffer extension works as designed") == 0,
   184             "value error for key: simple");
   186     UCX_TEST_END
   187     fclose(f);
   189     unlink("test_ucx_map_store");
   190 }

mercurial