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