test/list_tests.c

Sun, 15 Jan 2012 14:12:34 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 15 Jan 2012 14:12:34 +0100
changeset 22
76cdd8209f1f
parent 19
cdd7a3173249
child 24
e04822101291
permissions
-rw-r--r--

added ucx_dlist_remove and tests + fixed makefile error

     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 test1_data {
    12     int values[3];
    13     int i;
    14 };
    16 int int_cmp(void* e1, void *e2, void *data) {
    17     if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1;
    19     int *i1 = (int*)e1, *i2 = (int*)e2;
    20     int r = (*i1) - (*i2);
    21     return (r < 0) ? -1 : (r == 0 ? 0 : 1);
    22 }
    24 int dlist_tests_foreach(void *v, void *custom) {
    25     UcxDlist *dl = (UcxDlist*)v;
    26     struct test1_data *tdata = (struct test1_data*)custom;
    28     tdata->values[tdata->i] = *(int*)dl->data;
    29     tdata->i++;
    31     return 0;
    32 }
    34 int dlist_tests() {
    35     int r = 0;
    36     int v[8];
    37     UcxDlist *dl = NULL;
    38     // build list 0,1,2,3,4,5,6,7
    39     printf("   Test ucx_dlist_append\n");
    40     fflush(stdout);
    41     for(int i=0;i<8;i++) {
    42         v[i] = i;
    43         dl = ucx_dlist_append(dl, &v[i]);
    44     }
    46     printf("   Test ucx_dlist_get\n");
    47     fflush(stdout);
    48     for(int i=0;i<8;i++) {
    49         UcxDlist *elm = ucx_dlist_get(dl, i);
    50         if(elm == NULL) {
    51             fprintf(stderr, "ucx_dlist_get failed: element is NULL\n");
    52             r--;
    53         }
    54         if(elm->data == NULL) {
    55             fprintf(stderr, "ucx_dlist_get failed: data is NULL\n");
    56             r--;
    57         }
    58         int *data = (int*)elm->data;
    59         if(*data != i) {
    60             fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
    61             r--;
    62         }
    63     }
    65     printf("   Test ucx_dlist_free\n");
    66     fflush(stdout);
    67     ucx_dlist_free(dl);
    69     dl = NULL;
    70     // build list 4,0,4
    71     printf("   Test ucx_dlist_prepend\n");
    72     dl = ucx_dlist_prepend(dl, &v[0]);
    73     dl = ucx_dlist_prepend(dl, &v[4]);
    74     dl = ucx_dlist_append(dl, &v[4]);
    76     struct test1_data tdata;
    77     tdata.i = 0;
    78     ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata);
    80     if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
    81         fprintf(stderr, "prepend/append test failed\n");
    82         fprintf(stderr, "content: [%d, %d, %d]\n",
    83                 tdata.values[0], tdata.values[1], tdata.values[2]);
    84         r--;
    85     }
    87     printf("   Test ucx_dlist_equals\n");
    88     UcxDlist *dl2 = NULL;
    89     dl2 = ucx_dlist_append(dl2, &v[4]);
    90     dl2 = ucx_dlist_append(dl2, &v[0]);
    91     dl2 = ucx_dlist_append(dl2, &v[4]);
    92     if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
    93         fprintf(stderr, "ucx_dlist_equals failed (false negative)\n");
    94         r--;
    95     }
    96     dl2->next->data = NULL;
    97     if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
    98         fprintf(stderr, "ucx_dlist_equals failed (false positive)\n");
    99         r--;
   100     }
   101     dl2->next->data = &(tdata.values[1]);
   102     if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) {
   103         fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n");
   104         r--;
   105     }
   106     if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
   107         fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n");
   108         r--;
   109     }
   110     ucx_dlist_free(dl2);
   112     printf("   Test ucx_dlist_clone\n");
   113     dl2 = ucx_dlist_clone(dl, NULL, NULL);
   114     if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
   115         fprintf(stderr, "ucx_dlist_clone (without copy) failed\n");
   116         r--;
   117     }
   118     ucx_dlist_free(dl2);
   120     printf("   TODO: test clone with copy\n");
   122     ucx_dlist_free(dl);
   124     dl = NULL;
   125     printf("   Test ucx_dlist_remove\n");
   126     dl = ucx_dlist_append(dl, &v[4]);
   127     dl = ucx_dlist_append(dl, &v[0]);
   128     dl = ucx_dlist_append(dl, &v[3]);
   129     dl = ucx_dlist_remove(dl, dl->next);
   130     if (ucx_dlist_size(dl) == 2) {
   131         if ((*((int*)(dl->data)) != 4) || (*((int*)(dl->next->data)) != 3)) {
   132             fprintf(stderr, "ucx_dlist_remove failed (wrong data)\n");
   133             r--;
   134         }
   135     } else {
   136         fprintf(stderr, "ucx_dlist_remove failed (wrong size)\n");
   137         r--;
   138     }
   139     dl = ucx_dlist_remove(dl, dl);
   140     if (ucx_dlist_size(dl) == 1) {
   141         if ((*((int*)(dl->data)) != 3)) {
   142             fprintf(stderr, "ucx_dlist_remove first failed (wrong data)\n");
   143             r--;
   144         }
   145     } else {
   146         fprintf(stderr, "ucx_dlist_remove first failed (wrong size)\n");
   147         r--;
   148     }
   150     return r;
   151 }

mercurial