Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 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 "test.h" |
| 21 | |
| 22 | /* To prevent warning about "translation unit is empty" |
| 23 | * when this test is disabled. |
| 24 | */ |
| 25 | int dummy_fifobuf_test; |
| 26 | |
| 27 | #if INCLUDE_FIFOBUF_TEST |
| 28 | |
| 29 | #include <pjlib.h> |
| 30 | |
| 31 | int fifobuf_test() |
| 32 | { |
| 33 | enum { SIZE = 1024, MAX_ENTRIES = 128, |
| 34 | MIN_SIZE = 4, MAX_SIZE = 64, |
| 35 | LOOP=10000 }; |
| 36 | pj_pool_t *pool; |
| 37 | pj_fifobuf_t fifo; |
| 38 | unsigned available = SIZE; |
| 39 | void *entries[MAX_ENTRIES]; |
| 40 | void *buffer; |
| 41 | int i; |
| 42 | |
| 43 | pool = pj_pool_create(mem, NULL, SIZE+256, 0, NULL); |
| 44 | if (!pool) |
| 45 | return -10; |
| 46 | |
| 47 | buffer = pj_pool_alloc(pool, SIZE); |
| 48 | if (!buffer) |
| 49 | return -20; |
| 50 | |
| 51 | pj_fifobuf_init (&fifo, buffer, SIZE); |
| 52 | |
| 53 | // Test 1 |
| 54 | for (i=0; i<LOOP*MAX_ENTRIES; ++i) { |
| 55 | int size; |
| 56 | int c, f; |
| 57 | c = i%2; |
| 58 | f = (i+1)%2; |
| 59 | do { |
| 60 | size = MIN_SIZE+(pj_rand() % MAX_SIZE); |
| 61 | entries[c] = pj_fifobuf_alloc (&fifo, size); |
| 62 | } while (entries[c] == 0); |
| 63 | if ( i!=0) { |
| 64 | pj_fifobuf_free(&fifo, entries[f]); |
| 65 | } |
| 66 | } |
| 67 | if (entries[(i+1)%2]) |
| 68 | pj_fifobuf_free(&fifo, entries[(i+1)%2]); |
| 69 | |
| 70 | if (pj_fifobuf_max_size(&fifo) < SIZE-4) { |
| 71 | pj_assert(0); |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | // Test 2 |
| 76 | entries[0] = pj_fifobuf_alloc (&fifo, MIN_SIZE); |
| 77 | if (!entries[0]) return -1; |
| 78 | for (i=0; i<LOOP*MAX_ENTRIES; ++i) { |
| 79 | int size = MIN_SIZE+(pj_rand() % MAX_SIZE); |
| 80 | entries[1] = pj_fifobuf_alloc (&fifo, size); |
| 81 | if (entries[1]) |
| 82 | pj_fifobuf_unalloc(&fifo, entries[1]); |
| 83 | } |
| 84 | pj_fifobuf_unalloc(&fifo, entries[0]); |
| 85 | if (pj_fifobuf_max_size(&fifo) < SIZE-4) { |
| 86 | pj_assert(0); |
| 87 | return -2; |
| 88 | } |
| 89 | |
| 90 | // Test 3 |
| 91 | for (i=0; i<LOOP; ++i) { |
| 92 | int count, j; |
| 93 | for (count=0; available>=MIN_SIZE+4 && count < MAX_ENTRIES;) { |
| 94 | int size = MIN_SIZE+(pj_rand() % MAX_SIZE); |
| 95 | entries[count] = pj_fifobuf_alloc (&fifo, size); |
| 96 | if (entries[count]) { |
| 97 | available -= (size+4); |
| 98 | ++count; |
| 99 | } |
| 100 | } |
| 101 | for (j=0; j<count; ++j) { |
| 102 | pj_fifobuf_free (&fifo, entries[j]); |
| 103 | } |
| 104 | available = SIZE; |
| 105 | } |
| 106 | |
| 107 | if (pj_fifobuf_max_size(&fifo) < SIZE-4) { |
| 108 | pj_assert(0); |
| 109 | return -3; |
| 110 | } |
| 111 | pj_pool_release(pool); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | #endif /* INCLUDE_FIFOBUF_TEST */ |
| 116 | |
| 117 | |