modified: makesrc3.mk
[GalaxyCodeBases.git] / c_cpp / lib / htslib / test / thrash_threads6.c
blobe4bd76031280792a70854e51ed90052620a56c1d
1 /* The MIT/Expat License
3 Copyright (C) 2017 Genome Research Ltd.
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 DEALINGS IN THE SOFTWARE.
23 // Spam seeks
24 #include <config.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include "htslib/bgzf.h"
30 #include "htslib/thread_pool.h"
32 int main(int argc, char *argv[]) {
33 if (argc <= 1) {
34 fprintf(stderr, "Usage: thrash_threads4 input.bam\n");
35 exit(1);
38 // Find a valid seek location ~64M into the file
39 int i;
40 ssize_t got;
41 BGZF *fpin = bgzf_open(argv[1], "r");
42 uint64_t upos = 0, uend = 0;
43 char buf[100000];
44 for (i = 0; i < 100; i++) {
45 if ((got = bgzf_read(fpin, buf, 65536)) < 0)
46 abort();
47 upos += got;
49 int64_t pos = bgzf_tell(fpin);
50 while ((got = bgzf_read(fpin, buf, 65536)) > 0) {
51 uend += got;
53 if (got < 0) abort();
54 int64_t end = bgzf_tell(fpin);
55 bgzf_close(fpin);
57 // Ensure input is big enough to avoid case 3,4 below going off the end
58 // of the file
59 if (uend < upos + 10000000) {
60 fprintf(stderr, "Please supply a bigger input file\n");
61 exit(1);
64 #define N 1000
66 // Spam random seeks & reads
67 for (i = 0; i < 1000; i++) {
68 printf("i=%d\t", i);
69 fpin = bgzf_open(argv[1], "r");
70 int j, eof = 0, mt = 0;
71 for (j = 0; j < 80; j++) {
72 int n = rand() % 7;
73 putchar('0'+n); fflush(stdout);
74 switch (n) {
75 case 0: // start
76 if (bgzf_seek(fpin, 0LL, SEEK_SET) < 0) puts("!");//abort();
77 eof = 0;
78 break;
79 case 1: // mid
80 if (bgzf_seek(fpin, pos, SEEK_SET) < 0) puts("!");//abort();
81 eof = 0;
82 break;
83 case 2: // eof
84 if (bgzf_seek(fpin, end, SEEK_SET) < 0) puts("!");//abort();
85 eof = 1;
86 break;
87 case 3: case 4: {
88 int l = rand()%(n==3?100000:100);
89 if (bgzf_read(fpin, buf, l) != l*(1-eof)) abort();
90 break;
92 case 5:
93 usleep(N);
94 break;
95 case 6:
96 if (!mt)
97 bgzf_mt(fpin, 8, 256);
98 mt = 1;
99 break;
102 printf("\n");
103 if (bgzf_close(fpin))
104 abort();
107 return 0;