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