]> code.delx.au - pulseaudio/blob - src/tests/memblockq-test.c
memblockq: implement new call pa_memblockq_peek_fixed_size()
[pulseaudio] / src / tests / memblockq-test.c
1 /***
2 This file is part of PulseAudio.
3
4 PulseAudio is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License,
7 or (at your option) any later version.
8
9 PulseAudio is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with PulseAudio; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <assert.h>
26 #include <stdio.h>
27 #include <signal.h>
28
29 #include <pulsecore/memblockq.h>
30 #include <pulsecore/log.h>
31
32 static void dump_chunk(const pa_memchunk *chunk) {
33 size_t n;
34 void *q;
35 char *e;
36
37 pa_assert(chunk);
38
39 printf("[");
40
41 q = pa_memblock_acquire(chunk->memblock);
42 for (e = (char*) q + chunk->index, n = 0; n < chunk->length; n++, e++)
43 printf("%c", *e);
44 pa_memblock_release(chunk->memblock);
45
46 printf("]");
47 }
48
49 static void dump(pa_memblockq *bq) {
50 pa_memchunk out;
51
52 pa_assert(bq);
53
54 /* First let's dump this as fixed block */
55 printf("FIXED >");
56 pa_memblockq_peek_fixed_size(bq, 64, &out);
57 dump_chunk(&out);
58 pa_memblock_unref(out.memblock);
59 printf("<\n");
60
61 /* Then let's dump the queue manually */
62 printf("MANUAL>");
63
64 for (;;) {
65 if (pa_memblockq_peek(bq, &out) < 0)
66 break;
67
68 dump_chunk(&out);
69 pa_memblock_unref(out.memblock);
70 pa_memblockq_drop(bq, out.length);
71 }
72
73 printf("<\n");
74 }
75
76 int main(int argc, char *argv[]) {
77 int ret;
78
79 pa_mempool *p;
80 pa_memblockq *bq;
81 pa_memchunk chunk1, chunk2, chunk3, chunk4;
82 pa_memchunk silence;
83
84 pa_log_set_level(PA_LOG_DEBUG);
85
86 p = pa_mempool_new(FALSE, 0);
87
88 silence.memblock = pa_memblock_new_fixed(p, (char*) "__", 2, 1);
89 assert(silence.memblock);
90 silence.index = 0;
91 silence.length = pa_memblock_get_length(silence.memblock);
92
93 bq = pa_memblockq_new(0, 200, 10, 2, 4, 4, 40, &silence);
94 assert(bq);
95
96 chunk1.memblock = pa_memblock_new_fixed(p, (char*) "11", 2, 1);
97 chunk1.index = 0;
98 chunk1.length = 2;
99 assert(chunk1.memblock);
100
101 chunk2.memblock = pa_memblock_new_fixed(p, (char*) "XX22", 4, 1);
102 chunk2.index = 2;
103 chunk2.length = 2;
104 assert(chunk2.memblock);
105
106 chunk3.memblock = pa_memblock_new_fixed(p, (char*) "3333", 4, 1);
107 chunk3.index = 0;
108 chunk3.length = 4;
109 assert(chunk3.memblock);
110
111 chunk4.memblock = pa_memblock_new_fixed(p, (char*) "44444444", 8, 1);
112 chunk4.index = 0;
113 chunk4.length = 8;
114 assert(chunk4.memblock);
115
116 ret = pa_memblockq_push(bq, &chunk1);
117 assert(ret == 0);
118
119 ret = pa_memblockq_push(bq, &chunk2);
120 assert(ret == 0);
121
122 ret = pa_memblockq_push(bq, &chunk3);
123 assert(ret == 0);
124
125 ret = pa_memblockq_push(bq, &chunk4);
126 assert(ret == 0);
127
128 pa_memblockq_seek(bq, -6, 0, TRUE);
129 ret = pa_memblockq_push(bq, &chunk3);
130 assert(ret == 0);
131
132 pa_memblockq_seek(bq, -2, 0, TRUE);
133 ret = pa_memblockq_push(bq, &chunk1);
134 assert(ret == 0);
135
136 pa_memblockq_seek(bq, -10, 0, TRUE);
137 ret = pa_memblockq_push(bq, &chunk4);
138 assert(ret == 0);
139
140 pa_memblockq_seek(bq, 10, 0, TRUE);
141
142 ret = pa_memblockq_push(bq, &chunk1);
143 assert(ret == 0);
144
145 pa_memblockq_seek(bq, -6, 0, TRUE);
146 ret = pa_memblockq_push(bq, &chunk2);
147 assert(ret == 0);
148
149 /* Test splitting */
150 pa_memblockq_seek(bq, -12, 0, TRUE);
151 ret = pa_memblockq_push(bq, &chunk1);
152 assert(ret == 0);
153
154 pa_memblockq_seek(bq, 20, 0, TRUE);
155
156 /* Test merging */
157 ret = pa_memblockq_push(bq, &chunk3);
158 assert(ret == 0);
159 pa_memblockq_seek(bq, -2, 0, TRUE);
160
161 chunk3.index += 2;
162 chunk3.length -= 2;
163 ret = pa_memblockq_push(bq, &chunk3);
164 assert(ret == 0);
165
166 pa_memblockq_seek(bq, 30, PA_SEEK_RELATIVE, TRUE);
167
168 dump(bq);
169
170 pa_memblockq_rewind(bq, 52);
171
172 dump(bq);
173
174 pa_memblockq_free(bq);
175 pa_memblock_unref(silence.memblock);
176 pa_memblock_unref(chunk1.memblock);
177 pa_memblock_unref(chunk2.memblock);
178 pa_memblock_unref(chunk3.memblock);
179 pa_memblock_unref(chunk4.memblock);
180
181 pa_mempool_free(p);
182
183 return 0;
184 }