test/map_tests.c

Thu, 04 Oct 2012 18:46:57 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 04 Oct 2012 18:46:57 +0200
changeset 44
46356d74e873
parent 43
02f38adea013
child 46
48ca036d7d9c
permissions
-rw-r--r--

added map clone

     1 /*
     2  *
     3  */
     5 #include "map_tests.h"
     7 #ifndef _WIN32
     8 #include <unistd.h>
     9 #endif /* not _WIN32 */
    11 UCX_TEST_IMPLEMENT(test_ucx_map_new) {
    12     UcxMap *map = ucx_map_new(16);
    13     UCX_TEST_BEGIN
    14     UCX_TEST_ASSERT(map->size == 16, "wrong size");
    15     UCX_TEST_ASSERT(map->map != NULL, "failed");
    17     UCX_TEST_END
    18     ucx_map_free(map);
    19 }
    21 UCX_TEST_IMPLEMENT(test_ucx_key) {
    22     UcxKey key = ucx_key("This is a text.", 15);
    23     UCX_TEST_BEGIN
    24     UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed");
    25     UCX_TEST_ASSERT(key.len == 15, "failed");
    26     UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed");
    28     UCX_TEST_END
    29 }
    31 UCX_TEST_IMPLEMENT(test_ucx_map_put) {
    33     UcxMap *map = ucx_map_new(4);
    35     int td[5];
    36     td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
    38     UCX_TEST_BEGIN
    39     ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
    40     ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
    41     ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
    42     ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
    43     ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
    45     UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0");
    46     UCX_TEST_ASSERT(map->map[0]->next != NULL, "no list at slot 0");
    47     UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], "failed Key2");
    48     UCX_TEST_ASSERT(map->map[0]->next->next != NULL, "list corrupt at slot 0");
    49     UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4],
    50             "failed Key4")
    51     UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL,
    52             "slot 0 not terminated")
    54     UCX_TEST_ASSERT(map->map[1] == NULL, "slot 1 not terminated");
    56     UCX_TEST_ASSERT(*((int*)map->map[2]->data) == td[3], "failed KeY3");
    57     UCX_TEST_ASSERT(map->map[2]->next == NULL, "slot 2 not terminated");
    59     UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1");
    61     ucx_map_cstr_put(map, "Key0", &td[3]); /* 0 */
    63     UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[3], "overwrite failed");
    64     UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2],
    65             "overwrite failed")
    66     UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4], 
    67             "overwrite failed")
    68     UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed");
    70     UCX_TEST_END
    71     ucx_map_free(map);
    72 }
    74 UCX_TEST_IMPLEMENT(test_ucx_map_get) {
    75     UcxMap *map = ucx_map_new(4);
    77     int td[5];
    78     td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
    80     ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
    81     ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
    82     ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
    83     ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
    84     ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
    85     UCX_TEST_BEGIN
    87     td[0] = *((int*)ucx_map_cstr_get(map, "Key0"));
    88     td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
    89     td[2] = *((int*)ucx_map_cstr_get(map, "Key2"));
    90     td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
    91     td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
    92     UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
    93     UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
    94     UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
    95     UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
    96     UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
    98     UCX_TEST_END
    99     ucx_map_free(map);
   100 }
   102 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) {
   103     UcxMap *map = (UcxMap*) mapptr;
   104     int v1 = 10;
   105     int v2 = 15;
   106     int v3 = 7;
   107     int v4 = 9;
   109     ucx_map_cstr_put(map, "v1", &v1);
   110     ucx_map_cstr_put(map, "v2", &v2);
   111     ucx_map_cstr_put(map, "v3", &v3);
   112     ucx_map_cstr_put(map, "v4", &v4);
   114     UcxMapIterator i = ucx_map_iterator(map);
   115     int check = 0;
   116     int hit = 0;
   118     int* v;
   119     UCX_MAP_FOREACH(v, i) {
   120         check += *v;
   121         hit++;
   122     }
   124     UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
   125     UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
   126 }
   128 UCX_TEST_IMPLEMENT(test_ucx_map_iterator) {
   129     UcxMap *map = ucx_map_new(16);
   130     UCX_TEST_BEGIN
   131     UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
   132     UCX_TEST_END
   133     ucx_map_free(map);
   134 }
   136 UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) {
   137     UcxMap *map = ucx_map_new(1);
   138     UCX_TEST_BEGIN
   139     UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
   140     UCX_TEST_END
   141     ucx_map_free(map);
   142 }
   144 UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
   145     UcxMap *map = ucx_map_new(4);
   147     ucx_map_cstr_put(map, "test", "test");
   148     ucx_map_cstr_put(map, "key", "value");
   149     ucx_map_cstr_put(map, "other.very.long.key", "value");
   150     ucx_map_cstr_put(map, "testkey", "testvalue");
   151     ucx_map_cstr_put(map, "simple", "not a key but an extremely long value "
   152             "to test if the buffer extension works as designed");
   154     FILE *f = fopen("test_ucx_map_store", "w");
   155     int r;
   157     fwrite(" # comment test\n", 1, 16, f);
   158     r = ucx_map_store(map, f);
   159     fwrite("!discard this", 1, 13, f);
   161     fclose(f);
   162     ucx_map_free(map);
   163     map = ucx_map_new(1);
   164     f = fopen("test_ucx_map_store", "r");
   165     r += ucx_map_load(map, f);
   167     UCX_TEST_BEGIN
   168     char *value;
   169     UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
   171     value = ucx_map_cstr_get(map, "test");
   172     UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
   173     UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");
   175     value = ucx_map_cstr_get(map, "key");
   176     UCX_TEST_ASSERT(value != NULL, "value not found for key: key");
   177     UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");
   179     value = ucx_map_cstr_get(map, "other.very.long.key");
   180     UCX_TEST_ASSERT(value != NULL,
   181             "value not found for key: other.very.long.key");
   182     UCX_TEST_ASSERT(strcmp(value, "value") == 0,
   183             "value error for key: other.very.long.key");
   185     value = ucx_map_cstr_get(map, "testkey");
   186     UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey");
   187     UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
   188             "value error for key: testkey");
   190     value = ucx_map_cstr_get(map, "simple");
   191     UCX_TEST_ASSERT(value != NULL, "value not found for key: simple");
   192     UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
   193             "to test if the buffer extension works as designed") == 0,
   194             "value error for key: simple");
   196     UCX_TEST_END
   197     fclose(f);
   199     unlink("test_ucx_map_store");
   200 }
   202 UCX_TEST_IMPLEMENT(test_ucx_map_clone) {
   203     UcxMap *map = ucx_map_new(4);
   205     ucx_map_cstr_put(map, "key1", "value1");
   206     ucx_map_cstr_put(map, "key2", "value2");
   207     ucx_map_cstr_put(map, "key3", "value3");
   209     UcxMap *clone = ucx_map_clone(map, NULL, NULL);
   211     char *v1 = ucx_map_cstr_get(map, "key1");
   212     char *v2 = ucx_map_cstr_get(map, "key2");
   213     char *v3 = ucx_map_cstr_get(map, "key3");
   215     UCX_TEST_BEGIN
   217     UCX_TEST_ASSERT(v1 != NULL, "failed key 1");
   218     UCX_TEST_ASSERT(v2 != NULL, "failed key 2");
   219     UCX_TEST_ASSERT(v3 != NULL, "failed key 3");
   221     char *c1 = ucx_map_cstr_get(clone, "key1");
   222     char *c2 = ucx_map_cstr_get(clone, "key2");
   223     char *c3 = ucx_map_cstr_get(clone, "key3");
   225     UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)");
   226     UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)");
   227     UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)");
   229     UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1");
   230     UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2");
   231     UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3");
   233     UCX_TEST_END
   235     ucx_map_free(map);
   236     ucx_map_free(clone);
   237 }

mercurial