/* Copyright (c) 2019, Anthony Latorre * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation, either version 3 of the License, or (at your option) * any later version. * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "zebra.h" #include #include "Record_Info.h" #include "misc.h" char *filename; typedef int testFunction(char *err); int test_zdab(char *err) { /* Simple test to make sure we can read the 5 events from Muons.zdab. */ int rv; zebraBank b; int nzdabs = 0; zebraFile *z = zebra_open("Muons.zdab"); if (!z) { sprintf(err, "%s", zebra_err); return 1; } while (1) { rv = zebra_read_next_logical_record(z); if (rv == -1) { sprintf(err, "error getting logical record: %s", zebra_err); goto err; } else if (rv == 1) { break; } rv = zebra_get_bank(z, &b, z->first_bank); if (rv) { sprintf(err, "error getting first bank: %s\n", zebra_err); goto err; } if (b.idh == ZDAB_RECORD) nzdabs += 1; } if (nzdabs != 5) { sprintf(err, "expected %i zdab records but got %i", 5, nzdabs); goto err; } zebra_close(z); return 0; err: zebra_close(z); return -1; } int test_bank_links(zebraFile *z, uint32_t link, char *err) { int i, rv; zebraBank b1, b2; rv = zebra_get_bank(z, &b1, link); if (rv) { sprintf(err, "error getting bank: %s\n", zebra_err); goto err; } /* Test that all the structural links point back to the bank. */ for (i = 0; i < b1.num_structural_links; i++) { if (b1.links[i] == 0) continue; rv = zebra_get_bank(z, &b2, b1.links[i]); if (rv) { sprintf(err, "error getting bank: %s\n", zebra_err); goto err; } /* Check that the up pointer points back to the original bank. */ if (b2.up != link) { sprintf(err, "bank up pointer from bank %.4s does not point back to %.4s bank!", b2.name, b1.name); goto err; } rv = test_bank_links(z,b1.links[i],err); if (rv) goto err; } if (b1.next) { /* Test that the next/orig pointers are consistent. */ rv = zebra_get_bank(z, &b2, b1.next); if (rv) { sprintf(err, "error getting bank: %s\n", zebra_err); goto err; } /* Check that the orig pointer points back to the original bank. */ if (b2.orig != link) { sprintf(err, "bank orig pointer from bank %.4s does not point back to %.4s bank!", b2.name, b1.name); goto err; } rv = test_bank_links(z,b1.next,err); if (rv) goto err; } return 0; err: return -1; } int test_links(char *err) { /* Tests that all the next/up/orig bank pointers in the zebra file are * consistent. */ int rv; zebraFile *z = zebra_open(filename); if (!z) { sprintf(err, "%s", zebra_err); return 1; } while (1) { rv = zebra_read_next_logical_record(z); if (rv == -1) { sprintf(err, "error getting logical record: %s", zebra_err); goto err; } else if (rv == 1) { break; } if (test_bank_links(z,z->first_bank,err)) goto err; } zebra_close(z); return 0; err: zebra_close(z); return -1; } struct tests { testFunction *test; char *name; } tests[] = { {test_zdab, "test_zdab"}, {test_links, "test_links"} }; int main(int argc, char **argv) { int i; char err[256]; int retval = 0; struct tests test; if (argc < 2) { fprintf(stderr, "usage: test-zebra FILENAME\n"); return 1; } filename = argv[1]; for (i = 0; i < LEN(tests); i++) { test = tests[i]; if (!test.test(err)) { printf("[\033[92mok\033[0m] %s\n", test.name); } else { printf("[\033[91mfail\033[0m] %s: %s\n", test.name, err); retval = 1; } } return retval; }