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