new mempool tests

Mon, 20 Feb 2012 15:30:45 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 20 Feb 2012 15:30:45 +0100
changeset 28
1666cbeb1db8
parent 27
22644e2572bc
child 29
bce0d7f2912b

new mempool tests

test/main.c file | annotate | diff | comparison | revisions
test/mpool_tests.c file | annotate | diff | comparison | revisions
test/mpool_tests.h file | annotate | diff | comparison | revisions
ucx/mempool.c file | annotate | diff | comparison | revisions
     1.1 --- a/test/main.c	Sat Feb 18 18:36:30 2012 +0100
     1.2 +++ b/test/main.c	Mon Feb 20 15:30:45 2012 +0100
     1.3 @@ -64,7 +64,7 @@
     1.4  int main(int argc, char **argv) {
     1.5      printf("UCX Tests\n---------\n");
     1.6  
     1.7 -    printf("\nUcxTestSuite Tests (1 failure is intended!)\n");
     1.8 +    printf("\nUcxTestSuite tests (1 failure is intended!)\n");
     1.9      UcxTestSuite* suite = ucx_test_suite_new();
    1.10      ucx_test_register(suite, testTestSuitePositive);
    1.11      ucx_test_register(suite, testTestSuiteNegative);
    1.12 @@ -72,6 +72,7 @@
    1.13      if (suite->failure == 1 && suite->success == 1) {
    1.14          ucx_test_suite_free(suite);
    1.15  
    1.16 +        printf("\nLibrary function tests\n");
    1.17          suite = ucx_test_suite_new();
    1.18          /* UcxList Tests */
    1.19          ucx_test_register(suite, test_ucx_list_append);
    1.20 @@ -96,11 +97,14 @@
    1.21          ucx_test_register(suite, test_ucx_dlist_remove);
    1.22          ucx_test_register(suite, test_ucx_dlist_clone);
    1.23  
    1.24 -        /* TODO: replace these tests with "real" tests */
    1.25 -        printf("\nUcxMemPool Tests\n");
    1.26 -        if(mpool_tests()) {
    1.27 -            fprintf(stderr, "mpool_tests failed\n");
    1.28 -        }
    1.29 +        /* UcxMemPool Tests */
    1.30 +        ucx_test_register(suite, test_ucx_mempool_new);
    1.31 +        ucx_test_register(suite, test_ucx_mempool_malloc);
    1.32 +        ucx_test_register(suite, test_ucx_mempool_malloc_with_chcap);
    1.33 +        ucx_test_register(suite, test_ucx_mempool_calloc);
    1.34 +        ucx_test_register(suite, test_ucx_mempool_set_destr);
    1.35 +        ucx_test_register(suite, test_ucx_mempool_reg_destr);
    1.36 +        ucx_test_register(suite, test_ucx_mempool_realloc);
    1.37  
    1.38          printf("\nUcxMap Tests\n");
    1.39          if(map_tests()) {
     2.1 --- a/test/mpool_tests.c	Sat Feb 18 18:36:30 2012 +0100
     2.2 +++ b/test/mpool_tests.c	Mon Feb 20 15:30:45 2012 +0100
     2.3 @@ -2,82 +2,169 @@
     2.4   *
     2.5   */
     2.6  
     2.7 -#include <stdio.h>
     2.8 -#include <stdlib.h>
     2.9 -#include <string.h>
    2.10 -
    2.11  #include "mpool_tests.h"
    2.12  
    2.13 -int cmp_value = 2;
    2.14 -
    2.15 -void int_destructor(int *ptr) {
    2.16 -    if(*ptr != cmp_value) {
    2.17 -        fprintf(stderr, "ucx_mempool_free failed: wrong order\n");
    2.18 -        return;
    2.19 -    }
    2.20 -    cmp_value += 2;
    2.21 +UCX_TEST_BEGIN(test_ucx_mempool_new) {
    2.22 +    UcxMempool *pool = ucx_mempool_new(16);
    2.23 +    
    2.24 +    UCX_TEST_ASSERT(pool->size == 16, "wrong size")
    2.25 +    UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter")
    2.26 +    UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed")
    2.27 +    
    2.28 +    ucx_mempool_free(pool);
    2.29 +    
    2.30 +    UCX_TEST_END
    2.31  }
    2.32  
    2.33 -void hello_destructor(char *str) {
    2.34 -    if(strcmp(str, "Hello World!") != 0) {
    2.35 -        fprintf(stderr, "ucx_mempool_reg_destr failed\n");
    2.36 -    }
    2.37 +UCX_TEST_BEGIN(test_ucx_mempool_malloc) {
    2.38 +    
    2.39 +    UcxMempool *pool = ucx_mempool_new(1);
    2.40 +    
    2.41 +    int *test = (int*) ucx_mempool_malloc(pool, sizeof(int));
    2.42 +    
    2.43 +    UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented")
    2.44 +    UCX_TEST_ASSERT(pool->size == 1, "chcap called")
    2.45 +    
    2.46 +    int *pooladdr = (int*)((char*)pool->data[0] + sizeof(ucx_destructor));
    2.47 +    *pooladdr = 5;
    2.48 +    
    2.49 +    UCX_TEST_ASSERT(*test == 5, "wrong pointer")
    2.50 +    
    2.51 +    ucx_mempool_free(pool);
    2.52 +    
    2.53 +    UCX_TEST_END
    2.54  }
    2.55  
    2.56 -int mpool_tests() {
    2.57 -	int r = 0;
    2.58 +UCX_TEST_BEGIN(test_ucx_mempool_malloc_with_chcap) {
    2.59 +    
    2.60 +    UcxMempool *pool = ucx_mempool_new(1);
    2.61 +    
    2.62 +    ucx_mempool_malloc(pool, sizeof(int));
    2.63 +    int *test = (int*) ucx_mempool_malloc(pool, sizeof(int));
    2.64 +    
    2.65 +    UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented")
    2.66 +    UCX_TEST_ASSERT(pool->size == 17, "chcap not called")
    2.67 +    
    2.68 +    int *pooladdr = (int*)((char*)pool->data[1] + sizeof(ucx_destructor));
    2.69 +    *pooladdr = 5;
    2.70 +    
    2.71 +    UCX_TEST_ASSERT(*test == 5, "wrong pointer")
    2.72 +    
    2.73 +    ucx_mempool_free(pool);
    2.74 +    
    2.75 +    UCX_TEST_END
    2.76 +}
    2.77  
    2.78 -    printf("   Test ucx_mempool_new\n");
    2.79 -    UcxMempool *pool = ucx_mempool_new(16);
    2.80 +UCX_TEST_BEGIN(test_ucx_mempool_calloc) {
    2.81 +    
    2.82 +    UcxMempool *pool = ucx_mempool_new(1);
    2.83 +    
    2.84 +    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
    2.85 +    
    2.86 +    UCX_TEST_ASSERT(test != NULL, "no memory for test data")
    2.87 +    UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed")
    2.88 +    
    2.89 +    ucx_mempool_free(pool);
    2.90 +    
    2.91 +    UCX_TEST_END
    2.92 +}
    2.93  
    2.94 -    printf("   Test ucx_mempool_malloc\n");
    2.95 -    int *ptr1 = (int*)ucx_mempool_malloc(pool, sizeof(int));
    2.96 -    for(int i=0;i<256;i++) {
    2.97 -        ucx_mempool_malloc(pool, i+1);
    2.98 -    }
    2.99 -    int *ptr2 = (int*)ucx_mempool_malloc(pool, sizeof(int));
   2.100 -    int *ptr3 = (int*)ucx_mempool_malloc(pool, sizeof(int));
   2.101 -    for(int i=0;i<256;i++) {
   2.102 -        ucx_mempool_malloc(pool, i+1);
   2.103 -    }
   2.104 -    int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int));
   2.105 +void test_setdestr(void* elem) {
   2.106 +    int *cb = (int*) ((int*) elem)[1];
   2.107 +    *cb = 42;
   2.108 +}
   2.109  
   2.110 -    *ptr1 = 2;
   2.111 -    *ptr2 = 4;
   2.112 -    *ptr3 = 6;
   2.113 -    *ptr4 = 8;
   2.114 +UCX_TEST_BEGIN(test_ucx_mempool_set_destr) {
   2.115 +    
   2.116 +    UcxMempool *pool = ucx_mempool_new(2);
   2.117 +    
   2.118 +    ucx_mempool_malloc(pool, sizeof(int));
   2.119 +    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
   2.120 +    
   2.121 +    int *cb = (int*) malloc(sizeof(int));
   2.122 +    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
   2.123 +    
   2.124 +    test[0] = 5; test[1] = (int) cb;
   2.125 +    *cb = 13;
   2.126 +    
   2.127 +    ucx_mempool_set_destr(test, test_setdestr);
   2.128 +    
   2.129 +    UCX_TEST_ASSERT(
   2.130 +            *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
   2.131 +    UCX_TEST_ASSERT(
   2.132 +            test[0] == 5 && test[1] == (int) cb, "setdestr destroyed data")
   2.133 +    
   2.134 +    ucx_mempool_free(pool);
   2.135 +    
   2.136 +    UCX_TEST_ASSERT(*cb == 42, "destructor not called")
   2.137  
   2.138 -    printf("   Test ucx_mempool_set_destr\n");
   2.139 -    ucx_mempool_set_destr(ptr1, (ucx_destructor)int_destructor);
   2.140 -    ucx_mempool_set_destr(ptr2, (ucx_destructor)int_destructor);
   2.141 -    ucx_mempool_set_destr(ptr3, (ucx_destructor)int_destructor);
   2.142 -    ucx_mempool_set_destr(ptr4, (ucx_destructor)int_destructor);
   2.143 +    free(cb);
   2.144 +    
   2.145 +    UCX_TEST_END
   2.146 +}
   2.147  
   2.148 -    printf("   Test ucx_mempool_calloc\n");
   2.149 -    char *str = ucx_mempool_calloc(pool, 1, 3);
   2.150 -    if(str[0] != 0 || str[1] != 0 || str[2] != 0) {
   2.151 -        fprintf(stderr, "ucx_mempool_calloc failed\n");
   2.152 -        r--;
   2.153 -    }
   2.154 -    str[0] = 'O';
   2.155 -    str[1] = 'K';
   2.156  
   2.157 -    printf("   Test ucx_mempool_realloc\n");
   2.158 -    str = ucx_mempool_realloc(pool, str, 4);
   2.159 -    str[2] = '!';
   2.160 -    str[3] = 0;
   2.161 -    if(strcmp(str, "OK!") != 0) {
   2.162 -        fprintf(stderr, "Test ucx_mempool_realloc failed!\n");
   2.163 -        r--;
   2.164 -    }
   2.165 +UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) {
   2.166      
   2.167 -    printf("   Test ucx_mempool_reg_destr\n");
   2.168 -    char *hello = "Hello World!";
   2.169 -    ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor);
   2.170 +    UcxMempool *pool = ucx_mempool_new(1);
   2.171 +    
   2.172 +    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
   2.173 +    
   2.174 +    int *cb = (int*) malloc(sizeof(int));
   2.175 +    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
   2.176 +    
   2.177 +    test[0] = 5; test[1] = (int) cb;
   2.178 +    *cb = 13;
   2.179 +    
   2.180 +    ucx_mempool_reg_destr(pool, test, test_setdestr);
   2.181 +    
   2.182 +    ucx_destructor *pooladdr = (ucx_destructor*)
   2.183 +            ((char*)pool->data[1] + sizeof(ucx_destructor));
   2.184 +    
   2.185 +    UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed")
   2.186 +    
   2.187 +    ucx_mempool_free(pool);
   2.188 +    
   2.189 +    UCX_TEST_ASSERT(*cb == 42, "destructor not called")
   2.190  
   2.191 -    printf("   Test ucx_mempool_free\n");
   2.192 -    //ucx_mempool_free(pool);
   2.193 +    free(cb);
   2.194      
   2.195 +    UCX_TEST_END
   2.196 +}
   2.197  
   2.198 -    return r;
   2.199 +UCX_TEST_BEGIN(test_ucx_mempool_realloc) {
   2.200 +    
   2.201 +    UcxMempool *pool = ucx_mempool_new(2);
   2.202 +    
   2.203 +    ucx_mempool_malloc(pool, sizeof(int));
   2.204 +    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
   2.205 +    
   2.206 +    int *cb = (int*) malloc(sizeof(int));
   2.207 +    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
   2.208 +    
   2.209 +    test[0] = 5; test[1] = (int) cb;
   2.210 +    *cb = 13;
   2.211 +    
   2.212 +    ucx_mempool_set_destr(test, test_setdestr);
   2.213 +    
   2.214 +    int *rtest, n = 2;
   2.215 +    do {
   2.216 +        n *= 2;
   2.217 +        UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc")
   2.218 +        rtest = ucx_mempool_realloc(pool, test, n*sizeof(int));
   2.219 +    } while (rtest == test);
   2.220 +    test = rtest;
   2.221 +    
   2.222 +    UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
   2.223 +            "realloc killed destructor")
   2.224 +    UCX_TEST_ASSERT(
   2.225 +            test[0] == 5 && test[1] == (int) cb, "realloc destroyed data")
   2.226 +    
   2.227 +    ucx_mempool_free(pool);
   2.228 +    
   2.229 +    UCX_TEST_ASSERT(*cb == 42, "destructor not called")
   2.230 +
   2.231 +    free(cb);
   2.232 +    
   2.233 +    UCX_TEST_END
   2.234  }
     3.1 --- a/test/mpool_tests.h	Sat Feb 18 18:36:30 2012 +0100
     3.2 +++ b/test/mpool_tests.h	Mon Feb 20 15:30:45 2012 +0100
     3.3 @@ -5,15 +5,20 @@
     3.4  #ifndef MPOOL_TESTS_H
     3.5  #define	MPOOL_TESTS_H
     3.6  
     3.7 +#include "ucx/test.h"
     3.8  #include "ucx/mempool.h"
     3.9  
    3.10  #ifdef	__cplusplus
    3.11  extern "C" {
    3.12  #endif
    3.13  
    3.14 -int mpool_tests();
    3.15 -
    3.16 -
    3.17 +UCX_TEST_DECLARE(test_ucx_mempool_new)
    3.18 +UCX_TEST_DECLARE(test_ucx_mempool_malloc)
    3.19 +UCX_TEST_DECLARE(test_ucx_mempool_malloc_with_chcap)
    3.20 +UCX_TEST_DECLARE(test_ucx_mempool_calloc)
    3.21 +UCX_TEST_DECLARE(test_ucx_mempool_set_destr)
    3.22 +UCX_TEST_DECLARE(test_ucx_mempool_reg_destr)
    3.23 +UCX_TEST_DECLARE(test_ucx_mempool_realloc)
    3.24  
    3.25  #ifdef	__cplusplus
    3.26  }
     4.1 --- a/ucx/mempool.c	Sat Feb 18 18:36:30 2012 +0100
     4.2 +++ b/ucx/mempool.c	Mon Feb 20 15:30:45 2012 +0100
     4.3 @@ -75,20 +75,20 @@
     4.4  }
     4.5  
     4.6  void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
     4.7 -    void *mem = ((char*)ptr) - sizeof(ucx_destructor);
     4.8 +    char *mem = ((char*)ptr) - sizeof(ucx_destructor);
     4.9      char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
    4.10      if (newm == NULL) return NULL;
    4.11      if (mem != newm) {
    4.12          for(int i=0;i<pool->ndata;i++) {
    4.13              if(pool->data[i] == mem) {
    4.14                  pool->data[i] = newm;
    4.15 -                return ((char*) newm) + sizeof(ucx_destructor);
    4.16 +                return newm + sizeof(ucx_destructor);
    4.17              }
    4.18          }
    4.19 -        fprintf(stderr, "FATAL: %8x not in mpool %8x\n", mem, pool);
    4.20 +        fprintf(stderr, "FATAL: %8x not in mpool %8x\n", ptr, pool);
    4.21          exit(1);
    4.22      } else {
    4.23 -        return ((char*) newm) + sizeof(ucx_destructor);
    4.24 +        return newm + sizeof(ucx_destructor);
    4.25      }
    4.26  }
    4.27  

mercurial