1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/mpool_tests.c Sat Dec 31 21:05:59 2011 +0100 1.3 @@ -0,0 +1,79 @@ 1.4 +/* 1.5 + * 1.6 + */ 1.7 + 1.8 +#include <stdio.h> 1.9 +#include <stdlib.h> 1.10 +#include <string.h> 1.11 + 1.12 +#include "mpool_tests.h" 1.13 + 1.14 +int cmp_value = 2; 1.15 + 1.16 +void int_destructor(int *ptr) { 1.17 + if(*ptr != cmp_value) { 1.18 + fprintf(stderr, "ucx_mempool_free failed: wrong order\n"); 1.19 + return; 1.20 + } 1.21 + cmp_value += 2; 1.22 +} 1.23 + 1.24 +void hello_destructor(char *str) { 1.25 + if(strcmp(str, "Hello World!") != 0) { 1.26 + fprintf(stderr, "ucx_mempool_reg_destr failed\n"); 1.27 + } 1.28 +} 1.29 + 1.30 +int mpool_tests() { 1.31 + printf(" Test ucx_mempool_new\n"); 1.32 + UcxMempool *pool = ucx_mempool_new(16); 1.33 + 1.34 + printf(" Test ucx_mempool_malloc\n"); 1.35 + int *ptr1 = (int*)ucx_mempool_malloc(pool, sizeof(int)); 1.36 + for(int i=0;i<256;i++) { 1.37 + ucx_mempool_malloc(pool, i); 1.38 + } 1.39 + int *ptr2 = (int*)ucx_mempool_malloc(pool, sizeof(int)); 1.40 + int *ptr3 = (int*)ucx_mempool_malloc(pool, sizeof(int)); 1.41 + for(int i=0;i<256;i++) { 1.42 + ucx_mempool_malloc(pool, i); 1.43 + } 1.44 + int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int)); 1.45 + 1.46 + *ptr1 = 2; 1.47 + *ptr2 = 4; 1.48 + *ptr3 = 6; 1.49 + *ptr4 = 8; 1.50 + 1.51 + printf(" Test ucx_mempool_set_destr\n"); 1.52 + ucx_mempool_set_destr(ptr1, (ucx_destructor)int_destructor); 1.53 + ucx_mempool_set_destr(ptr2, (ucx_destructor)int_destructor); 1.54 + ucx_mempool_set_destr(ptr3, (ucx_destructor)int_destructor); 1.55 + ucx_mempool_set_destr(ptr4, (ucx_destructor)int_destructor); 1.56 + 1.57 + printf(" Test ucx_mempool_calloc\n"); 1.58 + char *str = ucx_mempool_calloc(pool, 1, 3); 1.59 + if(str[0] != 0 || str[1] != 0 || str[2] != 0) { 1.60 + fprintf(stderr, "ucx_mempool_calloc failed\n"); 1.61 + } 1.62 + str[0] = 'O'; 1.63 + str[1] = 'K'; 1.64 + 1.65 + printf(" Test ucx_mempool_realloc\n"); 1.66 + str = ucx_mempool_realloc(pool, str, 4); 1.67 + str[2] = '!'; 1.68 + str[3] = 0; 1.69 + if(strcmp(str, "OK!") != 0) { 1.70 + fprintf(stderr, "Test ucx_mempool_realloc failed!\n"); 1.71 + } 1.72 + 1.73 + printf(" Test ucx_mempool_reg_destr\n"); 1.74 + char *hello = "Hello World!"; 1.75 + ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor); 1.76 + 1.77 + printf(" Test ucx_mempool_free\n"); 1.78 + ucx_mempool_free(pool); 1.79 + 1.80 + 1.81 + return 0; 1.82 +}