test-sha1: test hashing large buffer
test to hash a large buffer in one go is more important than hashing large amount of data in small fixed chunks. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jun 24, 2006 at 02:59 UTC
b57cbbf8a8630b6d75c885348dc79b8b8cb96c2e
2 files changed
+32
-9
test-sha1.c
+31
-8
@@ -4,21 +4,44 @@ int main(int ac, char **av)
4
{
5
SHA_CTX ctx;
6
unsigned char sha1[20];
7
+ unsigned bufsz = 8192;
8
+ char *buffer;
9
+
10
+ if (ac == 2)
11
+ bufsz = strtoul(av[1], NULL, 10) * 1024 * 1024;
12
+
13
+ if (!bufsz)
14
+ bufsz = 8192;
15
+
16
+ while ((buffer = malloc(bufsz)) == NULL) {
17
+ fprintf(stderr, "bufsz %u is too big, halving...\n", bufsz);
18
+ bufsz /= 2;
19
+ if (bufsz < 1024)
20
+ die("OOPS");
21
+ }
22
23
SHA1_Init(&ctx);
24
25
while (1) {
11
- ssize_t sz;
12
- char buffer[8192];
13
- sz = xread(0, buffer, sizeof(buffer));
14
- if (sz == 0)
26
+ ssize_t sz, this_sz;
27
+ char *cp = buffer;
28
+ unsigned room = bufsz;
29
+ this_sz = 0;
30
+ while (room) {
31
+ sz = xread(0, cp, room);
32
+ if (sz == 0)
33
+ break;
34
+ if (sz < 0)
35
+ die("test-sha1: %s", strerror(errno));
36
+ this_sz += sz;
37
+ cp += sz;
38
+ room -= sz;
39
+ }
40
+ if (this_sz == 0)
41
break;
16
- if (sz < 0)
17
- die("test-sha1: %s", strerror(errno));
18
- SHA1_Update(&ctx, buffer, sz);
42
+ SHA1_Update(&ctx, buffer, this_sz);
43
}
44
SHA1_Final(sha1, &ctx);
45
puts(sha1_to_hex(sha1));
46
exit(0);
47
}
24
-
test-sha1.sh
+1
-1
@@ -11,7 +11,7 @@ do
11
test -z "$pfx" || echo "$pfx"
12
dd if=/dev/zero bs=1048576 count=$cnt 2>/dev/null |
13
tr '[\0]' '[g]'
14
- } | ./test-sha1
14
+ } | ./test-sha1 $cnt
15
`
16
if test "$expect" = "$actual"
17
then