test/list_tests.c

changeset 24
e04822101291
parent 22
76cdd8209f1f
child 27
22644e2572bc
     1.1 --- a/test/list_tests.c	Sun Jan 15 14:20:25 2012 +0100
     1.2 +++ b/test/list_tests.c	Wed Feb 08 23:43:02 2012 +0100
     1.3 @@ -8,14 +8,22 @@
     1.4  #include "ucx/list.h"
     1.5  #include "ucx/dlist.h"
     1.6  
     1.7 -struct test1_data {
     1.8 +struct foreach_testdata {
     1.9      int values[3];
    1.10      int i;
    1.11  };
    1.12  
    1.13 +void* int_cpy(void* source, void* data) {
    1.14 +    void *dest = malloc(sizeof(int));
    1.15 +    if (dest != NULL) {
    1.16 +        *((int*)dest) = *((int*)source);
    1.17 +    }
    1.18 +    return dest;
    1.19 +}
    1.20 +
    1.21  int int_cmp(void* e1, void *e2, void *data) {
    1.22      if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1;
    1.23 -    
    1.24 +
    1.25      int *i1 = (int*)e1, *i2 = (int*)e2;
    1.26      int r = (*i1) - (*i2);
    1.27      return (r < 0) ? -1 : (r == 0 ? 0 : 1);
    1.28 @@ -23,11 +31,17 @@
    1.29  
    1.30  int dlist_tests_foreach(void *v, void *custom) {
    1.31      UcxDlist *dl = (UcxDlist*)v;
    1.32 -    struct test1_data *tdata = (struct test1_data*)custom;
    1.33 +    struct foreach_testdata *tdata = (struct foreach_testdata*)custom;
    1.34  
    1.35      tdata->values[tdata->i] = *(int*)dl->data;
    1.36      tdata->i++;
    1.37 -    
    1.38 +
    1.39 +    return 0;
    1.40 +}
    1.41 +
    1.42 +int dlist_free_content(void *v, void *custom) {
    1.43 +    UcxDlist *dl = (UcxDlist*)v;
    1.44 +    free(dl->data);
    1.45      return 0;
    1.46  }
    1.47  
    1.48 @@ -50,15 +64,15 @@
    1.49          if(elm == NULL) {
    1.50              fprintf(stderr, "ucx_dlist_get failed: element is NULL\n");
    1.51              r--;
    1.52 -        }
    1.53 -        if(elm->data == NULL) {
    1.54 +        } else if(elm->data == NULL) {
    1.55              fprintf(stderr, "ucx_dlist_get failed: data is NULL\n");
    1.56              r--;
    1.57 -        }
    1.58 -        int *data = (int*)elm->data;
    1.59 -        if(*data != i) {
    1.60 -            fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
    1.61 -            r--;
    1.62 +        } else {
    1.63 +            int *data = (int*)elm->data;
    1.64 +            if(*data != i) {
    1.65 +                fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
    1.66 +                r--;
    1.67 +            }
    1.68          }
    1.69      }
    1.70  
    1.71 @@ -73,7 +87,7 @@
    1.72      dl = ucx_dlist_prepend(dl, &v[4]);
    1.73      dl = ucx_dlist_append(dl, &v[4]);
    1.74  
    1.75 -    struct test1_data tdata;
    1.76 +    struct foreach_testdata tdata;
    1.77      tdata.i = 0;
    1.78      ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata);
    1.79  
    1.80 @@ -108,7 +122,7 @@
    1.81          r--;
    1.82      }
    1.83      ucx_dlist_free(dl2);
    1.84 -    
    1.85 +
    1.86      printf("   Test ucx_dlist_clone\n");
    1.87      dl2 = ucx_dlist_clone(dl, NULL, NULL);
    1.88      if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
    1.89 @@ -116,8 +130,20 @@
    1.90          r--;
    1.91      }
    1.92      ucx_dlist_free(dl2);
    1.93 -    
    1.94 -    printf("   TODO: test clone with copy\n");
    1.95 +
    1.96 +    printf("   Test ucx_dlist_clone with copy\n");
    1.97 +    dl2 = ucx_dlist_clone(dl, int_cpy, NULL);
    1.98 +    if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
    1.99 +        if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) {
   1.100 +            fprintf(stderr, "ucx_dlist_clone (with copy) failed (compare)\n");
   1.101 +            r--;
   1.102 +        }
   1.103 +    } else {
   1.104 +        fprintf(stderr, "ucx_dlist_clone (with copy) failed (identity)\n");
   1.105 +        r--;
   1.106 +    }
   1.107 +    ucx_dlist_foreach(dl, dlist_free_content, NULL);
   1.108 +    ucx_dlist_free(dl2);
   1.109  
   1.110      ucx_dlist_free(dl);
   1.111  

mercurial