mirror of
https://github.com/git/git.git
synced 2026-03-05 14:59:04 +01:00
The `test-tool genrandom` test helper can be used to generate random
data, either as an infinite stream or with a specified number of bytes.
The way we handle parsing the number of bytes is lacking though:
- We don't have good error handling, so if the caller for example uses
`test-tool genrandom 200xyz` then we'll end up generating 200 bytes
of random data successfully.
- Many callers want to generate e.g. 1 kilobyte or megabyte of data,
but they have to either use unwieldy numbers like 1048576, or they
have to precompute them.
Fix both of these issues by using `git_parse_ulong()` to parse the
argument. This function has better error handling, and it knows to
handle unit suffixes.
Adapt a couple of our tests to use suffixes instead of manual
computations.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
38 lines
857 B
C
38 lines
857 B
C
/*
|
|
* Simple random data generator used to create reproducible test files.
|
|
* This is inspired from POSIX.1-2001 implementation example for rand().
|
|
* Copyright (C) 2007 by Nicolas Pitre, licensed under the GPL version 2.
|
|
*/
|
|
|
|
#include "test-tool.h"
|
|
#include "git-compat-util.h"
|
|
#include "parse.h"
|
|
|
|
int cmd__genrandom(int argc, const char **argv)
|
|
{
|
|
unsigned long count, next = 0;
|
|
unsigned char *c;
|
|
|
|
if (argc < 2 || argc > 3) {
|
|
fprintf(stderr, "usage: %s <seed_string> [<size>]\n", argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
c = (unsigned char *) argv[1];
|
|
do {
|
|
next = next * 11 + *c;
|
|
} while (*c++);
|
|
|
|
count = ULONG_MAX;
|
|
if (argc == 3 && !git_parse_ulong(argv[2], &count))
|
|
return error_errno("cannot parse argument '%s'", argv[2]);
|
|
|
|
while (count--) {
|
|
next = next * 1103515245 + 12345;
|
|
if (putchar((next >> 16) & 0xff) == EOF)
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|