LCOV - code coverage report
Current view: top level - tests - memblockq-test.c (source / functions) Hit Total Coverage
Test: lcov.out Lines: 87 87 100.0 %
Date: 2012-07-17 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 24 44 54.5 %

           Branch data     Line data    Source code
       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 <stdio.h>
      26                 :            : #include <signal.h>
      27                 :            : 
      28                 :            : #include <pulsecore/memblockq.h>
      29                 :            : #include <pulsecore/log.h>
      30                 :            : #include <pulsecore/macro.h>
      31                 :            : 
      32                 :         57 : static void dump_chunk(const pa_memchunk *chunk) {
      33                 :            :     size_t n;
      34                 :            :     void *q;
      35                 :            :     char *e;
      36                 :            : 
      37         [ -  + ]:         57 :     pa_assert(chunk);
      38                 :            : 
      39                 :         57 :     q = pa_memblock_acquire(chunk->memblock);
      40         [ +  + ]:        301 :     for (e = (char*) q + chunk->index, n = 0; n < chunk->length; n++, e++)
      41                 :        244 :         fprintf(stderr, "%c", *e);
      42                 :         57 :     pa_memblock_release(chunk->memblock);
      43                 :         57 : }
      44                 :            : 
      45                 :          2 : static void dump(pa_memblockq *bq) {
      46                 :            :     pa_memchunk out;
      47                 :            : 
      48         [ -  + ]:          2 :     pa_assert(bq);
      49                 :            : 
      50                 :            :     /* First let's dump this as fixed block */
      51                 :          2 :     fprintf(stderr, "FIXED >");
      52                 :          2 :     pa_memblockq_peek_fixed_size(bq, 64, &out);
      53                 :          2 :     dump_chunk(&out);
      54                 :          2 :     pa_memblock_unref(out.memblock);
      55                 :          2 :     fprintf(stderr, "<\n");
      56                 :            : 
      57                 :            :     /* Then let's dump the queue manually */
      58                 :          2 :     fprintf(stderr, "MANUAL>");
      59                 :            : 
      60                 :            :     for (;;) {
      61         [ +  + ]:         57 :         if (pa_memblockq_peek(bq, &out) < 0)
      62                 :            :             break;
      63                 :            : 
      64                 :         55 :         dump_chunk(&out);
      65                 :         55 :         pa_memblock_unref(out.memblock);
      66                 :         55 :         pa_memblockq_drop(bq, out.length);
      67                 :         55 :     }
      68                 :            : 
      69                 :          2 :     fprintf(stderr, "<\n");
      70                 :          2 : }
      71                 :            : 
      72                 :          1 : int main(int argc, char *argv[]) {
      73                 :            :     int ret;
      74                 :            : 
      75                 :            :     pa_mempool *p;
      76                 :            :     pa_memblockq *bq;
      77                 :            :     pa_memchunk chunk1, chunk2, chunk3, chunk4;
      78                 :            :     pa_memchunk silence;
      79                 :          1 :     pa_sample_spec ss = {
      80                 :            :         .format = PA_SAMPLE_S16LE,
      81                 :            :         .rate = 48000,
      82                 :            :         .channels = 1
      83                 :            :     };
      84                 :            : 
      85                 :          1 :     pa_log_set_level(PA_LOG_DEBUG);
      86                 :            : 
      87                 :          1 :     p = pa_mempool_new(FALSE, 0);
      88                 :            : 
      89         [ -  + ]:          1 :     pa_assert_se(silence.memblock = pa_memblock_new_fixed(p, (char*) "__", 2, 1));
      90                 :          1 :     silence.index = 0;
      91                 :          1 :     silence.length = pa_memblock_get_length(silence.memblock);
      92                 :            : 
      93         [ -  + ]:          1 :     pa_assert_se(bq = pa_memblockq_new("test memblockq", 0, 200, 10, &ss, 4, 4, 40, &silence));
      94                 :            : 
      95         [ -  + ]:          1 :     pa_assert_se(chunk1.memblock = pa_memblock_new_fixed(p, (char*) "11", 2, 1));
      96                 :          1 :     chunk1.index = 0;
      97                 :          1 :     chunk1.length = 2;
      98                 :            : 
      99         [ -  + ]:          1 :     pa_assert_se(chunk2.memblock = pa_memblock_new_fixed(p, (char*) "XX22", 4, 1));
     100                 :          1 :     chunk2.index = 2;
     101                 :          1 :     chunk2.length = 2;
     102                 :            : 
     103         [ -  + ]:          1 :     pa_assert_se(chunk3.memblock = pa_memblock_new_fixed(p, (char*) "3333", 4, 1));
     104                 :          1 :     chunk3.index = 0;
     105                 :          1 :     chunk3.length = 4;
     106                 :            : 
     107         [ -  + ]:          1 :     pa_assert_se(chunk4.memblock = pa_memblock_new_fixed(p, (char*) "44444444", 8, 1));
     108                 :          1 :     chunk4.index = 0;
     109                 :          1 :     chunk4.length = 8;
     110                 :            : 
     111                 :          1 :     ret = pa_memblockq_push(bq, &chunk1);
     112         [ -  + ]:          1 :     assert(ret == 0);
     113                 :            : 
     114                 :          1 :     ret = pa_memblockq_push(bq, &chunk2);
     115         [ -  + ]:          1 :     assert(ret == 0);
     116                 :            : 
     117                 :          1 :     ret = pa_memblockq_push(bq, &chunk3);
     118         [ -  + ]:          1 :     assert(ret == 0);
     119                 :            : 
     120                 :          1 :     ret = pa_memblockq_push(bq, &chunk4);
     121         [ -  + ]:          1 :     assert(ret == 0);
     122                 :            : 
     123                 :          1 :     pa_memblockq_seek(bq, -6, 0, TRUE);
     124                 :          1 :     ret = pa_memblockq_push(bq, &chunk3);
     125         [ -  + ]:          1 :     assert(ret == 0);
     126                 :            : 
     127                 :          1 :     pa_memblockq_seek(bq, -2, 0, TRUE);
     128                 :          1 :     ret = pa_memblockq_push(bq, &chunk1);
     129         [ -  + ]:          1 :     assert(ret == 0);
     130                 :            : 
     131                 :          1 :     pa_memblockq_seek(bq, -10, 0, TRUE);
     132                 :          1 :     ret = pa_memblockq_push(bq, &chunk4);
     133         [ -  + ]:          1 :     assert(ret == 0);
     134                 :            : 
     135                 :          1 :     pa_memblockq_seek(bq, 10, 0, TRUE);
     136                 :            : 
     137                 :          1 :     ret = pa_memblockq_push(bq, &chunk1);
     138         [ -  + ]:          1 :     assert(ret == 0);
     139                 :            : 
     140                 :          1 :     pa_memblockq_seek(bq, -6, 0, TRUE);
     141                 :          1 :     ret = pa_memblockq_push(bq, &chunk2);
     142         [ -  + ]:          1 :     assert(ret == 0);
     143                 :            : 
     144                 :            :     /* Test splitting */
     145                 :          1 :     pa_memblockq_seek(bq, -12, 0, TRUE);
     146                 :          1 :     ret = pa_memblockq_push(bq, &chunk1);
     147         [ -  + ]:          1 :     assert(ret == 0);
     148                 :            : 
     149                 :          1 :     pa_memblockq_seek(bq, 20, 0, TRUE);
     150                 :            : 
     151                 :            :     /* Test merging */
     152                 :          1 :     ret = pa_memblockq_push(bq, &chunk3);
     153         [ -  + ]:          1 :     assert(ret == 0);
     154                 :          1 :     pa_memblockq_seek(bq, -2, 0, TRUE);
     155                 :            : 
     156                 :          1 :     chunk3.index += 2;
     157                 :          1 :     chunk3.length -= 2;
     158                 :          1 :     ret = pa_memblockq_push(bq, &chunk3);
     159         [ -  + ]:          1 :     assert(ret == 0);
     160                 :            : 
     161                 :          1 :     pa_memblockq_seek(bq, 30, PA_SEEK_RELATIVE, TRUE);
     162                 :            : 
     163                 :          1 :     dump(bq);
     164                 :            : 
     165                 :          1 :     pa_memblockq_rewind(bq, 52);
     166                 :            : 
     167                 :          1 :     dump(bq);
     168                 :            : 
     169                 :          1 :     pa_memblockq_free(bq);
     170                 :          1 :     pa_memblock_unref(silence.memblock);
     171                 :          1 :     pa_memblock_unref(chunk1.memblock);
     172                 :          1 :     pa_memblock_unref(chunk2.memblock);
     173                 :          1 :     pa_memblock_unref(chunk3.memblock);
     174                 :          1 :     pa_memblock_unref(chunk4.memblock);
     175                 :            : 
     176                 :          1 :     pa_mempool_free(p);
     177                 :            : 
     178                 :            :     return 0;
     179                 :            : }

Generated by: LCOV version 1.9