Merge commit 'origin/master-tx'
[profile/ivi/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 <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(pa_memblockq *bq) {
33     printf(">");
34
35     for (;;) {
36         pa_memchunk out;
37         char *e;
38         size_t n;
39         void *q;
40
41         if (pa_memblockq_peek(bq, &out) < 0)
42             break;
43
44         q = pa_memblock_acquire(out.memblock);
45         for (e = (char*) q + out.index, n = 0; n < out.length; n++)
46             printf("%c", *e);
47         pa_memblock_release(out.memblock);
48
49         pa_memblock_unref(out.memblock);
50         pa_memblockq_drop(bq, out.length);
51     }
52
53     printf("<\n");
54 }
55
56 int main(int argc, char *argv[]) {
57     int ret;
58
59     pa_mempool *p;
60     pa_memblockq *bq;
61     pa_memchunk chunk1, chunk2, chunk3, chunk4;
62     pa_memchunk silence;
63
64     pa_log_set_level(PA_LOG_DEBUG);
65
66     p = pa_mempool_new(FALSE, 0);
67
68     silence.memblock = pa_memblock_new_fixed(p, (char*)  "__", 2, 1);
69     assert(silence.memblock);
70     silence.index = 0;
71     silence.length = pa_memblock_get_length(silence.memblock);
72
73     bq = pa_memblockq_new(0, 40, 10, 2, 4, 4, 40, &silence);
74     assert(bq);
75
76     chunk1.memblock = pa_memblock_new_fixed(p, (char*) "11", 2, 1);
77     chunk1.index = 0;
78     chunk1.length = 2;
79     assert(chunk1.memblock);
80
81     chunk2.memblock = pa_memblock_new_fixed(p, (char*) "XX22", 4, 1);
82     chunk2.index = 2;
83     chunk2.length = 2;
84     assert(chunk2.memblock);
85
86     chunk3.memblock = pa_memblock_new_fixed(p, (char*) "3333", 4, 1);
87     chunk3.index = 0;
88     chunk3.length = 4;
89     assert(chunk3.memblock);
90
91     chunk4.memblock = pa_memblock_new_fixed(p, (char*) "44444444", 8, 1);
92     chunk4.index = 0;
93     chunk4.length = 8;
94     assert(chunk4.memblock);
95
96     ret = pa_memblockq_push(bq, &chunk1);
97     assert(ret == 0);
98
99     ret = pa_memblockq_push(bq, &chunk2);
100     assert(ret == 0);
101
102     ret = pa_memblockq_push(bq, &chunk3);
103     assert(ret == 0);
104
105     ret = pa_memblockq_push(bq, &chunk4);
106     assert(ret == 0);
107
108     pa_memblockq_seek(bq, -6, 0, TRUE);
109     ret = pa_memblockq_push(bq, &chunk3);
110     assert(ret == 0);
111
112     pa_memblockq_seek(bq, -2, 0, TRUE);
113     ret = pa_memblockq_push(bq, &chunk1);
114     assert(ret == 0);
115
116     pa_memblockq_seek(bq, -10, 0, TRUE);
117     ret = pa_memblockq_push(bq, &chunk4);
118     assert(ret == 0);
119
120     pa_memblockq_seek(bq, 10, 0, TRUE);
121
122     ret = pa_memblockq_push(bq, &chunk1);
123     assert(ret == 0);
124
125     pa_memblockq_seek(bq, -6, 0, TRUE);
126     ret = pa_memblockq_push(bq, &chunk2);
127     assert(ret == 0);
128
129     /* Test splitting */
130     pa_memblockq_seek(bq, -12, 0, TRUE);
131     ret = pa_memblockq_push(bq, &chunk1);
132     assert(ret == 0);
133
134     pa_memblockq_seek(bq, 20, 0, TRUE);
135
136     /* Test merging */
137     ret = pa_memblockq_push(bq, &chunk3);
138     assert(ret == 0);
139     pa_memblockq_seek(bq, -2, 0, TRUE);
140
141     chunk3.index += 2;
142     chunk3.length -= 2;
143     ret = pa_memblockq_push(bq, &chunk3);
144     assert(ret == 0);
145
146     pa_memblockq_seek(bq, 30, PA_SEEK_RELATIVE, TRUE);
147
148     dump(bq);
149
150     pa_memblockq_rewind(bq, 52);
151
152     dump(bq);
153
154     pa_memblockq_free(bq);
155     pa_memblock_unref(silence.memblock);
156     pa_memblock_unref(chunk1.memblock);
157     pa_memblock_unref(chunk2.memblock);
158     pa_memblock_unref(chunk3.memblock);
159     pa_memblock_unref(chunk4.memblock);
160
161     pa_mempool_free(p);
162
163     return 0;
164 }