test/list_tests.c

Wed, 08 Feb 2012 23:43:02 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 08 Feb 2012 23:43:02 +0100
changeset 24
e04822101291
parent 22
76cdd8209f1f
child 27
22644e2572bc
permissions
-rw-r--r--

changed make clean + added dlist_clone with copy test + added va_end statements to string.c

     1 /*
     2  * tests of list implementation
     3  */
     5 #include <stdio.h>
     6 #include <stdlib.h>
     8 #include "ucx/list.h"
     9 #include "ucx/dlist.h"
    11 struct foreach_testdata {
    12     int values[3];
    13     int i;
    14 };
    16 void* int_cpy(void* source, void* data) {
    17     void *dest = malloc(sizeof(int));
    18     if (dest != NULL) {
    19         *((int*)dest) = *((int*)source);
    20     }
    21     return dest;
    22 }
    24 int int_cmp(void* e1, void *e2, void *data) {
    25     if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1;
    27     int *i1 = (int*)e1, *i2 = (int*)e2;
    28     int r = (*i1) - (*i2);
    29     return (r < 0) ? -1 : (r == 0 ? 0 : 1);
    30 }
    32 int dlist_tests_foreach(void *v, void *custom) {
    33     UcxDlist *dl = (UcxDlist*)v;
    34     struct foreach_testdata *tdata = (struct foreach_testdata*)custom;
    36     tdata->values[tdata->i] = *(int*)dl->data;
    37     tdata->i++;
    39     return 0;
    40 }
    42 int dlist_free_content(void *v, void *custom) {
    43     UcxDlist *dl = (UcxDlist*)v;
    44     free(dl->data);
    45     return 0;
    46 }
    48 int dlist_tests() {
    49     int r = 0;
    50     int v[8];
    51     UcxDlist *dl = NULL;
    52     // build list 0,1,2,3,4,5,6,7
    53     printf("   Test ucx_dlist_append\n");
    54     fflush(stdout);
    55     for(int i=0;i<8;i++) {
    56         v[i] = i;
    57         dl = ucx_dlist_append(dl, &v[i]);
    58     }
    60     printf("   Test ucx_dlist_get\n");
    61     fflush(stdout);
    62     for(int i=0;i<8;i++) {
    63         UcxDlist *elm = ucx_dlist_get(dl, i);
    64         if(elm == NULL) {
    65             fprintf(stderr, "ucx_dlist_get failed: element is NULL\n");
    66             r--;
    67         } else if(elm->data == NULL) {
    68             fprintf(stderr, "ucx_dlist_get failed: data is NULL\n");
    69             r--;
    70         } else {
    71             int *data = (int*)elm->data;
    72             if(*data != i) {
    73                 fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
    74                 r--;
    75             }
    76         }
    77     }
    79     printf("   Test ucx_dlist_free\n");
    80     fflush(stdout);
    81     ucx_dlist_free(dl);
    83     dl = NULL;
    84     // build list 4,0,4
    85     printf("   Test ucx_dlist_prepend\n");
    86     dl = ucx_dlist_prepend(dl, &v[0]);
    87     dl = ucx_dlist_prepend(dl, &v[4]);
    88     dl = ucx_dlist_append(dl, &v[4]);
    90     struct foreach_testdata tdata;
    91     tdata.i = 0;
    92     ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata);
    94     if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
    95         fprintf(stderr, "prepend/append test failed\n");
    96         fprintf(stderr, "content: [%d, %d, %d]\n",
    97                 tdata.values[0], tdata.values[1], tdata.values[2]);
    98         r--;
    99     }
   101     printf("   Test ucx_dlist_equals\n");
   102     UcxDlist *dl2 = NULL;
   103     dl2 = ucx_dlist_append(dl2, &v[4]);
   104     dl2 = ucx_dlist_append(dl2, &v[0]);
   105     dl2 = ucx_dlist_append(dl2, &v[4]);
   106     if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
   107         fprintf(stderr, "ucx_dlist_equals failed (false negative)\n");
   108         r--;
   109     }
   110     dl2->next->data = NULL;
   111     if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
   112         fprintf(stderr, "ucx_dlist_equals failed (false positive)\n");
   113         r--;
   114     }
   115     dl2->next->data = &(tdata.values[1]);
   116     if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) {
   117         fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n");
   118         r--;
   119     }
   120     if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
   121         fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n");
   122         r--;
   123     }
   124     ucx_dlist_free(dl2);
   126     printf("   Test ucx_dlist_clone\n");
   127     dl2 = ucx_dlist_clone(dl, NULL, NULL);
   128     if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
   129         fprintf(stderr, "ucx_dlist_clone (without copy) failed\n");
   130         r--;
   131     }
   132     ucx_dlist_free(dl2);
   134     printf("   Test ucx_dlist_clone with copy\n");
   135     dl2 = ucx_dlist_clone(dl, int_cpy, NULL);
   136     if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
   137         if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) {
   138             fprintf(stderr, "ucx_dlist_clone (with copy) failed (compare)\n");
   139             r--;
   140         }
   141     } else {
   142         fprintf(stderr, "ucx_dlist_clone (with copy) failed (identity)\n");
   143         r--;
   144     }
   145     ucx_dlist_foreach(dl, dlist_free_content, NULL);
   146     ucx_dlist_free(dl2);
   148     ucx_dlist_free(dl);
   150     dl = NULL;
   151     printf("   Test ucx_dlist_remove\n");
   152     dl = ucx_dlist_append(dl, &v[4]);
   153     dl = ucx_dlist_append(dl, &v[0]);
   154     dl = ucx_dlist_append(dl, &v[3]);
   155     dl = ucx_dlist_remove(dl, dl->next);
   156     if (ucx_dlist_size(dl) == 2) {
   157         if ((*((int*)(dl->data)) != 4) || (*((int*)(dl->next->data)) != 3)) {
   158             fprintf(stderr, "ucx_dlist_remove failed (wrong data)\n");
   159             r--;
   160         }
   161     } else {
   162         fprintf(stderr, "ucx_dlist_remove failed (wrong size)\n");
   163         r--;
   164     }
   165     dl = ucx_dlist_remove(dl, dl);
   166     if (ucx_dlist_size(dl) == 1) {
   167         if ((*((int*)(dl->data)) != 3)) {
   168             fprintf(stderr, "ucx_dlist_remove first failed (wrong data)\n");
   169             r--;
   170         }
   171     } else {
   172         fprintf(stderr, "ucx_dlist_remove first failed (wrong size)\n");
   173         r--;
   174     }
   176     return r;
   177 }

mercurial