File: compare_ints.c

package info (click to toggle)
codec2 1.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 121,056 kB
  • sloc: ansic: 414,118; sh: 2,612; objc: 2,574; python: 2,105; cpp: 2,091; asm: 683; makefile: 598
file content (159 lines) | stat: -rw-r--r-- 3,956 bytes parent folder | download
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
/* compare ints - a test utility */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <inttypes.h>

/*  Declarations */

/* Globals */

/* Functions */
int get_data(FILE *f, int64_t *dd, int signed_flag, int bytes) {
    int res;
    int8_t   d_8;
    int16_t  d_16;
    uint8_t  d_u8;
    uint16_t d_u16;
    // TODO Loop on reads until, but catch EOF!!
    if (signed_flag) {
        switch (bytes) {
            case 1: 
                res = fread(&d_8, bytes, 1, f);
                *dd = d_8;
                break;
            case 2: 
                res = fread(&d_16, bytes, 1, f);
                *dd = d_16;
                break;
            default: 
                fprintf(stderr, "Error: unsupported size %d bytes\n", bytes);
                exit(1);
            }
        } 
    else {  // unsigned
        switch (bytes) {
            case 1: 
                res = fread(&d_u8, bytes, 1, f);
                *dd = d_u8;
                break;
            case 2: 
                res = fread(&d_u16, bytes, 1, f);
                *dd = d_u16;
                break;
            default: 
                fprintf(stderr, "Error: unsupported size %d bytes\n", bytes);
                exit(1);
            }
        }

    if (res != 1) return(0);
    else return(1);
    }


/* Main */

int main(int argc, char *argv[]) {

    char usage[] = "Usage: %s [-b size_in_bytes] [-c] [-s] [-t tolerance] [-n numerrorstoexit] file1 file2\n";

    int bytes = 1;
    int count_errors = 0;
    int signed_flag = 0;
    int tol = 1;
    int numerrorstoexit = -1;
    
    int opt;
    while ((opt = getopt(argc, argv, "b:cst:n:")) != -1) {
        switch (opt) {
            case 'b':
                bytes = atoi(optarg);
                break;
            case 'c':
                count_errors = 1;
                break;
            case 's':
                signed_flag = 1;
                break;
            case 'n':
                numerrorstoexit = atoi(optarg);
                break;
            case 't':
                tol = atof(optarg);
                break;
            default:
                fprintf(stderr, usage, argv[0]);
                exit(1);
            }
        }

    if ((optind + 2) > argc) {
        fprintf(stderr, usage, argv[0]);
        exit(1);
        }
    char *fname1 = argv[optind++];
    char *fname2 = argv[optind++];

    FILE *f1 = fopen(fname1, "rb");
    if (f1 == NULL) {
        fprintf(stderr, "Error opening file1 \"%s\": ", fname1);
        perror(NULL);
        exit(1);
        }

    FILE *f2 = fopen(fname2, "rb");
    if (f2 == NULL) {
        fprintf(stderr, "Error opening file2 \"%s\": ", fname2);
        perror(NULL);
        exit(1);
        }

    // Convert inputs to SIGNED long values 
    int64_t data1, data2;

    int count = 0;
    int errors = 0;
    int rms_sum = 0;

    while (get_data(f1, &data1, signed_flag, bytes)) {
        if (!get_data(f2, &data2, signed_flag, bytes)) {
            fprintf(stderr, "Error: file2 is shorter\n");
            exit(1);
        }
        uint64_t err = llabs(data1 - data2);
        if (err > tol) {
            errors ++;
            printf("%d %" PRId64 " %" PRId64 "\n", count, data1, data2);
	    if (numerrorstoexit != -1)
	        if (errors > numerrorstoexit) {
		    printf("reached errors: %d, bailing!", numerrorstoexit);
		    exit(1);
		}
        }
        rms_sum += (err * err);
        count ++;
    }
    if (get_data(f2, &data2, signed_flag, bytes)) {
        fprintf(stderr, "Error: file1 is shorter\n");
        exit(1);
    }

    if (count_errors) exit(errors);
    else {
        if (errors) {
            printf("Fail: %d errors\n", errors);
            printf("      rms error = %f\n", ((double)rms_sum/count));
            exit(1);
            }
        else printf("Pass\n");
        exit(0);
        }

    } // main


/* vi:set ts=4 et sts=4: */