test/map_tests.c

changeset 29
bce0d7f2912b
parent 20
db7d9860dbbd
child 31
91ac86557290
     1.1 --- a/test/map_tests.c	Mon Feb 20 15:30:45 2012 +0100
     1.2 +++ b/test/map_tests.c	Tue Feb 21 01:13:17 2012 +0100
     1.3 @@ -2,43 +2,72 @@
     1.4   *
     1.5   */
     1.6  
     1.7 -#include <stdio.h>
     1.8 -#include <stdlib.h>
     1.9 -#include <string.h>
    1.10 -
    1.11  #include "map_tests.h"
    1.12  
    1.13 -int map_tests() {
    1.14 -    printf("   Test ucx_map_new\n");
    1.15 +UCX_TEST_BEGIN(test_ucx_map_new) {
    1.16      UcxMap *map = ucx_map_new(16);
    1.17 -    if(map == NULL) {
    1.18 -        return -1;
    1.19 -    }
    1.20 +    
    1.21 +    UCX_TEST_ASSERT(map->size == 16, "wrong size")
    1.22 +    UCX_TEST_ASSERT(map->map != NULL, "failed")
    1.23 +    
    1.24 +    ucx_map_free(map);
    1.25 +    
    1.26 +    UCX_TEST_END
    1.27 +}
    1.28  
    1.29 -    printf("   Test ucx_map_put\n");
    1.30 -    char *txt = "text/plain";
    1.31 -    char *xml = "text/xml";
    1.32 -    ucx_map_cstr_put(map, "txt", txt);
    1.33 -    ucx_map_cstr_put(map, "xml", xml);
    1.34 +UCX_TEST_BEGIN(test_ucx_key) {
    1.35 +    
    1.36 +    UcxKey key = ucx_key("This is a text.", 15);
    1.37 +    UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed")
    1.38 +    UCX_TEST_ASSERT(key.len == 15, "failed")
    1.39 +    UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed")
    1.40 +    
    1.41 +    UCX_TEST_END
    1.42 +}
    1.43  
    1.44 -    printf("   Test ucx_map_get\n");
    1.45 -    if(ucx_map_cstr_get(map, "txt") != txt) {
    1.46 -        fprintf(stderr, "ucx_map_get failed\n");
    1.47 -        return -1;
    1.48 -    }
    1.49 -    char xmlkey[4];
    1.50 -    xmlkey[0] = 'x';
    1.51 -    xmlkey[1] = 'm';
    1.52 -    xmlkey[2] = 'l';
    1.53 -    xmlkey[3] = 0;
    1.54 -    if(ucx_map_cstr_get(map, xmlkey) != xml) {
    1.55 -        fprintf(stderr, "ucx_map_get failed\n");
    1.56 -        return -1;
    1.57 -    }
    1.58 -    if(ucx_map_cstr_get(map, "nokey") != NULL) {
    1.59 -        fprintf(stderr, "ucx_map_get failed\n");
    1.60 -        return -1;
    1.61 -    }
    1.62 +UCX_TEST_BEGIN(test_ucx_map_put) {
    1.63 +    
    1.64 +    UcxMap *map = ucx_map_new(4);
    1.65 +    
    1.66 +    int td[5];
    1.67 +    td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
    1.68  
    1.69 -    return 0;
    1.70 +    ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
    1.71 +    ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
    1.72 +    ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
    1.73 +    ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
    1.74 +    ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
    1.75 +    
    1.76 +    UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0")
    1.77 +    UCX_TEST_ASSERT(map->map[0]->next != NULL, "no list at slot 0")
    1.78 +    UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], "failed Key2")
    1.79 +    UCX_TEST_ASSERT(map->map[0]->next->next != NULL, "list corrupt at slot 0")
    1.80 +    UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4],
    1.81 +            "failed Key4")
    1.82 +    UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL,
    1.83 +            "slot 0 not terminated")
    1.84 +
    1.85 +    UCX_TEST_ASSERT(map->map[1] == NULL, "slot 1 not terminated")
    1.86 +
    1.87 +    UCX_TEST_ASSERT(*((int*)map->map[2]->data) == td[3], "failed KeY3")
    1.88 +    UCX_TEST_ASSERT(map->map[2]->next == NULL, "slot 2 not terminated")
    1.89 +
    1.90 +    UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1")
    1.91 +
    1.92 +    ucx_map_cstr_put(map, "Key0", &td[3]); /* 0 */
    1.93 +    
    1.94 +    UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[3], "overwrite failed")
    1.95 +    UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2],
    1.96 +            "overwrite failed")
    1.97 +    UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4], 
    1.98 +            "overwrite failed")
    1.99 +    UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed")
   1.100 +    
   1.101 +    ucx_map_free(map);
   1.102 +    
   1.103 +    UCX_TEST_END
   1.104  }
   1.105 +
   1.106 +UCX_TEST_BEGIN(test_ucx_map_get) {
   1.107 +    UCX_TEST_END
   1.108 +}

mercurial