File: genmaptest.c

package info (click to toggle)
libhsync 0.5.7-1.2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,060 kB
  • ctags: 543
  • sloc: sh: 7,944; ansic: 5,413; makefile: 154
file content (300 lines) | stat: -rw-r--r-- 7,039 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*=                                     -*- c-file-style: "bsd" -*-
 *
 * $Id: genmaptest.c,v 1.8 2000/08/27 08:58:03 mbp Exp $
 *
 * Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


                                        /*
                                         | `It is easier to fight for our
                                         | principles than to live up to
                                         | them' -- Alfred Adler
                                         */


/* genmaptest -- Make up a test case and expected output for hsmapread.
 */


#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>


static char const *usage =
"Usage: genmaptest SERIES NUMTESTS CMDS EXPECT INPUT SEED\n";


/*
 * SIZE is the total length of the file; all of it is copied into the
 * newly allocated BUF.
 */
static int
open_all(char const *cmds_name,
         char const *expect_name,
         char const *input_name,
         char **buf, size_t *size,
         FILE **expect, FILE **cmds)
{
    struct stat          stat_buf;
    FILE                *in;

    if (!(in = fopen(input_name, "rb"))) {
        perror(input_name);
        return 1;
    }

    if (fstat(fileno(in), &stat_buf) < 0) {
        perror(input_name);
        return 1;
    }

    if (!(*expect = fopen(expect_name, "wb"))) {
        perror(expect_name);
        return 1;
    }

    if (!(*cmds = fopen(cmds_name, "wt"))) {
        perror(cmds_name);
        return 1;
    }


    /* Read all input. */
    *size = stat_buf.st_size;
    if (!(*buf = malloc(*size))) {
        perror("malloc"); return 1;
    }
    if (fread(*buf, 1, *size, in) != *size) {
        perror("read"); return 1;
    }

    return 0;
}


static void
emit(char const *buf, size_t size, off_t off, size_t len,
     FILE *expect, FILE *cmds)
{
    assert(off >= 0);

    fprintf(cmds, "%ld,%ld ", (long) off, (long) len);

    /* if this starts after the end, do nothing */
    if (off < (off_t) size) {
        /* if this runs past the end, truncate */
        if (off + len > size) {
            len = size - off;
        }

        fwrite(buf+off, 1, len, expect);
    }
}


/* Generate random maps within the file. */
static void
gen_map(int ntests, char const *buf, size_t size,
        FILE *expect, FILE *cmds)
{
    int i;
    off_t               off;
    size_t              len, remain;

    for (i = 0; i < ntests; i++) {
        off = rand() % size;
        remain = size - off;
        len = 1 + (rand() % remain);
        emit(buf, size, off, len, expect, cmds);
    }                           
}


/* Generate random maps possibly overlapping the end of the file. */
static void
gen_mapover(int ntests, char const *buf, size_t size,
            FILE *expect, FILE *cmds)
{
    int i;
    off_t               off;
    size_t              len, remain;

    for (i = 0; i < ntests; i++) {
        off = rand() % (2 * size);
        remain = size - off;
        len = rand() % (2 * size) + 1;
        emit(buf, size, off, len, expect, cmds);
    }
}


static void
gen_forward(int ntests, char const *buf, size_t size,
            FILE *expect, FILE *cmds)
{
    int i;
    off_t               off;
    size_t              len;

    i = 0; off = 0;
    while ((size_t) off < size  &&  i < ntests) {
        len = rand() % 8192 + 1;
        emit(buf, size, off, len, expect, cmds);

        off += rand() % len;
        i++;
    }
}


/*
 * Read the first NTESTS bytes one at a time, then the rest in one big
 * chunk.
 */
static void
gen_ones(int ntests, char const *buf, size_t size,
         FILE *expect, FILE *cmds)
{
    int                 i;

    for (i = 0; (size_t) i < size  &&  i < ntests; i++) {
        emit(buf, size, i, 1, expect, cmds);
    }

    if ((size_t) i < size)
        emit(buf, size, i, size - i, expect, cmds);
}



/*
 * Generate a pattern which looks like the decoder: read 1, read 1-7,
 * read 1-10000.
 */
static void
gen_decoder(int ntests, char const *buf, size_t size,
            FILE *expect, FILE *cmds)
{
    off_t pos = 0;
    size_t l;

    while (1) {
        if (ntests-- <= 0)
            return;
        l = 1;
        emit(buf, size, pos, l, expect, cmds);
        
        if (ntests-- <= 0)
            return;
        l = rand() % 14 + 1;
        emit(buf, size, pos, l, expect, cmds);
        pos += l;
        
        if (ntests-- <= 0)
            return;
        l = rand() % 50000 + 1;
        emit(buf, size, pos, l, expect, cmds);
        pos += l;

        if (pos > (off_t) size)
            return;
    }
}


/*
 * Generate instructions walking forward through the file in small
 * steps, similarly to nad encoding.
 */
static void
gen_stepping(int ntests, char const *buf, size_t size,
             FILE *expect, FILE *cmds)
{
    size_t              i;
    size_t              c;

    i = 0;
    while (ntests--) {
        c = rand() % 64 + 1;
        emit(buf, size, i, c, expect, cmds);
        i += rand() % c;
    }

    /* finally read any remaining data */
    if (size > i)
        emit(buf, size, i, size-i, expect, cmds);
}




static const struct {
    char const *name;
    void (*fn)(int ntests, char const *buf, size_t size,
               FILE *expect, FILE *cmds);
} all_tests[] = {
    { "map", gen_map },
    { "mapover", gen_mapover },
    { "forward", gen_forward },
    { "ones", gen_ones },
    { "stepping", gen_stepping },
    { "decoder", gen_decoder },
    { NULL, NULL }
};


int
main(int argc, char **argv) {
    char const          *series;
    FILE                *expect, *cmds;
    char                *buf;
    size_t              size;
    int                 numtests;
    int                 i;

    if (argc != 7) {
        fputs(usage, stderr);
        return 1;
    }

    if (open_all(argv[3], argv[4],argv[5], &buf, &size, &expect, &cmds))
        return 1;

    series = argv[1];
    numtests = atoi(argv[2]);

    srand(atoi(argv[6]));

    for (i = 0; all_tests[i].name; i++) {
        if (!strcmp(series, all_tests[i].name)) {
            all_tests[i].fn(numtests, buf, size, expect, cmds);
            return 0;
        }
    }
        
    fprintf(stderr, "unknown series %s\n", series);
    return 1;
}