changed make clean + added dlist_clone with copy test + added va_end statements to string.c

Wed, 08 Feb 2012 23:43:02 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 08 Feb 2012 23:43:02 +0100
changeset 24
e04822101291
parent 23
ccc294fafb9b
child 25
3192553c0df1

changed make clean + added dlist_clone with copy test + added va_end statements to string.c

Makefile file | annotate | diff | comparison | revisions
test/list_tests.c file | annotate | diff | comparison | revisions
ucx/list.c file | annotate | diff | comparison | revisions
ucx/string.c file | annotate | diff | comparison | revisions
--- a/Makefile	Sun Jan 15 14:20:25 2012 +0100
+++ b/Makefile	Wed Feb 08 23:43:02 2012 +0100
@@ -53,7 +53,7 @@
 	./build/test$(APP_EXT)
 
 clean: FORCE
-	$(RM) $(RMFLAGS) build/*
+	$(RM) $(RMFLAGS) build/*.${OBJ_EXT}
 
 FORCE:
 
--- 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);
 
--- a/ucx/list.c	Sun Jan 15 14:20:25 2012 +0100
+++ b/ucx/list.c	Wed Feb 08 23:43:02 2012 +0100
@@ -135,4 +135,4 @@
         }
     }
     return l;
-}
\ No newline at end of file
+}
--- a/ucx/string.c	Sun Jan 15 14:20:25 2012 +0100
+++ b/ucx/string.c	Wed Feb 08 23:43:02 2012 +0100
@@ -34,6 +34,7 @@
         sstr_t str = va_arg(ap, sstr_t);
         size += str.length;
     }
+    va_end(ap);
 
     return size;
 }
@@ -48,6 +49,7 @@
         s.ptr = strncat (s.ptr, str.ptr, s.length);
         str = va_arg (ap, sstr_t);
     }
+    va_end(ap);
 
     return s;
 }
@@ -62,6 +64,7 @@
         sstr_t str = va_arg (ap, sstr_t);
         s.ptr = strncat (s.ptr, str.ptr, s.length);
     }
+    va_end(ap);
 
     return s;
 }
@@ -89,11 +92,13 @@
 
 sstr_t sstrdub(sstr_t s) {
     sstr_t newstring;
-    newstring.ptr = malloc(s.length + 1);
-    newstring.length = s.length;
-    newstring.ptr[newstring.length] = 0;
+    newstring.ptr = (char*) malloc(s.length + 1);
+    if (newstring.ptr != NULL) {
+        newstring.length = s.length;
+        newstring.ptr[newstring.length] = 0;
 
-    memcpy(newstring.ptr, s.ptr, s.length);
+        memcpy(newstring.ptr, s.ptr, s.length);
+    }
 
     return newstring;
 }

mercurial