Wed, 11 Jan 2012 12:19:48 +0100
Removed linked list from tests (assume that they are correct if the dlist tests are correct)
1 /*
2 *
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 #include "mpool_tests.h"
11 int cmp_value = 2;
13 void int_destructor(int *ptr) {
14 if(*ptr != cmp_value) {
15 fprintf(stderr, "ucx_mempool_free failed: wrong order\n");
16 return;
17 }
18 cmp_value += 2;
19 }
21 void hello_destructor(char *str) {
22 if(strcmp(str, "Hello World!") != 0) {
23 fprintf(stderr, "ucx_mempool_reg_destr failed\n");
24 }
25 }
27 int mpool_tests() {
28 int r = 0;
30 printf(" Test ucx_mempool_new\n");
31 UcxMempool *pool = ucx_mempool_new(16);
33 printf(" Test ucx_mempool_malloc\n");
34 int *ptr1 = (int*)ucx_mempool_malloc(pool, sizeof(int));
35 for(int i=0;i<256;i++) {
36 ucx_mempool_malloc(pool, i+1);
37 }
38 int *ptr2 = (int*)ucx_mempool_malloc(pool, sizeof(int));
39 int *ptr3 = (int*)ucx_mempool_malloc(pool, sizeof(int));
40 for(int i=0;i<256;i++) {
41 ucx_mempool_malloc(pool, i+1);
42 }
43 int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int));
45 *ptr1 = 2;
46 *ptr2 = 4;
47 *ptr3 = 6;
48 *ptr4 = 8;
50 printf(" Test ucx_mempool_set_destr\n");
51 ucx_mempool_set_destr(ptr1, (ucx_destructor)int_destructor);
52 ucx_mempool_set_destr(ptr2, (ucx_destructor)int_destructor);
53 ucx_mempool_set_destr(ptr3, (ucx_destructor)int_destructor);
54 ucx_mempool_set_destr(ptr4, (ucx_destructor)int_destructor);
56 printf(" Test ucx_mempool_calloc\n");
57 char *str = ucx_mempool_calloc(pool, 1, 3);
58 if(str[0] != 0 || str[1] != 0 || str[2] != 0) {
59 fprintf(stderr, "ucx_mempool_calloc failed\n");
60 r--;
61 }
62 str[0] = 'O';
63 str[1] = 'K';
65 printf(" Test ucx_mempool_realloc\n");
66 str = ucx_mempool_realloc(pool, str, 4);
67 str[2] = '!';
68 str[3] = 0;
69 if(strcmp(str, "OK!") != 0) {
70 fprintf(stderr, "Test ucx_mempool_realloc failed!\n");
71 r--;
72 }
74 printf(" Test ucx_mempool_reg_destr\n");
75 char *hello = "Hello World!";
76 ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor);
78 printf(" Test ucx_mempool_free\n");
79 //ucx_mempool_free(pool);
82 return r;
83 }