blob: 7541475096d43f0b71610687c23f7a3c26c3cd60 [file] [log] [blame]
Benny Prijonoe7224612005-11-13 19:40:44 +00001/* $Id$
2 */
3/*
4 * PJLIB - PJ Foundation Library
5 * (C)2003-2005 Benny Prijono <bennylp@bulukucing.org>
6 *
7 * Author:
8 * Benny Prijono <bennylp@bulukucing.org>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24#include <pj/pool.h>
25#include <pj/rand.h>
26#include <pj/log.h>
27#include "test.h"
28
29/**
30 * \page page_pjlib_pool_test Test: Pool
31 *
32 * This file provides implementation of \b pool_test(). It tests the
33 * functionality of the memory pool.
34 *
35 *
36 * This file is <b>pjlib-test/pool.c</b>
37 *
38 * \include pjlib-test/pool.c
39 */
40
41
42#if INCLUDE_POOL_TEST
43
44#define SIZE 4096
45
46/* Normally we should throw exception when memory alloc fails.
47 * Here we do nothing so that the flow will go back to original caller,
48 * which will test the result using NULL comparison. Normally caller will
49 * catch the exception instead of checking for NULLs.
50 */
51static void null_callback(pj_pool_t *pool, pj_size_t size)
52{
53 PJ_UNUSED_ARG(pool);
54 PJ_UNUSED_ARG(size);
55}
56
57#define GET_FREE(p) (pj_pool_get_capacity(p)-pj_pool_get_used_size(p))
58
59/* Test that the capacity and used size reported by the pool is correct.
60 */
61static int capacity_test(void)
62{
63 pj_pool_t *pool = pj_pool_create(mem, NULL, SIZE, 0, &null_callback);
64 pj_size_t freesize;
65
66 PJ_LOG(3,("test", "...capacity_test()"));
67
68 if (!pool)
69 return -200;
70
71 freesize = GET_FREE(pool);
72
73 if (pj_pool_alloc(pool, freesize) == NULL) {
74 PJ_LOG(3,("test", "...error: wrong freesize %u reported",
75 freesize));
76 pj_pool_release(pool);
77 return -210;
78 }
79
80 pj_pool_release(pool);
81 return 0;
82}
83
84/* Test function to drain the pool's space.
85 */
86static int drain_test(pj_size_t size, pj_size_t increment)
87{
88 pj_pool_t *pool = pj_pool_create(mem, NULL, size, increment,
89 &null_callback);
90 pj_size_t freesize;
91 void *p;
92 int status = 0;
93
94 PJ_LOG(3,("test", "...drain_test(%d,%d)", size, increment));
95
96 if (!pool)
97 return -10;
98
99 /* Get free size */
100 freesize = GET_FREE(pool);
101 if (freesize < 1) {
102 status=-15;
103 goto on_error;
104 }
105
106 /* Drain the pool until there's nothing left. */
107 while (freesize > 0) {
108 int size;
109
110 if (freesize > 255)
111 size = ((pj_rand() & 0x000000FF) + 4) & ~0x03L;
112 else
113 size = freesize;
114
115 p = pj_pool_alloc(pool, size);
116 if (!p) {
117 status=-20; goto on_error;
118 }
119
120 freesize -= size;
121 }
122
123 /* Check that capacity is zero. */
124 if (GET_FREE(pool) != 0) {
125 PJ_LOG(3,("test", "....error: returned free=%u (expecting 0)",
126 GET_FREE(pool)));
127 status=-30; goto on_error;
128 }
129
130 /* Try to allocate once more */
131 p = pj_pool_alloc(pool, 257);
132 if (!p) {
133 status=-40; goto on_error;
134 }
135
136 /* Check that capacity is NOT zero. */
137 if (GET_FREE(pool) == 0) {
138 status=-50; goto on_error;
139 }
140
141
142on_error:
143 pj_pool_release(pool);
144 return status;
145}
146
147int pool_test(void)
148{
149 enum { LOOP = 2 };
150 int loop;
151 int rc;
152
153 rc = capacity_test();
154 if (rc) return rc;
155
156 for (loop=0; loop<LOOP; ++loop) {
157 /* Test that the pool should grow automaticly. */
158 rc = drain_test(SIZE, SIZE);
159 if (rc != 0) return rc;
160
161 /* Test situation where pool is not allowed to grow.
162 * We expect the test to return correct error.
163 */
164 rc = drain_test(SIZE, 0);
165 if (rc != -40) return rc;
166 }
167
168 return 0;
169}
170
171#else
172/* To prevent warning about "translation unit is empty"
173 * when this test is disabled.
174 */
175int dummy_pool_test;
176#endif /* INCLUDE_POOL_TEST */
177