test/list_tests.c

Sat, 31 Dec 2011 18:57:30 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 31 Dec 2011 18:57:30 +0100
changeset 9
013c5c4b7e44
child 11
4f6082f99bd7
permissions
-rw-r--r--

Added dlist tests

olaf@9 1 /*
olaf@9 2 * tests of list implementation
olaf@9 3 */
olaf@9 4
olaf@9 5 #include <stdio.h>
olaf@9 6 #include <stdlib.h>
olaf@9 7
olaf@9 8 #include "ucx/list.h"
olaf@9 9 #include "ucx/dlist.h"
olaf@9 10
olaf@9 11 struct test1_data {
olaf@9 12 int values[3];
olaf@9 13 int i;
olaf@9 14 };
olaf@9 15
olaf@9 16 int list_tests_foreach1(void *v, void *custom) {
olaf@9 17 UcxDlist *dl = (UcxDlist*)v;
olaf@9 18 struct test1_data *tdata = (struct test1_data*)custom;
olaf@9 19
olaf@9 20 tdata->values[tdata->i] = *(int*)dl->data;
olaf@9 21 tdata->i++;
olaf@9 22 }
olaf@9 23
olaf@9 24 int list_tests() {
olaf@9 25 int r = 0;
olaf@9 26 int v[8];
olaf@9 27 UcxDlist *dl = NULL;
olaf@9 28 // build list 0,1,2,3,4,5,6,7
olaf@9 29 for(int i=0;i<8;i++) {
olaf@9 30 v[i] = i;
olaf@9 31 dl = ucx_dlist_append(dl, &v[i]);
olaf@9 32 }
olaf@9 33
olaf@9 34 for(int i=0;i<8;i++) {
olaf@9 35 UcxDlist *elm = ucx_dlist_get(dl, i);
olaf@9 36 if(elm == NULL) {
olaf@9 37 fprintf(stderr, "ucx_dlist_get failed: element is NULL\n");
olaf@9 38 }
olaf@9 39 if(elm->data == NULL) {
olaf@9 40 fprintf(stderr, "ucx_dlist_get failed: data is NULL\n");
olaf@9 41 }
olaf@9 42 int *data = (int*)elm->data;
olaf@9 43 if(*data != i) {
olaf@9 44 fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
olaf@9 45 }
olaf@9 46 }
olaf@9 47
olaf@9 48 //TODO: ucx_dlist_free test
olaf@9 49
olaf@9 50 dl = NULL;
olaf@9 51 // build list 4,0,4
olaf@9 52 dl = ucx_dlist_prepend(dl, &v[0]);
olaf@9 53 dl = ucx_dlist_prepend(dl, &v[5]);
olaf@9 54 dl = ucx_dlist_append(dl, &v[5]);
olaf@9 55
olaf@9 56 struct test1_data tdata;
olaf@9 57 tdata.i = 0;
olaf@9 58 ucx_dlist_foreach(dl, list_tests_foreach1, &tdata);
olaf@9 59
olaf@9 60 if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
olaf@9 61 fprintf(stderr, "prepend/append test failed\n");
olaf@9 62 }
olaf@9 63
olaf@9 64 return r;
olaf@9 65 }

mercurial