blob: ce91ac6e4749ecb408488dde63abd34535712418 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pj/pool.h>
Benny Prijono83217fa2007-02-16 21:44:36 +000021#include <pj/pool_buf.h>
Benny Prijono5dcb38d2005-11-21 01:55:47 +000022#include <pj/rand.h>
23#include <pj/log.h>
Benny Prijono83217fa2007-02-16 21:44:36 +000024#include <pj/except.h>
Benny Prijono5dcb38d2005-11-21 01:55:47 +000025#include "test.h"
26
27/**
28 * \page page_pjlib_pool_test Test: Pool
29 *
30 * This file provides implementation of \b pool_test(). It tests the
31 * functionality of the memory pool.
32 *
33 *
34 * This file is <b>pjlib-test/pool.c</b>
35 *
36 * \include pjlib-test/pool.c
37 */
38
39
40#if INCLUDE_POOL_TEST
41
42#define SIZE 4096
43
44/* Normally we should throw exception when memory alloc fails.
45 * Here we do nothing so that the flow will go back to original caller,
46 * which will test the result using NULL comparison. Normally caller will
47 * catch the exception instead of checking for NULLs.
48 */
49static void null_callback(pj_pool_t *pool, pj_size_t size)
50{
51 PJ_UNUSED_ARG(pool);
52 PJ_UNUSED_ARG(size);
53}
54
55#define GET_FREE(p) (pj_pool_get_capacity(p)-pj_pool_get_used_size(p))
56
57/* Test that the capacity and used size reported by the pool is correct.
58 */
59static int capacity_test(void)
60{
61 pj_pool_t *pool = pj_pool_create(mem, NULL, SIZE, 0, &null_callback);
62 pj_size_t freesize;
63
64 PJ_LOG(3,("test", "...capacity_test()"));
65
66 if (!pool)
67 return -200;
68
69 freesize = GET_FREE(pool);
70
71 if (pj_pool_alloc(pool, freesize) == NULL) {
72 PJ_LOG(3,("test", "...error: wrong freesize %u reported",
73 freesize));
74 pj_pool_release(pool);
75 return -210;
76 }
77
78 pj_pool_release(pool);
79 return 0;
80}
81
82/* Test function to drain the pool's space.
83 */
84static int drain_test(pj_size_t size, pj_size_t increment)
85{
86 pj_pool_t *pool = pj_pool_create(mem, NULL, size, increment,
87 &null_callback);
88 pj_size_t freesize;
89 void *p;
90 int status = 0;
91
92 PJ_LOG(3,("test", "...drain_test(%d,%d)", size, increment));
93
94 if (!pool)
95 return -10;
96
97 /* Get free size */
98 freesize = GET_FREE(pool);
99 if (freesize < 1) {
100 status=-15;
101 goto on_error;
102 }
103
104 /* Drain the pool until there's nothing left. */
105 while (freesize > 0) {
106 int size;
107
108 if (freesize > 255)
Nanang Izzuddin9375aca2008-04-23 14:35:24 +0000109 size = ((pj_rand() & 0x000000FF) + PJ_POOL_ALIGNMENT) &
110 ~(PJ_POOL_ALIGNMENT - 1);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000111 else
112 size = freesize;
113
114 p = pj_pool_alloc(pool, size);
115 if (!p) {
116 status=-20; goto on_error;
117 }
118
119 freesize -= size;
120 }
121
122 /* Check that capacity is zero. */
123 if (GET_FREE(pool) != 0) {
124 PJ_LOG(3,("test", "....error: returned free=%u (expecting 0)",
125 GET_FREE(pool)));
126 status=-30; goto on_error;
127 }
128
129 /* Try to allocate once more */
130 p = pj_pool_alloc(pool, 257);
131 if (!p) {
132 status=-40; goto on_error;
133 }
134
135 /* Check that capacity is NOT zero. */
136 if (GET_FREE(pool) == 0) {
137 status=-50; goto on_error;
138 }
139
140
141on_error:
142 pj_pool_release(pool);
143 return status;
144}
145
Benny Prijono83217fa2007-02-16 21:44:36 +0000146/* Test the buffer based pool */
147static int pool_buf_test(void)
148{
149 enum { STATIC_BUF_SIZE = 40 };
150 /* 16 is the internal struct in pool_buf */
151 static char buf[ STATIC_BUF_SIZE + sizeof(pj_pool_t) +
152 sizeof(pj_pool_block) + 16];
153 pj_pool_t *pool;
154 void *p;
155 PJ_USE_EXCEPTION;
156
157 PJ_LOG(3,("test", "...pool_buf test"));
158
159 pool = pj_pool_create_on_buf("no name", buf, sizeof(buf));
160 if (!pool)
161 return -70;
162
163 /* Drain the pool */
164 PJ_TRY {
165 if ((p=pj_pool_alloc(pool, STATIC_BUF_SIZE/2)) == NULL)
166 return -75;
167
168 if ((p=pj_pool_alloc(pool, STATIC_BUF_SIZE/2)) == NULL)
169 return -76;
170 }
171 PJ_CATCH_ANY {
172 return -77;
173 }
174 PJ_END;
175
176 /* On the next alloc, exception should be thrown */
177 PJ_TRY {
178 p = pj_pool_alloc(pool, STATIC_BUF_SIZE);
179 if (p != NULL) {
180 /* This is unexpected, the alloc should fail */
181 return -78;
182 }
183 }
184 PJ_CATCH_ANY {
185 /* This is the expected result */
186 }
187 PJ_END;
188
189 /* Done */
190 return 0;
191}
192
193
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000194int pool_test(void)
195{
196 enum { LOOP = 2 };
197 int loop;
198 int rc;
199
200 rc = capacity_test();
201 if (rc) return rc;
202
203 for (loop=0; loop<LOOP; ++loop) {
204 /* Test that the pool should grow automaticly. */
205 rc = drain_test(SIZE, SIZE);
206 if (rc != 0) return rc;
207
208 /* Test situation where pool is not allowed to grow.
209 * We expect the test to return correct error.
210 */
211 rc = drain_test(SIZE, 0);
212 if (rc != -40) return rc;
213 }
214
Benny Prijono83217fa2007-02-16 21:44:36 +0000215 rc = pool_buf_test();
216 if (rc != 0)
217 return rc;
218
219
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000220 return 0;
221}
222
223#else
224/* To prevent warning about "translation unit is empty"
225 * when this test is disabled.
226 */
227int dummy_pool_test;
228#endif /* INCLUDE_POOL_TEST */
229