test/map_tests.c

changeset 33
9c219a62070d
parent 32
c7af4ec56e19
child 34
0dcd2ca2a223
equal deleted inserted replaced
32:c7af4ec56e19 33:9c219a62070d
2 * 2 *
3 */ 3 */
4 4
5 #include "map_tests.h" 5 #include "map_tests.h"
6 6
7 UCX_TEST_BEGIN(test_ucx_map_new) { 7 UCX_TEST_IMPLEMENT(test_ucx_map_new) {
8 UcxMap *map = ucx_map_new(16); 8 UcxMap *map = ucx_map_new(16);
9 9 UCX_TEST_BEGIN
10 UCX_TEST_ASSERT(map->size == 16, "wrong size") 10 UCX_TEST_ASSERT(map->size == 16, "wrong size")
11 UCX_TEST_ASSERT(map->map != NULL, "failed") 11 UCX_TEST_ASSERT(map->map != NULL, "failed")
12 12
13 UCX_TEST_END
13 ucx_map_free(map); 14 ucx_map_free(map);
14
15 UCX_TEST_END
16 } 15 }
17 16
18 UCX_TEST_BEGIN(test_ucx_key) { 17 UCX_TEST_IMPLEMENT(test_ucx_key) {
19
20 UcxKey key = ucx_key("This is a text.", 15); 18 UcxKey key = ucx_key("This is a text.", 15);
19 UCX_TEST_BEGIN
21 UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed") 20 UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed")
22 UCX_TEST_ASSERT(key.len == 15, "failed") 21 UCX_TEST_ASSERT(key.len == 15, "failed")
23 UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed") 22 UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed")
24 23
25 UCX_TEST_END 24 UCX_TEST_END
26 } 25 }
27 26
28 UCX_TEST_BEGIN(test_ucx_map_put) { 27 UCX_TEST_IMPLEMENT(test_ucx_map_put) {
29 28
30 UcxMap *map = ucx_map_new(4); 29 UcxMap *map = ucx_map_new(4);
31 30
32 int td[5]; 31 int td[5];
33 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; 32 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
34 33
34 UCX_TEST_BEGIN
35 ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */ 35 ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
36 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */ 36 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
37 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */ 37 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
38 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */ 38 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
39 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */ 39 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
61 "overwrite failed") 61 "overwrite failed")
62 UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4], 62 UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4],
63 "overwrite failed") 63 "overwrite failed")
64 UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed") 64 UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed")
65 65
66 UCX_TEST_END
66 ucx_map_free(map); 67 ucx_map_free(map);
67 68 }
69
70 UCX_TEST_IMPLEMENT(test_ucx_map_get) {
71 UCX_TEST_BEGIN
72 UCX_TEST_ASSERT(0, "not implemented");
68 UCX_TEST_END 73 UCX_TEST_END
69 } 74 }
70 75
71 UCX_TEST_BEGIN(test_ucx_map_get) { 76 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) {
72 // TODO: 77 UcxMap *map = (UcxMap*) mapptr;
73 UCX_TEST_END
74 }
75
76 UCX_TEST_BEGIN(test_ucx_map_iterator) {
77 UcxMap *map = ucx_map_new(16);
78
79 int v1 = 10; 78 int v1 = 10;
80 int v2 = 15; 79 int v2 = 15;
81 int v3 = 7; 80 int v3 = 7;
82 int v4 = 9; 81 int v4 = 9;
83 82
95 hit++; 94 hit++;
96 } 95 }
97 96
98 UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits"); 97 UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
99 UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result"); 98 UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
99 }
100 100
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
101 ucx_map_free(map); 106 ucx_map_free(map);
102 107 }
103 map = ucx_map_new(1); 108
104 ucx_map_cstr_put(map, "v1", &v1); 109 UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) {
105 ucx_map_cstr_put(map, "v2", &v2); 110 UcxMap *map = ucx_map_new(1);
106 ucx_map_cstr_put(map, "v3", &v3); 111 UCX_TEST_BEGIN
107 ucx_map_cstr_put(map, "v4", &v4); 112 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
108 113 UCX_TEST_END
109 i = ucx_map_iterator(map);
110 check = 0;
111 hit = 0;
112
113 UCX_MAP_FOREACH(int*, v, map, i) {
114 check += *v;
115 hit++;
116 }
117
118 UCX_TEST_ASSERT(hit == 4, "test2: wrong number of hits");
119 UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test2: wrong result");
120
121
122 ucx_map_free(map); 114 ucx_map_free(map);
123
124 UCX_TEST_END
125 } 115 }

mercurial