]>
code.delx.au - pulseaudio/blob - src/tests/memblockq-test.c
2 This file is part of PulseAudio.
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.
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.
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
30 #include <pulsecore/memblockq.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/strbuf.h>
34 #include <pulsecore/core-util.h>
36 #include <pulse/xmalloc.h>
38 static const char *fixed
[] = {
39 "1122444411441144__22__11______3333______________________________",
40 "__________________3333__________________________________________"
42 static const char *manual
[] = {
43 "1122444411441144__22__11______3333______________________________",
44 "__________________3333______________________________"
47 static void dump_chunk(const pa_memchunk
*chunk
, pa_strbuf
*buf
) {
52 fail_unless(chunk
!= NULL
);
54 q
= pa_memblock_acquire(chunk
->memblock
);
55 for (e
= (char*) q
+ chunk
->index
, n
= 0; n
< chunk
->length
; n
++, e
++) {
56 fprintf(stderr
, "%c", *e
);
57 pa_strbuf_putc(buf
, *e
);
59 pa_memblock_release(chunk
->memblock
);
62 static void dump(pa_memblockq
*bq
, int n
) {
69 /* First let's dump this as fixed block */
70 fprintf(stderr
, "FIXED >");
71 pa_memblockq_peek_fixed_size(bq
, 64, &out
);
72 buf
= pa_strbuf_new();
73 dump_chunk(&out
, buf
);
74 pa_memblock_unref(out
.memblock
);
75 str
= pa_strbuf_tostring_free(buf
);
76 fail_unless(pa_streq(str
, fixed
[n
]));
78 fprintf(stderr
, "<\n");
80 /* Then let's dump the queue manually */
81 fprintf(stderr
, "MANUAL>");
83 buf
= pa_strbuf_new();
85 if (pa_memblockq_peek(bq
, &out
) < 0)
88 dump_chunk(&out
, buf
);
89 pa_memblock_unref(out
.memblock
);
90 pa_memblockq_drop(bq
, out
.length
);
92 str
= pa_strbuf_tostring_free(buf
);
93 fail_unless(pa_streq(str
, manual
[n
]));
95 fprintf(stderr
, "<\n");
98 START_TEST (memblockq_test
) {
103 pa_memchunk chunk1
, chunk2
, chunk3
, chunk4
;
105 pa_sample_spec ss
= {
106 .format
= PA_SAMPLE_S16LE
,
111 pa_log_set_level(PA_LOG_DEBUG
);
113 p
= pa_mempool_new(false, 0);
115 silence
.memblock
= pa_memblock_new_fixed(p
, (char*) "__", 2, 1);
116 fail_unless(silence
.memblock
!= NULL
);
119 silence
.length
= pa_memblock_get_length(silence
.memblock
);
121 bq
= pa_memblockq_new("test memblockq", 0, 200, 10, &ss
, 4, 4, 40, &silence
);
122 fail_unless(bq
!= NULL
);
124 chunk1
.memblock
= pa_memblock_new_fixed(p
, (char*) "11", 2, 1);
125 fail_unless(chunk1
.memblock
!= NULL
);
130 chunk2
.memblock
= pa_memblock_new_fixed(p
, (char*) "XX22", 4, 1);
131 fail_unless(chunk2
.memblock
!= NULL
);
136 chunk3
.memblock
= pa_memblock_new_fixed(p
, (char*) "3333", 4, 1);
137 fail_unless(chunk3
.memblock
!= NULL
);
142 chunk4
.memblock
= pa_memblock_new_fixed(p
, (char*) "44444444", 8, 1);
143 fail_unless(chunk4
.memblock
!= NULL
);
148 ret
= pa_memblockq_push(bq
, &chunk1
);
149 fail_unless(ret
== 0);
151 ret
= pa_memblockq_push(bq
, &chunk2
);
152 fail_unless(ret
== 0);
154 ret
= pa_memblockq_push(bq
, &chunk3
);
155 fail_unless(ret
== 0);
157 ret
= pa_memblockq_push(bq
, &chunk4
);
158 fail_unless(ret
== 0);
160 pa_memblockq_seek(bq
, -6, 0, true);
161 ret
= pa_memblockq_push(bq
, &chunk3
);
162 fail_unless(ret
== 0);
164 pa_memblockq_seek(bq
, -2, 0, true);
165 ret
= pa_memblockq_push(bq
, &chunk1
);
166 fail_unless(ret
== 0);
168 pa_memblockq_seek(bq
, -10, 0, true);
169 ret
= pa_memblockq_push(bq
, &chunk4
);
170 fail_unless(ret
== 0);
172 pa_memblockq_seek(bq
, 10, 0, true);
174 ret
= pa_memblockq_push(bq
, &chunk1
);
175 fail_unless(ret
== 0);
177 pa_memblockq_seek(bq
, -6, 0, true);
178 ret
= pa_memblockq_push(bq
, &chunk2
);
179 fail_unless(ret
== 0);
182 pa_memblockq_seek(bq
, -12, 0, true);
183 ret
= pa_memblockq_push(bq
, &chunk1
);
184 fail_unless(ret
== 0);
186 pa_memblockq_seek(bq
, 20, 0, true);
189 ret
= pa_memblockq_push(bq
, &chunk3
);
190 fail_unless(ret
== 0);
191 pa_memblockq_seek(bq
, -2, 0, true);
195 ret
= pa_memblockq_push(bq
, &chunk3
);
196 fail_unless(ret
== 0);
198 pa_memblockq_seek(bq
, 30, PA_SEEK_RELATIVE
, true);
202 pa_memblockq_rewind(bq
, 52);
206 pa_memblockq_free(bq
);
207 pa_memblock_unref(silence
.memblock
);
208 pa_memblock_unref(chunk1
.memblock
);
209 pa_memblock_unref(chunk2
.memblock
);
210 pa_memblock_unref(chunk3
.memblock
);
211 pa_memblock_unref(chunk4
.memblock
);
217 int main(int argc
, char *argv
[]) {
223 s
= suite_create("Memblock Queue");
224 tc
= tcase_create("memblockq");
225 tcase_add_test(tc
, memblockq_test
);
226 suite_add_tcase(s
, tc
);
228 sr
= srunner_create(s
);
229 srunner_run_all(sr
, CK_NORMAL
);
230 failed
= srunner_ntests_failed(sr
);
233 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;