1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/* test/split/test_expand_format_string.c -- split format string test cases.
Copyright (C) 2014-2015,2024 Genome Research Ltd.
Author: Martin O. Pollard <mp15@sanger.ac.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. */
#include <config.h>
#include "../../bam_split.c"
#include "../test.h"
#include <stdlib.h>
#include <unistd.h>
typedef struct {
char *tempfname;
kstring_t res;
int verbose;
int test_num;
int success;
int failure;
} TestState;
void run_test(TestState *state, const char* format_string, const char* basename,
const char* rg_id, const int rg_idx, const int padding,
const htsFormat *format, const char *expected) {
FILE* check = NULL;
++state->test_num;
if (state->verbose) printf("BEGIN test %d\n", state->test_num);
if (state->verbose > 1) {
printf("format_string:%s\n"
"basename:%s\n"
"rg_id:%s\n"
"rg_idx:%d\n", format_string, basename, rg_id, rg_idx);
}
if (state->verbose) printf("RUN test %d\n", state->test_num);
// test
xfreopen(state->tempfname, "w", stderr); // Redirect stderr to pipe
char* output = expand_format_string(format_string, basename,
rg_id, rg_idx, padding, format);
fclose(stderr);
if (state->verbose) printf("END RUN test %d\n", state->test_num);
if (state->verbose > 1) {
printf("got: \"%s\" expected: \"%s\"\n", output ? output : "(null)",
expected ? expected : "(null)");
}
if (state->verbose > 2) {
printf("format_string:%s\n"
"basename:%s\n"
"rg_id:%s\n"
"rg_idx:%d\n", format_string, basename, rg_id, rg_idx);
}
// check result
state->res.l = 0;
check = fopen(state->tempfname, "r");
if (expected != NULL) {
// Good input, should produce a file name
if (check && output != NULL && !strcmp(output, expected)
&& kgetline(&state->res, (kgets_func *)fgets, check) < 0
&& (feof(check) || state->res.l == 0)) {
++state->success;
} else {
++state->failure;
if (state->verbose) printf("FAIL test %d\n", state->test_num);
}
} else {
// Bad input, should return NULL and print an error message
if (check && output == NULL
&& kgetline(&state->res, (kgets_func *)fgets, check) == 0
&& state->res.l > 0) {
++state->success;
} else {
++state->failure;
if (state->verbose) printf("FAIL test %d\n", state->test_num);
}
}
fclose(check);
// teardown
free(output);
if (state->verbose) printf("END test %d\n", state->test_num);
return;
}
int main(int argc, char**argv)
{
// test state
TestState state = { NULL, KS_INITIALIZE, 0, 0, 0, 0};
static const htsFormat sam_fmt = { sequence_data, sam };
static const htsFormat bam_fmt = { sequence_data, bam };
static const htsFormat cram_fmt = { sequence_data, cram };
int getopt_char;
while ((getopt_char = getopt(argc, argv, "v")) != -1) {
switch (getopt_char) {
case 'v':
++state.verbose;
break;
default:
printf(
"usage: test_expand_format_string [-v]\n\n"
" -v verbose output\n"
);
break;
}
}
// Setup stderr redirect
FILE* orig_stderr = fdopen(dup(STDERR_FILENO), "a"); // Save stderr
state.tempfname = (optind < argc)? argv[optind] : "test_expand_format_string.tmp";
// default format string test
run_test(&state, "%*_%#.%.", "basename", "1#2.3", 4, 0, &bam_fmt,
"basename_4.bam");
// default for non-RG tags
run_test(&state, "%*_%!.%.", "basename", "1#2.3", 4, 0, &bam_fmt,
"basename_1#2.3.bam");
// alternate file types
run_test(&state, "%*_%#.%.", "basename", "1#2.3", 4, 0, &sam_fmt,
"basename_4.sam");
run_test(&state, "%*_%#.%.", "basename", "1#2.3", 4, 0, &cram_fmt,
"basename_4.cram");
// zero padding
run_test(&state, "%*_%#.%.", "basename", "1#2.3", 4, 5, &bam_fmt,
"basename_00004.bam");
// percent sign
run_test(&state, "%%%*_%#.%.%%", "basename", "1#2.3", 4, 0, &bam_fmt,
"%basename_4.bam%");
// Bad input - single percent sign at end
run_test(&state, "%%%*_%#.%.%", "basename", "1#2.3", 4, 0, &bam_fmt,
NULL);
// Bad input - invalid format character
run_test(&state, "%s_%#.%.", "basename", "1#2.3", 4, 0, &bam_fmt,
NULL);
// Cleanup test harness
free(state.res.s);
remove(state.tempfname);
if (state.failure > 0)
fprintf(orig_stderr, "%d failures %d successes\n",
state.failure, state.success);
fclose(orig_stderr);
return (state.success == state.test_num)? EXIT_SUCCESS : EXIT_FAILURE;
}
|