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