test/map_tests.c

Thu, 31 May 2012 12:51:22 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 31 May 2012 12:51:22 +0200
changeset 33
9c219a62070d
parent 32
c7af4ec56e19
child 34
0dcd2ca2a223
permissions
-rw-r--r--

major refactoring of test framework

     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     UCX_TEST_BEGIN
    72     UCX_TEST_ASSERT(0, "not implemented");
    73     UCX_TEST_END
    74 }
    76 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) {
    77     UcxMap *map = (UcxMap*) mapptr;
    78     int v1 = 10;
    79     int v2 = 15;
    80     int v3 = 7;
    81     int v4 = 9;
    83     ucx_map_cstr_put(map, "v1", &v1);
    84     ucx_map_cstr_put(map, "v2", &v2);
    85     ucx_map_cstr_put(map, "v3", &v3);
    86     ucx_map_cstr_put(map, "v4", &v4);
    88     UcxMapIterator i = ucx_map_iterator(map);
    89     int check = 0;
    90     int hit = 0;
    92     UCX_MAP_FOREACH(int*, v, map, i) {
    93         check += *v;
    94         hit++;
    95     }
    97     UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
    98     UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
    99 }
   101 UCX_TEST_IMPLEMENT(test_ucx_map_iterator) {
   102     UcxMap *map = ucx_map_new(16);
   103     UCX_TEST_BEGIN
   104     UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
   105     UCX_TEST_END
   106     ucx_map_free(map);
   107 }
   109 UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) {
   110     UcxMap *map = ucx_map_new(1);
   111     UCX_TEST_BEGIN
   112     UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
   113     UCX_TEST_END
   114     ucx_map_free(map);
   115 }

mercurial