test/mpool_tests.c

Wed, 11 Jan 2012 12:19:48 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 11 Jan 2012 12:19:48 +0100
changeset 19
cdd7a3173249
parent 14
b78e174b6814
child 28
1666cbeb1db8
permissions
-rw-r--r--

Removed linked list from tests (assume that they are correct if the dlist tests are correct)

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() {
universe@19 28 int r = 0;
universe@19 29
olaf@13 30 printf(" Test ucx_mempool_new\n");
olaf@13 31 UcxMempool *pool = ucx_mempool_new(16);
olaf@13 32
olaf@13 33 printf(" Test ucx_mempool_malloc\n");
olaf@13 34 int *ptr1 = (int*)ucx_mempool_malloc(pool, sizeof(int));
olaf@13 35 for(int i=0;i<256;i++) {
olaf@14 36 ucx_mempool_malloc(pool, i+1);
olaf@13 37 }
olaf@13 38 int *ptr2 = (int*)ucx_mempool_malloc(pool, sizeof(int));
olaf@13 39 int *ptr3 = (int*)ucx_mempool_malloc(pool, sizeof(int));
olaf@13 40 for(int i=0;i<256;i++) {
olaf@14 41 ucx_mempool_malloc(pool, i+1);
olaf@13 42 }
olaf@13 43 int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int));
olaf@13 44
olaf@13 45 *ptr1 = 2;
olaf@13 46 *ptr2 = 4;
olaf@13 47 *ptr3 = 6;
olaf@13 48 *ptr4 = 8;
olaf@13 49
olaf@13 50 printf(" Test ucx_mempool_set_destr\n");
olaf@13 51 ucx_mempool_set_destr(ptr1, (ucx_destructor)int_destructor);
olaf@13 52 ucx_mempool_set_destr(ptr2, (ucx_destructor)int_destructor);
olaf@13 53 ucx_mempool_set_destr(ptr3, (ucx_destructor)int_destructor);
olaf@13 54 ucx_mempool_set_destr(ptr4, (ucx_destructor)int_destructor);
olaf@13 55
olaf@13 56 printf(" Test ucx_mempool_calloc\n");
olaf@13 57 char *str = ucx_mempool_calloc(pool, 1, 3);
olaf@13 58 if(str[0] != 0 || str[1] != 0 || str[2] != 0) {
olaf@13 59 fprintf(stderr, "ucx_mempool_calloc failed\n");
universe@19 60 r--;
olaf@13 61 }
olaf@13 62 str[0] = 'O';
olaf@13 63 str[1] = 'K';
olaf@13 64
olaf@13 65 printf(" Test ucx_mempool_realloc\n");
olaf@13 66 str = ucx_mempool_realloc(pool, str, 4);
olaf@13 67 str[2] = '!';
olaf@13 68 str[3] = 0;
olaf@13 69 if(strcmp(str, "OK!") != 0) {
olaf@13 70 fprintf(stderr, "Test ucx_mempool_realloc failed!\n");
universe@19 71 r--;
olaf@13 72 }
olaf@14 73
olaf@13 74 printf(" Test ucx_mempool_reg_destr\n");
olaf@13 75 char *hello = "Hello World!";
olaf@13 76 ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor);
olaf@13 77
olaf@13 78 printf(" Test ucx_mempool_free\n");
olaf@14 79 //ucx_mempool_free(pool);
olaf@13 80
olaf@13 81
universe@19 82 return r;
olaf@13 83 }

mercurial