Added list tests

Sat, 31 Dec 2011 19:08:27 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 31 Dec 2011 19:08:27 +0100
changeset 11
4f6082f99bd7
parent 10
df8140ba781c
child 12
fe50a85e69e7

Added list tests

test/list_tests.c file | annotate | diff | comparison | revisions
--- a/test/list_tests.c	Sat Dec 31 19:05:26 2011 +0100
+++ b/test/list_tests.c	Sat Dec 31 19:08:27 2011 +0100
@@ -21,37 +21,55 @@
     tdata->i++;
 }
 
-int list_tests() {
+int list_tests_foreach2(void *v, void *custom) {
+    UcxList *dl = (UcxList*)v;
+    struct test1_data *tdata = (struct test1_data*)custom;
+
+    tdata->values[tdata->i] = *(int*)dl->data;
+    tdata->i++;
+}
+
+int dlist_tests() {
     int r = 0;
     int v[8];
     UcxDlist *dl = NULL;
     // build list 0,1,2,3,4,5,6,7
+    printf("   Test ucx_dlist_append\n");
+    fflush(stdout);
     for(int i=0;i<8;i++) {
         v[i] = i;
         dl = ucx_dlist_append(dl, &v[i]);
     }
 
+    printf("   Test ucx_dlist_get\n");
+    fflush(stdout);
     for(int i=0;i<8;i++) {
         UcxDlist *elm = ucx_dlist_get(dl, i);
         if(elm == NULL) {
             fprintf(stderr, "ucx_dlist_get failed: element is NULL\n");
+            r--;
         }
         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--;
         }
     }
 
-    //TODO: ucx_dlist_free test
-    
+    printf("   Test ucx_dlist_free\n");
+    fflush(stdout);
+    ucx_dlist_free(dl);
+
     dl = NULL;
     // build list 4,0,4
+    printf("   Test ucx_dlist_prepend\n");
     dl = ucx_dlist_prepend(dl, &v[0]);
-    dl = ucx_dlist_prepend(dl, &v[5]);
-    dl = ucx_dlist_append(dl, &v[5]);
+    dl = ucx_dlist_prepend(dl, &v[4]);
+    dl = ucx_dlist_append(dl, &v[4]);
 
     struct test1_data tdata;
     tdata.i = 0;
@@ -59,7 +77,64 @@
 
     if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
         fprintf(stderr, "prepend/append test failed\n");
+        fprintf(stderr, "content: [%d, %d, %d]\n", tdata.values[0], tdata.values[1], tdata.values[2]);
+        r--;
     }
 
     return r;
 }
+
+int list_tests() {
+    int r = 0;
+    int v[8];
+    UcxList *dl = NULL;
+    // build list 0,1,2,3,4,5,6,7
+    printf("   Test ucx_list_append\n");
+    fflush(stdout);
+    for(int i=0;i<8;i++) {
+        v[i] = i;
+        dl = ucx_list_append(dl, &v[i]);
+    }
+
+    printf("   Test ucx_list_get\n");
+    fflush(stdout);
+    for(int i=0;i<8;i++) {
+        UcxList *elm = ucx_list_get(dl, i);
+        if(elm == NULL) {
+            fprintf(stderr, "ucx_list_get failed: element is NULL\n");
+            r--;
+        }
+        if(elm->data == NULL) {
+            fprintf(stderr, "ucx_list_get failed: data is NULL\n");
+            r--;
+        }
+        int *data = (int*)elm->data;
+        if(*data != i) {
+            fprintf(stderr, "ucx_list_get failed with index %d\n", i);
+            r--;
+        }
+    }
+
+    printf("   Test ucx_list_free\n");
+    fflush(stdout);
+    ucx_list_free(dl);
+
+    dl = NULL;
+    // build list 4,0,4
+    printf("   Test ucx_list_prepend\n");
+    dl = ucx_list_prepend(dl, &v[0]);
+    dl = ucx_list_prepend(dl, &v[4]);
+    dl = ucx_list_append(dl, &v[4]);
+
+    struct test1_data tdata;
+    tdata.i = 0;
+    ucx_list_foreach(dl, list_tests_foreach1, &tdata);
+
+    if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
+        fprintf(stderr, "prepend/append test failed\n");
+        fprintf(stderr, "content: [%d, %d, %d]\n", tdata.values[0], tdata.values[1], tdata.values[2]);
+        r--;
+    }
+
+    return r;
+}

mercurial