test/list_tests.c

Wed, 04 Jan 2012 14:51:54 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 04 Jan 2012 14:51:54 +0100
changeset 18
69636f81db31
parent 11
4f6082f99bd7
child 19
cdd7a3173249
permissions
-rw-r--r--

added clone and equals to lists

olaf@9 1 /*
olaf@9 2 * tests of list implementation
olaf@9 3 */
olaf@9 4
olaf@9 5 #include <stdio.h>
olaf@9 6 #include <stdlib.h>
olaf@9 7
olaf@9 8 #include "ucx/list.h"
olaf@9 9 #include "ucx/dlist.h"
olaf@9 10
olaf@9 11 struct test1_data {
olaf@9 12 int values[3];
olaf@9 13 int i;
olaf@9 14 };
olaf@9 15
universe@18 16 int int_cmp(void* e1, void *e2, void *data) {
universe@18 17 if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1;
universe@18 18
universe@18 19 int *i1 = (int*)e1, *i2 = (int*)e2;
universe@18 20 int r = (*i1) - (*i2);
universe@18 21 return (r < 0) ? -1 : (r == 0 ? 0 : 1);
universe@18 22 }
universe@18 23
universe@18 24 int dlist_tests_foreach(void *v, void *custom) {
olaf@9 25 UcxDlist *dl = (UcxDlist*)v;
olaf@9 26 struct test1_data *tdata = (struct test1_data*)custom;
olaf@9 27
olaf@9 28 tdata->values[tdata->i] = *(int*)dl->data;
olaf@9 29 tdata->i++;
universe@18 30
universe@18 31 return 0;
olaf@9 32 }
olaf@9 33
universe@18 34 int list_tests_foreach(void *v, void *custom) {
olaf@11 35 UcxList *dl = (UcxList*)v;
olaf@11 36 struct test1_data *tdata = (struct test1_data*)custom;
olaf@11 37
olaf@11 38 tdata->values[tdata->i] = *(int*)dl->data;
olaf@11 39 tdata->i++;
universe@18 40
universe@18 41 return 0;
olaf@11 42 }
olaf@11 43
olaf@11 44 int dlist_tests() {
olaf@9 45 int r = 0;
olaf@9 46 int v[8];
olaf@9 47 UcxDlist *dl = NULL;
olaf@9 48 // build list 0,1,2,3,4,5,6,7
olaf@11 49 printf(" Test ucx_dlist_append\n");
olaf@11 50 fflush(stdout);
olaf@9 51 for(int i=0;i<8;i++) {
olaf@9 52 v[i] = i;
olaf@9 53 dl = ucx_dlist_append(dl, &v[i]);
olaf@9 54 }
olaf@9 55
olaf@11 56 printf(" Test ucx_dlist_get\n");
olaf@11 57 fflush(stdout);
olaf@9 58 for(int i=0;i<8;i++) {
olaf@9 59 UcxDlist *elm = ucx_dlist_get(dl, i);
olaf@9 60 if(elm == NULL) {
olaf@9 61 fprintf(stderr, "ucx_dlist_get failed: element is NULL\n");
olaf@11 62 r--;
olaf@9 63 }
olaf@9 64 if(elm->data == NULL) {
olaf@9 65 fprintf(stderr, "ucx_dlist_get failed: data is NULL\n");
olaf@11 66 r--;
olaf@9 67 }
olaf@9 68 int *data = (int*)elm->data;
olaf@9 69 if(*data != i) {
olaf@9 70 fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
olaf@11 71 r--;
olaf@9 72 }
olaf@9 73 }
olaf@9 74
olaf@11 75 printf(" Test ucx_dlist_free\n");
olaf@11 76 fflush(stdout);
olaf@11 77 ucx_dlist_free(dl);
olaf@11 78
olaf@9 79 dl = NULL;
olaf@9 80 // build list 4,0,4
olaf@11 81 printf(" Test ucx_dlist_prepend\n");
olaf@9 82 dl = ucx_dlist_prepend(dl, &v[0]);
olaf@11 83 dl = ucx_dlist_prepend(dl, &v[4]);
olaf@11 84 dl = ucx_dlist_append(dl, &v[4]);
olaf@9 85
olaf@9 86 struct test1_data tdata;
olaf@9 87 tdata.i = 0;
universe@18 88 ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata);
olaf@9 89
olaf@9 90 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
olaf@9 91 fprintf(stderr, "prepend/append test failed\n");
universe@18 92 fprintf(stderr, "content: [%d, %d, %d]\n",
universe@18 93 tdata.values[0], tdata.values[1], tdata.values[2]);
olaf@11 94 r--;
olaf@9 95 }
olaf@9 96
universe@18 97 printf(" Test ucx_dlist_equals\n");
universe@18 98 UcxDlist *dl2 = NULL;
universe@18 99 dl2 = ucx_dlist_append(dl2, &v[4]);
universe@18 100 dl2 = ucx_dlist_append(dl2, &v[0]);
universe@18 101 dl2 = ucx_dlist_append(dl2, &v[4]);
universe@18 102 if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
universe@18 103 fprintf(stderr, "ucx_dlist_equals failed (false negative)\n");
universe@18 104 }
universe@18 105 dl2->next->data = NULL;
universe@18 106 if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
universe@18 107 fprintf(stderr, "ucx_dlist_equals failed (false positive)\n");
universe@18 108 }
universe@18 109 dl2->next->data = &(tdata.values[1]);
universe@18 110 if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) {
universe@18 111 fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n");
universe@18 112 }
universe@18 113 if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
universe@18 114 fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n");
universe@18 115 }
universe@18 116 ucx_dlist_free(dl2);
universe@18 117
universe@18 118 printf(" Test ucx_dlist_clone\n");
universe@18 119 dl2 = ucx_dlist_clone(dl, NULL, NULL);
universe@18 120 if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
universe@18 121 fprintf(stderr, "ucx_dlist_clone (without copy) failed\n");
universe@18 122 }
universe@18 123 ucx_dlist_free(dl2);
universe@18 124
universe@18 125 printf(" TODO: test clone with copy\n");
universe@18 126
olaf@9 127 return r;
olaf@9 128 }
olaf@11 129
olaf@11 130 int list_tests() {
olaf@11 131 int r = 0;
olaf@11 132 int v[8];
olaf@11 133 UcxList *dl = NULL;
olaf@11 134 // build list 0,1,2,3,4,5,6,7
olaf@11 135 printf(" Test ucx_list_append\n");
olaf@11 136 fflush(stdout);
olaf@11 137 for(int i=0;i<8;i++) {
olaf@11 138 v[i] = i;
olaf@11 139 dl = ucx_list_append(dl, &v[i]);
olaf@11 140 }
olaf@11 141
olaf@11 142 printf(" Test ucx_list_get\n");
olaf@11 143 fflush(stdout);
olaf@11 144 for(int i=0;i<8;i++) {
olaf@11 145 UcxList *elm = ucx_list_get(dl, i);
olaf@11 146 if(elm == NULL) {
olaf@11 147 fprintf(stderr, "ucx_list_get failed: element is NULL\n");
olaf@11 148 r--;
olaf@11 149 }
olaf@11 150 if(elm->data == NULL) {
olaf@11 151 fprintf(stderr, "ucx_list_get failed: data is NULL\n");
olaf@11 152 r--;
olaf@11 153 }
olaf@11 154 int *data = (int*)elm->data;
olaf@11 155 if(*data != i) {
olaf@11 156 fprintf(stderr, "ucx_list_get failed with index %d\n", i);
olaf@11 157 r--;
olaf@11 158 }
olaf@11 159 }
olaf@11 160
olaf@11 161 printf(" Test ucx_list_free\n");
olaf@11 162 fflush(stdout);
olaf@11 163 ucx_list_free(dl);
olaf@11 164
olaf@11 165 dl = NULL;
olaf@11 166 // build list 4,0,4
olaf@11 167 printf(" Test ucx_list_prepend\n");
olaf@11 168 dl = ucx_list_prepend(dl, &v[0]);
olaf@11 169 dl = ucx_list_prepend(dl, &v[4]);
olaf@11 170 dl = ucx_list_append(dl, &v[4]);
olaf@11 171
olaf@11 172 struct test1_data tdata;
olaf@11 173 tdata.i = 0;
universe@18 174 ucx_list_foreach(dl, list_tests_foreach, &tdata);
olaf@11 175
olaf@11 176 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
olaf@11 177 fprintf(stderr, "prepend/append test failed\n");
universe@18 178 fprintf(stderr, "content: [%d, %d, %d]\n",
universe@18 179 tdata.values[0], tdata.values[1], tdata.values[2]);
olaf@11 180 r--;
olaf@11 181 }
universe@18 182
universe@18 183 printf(" Test ucx_list_equals\n");
universe@18 184 UcxList *dl2 = NULL;
universe@18 185 dl2 = ucx_list_append(dl2, &v[4]);
universe@18 186 dl2 = ucx_list_append(dl2, &v[0]);
universe@18 187 dl2 = ucx_list_append(dl2, &v[4]);
universe@18 188 if (!ucx_list_equals(dl, dl2, NULL, NULL)) {
universe@18 189 fprintf(stderr, "ucx_list_equals failed (false negative)\n");
universe@18 190 }
universe@18 191 dl2->next->data = NULL;
universe@18 192 if (ucx_list_equals(dl, dl2, NULL, NULL)) {
universe@18 193 fprintf(stderr, "ucx_list_equals failed (false positive)\n");
universe@18 194 }
universe@18 195 dl2->next->data = &(tdata.values[1]);
universe@18 196 if (!ucx_list_equals(dl, dl2, int_cmp, NULL)) {
universe@18 197 fprintf(stderr, "ucx_list_equals failed (cmp_func false negative)\n");
universe@18 198 }
universe@18 199 if (ucx_list_equals(dl, dl2, NULL, NULL)) {
universe@18 200 fprintf(stderr, "ucx_list_equals failed (cmp_func false positive)\n");
universe@18 201 }
universe@18 202 ucx_list_free(dl2);
universe@18 203
universe@18 204 printf(" Test ucx_list_clone\n");
universe@18 205 dl2 = ucx_list_clone(dl, NULL, NULL);
universe@18 206 if (!ucx_list_equals(dl, dl2, NULL, NULL)) {
universe@18 207 fprintf(stderr, "ucx_list_clone (without copy) failed\n");
universe@18 208 }
universe@18 209 ucx_list_free(dl2);
universe@18 210
universe@18 211 printf(" TODO: test clone with copy\n");
olaf@11 212
olaf@11 213 return r;
olaf@11 214 }

mercurial