removed old foreach + refactored list tests

Sat, 18 Feb 2012 18:36:30 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 18 Feb 2012 18:36:30 +0100
changeset 27
22644e2572bc
parent 26
59f147baea31
child 28
1666cbeb1db8

removed old foreach + refactored list tests

test/Makefile file | annotate | diff | comparison | revisions
test/dlist_tests.c file | annotate | diff | comparison | revisions
test/dlist_tests.h file | annotate | diff | comparison | revisions
test/list_tests.c file | annotate | diff | comparison | revisions
test/list_tests.h file | annotate | diff | comparison | revisions
test/main.c file | annotate | diff | comparison | revisions
test/main.h 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/test.h file | annotate | diff | comparison | revisions
     1.1 --- a/test/Makefile	Sat Feb 18 15:50:43 2012 +0100
     1.2 +++ b/test/Makefile	Sat Feb 18 18:36:30 2012 +0100
     1.3 @@ -28,7 +28,7 @@
     1.4  
     1.5  include ../$(CONF).mk
     1.6  
     1.7 -SRC = main.c list_tests.c mpool_tests.c map_tests.c
     1.8 +SRC = main.c list_tests.c dlist_tests.c mpool_tests.c map_tests.c
     1.9  
    1.10  OBJ = $(SRC:%.c=../build/%.$(OBJ_EXT))
    1.11  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/dlist_tests.c	Sat Feb 18 18:36:30 2012 +0100
     2.3 @@ -0,0 +1,167 @@
     2.4 +/*
     2.5 + * tests of dlist implementation
     2.6 + */
     2.7 +
     2.8 +#include "dlist_tests.h"
     2.9 +
    2.10 +UCX_TEST_BEGIN(test_ucx_dlist_append) {
    2.11 +    UcxDlist *list = ucx_dlist_append(NULL, "Hello");
    2.12 +    
    2.13 +    UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    2.14 +    
    2.15 +    list = ucx_dlist_append(list, " World!");
    2.16 +    
    2.17 +    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    2.18 +    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    2.19 +    
    2.20 +    ucx_dlist_free(list);
    2.21 +    
    2.22 +    UCX_TEST_END
    2.23 +}
    2.24 +
    2.25 +UCX_TEST_BEGIN(test_ucx_dlist_prepend) {
    2.26 +    UcxDlist *list = ucx_dlist_prepend(NULL, " World!");
    2.27 +    list = ucx_dlist_prepend(list, "Hello");
    2.28 +    
    2.29 +    UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    2.30 +    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    2.31 +    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    2.32 +    
    2.33 +    ucx_dlist_free(list);
    2.34 +    
    2.35 +    UCX_TEST_END
    2.36 +}
    2.37 +
    2.38 +UCX_TEST_BEGIN(test_ucx_dlist_equals) {
    2.39 +    UcxDlist *list = ucx_dlist_append(NULL, "Hello");
    2.40 +    list = ucx_dlist_append(list, " World!");
    2.41 +    UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!");
    2.42 +    list2 = ucx_dlist_prepend(list2, "Hello");
    2.43 +    UcxDlist *list3 = ucx_dlist_prepend(NULL, " Welt!");
    2.44 +    list3 = ucx_dlist_prepend(list3, "Hallo");
    2.45 +    
    2.46 +    UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, cmp_string, NULL), "failed")
    2.47 +    UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, cmp_string, NULL), "failed")
    2.48 +    
    2.49 +    ucx_dlist_free(list3);
    2.50 +    ucx_dlist_free(list2);
    2.51 +    ucx_dlist_free(list);
    2.52 +    
    2.53 +    UCX_TEST_END
    2.54 +}
    2.55 +
    2.56 +UCX_TEST_BEGIN(test_ucx_dlist_concat) {
    2.57 +    UcxDlist *list = ucx_dlist_append(NULL, "Hello");
    2.58 +    UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!");
    2.59 +    
    2.60 +    list = ucx_dlist_concat(list, list2);
    2.61 +    
    2.62 +    UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    2.63 +    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    2.64 +    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    2.65 +    
    2.66 +    ucx_dlist_free(list2);
    2.67 +    ucx_dlist_free(list);
    2.68 +    
    2.69 +    UCX_TEST_END
    2.70 +}
    2.71 +
    2.72 +UCX_TEST_BEGIN(test_ucx_dlist_size) {
    2.73 +    UcxDlist *list = ucx_dlist_append(NULL, "This ");
    2.74 +    list = ucx_dlist_append(list, "list ");
    2.75 +    list = ucx_dlist_append(list, "has ");
    2.76 +    list = ucx_dlist_append(list, "size ");
    2.77 +    list = ucx_dlist_append(list, "5!");
    2.78 +    
    2.79 +    UCX_TEST_ASSERT(ucx_dlist_size(list) == 5, "failed");
    2.80 +    
    2.81 +    ucx_dlist_free(list);
    2.82 +    
    2.83 +    UCX_TEST_END
    2.84 +}
    2.85 +
    2.86 +UCX_TEST_BEGIN(test_ucx_dlist_first) {
    2.87 +    UcxDlist *list = ucx_dlist_append(NULL, "Find ");
    2.88 +    list = ucx_dlist_append(list, "the ");
    2.89 +    list = ucx_dlist_append(list, "first!");
    2.90 +    
    2.91 +    char* first = (char*) (ucx_dlist_first(list)->data);
    2.92 +    
    2.93 +    UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
    2.94 +    
    2.95 +    ucx_dlist_free(list);
    2.96 +    
    2.97 +    UCX_TEST_END
    2.98 +}
    2.99 +
   2.100 +UCX_TEST_BEGIN(test_ucx_dlist_last) {
   2.101 +    UcxDlist *list = ucx_dlist_append(NULL, "Find ");
   2.102 +    list = ucx_dlist_append(list, "the ");
   2.103 +    list = ucx_dlist_append(list, "last!");
   2.104 +    
   2.105 +    char* last = (char*) (ucx_dlist_last(list)->data);
   2.106 +    
   2.107 +    UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
   2.108 +    
   2.109 +    ucx_dlist_free(list);
   2.110 +    
   2.111 +    UCX_TEST_END
   2.112 +}
   2.113 +
   2.114 +UCX_TEST_BEGIN(test_ucx_dlist_get) {
   2.115 +    UcxDlist *list = ucx_dlist_append(NULL, "Find ");
   2.116 +    list = ucx_dlist_append(list, "the ");
   2.117 +    list = ucx_dlist_append(list, "mid!");
   2.118 +    
   2.119 +    char* mid = (char*) (ucx_dlist_get(list, 1)->data);
   2.120 +    
   2.121 +    UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
   2.122 +    
   2.123 +    ucx_dlist_free(list);
   2.124 +    
   2.125 +    UCX_TEST_END
   2.126 +}
   2.127 +
   2.128 +UCX_TEST_BEGIN(test_ucx_dlist_remove) {
   2.129 +    UcxDlist *list = ucx_dlist_append(NULL, "Hello");
   2.130 +    list = ucx_dlist_append(list, " fucking");
   2.131 +    list = ucx_dlist_append(list, " World!");
   2.132 +    
   2.133 +    list = ucx_dlist_remove(list, ucx_dlist_get(list, 1));
   2.134 +    
   2.135 +    UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
   2.136 +    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
   2.137 +    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
   2.138 +    
   2.139 +    ucx_dlist_free(list);
   2.140 +    
   2.141 +    UCX_TEST_END
   2.142 +}
   2.143 +
   2.144 +UCX_TEST_BEGIN(test_ucx_dlist_clone) {
   2.145 +   
   2.146 +    char *hello = (char*)malloc(6);
   2.147 +    char *world = (char*)malloc(8);
   2.148 +    
   2.149 +    memcpy(hello, "Hello", 6);
   2.150 +    memcpy(world, " World!", 8);
   2.151 +    
   2.152 +    UcxDlist *list = ucx_dlist_append(NULL, hello);
   2.153 +    list = ucx_dlist_append(list, world);
   2.154 +    
   2.155 +    UcxDlist *copy = ucx_dlist_clone(list, copy_string, NULL);
   2.156 +
   2.157 +    UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, cmp_string, NULL), "failed")
   2.158 +    UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
   2.159 +    UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
   2.160 +
   2.161 +    free(copy->next->data);
   2.162 +    free(copy->data);
   2.163 +
   2.164 +    free(world);
   2.165 +    free(hello);
   2.166 +    free(list);
   2.167 +    free(copy);
   2.168 +    
   2.169 +    UCX_TEST_END
   2.170 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/dlist_tests.h	Sat Feb 18 18:36:30 2012 +0100
     3.3 @@ -0,0 +1,41 @@
     3.4 +/* 
     3.5 + * File:   dlist_tests.h
     3.6 + * Author: Mike
     3.7 + *
     3.8 + * Created on 18. Februar 2012, 18:26
     3.9 + */
    3.10 +
    3.11 +#ifndef DLIST_TESTS_H
    3.12 +#define	DLIST_TESTS_H
    3.13 +
    3.14 +#include "main.h"
    3.15 +
    3.16 +#include "ucx/dlist.h"
    3.17 +#include "ucx/test.h"
    3.18 +
    3.19 +#ifdef	__cplusplus
    3.20 +extern "C" {
    3.21 +#endif
    3.22 +
    3.23 +/*
    3.24 + * Assumed to be correct:
    3.25 + *   ucx_dlist_free
    3.26 + */
    3.27 +
    3.28 +UCX_TEST_DECLARE(test_ucx_dlist_append)
    3.29 +UCX_TEST_DECLARE(test_ucx_dlist_prepend)
    3.30 +UCX_TEST_DECLARE(test_ucx_dlist_equals)
    3.31 +UCX_TEST_DECLARE(test_ucx_dlist_concat)
    3.32 +UCX_TEST_DECLARE(test_ucx_dlist_size)
    3.33 +UCX_TEST_DECLARE(test_ucx_dlist_first)
    3.34 +UCX_TEST_DECLARE(test_ucx_dlist_last)
    3.35 +UCX_TEST_DECLARE(test_ucx_dlist_get)
    3.36 +UCX_TEST_DECLARE(test_ucx_dlist_remove)
    3.37 +UCX_TEST_DECLARE(test_ucx_dlist_clone)
    3.38 +
    3.39 +#ifdef	__cplusplus
    3.40 +}
    3.41 +#endif
    3.42 +
    3.43 +#endif	/* DLIST_TESTS_H */
    3.44 +
     4.1 --- a/test/list_tests.c	Sat Feb 18 15:50:43 2012 +0100
     4.2 +++ b/test/list_tests.c	Sat Feb 18 18:36:30 2012 +0100
     4.3 @@ -2,177 +2,152 @@
     4.4   * tests of list implementation
     4.5   */
     4.6  
     4.7 -#include <stdio.h>
     4.8 -#include <stdlib.h>
     4.9 +#include "list_tests.h"
    4.10  
    4.11 -#include "ucx/list.h"
    4.12 -#include "ucx/dlist.h"
    4.13 -
    4.14 -struct foreach_testdata {
    4.15 -    int values[3];
    4.16 -    int i;
    4.17 -};
    4.18 -
    4.19 -void* int_cpy(void* source, void* data) {
    4.20 -    void *dest = malloc(sizeof(int));
    4.21 -    if (dest != NULL) {
    4.22 -        *((int*)dest) = *((int*)source);
    4.23 -    }
    4.24 -    return dest;
    4.25 +UCX_TEST_BEGIN(test_ucx_list_append) {
    4.26 +    UcxList *list = ucx_list_append(NULL, "Hello");
    4.27 +    
    4.28 +    UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    4.29 +    
    4.30 +    list = ucx_list_append(list, " World!");
    4.31 +    
    4.32 +    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    4.33 +    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    4.34 +    
    4.35 +    ucx_list_free(list);
    4.36 +    
    4.37 +    UCX_TEST_END
    4.38  }
    4.39  
    4.40 -int int_cmp(void* e1, void *e2, void *data) {
    4.41 -    if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1;
    4.42 -
    4.43 -    int *i1 = (int*)e1, *i2 = (int*)e2;
    4.44 -    int r = (*i1) - (*i2);
    4.45 -    return (r < 0) ? -1 : (r == 0 ? 0 : 1);
    4.46 +UCX_TEST_BEGIN(test_ucx_list_prepend) {
    4.47 +    UcxList *list = ucx_list_prepend(NULL, " World!");
    4.48 +    list = ucx_list_prepend(list, "Hello");
    4.49 +    
    4.50 +    UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    4.51 +    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    4.52 +    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    4.53 +    
    4.54 +    ucx_list_free(list);
    4.55 +    
    4.56 +    UCX_TEST_END
    4.57  }
    4.58  
    4.59 -int dlist_tests_foreach(void *v, void *custom) {
    4.60 -    UcxDlist *dl = (UcxDlist*)v;
    4.61 -    struct foreach_testdata *tdata = (struct foreach_testdata*)custom;
    4.62 -
    4.63 -    tdata->values[tdata->i] = *(int*)dl->data;
    4.64 -    tdata->i++;
    4.65 -
    4.66 -    return 0;
    4.67 +UCX_TEST_BEGIN(test_ucx_list_equals) {
    4.68 +    UcxList *list = ucx_list_append(NULL, "Hello");
    4.69 +    list = ucx_list_append(list, " World!");
    4.70 +    UcxList *list2 = ucx_list_prepend(NULL, " World!");
    4.71 +    list2 = ucx_list_prepend(list2, "Hello");
    4.72 +    UcxList *list3 = ucx_list_prepend(NULL, " Welt!");
    4.73 +    list3 = ucx_list_prepend(list3, "Hallo");
    4.74 +    
    4.75 +    UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed")
    4.76 +    UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed")
    4.77 +    
    4.78 +    ucx_list_free(list3);
    4.79 +    ucx_list_free(list2);
    4.80 +    ucx_list_free(list);
    4.81 +    
    4.82 +    UCX_TEST_END
    4.83  }
    4.84  
    4.85 -int dlist_free_content(void *v, void *custom) {
    4.86 -    UcxDlist *dl = (UcxDlist*)v;
    4.87 -    free(dl->data);
    4.88 -    return 0;
    4.89 +UCX_TEST_BEGIN(test_ucx_list_concat) {
    4.90 +    UcxList *list = ucx_list_append(NULL, "Hello");
    4.91 +    UcxList *list2 = ucx_list_prepend(NULL, " World!");
    4.92 +    
    4.93 +    list = ucx_list_concat(list, list2);
    4.94 +    
    4.95 +    UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    4.96 +    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    4.97 +    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    4.98 +    
    4.99 +    ucx_list_free(list2);
   4.100 +    ucx_list_free(list);
   4.101 +    
   4.102 +    UCX_TEST_END
   4.103  }
   4.104  
   4.105 -int dlist_tests() {
   4.106 -    int r = 0;
   4.107 -    int v[8];
   4.108 -    UcxDlist *dl = NULL;
   4.109 -    // build list 0,1,2,3,4,5,6,7
   4.110 -    printf("   Test ucx_dlist_append\n");
   4.111 -    fflush(stdout);
   4.112 -    for(int i=0;i<8;i++) {
   4.113 -        v[i] = i;
   4.114 -        dl = ucx_dlist_append(dl, &v[i]);
   4.115 -    }
   4.116 -
   4.117 -    printf("   Test ucx_dlist_get\n");
   4.118 -    fflush(stdout);
   4.119 -    for(int i=0;i<8;i++) {
   4.120 -        UcxDlist *elm = ucx_dlist_get(dl, i);
   4.121 -        if(elm == NULL) {
   4.122 -            fprintf(stderr, "ucx_dlist_get failed: element is NULL\n");
   4.123 -            r--;
   4.124 -        } else if(elm->data == NULL) {
   4.125 -            fprintf(stderr, "ucx_dlist_get failed: data is NULL\n");
   4.126 -            r--;
   4.127 -        } else {
   4.128 -            int *data = (int*)elm->data;
   4.129 -            if(*data != i) {
   4.130 -                fprintf(stderr, "ucx_dlist_get failed with index %d\n", i);
   4.131 -                r--;
   4.132 -            }
   4.133 -        }
   4.134 -    }
   4.135 -
   4.136 -    printf("   Test ucx_dlist_free\n");
   4.137 -    fflush(stdout);
   4.138 -    ucx_dlist_free(dl);
   4.139 -
   4.140 -    dl = NULL;
   4.141 -    // build list 4,0,4
   4.142 -    printf("   Test ucx_dlist_prepend\n");
   4.143 -    dl = ucx_dlist_prepend(dl, &v[0]);
   4.144 -    dl = ucx_dlist_prepend(dl, &v[4]);
   4.145 -    dl = ucx_dlist_append(dl, &v[4]);
   4.146 -
   4.147 -    struct foreach_testdata tdata;
   4.148 -    tdata.i = 0;
   4.149 -    ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata);
   4.150 -
   4.151 -    if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
   4.152 -        fprintf(stderr, "prepend/append test failed\n");
   4.153 -        fprintf(stderr, "content: [%d, %d, %d]\n",
   4.154 -                tdata.values[0], tdata.values[1], tdata.values[2]);
   4.155 -        r--;
   4.156 -    }
   4.157 -
   4.158 -    printf("   Test ucx_dlist_equals\n");
   4.159 -    UcxDlist *dl2 = NULL;
   4.160 -    dl2 = ucx_dlist_append(dl2, &v[4]);
   4.161 -    dl2 = ucx_dlist_append(dl2, &v[0]);
   4.162 -    dl2 = ucx_dlist_append(dl2, &v[4]);
   4.163 -    if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
   4.164 -        fprintf(stderr, "ucx_dlist_equals failed (false negative)\n");
   4.165 -        r--;
   4.166 -    }
   4.167 -    dl2->next->data = NULL;
   4.168 -    if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
   4.169 -        fprintf(stderr, "ucx_dlist_equals failed (false positive)\n");
   4.170 -        r--;
   4.171 -    }
   4.172 -    dl2->next->data = &(tdata.values[1]);
   4.173 -    if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) {
   4.174 -        fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n");
   4.175 -        r--;
   4.176 -    }
   4.177 -    if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
   4.178 -        fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n");
   4.179 -        r--;
   4.180 -    }
   4.181 -    ucx_dlist_free(dl2);
   4.182 -
   4.183 -    printf("   Test ucx_dlist_clone\n");
   4.184 -    dl2 = ucx_dlist_clone(dl, NULL, NULL);
   4.185 -    if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
   4.186 -        fprintf(stderr, "ucx_dlist_clone (without copy) failed\n");
   4.187 -        r--;
   4.188 -    }
   4.189 -    ucx_dlist_free(dl2);
   4.190 -
   4.191 -    printf("   Test ucx_dlist_clone with copy\n");
   4.192 -    dl2 = ucx_dlist_clone(dl, int_cpy, NULL);
   4.193 -    if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
   4.194 -        if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) {
   4.195 -            fprintf(stderr, "ucx_dlist_clone (with copy) failed (compare)\n");
   4.196 -            r--;
   4.197 -        }
   4.198 -    } else {
   4.199 -        fprintf(stderr, "ucx_dlist_clone (with copy) failed (identity)\n");
   4.200 -        r--;
   4.201 -    }
   4.202 -    ucx_dlist_foreach(dl, dlist_free_content, NULL);
   4.203 -    ucx_dlist_free(dl2);
   4.204 -
   4.205 -    ucx_dlist_free(dl);
   4.206 -
   4.207 -    dl = NULL;
   4.208 -    printf("   Test ucx_dlist_remove\n");
   4.209 -    dl = ucx_dlist_append(dl, &v[4]);
   4.210 -    dl = ucx_dlist_append(dl, &v[0]);
   4.211 -    dl = ucx_dlist_append(dl, &v[3]);
   4.212 -    dl = ucx_dlist_remove(dl, dl->next);
   4.213 -    if (ucx_dlist_size(dl) == 2) {
   4.214 -        if ((*((int*)(dl->data)) != 4) || (*((int*)(dl->next->data)) != 3)) {
   4.215 -            fprintf(stderr, "ucx_dlist_remove failed (wrong data)\n");
   4.216 -            r--;
   4.217 -        }
   4.218 -    } else {
   4.219 -        fprintf(stderr, "ucx_dlist_remove failed (wrong size)\n");
   4.220 -        r--;
   4.221 -    }
   4.222 -    dl = ucx_dlist_remove(dl, dl);
   4.223 -    if (ucx_dlist_size(dl) == 1) {
   4.224 -        if ((*((int*)(dl->data)) != 3)) {
   4.225 -            fprintf(stderr, "ucx_dlist_remove first failed (wrong data)\n");
   4.226 -            r--;
   4.227 -        }
   4.228 -    } else {
   4.229 -        fprintf(stderr, "ucx_dlist_remove first failed (wrong size)\n");
   4.230 -        r--;
   4.231 -    }
   4.232 -
   4.233 -    return r;
   4.234 +UCX_TEST_BEGIN(test_ucx_list_size) {
   4.235 +    UcxList *list = ucx_list_append(NULL, "This ");
   4.236 +    list = ucx_list_append(list, "list ");
   4.237 +    list = ucx_list_append(list, "has ");
   4.238 +    list = ucx_list_append(list, "size ");
   4.239 +    list = ucx_list_append(list, "5!");
   4.240 +    
   4.241 +    UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
   4.242 +    
   4.243 +    ucx_list_free(list);
   4.244 +    
   4.245 +    UCX_TEST_END
   4.246  }
   4.247  
   4.248 +UCX_TEST_BEGIN(test_ucx_list_last) {
   4.249 +    UcxList *list = ucx_list_append(NULL, "Find ");
   4.250 +    list = ucx_list_append(list, "the ");
   4.251 +    list = ucx_list_append(list, "last!");
   4.252 +    
   4.253 +    char* last = (char*) (ucx_list_last(list)->data);
   4.254 +    
   4.255 +    UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
   4.256 +    
   4.257 +    ucx_list_free(list);
   4.258 +    
   4.259 +    UCX_TEST_END
   4.260 +}
   4.261 +
   4.262 +UCX_TEST_BEGIN(test_ucx_list_get) {
   4.263 +    UcxList *list = ucx_list_append(NULL, "Find ");
   4.264 +    list = ucx_list_append(list, "the ");
   4.265 +    list = ucx_list_append(list, "mid!");
   4.266 +    
   4.267 +    char* mid = (char*) (ucx_list_get(list, 1)->data);
   4.268 +    
   4.269 +    UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
   4.270 +    
   4.271 +    ucx_list_free(list);
   4.272 +    
   4.273 +    UCX_TEST_END
   4.274 +}
   4.275 +
   4.276 +UCX_TEST_BEGIN(test_ucx_list_remove) {
   4.277 +    UcxList *list = ucx_list_append(NULL, "Hello");
   4.278 +    list = ucx_list_append(list, " fucking");
   4.279 +    list = ucx_list_append(list, " World!");
   4.280 +    
   4.281 +    list = ucx_list_remove(list, ucx_list_get(list, 1));
   4.282 +    
   4.283 +    UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
   4.284 +    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
   4.285 +    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
   4.286 +    
   4.287 +    ucx_list_free(list);
   4.288 +    
   4.289 +    UCX_TEST_END
   4.290 +}
   4.291 +
   4.292 +UCX_TEST_BEGIN(test_ucx_list_clone) {
   4.293 +   
   4.294 +    char *hello = (char*)malloc(6);
   4.295 +    char *world = (char*)malloc(8);
   4.296 +    
   4.297 +    memcpy(hello, "Hello", 6);
   4.298 +    memcpy(world, " World!", 8);
   4.299 +    
   4.300 +    UcxList *list = ucx_list_append(NULL, hello);
   4.301 +    list = ucx_list_append(list, world);
   4.302 +    
   4.303 +    UcxList *copy = ucx_list_clone(list, copy_string, NULL);
   4.304 +
   4.305 +    UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed")
   4.306 +    UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
   4.307 +    UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
   4.308 +
   4.309 +    free(copy->next->data);
   4.310 +    free(copy->data);
   4.311 +
   4.312 +    free(world);
   4.313 +    free(hello);
   4.314 +    free(list);
   4.315 +    free(copy);
   4.316 +    
   4.317 +    UCX_TEST_END
   4.318 +}
     5.1 --- a/test/list_tests.h	Sat Feb 18 15:50:43 2012 +0100
     5.2 +++ b/test/list_tests.h	Sat Feb 18 18:36:30 2012 +0100
     5.3 @@ -8,12 +8,29 @@
     5.4  #ifndef LIST_TESTS_H
     5.5  #define	LIST_TESTS_H
     5.6  
     5.7 +#include "main.h"
     5.8 +
     5.9 +#include "ucx/list.h"
    5.10 +#include "ucx/test.h"
    5.11 +
    5.12  #ifdef	__cplusplus
    5.13  extern "C" {
    5.14  #endif
    5.15  
    5.16 -int dlist_tests();
    5.17 +/*
    5.18 + * Assumed to be correct:
    5.19 + *   ucx_list_free
    5.20 + */
    5.21  
    5.22 +UCX_TEST_DECLARE(test_ucx_list_append)
    5.23 +UCX_TEST_DECLARE(test_ucx_list_prepend)
    5.24 +UCX_TEST_DECLARE(test_ucx_list_equals)
    5.25 +UCX_TEST_DECLARE(test_ucx_list_concat)
    5.26 +UCX_TEST_DECLARE(test_ucx_list_size)
    5.27 +UCX_TEST_DECLARE(test_ucx_list_last)
    5.28 +UCX_TEST_DECLARE(test_ucx_list_get)
    5.29 +UCX_TEST_DECLARE(test_ucx_list_remove)
    5.30 +UCX_TEST_DECLARE(test_ucx_list_clone)
    5.31  
    5.32  #ifdef	__cplusplus
    5.33  }
     6.1 --- a/test/main.c	Sat Feb 18 15:50:43 2012 +0100
     6.2 +++ b/test/main.c	Sat Feb 18 18:36:30 2012 +0100
     6.3 @@ -31,10 +31,26 @@
     6.4  
     6.5  #include "ucx/test.h"
     6.6  
     6.7 +#include "main.h"
     6.8 +
     6.9  #include "list_tests.h"
    6.10 +#include "dlist_tests.h"
    6.11 +
    6.12  #include "mpool_tests.h"
    6.13  #include "map_tests.h"
    6.14  
    6.15 +int cmp_string(void* o1, void* o2, void* data) {
    6.16 +    return strcmp((char*)o1, (char*)o2);
    6.17 +}
    6.18 +
    6.19 +void* copy_string(void* e, void* data) {
    6.20 +    char *str = (char*) e;
    6.21 +    size_t n = 1+strlen(str);
    6.22 +    char *cpy = (char*) malloc(n);
    6.23 +    memcpy(cpy, str, n);
    6.24 +    return cpy;
    6.25 +}
    6.26 +
    6.27  UCX_TEST_BEGIN(testTestSuitePositive) {
    6.28      UCX_TEST_ASSERT(2*2 == 4, "the test framework fails")
    6.29      UCX_TEST_END
    6.30 @@ -48,22 +64,39 @@
    6.31  int main(int argc, char **argv) {
    6.32      printf("UCX Tests\n---------\n");
    6.33  
    6.34 -    printf("\nUcxTestSuite Tests\n");
    6.35 +    printf("\nUcxTestSuite Tests (1 failure is intended!)\n");
    6.36      UcxTestSuite* suite = ucx_test_suite_new();
    6.37      ucx_test_register(suite, testTestSuitePositive);
    6.38      ucx_test_register(suite, testTestSuiteNegative);
    6.39      ucx_test_run(suite, stdout);
    6.40      if (suite->failure == 1 && suite->success == 1) {
    6.41          ucx_test_suite_free(suite);
    6.42 -    
    6.43 +
    6.44 +        suite = ucx_test_suite_new();
    6.45 +        /* UcxList Tests */
    6.46 +        ucx_test_register(suite, test_ucx_list_append);
    6.47 +        ucx_test_register(suite, test_ucx_list_prepend);
    6.48 +        ucx_test_register(suite, test_ucx_list_equals);
    6.49 +        ucx_test_register(suite, test_ucx_list_concat);
    6.50 +        ucx_test_register(suite, test_ucx_list_size);
    6.51 +        ucx_test_register(suite, test_ucx_list_last);
    6.52 +        ucx_test_register(suite, test_ucx_list_get);
    6.53 +        ucx_test_register(suite, test_ucx_list_remove);
    6.54 +        ucx_test_register(suite, test_ucx_list_clone);
    6.55 +        
    6.56 +        /* UcxDlist Tests */
    6.57 +        ucx_test_register(suite, test_ucx_dlist_append);
    6.58 +        ucx_test_register(suite, test_ucx_dlist_prepend);
    6.59 +        ucx_test_register(suite, test_ucx_dlist_equals);
    6.60 +        ucx_test_register(suite, test_ucx_dlist_concat);
    6.61 +        ucx_test_register(suite, test_ucx_dlist_size);
    6.62 +        ucx_test_register(suite, test_ucx_dlist_first);
    6.63 +        ucx_test_register(suite, test_ucx_dlist_last);
    6.64 +        ucx_test_register(suite, test_ucx_dlist_get);
    6.65 +        ucx_test_register(suite, test_ucx_dlist_remove);
    6.66 +        ucx_test_register(suite, test_ucx_dlist_clone);
    6.67 +
    6.68          /* TODO: replace these tests with "real" tests */
    6.69 -        printf("\nUcxDlist Tests\n");
    6.70 -        if(dlist_tests()) {
    6.71 -            fprintf(stderr, "dlist_tests failed\n");
    6.72 -        }
    6.73 -
    6.74 -        printf("\nUcxList Tests\n   Assumed to be correct\n");
    6.75 -
    6.76          printf("\nUcxMemPool Tests\n");
    6.77          if(mpool_tests()) {
    6.78              fprintf(stderr, "mpool_tests failed\n");
    6.79 @@ -74,6 +107,9 @@
    6.80              fprintf(stderr, "map_tests failed\n");
    6.81          }
    6.82          
    6.83 +        ucx_test_run(suite, stdout);
    6.84 +        ucx_test_suite_free(suite);
    6.85 +        
    6.86          return EXIT_SUCCESS;
    6.87      } else {
    6.88          ucx_test_suite_free(suite);
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/test/main.h	Sat Feb 18 18:36:30 2012 +0100
     7.3 @@ -0,0 +1,25 @@
     7.4 +/* 
     7.5 + * File:   main.h
     7.6 + * Author: Mike
     7.7 + *
     7.8 + * Created on 18. Februar 2012, 18:33
     7.9 + */
    7.10 +
    7.11 +#ifndef MAIN_H
    7.12 +#define	MAIN_H
    7.13 +
    7.14 +#ifdef	__cplusplus
    7.15 +extern "C" {
    7.16 +#endif
    7.17 +
    7.18 +/* Some functions that are shared over the test */
    7.19 +int cmp_string(void*, void*, void*);
    7.20 +void* copy_string(void*, void*);
    7.21 +
    7.22 +
    7.23 +#ifdef	__cplusplus
    7.24 +}
    7.25 +#endif
    7.26 +
    7.27 +#endif	/* MAIN_H */
    7.28 +
     8.1 --- a/ucx/dlist.c	Sat Feb 18 15:50:43 2012 +0100
     8.2 +++ b/ucx/dlist.c	Sat Feb 18 18:36:30 2012 +0100
     8.3 @@ -112,16 +112,6 @@
     8.4      return s;
     8.5  }
     8.6  
     8.7 -void ucx_dlist_foreach(UcxDlist *l, ucx_callback fnc, void* data) {
     8.8 -    UcxDlist *e = l;
     8.9 -    UcxDlist *n;
    8.10 -    while (e != NULL) {
    8.11 -        n = e->next;
    8.12 -        fnc(e, data);
    8.13 -        e = n;
    8.14 -    }
    8.15 -}
    8.16 -
    8.17  /* dlist specific functions */
    8.18  UcxDlist *ucx_dlist_first(UcxDlist *l) {
    8.19      if (l == NULL) return NULL;
     9.1 --- a/ucx/dlist.h	Sat Feb 18 15:50:43 2012 +0100
     9.2 +++ b/ucx/dlist.h	Sat Feb 18 18:36:30 2012 +0100
     9.3 @@ -29,7 +29,6 @@
     9.4  UcxDlist *ucx_dlist_last(UcxDlist *l);
     9.5  UcxDlist *ucx_dlist_get(UcxDlist *l, int index);
     9.6  size_t ucx_dlist_size(UcxDlist *l);
     9.7 -void ucx_dlist_foreach(UcxDlist *l, ucx_callback fnc, void* data);
     9.8  
     9.9  /* dlist specific functions */
    9.10  UcxDlist *ucx_dlist_first(UcxDlist *l);
    10.1 --- a/ucx/list.c	Sat Feb 18 15:50:43 2012 +0100
    10.2 +++ b/ucx/list.c	Sat Feb 18 18:36:30 2012 +0100
    10.3 @@ -108,16 +108,6 @@
    10.4      return s;
    10.5  }
    10.6  
    10.7 -void ucx_list_foreach(UcxList *l, ucx_callback fnc, void* data) {
    10.8 -    UcxList *e = l;
    10.9 -    UcxList *n;
   10.10 -    while (e != NULL) {
   10.11 -        n = e->next;
   10.12 -        fnc(e, data);
   10.13 -        e = n;
   10.14 -    }
   10.15 -}
   10.16 -
   10.17  /* list specific functions */
   10.18  UcxList *ucx_list_remove(UcxList *l, UcxList *e) {
   10.19      if (e == l) {
    11.1 --- a/ucx/list.h	Sat Feb 18 15:50:43 2012 +0100
    11.2 +++ b/ucx/list.h	Sat Feb 18 18:36:30 2012 +0100
    11.3 @@ -28,7 +28,6 @@
    11.4  UcxList *ucx_list_last(UcxList *l);
    11.5  UcxList *ucx_list_get(UcxList *l, int index);
    11.6  size_t ucx_list_size(UcxList *l);
    11.7 -void ucx_list_foreach(UcxList *l, ucx_callback fnc, void *data);
    11.8  
    11.9  /* list specific functions */
   11.10  UcxList *ucx_list_remove(UcxList *l, UcxList *e);
    12.1 --- a/ucx/test.h	Sat Feb 18 15:50:43 2012 +0100
    12.2 +++ b/ucx/test.h	Sat Feb 18 18:36:30 2012 +0100
    12.3 @@ -30,6 +30,7 @@
    12.4  void ucx_test_register(UcxTestSuite*, UcxTest);
    12.5  void ucx_test_run(UcxTestSuite*, FILE*);
    12.6  
    12.7 +#define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *);
    12.8  #define UCX_TEST_BEGIN(name) void name(UcxTestSuite* _suite_,FILE *_output_) {\
    12.9      fwrite("Running "#name"... ", 1, 12+strlen(#name), _output_);
   12.10  

mercurial