test/list_tests.c

changeset 24
e04822101291
parent 22
76cdd8209f1f
child 27
22644e2572bc
--- a/test/list_tests.c	Sun Jan 15 14:20:25 2012 +0100
+++ b/test/list_tests.c	Wed Feb 08 23:43:02 2012 +0100
@@ -8,14 +8,22 @@
 #include "ucx/list.h"
 #include "ucx/dlist.h"
 
-struct test1_data {
+struct foreach_testdata {
     int values[3];
     int i;
 };
 
+void* int_cpy(void* source, void* data) {
+    void *dest = malloc(sizeof(int));
+    if (dest != NULL) {
+        *((int*)dest) = *((int*)source);
+    }
+    return dest;
+}
+
 int int_cmp(void* e1, void *e2, void *data) {
     if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1;
-    
+
     int *i1 = (int*)e1, *i2 = (int*)e2;
     int r = (*i1) - (*i2);
     return (r < 0) ? -1 : (r == 0 ? 0 : 1);
@@ -23,11 +31,17 @@
 
 int dlist_tests_foreach(void *v, void *custom) {
     UcxDlist *dl = (UcxDlist*)v;
-    struct test1_data *tdata = (struct test1_data*)custom;
+    struct foreach_testdata *tdata = (struct foreach_testdata*)custom;
 
     tdata->values[tdata->i] = *(int*)dl->data;
     tdata->i++;
-    
+
+    return 0;
+}
+
+int dlist_free_content(void *v, void *custom) {
+    UcxDlist *dl = (UcxDlist*)v;
+    free(dl->data);
     return 0;
 }
 
@@ -50,15 +64,15 @@
         if(elm == NULL) {
             fprintf(stderr, "ucx_dlist_get failed: element is NULL\n");
             r--;
-        }
-        if(elm->data == NULL) {
+        } else if(elm->data == NULL) {
             fprintf(stderr, "ucx_dlist_get failed: data is NULL\n");
             r--;
-        }
-        int *data = (int*)elm->data;
-        if(*data != i) {
-            fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
-            r--;
+        } else {
+            int *data = (int*)elm->data;
+            if(*data != i) {
+                fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
+                r--;
+            }
         }
     }
 
@@ -73,7 +87,7 @@
     dl = ucx_dlist_prepend(dl, &v[4]);
     dl = ucx_dlist_append(dl, &v[4]);
 
-    struct test1_data tdata;
+    struct foreach_testdata tdata;
     tdata.i = 0;
     ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata);
 
@@ -108,7 +122,7 @@
         r--;
     }
     ucx_dlist_free(dl2);
-    
+
     printf("   Test ucx_dlist_clone\n");
     dl2 = ucx_dlist_clone(dl, NULL, NULL);
     if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
@@ -116,8 +130,20 @@
         r--;
     }
     ucx_dlist_free(dl2);
-    
-    printf("   TODO: test clone with copy\n");
+
+    printf("   Test ucx_dlist_clone with copy\n");
+    dl2 = ucx_dlist_clone(dl, int_cpy, NULL);
+    if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
+        if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) {
+            fprintf(stderr, "ucx_dlist_clone (with copy) failed (compare)\n");
+            r--;
+        }
+    } else {
+        fprintf(stderr, "ucx_dlist_clone (with copy) failed (identity)\n");
+        r--;
+    }
+    ucx_dlist_foreach(dl, dlist_free_content, NULL);
+    ucx_dlist_free(dl2);
 
     ucx_dlist_free(dl);
 

mercurial