olaf@13: /* olaf@13: * olaf@13: */ olaf@13: olaf@13: #include olaf@13: #include olaf@13: #include olaf@13: olaf@13: #include "mpool_tests.h" olaf@13: olaf@13: int cmp_value = 2; olaf@13: olaf@13: void int_destructor(int *ptr) { olaf@13: if(*ptr != cmp_value) { olaf@13: fprintf(stderr, "ucx_mempool_free failed: wrong order\n"); olaf@13: return; olaf@13: } olaf@13: cmp_value += 2; olaf@13: } olaf@13: olaf@13: void hello_destructor(char *str) { olaf@13: if(strcmp(str, "Hello World!") != 0) { olaf@13: fprintf(stderr, "ucx_mempool_reg_destr failed\n"); olaf@13: } olaf@13: } olaf@13: olaf@13: int mpool_tests() { olaf@13: printf(" Test ucx_mempool_new\n"); olaf@13: UcxMempool *pool = ucx_mempool_new(16); olaf@13: olaf@13: printf(" Test ucx_mempool_malloc\n"); olaf@13: int *ptr1 = (int*)ucx_mempool_malloc(pool, sizeof(int)); olaf@13: for(int i=0;i<256;i++) { olaf@13: ucx_mempool_malloc(pool, i); olaf@13: } olaf@13: int *ptr2 = (int*)ucx_mempool_malloc(pool, sizeof(int)); olaf@13: int *ptr3 = (int*)ucx_mempool_malloc(pool, sizeof(int)); olaf@13: for(int i=0;i<256;i++) { olaf@13: ucx_mempool_malloc(pool, i); olaf@13: } olaf@13: int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int)); olaf@13: olaf@13: *ptr1 = 2; olaf@13: *ptr2 = 4; olaf@13: *ptr3 = 6; olaf@13: *ptr4 = 8; olaf@13: olaf@13: printf(" Test ucx_mempool_set_destr\n"); olaf@13: ucx_mempool_set_destr(ptr1, (ucx_destructor)int_destructor); olaf@13: ucx_mempool_set_destr(ptr2, (ucx_destructor)int_destructor); olaf@13: ucx_mempool_set_destr(ptr3, (ucx_destructor)int_destructor); olaf@13: ucx_mempool_set_destr(ptr4, (ucx_destructor)int_destructor); olaf@13: olaf@13: printf(" Test ucx_mempool_calloc\n"); olaf@13: char *str = ucx_mempool_calloc(pool, 1, 3); olaf@13: if(str[0] != 0 || str[1] != 0 || str[2] != 0) { olaf@13: fprintf(stderr, "ucx_mempool_calloc failed\n"); olaf@13: } olaf@13: str[0] = 'O'; olaf@13: str[1] = 'K'; olaf@13: olaf@13: printf(" Test ucx_mempool_realloc\n"); olaf@13: str = ucx_mempool_realloc(pool, str, 4); olaf@13: str[2] = '!'; olaf@13: str[3] = 0; olaf@13: if(strcmp(str, "OK!") != 0) { olaf@13: fprintf(stderr, "Test ucx_mempool_realloc failed!\n"); olaf@13: } olaf@13: olaf@13: printf(" Test ucx_mempool_reg_destr\n"); olaf@13: char *hello = "Hello World!"; olaf@13: ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor); olaf@13: olaf@13: printf(" Test ucx_mempool_free\n"); olaf@13: ucx_mempool_free(pool); olaf@13: olaf@13: olaf@13: return 0; olaf@13: }