find-first-fail: Passes testsuite.

This commit is contained in:
Ole Tange 2020-11-14 01:10:13 +01:00
parent c445e9352f
commit f3277b3331
2 changed files with 54 additions and 11 deletions

View file

@ -268,6 +268,7 @@ find-first-fail() {
}
local a="$1"
local b="$2"
# TODO if defined memory{$a,$b}: return value
shift
shift
local cmd=("$@")
@ -279,6 +280,7 @@ find-first-fail() {
eval "$not" _inner_run "$quiet"
# Postpone exit code
local _exit="$?"
# TODO memory{$a,$b}="$_exit"
rm "$tmp"
return "$_exit"
}
@ -287,13 +289,16 @@ find-first-fail() {
local low=$1
local high=$2
if [ $low -gt $(($high - 2)) ]; then
# if $low > $high - 0.2: $global_low = $low
global_low=$low
return
fi
shift
shift
local middle=$(( ( $low + $high ) / 2 ))
if _run_file $middle $global_high "$@" ; then
local middledeci=$(( $middle / 10 ))
local global_highdeci=$(( $global_high / 10 ))
if _run_file $middledeci $global_highdeci "$@" ; then
low=$middle
else
high=$middle
@ -304,14 +309,17 @@ find-first-fail() {
_find-high-file() {
local low=$1
local high=$2
if [ $low -gt $(($high - 2)) ]; then
global_high=$high
if [ $low -gt $(($high - 5)) ]; then
# if $low > $high - 0.5: $global_high = $high + 0.5
global_high=$(($high + 5))
return
fi
shift
shift
local middle=$(( ( $low + $high ) / 2 ))
if _run_file $global_low $middle "$@" ; then
local middledeci=$(( $middle / 10 ))
local global_lowdeci=$(( $global_low / 10 ))
if _run_file $global_lowdeci $middledeci "$@" ; then
high=$middle
else
low=$middle
@ -322,23 +330,29 @@ find-first-fail() {
_find_in_file() {
# If function(1) = false: run 'not function()' instead
local not
local global_low=$(($start+1))
if [ ! -e "$inputfile" ] ; then
echo "find-first-fail: Error: File not found '$inputfile'" >&2
return 1
fi
local global_high=$(wc -l < $inputfile)
if _run_file "$global_low" "$global_high" "$@" ; then
local not
local global_low=$(( $start + 1 ))0
local global_high=$(wc -l < $inputfile)0
local global_lowdeci=$(( $global_low / 10 ))
local global_highdeci=$(( $global_high / 10 ))
if _run_file "$global_lowdeci" "$global_highdeci" "$@" ; then
not=''
else
not='!'
fi
# Binary search for $global_low
_find-low-file $global_low $global_high "$@"
# Binary search for minimal $global_high that fails
_find-high-file $global_low $global_high "$@"
# Binary search for minimal $global_low that fails
_find-low-file $global_low $global_high "$@"
local global_lowdeci=$(( $global_low / 10 ))
local global_highdeci=$(( $global_high / 10 ))
# Print the resulting minimal file
perl -ne "($start and 1..$start) and print" "$inputfile"
perl -ne "$global_low..$global_high and print" "$inputfile"
perl -ne "$global_lowdeci..$global_highdeci and print" "$inputfile"
unset low high start quiet
}

View file

@ -65,6 +65,35 @@ test_header() {
find-first-fail -s1 -f $tmp myparser
}
test_file_start_middle_end() {
tmp=`tempfile`
testfile() {
(echo MyHeader
echo $1
echo 2
echo $2
echo 4
echo $3) > "$tmp"
}
myparser() { perl -ne 'if($_ > 10) { exit 1 }' "$@"; }
export -f myparser
echo 'Find 1001'
testfile 1001 3 5
find-first-fail -s1 -f "$tmp" myparser
echo 'Find 1003'
testfile 1 1003 5
find-first-fail -s1 -f "$tmp" myparser
echo 'Find 1005'
testfile 1 3 1005
find-first-fail -s1 -f "$tmp" myparser
echo 'Find 1001/1005'
testfile 1001 3 1005
find-first-fail -s1 -f "$tmp" myparser
echo 'Find 1001/1003/1005'
testfile 1001 1003 1005
find-first-fail -s1 -f "$tmp" myparser
}
export -f $(compgen -A function | grep test_)
compgen -A function | grep test_ | LC_ALL=C sort |
parallel --timeout 1000% --tag -k --joblog /tmp/jl-`basename $0` '{} 2>&1'