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: universe@18: int int_cmp(void* e1, void *e2, void *data) { universe@18: if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1; universe@18: universe@18: int *i1 = (int*)e1, *i2 = (int*)e2; universe@18: int r = (*i1) - (*i2); universe@18: return (r < 0) ? -1 : (r == 0 ? 0 : 1); universe@18: } universe@18: universe@18: int dlist_tests_foreach(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++; universe@18: universe@18: return 0; olaf@9: } olaf@9: universe@18: int list_tests_foreach(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++; universe@18: universe@18: return 0; 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; universe@18: ucx_dlist_foreach(dl, dlist_tests_foreach, &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"); universe@18: fprintf(stderr, "content: [%d, %d, %d]\n", universe@18: tdata.values[0], tdata.values[1], tdata.values[2]); olaf@11: r--; olaf@9: } olaf@9: universe@18: printf(" Test ucx_dlist_equals\n"); universe@18: UcxDlist *dl2 = NULL; universe@18: dl2 = ucx_dlist_append(dl2, &v[4]); universe@18: dl2 = ucx_dlist_append(dl2, &v[0]); universe@18: dl2 = ucx_dlist_append(dl2, &v[4]); universe@18: if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { universe@18: fprintf(stderr, "ucx_dlist_equals failed (false negative)\n"); universe@18: } universe@18: dl2->next->data = NULL; universe@18: if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { universe@18: fprintf(stderr, "ucx_dlist_equals failed (false positive)\n"); universe@18: } universe@18: dl2->next->data = &(tdata.values[1]); universe@18: if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) { universe@18: fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n"); universe@18: } universe@18: if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { universe@18: fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n"); universe@18: } universe@18: ucx_dlist_free(dl2); universe@18: universe@18: printf(" Test ucx_dlist_clone\n"); universe@18: dl2 = ucx_dlist_clone(dl, NULL, NULL); universe@18: if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { universe@18: fprintf(stderr, "ucx_dlist_clone (without copy) failed\n"); universe@18: } universe@18: ucx_dlist_free(dl2); universe@18: universe@18: printf(" TODO: test clone with copy\n"); universe@18: 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; universe@18: ucx_list_foreach(dl, list_tests_foreach, &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"); universe@18: fprintf(stderr, "content: [%d, %d, %d]\n", universe@18: tdata.values[0], tdata.values[1], tdata.values[2]); olaf@11: r--; olaf@11: } universe@18: universe@18: printf(" Test ucx_list_equals\n"); universe@18: UcxList *dl2 = NULL; universe@18: dl2 = ucx_list_append(dl2, &v[4]); universe@18: dl2 = ucx_list_append(dl2, &v[0]); universe@18: dl2 = ucx_list_append(dl2, &v[4]); universe@18: if (!ucx_list_equals(dl, dl2, NULL, NULL)) { universe@18: fprintf(stderr, "ucx_list_equals failed (false negative)\n"); universe@18: } universe@18: dl2->next->data = NULL; universe@18: if (ucx_list_equals(dl, dl2, NULL, NULL)) { universe@18: fprintf(stderr, "ucx_list_equals failed (false positive)\n"); universe@18: } universe@18: dl2->next->data = &(tdata.values[1]); universe@18: if (!ucx_list_equals(dl, dl2, int_cmp, NULL)) { universe@18: fprintf(stderr, "ucx_list_equals failed (cmp_func false negative)\n"); universe@18: } universe@18: if (ucx_list_equals(dl, dl2, NULL, NULL)) { universe@18: fprintf(stderr, "ucx_list_equals failed (cmp_func false positive)\n"); universe@18: } universe@18: ucx_list_free(dl2); universe@18: universe@18: printf(" Test ucx_list_clone\n"); universe@18: dl2 = ucx_list_clone(dl, NULL, NULL); universe@18: if (!ucx_list_equals(dl, dl2, NULL, NULL)) { universe@18: fprintf(stderr, "ucx_list_clone (without copy) failed\n"); universe@18: } universe@18: ucx_list_free(dl2); universe@18: universe@18: printf(" TODO: test clone with copy\n"); olaf@11: olaf@11: return r; olaf@11: }