test/map_tests.c

Fri, 25 May 2012 17:39:27 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 25 May 2012 17:39:27 +0200
changeset 31
91ac86557290
parent 29
bce0d7f2912b
child 32
c7af4ec56e19
permissions
-rw-r--r--

added map iterator

     1 /*
     2  *
     3  */
     5 #include "map_tests.h"
     7 UCX_TEST_BEGIN(test_ucx_map_new) {
     8     UcxMap *map = ucx_map_new(16);
    10     UCX_TEST_ASSERT(map->size == 16, "wrong size")
    11     UCX_TEST_ASSERT(map->map != NULL, "failed")
    13     ucx_map_free(map);
    15     UCX_TEST_END
    16 }
    18 UCX_TEST_BEGIN(test_ucx_key) {
    20     UcxKey key = ucx_key("This is a text.", 15);
    21     UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed")
    22     UCX_TEST_ASSERT(key.len == 15, "failed")
    23     UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed")
    25     UCX_TEST_END
    26 }
    28 UCX_TEST_BEGIN(test_ucx_map_put) {
    30     UcxMap *map = ucx_map_new(4);
    32     int td[5];
    33     td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
    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_map_free(map);
    68     UCX_TEST_END
    69 }
    71 UCX_TEST_BEGIN(test_ucx_map_get) {
    72     UCX_TEST_END
    73 }
    75 UCX_TEST_BEGIN(test_ucx_map_iterator) {
    76     UcxMap *map = ucx_map_new(16);
    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");
   100     ucx_map_free(map);
   102     map = ucx_map_new(1);
   103     ucx_map_cstr_put(map, "v1", &v1);
   104     ucx_map_cstr_put(map, "v2", &v2);
   105     ucx_map_cstr_put(map, "v3", &v3);
   106     ucx_map_cstr_put(map, "v4", &v4);
   108     i = ucx_map_iterator(map);
   109     check = 0;
   110     hit = 0;
   112     UCX_MAP_FOREACH(int*, v, map, i) {
   113         check += *v;
   114         hit++;
   115     }
   117     UCX_TEST_ASSERT(hit == 4, "test2: wrong number of hits");
   118     UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test2: wrong result");
   121     ucx_map_free(map);
   123     UCX_TEST_END
   124 }

mercurial