From b65bc21e7d8dc8cafc70dfa6354cb66b8874b2d9 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 24 Jun 2006 00:59:49 -0700 Subject: [PATCH] Makefile: add framework to verify and bench sha1 implementations. Signed-off-by: Junio C Hamano --- Makefile | 6 ++++ test-sha1.c | 24 +++++++++++++++ test-sha1.sh | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 test-sha1.c create mode 100755 test-sha1.sh diff --git a/Makefile b/Makefile index e29e3fa38..ea0044d62 100644 --- a/Makefile +++ b/Makefile @@ -636,6 +636,12 @@ test-delta$X: test-delta.c diff-delta.o patch-delta.o test-dump-cache-tree$X: dump-cache-tree.o $(GITLIBS) $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) +test-sha1$X: test-sha1.o $(GITLIBS) + $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) + +check-sha1:: test-sha1$X + ./test-sha1.sh + check: for i in *.c; do sparse $(ALL_CFLAGS) $(SPARSE_FLAGS) $$i || exit; done diff --git a/test-sha1.c b/test-sha1.c new file mode 100644 index 000000000..2efc315ee --- /dev/null +++ b/test-sha1.c @@ -0,0 +1,24 @@ +#include "cache.h" + +int main(int ac, char **av) +{ + SHA_CTX ctx; + unsigned char sha1[20]; + + SHA1_Init(&ctx); + + while (1) { + ssize_t sz; + char buffer[8192]; + sz = xread(0, buffer, sizeof(buffer)); + if (sz == 0) + break; + if (sz < 0) + die("test-sha1: %s", strerror(errno)); + SHA1_Update(&ctx, buffer, sz); + } + SHA1_Final(sha1, &ctx); + puts(sha1_to_hex(sha1)); + exit(0); +} + diff --git a/test-sha1.sh b/test-sha1.sh new file mode 100755 index 000000000..01bbb5782 --- /dev/null +++ b/test-sha1.sh @@ -0,0 +1,83 @@ +#!/bin/sh + +dd if=/dev/zero bs=1048576 count=100 2>/dev/null | +/usr/bin/time ./test-sha1 >/dev/null + +while read expect cnt pfx +do + case "$expect" in '#'*) continue ;; esac + actual=` + { + test -z "$pfx" || echo "$pfx" + dd if=/dev/zero bs=1048576 count=$cnt 2>/dev/null | + tr '[\0]' '[g]' + } | ./test-sha1 + ` + if test "$expect" = "$actual" + then + echo "OK: $expect $cnt $pfx" + else + echo >&2 "OOPS: $cnt" + echo >&2 "expect: $expect" + echo >&2 "actual: $actual" + exit 1 + fi +done </dev/null | + tr '[\0]' '[g]' + } | sha1sum | + sed -e 's/ .*//' + ` + echo "$actual $cnt $pfx" +done <