test/list_tests.c

Thu, 05 Jan 2012 14:53:54 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 05 Jan 2012 14:53:54 +0100
changeset 20
db7d9860dbbd
parent 11
4f6082f99bd7
child 18
69636f81db31
permissions
-rw-r--r--

added some map functions

9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * tests of list implementation
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 */
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 #include <stdio.h>
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 #include <stdlib.h>
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 #include "ucx/list.h"
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 #include "ucx/dlist.h"
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 struct test1_data {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 int values[3];
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 int i;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 };
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 int list_tests_foreach1(void *v, void *custom) {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 UcxDlist *dl = (UcxDlist*)v;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 struct test1_data *tdata = (struct test1_data*)custom;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 tdata->values[tdata->i] = *(int*)dl->data;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 tdata->i++;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 }
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
24 int list_tests_foreach2(void *v, void *custom) {
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
25 UcxList *dl = (UcxList*)v;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
26 struct test1_data *tdata = (struct test1_data*)custom;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
27
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
28 tdata->values[tdata->i] = *(int*)dl->data;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
29 tdata->i++;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
30 }
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
31
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
32 int dlist_tests() {
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 int r = 0;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 int v[8];
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 UcxDlist *dl = NULL;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 // build list 0,1,2,3,4,5,6,7
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
37 printf(" Test ucx_dlist_append\n");
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
38 fflush(stdout);
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 for(int i=0;i<8;i++) {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 v[i] = i;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 dl = ucx_dlist_append(dl, &v[i]);
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 }
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
44 printf(" Test ucx_dlist_get\n");
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
45 fflush(stdout);
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 for(int i=0;i<8;i++) {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 UcxDlist *elm = ucx_dlist_get(dl, i);
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 if(elm == NULL) {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 fprintf(stderr, "ucx_dlist_get failed: element is NULL\n");
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
50 r--;
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 }
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 if(elm->data == NULL) {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 fprintf(stderr, "ucx_dlist_get failed: data is NULL\n");
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
54 r--;
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 }
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 int *data = (int*)elm->data;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 if(*data != i) {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
59 r--;
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 }
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 }
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
63 printf(" Test ucx_dlist_free\n");
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
64 fflush(stdout);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
65 ucx_dlist_free(dl);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
66
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 dl = NULL;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 // build list 4,0,4
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
69 printf(" Test ucx_dlist_prepend\n");
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 dl = ucx_dlist_prepend(dl, &v[0]);
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
71 dl = ucx_dlist_prepend(dl, &v[4]);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
72 dl = ucx_dlist_append(dl, &v[4]);
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 struct test1_data tdata;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 tdata.i = 0;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 ucx_dlist_foreach(dl, list_tests_foreach1, &tdata);
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 fprintf(stderr, "prepend/append test failed\n");
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
80 fprintf(stderr, "content: [%d, %d, %d]\n", tdata.values[0], tdata.values[1], tdata.values[2]);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
81 r--;
9
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 }
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 return r;
013c5c4b7e44 Added dlist tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 }
11
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
86
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
87 int list_tests() {
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
88 int r = 0;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
89 int v[8];
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
90 UcxList *dl = NULL;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
91 // build list 0,1,2,3,4,5,6,7
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
92 printf(" Test ucx_list_append\n");
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
93 fflush(stdout);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
94 for(int i=0;i<8;i++) {
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
95 v[i] = i;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
96 dl = ucx_list_append(dl, &v[i]);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
97 }
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
98
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
99 printf(" Test ucx_list_get\n");
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
100 fflush(stdout);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
101 for(int i=0;i<8;i++) {
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
102 UcxList *elm = ucx_list_get(dl, i);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
103 if(elm == NULL) {
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
104 fprintf(stderr, "ucx_list_get failed: element is NULL\n");
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
105 r--;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
106 }
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
107 if(elm->data == NULL) {
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
108 fprintf(stderr, "ucx_list_get failed: data is NULL\n");
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
109 r--;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
110 }
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
111 int *data = (int*)elm->data;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
112 if(*data != i) {
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
113 fprintf(stderr, "ucx_list_get failed with index %d\n", i);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
114 r--;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
115 }
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
116 }
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
117
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
118 printf(" Test ucx_list_free\n");
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
119 fflush(stdout);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
120 ucx_list_free(dl);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
121
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
122 dl = NULL;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
123 // build list 4,0,4
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
124 printf(" Test ucx_list_prepend\n");
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
125 dl = ucx_list_prepend(dl, &v[0]);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
126 dl = ucx_list_prepend(dl, &v[4]);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
127 dl = ucx_list_append(dl, &v[4]);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
128
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
129 struct test1_data tdata;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
130 tdata.i = 0;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
131 ucx_list_foreach(dl, list_tests_foreach1, &tdata);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
132
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
133 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
134 fprintf(stderr, "prepend/append test failed\n");
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
135 fprintf(stderr, "content: [%d, %d, %d]\n", tdata.values[0], tdata.values[1], tdata.values[2]);
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
136 r--;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
137 }
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
138
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
139 return r;
4f6082f99bd7 Added list tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
140 }

mercurial