EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / tests / eina_test_mempool.c
1 /* EINA - EFL data type library
2  * Copyright (C) 2008 Cedric Bail
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library;
16  * if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "eina_suite.h"
24 #include "Eina.h"
25
26 static Eina_Array *_modules;
27
28 static void
29 _mempool_init(void)
30 {
31    eina_init();
32    /* force modules to be loaded in case they are not installed */
33    _modules = eina_module_list_get(NULL,
34                                    PACKAGE_BUILD_DIR "/src/modules",
35                                    EINA_TRUE,
36                                    NULL,
37                                    NULL);
38    eina_module_list_load(_modules);
39 }
40
41 static void
42 _mempool_shutdown(void)
43 {
44    eina_module_list_free(_modules);
45    /* TODO delete the list */
46    eina_shutdown();
47 }
48
49 static void
50 _eina_mempool_test(Eina_Mempool *mp, Eina_Bool with_realloc, Eina_Bool with_gc)
51 {
52    int *tbl[512];
53    int i;
54
55         fail_if(!mp);
56
57    for (i = 0; i < 512; ++i)
58      {
59         tbl[i] = eina_mempool_malloc(mp, sizeof (int));
60         fail_if(!tbl[i]);
61         *tbl[i] = i;
62      }
63
64    for (i = 0; i < 512; ++i)
65         fail_if(*tbl[i] != i);
66
67    for (i = 0; i < 256; ++i)
68         eina_mempool_free(mp, tbl[i]);
69
70    if (with_realloc)
71       fail_if(eina_mempool_realloc(mp, tbl[500], 25) == NULL);
72    else
73       fail_if(eina_mempool_realloc(mp, tbl[500], 25) != NULL);
74
75    if (with_gc)
76      {
77         eina_mempool_gc(mp);
78         eina_mempool_statistics(mp);
79      }
80
81    eina_mempool_del(mp);
82 }
83
84 #ifdef EINA_BUILD_CHAINED_POOL
85 START_TEST(eina_mempool_chained_mempool)
86 {
87    Eina_Mempool *mp;
88
89    _mempool_init();
90
91    mp = eina_mempool_add("chained_mempool", "test", NULL, sizeof (int), 256);
92    _eina_mempool_test(mp, EINA_FALSE, EINA_FALSE);
93
94    _mempool_shutdown();
95 }
96 END_TEST
97 #endif
98
99 #ifdef EINA_BUILD_PASS_THROUGH
100 START_TEST(eina_mempool_pass_through)
101 {
102    Eina_Mempool *mp;
103
104    _mempool_init();
105
106    mp = eina_mempool_add("pass_through", "test", NULL, sizeof (int), 8, 0);
107    _eina_mempool_test(mp, EINA_TRUE, EINA_FALSE);
108
109    _mempool_shutdown();
110 }
111 END_TEST
112 #endif
113
114 #ifdef EINA_BUILD_FIXED_BITMAP
115 START_TEST(eina_mempool_fixed_bitmap)
116 {
117    Eina_Mempool *mp;
118
119    _mempool_init();
120
121    mp = eina_mempool_add("fixed_bitmap", "test", NULL, sizeof (int));
122    _eina_mempool_test(mp, EINA_FALSE, EINA_FALSE);
123
124    _mempool_shutdown();
125 }
126 END_TEST
127 #endif
128
129 #ifdef EINA_BUILD_EMEMOA_FIXED
130 START_TEST(eina_mempool_ememoa_fixed)
131 {
132    Eina_Mempool *mp;
133
134    _mempool_init();
135
136    mp = eina_mempool_add("ememoa_fixed", "test", NULL, sizeof (int), 8, 0);
137    _eina_mempool_test(mp, EINA_FALSE, EINA_TRUE);
138
139    _mempool_shutdown();
140 }
141 END_TEST
142 #endif
143
144 #ifdef EINA_BUILD_EMEMOA_UNKNOWN
145 START_TEST(eina_mempool_ememoa_unknown)
146 {
147    Eina_Mempool *mp;
148
149    _mempool_init();
150
151    mp = eina_mempool_add("ememoa_unknown",
152                          "test",
153                          NULL,
154                          0,
155                          2,
156                          sizeof (int),
157                          8,
158                          sizeof (int) * 2,
159                          8);
160    _eina_mempool_test(mp, EINA_TRUE, EINA_TRUE);
161
162    _mempool_shutdown();
163 }
164 END_TEST
165 #endif
166
167 void
168 eina_test_mempool(TCase *tc)
169 {
170 #ifdef EINA_BUILD_CHAINED_POOL
171    tcase_add_test(tc, eina_mempool_chained_mempool);
172 #endif
173 #ifdef EINA_BUILD_PASS_THROUGH
174    tcase_add_test(tc, eina_mempool_pass_through);
175 #endif
176 #ifdef EINA_BUILD_FIXED_BITMAP
177    tcase_add_test(tc, eina_mempool_fixed_bitmap);
178 #endif
179 #ifdef EINA_BUILD_EMEMOA_FIXED
180    tcase_add_test(tc, eina_mempool_ememoa_fixed);
181 #endif
182 #ifdef EINA_BUILD_EMEMOA_UNKNOWN
183    tcase_add_test(tc, eina_mempool_ememoa_unknown);
184 #endif
185 }
186
187