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