Added dlist tests

Sat, 31 Dec 2011 18:57:30 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 31 Dec 2011 18:57:30 +0100
changeset 9
013c5c4b7e44
parent 8
9cd2b2460db0
child 10
df8140ba781c

Added dlist tests

test/Makefile file | annotate | diff | comparison | revisions
test/list_tests.c file | annotate | diff | comparison | revisions
test/list_tests.h file | annotate | diff | comparison | revisions
test/main.c file | annotate | diff | comparison | revisions
ucx/list.h file | annotate | diff | comparison | revisions
--- a/test/Makefile	Sat Dec 31 18:46:48 2011 +0100
+++ b/test/Makefile	Sat Dec 31 18:57:30 2011 +0100
@@ -28,7 +28,7 @@
 
 include ../$(CONF).mk
 
-SRC = main.c
+SRC = main.c list_tests.c
 
 OBJ = $(SRC:%.c=../build/%.$(OBJ_EXT))
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/list_tests.c	Sat Dec 31 18:57:30 2011 +0100
@@ -0,0 +1,65 @@
+/*
+ * tests of list implementation
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "ucx/list.h"
+#include "ucx/dlist.h"
+
+struct test1_data {
+    int values[3];
+    int i;
+};
+
+int list_tests_foreach1(void *v, void *custom) {
+    UcxDlist *dl = (UcxDlist*)v;
+    struct test1_data *tdata = (struct test1_data*)custom;
+
+    tdata->values[tdata->i] = *(int*)dl->data;
+    tdata->i++;
+}
+
+int list_tests() {
+    int r = 0;
+    int v[8];
+    UcxDlist *dl = NULL;
+    // build list 0,1,2,3,4,5,6,7
+    for(int i=0;i<8;i++) {
+        v[i] = i;
+        dl = ucx_dlist_append(dl, &v[i]);
+    }
+
+    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");
+        }
+        if(elm->data == NULL) {
+            fprintf(stderr, "ucx_dlist_get failed: data is NULL\n");
+        }
+        int *data = (int*)elm->data;
+        if(*data != i) {
+            fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
+        }
+    }
+
+    //TODO: ucx_dlist_free test
+    
+    dl = NULL;
+    // build list 4,0,4
+    dl = ucx_dlist_prepend(dl, &v[0]);
+    dl = ucx_dlist_prepend(dl, &v[5]);
+    dl = ucx_dlist_append(dl, &v[5]);
+
+    struct test1_data tdata;
+    tdata.i = 0;
+    ucx_dlist_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");
+    }
+
+    return r;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/list_tests.h	Sat Dec 31 18:57:30 2011 +0100
@@ -0,0 +1,23 @@
+/* 
+ * File:   list_tests.h
+ * Author: olaf
+ *
+ * Created on 31. Dezember 2011, 18:07
+ */
+
+#ifndef LIST_TESTS_H
+#define	LIST_TESTS_H
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+int list_tests();
+
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* LIST_TESTS_H */
+
--- a/test/main.c	Sat Dec 31 18:46:48 2011 +0100
+++ b/test/main.c	Sat Dec 31 18:57:30 2011 +0100
@@ -29,8 +29,14 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "list_tests.h"
+
 int main(int argc, char **argv) {
-    printf("Hello World!\n");
+    printf("UCX Tests\n---------\n\n");
+
+    if(list_tests()) {
+        fprintf(stderr, "list_tests failed\n");
+    }
     
     return EXIT_SUCCESS; 
 }
--- a/ucx/list.h	Sat Dec 31 18:46:48 2011 +0100
+++ b/ucx/list.h	Sat Dec 31 18:57:30 2011 +0100
@@ -20,7 +20,7 @@
 
 UcxList *ucx_list_append(UcxList *l, void *data);
 UcxList *ucx_list_prepend(UcxList *l, void *data);
-UcxList *ucx_list_concat(UcxList *l1, UcxDlist *l2);
+UcxList *ucx_list_concat(UcxList *l1, UcxList *l2);
 UcxList *ucx_list_last(UcxList *l);
 UcxList *ucx_list_get(UcxList *l, int index);
 size_t ucx_list_size(UcxList *l);

mercurial