test/map_tests.c

changeset 374
be77fb2da242
parent 327
fbc33813265b
--- a/test/map_tests.c	Thu Dec 19 18:47:23 2019 +0100
+++ b/test/map_tests.c	Thu Dec 19 19:58:41 2019 +0100
@@ -27,6 +27,7 @@
  */
 
 #include "map_tests.h"
+#include <ucx/utils.h>
 
 UCX_TEST(test_ucx_map_new) {
     UcxMap *map = ucx_map_new(16);
@@ -304,3 +305,127 @@
 
     ucx_map_free(map);
 }
+
+UCX_TEST(test_ucx_map_union) {
+    int td[5];
+    size_t intlen = sizeof(int);
+    td[0] = 10; td[1] = 42; td[2] = 47; td[3] = 1337; td[4] = 9000;
+
+    UcxMap *first = ucx_map_new(4);
+    UcxMap *second = ucx_map_new(4);
+
+    ucx_map_cstr_put(first, "key0", &td[0]);
+    ucx_map_cstr_put(first, "key1", &td[1]);
+    ucx_map_cstr_put(second, "key2", &td[2]);
+    ucx_map_cstr_put(second, "key0", &td[3]);
+    ucx_map_cstr_put(second, "key3", &td[4]);
+
+    UcxMap *result = ucx_map_union(first, second, ucx_memcpy, &intlen);
+
+    UCX_TEST_BEGIN
+
+    int* r;
+    UCX_TEST_ASSERT(result->count == 4,
+            "result has incorrect number of elements");
+
+    r = (int*)ucx_map_cstr_get(result, "key0");
+    UCX_TEST_ASSERT(!!r, "key0 is not present");
+    UCX_TEST_ASSERT(*r == td[3], "key0 has not been overwritten");
+    r = (int*)ucx_map_cstr_get(result, "key1");
+    UCX_TEST_ASSERT(!!r, "key1 is not present");
+    UCX_TEST_ASSERT(*r == td[1], "key1 contains wrong data");
+    r = (int*)ucx_map_cstr_get(result, "key2");
+    UCX_TEST_ASSERT(!!r, "key2 is not present");
+    UCX_TEST_ASSERT(*r == td[2], "key2 contains wrong data");
+    r = (int*)ucx_map_cstr_get(result, "key3");
+    UCX_TEST_ASSERT(!!r, "key3 is not present");
+    UCX_TEST_ASSERT(*r == td[4], "key3 contains wrong data");
+
+    UCX_TEST_END
+
+    ucx_map_free_content(result, NULL);
+    ucx_map_free(result);
+    ucx_map_free(second);
+    ucx_map_free(first);
+}
+
+UCX_TEST(test_ucx_map_intersection) {
+        int td[5];
+        size_t intlen = sizeof(int);
+        td[0] = 10; td[1] = 42; td[2] = 47; td[3] = 1337; td[4] = 9000;
+
+        UcxMap *first = ucx_map_new(4);
+        UcxMap *second = ucx_map_new(4);
+
+        ucx_map_cstr_put(first, "key0", &td[0]);
+        ucx_map_cstr_put(first, "key1", &td[1]);
+        ucx_map_cstr_put(first, "key4", &td[3]);
+        ucx_map_cstr_put(second, "key2", &td[2]);
+        ucx_map_cstr_put(second, "key0", &td[3]);
+        ucx_map_cstr_put(second, "key3", &td[4]);
+        ucx_map_cstr_put(second, "key4", &td[4]);
+
+        UcxMap *result = ucx_map_intersection(first, second,
+                ucx_memcpy, &intlen);
+
+        UCX_TEST_BEGIN
+
+        int* r;
+        UCX_TEST_ASSERT(result->count == 2,
+                "result has incorrect number of elements");
+
+        r = (int*)ucx_map_cstr_get(result, "key0");
+        UCX_TEST_ASSERT(!!r, "key0 is not present");
+        UCX_TEST_ASSERT(*r == td[0], "key0 has not original data");
+        r = (int*)ucx_map_cstr_get(result, "key4");
+        UCX_TEST_ASSERT(!!r, "key4 is not present");
+        UCX_TEST_ASSERT(*r == td[3], "key4 has not original data");
+
+        UCX_TEST_END
+
+        ucx_map_free_content(result, NULL);
+        ucx_map_free(result);
+        ucx_map_free(second);
+        ucx_map_free(first);
+}
+
+
+UCX_TEST(test_ucx_map_difference) {
+        int td[5];
+        size_t intlen = sizeof(int);
+        td[0] = 10; td[1] = 42; td[2] = 47; td[3] = 1337; td[4] = 9000;
+
+        UcxMap *first = ucx_map_new(4);
+        UcxMap *second = ucx_map_new(4);
+
+        ucx_map_cstr_put(first, "key0", &td[0]);
+        ucx_map_cstr_put(first, "key1", &td[1]);
+        ucx_map_cstr_put(first, "key2", &td[2]);
+        ucx_map_cstr_put(first, "key4", &td[3]);
+        ucx_map_cstr_put(second, "key0", &td[3]);
+        ucx_map_cstr_put(second, "key3", &td[4]);
+        ucx_map_cstr_put(second, "key4", &td[4]);
+
+        UcxMap *result = ucx_map_difference(first, second, ucx_memcpy, &intlen);
+
+        UCX_TEST_BEGIN
+
+        int* r;
+        UCX_TEST_ASSERT(result->count == 2,
+                "result has incorrect number of elements");
+
+        r = (int*)ucx_map_cstr_get(result, "key1");
+        UCX_TEST_ASSERT(!!r, "key1 is not present");
+        UCX_TEST_ASSERT(*r == td[1], "key1 has incorrect data");
+        r = (int*)ucx_map_cstr_get(result, "key2");
+        UCX_TEST_ASSERT(!!r, "key2 is not present");
+        UCX_TEST_ASSERT(*r == td[2], "key2 has incorrect data");
+
+        UCX_TEST_END
+
+        ucx_map_free_content(result, NULL);
+        ucx_map_free(result);
+        ucx_map_free(second);
+        ucx_map_free(first);
+}
+

mercurial