modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / lib / htslib / test / test-regidx.c
blob0aabc2d7099b849145519743208999fedf87b22a
1 /* test/test-regidx.c -- Regions index test harness.
3 Copyright (C) 2014 Genome Research Ltd.
5 Author: Petr Danecek <pd3@sanger.ac.uk>
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:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
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 THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
26 #include <config.h>
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <htslib/regidx.h>
33 #include "hts_internal.h"
35 void error(const char *format, ...)
37 va_list ap;
38 va_start(ap, format);
39 vfprintf(stderr, format, ap);
40 va_end(ap);
41 exit(-1);
44 int custom_parse(const char *line, char **chr_beg, char **chr_end, reg_t *reg, void *payload, void *usr)
46 // Use the standard parser for CHROM,FROM,TO
47 int i, ret = regidx_parse_tab(line,chr_beg,chr_end,reg,NULL,NULL);
48 if ( ret!=0 ) return ret;
50 // Skip the fields that were parsed above
51 char *ss = (char*) line;
52 while ( *ss && isspace_c(*ss) ) ss++;
53 for (i=0; i<3; i++)
55 while ( *ss && !isspace_c(*ss) ) ss++;
56 if ( !*ss ) return -2; // wrong number of fields
57 while ( *ss && isspace_c(*ss) ) ss++;
59 if ( !*ss ) return -2;
61 // Parse the payload
62 char *se = ss;
63 while ( *se && !isspace_c(*se) ) se++;
64 char **dat = (char**) payload;
65 *dat = (char*) malloc(se-ss+1);
66 memcpy(*dat,ss,se-ss+1);
67 (*dat)[se-ss] = 0;
68 return 0;
70 void custom_free(void *payload)
72 char **dat = (char**)payload;
73 free(*dat);
76 int main(int argc, char **argv)
78 // Init index with no file name, we will insert the regions manually
79 regidx_t *idx = regidx_init(NULL,custom_parse,custom_free,sizeof(char*),NULL);
80 if ( !idx ) error("init failed\n");
82 // Insert regions
83 char *line;
84 line = "1 10000000 10000000 1:10000000-10000000"; if ( regidx_insert(idx,line)!=0 ) error("insert failed: %s\n", line);
85 line = "1 20000000 20000001 1:20000000-20000001"; if ( regidx_insert(idx,line)!=0 ) error("insert failed: %s\n", line);
86 line = "1 20000002 20000002 1:20000002-20000002"; if ( regidx_insert(idx,line)!=0 ) error("insert failed: %s\n", line);
87 line = "1 30000000 30000000 1:30000000-30000000"; if ( regidx_insert(idx,line)!=0 ) error("insert failed: %s\n", line);
89 // Finish initialization
90 regidx_insert(idx,NULL);
92 // Test
93 regitr_t itr;
94 int from, to;
96 from = to = 10000000;
97 if ( !regidx_overlap(idx,"1",from-1,to-1,&itr) ) error("query failed: 1:%d-%d\n",from,to);
98 if ( strcmp("1:10000000-10000000",REGITR_PAYLOAD(itr,char*)) ) error("query failed: 1:%d-%d vs %s\n", from,to,REGITR_PAYLOAD(itr,char*));
99 if ( !regidx_overlap(idx,"1",from-2,to-1,&itr) ) error("query failed: 1:%d-%d\n",from-1,to);
100 if ( !regidx_overlap(idx,"1",from-2,to+3,&itr) ) error("query failed: 1:%d-%d\n",from-1,to+2);
101 if ( regidx_overlap(idx,"1",from-2,to-2,&itr) ) error("query failed: 1:%d-%d\n",from-1,to-1);
103 from = to = 20000000;
104 if ( !regidx_overlap(idx,"1",from-1,to-1,&itr) ) error("query failed: 1:%d-%d\n",from,to);
106 from = to = 20000002;
107 if ( !regidx_overlap(idx,"1",from-1,to-1,&itr) ) error("query failed: 1:%d-%d\n",from,to);
109 from = to = 30000000;
110 if ( !regidx_overlap(idx,"1",from-1,to-1,&itr) ) error("query failed: 1:%d-%d\n",from,to);
112 // Clean up
113 regidx_destroy(idx);
115 return 0;