test/mpool_tests.c

Sat, 18 Feb 2012 18:36:30 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 18 Feb 2012 18:36:30 +0100
changeset 27
22644e2572bc
parent 19
cdd7a3173249
child 28
1666cbeb1db8
permissions
-rw-r--r--

removed old foreach + refactored list tests

13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 *
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 */
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 #include <stdio.h>
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 #include <stdlib.h>
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 #include <string.h>
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 #include "mpool_tests.h"
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 int cmp_value = 2;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 void int_destructor(int *ptr) {
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 if(*ptr != cmp_value) {
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 fprintf(stderr, "ucx_mempool_free failed: wrong order\n");
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 return;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 cmp_value += 2;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 void hello_destructor(char *str) {
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 if(strcmp(str, "Hello World!") != 0) {
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 fprintf(stderr, "ucx_mempool_reg_destr failed\n");
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 int mpool_tests() {
19
cdd7a3173249 Removed linked list from tests (assume that they are correct if the dlist tests are correct)
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
28 int r = 0;
cdd7a3173249 Removed linked list from tests (assume that they are correct if the dlist tests are correct)
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
29
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 printf(" Test ucx_mempool_new\n");
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 UcxMempool *pool = ucx_mempool_new(16);
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 printf(" Test ucx_mempool_malloc\n");
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 int *ptr1 = (int*)ucx_mempool_malloc(pool, sizeof(int));
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 for(int i=0;i<256;i++) {
14
b78e174b6814 Fixed realloc
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
36 ucx_mempool_malloc(pool, i+1);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 int *ptr2 = (int*)ucx_mempool_malloc(pool, sizeof(int));
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 int *ptr3 = (int*)ucx_mempool_malloc(pool, sizeof(int));
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 for(int i=0;i<256;i++) {
14
b78e174b6814 Fixed realloc
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
41 ucx_mempool_malloc(pool, i+1);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int));
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 *ptr1 = 2;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 *ptr2 = 4;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 *ptr3 = 6;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 *ptr4 = 8;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 printf(" Test ucx_mempool_set_destr\n");
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 ucx_mempool_set_destr(ptr1, (ucx_destructor)int_destructor);
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 ucx_mempool_set_destr(ptr2, (ucx_destructor)int_destructor);
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 ucx_mempool_set_destr(ptr3, (ucx_destructor)int_destructor);
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 ucx_mempool_set_destr(ptr4, (ucx_destructor)int_destructor);
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 printf(" Test ucx_mempool_calloc\n");
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 char *str = ucx_mempool_calloc(pool, 1, 3);
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 if(str[0] != 0 || str[1] != 0 || str[2] != 0) {
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 fprintf(stderr, "ucx_mempool_calloc failed\n");
19
cdd7a3173249 Removed linked list from tests (assume that they are correct if the dlist tests are correct)
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
60 r--;
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 str[0] = 'O';
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 str[1] = 'K';
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 printf(" Test ucx_mempool_realloc\n");
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 str = ucx_mempool_realloc(pool, str, 4);
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 str[2] = '!';
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 str[3] = 0;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 if(strcmp(str, "OK!") != 0) {
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 fprintf(stderr, "Test ucx_mempool_realloc failed!\n");
19
cdd7a3173249 Removed linked list from tests (assume that they are correct if the dlist tests are correct)
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
71 r--;
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 }
14
b78e174b6814 Fixed realloc
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
73
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 printf(" Test ucx_mempool_reg_destr\n");
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 char *hello = "Hello World!";
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor);
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 printf(" Test ucx_mempool_free\n");
14
b78e174b6814 Fixed realloc
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
79 //ucx_mempool_free(pool);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81
19
cdd7a3173249 Removed linked list from tests (assume that they are correct if the dlist tests are correct)
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
82 return r;
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 }

mercurial