Sat, 31 Dec 2011 22:21:45 +0100
Fixed realloc
olaf@13 | 1 | /* |
olaf@13 | 2 | * |
olaf@13 | 3 | */ |
olaf@13 | 4 | |
olaf@13 | 5 | #include <stdio.h> |
olaf@13 | 6 | #include <stdlib.h> |
olaf@13 | 7 | #include <string.h> |
olaf@13 | 8 | |
olaf@13 | 9 | #include "mpool_tests.h" |
olaf@13 | 10 | |
olaf@13 | 11 | int cmp_value = 2; |
olaf@13 | 12 | |
olaf@13 | 13 | void int_destructor(int *ptr) { |
olaf@13 | 14 | if(*ptr != cmp_value) { |
olaf@13 | 15 | fprintf(stderr, "ucx_mempool_free failed: wrong order\n"); |
olaf@13 | 16 | return; |
olaf@13 | 17 | } |
olaf@13 | 18 | cmp_value += 2; |
olaf@13 | 19 | } |
olaf@13 | 20 | |
olaf@13 | 21 | void hello_destructor(char *str) { |
olaf@13 | 22 | if(strcmp(str, "Hello World!") != 0) { |
olaf@13 | 23 | fprintf(stderr, "ucx_mempool_reg_destr failed\n"); |
olaf@13 | 24 | } |
olaf@13 | 25 | } |
olaf@13 | 26 | |
olaf@13 | 27 | int mpool_tests() { |
olaf@13 | 28 | printf(" Test ucx_mempool_new\n"); |
olaf@13 | 29 | UcxMempool *pool = ucx_mempool_new(16); |
olaf@13 | 30 | |
olaf@13 | 31 | printf(" Test ucx_mempool_malloc\n"); |
olaf@13 | 32 | int *ptr1 = (int*)ucx_mempool_malloc(pool, sizeof(int)); |
olaf@13 | 33 | for(int i=0;i<256;i++) { |
olaf@14 | 34 | ucx_mempool_malloc(pool, i+1); |
olaf@13 | 35 | } |
olaf@13 | 36 | int *ptr2 = (int*)ucx_mempool_malloc(pool, sizeof(int)); |
olaf@13 | 37 | int *ptr3 = (int*)ucx_mempool_malloc(pool, sizeof(int)); |
olaf@13 | 38 | for(int i=0;i<256;i++) { |
olaf@14 | 39 | ucx_mempool_malloc(pool, i+1); |
olaf@13 | 40 | } |
olaf@13 | 41 | int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int)); |
olaf@13 | 42 | |
olaf@13 | 43 | *ptr1 = 2; |
olaf@13 | 44 | *ptr2 = 4; |
olaf@13 | 45 | *ptr3 = 6; |
olaf@13 | 46 | *ptr4 = 8; |
olaf@13 | 47 | |
olaf@13 | 48 | printf(" Test ucx_mempool_set_destr\n"); |
olaf@13 | 49 | ucx_mempool_set_destr(ptr1, (ucx_destructor)int_destructor); |
olaf@13 | 50 | ucx_mempool_set_destr(ptr2, (ucx_destructor)int_destructor); |
olaf@13 | 51 | ucx_mempool_set_destr(ptr3, (ucx_destructor)int_destructor); |
olaf@13 | 52 | ucx_mempool_set_destr(ptr4, (ucx_destructor)int_destructor); |
olaf@13 | 53 | |
olaf@13 | 54 | printf(" Test ucx_mempool_calloc\n"); |
olaf@13 | 55 | char *str = ucx_mempool_calloc(pool, 1, 3); |
olaf@13 | 56 | if(str[0] != 0 || str[1] != 0 || str[2] != 0) { |
olaf@13 | 57 | fprintf(stderr, "ucx_mempool_calloc failed\n"); |
olaf@13 | 58 | } |
olaf@13 | 59 | str[0] = 'O'; |
olaf@13 | 60 | str[1] = 'K'; |
olaf@13 | 61 | |
olaf@13 | 62 | printf(" Test ucx_mempool_realloc\n"); |
olaf@13 | 63 | str = ucx_mempool_realloc(pool, str, 4); |
olaf@13 | 64 | str[2] = '!'; |
olaf@13 | 65 | str[3] = 0; |
olaf@13 | 66 | if(strcmp(str, "OK!") != 0) { |
olaf@13 | 67 | fprintf(stderr, "Test ucx_mempool_realloc failed!\n"); |
olaf@13 | 68 | } |
olaf@14 | 69 | |
olaf@13 | 70 | printf(" Test ucx_mempool_reg_destr\n"); |
olaf@13 | 71 | char *hello = "Hello World!"; |
olaf@13 | 72 | ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor); |
olaf@13 | 73 | |
olaf@13 | 74 | printf(" Test ucx_mempool_free\n"); |
olaf@14 | 75 | //ucx_mempool_free(pool); |
olaf@13 | 76 | |
olaf@13 | 77 | |
olaf@13 | 78 | return 0; |
olaf@13 | 79 | } |