Fri, 05 Oct 2012 10:25:33 +0200
implemented encoder/decoder for map store/load
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 void* test_ucx_map_store_load_encdec(void *value, void *data) {
145 char *string = (char*) value;
146 size_t n = strlen(string);
147 char *encoded = malloc(n+1);
148 for (int i = 0 ; i < n ; i++) {
149 encoded[i] = string[n-1-i];
150 }
151 encoded[n] = 0;
152 return encoded;
153 }
155 UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
156 UcxMap *map = ucx_map_new(4);
158 ucx_map_cstr_put(map, "test", "test");
159 ucx_map_cstr_put(map, "key", "value");
160 ucx_map_cstr_put(map, "other.very.long.key", "value");
161 ucx_map_cstr_put(map, "testkey", "testvalue");
162 ucx_map_cstr_put(map, "simple", "not a key but an extremely long value "
163 "to test if the buffer extension works as designed");
165 FILE *f = fopen("test_ucx_map_store", "w");
166 int r;
168 fwrite(" # comment test\n", 1, 16, f);
169 r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL);
170 fwrite("!discard this", 1, 13, f);
172 fclose(f);
173 ucx_map_free(map);
174 map = ucx_map_new(1);
175 f = fopen("test_ucx_map_store", "r");
176 r += ucx_map_load_enc(map, f, test_ucx_map_store_load_encdec, NULL);
178 UCX_TEST_BEGIN
179 char *value;
180 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
182 value = ucx_map_cstr_get(map, "test");
183 UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
184 UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");
186 value = ucx_map_cstr_get(map, "key");
187 UCX_TEST_ASSERT(value != NULL, "value not found for key: key");
188 UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");
190 value = ucx_map_cstr_get(map, "other.very.long.key");
191 UCX_TEST_ASSERT(value != NULL,
192 "value not found for key: other.very.long.key");
193 UCX_TEST_ASSERT(strcmp(value, "value") == 0,
194 "value error for key: other.very.long.key");
196 value = ucx_map_cstr_get(map, "testkey");
197 UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey");
198 UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
199 "value error for key: testkey");
201 value = ucx_map_cstr_get(map, "simple");
202 UCX_TEST_ASSERT(value != NULL, "value not found for key: simple");
203 UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
204 "to test if the buffer extension works as designed") == 0,
205 "value error for key: simple");
207 UCX_TEST_END
208 fclose(f);
210 unlink("test_ucx_map_store");
211 }
213 UCX_TEST_IMPLEMENT(test_ucx_map_clone) {
214 UcxMap *map = ucx_map_new(4);
216 ucx_map_cstr_put(map, "key1", "value1");
217 ucx_map_cstr_put(map, "key2", "value2");
218 ucx_map_cstr_put(map, "key3", "value3");
220 UcxMap *clone = ucx_map_clone(map, NULL, NULL);
222 char *v1 = ucx_map_cstr_get(map, "key1");
223 char *v2 = ucx_map_cstr_get(map, "key2");
224 char *v3 = ucx_map_cstr_get(map, "key3");
226 UCX_TEST_BEGIN
228 UCX_TEST_ASSERT(v1 != NULL, "failed key 1");
229 UCX_TEST_ASSERT(v2 != NULL, "failed key 2");
230 UCX_TEST_ASSERT(v3 != NULL, "failed key 3");
232 char *c1 = ucx_map_cstr_get(clone, "key1");
233 char *c2 = ucx_map_cstr_get(clone, "key2");
234 char *c3 = ucx_map_cstr_get(clone, "key3");
236 UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)");
237 UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)");
238 UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)");
240 UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1");
241 UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2");
242 UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3");
244 UCX_TEST_END
246 ucx_map_free(map);
247 ucx_map_free(clone);
248 }