Wed, 04 Jan 2012 14:51:54 +0100
added clone and equals to lists
test/list_tests.c | file | annotate | diff | comparison | revisions | |
ucx/dlist.c | file | annotate | diff | comparison | revisions | |
ucx/dlist.h | file | annotate | diff | comparison | revisions | |
ucx/list.c | file | annotate | diff | comparison | revisions | |
ucx/list.h | file | annotate | diff | comparison | revisions | |
ucx/ucx.h | file | annotate | diff | comparison | revisions |
--- a/test/list_tests.c Sat Dec 31 22:48:28 2011 +0100 +++ b/test/list_tests.c Wed Jan 04 14:51:54 2012 +0100 @@ -13,20 +13,32 @@ int i; }; -int list_tests_foreach1(void *v, void *custom) { +int int_cmp(void* e1, void *e2, void *data) { + if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1; + + int *i1 = (int*)e1, *i2 = (int*)e2; + int r = (*i1) - (*i2); + return (r < 0) ? -1 : (r == 0 ? 0 : 1); +} + +int dlist_tests_foreach(void *v, void *custom) { UcxDlist *dl = (UcxDlist*)v; struct test1_data *tdata = (struct test1_data*)custom; tdata->values[tdata->i] = *(int*)dl->data; tdata->i++; + + return 0; } -int list_tests_foreach2(void *v, void *custom) { +int list_tests_foreach(void *v, void *custom) { UcxList *dl = (UcxList*)v; struct test1_data *tdata = (struct test1_data*)custom; tdata->values[tdata->i] = *(int*)dl->data; tdata->i++; + + return 0; } int dlist_tests() { @@ -73,14 +85,45 @@ struct test1_data tdata; tdata.i = 0; - ucx_dlist_foreach(dl, list_tests_foreach1, &tdata); + ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata); if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { fprintf(stderr, "prepend/append test failed\n"); - fprintf(stderr, "content: [%d, %d, %d]\n", tdata.values[0], tdata.values[1], tdata.values[2]); + fprintf(stderr, "content: [%d, %d, %d]\n", + tdata.values[0], tdata.values[1], tdata.values[2]); r--; } + printf(" Test ucx_dlist_equals\n"); + UcxDlist *dl2 = NULL; + dl2 = ucx_dlist_append(dl2, &v[4]); + dl2 = ucx_dlist_append(dl2, &v[0]); + dl2 = ucx_dlist_append(dl2, &v[4]); + if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { + fprintf(stderr, "ucx_dlist_equals failed (false negative)\n"); + } + dl2->next->data = NULL; + if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { + fprintf(stderr, "ucx_dlist_equals failed (false positive)\n"); + } + dl2->next->data = &(tdata.values[1]); + if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) { + fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n"); + } + if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { + fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n"); + } + ucx_dlist_free(dl2); + + printf(" Test ucx_dlist_clone\n"); + dl2 = ucx_dlist_clone(dl, NULL, NULL); + if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { + fprintf(stderr, "ucx_dlist_clone (without copy) failed\n"); + } + ucx_dlist_free(dl2); + + printf(" TODO: test clone with copy\n"); + return r; } @@ -128,13 +171,44 @@ struct test1_data tdata; tdata.i = 0; - ucx_list_foreach(dl, list_tests_foreach1, &tdata); + ucx_list_foreach(dl, list_tests_foreach, &tdata); if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { fprintf(stderr, "prepend/append test failed\n"); - fprintf(stderr, "content: [%d, %d, %d]\n", tdata.values[0], tdata.values[1], tdata.values[2]); + fprintf(stderr, "content: [%d, %d, %d]\n", + tdata.values[0], tdata.values[1], tdata.values[2]); r--; } + + printf(" Test ucx_list_equals\n"); + UcxList *dl2 = NULL; + dl2 = ucx_list_append(dl2, &v[4]); + dl2 = ucx_list_append(dl2, &v[0]); + dl2 = ucx_list_append(dl2, &v[4]); + if (!ucx_list_equals(dl, dl2, NULL, NULL)) { + fprintf(stderr, "ucx_list_equals failed (false negative)\n"); + } + dl2->next->data = NULL; + if (ucx_list_equals(dl, dl2, NULL, NULL)) { + fprintf(stderr, "ucx_list_equals failed (false positive)\n"); + } + dl2->next->data = &(tdata.values[1]); + if (!ucx_list_equals(dl, dl2, int_cmp, NULL)) { + fprintf(stderr, "ucx_list_equals failed (cmp_func false negative)\n"); + } + if (ucx_list_equals(dl, dl2, NULL, NULL)) { + fprintf(stderr, "ucx_list_equals failed (cmp_func false positive)\n"); + } + ucx_list_free(dl2); + + printf(" Test ucx_list_clone\n"); + dl2 = ucx_list_clone(dl, NULL, NULL); + if (!ucx_list_equals(dl, dl2, NULL, NULL)) { + fprintf(stderr, "ucx_list_clone (without copy) failed\n"); + } + ucx_list_free(dl2); + + printf(" TODO: test clone with copy\n"); return r; }
--- a/ucx/dlist.c Sat Dec 31 22:48:28 2011 +0100 +++ b/ucx/dlist.c Wed Jan 04 14:51:54 2012 +0100 @@ -1,5 +1,34 @@ #include "dlist.h" +UcxDlist *ucx_dlist_clone(UcxDlist *l, copy_func fnc, void *data) { + UcxDlist *ret = NULL; + while (l != NULL) { + if (fnc != NULL) { + ret = ucx_dlist_append(ret, fnc(l->data, data)); + } else { + ret = ucx_dlist_append(ret, l->data); + } + l = l->next; + } + return ret; +} + +int ucx_dlist_equals(UcxDlist *l1, UcxDlist *l2, cmp_func fnc, void* data) { + if (l1 == l2) return 1; + + while (l1 != NULL && l2 != NULL) { + if (fnc == NULL) { + if (l1->data != l2->data) return 0; + } else { + if (fnc(l1->data, l2->data, data) != 0) return 0; + } + l1 = l1->next; + l2 = l2->next; + } + + return (l1 == NULL && l2 == NULL); +} + void ucx_dlist_free(UcxDlist *l) { UcxDlist *e = l, *f; while (e != NULL) {
--- a/ucx/dlist.h Sat Dec 31 22:48:28 2011 +0100 +++ b/ucx/dlist.h Wed Jan 04 14:51:54 2012 +0100 @@ -19,6 +19,9 @@ UcxDlist *prev; }; +UcxDlist *ucx_dlist_clone(UcxDlist *l, copy_func fnc, void* data); +int ucx_dlist_equals(UcxDlist *l1, UcxDlist *l2, cmp_func fnc, void* data); + void ucx_dlist_free(UcxDlist *l); UcxDlist *ucx_dlist_append(UcxDlist *l, void *data); UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data);
--- a/ucx/list.c Sat Dec 31 22:48:28 2011 +0100 +++ b/ucx/list.c Wed Jan 04 14:51:54 2012 +0100 @@ -1,5 +1,34 @@ #include "list.h" +UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) { + UcxList *ret = NULL; + while (l != NULL) { + if (fnc != NULL) { + ret = ucx_list_append(ret, fnc(l->data, data)); + } else { + ret = ucx_list_append(ret, l->data); + } + l = l->next; + } + return ret; +} + +int ucx_list_equals(UcxList *l1, UcxList *l2, cmp_func fnc, void* data) { + if (l1 == l2) return 1; + + while (l1 != NULL && l2 != NULL) { + if (fnc == NULL) { + if (l1->data != l2->data) return 0; + } else { + if (fnc(l1->data, l2->data, data) != 0) return 0; + } + l1 = l1->next; + l2 = l2->next; + } + + return (l1 == NULL && l2 == NULL); +} + void ucx_list_free(UcxList *l) { UcxList *e = l, *f; while (e != NULL) {
--- a/ucx/list.h Sat Dec 31 22:48:28 2011 +0100 +++ b/ucx/list.h Wed Jan 04 14:51:54 2012 +0100 @@ -18,6 +18,9 @@ UcxList *next; }; +UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data); +int ucx_list_equals(UcxList *l1, UcxList *l2, cmp_func fnc, void *data); + void ucx_list_free(UcxList *l); UcxList *ucx_list_append(UcxList *l, void *data); UcxList *ucx_list_prepend(UcxList *l, void *data);
--- a/ucx/ucx.h Sat Dec 31 22:48:28 2011 +0100 +++ b/ucx/ucx.h Wed Jan 04 14:51:54 2012 +0100 @@ -14,8 +14,15 @@ extern "C" { #endif +/* source,data -> errno */ typedef int(*ucx_callback)(void*,void*); +/* element1,element2,custom data -> {-1,0,1} */ +typedef int(*cmp_func)(void*,void*,void*); + +/* element,custom data -> copy of element */ +typedef void*(*copy_func)(void*,void*); + #ifdef __cplusplus } #endif