summaryrefslogtreecommitdiff
path: root/tests/refcmp-metadata.awk
blob: fa21aad0e0401b613e14b8e4930acbf52dcf67c4 (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
# Compare metadata filter output containing float value strings to reference
# output data. Returns the whole reference data if delta of each value is below
# threshold, else returns the whole input data.

function abs(val) {
    return ((val < 0.0) ? -val : val);
}

function max(val1, val2) {
    return ((val1 >= val2) ? val1 : val2);
}

function is_numeric_str(str) {
    return (str ~ /^[+-]?[0-9]*\.?[0-9]+$/);
}

BEGIN {
    FS = "=";
    # check for "fuzz" (threshold) program parameter, else use default
    if (fuzz <= 0.0) {
        fuzz = 0.1;
    }
    # check for "ref" (reference file) program parameter
    if (ref) {
        ref_nr = 0;
        while ((getline line < ref) > 0) {
            ref_nr++;
            ref_lines[ref_nr] = line;
            if (split(line, fields) == 2 && is_numeric_str(fields[2])) {
                ref_keys[ref_nr] = fields[1];
                ref_vals[ref_nr] = fields[2] + 0;  # convert to number
            }
        }
        close(ref);
    }
    delta_max = 0;
    result = (ref ? 1 : 0);
}

{
    cmp_lines[NR] = $0;
    if (NF == 2 && is_numeric_str($2) && ref_vals[NR]) {
        val = $2 + 0;  # convert to number
        delta = abs((val / ref_vals[NR]) - 1);
        delta_max = max(delta_max, delta);
        result = result && ($1 == ref_keys[NR]) && (delta <= fuzz);
    } else {
        result = result && ($0 == ref_lines[NR]);
    }
}

END {
    if (result) {
        for (i = 1; i <= ref_nr; i++)
            print ref_lines[i];
    } else {
        for (i = 1; i <= NR; i++)
            print cmp_lines[i];
        if (NR != ref_nr)
            print "[refcmp] lines: " NR " != " ref_nr > "/dev/stderr";
        if (delta_max >= fuzz)
            print "[refcmp] delta_max: " delta_max " >= " fuzz > "/dev/stderr";
    }
}