test/map_tests.c

changeset 48
621a4430c404
parent 46
48ca036d7d9c
child 51
1c78cd19fb6b
equal deleted inserted replaced
46:48ca036d7d9c 48:621a4430c404
139 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map) 139 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
140 UCX_TEST_END 140 UCX_TEST_END
141 ucx_map_free(map); 141 ucx_map_free(map);
142 } 142 }
143 143
144 void* test_ucx_map_store_load_encdec(void *value, void *data) { 144 void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) {
145 char *string = (char*) value; 145 char *string = (char*) value;
146 size_t n = strlen(string); 146 size_t n = strlen(string);
147 char *encoded = malloc(n+1); 147 char *encoded = malloc(n+1);
148 for (int i = 0 ; i < n ; i++) { 148 for (int i = 0 ; i < n ; i++) {
149 encoded[i] = string[n-1-i]; 149 encoded[i] = string[n-1-i];
150 } 150 }
151 encoded[n] = 0; 151 encoded[n] = 0;
152 *size = n+1;
152 return encoded; 153 return encoded;
153 } 154 }
154 155
155 UCX_TEST_IMPLEMENT(test_ucx_map_store_load) { 156 UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
156 UcxMap *map = ucx_map_new(4); 157 UcxMap *map = ucx_map_new(4);
171 172
172 fclose(f); 173 fclose(f);
173 ucx_map_free(map); 174 ucx_map_free(map);
174 map = ucx_map_new(1); 175 map = ucx_map_new(1);
175 f = fopen("test_ucx_map_store", "r"); 176 f = fopen("test_ucx_map_store", "r");
176 r += ucx_map_load_enc(map, f, test_ucx_map_store_load_encdec, NULL); 177 UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT;
178 r += ucx_map_load_enc(map, f, allocator,
179 test_ucx_map_store_load_encdec, NULL);
180 fclose(f);
181 unlink("test_ucx_map_store");
177 182
178 UCX_TEST_BEGIN 183 UCX_TEST_BEGIN
179 char *value; 184 char *value;
180 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); 185 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
181 186
203 UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value " 208 UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
204 "to test if the buffer extension works as designed") == 0, 209 "to test if the buffer extension works as designed") == 0,
205 "value error for key: simple"); 210 "value error for key: simple");
206 211
207 UCX_TEST_END 212 UCX_TEST_END
208 fclose(f); 213 }
209 214
215 UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) {
216 UcxMap *map = ucx_map_new(4);
217
218 ucx_map_cstr_put(map, "test", "test");
219 ucx_map_cstr_put(map, "key", "value");
220 ucx_map_cstr_put(map, "testkey", "testvalue");
221 ucx_map_cstr_put(map, "simple", "a simple value");
222
223 FILE *f = fopen("test_ucx_map_store", "w");
224 int r;
225 r = ucx_map_store_enc(map, f, NULL, NULL);
226 fclose(f);
227 ucx_map_free(map);
228
229 UcxMempool *pool = ucx_mempool_new(4);
230 map = ucx_map_new(4);
231 f = fopen("test_ucx_map_store", "r");
232 UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool);
233 r += ucx_map_load_enc(map, f, allocator,
234 test_ucx_map_store_load_encdec, NULL);
235 fclose(f);
210 unlink("test_ucx_map_store"); 236 unlink("test_ucx_map_store");
237
238 UCX_TEST_BEGIN
239 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
240 UcxMapIterator iter = ucx_map_iterator(map);
241 char *value; size_t n;
242 UCX_MAP_FOREACH(value, iter) {
243 n = strlen(value);
244 UCX_TEST_ASSERT(strncmp(pool->data[iter.index], value, n),
245 "values of map does not match pooled values");
246 }
247 UCX_TEST_END
248
249 ucx_mempool_free(pool);
211 } 250 }
212 251
213 UCX_TEST_IMPLEMENT(test_ucx_map_clone) { 252 UCX_TEST_IMPLEMENT(test_ucx_map_clone) {
214 UcxMap *map = ucx_map_new(4); 253 UcxMap *map = ucx_map_new(4);
215 254

mercurial