Wed, 08 Feb 2012 23:43:02 +0100
changed make clean + added dlist_clone with copy test + added va_end statements to string.c
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 | |
universe@24 | 11 | struct foreach_testdata { |
olaf@9 | 12 | int values[3]; |
olaf@9 | 13 | int i; |
olaf@9 | 14 | }; |
olaf@9 | 15 | |
universe@24 | 16 | void* int_cpy(void* source, void* data) { |
universe@24 | 17 | void *dest = malloc(sizeof(int)); |
universe@24 | 18 | if (dest != NULL) { |
universe@24 | 19 | *((int*)dest) = *((int*)source); |
universe@24 | 20 | } |
universe@24 | 21 | return dest; |
universe@24 | 22 | } |
universe@24 | 23 | |
universe@18 | 24 | int int_cmp(void* e1, void *e2, void *data) { |
universe@18 | 25 | if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1; |
universe@24 | 26 | |
universe@18 | 27 | int *i1 = (int*)e1, *i2 = (int*)e2; |
universe@18 | 28 | int r = (*i1) - (*i2); |
universe@18 | 29 | return (r < 0) ? -1 : (r == 0 ? 0 : 1); |
universe@18 | 30 | } |
universe@18 | 31 | |
universe@18 | 32 | int dlist_tests_foreach(void *v, void *custom) { |
olaf@9 | 33 | UcxDlist *dl = (UcxDlist*)v; |
universe@24 | 34 | struct foreach_testdata *tdata = (struct foreach_testdata*)custom; |
olaf@9 | 35 | |
olaf@9 | 36 | tdata->values[tdata->i] = *(int*)dl->data; |
olaf@9 | 37 | tdata->i++; |
universe@24 | 38 | |
universe@24 | 39 | return 0; |
universe@24 | 40 | } |
universe@24 | 41 | |
universe@24 | 42 | int dlist_free_content(void *v, void *custom) { |
universe@24 | 43 | UcxDlist *dl = (UcxDlist*)v; |
universe@24 | 44 | free(dl->data); |
universe@18 | 45 | return 0; |
olaf@9 | 46 | } |
olaf@9 | 47 | |
olaf@11 | 48 | int dlist_tests() { |
olaf@9 | 49 | int r = 0; |
olaf@9 | 50 | int v[8]; |
olaf@9 | 51 | UcxDlist *dl = NULL; |
olaf@9 | 52 | // build list 0,1,2,3,4,5,6,7 |
olaf@11 | 53 | printf(" Test ucx_dlist_append\n"); |
olaf@11 | 54 | fflush(stdout); |
olaf@9 | 55 | for(int i=0;i<8;i++) { |
olaf@9 | 56 | v[i] = i; |
olaf@9 | 57 | dl = ucx_dlist_append(dl, &v[i]); |
olaf@9 | 58 | } |
olaf@9 | 59 | |
olaf@11 | 60 | printf(" Test ucx_dlist_get\n"); |
olaf@11 | 61 | fflush(stdout); |
olaf@9 | 62 | for(int i=0;i<8;i++) { |
olaf@9 | 63 | UcxDlist *elm = ucx_dlist_get(dl, i); |
olaf@9 | 64 | if(elm == NULL) { |
olaf@9 | 65 | fprintf(stderr, "ucx_dlist_get failed: element is NULL\n"); |
olaf@11 | 66 | r--; |
universe@24 | 67 | } else if(elm->data == NULL) { |
olaf@9 | 68 | fprintf(stderr, "ucx_dlist_get failed: data is NULL\n"); |
olaf@11 | 69 | r--; |
universe@24 | 70 | } else { |
universe@24 | 71 | int *data = (int*)elm->data; |
universe@24 | 72 | if(*data != i) { |
universe@24 | 73 | fprintf(stderr, "ucx_dlist_get failed with index %d\n", i); |
universe@24 | 74 | r--; |
universe@24 | 75 | } |
olaf@9 | 76 | } |
olaf@9 | 77 | } |
olaf@9 | 78 | |
olaf@11 | 79 | printf(" Test ucx_dlist_free\n"); |
olaf@11 | 80 | fflush(stdout); |
olaf@11 | 81 | ucx_dlist_free(dl); |
olaf@11 | 82 | |
olaf@9 | 83 | dl = NULL; |
olaf@9 | 84 | // build list 4,0,4 |
olaf@11 | 85 | printf(" Test ucx_dlist_prepend\n"); |
olaf@9 | 86 | dl = ucx_dlist_prepend(dl, &v[0]); |
olaf@11 | 87 | dl = ucx_dlist_prepend(dl, &v[4]); |
olaf@11 | 88 | dl = ucx_dlist_append(dl, &v[4]); |
olaf@9 | 89 | |
universe@24 | 90 | struct foreach_testdata tdata; |
olaf@9 | 91 | tdata.i = 0; |
universe@18 | 92 | ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata); |
olaf@9 | 93 | |
olaf@9 | 94 | if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { |
olaf@9 | 95 | fprintf(stderr, "prepend/append test failed\n"); |
universe@18 | 96 | fprintf(stderr, "content: [%d, %d, %d]\n", |
universe@18 | 97 | tdata.values[0], tdata.values[1], tdata.values[2]); |
olaf@11 | 98 | r--; |
olaf@9 | 99 | } |
olaf@9 | 100 | |
universe@18 | 101 | printf(" Test ucx_dlist_equals\n"); |
universe@18 | 102 | UcxDlist *dl2 = NULL; |
universe@18 | 103 | dl2 = ucx_dlist_append(dl2, &v[4]); |
universe@18 | 104 | dl2 = ucx_dlist_append(dl2, &v[0]); |
universe@18 | 105 | dl2 = ucx_dlist_append(dl2, &v[4]); |
universe@18 | 106 | if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
universe@18 | 107 | fprintf(stderr, "ucx_dlist_equals failed (false negative)\n"); |
universe@19 | 108 | r--; |
universe@18 | 109 | } |
universe@18 | 110 | dl2->next->data = NULL; |
universe@18 | 111 | if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
universe@18 | 112 | fprintf(stderr, "ucx_dlist_equals failed (false positive)\n"); |
universe@19 | 113 | r--; |
universe@18 | 114 | } |
universe@18 | 115 | dl2->next->data = &(tdata.values[1]); |
universe@18 | 116 | if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) { |
universe@18 | 117 | fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n"); |
universe@19 | 118 | r--; |
universe@18 | 119 | } |
universe@18 | 120 | if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
universe@18 | 121 | fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n"); |
universe@19 | 122 | r--; |
universe@18 | 123 | } |
universe@18 | 124 | ucx_dlist_free(dl2); |
universe@24 | 125 | |
universe@18 | 126 | printf(" Test ucx_dlist_clone\n"); |
universe@18 | 127 | dl2 = ucx_dlist_clone(dl, NULL, NULL); |
universe@18 | 128 | if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
universe@18 | 129 | fprintf(stderr, "ucx_dlist_clone (without copy) failed\n"); |
universe@19 | 130 | r--; |
universe@18 | 131 | } |
universe@18 | 132 | ucx_dlist_free(dl2); |
universe@24 | 133 | |
universe@24 | 134 | printf(" Test ucx_dlist_clone with copy\n"); |
universe@24 | 135 | dl2 = ucx_dlist_clone(dl, int_cpy, NULL); |
universe@24 | 136 | if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
universe@24 | 137 | if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) { |
universe@24 | 138 | fprintf(stderr, "ucx_dlist_clone (with copy) failed (compare)\n"); |
universe@24 | 139 | r--; |
universe@24 | 140 | } |
universe@24 | 141 | } else { |
universe@24 | 142 | fprintf(stderr, "ucx_dlist_clone (with copy) failed (identity)\n"); |
universe@24 | 143 | r--; |
universe@24 | 144 | } |
universe@24 | 145 | ucx_dlist_foreach(dl, dlist_free_content, NULL); |
universe@24 | 146 | ucx_dlist_free(dl2); |
universe@18 | 147 | |
universe@22 | 148 | ucx_dlist_free(dl); |
universe@22 | 149 | |
universe@22 | 150 | dl = NULL; |
universe@22 | 151 | printf(" Test ucx_dlist_remove\n"); |
universe@22 | 152 | dl = ucx_dlist_append(dl, &v[4]); |
universe@22 | 153 | dl = ucx_dlist_append(dl, &v[0]); |
universe@22 | 154 | dl = ucx_dlist_append(dl, &v[3]); |
universe@22 | 155 | dl = ucx_dlist_remove(dl, dl->next); |
universe@22 | 156 | if (ucx_dlist_size(dl) == 2) { |
universe@22 | 157 | if ((*((int*)(dl->data)) != 4) || (*((int*)(dl->next->data)) != 3)) { |
universe@22 | 158 | fprintf(stderr, "ucx_dlist_remove failed (wrong data)\n"); |
universe@22 | 159 | r--; |
universe@22 | 160 | } |
universe@22 | 161 | } else { |
universe@22 | 162 | fprintf(stderr, "ucx_dlist_remove failed (wrong size)\n"); |
universe@22 | 163 | r--; |
universe@22 | 164 | } |
universe@22 | 165 | dl = ucx_dlist_remove(dl, dl); |
universe@22 | 166 | if (ucx_dlist_size(dl) == 1) { |
universe@22 | 167 | if ((*((int*)(dl->data)) != 3)) { |
universe@22 | 168 | fprintf(stderr, "ucx_dlist_remove first failed (wrong data)\n"); |
universe@22 | 169 | r--; |
universe@22 | 170 | } |
universe@22 | 171 | } else { |
universe@22 | 172 | fprintf(stderr, "ucx_dlist_remove first failed (wrong size)\n"); |
universe@22 | 173 | r--; |
universe@22 | 174 | } |
universe@22 | 175 | |
olaf@9 | 176 | return r; |
olaf@9 | 177 | } |
olaf@11 | 178 |