Remove unnecessary #includes
[profile/ivi/pulseaudio-panda.git] / src / tests / mcalign-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
6   published by the Free Software Foundation; either version 2.1 of the
7   License, 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   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License 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 <assert.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <time.h>
31
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/mcalign.h>
34
35 /* A simple program for testing pa_mcalign */
36
37 int main(int argc, char *argv[]) {
38     pa_mempool *p;
39     pa_mcalign *a;
40     pa_memchunk c;
41
42     p = pa_mempool_new(FALSE, 0);
43
44     a = pa_mcalign_new(11);
45
46     pa_memchunk_reset(&c);
47
48     srand((unsigned) time(NULL));
49
50     for (;;) {
51         ssize_t r;
52         size_t l;
53
54         if (!c.memblock) {
55             c.memblock = pa_memblock_new(p, 2048);
56             c.index = c.length = 0;
57         }
58
59         assert(c.index < pa_memblock_get_length(c.memblock));
60
61         l = pa_memblock_get_length(c.memblock) - c.index;
62
63         l = l <= 1 ? l : (size_t) rand() % (l-1) +1;
64
65         p = pa_memblock_acquire(c.memblock);
66
67         if ((r = read(STDIN_FILENO, (uint8_t*) p + c.index, l)) <= 0) {
68             pa_memblock_release(c.memblock);
69             fprintf(stderr, "read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
70             break;
71         }
72
73         pa_memblock_release(c.memblock);
74
75         c.length = (size_t) r;
76         pa_mcalign_push(a, &c);
77         fprintf(stderr, "Read %ld bytes\n", (long)r);
78
79         c.index += (size_t) r;
80
81         if (c.index >= pa_memblock_get_length(c.memblock)) {
82             pa_memblock_unref(c.memblock);
83             pa_memchunk_reset(&c);
84         }
85
86         for (;;) {
87             pa_memchunk t;
88
89             if (pa_mcalign_pop(a, &t) < 0)
90                 break;
91
92             p = pa_memblock_acquire(t.memblock);
93             pa_loop_write(STDOUT_FILENO, (uint8_t*) p + t.index, t.length, NULL);
94             pa_memblock_release(t.memblock);
95             fprintf(stderr, "Wrote %lu bytes.\n", (unsigned long) t.length);
96
97             pa_memblock_unref(t.memblock);
98         }
99     }
100
101     pa_mcalign_free(a);
102
103     if (c.memblock)
104         pa_memblock_unref(c.memblock);
105
106     pa_mempool_free(p);
107
108     return 0;
109 }