sink, source: Really set the fixed latency in set_fixed_latency_within_thread(),...
[platform/upstream/pulseaudio.git] / 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 <stdio.h>
26 #include <signal.h>
27
28 #include <check.h>
29
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>
35
36 #include <pulse/xmalloc.h>
37
38 static const char *fixed[] = {
39     "1122444411441144__22__11______3333______________________________",
40     "__________________3333__________________________________________"
41 };
42 static const char *manual[] = {
43     "1122444411441144__22__11______3333______________________________",
44     "__________________3333______________________________"
45 };
46
47 static void dump_chunk(const pa_memchunk *chunk, pa_strbuf *buf) {
48     size_t n;
49     void *q;
50     char *e;
51
52     fail_unless(chunk != NULL);
53
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);
58     }
59     pa_memblock_release(chunk->memblock);
60 }
61
62 static void dump(pa_memblockq *bq, int n) {
63     pa_memchunk out;
64     pa_strbuf *buf;
65     char *str;
66
67     pa_assert(bq);
68
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]));
77     pa_xfree(str);
78     fprintf(stderr, "<\n");
79
80     /* Then let's dump the queue manually */
81     fprintf(stderr, "MANUAL>");
82
83     buf = pa_strbuf_new();
84     for (;;) {
85         if (pa_memblockq_peek(bq, &out) < 0)
86             break;
87
88         dump_chunk(&out, buf);
89         pa_memblock_unref(out.memblock);
90         pa_memblockq_drop(bq, out.length);
91     }
92     str = pa_strbuf_tostring_free(buf);
93     fail_unless(pa_streq(str, manual[n]));
94     pa_xfree(str);
95     fprintf(stderr, "<\n");
96 }
97
98 START_TEST (memblockq_test) {
99     int ret;
100
101     pa_mempool *p;
102     pa_memblockq *bq;
103     pa_memchunk chunk1, chunk2, chunk3, chunk4;
104     pa_memchunk silence;
105     pa_sample_spec ss = {
106         .format = PA_SAMPLE_S16LE,
107         .rate = 48000,
108         .channels = 1
109     };
110
111     pa_log_set_level(PA_LOG_DEBUG);
112
113     p = pa_mempool_new(FALSE, 0);
114
115     silence.memblock = pa_memblock_new_fixed(p, (char*) "__", 2, 1);
116     fail_unless(silence.memblock != NULL);
117
118     silence.index = 0;
119     silence.length = pa_memblock_get_length(silence.memblock);
120
121     bq = pa_memblockq_new("test memblockq", 0, 200, 10, &ss, 4, 4, 40, &silence);
122     fail_unless(bq != NULL);
123
124     chunk1.memblock = pa_memblock_new_fixed(p, (char*) "11", 2, 1);
125     fail_unless(chunk1.memblock != NULL);
126
127     chunk1.index = 0;
128     chunk1.length = 2;
129
130     chunk2.memblock = pa_memblock_new_fixed(p, (char*) "XX22", 4, 1);
131     fail_unless(chunk2.memblock != NULL);
132
133     chunk2.index = 2;
134     chunk2.length = 2;
135
136     chunk3.memblock = pa_memblock_new_fixed(p, (char*) "3333", 4, 1);
137     fail_unless(chunk3.memblock != NULL);
138
139     chunk3.index = 0;
140     chunk3.length = 4;
141
142     chunk4.memblock = pa_memblock_new_fixed(p, (char*) "44444444", 8, 1);
143     fail_unless(chunk4.memblock != NULL);
144
145     chunk4.index = 0;
146     chunk4.length = 8;
147
148     ret = pa_memblockq_push(bq, &chunk1);
149     fail_unless(ret == 0);
150
151     ret = pa_memblockq_push(bq, &chunk2);
152     fail_unless(ret == 0);
153
154     ret = pa_memblockq_push(bq, &chunk3);
155     fail_unless(ret == 0);
156
157     ret = pa_memblockq_push(bq, &chunk4);
158     fail_unless(ret == 0);
159
160     pa_memblockq_seek(bq, -6, 0, TRUE);
161     ret = pa_memblockq_push(bq, &chunk3);
162     fail_unless(ret == 0);
163
164     pa_memblockq_seek(bq, -2, 0, TRUE);
165     ret = pa_memblockq_push(bq, &chunk1);
166     fail_unless(ret == 0);
167
168     pa_memblockq_seek(bq, -10, 0, TRUE);
169     ret = pa_memblockq_push(bq, &chunk4);
170     fail_unless(ret == 0);
171
172     pa_memblockq_seek(bq, 10, 0, TRUE);
173
174     ret = pa_memblockq_push(bq, &chunk1);
175     fail_unless(ret == 0);
176
177     pa_memblockq_seek(bq, -6, 0, TRUE);
178     ret = pa_memblockq_push(bq, &chunk2);
179     fail_unless(ret == 0);
180
181     /* Test splitting */
182     pa_memblockq_seek(bq, -12, 0, TRUE);
183     ret = pa_memblockq_push(bq, &chunk1);
184     fail_unless(ret == 0);
185
186     pa_memblockq_seek(bq, 20, 0, TRUE);
187
188     /* Test merging */
189     ret = pa_memblockq_push(bq, &chunk3);
190     fail_unless(ret == 0);
191     pa_memblockq_seek(bq, -2, 0, TRUE);
192
193     chunk3.index += 2;
194     chunk3.length -= 2;
195     ret = pa_memblockq_push(bq, &chunk3);
196     fail_unless(ret == 0);
197
198     pa_memblockq_seek(bq, 30, PA_SEEK_RELATIVE, TRUE);
199
200     dump(bq, 0);
201
202     pa_memblockq_rewind(bq, 52);
203
204     dump(bq, 1);
205
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);
212
213     pa_mempool_free(p);
214 }
215 END_TEST
216
217 int main(int argc, char *argv[]) {
218     int failed = 0;
219     Suite *s;
220     TCase *tc;
221     SRunner *sr;
222
223     s = suite_create("Memblock Queue");
224     tc = tcase_create("memblockq");
225     tcase_add_test(tc, memblockq_test);
226     suite_add_tcase(s, tc);
227
228     sr = srunner_create(s);
229     srunner_run_all(sr, CK_NORMAL);
230     failed = srunner_ntests_failed(sr);
231     srunner_free(sr);
232
233     return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
234 }