comparison ezBAMQC/src/htslib/test/hfile.c @ 0:dfa3745e5fd8

Uploaded
author youngkim
date Thu, 24 Mar 2016 17:12:52 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:dfa3745e5fd8
1 /* test/hfile.c -- Test cases for low-level input/output streams.
2
3 Copyright (C) 2013-2014 Genome Research Ltd.
4
5 Author: John Marshall <jm18@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 <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29
30 #include <sys/stat.h>
31
32 #include "htslib/hfile.h"
33 #include "htslib/hts_defs.h"
34
35 void HTS_NORETURN fail(const char *format, ...)
36 {
37 int err = errno;
38 va_list args;
39 va_start(args, format);
40 vfprintf(stderr, format, args);
41 va_end(args);
42 if (err != 0) fprintf(stderr, ": %s", strerror(err));
43 fprintf(stderr, "\n");
44 exit(EXIT_FAILURE);
45 }
46
47 void check_offset(hFILE *f, off_t off, const char *message)
48 {
49 off_t ret = htell(f);
50 if (ret < 0) fail("htell(%s)", message);
51 if (ret == off) return;
52
53 fprintf(stderr, "%s offset incorrect: expected %ld but got %ld\n",
54 message, (long)off, (long)ret);
55 exit(EXIT_FAILURE);
56 }
57
58 char *slurp(const char *filename)
59 {
60 char *text;
61 struct stat sbuf;
62 size_t filesize;
63 FILE *f = fopen(filename, "r");
64 if (f == NULL) fail("fopen(\"%s\", \"r\")", filename);
65 if (fstat(fileno(f), &sbuf) != 0) fail("fstat(\"%s\")", filename);
66 filesize = sbuf.st_size;
67
68 text = (char *) malloc(filesize + 1);
69 if (text == NULL) fail("malloc(text)");
70
71 if (fread(text, 1, filesize, f) != filesize) fail("fread");
72 fclose(f);
73
74 text[filesize] = '\0';
75 return text;
76 }
77
78 hFILE *fin = NULL;
79 hFILE *fout = NULL;
80
81 void reopen(const char *infname, const char *outfname)
82 {
83 if (fin) { if (hclose(fin) != 0) fail("hclose(input)"); }
84 if (fout) { if (hclose(fout) != 0) fail("hclose(output)"); }
85
86 fin = hopen(infname, "r");
87 if (fin == NULL) fail("hopen(\"%s\")", infname);
88
89 fout = hopen(outfname, "w");
90 if (fout == NULL) fail("hopen(\"%s\")", outfname);
91 }
92
93 int main(void)
94 {
95 static const int size[] = { 1, 13, 403, 999, 30000 };
96
97 char buffer[40000];
98 char *original;
99 int c, i;
100 ssize_t n;
101 off_t off;
102
103 reopen("vcf.c", "test/hfile1.tmp");
104 while ((c = hgetc(fin)) != EOF) {
105 if (hputc(c, fout) == EOF) fail("hputc");
106 }
107 if (herrno(fin)) { errno = herrno(fin); fail("hgetc"); }
108
109 reopen("test/hfile1.tmp", "test/hfile2.tmp");
110 if (hpeek(fin, buffer, 50) < 0) fail("hpeek");
111 while ((n = hread(fin, buffer, 17)) > 0) {
112 if (hwrite(fout, buffer, n) != n) fail("hwrite");
113 }
114 if (n < 0) fail("hread");
115
116 reopen("test/hfile2.tmp", "test/hfile3.tmp");
117 while ((n = hread(fin, buffer, sizeof buffer)) > 0) {
118 if (hwrite(fout, buffer, n) != n) fail("hwrite");
119 if (hpeek(fin, buffer, 700) < 0) fail("hpeek");
120 }
121 if (n < 0) fail("hread");
122
123 reopen("test/hfile3.tmp", "test/hfile4.tmp");
124 i = 0;
125 off = 0;
126 while ((n = hread(fin, buffer, size[i++ % 5])) > 0) {
127 off += n;
128 buffer[n] = '\0';
129 check_offset(fin, off, "pre-peek");
130 if (hputs(buffer, fout) == EOF) fail("hputs");
131 if ((n = hpeek(fin, buffer, size[(i+3) % 5])) < 0) fail("hpeek");
132 check_offset(fin, off, "post-peek");
133 }
134 if (n < 0) fail("hread");
135
136 reopen("test/hfile4.tmp", "test/hfile5.tmp");
137 n = hread(fin, buffer, 200);
138 if (n < 0) fail("hread");
139 else if (n != 200) fail("hread only got %d", (int)n);
140 if (hwrite(fout, buffer, 1000) != 1000) fail("hwrite");
141 check_offset(fin, 200, "input/first200");
142 check_offset(fout, 1000, "output/first200");
143
144 if (hseek(fin, 800, SEEK_CUR) < 0) fail("hseek/cur");
145 check_offset(fin, 1000, "input/seek");
146 for (off = 1000; (n = hread(fin, buffer, sizeof buffer)) > 0; off += n)
147 if (hwrite(fout, buffer, n) != n) fail("hwrite");
148 if (n < 0) fail("hread");
149 check_offset(fin, off, "input/eof");
150 check_offset(fout, off, "output/eof");
151
152 if (hseek(fin, 200, SEEK_SET) < 0) fail("hseek/set");
153 if (hseek(fout, 200, SEEK_SET) < 0) fail("hseek(output)");
154 check_offset(fin, 200, "input/backto200");
155 check_offset(fout, 200, "output/backto200");
156 n = hread(fin, buffer, 800);
157 if (n < 0) fail("hread");
158 else if (n != 800) fail("hread only got %d", (int)n);
159 if (hwrite(fout, buffer, 800) != 800) fail("hwrite");
160 check_offset(fin, 1000, "input/wrote800");
161 check_offset(fout, 1000, "output/wrote800");
162
163 if (hflush(fout) == EOF) fail("hflush");
164
165 original = slurp("vcf.c");
166 for (i = 1; i <= 5; i++) {
167 char *text;
168 sprintf(buffer, "test/hfile%d.tmp", i);
169 text = slurp(buffer);
170 if (strcmp(original, text) != 0) {
171 fprintf(stderr, "%s differs from vcf.c\n", buffer);
172 return EXIT_FAILURE;
173 }
174 free(text);
175 }
176 free(original);
177
178 if (hclose(fin) != 0) fail("hclose(input)");
179 if (hclose(fout) != 0) fail("hclose(output)");
180
181 fout = hopen("test/hfile_chars.tmp", "w");
182 if (fout == NULL) fail("hopen(\"test/hfile_chars.tmp\")");
183 for (i = 0; i < 256; i++)
184 if (hputc(i, fout) != i) fail("chars: hputc (%d)", i);
185 if (hclose(fout) != 0) fail("hclose(test/hfile_chars.tmp)");
186
187 fin = hopen("test/hfile_chars.tmp", "r");
188 if (fin == NULL) fail("hopen(\"test/hfile_chars.tmp\") for reading");
189 for (i = 0; i < 256; i++)
190 if ((c = hgetc(fin)) != i)
191 fail("chars: hgetc (%d = 0x%x) returned %d = 0x%x", i, i, c, c);
192 if ((c = hgetc(fin)) != EOF) fail("chars: hgetc (EOF) returned %d", c);
193 if (hclose(fin) != 0) fail("hclose(test/hfile_chars.tmp) for reading");
194
195 fin = hopen("data:hello, world!\n", "r");
196 if (fin == NULL) fail("hopen(\"data:...\")");
197 n = hread(fin, buffer, 300);
198 if (n < 0) fail("hread");
199 buffer[n] = '\0';
200 if (strcmp(buffer, "hello, world!\n") != 0) fail("hread result");
201 if (hclose(fin) != 0) fail("hclose(\"data:...\")");
202
203 return EXIT_SUCCESS;
204 }