1 /*  test/test.c -- test harness utility routines.
2 
3     Copyright (C) 2014, 2016, 2019 Genome Research Ltd.
4 
5     Author: Martin O. Pollard <mp15@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 <config.h>
26 
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <inttypes.h>
37 #include <htslib/sam.h>
38 
39 #include "test.h"
40 
xfreopen(const char * path,const char * mode,FILE * stream)41 void xfreopen(const char *path, const char *mode, FILE *stream)
42 {
43     if (freopen(path, mode, stream) == NULL) {
44         fprintf(stderr, __FILE__": error reopening %s: %s\n",
45                 path, strerror(errno));
46         exit(2);
47     }
48 }
49 
redirect_stderr(const char * path)50 int redirect_stderr(const char *path) {
51     int fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, 0666);
52     if (!fd) {
53         fprintf(stderr, "Couldn't open \"%s\" : %s\n", path, strerror(errno));
54         exit(2);
55     }
56     fflush(stderr);
57     dup2(fd, STDERR_FILENO);
58     return fd;
59 }
60 
flush_and_restore_stderr(int orig_stderr,int redirect_fd)61 void flush_and_restore_stderr(int orig_stderr, int redirect_fd) {
62     fflush(stderr);
63     dup2(orig_stderr, STDERR_FILENO);
64     close(redirect_fd);
65 }
66 
dump_hdr(const sam_hdr_t * hdr)67 void dump_hdr(const sam_hdr_t* hdr)
68 {
69     printf("n_targets: %d\n", sam_hdr_nref(hdr));
70     printf("ignore_sam_err: %d\n", hdr->ignore_sam_err);
71     printf("l_text: %zu\n", (size_t) sam_hdr_length((sam_hdr_t*)hdr));
72     printf("idx\ttarget_len\ttarget_name:\n");
73     int32_t target;
74     for (target = 0; target < sam_hdr_nref(hdr); ++target) {
75         printf("%d\t%"PRId64"\t\"%s\"\n", target, (int64_t) sam_hdr_tid2len(hdr, target), sam_hdr_tid2name(hdr, target));
76     }
77     printf("text: \"%s\"\n", sam_hdr_str((sam_hdr_t*)hdr));
78 }
79 
80 // For tests, just return a constant that can be embedded in expected output.
samtools_version(void)81 const char *samtools_version(void)
82 {
83     return "x.y.test";
84 }
85