]> code.delx.au - pulseaudio/blob - src/tests/memblockq-test.c
memblockq: Improve debuggability by storing a name and a sample spec.
[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 q = pa_memblock_acquire(chunk->memblock);
40 for (e = (char*) q + chunk->index, n = 0; n < chunk->length; n++, e++)
41 printf("%c", *e);
42 pa_memblock_release(chunk->memblock);
43 }
44
45 static void dump(pa_memblockq *bq) {
46 pa_memchunk out;
47
48 pa_assert(bq);
49
50 /* First let's dump this as fixed block */
51 printf("FIXED >");
52 pa_memblockq_peek_fixed_size(bq, 64, &out);
53 dump_chunk(&out);
54 pa_memblock_unref(out.memblock);
55 printf("<\n");
56
57 /* Then let's dump the queue manually */
58 printf("MANUAL>");
59
60 for (;;) {
61 if (pa_memblockq_peek(bq, &out) < 0)
62 break;
63
64 dump_chunk(&out);
65 pa_memblock_unref(out.memblock);
66 pa_memblockq_drop(bq, out.length);
67 }
68
69 printf("<\n");
70 }
71
72 int main(int argc, char *argv[]) {
73 int ret;
74
75 pa_mempool *p;
76 pa_memblockq *bq;
77 pa_memchunk chunk1, chunk2, chunk3, chunk4;
78 pa_memchunk silence;
79 pa_sample_spec ss = {
80 .format = PA_SAMPLE_S16LE,
81 .rate = 48000,
82 .channels = 1
83 };
84
85 pa_log_set_level(PA_LOG_DEBUG);
86
87 p = pa_mempool_new(FALSE, 0);
88
89 silence.memblock = pa_memblock_new_fixed(p, (char*) "__", 2, 1);
90 assert(silence.memblock);
91 silence.index = 0;
92 silence.length = pa_memblock_get_length(silence.memblock);
93
94 bq = pa_memblockq_new("test memblockq", 0, 200, 10, &ss, 4, 4, 40, &silence);
95 assert(bq);
96
97 chunk1.memblock = pa_memblock_new_fixed(p, (char*) "11", 2, 1);
98 chunk1.index = 0;
99 chunk1.length = 2;
100 assert(chunk1.memblock);
101
102 chunk2.memblock = pa_memblock_new_fixed(p, (char*) "XX22", 4, 1);
103 chunk2.index = 2;
104 chunk2.length = 2;
105 assert(chunk2.memblock);
106
107 chunk3.memblock = pa_memblock_new_fixed(p, (char*) "3333", 4, 1);
108 chunk3.index = 0;
109 chunk3.length = 4;
110 assert(chunk3.memblock);
111
112 chunk4.memblock = pa_memblock_new_fixed(p, (char*) "44444444", 8, 1);
113 chunk4.index = 0;
114 chunk4.length = 8;
115 assert(chunk4.memblock);
116
117 ret = pa_memblockq_push(bq, &chunk1);
118 assert(ret == 0);
119
120 ret = pa_memblockq_push(bq, &chunk2);
121 assert(ret == 0);
122
123 ret = pa_memblockq_push(bq, &chunk3);
124 assert(ret == 0);
125
126 ret = pa_memblockq_push(bq, &chunk4);
127 assert(ret == 0);
128
129 pa_memblockq_seek(bq, -6, 0, TRUE);
130 ret = pa_memblockq_push(bq, &chunk3);
131 assert(ret == 0);
132
133 pa_memblockq_seek(bq, -2, 0, TRUE);
134 ret = pa_memblockq_push(bq, &chunk1);
135 assert(ret == 0);
136
137 pa_memblockq_seek(bq, -10, 0, TRUE);
138 ret = pa_memblockq_push(bq, &chunk4);
139 assert(ret == 0);
140
141 pa_memblockq_seek(bq, 10, 0, TRUE);
142
143 ret = pa_memblockq_push(bq, &chunk1);
144 assert(ret == 0);
145
146 pa_memblockq_seek(bq, -6, 0, TRUE);
147 ret = pa_memblockq_push(bq, &chunk2);
148 assert(ret == 0);
149
150 /* Test splitting */
151 pa_memblockq_seek(bq, -12, 0, TRUE);
152 ret = pa_memblockq_push(bq, &chunk1);
153 assert(ret == 0);
154
155 pa_memblockq_seek(bq, 20, 0, TRUE);
156
157 /* Test merging */
158 ret = pa_memblockq_push(bq, &chunk3);
159 assert(ret == 0);
160 pa_memblockq_seek(bq, -2, 0, TRUE);
161
162 chunk3.index += 2;
163 chunk3.length -= 2;
164 ret = pa_memblockq_push(bq, &chunk3);
165 assert(ret == 0);
166
167 pa_memblockq_seek(bq, 30, PA_SEEK_RELATIVE, TRUE);
168
169 dump(bq);
170
171 pa_memblockq_rewind(bq, 52);
172
173 dump(bq);
174
175 pa_memblockq_free(bq);
176 pa_memblock_unref(silence.memblock);
177 pa_memblock_unref(chunk1.memblock);
178 pa_memblock_unref(chunk2.memblock);
179 pa_memblock_unref(chunk3.memblock);
180 pa_memblock_unref(chunk4.memblock);
181
182 pa_mempool_free(p);
183
184 return 0;
185 }