int samtools_test_rtrans_build_main(int argc, char**argv)
{
    const int NUM_TESTS = 1;
    int verbose = 0;
    int success = 0;
    int failure = 0;
    int getopt_char;
    while ((getopt_char = getopt(argc, argv, "v")) != -1) {
        switch (getopt_char) {
            case 'v':
                ++verbose;
                break;
            default:
                break;
        }
    }
    const long GIMMICK_SEED = 0x1234330e;
    srand48(GIMMICK_SEED);

    if (verbose) fprintf(pysam_stdout, "BEGIN test 1\n");
    // setup
    trans_tbl_t tbl_1[2];
    int n_targets_1 = 3;
    int n_1 = 2;
    int* rtrans_1 = NULL;
    setup_test_1(&tbl_1[0]);
    // test
    if (verbose > 1) {
        // dump_trans_tid
    }
    if (verbose) fprintf(pysam_stdout, "RUN test 1\n");
    rtrans_1 = rtrans_build(n_1, n_targets_1, &tbl_1[0]);
    if (verbose) fprintf(pysam_stdout, "END RUN test 1\n");
    if (verbose > 1) {
        fprintf(pysam_stdout, "rtrans\n");
        dump_rtrans(rtrans_1, n_1, n_targets_1);
    }
    if (check_test_1(&tbl_1[0], rtrans_1)) {
        ++success;
    } else {
        ++failure;
        if (verbose) fprintf(pysam_stdout, "FAIL test 1\n");
    }
    // teardown
    trans_tbl_destroy(&tbl_1[0]);
    trans_tbl_destroy(&tbl_1[1]);
    free(rtrans_1);
    if (verbose) fprintf(pysam_stdout, "END test 1\n");

    if (success == NUM_TESTS) {
        return 0;
    } else {
        fprintf(pysam_stderr, "%d failures %d successes\n", failure, success);
        return 1;
    }
}
int samtools_test_trans_tbl_init_main(int argc, char**argv)
{
    const int NUM_TESTS = 6;
    int verbose = 0;
    int success = 0;
    int failure = 0;
    int getopt_char;
    while ((getopt_char = getopt(argc, argv, "v")) != -1) {
        switch (getopt_char) {
            case 'v':
                ++verbose;
                break;
            default:
                break;
        }
    }

    // Set the seed to a fixed value so that calls to lrand48 within functions return predictable values
    const long GIMMICK_SEED = 0x1234330e;
    srand48(GIMMICK_SEED);

    bam_hdr_t* out;
    bam_hdr_t* translate;

    if (verbose) fprintf(pysam_stdout, "BEGIN test 1\n");
    // setup
    trans_tbl_t tbl_1;
    merged_header_t *merged_hdr = init_merged_header();
    translate = setup_test_1(merged_hdr);
    assert(translate);
    // test
    if (verbose > 1) {
        fprintf(pysam_stdout, "translate\n");
        dump_header(translate);
    }
    if (verbose) fprintf(pysam_stdout, "RUN test 1\n");
    trans_tbl_init(merged_hdr, translate, &tbl_1, false, false, true, NULL);
    out = finish_merged_header(merged_hdr);
    free_merged_header(merged_hdr);
    if (verbose) fprintf(pysam_stdout, "END RUN test 1\n");
    if (verbose > 1) {
        fprintf(pysam_stdout, "translate\n");
        dump_header(translate);
        fprintf(pysam_stdout, "out\n");
        dump_header(out);
    }
    if (check_test_1(translate, out, &tbl_1)) {
        if (verbose) fprintf(pysam_stdout, "Test 1 : PASS\n");
        ++success;
    } else {
        if (verbose) fprintf(pysam_stdout, "Test 1 : FAIL\n");
        fprintf(pysam_stderr, "Test 1 : FAIL\n");
        ++failure;
    }
    // teardown
    bam_hdr_destroy(translate);
    bam_hdr_destroy(out);
    trans_tbl_destroy(&tbl_1);
    if (verbose) fprintf(pysam_stdout, "END test 1\n");

    // test
    if (verbose) fprintf(pysam_stdout, "BEGIN test 2\n");
    // reinit
    trans_tbl_t tbl_2;

    merged_hdr = init_merged_header();
    translate = setup_test_2(merged_hdr);
    assert(translate);
    if (verbose > 1) {
        fprintf(pysam_stdout, "translate\n");
        dump_header(translate);
    }
    if (verbose) fprintf(pysam_stdout, "RUN test 2\n");
    trans_tbl_init(merged_hdr, translate, &tbl_2, false, false, true, NULL);
    out = finish_merged_header(merged_hdr);
    free_merged_header(merged_hdr);
    if (verbose) fprintf(pysam_stdout, "END RUN test 2\n");
    if (verbose > 1) {
        fprintf(pysam_stdout, "translate\n");
        dump_header(translate);
        fprintf(pysam_stdout, "out\n");
        dump_header(out);
    }
    if (check_test_2(translate, out, &tbl_2)) {
        if (verbose) fprintf(pysam_stdout, "Test 2 : PASS\n");
        ++success;
    } else {
        if (verbose) fprintf(pysam_stdout, "Test 2 : FAIL\n");
        fprintf(pysam_stderr, "Test 2 : FAIL\n");
        ++failure;
    }
    // teardown
    bam_hdr_destroy(translate);
    bam_hdr_destroy(out);
    trans_tbl_destroy(&tbl_2);
    if (verbose) fprintf(pysam_stdout, "END test 2\n");

    // test
    if (verbose) fprintf(pysam_stdout, "BEGIN test 3\n");
    // reinit
    trans_tbl_t tbl_3;
    merged_hdr = init_merged_header();
    translate = setup_test_3(merged_hdr);
    assert(translate);
    if (verbose > 1) {
        fprintf(pysam_stdout, "translate\n");
        dump_header(translate);
     }
    if (verbose) fprintf(pysam_stdout, "RUN test 3\n");
    trans_tbl_init(merged_hdr, translate, &tbl_3, false, false, true, NULL);
    out = finish_merged_header(merged_hdr);
    free_merged_header(merged_hdr);
    if (verbose) fprintf(pysam_stdout, "END RUN test 3\n");
    if (verbose > 1) {
        fprintf(pysam_stdout, "translate\n");
        dump_header(translate);
        fprintf(pysam_stdout, "out\n");
        dump_header(out);
    }
    if (check_test_3(translate, out, &tbl_3)) {
        if (verbose) fprintf(pysam_stdout, "Test 3 : PASS\n");
        ++success;
    } else {
        if (verbose) fprintf(pysam_stdout, "Test 3 : FAIL\n");
        fprintf(pysam_stderr, "Test 3 : FAIL\n");
        ++failure;
    }
    // teardown
    bam_hdr_destroy(translate);
    bam_hdr_destroy(out);
    trans_tbl_destroy(&tbl_3);
    if (verbose) fprintf(pysam_stdout, "END test 3\n");

    // test
    if (verbose) fprintf(pysam_stdout, "BEGIN test 4\n");
    // reinit
    trans_tbl_t tbl_4;
    merged_hdr = init_merged_header();
    translate = setup_test_4(merged_hdr);
    assert(translate);
    if (verbose > 1) {
        fprintf(pysam_stdout, "translate\n");
        dump_header(translate);
    }
    if (verbose) fprintf(pysam_stdout, "RUN test 4\n");
    trans_tbl_init(merged_hdr, translate, &tbl_4, false, false, true, NULL);
    out = finish_merged_header(merged_hdr);
    free_merged_header(merged_hdr);
    if (verbose) fprintf(pysam_stdout, "END RUN test 4\n");
    if (verbose > 1) {
        fprintf(pysam_stdout, "translate\n");
        dump_header(translate);
        fprintf(pysam_stdout, "out\n");
        dump_header(out);
    }
    if (check_test_4(translate, out, &tbl_4)) {
        if (verbose) fprintf(pysam_stdout, "Test 4 : PASS\n");
        ++success;
    } else {
        if (verbose) fprintf(pysam_stdout, "Test 4 : FAIL\n");
        fprintf(pysam_stderr, "Test 4 : FAIL\n");
        ++failure;
    }
    // teardown
    bam_hdr_destroy(translate);
    bam_hdr_destroy(out);
    trans_tbl_destroy(&tbl_4);
    if (verbose) fprintf(pysam_stdout, "END test 4\n");

    // test
    if (verbose) fprintf(pysam_stdout, "BEGIN test 5\n");
    // reinit
    trans_tbl_t tbl_5;
    merged_hdr = init_merged_header();
    translate = setup_test_5(merged_hdr);
    assert(translate);
    if (verbose > 1) {

        fprintf(pysam_stdout, "translate\n");
        dump_header(translate);
    }
    if (verbose) fprintf(pysam_stdout, "RUN test 5\n");
    trans_tbl_init(merged_hdr, translate, &tbl_5, false, false, true, NULL);
    out = finish_merged_header(merged_hdr);
    free_merged_header(merged_hdr);
    if (verbose) fprintf(pysam_stdout, "END RUN test 5\n");
    if (verbose > 1) {
        fprintf(pysam_stdout, "translate\n");
        dump_header(translate);
        fprintf(pysam_stdout, "out\n");
        dump_header(out);
    }
    if (check_test_5(translate, out, &tbl_5)) {
        if (verbose) fprintf(pysam_stdout, "Test 5 : PASS\n");
        ++success;
    } else {
        if (verbose) fprintf(pysam_stdout, "Test 5 : FAIL\n");
        fprintf(pysam_stderr, "Test 5 : FAIL\n");
        ++failure;
    }
    // teardown
    bam_hdr_destroy(translate);
    bam_hdr_destroy(out);
    trans_tbl_destroy(&tbl_5);
    if (verbose) fprintf(pysam_stdout, "END test 5\n");

    // test
    if (verbose) fprintf(pysam_stdout, "BEGIN test 6\n");
    // reinit
    trans_tbl_t tbl_6;
    merged_hdr = init_merged_header();
    translate = setup_test_6(merged_hdr);
    assert(translate);
    if (verbose > 1) {
        fprintf(pysam_stdout, "translate\n");
        dump_header(translate);
    }
    if (verbose) fprintf(pysam_stdout, "RUN test 6\n");
    trans_tbl_init(merged_hdr, translate, &tbl_6, false, false, true, "filename");
    out = finish_merged_header(merged_hdr);
    free_merged_header(merged_hdr);
    if (verbose) fprintf(pysam_stdout, "END RUN test 6\n");
    if (verbose > 1) {
        fprintf(pysam_stdout, "translate\n");
        dump_header(translate);
        fprintf(pysam_stdout, "out\n");
        dump_header(out);
    }
    if (check_test_6(translate, out, &tbl_6)) {
        if (verbose) fprintf(pysam_stdout, "Test 6 : PASS\n");
        ++success;
    } else {
        if (verbose) fprintf(pysam_stdout, "Test 6 : FAIL\n");
        fprintf(pysam_stderr, "Test 6 : FAIL\n");
        ++failure;
    }
    // teardown
    bam_hdr_destroy(translate);
    bam_hdr_destroy(out);
    trans_tbl_destroy(&tbl_6);
    if (verbose) fprintf(pysam_stdout, "END test 6\n");

    if (success == NUM_TESTS) {
        return 0;
    } else {
        fprintf(pysam_stderr, "%d failures %d successes\n", failure, success);
        return 1;
    }
}
int main(int argc, char**argv)
{
    // test state
    const int NUM_TESTS = 2;
    int verbose = 0;
    int success = 0;
    int failure = 0;

    int getopt_char;
    while ((getopt_char = getopt(argc, argv, "v")) != -1) {
        switch (getopt_char) {
            case 'v':
                ++verbose;
                break;
            default:
                printf(
                       "usage: test_filter_header_rg [-v]\n\n"
                       " -v verbose output\n"
                       );
                break;
        }
    }


    // Setup pysamerr redirect
    kstring_t res = { 0, 0, NULL };
    FILE* orig_pysamerr = fdopen(dup(STDERR_FILENO), "a"); // Save pysamerr
    char* tempfname = (optind < argc)? argv[optind] : "test_count_rg.tmp";
    FILE* check = NULL;

    // setup
    if (verbose) printf("BEGIN test 1\n");  // test eliminating a tag that isn't there
    bam_hdr_t* hdr1;
    const char* id_to_keep_1 = "1#2.3";
    setup_test_1(&hdr1);
    if (verbose > 1) {
        printf("hdr1\n");
        dump_hdr(hdr1);
    }
    if (verbose) printf("RUN test 1\n");

    // test
    xfreopen(tempfname, "w", pysamerr); // Redirect pysamerr to pipe
    bool result_1 = filter_header_rg(hdr1, id_to_keep_1);
    fclose(pysamerr);

    if (verbose) printf("END RUN test 1\n");
    if (verbose > 1) {
        printf("hdr1\n");
        dump_hdr(hdr1);
    }

    // check result
    res.l = 0;
    check = fopen(tempfname, "r");
    if ( result_1
        && check_test_1(hdr1)
        && kgetline(&res, (kgets_func *)fgets, check) < 0
        && (feof(check) || res.l == 0)) {
        ++success;
    } else {
        ++failure;
        if (verbose) printf("FAIL test 1\n");
    }
    fclose(check);

    // teardown
    bam_hdr_destroy(hdr1);
    if (verbose) printf("END test 1\n");

    if (verbose) printf("BEGIN test 2\n");  // test eliminating a tag that is there
    bam_hdr_t* hdr2;
    const char* id_to_keep_2 = "fish";
    setup_test_2(&hdr2);
    if (verbose > 1) {
        printf("hdr2\n");
        dump_hdr(hdr2);
    }
    if (verbose) printf("RUN test 2\n");

    // test
    xfreopen(tempfname, "w", pysamerr); // Redirect pysamerr to pipe
    bool result_2 = filter_header_rg(hdr2, id_to_keep_2);
    fclose(pysamerr);

    if (verbose) printf("END RUN test 2\n");
    if (verbose > 1) {
        printf("hdr2\n");
        dump_hdr(hdr2);
    }

    // check result
    res.l = 0;
    check = fopen(tempfname, "r");
    if ( result_2
        && check_test_2(hdr2)
        && kgetline(&res, (kgets_func *)fgets, check) < 0
        && (feof(check) || res.l == 0)) {
        ++success;
    } else {
        ++failure;
        if (verbose) printf("FAIL test 2\n");
    }
    fclose(check);

    // teardown
    bam_hdr_destroy(hdr2);
    if (verbose) printf("END test 2\n");


    // Cleanup
    free(res.s);
    remove(tempfname);
    if (failure > 0)
        fprintf(orig_pysamerr, "%d failures %d successes\n", failure, success);
    fclose(orig_pysamerr);

    return (success == NUM_TESTS)? EXIT_SUCCESS : EXIT_FAILURE;
}