]> code.delx.au - pulseaudio/blob - src/tests/memblockq-test.c
Merge HUGE set of changes temporarily into a branch, to allow me to move them from...
[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
30 #include <pulsecore/memblockq.h>
31 #include <pulsecore/log.h>
32
33 int main(int argc, char *argv[]) {
34 int ret;
35
36 pa_mempool *p;
37 pa_memblockq *bq;
38 pa_memchunk chunk1, chunk2, chunk3, chunk4;
39 pa_memblock *silence;
40
41 pa_log_set_maximal_level(PA_LOG_DEBUG);
42
43 p = pa_mempool_new(0);
44
45 silence = pa_memblock_new_fixed(p, (char*) "__", 2, 1);
46 assert(silence);
47
48 bq = pa_memblockq_new(0, 40, 10, 2, 4, 4, silence);
49 assert(bq);
50
51 chunk1.memblock = pa_memblock_new_fixed(p, (char*) "AA", 2, 1);
52 chunk1.index = 0;
53 chunk1.length = 2;
54 assert(chunk1.memblock);
55
56 chunk2.memblock = pa_memblock_new_fixed(p, (char*) "TTBB", 4, 1);
57 chunk2.index = 2;
58 chunk2.length = 2;
59 assert(chunk2.memblock);
60
61 chunk3.memblock = pa_memblock_new_fixed(p, (char*) "ZZZZ", 4, 1);
62 chunk3.index = 0;
63 chunk3.length = 4;
64 assert(chunk3.memblock);
65
66 chunk4.memblock = pa_memblock_new_fixed(p, (char*) "KKKKKKKK", 8, 1);
67 chunk4.index = 0;
68 chunk4.length = 8;
69 assert(chunk4.memblock);
70
71 ret = pa_memblockq_push(bq, &chunk1);
72 assert(ret == 0);
73
74 ret = pa_memblockq_push(bq, &chunk1);
75 assert(ret == 0);
76
77 ret = pa_memblockq_push(bq, &chunk2);
78 assert(ret == 0);
79
80 ret = pa_memblockq_push(bq, &chunk2);
81 assert(ret == 0);
82
83 pa_memblockq_seek(bq, -6, 0);
84 ret = pa_memblockq_push(bq, &chunk3);
85 assert(ret == 0);
86
87 pa_memblockq_seek(bq, -2, 0);
88 ret = pa_memblockq_push(bq, &chunk3);
89 assert(ret == 0);
90
91 pa_memblockq_seek(bq, -10, 0);
92 ret = pa_memblockq_push(bq, &chunk4);
93 assert(ret == 0);
94
95 pa_memblockq_seek(bq, 10, 0);
96
97 ret = pa_memblockq_push(bq, &chunk1);
98 assert(ret == 0);
99
100 pa_memblockq_seek(bq, -6, 0);
101 ret = pa_memblockq_push(bq, &chunk2);
102 assert(ret == 0);
103
104 /* Test splitting */
105 pa_memblockq_seek(bq, -12, 0);
106 ret = pa_memblockq_push(bq, &chunk1);
107 assert(ret == 0);
108
109 pa_memblockq_seek(bq, 20, 0);
110
111 /* Test merging */
112 ret = pa_memblockq_push(bq, &chunk3);
113 assert(ret == 0);
114 pa_memblockq_seek(bq, -2, 0);
115
116 chunk3.index += 2;
117 chunk3.length -= 2;
118
119 ret = pa_memblockq_push(bq, &chunk3);
120 assert(ret == 0);
121
122 printf(">");
123
124 pa_memblockq_shorten(bq, 6);
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, 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 }