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