#! /bin/sh

#
# fshash-test                                                   (jh,30.04.2011)
#

#
#   Copyright (C) 2004, 2005, 2011  Jochen Hepp <jochen.hepp@gmx.de>
#
#   This file is part of fshash.
#
#   fshash is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   fshash 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 General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

script="${0##*/}"
cmd="$PWD/../${script%-test}"
tests=


# --- make_tmpdir ---

make_tmpdir () {
	local dir

	dir="$(mktemp -d -t $script.XXXXXX)"
	if [ -z "$dir" ]; then
		echo "$script: can't create temporary directory" >&2
		exit 1
	fi
	echo "$dir"
}


# --- init ---

init () { # [DIR] [TYPE=file,perm,time,content] ...
          # DIR:=data|hash
          # see create () for TYPE=file,perm,time,content
          # global: workdir, hashdir
	local dir
	local d

	mkdir -p "$workdir"
	dir="$workdir"

	while [ $# -gt 0 ]; do
		case "$1" in
			work:)
				dir="$workdir"
				;;
			hash:)
				if [ ! -d "$hashdir" ]; then
					mkdir -p "$hashdir"
				fi
				dir="$hashdir"
				;;
			*)
				create "$dir" "$1"
				;;
		esac
		shift
	done
}


# --- create ---

create () { # dir TYPE=file,perm,time,content
            #     TYPE=file|link|dir|block|char|uchar|fifo
            #     content is mayor_minor when TYPE is block, char or uchar
	local dir="$1"
	local arg="$2"
	local type; local file; local perm; local time; local content
	local dir_file
	local d

	arg="$(echo "$arg" | sed 's/ /_/g; s/[=,]/ /g')"
	set -- $arg
	type="$1"
	file="$(echo "$2" | sed 's/_/ /g')"
	perm="$3"
	time="${4:-0}"
	content="$(echo "$5" | sed 's/_/ /g')"

	dir_file="$dir/$file"
	d="${dir_file%/*}"
	if [ ! -d "$d" ]; then
		mkdir -p "$d"
	fi

	case "$type" in
		file)
			echo "$content" >"$dir_file"
			chmod "$perm" "$dir_file"
			touch -d $((20000101+$time)) "$dir_file"
			;;
		link)
			ln -s "$content" "$dir_file"
			;;
		dir)
			mkdir -p -m "$perm" "$dir_file"
			;;
		block)
			mknod -m "$perm" "$dir_file" b $content
			touch -d $((20000101+$time)) "$dir_file"
			;;
		char)
			mknod -m "$perm" "$dir_file" c $content
			touch -d $((20000101+$time)) "$dir_file"
			;;
		uchar)
			mknod -m "$perm" "$dir_file" u $content
			touch -d $((20000101+$time)) "$dir_file"
			;;
		fifo)
			mkfifo -m "$perm" "$dir_file"
			touch -d $((20000101+$time)) "$dir_file"
			;;
		*)
			echo "$script: $type: can't create this type" >&2
			exit 1
			;;
	esac
}


# --- listdirs ---

listdirs () { # global: tmpdir, workdir, hashdir
	cd "$tmpdir" && \
	find hash work | \
	LC_ALL=C sort | \
	while read file; do
		ls -1 -d -g --no-group --time-style='+' "$file"
	done | \
	sed 's/[[:blank:]]\+[0-9]\+[[:blank:]]\+/ /;
	     /^[dl]/ { s/[[:blank:]]\+[0-9]\+[[:blank:]]\+/ /; b }
	     s/[[:blank:]][[:blank:]]\+/ /'

	cd "$tmpdir" && \
	find hash -type f | \
	LC_ALL=C sort | \
	while read file; do
		echo "$file"
		sed 's/^/|/' "$file" | \
		LC_ALL=C sort
	done
}


# --- clean ---

clean () { # global: workdir, hashdir
	if [ -d "$workdir" ]; then
		rm -rf "$workdir"
	fi
	if [ -d "$hashdir" ]; then
		rm -rf "$hashdir"
	fi
}


# --- diff_sed2nd ---

diff_sed2nd () { # global: tmpdir
	sed "s%$tmpdir\(/work\)\?%%g" "$3" | \
	diff "$1" "$2" -
}


# --- diff_listdirs ---

diff_listdirs () {
	listdirs | \
	diff "$1" "$2" -
}


# --- tests ---

tests="$tests no_args"
no_args () {
	[ -x "$cmd" ] && ! "$cmd" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && diff_sed2nd -u ref/help "$err" >"$diff"
}

tests="$tests error_unknown_option"
error_unknown_option () {
	[ -x "$cmd" ] && ! "$cmd" --unknown >"$out" 2>"$err" && \
	[ ! -s "$out" ] && diff -u ref/error_unknown_option "$err" >"$diff"
}

tests="$tests error_unknown_command"
error_unknown_command () {
	[ -x "$cmd" ] && ! "$cmd" unknown >"$out" 2>"$err" && \
	[ ! -s "$out" ] && diff -u ref/error_unknown_command "$err" >"$diff"
}

tests="$tests error_no_dataroot"
error_no_dataroot () {
	[ -x "$cmd" ] && \
	! "$cmd" --dataroot=/NONE found test >"$out" 2>"$err" && \
	[ ! -s "$out" ] && diff_sed2nd -u ref/error_no_dataroot "$err" >"$diff"
}

tests="$tests error_no_hashroot"
error_no_hashroot () {
	[ -x "$cmd" ] && \
	! "$cmd" --dataroot=/ --hashroot=/NONE found test >"$out" 2>"$err" && \
	[ ! -s "$out" ] && diff_sed2nd -u ref/error_no_hashroot "$err" >"$diff"
}

tests="$tests error_dataroot_sub_of_hashroot"
error_dataroot_sub_of_hashroot () {
	[ -x "$cmd" ] && \
	! "$cmd" --dataroot=/tmp --hashroot=/ found test >"$out" 2>"$err" && \
	[ ! -s "$out" ] && \
	diff_sed2nd -u ref/error_dataroot_sub_of_hashroot "$err" >"$diff"
}

tests="$tests error_hash_in_sub_exists"
error_hash_in_sub_exists () {
	init work: dir=dir/test,755,0 \
	     hash: "file=dir/test,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	[ -x "$cmd" ] && ! "$cmd" generate "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && \
	diff_sed2nd -u ref/error_hash_in_sub_exists "$err" >"$diff"
}

tests="$tests error_generate_no_file_or_dir"
error_generate_no_file_or_dir () {
	init hash:
	[ -x "$cmd" ] && ! "$cmd" generate "$workdir/NONE" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && \
	diff_sed2nd -u ref/error_no_file_or_dir "$err" >"$diff"
}

tests="$tests error_lostquick_no_file_or_dir"
error_lostquick_no_file_or_dir () {
	init hash:
	[ -x "$cmd" ] && ! "$cmd" lostquick "$workdir/NONE" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && \
	diff_sed2nd -u ref/error_no_file_or_dir "$err" >"$diff"
}

tests="$tests generate_recursive_basedir"
generate_recursive_basedir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world
	"$cmd" generate "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_dir.ls >"$diff"
}

tests="$tests generate_recursive_basedir_updir"
generate_recursive_basedir_updir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world
	"$cmd" generate "$workdir/dir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_dir.ls >"$diff"
}

tests="$tests generate_recursive_basedir_space"
generate_recursive_basedir_space () {
	init work: "file=dir  space/test  1,644,0,content" \
	           "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world"
	"$cmd" generate "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_dir_space.ls >"$diff"
}

tests="$tests generate_recursive_subdir"
generate_recursive_subdir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world
	"$cmd" generate "$workdir/dir/sub" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir.ls >"$diff"
}

tests="$tests generate_recursive_subdir_updir"
generate_recursive_subdir_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world
	"$cmd" generate "$workdir/dir/aaa/bb/c/../../../sub" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir.ls >"$diff"
}

tests="$tests generate_recursive_subdir_space"
generate_recursive_subdir_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world"
	"$cmd" generate "$workdir/dir  space/sub  dir" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir_space.ls >"$diff"
}

tests="$tests generate_single_basedir"
generate_single_basedir () {
	init work: file=test,644,0,content
	"$cmd" generate "$workdir/test" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_single.ls >"$diff"
}

tests="$tests generate_single_basedir_updir"
generate_single_basedir_updir () {
	init work: file=test,644,0,content
	"$cmd" generate "$workdir/aaa/bb/c/../../../test" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_single.ls >"$diff"
}

tests="$tests generate_single_basedir_space"
generate_single_basedir_space () {
	init work: "file=test  file,644,0,content"
	"$cmd" generate "$workdir/test  file" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_single_space.ls >"$diff"
}

tests="$tests generate_single_subdir"
generate_single_subdir () {
	init work: file=dir/test,644,0,content
	"$cmd" generate "$workdir/dir/test" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_single_subdir.ls >"$diff"
}

tests="$tests generate_single_subdir_updir"
generate_single_subdir_updir () {
	init work: file=dir/test,644,0,content
	"$cmd" generate "$workdir/dir/aaa/bb/c/../../../test" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_single_subdir.ls >"$diff"
}

tests="$tests generate_single_subdir_space"
generate_single_subdir_space () {
	init work: "file=dir  space/test  file,644,0,content"
	"$cmd" generate "$workdir/dir  space/test  file" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_single_subdir_space.ls >"$diff"
}

tests="$tests add_recursive_basedir"
add_recursive_basedir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	     hash:
	"$cmd" add "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_dir.ls >"$diff"
}

tests="$tests add_recursive_basedir_updir"
add_recursive_basedir_updir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	     hash:
	"$cmd" add "$workdir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_dir.ls >"$diff"
}

tests="$tests add_recursive_basedir_space"
add_recursive_basedir_space () {
	init work: "file=dir  space/test  1,644,0,content" \
	           "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	     hash:
	"$cmd" add "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_dir_space.ls >"$diff"
}

tests="$tests add_recursive_subdir"
add_recursive_subdir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: dir=dir,755,0
	"$cmd" add "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir.ls >"$diff"
}

tests="$tests add_recursive_subdir_updir"
add_recursive_subdir_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: dir=dir,755,0
	"$cmd" add "$workdir/dir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir.ls >"$diff"
}

tests="$tests add_recursive_subdir_space"
add_recursive_subdir_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	     hash: "dir=dir  space,755,0"
	"$cmd" add "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir_space.ls >"$diff"
}

tests="$tests add_single_basedir"
add_single_basedir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	     hash: "file=dir,644,0,b1946ac92492d2347c6235b4d2611184  ./test2"
	"$cmd" add "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_dir.ls >"$diff"
}

tests="$tests add_single_basedir_updir"
add_single_basedir_updir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	     hash: "file=dir,644,0,b1946ac92492d2347c6235b4d2611184  ./test2"
	"$cmd" add "$workdir/dir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_dir.ls >"$diff"
}

tests="$tests add_single_basedir_space"
add_single_basedir_space () {
	init work: "file=dir  space/test  1,644,0,content" \
	           "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	     hash: "file=dir  space,644,0,b1946ac92492d2347c6235b4d2611184  ./test  2"
	"$cmd" add "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_dir_space.ls >"$diff"
}

tests="$tests add_single_subdir"
add_single_subdir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: "file=dir/sub,644,0,b1946ac92492d2347c6235b4d2611184  ./test2"
	"$cmd" add "$workdir/dir/sub" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir.ls >"$diff"
}

tests="$tests add_single_subdir_updir"
add_single_subdir_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: "file=dir/sub,644,0,b1946ac92492d2347c6235b4d2611184  ./test2"
	"$cmd" add "$workdir/dir/aaa/bb/c/../../../sub" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir.ls >"$diff"
}

tests="$tests add_single_subdir_space"
add_single_subdir_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	     hash: "file=dir  space/sub  dir,644,0,b1946ac92492d2347c6235b4d2611184  ./test  2"
	"$cmd" add "$workdir/dir  space/sub  dir" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir_space.ls >"$diff"
}

tests="$tests add_single_deeper"
add_single_deeper () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: "file=dir/sub,644,0,b1946ac92492d2347c6235b4d2611184  ./test2"
	"$cmd" add "$workdir/dir/sub/test1" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir_nodeep.ls >"$diff"
}

tests="$tests add_single_deeper_updir"
add_single_deeper_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: "file=dir/sub,644,0,b1946ac92492d2347c6235b4d2611184  ./test2"
	"$cmd" add "$workdir/dir/aaa/bb/c/../../../sub/test1" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir_nodeep.ls >"$diff"
}

tests="$tests add_single_deeper_space"
add_single_deeper_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	     hash: "file=dir  space/sub  dir,644,0,b1946ac92492d2347c6235b4d2611184  ./test  2"
	"$cmd" add "$workdir/dir  space/sub  dir/test  1" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir_space_nodeep.ls >"$diff"
}

tests="$tests add_single_higher"
add_single_higher () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: "file=dir/sub,644,0,b1946ac92492d2347c6235b4d2611184  ./test2"
	"$cmd" add "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir.ls >"$diff"
}

tests="$tests add_single_higher_updir"
add_single_higher_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: "file=dir/sub,644,0,b1946ac92492d2347c6235b4d2611184  ./test2"
	"$cmd" add "$workdir/dir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir.ls >"$diff"
}

tests="$tests add_single_higher_space"
add_single_higher_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	     hash: "file=dir  space/sub  dir,644,0,b1946ac92492d2347c6235b4d2611184  ./test  2"
	"$cmd" add "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/hash_subdir_space.ls >"$diff"
}

tests="$tests check_recursive_basedir"
check_recursive_basedir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	     hash: "file=dir,644,0,b1946ac92492d2347c6235b4d2611184  ./test2"
	grep -v '/test2$' <ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" check "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests check_recursive_basedir_updir"
check_recursive_basedir_updir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	     hash: "file=dir,644,0,b1946ac92492d2347c6235b4d2611184  ./test2"
	grep -v '/test2$' <ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" check "$workdir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests check_recursive_basedir_space"
check_recursive_basedir_space () {
	init work: "file=dir  space/test  1,644,0,content" \
	           "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	     hash: "file=dir  space,644,0,b1946ac92492d2347c6235b4d2611184  ./test  2"
	grep -v '/test  2$' <ref/hash_dir_space.md5sum >>"$hashdir/dir  space"
	"$cmd" check "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests check_recursive_subdir"
check_recursive_subdir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: "file=dir/sub,644,0,b1946ac92492d2347c6235b4d2611184  ./test2"
	grep -v '/test2$' <ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" check "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests check_recursive_subdir_updir"
check_recursive_subdir_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: "file=dir/sub,644,0,b1946ac92492d2347c6235b4d2611184  ./test2"
	grep -v '/test2$' <ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" check "$workdir/dir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests check_recursive_subdir_space"
check_recursive_subdir_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	     hash: "file=dir  space/sub  dir,644,0,b1946ac92492d2347c6235b4d2611184  ./test  2"
	grep -v '/test  2$' <ref/hash_subdir_space.md5sum \
	     >>"$hashdir/dir  space/sub  dir"
	"$cmd" check "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests check_single_basedir"
check_single_basedir () {
	init work: file=test,644,0,content \
	     hash: "file=test,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	"$cmd" check "$workdir/test" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests check_single_basedir_updir"
check_single_basedir_updir () {
	init work: file=test,644,0,content \
	     hash: "file=test,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	"$cmd" check "$workdir/aaa/bb/c/../../../test" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests check_single_basedir_space"
check_single_basedir_space () {
	init work: "file=test  file,644,0,content" \
	     hash: "file=test  file,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	"$cmd" check "$workdir/test  file" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests check_single_subdir"
check_single_subdir () {
	init work: file=dir/test,644,0,content \
	     hash: "file=dir/test,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	"$cmd" check "$workdir/dir/test" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests check_single_subdir_updir"
check_single_subdir_updir () {
	init work: file=dir/test,644,0,content \
	     hash: "file=dir/test,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	"$cmd" check "$workdir/dir/aaa/bb/c/../../../test" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests check_single_subdir_space"
check_single_subdir_space () {
	init work: "file=dir  space/test  file,644,0,content" \
	     hash: "file=dir  space/test  file,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	"$cmd" check "$workdir/dir  space/test  file" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests check_single_deeper"
check_single_deeper () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: "file=dir/sub,644,0,b1946ac92492d2347c6235b4d2611184  ./test2"
	grep -v '/test2$' <ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" check "$workdir/dir/sub/test2" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests check_single_deeper_updir"
check_single_deeper_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: "file=dir/sub,644,0,b1946ac92492d2347c6235b4d2611184  ./test2"
	grep -v '/test2$' <ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" check "$workdir/dir/aaa/bb/c/../../../sub/test2" \
	       >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests check_single_deeper_space"
check_single_deeper_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	     hash: "file=dir  space/sub  dir,644,0,b1946ac92492d2347c6235b4d2611184  ./test  2"
	grep -v '/test  2$' <ref/hash_subdir_space.md5sum \
	     >>"$hashdir/dir  space/sub  dir"
	"$cmd" check "$workdir/dir  space/sub  dir/test  2" >"$out" 2>"$err" && \
	[ ! -s "$out" ] && [ ! -s "$err" ]
}

tests="$tests hashsum_recursive_basedir"
hashsum_recursive_basedir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	     hash: file=dir,644,0,dummy
	cat ref/hash_dir.md5sum >"$hashdir/dir"
	"$cmd" hashsum "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/hash_dir.hashsum "$out" >"$diff"
}

tests="$tests hashsum_recursive_basedir_updir"
hashsum_recursive_basedir_updir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	     hash: file=dir,644,0,dummy
	cat ref/hash_dir.md5sum >"$hashdir/dir"
	"$cmd" hashsum "$workdir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/hash_dir.hashsum "$out" >"$diff"
}

tests="$tests hashsum_recursive_basedir_space"
hashsum_recursive_basedir_space () {
	init work: "file=dir  space/test  1,644,0,content" \
	           "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	     hash: "file=dir  space,644,0,dummy"
	cat ref/hash_dir_space.md5sum >"$hashdir/dir  space"
	"$cmd" hashsum "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/hash_dir_space.hashsum "$out" >"$diff"
}

tests="$tests hashsum_recursive_subdir"
hashsum_recursive_subdir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: file=dir/sub,644,0,dummy
	cat ref/hash_subdir.md5sum >"$hashdir/dir/sub"
	"$cmd" hashsum "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/hash_subdir.hashsum "$out" >"$diff"
}

tests="$tests hashsum_recursive_subdir_updir"
hashsum_recursive_subdir_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: file=dir/sub,644,0,dummy
	cat ref/hash_subdir.md5sum >"$hashdir/dir/sub"
	"$cmd" hashsum "$workdir/dir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/hash_subdir.hashsum "$out" >"$diff"
}

tests="$tests hashsum_recursive_subdir_space"
hashsum_recursive_subdir_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	     hash: "file=dir  space/sub  dir,644,0,dummy"
	cat ref/hash_subdir_space.md5sum >"$hashdir/dir  space/sub  dir"
	"$cmd" hashsum "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/hash_subdir_space.hashsum "$out" >"$diff"
}

tests="$tests hashsum_single_basedir"
hashsum_single_basedir () {
	init work: file=test,644,0,content \
	     hash: "file=test,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	"$cmd" hashsum "$workdir/test" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/hash_single.hashsum "$out" >"$diff"
}

tests="$tests hashsum_single_basedir_updir"
hashsum_single_basedir_updir () {
	init work: file=test,644,0,content \
	     hash: "file=test,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	"$cmd" hashsum "$workdir/aaa/bb/c/../../../test" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/hash_single.hashsum "$out" >"$diff"
}

tests="$tests hashsum_single_basedir_space"
hashsum_single_basedir_space () {
	init work: "file=test  file,644,0,content" \
	     hash: "file=test  file,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	"$cmd" hashsum "$workdir/test  file" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/hash_single_space.hashsum "$out" >"$diff"
}

tests="$tests hashsum_single_subdir"
hashsum_single_subdir () {
	init work: file=dir/test,644,0,content \
	     hash: "file=dir/test,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	"$cmd" hashsum "$workdir/dir/test" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/hash_single_subdir.hashsum "$out" >"$diff"
}

tests="$tests hashsum_single_subdir_updir"
hashsum_single_subdir_updir () {
	init work: file=dir/test,644,0,content \
	     hash: "file=dir/test,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	"$cmd" hashsum "$workdir/dir/aaa/bb/c/../../../test" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/hash_single_subdir.hashsum "$out" >"$diff"
}

tests="$tests hashsum_single_subdir_space"
hashsum_single_subdir_space () {
	init work: "file=dir  space/test  file,644,0,content" \
	     hash: "file=dir  space/test  file,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	"$cmd" hashsum "$workdir/dir  space/test  file" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/hash_single_subdir_space.hashsum "$out" >"$diff"
}

tests="$tests hashsum_single_deeper"
hashsum_single_deeper () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: file=dir/sub,644,0,dummy
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" hashsum "$workdir/dir/sub/test2" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/hash_single_deeper.hashsum "$out" >"$diff"
}

tests="$tests hashsum_single_deeper_updir"
hashsum_single_deeper_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	     hash: file=dir/sub,644,0,dummy
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" hashsum "$workdir/dir/aaa/bb/c/../../../sub/test2" \
	       >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/hash_single_deeper.hashsum "$out" >"$diff"
}

tests="$tests hashsum_single_deeper_space"
hashsum_single_deeper_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	     hash: "file=dir  space/sub  dir,644,0,dummy"
	cat ref/hash_subdir_space.md5sum >>"$hashdir/dir  space/sub  dir"
	"$cmd" hashsum "$workdir/dir  space/sub  dir/test  2" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && \
	diff -u ref/hash_single_deeper_space.hashsum "$out" >"$diff"
}

tests="$tests lost_recursive_basedir"
lost_recursive_basedir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lost "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_dir_all "$out" >"$diff"
}

tests="$tests lost_recursive_basedir_updir"
lost_recursive_basedir_updir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lost "$workdir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_dir_all "$out" >"$diff"
}

tests="$tests lost_recursive_basedir_space"
lost_recursive_basedir_space () {
	init work: "file=dir  space/test  1,644,0,content" \
	           "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	           "file=new  1,644,0,baby" \
	           "file=dir  space/new  2,644,0,born" \
	           "file=dir  space/sub  dir/new  3,644,0,mama" \
	     hash: "file=dir  space,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir_space.md5sum >>"$hashdir/dir  space"
	"$cmd" lost "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_dir_space_all "$out" >"$diff"
}

tests="$tests lost_recursive_subdir"
lost_recursive_subdir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" lost "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_subdir_all "$out" >"$diff"
}

tests="$tests lost_recursive_subdir_updir"
lost_recursive_subdir_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" lost "$workdir/dir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_subdir_all "$out" >"$diff"
}

tests="$tests lost_recursive_subdir_space"
lost_recursive_subdir_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	           "file=dir  space/new  1,644,0,baby" \
	           "file=dir  space/sub  dir/new  2,644,0,born" \
	           "file=dir  space/sub  dir/deep  down/new  3,644,0,mama" \
	     hash: "file=dir  space/sub  dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=dir  space/g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir_space.md5sum >>"$hashdir/dir  space/sub  dir"
	"$cmd" lost "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_subdir_space_all "$out" >"$diff"
}

tests="$tests lost_single_basedir"
lost_single_basedir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lost "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_dir "$out" >"$diff"
}

tests="$tests lost_single_basedir_updir"
lost_single_basedir_updir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lost "$workdir/aaa/bb/c/../../../dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_dir "$out" >"$diff"
}

tests="$tests lost_single_basedir_space"
lost_single_basedir_space () {
	init work: "file=dir  space/test  1,644,0,content" \
	           "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	           "file=new  1,644,0,baby" \
	           "file=dir  space/new  2,644,0,born" \
	           "file=dir  space/sub  dir/new  3,644,0,mama" \
	     hash: "file=dir  space,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir_space.md5sum >>"$hashdir/dir  space"
	"$cmd" lost "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_dir_space "$out" >"$diff"
}

tests="$tests lost_single_subdir"
lost_single_subdir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" lost "$workdir/dir/sub" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_subdir "$out" >"$diff"
}

tests="$tests lost_single_subdir_updir"
lost_single_subdir_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" lost "$workdir/dir/aaa/bb/c/../../../sub" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_subdir "$out" >"$diff"
}

tests="$tests lost_single_subdir_space"
lost_single_subdir_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	           "file=dir  space/new  1,644,0,baby" \
	           "file=dir  space/sub  dir/new  2,644,0,born" \
	           "file=dir  space/sub  dir/deep  down/new  3,644,0,mama" \
	     hash: "file=dir  space/sub  dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=dir  space/g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir_space.md5sum >>"$hashdir/dir  space/sub  dir"
	"$cmd" lost "$workdir/dir  space/sub  dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_subdir_space "$out" >"$diff"
}

tests="$tests lost_single_deeper"
lost_single_deeper () {
	init work: file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./sub/treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lost "$workdir/dir/sub" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_deeper "$out" >"$diff"
}

tests="$tests lost_single_deeper_updir"
lost_single_deeper_updir () {
	init work: file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./sub/treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lost "$workdir/dir/aaa/bb/c/../../../sub" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_deeper "$out" >"$diff"
}

tests="$tests lost_single_deeper_space"
lost_single_deeper_space () {
	init work: "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	           "file=new  1,644,0,baby" \
	           "file=dir  space/new  2,644,0,born" \
	           "file=dir  space/sub  dir/new  3,644,0,mama" \
	     hash: "file=dir  space,644,0,5488d13aac0974ec5831e3e089f39a24  ./sub  dir/treasure  x" \
	           "file=g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir_space.md5sum >>"$hashdir/dir  space"
	"$cmd" lost "$workdir/dir  space/sub  dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lost_deeper_space "$out" >"$diff"
}

tests="$tests found_recursive_basedir"
found_recursive_basedir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" found "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_dir_all "$out" >"$diff"
}

tests="$tests found_recursive_basedir_updir"
found_recursive_basedir_updir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" found "$workdir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_dir_all "$out" >"$diff"
}

tests="$tests found_recursive_basedir_space"
found_recursive_basedir_space () {
	init work: "file=dir  space/test  1,644,0,content" \
	           "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	           "file=new  1,644,0,baby" \
	           "file=dir  space/new  2,644,0,born" \
	           "file=dir  space/sub  dir/new  3,644,0,mama" \
	     hash: "file=dir  space,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir_space.md5sum >>"$hashdir/dir  space"
	"$cmd" found "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_dir_space_all "$out" >"$diff"
}

tests="$tests found_recursive_subdir"
found_recursive_subdir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" found "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_subdir_all "$out" >"$diff"
}

tests="$tests found_recursive_subdir_updir"
found_recursive_subdir_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" found "$workdir/dir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_subdir_all "$out" >"$diff"
}

tests="$tests found_recursive_subdir_space"
found_recursive_subdir_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	           "file=dir  space/new  1,644,0,baby" \
	           "file=dir  space/sub  dir/new  2,644,0,born" \
	           "file=dir  space/sub  dir/deep  down/new  3,644,0,mama" \
	     hash: "file=dir  space/sub  dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=dir  space/g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir_space.md5sum >>"$hashdir/dir  space/sub  dir"
	"$cmd" found "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_subdir_space_all "$out" >"$diff"
}

tests="$tests found_single_basedir"
found_single_basedir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" found "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_dir "$out" >"$diff"
}

tests="$tests found_single_basedir_updir"
found_single_basedir_updir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" found "$workdir/aaa/bb/c/../../../dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_dir "$out" >"$diff"
}

tests="$tests found_single_basedir_space"
found_single_basedir_space () {
	init work: "file=dir  space/test  1,644,0,content" \
	           "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	           "file=new  1,644,0,baby" \
	           "file=dir  space/new  2,644,0,born" \
	           "file=dir  space/sub  dir/new  3,644,0,mama" \
	     hash: "file=dir  space,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir_space.md5sum >>"$hashdir/dir  space"
	"$cmd" found "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_dir_space "$out" >"$diff"
}

tests="$tests found_single_subdir"
found_single_subdir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" found "$workdir/dir/sub" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_subdir "$out" >"$diff"
}

tests="$tests found_single_subdir_updir"
found_single_subdir_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" found "$workdir/dir/aaa/bb/c/../../../sub" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_subdir "$out" >"$diff"
}

tests="$tests found_single_subdir_space"
found_single_subdir_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	           "file=dir  space/new  1,644,0,baby" \
	           "file=dir  space/sub  dir/new  2,644,0,born" \
	           "file=dir  space/sub  dir/deep  down/new  3,644,0,mama" \
	     hash: "file=dir  space/sub  dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=dir  space/g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir_space.md5sum >>"$hashdir/dir  space/sub  dir"
	"$cmd" found "$workdir/dir  space/sub  dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_subdir_space "$out" >"$diff"
}

tests="$tests found_single_deeper"
found_single_deeper () {
	init work: file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./sub/treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" found "$workdir/dir/sub" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_deeper "$out" >"$diff"
}

tests="$tests found_single_deeper_updir"
found_single_deeper_updir () {
	init work: file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./sub/treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" found "$workdir/dir/aaa/bb/c/../../../sub" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_deeper "$out" >"$diff"
}

tests="$tests found_single_deeper_space"
found_single_deeper_space () {
	init work: "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	           "file=new  1,644,0,baby" \
	           "file=dir  space/new  2,644,0,born" \
	           "file=dir  space/sub  dir/new  3,644,0,mama" \
	     hash: "file=dir  space,644,0,5488d13aac0974ec5831e3e089f39a24  ./sub  dir/treasure  x" \
	           "file=g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir_space.md5sum >>"$hashdir/dir  space"
	"$cmd" found "$workdir/dir  space/sub  dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/found_deeper_space "$out" >"$diff"
}

tests="$tests lostfound_recursive_basedir"
lostfound_recursive_basedir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lostfound "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_dir_all "$out" >"$diff"
}

tests="$tests lostfound_recursive_basedir_updir"
lostfound_recursive_basedir_updir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lostfound "$workdir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_dir_all "$out" >"$diff"
}

tests="$tests lostfound_recursive_basedir_space"
lostfound_recursive_basedir_space () {
	init work: "file=dir  space/test  1,644,0,content" \
	           "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	           "file=new  1,644,0,baby" \
	           "file=dir  space/new  2,644,0,born" \
	           "file=dir  space/sub  dir/new  3,644,0,mama" \
	     hash: "file=dir  space,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir_space.md5sum >>"$hashdir/dir  space"
	"$cmd" lostfound "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_dir_space_all "$out" >"$diff"
}

tests="$tests lostfound_recursive_subdir"
lostfound_recursive_subdir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" lostfound "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_subdir_all "$out" >"$diff"
}

tests="$tests lostfound_recursive_subdir_updir"
lostfound_recursive_subdir_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" lostfound "$workdir/dir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_subdir_all "$out" >"$diff"
}

tests="$tests lostfound_recursive_subdir_space"
lostfound_recursive_subdir_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	           "file=dir  space/new  1,644,0,baby" \
	           "file=dir  space/sub  dir/new  2,644,0,born" \
	           "file=dir  space/sub  dir/deep  down/new  3,644,0,mama" \
	     hash: "file=dir  space/sub  dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=dir  space/g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir_space.md5sum >>"$hashdir/dir  space/sub  dir"
	"$cmd" lostfound "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_subdir_space_all "$out" >"$diff"
}

tests="$tests lostfound_single_basedir"
lostfound_single_basedir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lostfound "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_dir "$out" >"$diff"
}

tests="$tests lostfound_single_basedir_updir"
lostfound_single_basedir_updir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lostfound "$workdir/aaa/bb/c/../../../dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_dir "$out" >"$diff"
}

tests="$tests lostfound_single_basedir_space"
lostfound_single_basedir_space () {
	init work: "file=dir  space/test  1,644,0,content" \
	           "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	           "file=new  1,644,0,baby" \
	           "file=dir  space/new  2,644,0,born" \
	           "file=dir  space/sub  dir/new  3,644,0,mama" \
	     hash: "file=dir  space,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir_space.md5sum >>"$hashdir/dir  space"
	"$cmd" lostfound "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_dir_space "$out" >"$diff"
}

tests="$tests lostfound_single_subdir"
lostfound_single_subdir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" lostfound "$workdir/dir/sub" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_subdir "$out" >"$diff"
}

tests="$tests lostfound_single_subdir_updir"
lostfound_single_subdir_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" lostfound "$workdir/dir/aaa/bb/c/../../../sub" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_subdir "$out" >"$diff"
}

tests="$tests lostfound_single_subdir_space"
lostfound_single_subdir_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	           "file=dir  space/new  1,644,0,baby" \
	           "file=dir  space/sub  dir/new  2,644,0,born" \
	           "file=dir  space/sub  dir/deep  down/new  3,644,0,mama" \
	     hash: "file=dir  space/sub  dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=dir  space/g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir_space.md5sum >>"$hashdir/dir  space/sub  dir"
	"$cmd" lostfound "$workdir/dir  space/sub  dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_subdir_space "$out" >"$diff"
}

tests="$tests lostfound_single_deeper"
lostfound_single_deeper () {
	init work: file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./sub/treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lostfound "$workdir/dir/sub" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_deeper "$out" >"$diff"
}

tests="$tests lostfound_single_deeper_updir"
lostfound_single_deeper_updir () {
	init work: file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./sub/treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lostfound "$workdir/dir/aaa/bb/c/../../../sub" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_deeper "$out" >"$diff"
}

tests="$tests lostfound_single_deeper_space"
lostfound_single_deeper_space () {
	init work: "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	           "file=new  1,644,0,baby" \
	           "file=dir  space/new  2,644,0,born" \
	           "file=dir  space/sub  dir/new  3,644,0,mama" \
	     hash: "file=dir  space,644,0,5488d13aac0974ec5831e3e089f39a24  ./sub  dir/treasure  x" \
	           "file=g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir_space.md5sum >>"$hashdir/dir  space"
	"$cmd" lostfound "$workdir/dir  space/sub  dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfound_deeper_space "$out" >"$diff"
}

tests="$tests lostquick_basedir"
lostquick_basedir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	           file=newdir/pizza1,644,0,cheese \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lostquick "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostquick_dir "$out" >"$diff"
}

tests="$tests lostquick_basedir_updir"
lostquick_basedir_updir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	           file=newdir/pizza1,644,0,cheese \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lostquick "$workdir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostquick_dir "$out" >"$diff"
}

tests="$tests lostquick_basedir_space"
lostquick_basedir_space () {
	init work: "file=dir  space/test  1,644,0,content" \
	           "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	           "file=new  1,644,0,baby" \
	           "file=dir  space/new  2,644,0,born" \
	           "file=dir  space/sub  dir/new  3,644,0,mama" \
	           "file=new  dir/pizza  1,644,0,cheese" \
	     hash: "file=dir  space,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir_space.md5sum >>"$hashdir/dir  space"
	"$cmd" lostquick "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostquick_dir_space "$out" >"$diff"
}

tests="$tests lostquick_subdir"
lostquick_subdir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	           file=dir/newdir/pizza1,644,0,cheese \
	           file=otherdir/drink1,644,0,milk \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" lostquick "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostquick_subdir "$out" >"$diff"
}

tests="$tests lostquick_subdir_updir"
lostquick_subdir_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	           file=dir/newdir/pizza1,644,0,cheese \
	           file=otherdir/drink1,644,0,milk \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" lostquick "$workdir/dir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostquick_subdir "$out" >"$diff"
}

tests="$tests lostquick_subdir_space"
lostquick_subdir_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	           "file=dir  space/new  1,644,0,baby" \
	           "file=dir  space/sub  dir/new  2,644,0,born" \
	           "file=dir  space/sub  dir/deep  down/new  3,644,0,mama" \
	           "file=dir  space/new  dir/pizza  1,644,0,cheese" \
	           "file=other  dir/drink  1,644,0,milk" \
	     hash: "file=dir  space/sub  dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=dir  space/g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir_space.md5sum >>"$hashdir/dir  space/sub  dir"
	"$cmd" lostquick "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostquick_subdir_space "$out" >"$diff"
}

tests="$tests foundquick_basedir"
foundquick_basedir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	           file=newdir/pizza1,644,0,cheese \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" foundquick "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/foundquick_dir "$out" >"$diff"
}

tests="$tests foundquick_basedir_updir"
foundquick_basedir_updir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	           file=newdir/pizza1,644,0,cheese \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" foundquick "$workdir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/foundquick_dir "$out" >"$diff"
}

tests="$tests foundquick_basedir_space"
foundquick_basedir_space () {
	init work: "file=dir  space/test  1,644,0,content" \
	           "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	           "file=new  1,644,0,baby" \
	           "file=dir  space/new  2,644,0,born" \
	           "file=dir  space/sub  dir/new  3,644,0,mama" \
	           "file=new  dir/pizza  1,644,0,cheese" \
	     hash: "file=dir  space,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir_space.md5sum >>"$hashdir/dir  space"
	"$cmd" foundquick "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/foundquick_dir_space "$out" >"$diff"
}

tests="$tests foundquick_subdir"
foundquick_subdir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	           file=dir/newdir/pizza1,644,0,cheese \
	           file=otherdir/drink1,644,0,milk \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" foundquick "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/foundquick_subdir "$out" >"$diff"
}

tests="$tests foundquick_subdir_updir"
foundquick_subdir_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	           file=dir/newdir/pizza1,644,0,cheese \
	           file=otherdir/drink1,644,0,milk \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" foundquick "$workdir/dir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/foundquick_subdir "$out" >"$diff"
}

tests="$tests foundquick_subdir_space"
foundquick_subdir_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	           "file=dir  space/new  1,644,0,baby" \
	           "file=dir  space/sub  dir/new  2,644,0,born" \
	           "file=dir  space/sub  dir/deep  down/new  3,644,0,mama" \
	           "file=dir  space/new  dir/pizza  1,644,0,cheese" \
	           "file=other  dir/drink  1,644,0,milk" \
	     hash: "file=dir  space/sub  dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=dir  space/g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir_space.md5sum >>"$hashdir/dir  space/sub  dir"
	"$cmd" foundquick "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/foundquick_subdir_space "$out" >"$diff"
}

tests="$tests lostfoundquick_basedir"
lostfoundquick_basedir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	           file=newdir/pizza1,644,0,cheese \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lostfoundquick "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfoundquick_dir "$out" >"$diff"
}

tests="$tests lostfoundquick_basedir_updir"
lostfoundquick_basedir_updir () {
	init work: file=dir/test1,644,0,content \
	           file=dir/test2,644,0,hello \
	           file=dir/sub/test3,644,0,world \
	           file=new1,644,0,baby \
	           file=dir/new2,644,0,born \
	           file=dir/sub/new3,644,0,mama \
	           file=newdir/pizza1,644,0,cheese \
	     hash: "file=dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir.md5sum >>"$hashdir/dir"
	"$cmd" lostfoundquick "$workdir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfoundquick_dir "$out" >"$diff"
}

tests="$tests lostfoundquick_basedir_space"
lostfoundquick_basedir_space () {
	init work: "file=dir  space/test  1,644,0,content" \
	           "file=dir  space/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/test  3,644,0,world" \
	           "file=new  1,644,0,baby" \
	           "file=dir  space/new  2,644,0,born" \
	           "file=dir  space/sub  dir/new  3,644,0,mama" \
	           "file=new  dir/pizza  1,644,0,cheese" \
	     hash: "file=dir  space,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_dir_space.md5sum >>"$hashdir/dir  space"
	"$cmd" lostfoundquick "$workdir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfoundquick_dir_space "$out" >"$diff"
}

tests="$tests lostfoundquick_subdir"
lostfoundquick_subdir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	           file=dir/newdir/pizza1,644,0,cheese \
	           file=otherdir/drink1,644,0,milk \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" lostfoundquick "$workdir/dir" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfoundquick_subdir "$out" >"$diff"
}

tests="$tests lostfoundquick_subdir_updir"
lostfoundquick_subdir_updir () {
	init work: file=dir/sub/test1,644,0,content \
	           file=dir/sub/test2,644,0,hello \
	           file=dir/sub/deep/test3,644,0,world \
	           file=dir/new1,644,0,baby \
	           file=dir/sub/new2,644,0,born \
	           file=dir/sub/deep/new3,644,0,mama \
	           file=dir/newdir/pizza1,644,0,cheese \
	           file=otherdir/drink1,644,0,milk \
	     hash: "file=dir/sub,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure" \
	           "file=dir/gpl,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir.md5sum >>"$hashdir/dir/sub"
	"$cmd" lostfoundquick "$workdir/dir/aaa/bb/c/../../.." >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfoundquick_subdir "$out" >"$diff"
}

tests="$tests lostfoundquick_subdir_space"
lostfoundquick_subdir_space () {
	init work: "file=dir  space/sub  dir/test  1,644,0,content" \
	           "file=dir  space/sub  dir/test  2,644,0,hello" \
	           "file=dir  space/sub  dir/deep  down/test  3,644,0,world" \
	           "file=dir  space/new  1,644,0,baby" \
	           "file=dir  space/sub  dir/new  2,644,0,born" \
	           "file=dir  space/sub  dir/deep  down/new  3,644,0,mama" \
	           "file=dir  space/new  dir/pizza  1,644,0,cheese" \
	           "file=other  dir/drink  1,644,0,milk" \
	     hash: "file=dir  space/sub  dir,644,0,5488d13aac0974ec5831e3e089f39a24  ./treasure  x" \
	           "file=dir  space/g  p  l,644,0,03c0c8d9c9bd80523ff274a252c6ebc0  -"
	cat ref/hash_subdir_space.md5sum >>"$hashdir/dir  space/sub  dir"
	"$cmd" lostfoundquick "$workdir/dir  space" >"$out" 2>"$err" && \
	[ ! -s "$err" ] && diff -u ref/lostfoundquick_subdir_space "$out" >"$diff"
}

tests="$tests cmd_mkdir"
cmd_mkdir () {
	init work: file=test,644,0,content \
	     hash:
	(cd "$workdir" && \
	 "$cmd" mkdir testdir >"$out" 2>"$err") && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/cmd_mkdir.ls >"$diff"
}

tests="$tests cmd_rmdir"
cmd_rmdir () {
	init work: file=test,644,0,content \
	           dir=testdir,755,0 \
	     hash: dir=testdir,755,0
	(cd "$workdir" && \
	 "$cmd" rmdir testdir >"$out" 2>"$err") && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/cmd_rmdir.ls >"$diff"
}

tests="$tests cmd_mv"
cmd_mv () {
	init work: file=test,644,0,content \
	     hash: "file=test,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	(cd "$workdir" && \
	 "$cmd" mv test newname >"$out" 2>"$err") && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/cmd_mv.ls >"$diff"
}

tests="$tests cmd_rm"
cmd_rm () {
	init work: file=test,644,0,content \
	     hash: "file=test,644,0,f75b8179e4bbe7e2b4a074dcef62de95  -"
	(cd "$workdir" && \
	 "$cmd" rm test >"$out" 2>"$err") && \
	[ ! -s "$out" ] && [ ! -s "$err" ] && \
	diff_listdirs -u ref/cmd_rm.ls >"$diff"
}

tests="$tests version"
version () {
	"$cmd" -V >"$out" 2>"$err" && \
	sed '1 s/ .*$//' "$out" | \
	diff -u ref/version - >"$diff" && [ ! -s "$err" ]
}

tests="$tests version_long"
version_long () {
	"$cmd" --version >"$out" 2>"$err" && \
	sed '1 s/ .*$//' "$out" | \
	diff -u ref/version - >"$diff" && [ ! -s "$err" ]
}

tests="$tests help"
help () {
	"$cmd" -h >"$out" 2>"$err" && \
	diff_sed2nd -u ref/help "$out" >"$diff" && [ ! -s "$err" ]
}

tests="$tests help_long"
help_long () {
	"$cmd" --help >"$out" 2>"$err" && \
	diff_sed2nd -u ref/help "$out" >"$diff" && [ ! -s "$err" ]
}


# --- error_report ---

error_report () { # name file
	local name="$1"
	local file="$2"

	if [ -f "$file" ]; then
		if [ -s "$file" ]; then
			echo "$name:"
			cat "$file"
		else
			echo "$name: (none)"
		fi
	fi
}


# --- main ---

result='ok'
count=0

tmpdir="$(make_tmpdir)"
workdir="$tmpdir/work"
hashdir="$tmpdir/hash"
out="$tmpdir/stdout"
err="$tmpdir/stderr"
content="$tmpdir/content"
diff="$tmpdir/diff"
errorlog="$tmpdir/error.log"
export DATAROOT="$workdir"
export HASHROOT="$hashdir"

if [ -e 'error.log' ]; then
	rm -f error.log
fi

echo "testsuite ${script%-test}"
echo "----------------------------------------"

for func in $tests; do
	if "$func" && [ ! -e "$diff" -o ! -s "$diff" ]; then
		rc='ok'
	else
		rc='failed'
		count=$(($count+1))
		result='failed'
		{
			echo "test: $func"
			error_report 'stdout' "$out"
			error_report 'stderr' "$err"
			error_report 'diff'   "$diff"
			echo
		} >>"$errorlog"
	fi
	rm -f "$out" "$err" "$diff"
	clean
	awk -v n="$func" -v r="$rc" 'BEGIN { \
			l=length(n)+1+length(r); \
			while (l<40) { l++; n=n "." }; print n " " r }'
done

echo "----------------------------------------"

if [ -e "$errorlog" ]; then
	mv "$errorlog" error.log
fi
rm -rf "$tmpdir"

if [ "$result" = 'ok' ]; then
	echo "all tests passed."
	exit 0
else
	if [ "$test" = 1 ]; then
		echo '1 test failed.'
	else
		echo "$count tests failed."
	fi
	echo "see file 'error.log' for details."
	exit 1
fi


# --- end ---

