1 /*  test/test-rbuf.c -- rbuf_t test harness.
2 
3     Copyright (C) 2014 Genome Research Ltd.
4 
5     Author: Petr Danecek <pd3@sanger.ac.uk>
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE.  */
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include "rbuf.h"
28 
debug_print(rbuf_t * rbuf,int * dat)29 void debug_print(rbuf_t *rbuf, int *dat)
30 {
31     int i;
32     for (i=-1; rbuf_next(rbuf, &i); ) printf(" %2d", i);
33     printf("\n");
34     for (i=-1; rbuf_next(rbuf, &i); ) printf(" %2d", dat[i]);
35     printf("\n");
36 }
37 
main(int argc,char ** argv)38 int main(int argc, char **argv)
39 {
40     int i, j, *dat = (int*)calloc(10,sizeof(int));
41     rbuf_t rbuf;
42     rbuf_init(&rbuf,10);
43 
44     rbuf.f = 5; // force wrapping
45     for (i=0; i<9; i++)
46     {
47         j = rbuf_append(&rbuf);
48         dat[j] = i+1;
49     }
50     printf("Inserted 1-9 starting at offset 5:\n");
51     debug_print(&rbuf, dat);
52 
53     i = rbuf_kth(&rbuf, 3);
54     printf("4th is %d\n", dat[i]);
55 
56     printf("Deleting 1-2:\n");
57     rbuf_shift_n(&rbuf, 2);
58     debug_print(&rbuf, dat);
59 
60     printf("Prepending 0-8:\n");
61     for (i=0; i<9; i++)
62     {
63         j = rbuf_prepend(&rbuf);
64         dat[j] = i;
65     }
66     debug_print(&rbuf, dat);
67 
68     printf("Expanding:\n");
69     rbuf_expand0(&rbuf,int,rbuf.n+1,dat);
70     debug_print(&rbuf, dat);
71 
72     free(dat);
73     return 0;
74 }
75 
76