test/mpool_tests.c

changeset 28
1666cbeb1db8
parent 19
cdd7a3173249
child 30
23bb65cbf7a4
     1.1 --- a/test/mpool_tests.c	Sat Feb 18 18:36:30 2012 +0100
     1.2 +++ b/test/mpool_tests.c	Mon Feb 20 15:30:45 2012 +0100
     1.3 @@ -2,82 +2,169 @@
     1.4   *
     1.5   */
     1.6  
     1.7 -#include <stdio.h>
     1.8 -#include <stdlib.h>
     1.9 -#include <string.h>
    1.10 -
    1.11  #include "mpool_tests.h"
    1.12  
    1.13 -int cmp_value = 2;
    1.14 -
    1.15 -void int_destructor(int *ptr) {
    1.16 -    if(*ptr != cmp_value) {
    1.17 -        fprintf(stderr, "ucx_mempool_free failed: wrong order\n");
    1.18 -        return;
    1.19 -    }
    1.20 -    cmp_value += 2;
    1.21 +UCX_TEST_BEGIN(test_ucx_mempool_new) {
    1.22 +    UcxMempool *pool = ucx_mempool_new(16);
    1.23 +    
    1.24 +    UCX_TEST_ASSERT(pool->size == 16, "wrong size")
    1.25 +    UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter")
    1.26 +    UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed")
    1.27 +    
    1.28 +    ucx_mempool_free(pool);
    1.29 +    
    1.30 +    UCX_TEST_END
    1.31  }
    1.32  
    1.33 -void hello_destructor(char *str) {
    1.34 -    if(strcmp(str, "Hello World!") != 0) {
    1.35 -        fprintf(stderr, "ucx_mempool_reg_destr failed\n");
    1.36 -    }
    1.37 +UCX_TEST_BEGIN(test_ucx_mempool_malloc) {
    1.38 +    
    1.39 +    UcxMempool *pool = ucx_mempool_new(1);
    1.40 +    
    1.41 +    int *test = (int*) ucx_mempool_malloc(pool, sizeof(int));
    1.42 +    
    1.43 +    UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented")
    1.44 +    UCX_TEST_ASSERT(pool->size == 1, "chcap called")
    1.45 +    
    1.46 +    int *pooladdr = (int*)((char*)pool->data[0] + sizeof(ucx_destructor));
    1.47 +    *pooladdr = 5;
    1.48 +    
    1.49 +    UCX_TEST_ASSERT(*test == 5, "wrong pointer")
    1.50 +    
    1.51 +    ucx_mempool_free(pool);
    1.52 +    
    1.53 +    UCX_TEST_END
    1.54  }
    1.55  
    1.56 -int mpool_tests() {
    1.57 -	int r = 0;
    1.58 +UCX_TEST_BEGIN(test_ucx_mempool_malloc_with_chcap) {
    1.59 +    
    1.60 +    UcxMempool *pool = ucx_mempool_new(1);
    1.61 +    
    1.62 +    ucx_mempool_malloc(pool, sizeof(int));
    1.63 +    int *test = (int*) ucx_mempool_malloc(pool, sizeof(int));
    1.64 +    
    1.65 +    UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented")
    1.66 +    UCX_TEST_ASSERT(pool->size == 17, "chcap not called")
    1.67 +    
    1.68 +    int *pooladdr = (int*)((char*)pool->data[1] + sizeof(ucx_destructor));
    1.69 +    *pooladdr = 5;
    1.70 +    
    1.71 +    UCX_TEST_ASSERT(*test == 5, "wrong pointer")
    1.72 +    
    1.73 +    ucx_mempool_free(pool);
    1.74 +    
    1.75 +    UCX_TEST_END
    1.76 +}
    1.77  
    1.78 -    printf("   Test ucx_mempool_new\n");
    1.79 -    UcxMempool *pool = ucx_mempool_new(16);
    1.80 +UCX_TEST_BEGIN(test_ucx_mempool_calloc) {
    1.81 +    
    1.82 +    UcxMempool *pool = ucx_mempool_new(1);
    1.83 +    
    1.84 +    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
    1.85 +    
    1.86 +    UCX_TEST_ASSERT(test != NULL, "no memory for test data")
    1.87 +    UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed")
    1.88 +    
    1.89 +    ucx_mempool_free(pool);
    1.90 +    
    1.91 +    UCX_TEST_END
    1.92 +}
    1.93  
    1.94 -    printf("   Test ucx_mempool_malloc\n");
    1.95 -    int *ptr1 = (int*)ucx_mempool_malloc(pool, sizeof(int));
    1.96 -    for(int i=0;i<256;i++) {
    1.97 -        ucx_mempool_malloc(pool, i+1);
    1.98 -    }
    1.99 -    int *ptr2 = (int*)ucx_mempool_malloc(pool, sizeof(int));
   1.100 -    int *ptr3 = (int*)ucx_mempool_malloc(pool, sizeof(int));
   1.101 -    for(int i=0;i<256;i++) {
   1.102 -        ucx_mempool_malloc(pool, i+1);
   1.103 -    }
   1.104 -    int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int));
   1.105 +void test_setdestr(void* elem) {
   1.106 +    int *cb = (int*) ((int*) elem)[1];
   1.107 +    *cb = 42;
   1.108 +}
   1.109  
   1.110 -    *ptr1 = 2;
   1.111 -    *ptr2 = 4;
   1.112 -    *ptr3 = 6;
   1.113 -    *ptr4 = 8;
   1.114 +UCX_TEST_BEGIN(test_ucx_mempool_set_destr) {
   1.115 +    
   1.116 +    UcxMempool *pool = ucx_mempool_new(2);
   1.117 +    
   1.118 +    ucx_mempool_malloc(pool, sizeof(int));
   1.119 +    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
   1.120 +    
   1.121 +    int *cb = (int*) malloc(sizeof(int));
   1.122 +    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
   1.123 +    
   1.124 +    test[0] = 5; test[1] = (int) cb;
   1.125 +    *cb = 13;
   1.126 +    
   1.127 +    ucx_mempool_set_destr(test, test_setdestr);
   1.128 +    
   1.129 +    UCX_TEST_ASSERT(
   1.130 +            *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
   1.131 +    UCX_TEST_ASSERT(
   1.132 +            test[0] == 5 && test[1] == (int) cb, "setdestr destroyed data")
   1.133 +    
   1.134 +    ucx_mempool_free(pool);
   1.135 +    
   1.136 +    UCX_TEST_ASSERT(*cb == 42, "destructor not called")
   1.137  
   1.138 -    printf("   Test ucx_mempool_set_destr\n");
   1.139 -    ucx_mempool_set_destr(ptr1, (ucx_destructor)int_destructor);
   1.140 -    ucx_mempool_set_destr(ptr2, (ucx_destructor)int_destructor);
   1.141 -    ucx_mempool_set_destr(ptr3, (ucx_destructor)int_destructor);
   1.142 -    ucx_mempool_set_destr(ptr4, (ucx_destructor)int_destructor);
   1.143 +    free(cb);
   1.144 +    
   1.145 +    UCX_TEST_END
   1.146 +}
   1.147  
   1.148 -    printf("   Test ucx_mempool_calloc\n");
   1.149 -    char *str = ucx_mempool_calloc(pool, 1, 3);
   1.150 -    if(str[0] != 0 || str[1] != 0 || str[2] != 0) {
   1.151 -        fprintf(stderr, "ucx_mempool_calloc failed\n");
   1.152 -        r--;
   1.153 -    }
   1.154 -    str[0] = 'O';
   1.155 -    str[1] = 'K';
   1.156  
   1.157 -    printf("   Test ucx_mempool_realloc\n");
   1.158 -    str = ucx_mempool_realloc(pool, str, 4);
   1.159 -    str[2] = '!';
   1.160 -    str[3] = 0;
   1.161 -    if(strcmp(str, "OK!") != 0) {
   1.162 -        fprintf(stderr, "Test ucx_mempool_realloc failed!\n");
   1.163 -        r--;
   1.164 -    }
   1.165 +UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) {
   1.166      
   1.167 -    printf("   Test ucx_mempool_reg_destr\n");
   1.168 -    char *hello = "Hello World!";
   1.169 -    ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor);
   1.170 +    UcxMempool *pool = ucx_mempool_new(1);
   1.171 +    
   1.172 +    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
   1.173 +    
   1.174 +    int *cb = (int*) malloc(sizeof(int));
   1.175 +    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
   1.176 +    
   1.177 +    test[0] = 5; test[1] = (int) cb;
   1.178 +    *cb = 13;
   1.179 +    
   1.180 +    ucx_mempool_reg_destr(pool, test, test_setdestr);
   1.181 +    
   1.182 +    ucx_destructor *pooladdr = (ucx_destructor*)
   1.183 +            ((char*)pool->data[1] + sizeof(ucx_destructor));
   1.184 +    
   1.185 +    UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed")
   1.186 +    
   1.187 +    ucx_mempool_free(pool);
   1.188 +    
   1.189 +    UCX_TEST_ASSERT(*cb == 42, "destructor not called")
   1.190  
   1.191 -    printf("   Test ucx_mempool_free\n");
   1.192 -    //ucx_mempool_free(pool);
   1.193 +    free(cb);
   1.194      
   1.195 +    UCX_TEST_END
   1.196 +}
   1.197  
   1.198 -    return r;
   1.199 +UCX_TEST_BEGIN(test_ucx_mempool_realloc) {
   1.200 +    
   1.201 +    UcxMempool *pool = ucx_mempool_new(2);
   1.202 +    
   1.203 +    ucx_mempool_malloc(pool, sizeof(int));
   1.204 +    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
   1.205 +    
   1.206 +    int *cb = (int*) malloc(sizeof(int));
   1.207 +    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
   1.208 +    
   1.209 +    test[0] = 5; test[1] = (int) cb;
   1.210 +    *cb = 13;
   1.211 +    
   1.212 +    ucx_mempool_set_destr(test, test_setdestr);
   1.213 +    
   1.214 +    int *rtest, n = 2;
   1.215 +    do {
   1.216 +        n *= 2;
   1.217 +        UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc")
   1.218 +        rtest = ucx_mempool_realloc(pool, test, n*sizeof(int));
   1.219 +    } while (rtest == test);
   1.220 +    test = rtest;
   1.221 +    
   1.222 +    UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
   1.223 +            "realloc killed destructor")
   1.224 +    UCX_TEST_ASSERT(
   1.225 +            test[0] == 5 && test[1] == (int) cb, "realloc destroyed data")
   1.226 +    
   1.227 +    ucx_mempool_free(pool);
   1.228 +    
   1.229 +    UCX_TEST_ASSERT(*cb == 42, "destructor not called")
   1.230 +
   1.231 +    free(cb);
   1.232 +    
   1.233 +    UCX_TEST_END
   1.234  }

mercurial