1 /* |
1 /* |
2 * |
2 * |
3 */ |
3 */ |
4 |
4 |
5 #include <stdio.h> |
|
6 #include <stdlib.h> |
|
7 #include <string.h> |
|
8 |
|
9 #include "map_tests.h" |
5 #include "map_tests.h" |
10 |
6 |
11 int map_tests() { |
7 UCX_TEST_BEGIN(test_ucx_map_new) { |
12 printf(" Test ucx_map_new\n"); |
|
13 UcxMap *map = ucx_map_new(16); |
8 UcxMap *map = ucx_map_new(16); |
14 if(map == NULL) { |
9 |
15 return -1; |
10 UCX_TEST_ASSERT(map->size == 16, "wrong size") |
16 } |
11 UCX_TEST_ASSERT(map->map != NULL, "failed") |
|
12 |
|
13 ucx_map_free(map); |
|
14 |
|
15 UCX_TEST_END |
|
16 } |
17 |
17 |
18 printf(" Test ucx_map_put\n"); |
18 UCX_TEST_BEGIN(test_ucx_key) { |
19 char *txt = "text/plain"; |
19 |
20 char *xml = "text/xml"; |
20 UcxKey key = ucx_key("This is a text.", 15); |
21 ucx_map_cstr_put(map, "txt", txt); |
21 UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed") |
22 ucx_map_cstr_put(map, "xml", xml); |
22 UCX_TEST_ASSERT(key.len == 15, "failed") |
|
23 UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed") |
|
24 |
|
25 UCX_TEST_END |
|
26 } |
23 |
27 |
24 printf(" Test ucx_map_get\n"); |
28 UCX_TEST_BEGIN(test_ucx_map_put) { |
25 if(ucx_map_cstr_get(map, "txt") != txt) { |
29 |
26 fprintf(stderr, "ucx_map_get failed\n"); |
30 UcxMap *map = ucx_map_new(4); |
27 return -1; |
31 |
28 } |
32 int td[5]; |
29 char xmlkey[4]; |
33 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; |
30 xmlkey[0] = 'x'; |
|
31 xmlkey[1] = 'm'; |
|
32 xmlkey[2] = 'l'; |
|
33 xmlkey[3] = 0; |
|
34 if(ucx_map_cstr_get(map, xmlkey) != xml) { |
|
35 fprintf(stderr, "ucx_map_get failed\n"); |
|
36 return -1; |
|
37 } |
|
38 if(ucx_map_cstr_get(map, "nokey") != NULL) { |
|
39 fprintf(stderr, "ucx_map_get failed\n"); |
|
40 return -1; |
|
41 } |
|
42 |
34 |
43 return 0; |
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 */ |
|
40 |
|
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") |
|
49 |
|
50 UCX_TEST_ASSERT(map->map[1] == NULL, "slot 1 not terminated") |
|
51 |
|
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") |
|
54 |
|
55 UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1") |
|
56 |
|
57 ucx_map_cstr_put(map, "Key0", &td[3]); /* 0 */ |
|
58 |
|
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") |
|
65 |
|
66 ucx_map_free(map); |
|
67 |
|
68 UCX_TEST_END |
44 } |
69 } |
|
70 |
|
71 UCX_TEST_BEGIN(test_ucx_map_get) { |
|
72 UCX_TEST_END |
|
73 } |