diff --git a/Makefile.am b/Makefile.am index f1e7bc3..9f7dd5c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,6 @@ -SUBDIRS = src doc -DIST_SUBDIRS = $(SUBDIRS) test +SUBDIRS = src test doc +DIST_SUBDIRS = $(SUBDIRS) EXTRA_DIST = bootstrap AUTHORS AUTOMAKE_OPTIONS = foreign dist-bzip2 diff --git a/test/Makefile.am b/test/Makefile.am index 9769079..7353897 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,3 +1,8 @@ EXTRA_DIST = testsuite.sh +noinst_PROGRAMS = fdcat streamcat + +fdcat_SOURCES = fdcat.c +streamcat_SOURCES = streamcat.c + diff --git a/test/fdcat.c b/test/fdcat.c new file mode 100644 index 0000000..edaabd1 --- /dev/null +++ b/test/fdcat.c @@ -0,0 +1,66 @@ +/* + * fdcat - cat reimplementation + * Copyright (c) 2006 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +#include "config.h" + +#define _LARGEFILE64_SOURCE /* for lseek64() */ + +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + long int pos; + unsigned char *data; + int i, j, fd; + + srand(0); + + if(argc != 2) + return EXIT_FAILURE; + + fd = open(argv[1], O_RDONLY); + if(fd < 0) + return EXIT_FAILURE; + + pos = lseek(fd, 0, SEEK_END); + if(pos < 0) + return EXIT_FAILURE; + + /* Read the whole file */ + data = malloc(pos); + lseek(fd, 0, SEEK_SET); + read(fd, data, pos); + + /* Read shit here and there */ + for(i = 0; i < 128; i++) + { + lseek(fd, rand() % pos, SEEK_SET); + for(j = 0; j < 16; j++) + read(fd, data + lseek(fd, 0, SEEK_CUR), rand() % 4096); + lseek64(fd, rand() % pos, SEEK_SET); + for(j = 0; j < 16; j++) + read(fd, data + lseek(fd, 0, SEEK_CUR), rand() % 4096); + } + + fwrite(data, pos, 1, stdout); + + return EXIT_SUCCESS; +} + diff --git a/test/streamcat.c b/test/streamcat.c new file mode 100644 index 0000000..4b2977b --- /dev/null +++ b/test/streamcat.c @@ -0,0 +1,67 @@ +/* + * streamcat - cat reimplementation + * Copyright (c) 2006 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +#include "config.h" + +#include +#include +#include + +int main(int argc, char *argv[]) +{ + long int pos; + unsigned char *data; + int i, j; + FILE *stream; + + srand(0); + + if(argc != 2) + return EXIT_FAILURE; + + stream = fopen(argv[1], "r"); + if(!stream) + return EXIT_FAILURE; + + fseek(stream, 0, SEEK_END); + pos = ftell(stream); + if(pos < 0) + return EXIT_FAILURE; + + /* Read the whole file */ + data = malloc(pos + 16); /* 16 safety bytes */ + fseek(stream, 0, SEEK_SET); + fread(data, pos, 1, stream); + + /* Read shit here and there */ + for(i = 0; i < 128; i++) + { + long int now; + fseek(stream, rand() % pos, SEEK_SET); + for(j = 0; j < 16; j++) + fread(data + ftell(stream), rand() % 4096, 1, stream); + fseek(stream, rand() % pos, SEEK_SET); + now = ftell(stream); + for(j = 0; j < 16; j++) + data[now + j] = getc(stream); + now = ftell(stream); + for(j = 0; j < 16; j++) + data[now + j] = fgetc(stream); + } + + fwrite(data, pos, 1, stdout); + + return EXIT_SUCCESS; +} + diff --git a/test/testsuite.sh b/test/testsuite.sh index 139754d..b4cc1b5 100755 --- a/test/testsuite.sh +++ b/test/testsuite.sh @@ -41,6 +41,8 @@ trap "echo ''; echo 'Aborted.'; cleanup; exit 0" 1 2 15 seed=$(($RANDOM * $$)) ZZUF="$(dirname "$0")/../src/zzuf" +FDCAT="$(dirname "$0")/fdcat" +STRAMCAT="$(dirname "$0")/streamcat" FAILED=0 TESTED=0 @@ -62,6 +64,8 @@ for file in /tmp/zzuf-text-$$ /tmp/zzuf-zero-$$ /tmp/zzuf-random-$$; do check $seed $r "dd bs=1111 if=$file" "dd(bs=1111)" check $seed $r "dd bs=1024 if=$file" "dd(bs=1024)" check $seed $r "dd bs=1 if=$file" "dd(bs=1)" + check $seed $r "$FDCAT $file" "fdcat" + check $seed $r "$STRAMCAT $file" "streamcat" if [ "$OK" != 1 ]; then echo "*** FAILED ***" FAILED=$(($FAILED + 1))