olaf@9: /* olaf@9: * tests of list implementation olaf@9: */ olaf@9: olaf@9: #include olaf@9: #include olaf@9: olaf@9: #include "ucx/list.h" olaf@9: #include "ucx/dlist.h" olaf@9: olaf@9: struct test1_data { olaf@9: int values[3]; olaf@9: int i; olaf@9: }; olaf@9: olaf@9: int list_tests_foreach1(void *v, void *custom) { olaf@9: UcxDlist *dl = (UcxDlist*)v; olaf@9: struct test1_data *tdata = (struct test1_data*)custom; olaf@9: olaf@9: tdata->values[tdata->i] = *(int*)dl->data; olaf@9: tdata->i++; olaf@9: } olaf@9: olaf@11: int list_tests_foreach2(void *v, void *custom) { olaf@11: UcxList *dl = (UcxList*)v; olaf@11: struct test1_data *tdata = (struct test1_data*)custom; olaf@11: olaf@11: tdata->values[tdata->i] = *(int*)dl->data; olaf@11: tdata->i++; olaf@11: } olaf@11: olaf@11: int dlist_tests() { olaf@9: int r = 0; olaf@9: int v[8]; olaf@9: UcxDlist *dl = NULL; olaf@9: // build list 0,1,2,3,4,5,6,7 olaf@11: printf(" Test ucx_dlist_append\n"); olaf@11: fflush(stdout); olaf@9: for(int i=0;i<8;i++) { olaf@9: v[i] = i; olaf@9: dl = ucx_dlist_append(dl, &v[i]); olaf@9: } olaf@9: olaf@11: printf(" Test ucx_dlist_get\n"); olaf@11: fflush(stdout); olaf@9: for(int i=0;i<8;i++) { olaf@9: UcxDlist *elm = ucx_dlist_get(dl, i); olaf@9: if(elm == NULL) { olaf@9: fprintf(stderr, "ucx_dlist_get failed: element is NULL\n"); olaf@11: r--; olaf@9: } olaf@9: if(elm->data == NULL) { olaf@9: fprintf(stderr, "ucx_dlist_get failed: data is NULL\n"); olaf@11: r--; olaf@9: } olaf@9: int *data = (int*)elm->data; olaf@9: if(*data != i) { olaf@9: fprintf(stderr, "ucx_dlist_get failed with index %d\n", i); olaf@11: r--; olaf@9: } olaf@9: } olaf@9: olaf@11: printf(" Test ucx_dlist_free\n"); olaf@11: fflush(stdout); olaf@11: ucx_dlist_free(dl); olaf@11: olaf@9: dl = NULL; olaf@9: // build list 4,0,4 olaf@11: printf(" Test ucx_dlist_prepend\n"); olaf@9: dl = ucx_dlist_prepend(dl, &v[0]); olaf@11: dl = ucx_dlist_prepend(dl, &v[4]); olaf@11: dl = ucx_dlist_append(dl, &v[4]); olaf@9: olaf@9: struct test1_data tdata; olaf@9: tdata.i = 0; olaf@9: ucx_dlist_foreach(dl, list_tests_foreach1, &tdata); olaf@9: olaf@9: if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { olaf@9: fprintf(stderr, "prepend/append test failed\n"); olaf@11: fprintf(stderr, "content: [%d, %d, %d]\n", tdata.values[0], tdata.values[1], tdata.values[2]); olaf@11: r--; olaf@9: } olaf@9: olaf@9: return r; olaf@9: } olaf@11: olaf@11: int list_tests() { olaf@11: int r = 0; olaf@11: int v[8]; olaf@11: UcxList *dl = NULL; olaf@11: // build list 0,1,2,3,4,5,6,7 olaf@11: printf(" Test ucx_list_append\n"); olaf@11: fflush(stdout); olaf@11: for(int i=0;i<8;i++) { olaf@11: v[i] = i; olaf@11: dl = ucx_list_append(dl, &v[i]); olaf@11: } olaf@11: olaf@11: printf(" Test ucx_list_get\n"); olaf@11: fflush(stdout); olaf@11: for(int i=0;i<8;i++) { olaf@11: UcxList *elm = ucx_list_get(dl, i); olaf@11: if(elm == NULL) { olaf@11: fprintf(stderr, "ucx_list_get failed: element is NULL\n"); olaf@11: r--; olaf@11: } olaf@11: if(elm->data == NULL) { olaf@11: fprintf(stderr, "ucx_list_get failed: data is NULL\n"); olaf@11: r--; olaf@11: } olaf@11: int *data = (int*)elm->data; olaf@11: if(*data != i) { olaf@11: fprintf(stderr, "ucx_list_get failed with index %d\n", i); olaf@11: r--; olaf@11: } olaf@11: } olaf@11: olaf@11: printf(" Test ucx_list_free\n"); olaf@11: fflush(stdout); olaf@11: ucx_list_free(dl); olaf@11: olaf@11: dl = NULL; olaf@11: // build list 4,0,4 olaf@11: printf(" Test ucx_list_prepend\n"); olaf@11: dl = ucx_list_prepend(dl, &v[0]); olaf@11: dl = ucx_list_prepend(dl, &v[4]); olaf@11: dl = ucx_list_append(dl, &v[4]); olaf@11: olaf@11: struct test1_data tdata; olaf@11: tdata.i = 0; olaf@11: ucx_list_foreach(dl, list_tests_foreach1, &tdata); olaf@11: olaf@11: if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { olaf@11: fprintf(stderr, "prepend/append test failed\n"); olaf@11: fprintf(stderr, "content: [%d, %d, %d]\n", tdata.values[0], tdata.values[1], tdata.values[2]); olaf@11: r--; olaf@11: } olaf@11: olaf@11: return r; olaf@11: }