Wed, 11 Jan 2012 12:19:48 +0100
Removed linked list from tests (assume that they are correct if the dlist tests are correct)
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 | |
olaf@11 | 34 | int dlist_tests() { |
olaf@9 | 35 | int r = 0; |
olaf@9 | 36 | int v[8]; |
olaf@9 | 37 | UcxDlist *dl = NULL; |
olaf@9 | 38 | // build list 0,1,2,3,4,5,6,7 |
olaf@11 | 39 | printf(" Test ucx_dlist_append\n"); |
olaf@11 | 40 | fflush(stdout); |
olaf@9 | 41 | for(int i=0;i<8;i++) { |
olaf@9 | 42 | v[i] = i; |
olaf@9 | 43 | dl = ucx_dlist_append(dl, &v[i]); |
olaf@9 | 44 | } |
olaf@9 | 45 | |
olaf@11 | 46 | printf(" Test ucx_dlist_get\n"); |
olaf@11 | 47 | fflush(stdout); |
olaf@9 | 48 | for(int i=0;i<8;i++) { |
olaf@9 | 49 | UcxDlist *elm = ucx_dlist_get(dl, i); |
olaf@9 | 50 | if(elm == NULL) { |
olaf@9 | 51 | fprintf(stderr, "ucx_dlist_get failed: element is NULL\n"); |
olaf@11 | 52 | r--; |
olaf@9 | 53 | } |
olaf@9 | 54 | if(elm->data == NULL) { |
olaf@9 | 55 | fprintf(stderr, "ucx_dlist_get failed: data is NULL\n"); |
olaf@11 | 56 | r--; |
olaf@9 | 57 | } |
olaf@9 | 58 | int *data = (int*)elm->data; |
olaf@9 | 59 | if(*data != i) { |
olaf@9 | 60 | fprintf(stderr, "ucx_dlist_get failed with index %d\n", i); |
olaf@11 | 61 | r--; |
olaf@9 | 62 | } |
olaf@9 | 63 | } |
olaf@9 | 64 | |
olaf@11 | 65 | printf(" Test ucx_dlist_free\n"); |
olaf@11 | 66 | fflush(stdout); |
olaf@11 | 67 | ucx_dlist_free(dl); |
olaf@11 | 68 | |
olaf@9 | 69 | dl = NULL; |
olaf@9 | 70 | // build list 4,0,4 |
olaf@11 | 71 | printf(" Test ucx_dlist_prepend\n"); |
olaf@9 | 72 | dl = ucx_dlist_prepend(dl, &v[0]); |
olaf@11 | 73 | dl = ucx_dlist_prepend(dl, &v[4]); |
olaf@11 | 74 | dl = ucx_dlist_append(dl, &v[4]); |
olaf@9 | 75 | |
olaf@9 | 76 | struct test1_data tdata; |
olaf@9 | 77 | tdata.i = 0; |
universe@18 | 78 | ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata); |
olaf@9 | 79 | |
olaf@9 | 80 | if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { |
olaf@9 | 81 | fprintf(stderr, "prepend/append test failed\n"); |
universe@18 | 82 | fprintf(stderr, "content: [%d, %d, %d]\n", |
universe@18 | 83 | tdata.values[0], tdata.values[1], tdata.values[2]); |
olaf@11 | 84 | r--; |
olaf@9 | 85 | } |
olaf@9 | 86 | |
universe@18 | 87 | printf(" Test ucx_dlist_equals\n"); |
universe@18 | 88 | UcxDlist *dl2 = NULL; |
universe@18 | 89 | dl2 = ucx_dlist_append(dl2, &v[4]); |
universe@18 | 90 | dl2 = ucx_dlist_append(dl2, &v[0]); |
universe@18 | 91 | dl2 = ucx_dlist_append(dl2, &v[4]); |
universe@18 | 92 | if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
universe@18 | 93 | fprintf(stderr, "ucx_dlist_equals failed (false negative)\n"); |
universe@19 | 94 | r--; |
universe@18 | 95 | } |
universe@18 | 96 | dl2->next->data = NULL; |
universe@18 | 97 | if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
universe@18 | 98 | fprintf(stderr, "ucx_dlist_equals failed (false positive)\n"); |
universe@19 | 99 | r--; |
universe@18 | 100 | } |
universe@18 | 101 | dl2->next->data = &(tdata.values[1]); |
universe@18 | 102 | if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) { |
universe@18 | 103 | fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n"); |
universe@19 | 104 | r--; |
universe@18 | 105 | } |
universe@18 | 106 | if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
universe@18 | 107 | fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n"); |
universe@19 | 108 | r--; |
universe@18 | 109 | } |
universe@18 | 110 | ucx_dlist_free(dl2); |
universe@18 | 111 | |
universe@18 | 112 | printf(" Test ucx_dlist_clone\n"); |
universe@18 | 113 | dl2 = ucx_dlist_clone(dl, NULL, NULL); |
universe@18 | 114 | if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { |
universe@18 | 115 | fprintf(stderr, "ucx_dlist_clone (without copy) failed\n"); |
universe@19 | 116 | r--; |
universe@18 | 117 | } |
universe@18 | 118 | ucx_dlist_free(dl2); |
universe@18 | 119 | |
universe@18 | 120 | printf(" TODO: test clone with copy\n"); |
universe@18 | 121 | |
olaf@9 | 122 | return r; |
olaf@9 | 123 | } |
olaf@11 | 124 |