Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/fifobuf.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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
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 #define THIS_FILE "fifobuf.c"
32
33 enum {
34 SZ = sizeof(unsigned),
35 };
36
37 static int fifobuf_size_test()
38 {
39 enum { SIZE = 32 };
40 char before[8];
41 char buffer[SIZE];
42 char after[8];
43 char zero[8];
44 void *p0, *p1;
45 pj_fifobuf_t fifo;
46
47 pj_bzero(before, sizeof(before));
48 pj_bzero(after, sizeof(after));
49 pj_bzero(zero, sizeof(zero));
50
51 pj_fifobuf_init (&fifo, buffer, sizeof(buffer));
52
53 PJ_TEST_EQ(pj_fifobuf_capacity(&fifo), SIZE-SZ, NULL, return -11);
54 PJ_TEST_EQ(pj_fifobuf_available_size(&fifo), SIZE-SZ, NULL, return -12);
55
56 p0 = pj_fifobuf_alloc(&fifo, 16);
57 p1 = pj_fifobuf_alloc(&fifo, 4);
58 PJ_UNUSED_ARG(p1);
59
60 pj_fifobuf_free(&fifo, p0);
61
62 PJ_TEST_EQ( pj_fifobuf_available_size(&fifo), 16, NULL, return -14);
63 PJ_TEST_EQ( pj_memcmp(before, zero, sizeof(zero)), 0, NULL, return -18);
64 PJ_TEST_EQ( pj_memcmp(after, zero, sizeof(zero)), 0, NULL, return -19);
65 return 0;
66 }
67
68 static int fifobuf_rolling_test()
69 {
70 enum {
71 REPEAT=2048,
72 N=100,
73 MIN_SIZE = sizeof(pj_list),
74 MAX_SIZE = 64,
75 SIZE=(MIN_SIZE+MAX_SIZE)/2*N,
76 };
77 pj_list chunks;
78 char buffer[SIZE];
79 pj_fifobuf_t fifo;
80 unsigned rep;
81
82 pj_fifobuf_init(&fifo, buffer, sizeof(buffer));
83 pj_list_init(&chunks);
84
85 PJ_TEST_EQ(pj_fifobuf_capacity(&fifo), SIZE-SZ, NULL, return -300);
86 PJ_TEST_EQ(pj_fifobuf_available_size(&fifo), SIZE-SZ, NULL, return -310);
87
88 /* Repeat the test */
89 for (rep=0; rep<REPEAT; rep++) {
90 pj_list *chunk;
91 int i, n;
92
93 /* Allocate random number of chunks */
94 n = N/2 + (pj_rand() % (N/2)) - (int)pj_list_size(&chunks);
95 for (i=0; i<n; ++i) {
96 unsigned size = MIN_SIZE + (pj_rand() % (MAX_SIZE-MIN_SIZE));
97 chunk = (pj_list*)pj_fifobuf_alloc(&fifo, size);
98 if (!chunk)
99 break;
100 pj_memset(chunk, 0x44, size);
101 pj_list_push_back(&chunks, chunk);
102 }
103
104 PJ_TEST_GTE(pj_list_size(&chunks), N/2, NULL, return -330);
105
106 /* Free cunks, leave some chunks for the next repeat */
107 n = N/4 + (pj_rand() % (N/4));
108 while ((int)pj_list_size(&chunks) > n) {
109 chunk = chunks.next;
110 pj_list_erase(chunk);
111 pj_fifobuf_free(&fifo, chunk);
112 }
113 }
114
115 while (pj_list_size(&chunks)) {
116 pj_list *chunk = chunks.next;
117 pj_list_erase(chunk);
118 pj_fifobuf_free(&fifo, chunk);
119 }
120
121 PJ_TEST_EQ(pj_fifobuf_capacity(&fifo), SIZE-SZ, NULL, return -350);
122 PJ_TEST_EQ(pj_fifobuf_available_size(&fifo), SIZE-SZ, NULL, return -360);
123
124 return 0;
125 }
126
127 static int fifobuf_misc_test()
128 {
129 enum { SIZE = 1024, MAX_ENTRIES = 128,
130 MIN_SIZE = 4, MAX_SIZE = 64,
131 LOOP=10000 };
132 pj_pool_t *pool;
133 pj_fifobuf_t fifo;
134 void *entries[MAX_ENTRIES];
135 void *buffer;
136 int i;
137
138 pool = pj_pool_create(mem, NULL, SIZE+256, 0, NULL);
139 PJ_TEST_NOT_NULL(pool, NULL, return -10);
140
141 buffer = pj_pool_alloc(pool, SIZE);
142 PJ_TEST_NOT_NULL(buffer, NULL, return -20);
143
144 pj_fifobuf_init (&fifo, buffer, SIZE);
145
146 /* Capacity and maximum alloc */
147 PJ_TEST_EQ(pj_fifobuf_capacity(&fifo), SIZE-SZ, NULL, return -21);
148 PJ_TEST_EQ(pj_fifobuf_available_size(&fifo), SIZE-SZ, NULL, return -22);
149
150 entries[0] = pj_fifobuf_alloc(&fifo, SIZE-SZ);
151 PJ_TEST_NOT_NULL(entries[0], NULL, return -23);
152
153 pj_fifobuf_free(&fifo, entries[0]);
154 PJ_TEST_EQ(pj_fifobuf_capacity(&fifo), SIZE-SZ, NULL, return -21);
155 PJ_TEST_EQ(pj_fifobuf_available_size(&fifo), SIZE-SZ, NULL, return -22);
156
157 /* Alignment test */
158 for (i=0; i<30; ++i) {
159 entries[i] = pj_fifobuf_alloc(&fifo, i+1);
160 PJ_TEST_NOT_NULL(entries[i], NULL, return -50);
161 //fifobuf is no longer aligned.
162 //PJ_TEST_EQ(((pj_size_t)entries[i]) % sizeof(unsigned), 0, -60,
163 // "alignment error");
164 }
165 for (i=0; i<30; ++i) {
166 PJ_TEST_SUCCESS( pj_fifobuf_free(&fifo, entries[i]), NULL, return -70);
167 }
168
169 PJ_TEST_EQ(pj_fifobuf_capacity(&fifo), SIZE-SZ, NULL, return -31);
170 PJ_TEST_EQ(pj_fifobuf_available_size(&fifo), SIZE-SZ, NULL, return -32);
171
172 /* alloc() and free() */
173 for (i=0; i<LOOP*MAX_ENTRIES; ++i) {
174 int size;
175 int c, f;
176 c = i%2;
177 f = (i+1)%2;
178 do {
179 size = MIN_SIZE+(pj_rand() % MAX_SIZE);
180 entries[c] = pj_fifobuf_alloc (&fifo, size);
181 } while (entries[c] == 0);
182 if ( i!=0) {
183 pj_fifobuf_free(&fifo, entries[f]);
184 }
185 }
186 if (entries[(i+1)%2])
187 pj_fifobuf_free(&fifo, entries[(i+1)%2]);
188
189 PJ_TEST_EQ(pj_fifobuf_available_size(&fifo), SIZE-SZ, NULL, return -42);
190
191 /* Fifobuf must be empty (==not used) */
192 PJ_TEST_EQ(pj_fifobuf_available_size(&fifo), SIZE-SZ, NULL, return -62);
193
194 pj_pool_release(pool);
195 return 0;
196 }
197
198 int fifobuf_test()
199 {
200 int rc;
201
202 rc = fifobuf_size_test();
203 if (rc != 0)
204 return rc;
205
206 rc = fifobuf_rolling_test();
207 if (rc)
208 return rc;
209
210 rc = fifobuf_misc_test();
211 if (rc)
212 return rc;
213
214 return 0;
215 }
216
217 #endif /* INCLUDE_FIFOBUF_TEST */
218
219