test/map_tests.c

changeset 374
be77fb2da242
parent 327
fbc33813265b
equal deleted inserted replaced
373:6f63f5ed3cab 374:be77fb2da242
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "map_tests.h" 29 #include "map_tests.h"
30 #include <ucx/utils.h>
30 31
31 UCX_TEST(test_ucx_map_new) { 32 UCX_TEST(test_ucx_map_new) {
32 UcxMap *map = ucx_map_new(16); 33 UcxMap *map = ucx_map_new(16);
33 UCX_TEST_BEGIN 34 UCX_TEST_BEGIN
34 UCX_TEST_ASSERT(map->size == 16, "wrong size"); 35 UCX_TEST_ASSERT(map->size == 16, "wrong size");
302 "subsequent rehashing call shall not change size"); 303 "subsequent rehashing call shall not change size");
303 UCX_TEST_END 304 UCX_TEST_END
304 305
305 ucx_map_free(map); 306 ucx_map_free(map);
306 } 307 }
308
309 UCX_TEST(test_ucx_map_union) {
310 int td[5];
311 size_t intlen = sizeof(int);
312 td[0] = 10; td[1] = 42; td[2] = 47; td[3] = 1337; td[4] = 9000;
313
314 UcxMap *first = ucx_map_new(4);
315 UcxMap *second = ucx_map_new(4);
316
317 ucx_map_cstr_put(first, "key0", &td[0]);
318 ucx_map_cstr_put(first, "key1", &td[1]);
319 ucx_map_cstr_put(second, "key2", &td[2]);
320 ucx_map_cstr_put(second, "key0", &td[3]);
321 ucx_map_cstr_put(second, "key3", &td[4]);
322
323 UcxMap *result = ucx_map_union(first, second, ucx_memcpy, &intlen);
324
325 UCX_TEST_BEGIN
326
327 int* r;
328 UCX_TEST_ASSERT(result->count == 4,
329 "result has incorrect number of elements");
330
331 r = (int*)ucx_map_cstr_get(result, "key0");
332 UCX_TEST_ASSERT(!!r, "key0 is not present");
333 UCX_TEST_ASSERT(*r == td[3], "key0 has not been overwritten");
334 r = (int*)ucx_map_cstr_get(result, "key1");
335 UCX_TEST_ASSERT(!!r, "key1 is not present");
336 UCX_TEST_ASSERT(*r == td[1], "key1 contains wrong data");
337 r = (int*)ucx_map_cstr_get(result, "key2");
338 UCX_TEST_ASSERT(!!r, "key2 is not present");
339 UCX_TEST_ASSERT(*r == td[2], "key2 contains wrong data");
340 r = (int*)ucx_map_cstr_get(result, "key3");
341 UCX_TEST_ASSERT(!!r, "key3 is not present");
342 UCX_TEST_ASSERT(*r == td[4], "key3 contains wrong data");
343
344 UCX_TEST_END
345
346 ucx_map_free_content(result, NULL);
347 ucx_map_free(result);
348 ucx_map_free(second);
349 ucx_map_free(first);
350 }
351
352 UCX_TEST(test_ucx_map_intersection) {
353 int td[5];
354 size_t intlen = sizeof(int);
355 td[0] = 10; td[1] = 42; td[2] = 47; td[3] = 1337; td[4] = 9000;
356
357 UcxMap *first = ucx_map_new(4);
358 UcxMap *second = ucx_map_new(4);
359
360 ucx_map_cstr_put(first, "key0", &td[0]);
361 ucx_map_cstr_put(first, "key1", &td[1]);
362 ucx_map_cstr_put(first, "key4", &td[3]);
363 ucx_map_cstr_put(second, "key2", &td[2]);
364 ucx_map_cstr_put(second, "key0", &td[3]);
365 ucx_map_cstr_put(second, "key3", &td[4]);
366 ucx_map_cstr_put(second, "key4", &td[4]);
367
368 UcxMap *result = ucx_map_intersection(first, second,
369 ucx_memcpy, &intlen);
370
371 UCX_TEST_BEGIN
372
373 int* r;
374 UCX_TEST_ASSERT(result->count == 2,
375 "result has incorrect number of elements");
376
377 r = (int*)ucx_map_cstr_get(result, "key0");
378 UCX_TEST_ASSERT(!!r, "key0 is not present");
379 UCX_TEST_ASSERT(*r == td[0], "key0 has not original data");
380 r = (int*)ucx_map_cstr_get(result, "key4");
381 UCX_TEST_ASSERT(!!r, "key4 is not present");
382 UCX_TEST_ASSERT(*r == td[3], "key4 has not original data");
383
384 UCX_TEST_END
385
386 ucx_map_free_content(result, NULL);
387 ucx_map_free(result);
388 ucx_map_free(second);
389 ucx_map_free(first);
390 }
391
392
393 UCX_TEST(test_ucx_map_difference) {
394 int td[5];
395 size_t intlen = sizeof(int);
396 td[0] = 10; td[1] = 42; td[2] = 47; td[3] = 1337; td[4] = 9000;
397
398 UcxMap *first = ucx_map_new(4);
399 UcxMap *second = ucx_map_new(4);
400
401 ucx_map_cstr_put(first, "key0", &td[0]);
402 ucx_map_cstr_put(first, "key1", &td[1]);
403 ucx_map_cstr_put(first, "key2", &td[2]);
404 ucx_map_cstr_put(first, "key4", &td[3]);
405 ucx_map_cstr_put(second, "key0", &td[3]);
406 ucx_map_cstr_put(second, "key3", &td[4]);
407 ucx_map_cstr_put(second, "key4", &td[4]);
408
409 UcxMap *result = ucx_map_difference(first, second, ucx_memcpy, &intlen);
410
411 UCX_TEST_BEGIN
412
413 int* r;
414 UCX_TEST_ASSERT(result->count == 2,
415 "result has incorrect number of elements");
416
417 r = (int*)ucx_map_cstr_get(result, "key1");
418 UCX_TEST_ASSERT(!!r, "key1 is not present");
419 UCX_TEST_ASSERT(*r == td[1], "key1 has incorrect data");
420 r = (int*)ucx_map_cstr_get(result, "key2");
421 UCX_TEST_ASSERT(!!r, "key2 is not present");
422 UCX_TEST_ASSERT(*r == td[2], "key2 has incorrect data");
423
424 UCX_TEST_END
425
426 ucx_map_free_content(result, NULL);
427 ucx_map_free(result);
428 ucx_map_free(second);
429 ucx_map_free(first);
430 }
431

mercurial