From f3277b333159976a0fceaaa052f545d55d252b9a Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Sat, 14 Nov 2020 01:10:13 +0100 Subject: [PATCH] find-first-fail: Passes testsuite. --- find-first-fail/find-first-fail | 36 +++++++++++++++++++++++---------- find-first-fail/testsuite | 29 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/find-first-fail/find-first-fail b/find-first-fail/find-first-fail index af90362..fbd10b0 100755 --- a/find-first-fail/find-first-fail +++ b/find-first-fail/find-first-fail @@ -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 } diff --git a/find-first-fail/testsuite b/find-first-fail/testsuite index 8b78f8e..e3c1b32 100644 --- a/find-first-fail/testsuite +++ b/find-first-fail/testsuite @@ -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'