aboutsummaryrefslogtreecommitdiff
path: root/src/test-zebra.c
blob: 7cd25286d38ed563b3a8272947c3d11ad028f0c3 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Copyright (c) 2019, Anthony Latorre <tlatorre at uchicago>
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

#include "zebra.h"
#include <stdio.h>
#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;
}