#!/bin/sh 
# test2 is supposed to fail, do not expect success.
#  With -byvalue it gets one error, without -byvalue it gets two.
echo "start at " `date`
# test17 is also supposed to fail, it adds 8 twice.
myexit=0
#The following line assumes the test cases have been
#placed in a directory named 'testcases' and
#that the .gz versions expanded.
#The tsearch test data is in the 
#libdwarf regression tests (available from sourceforge)
#in the directory
#     tsearchtests
# It's appropriate to copy the tests from regressiontests
# into 'testcases'
# Not change what b refers to.
# gunzip the .gz files in testcases, not in regressiontets
# Note that the output file testpass and the basline
# testcases/testpass.base # are filled with pointers as hex
# values so  we transform those below before diff-ing.
#
savetemps=n
rmtemps() {
  rm -f testpasserrs
  rm -f junkdiffout
  rm -f junkbase
  rm -f junknew 
  rm -f testpass
  rm -f testfails
}
fixpointers() {
   sed 's/0x[0-9a-f]*/0xyyy/g' <$1 >$2
}
rmtemps
if [ $# -ne 0 ]
then
  if [ "$1" = "-save" ]
  then
    savetemps=y
  else
    echo " Option $1 is unrecognized."
    echo " Perhaps you meant -save"
    echo " to save the temp files, not delete them"
    exit 1
  fi
fi
stsecs=`date '+%s'`
b=testcases

#These test expecting failure.
echo >testfail
for opts in "" -byvalue
do
  for test in $b/test2 $b/test17
  do
    for app  in  ./binarysearch ./eppingerdel ./balancedsearch ./redblack ./gnusearch ./hashsearch
    do
      name=`basename $app`
      echo '====' $name $opts $test  '====' >>testfail
      $app $opts $test >>testfail
    done
  done
done

diff  $b/testfail.base testfail  >junkdiffout
r=$?
head -12 junkdiffout
if [  $r -ne 0 ]
then
   echo "FAIL because testfail got unexpected output."
   echo "We only show the first 12 lines of difference"
   echo "To update: mv testfail $b/testfail.base"
   echo "To diff  : diff $b/testfail.base testfail"
   myexit=1
else
   echo PASS the testfail  set of tests.
fi

echo >testpass
for opts in "" -byvalue
do
  for app  in  ./binarysearch ./eppingerdel ./balancedsearch ./redblack ./gnusearch ./hashsearch
  do
    name=`basename $app`
    echo '====' $name $opts '====' >>testpass 
    $app $opts                     >>testpass
  done
done

for opts in "" -byvalue
do
  for test in $b/action-F-dwgenb-dwarfgen.log $b/action-f-v-v-irix64-libc.so.log \
  $b/action-i-dwarf4-dd2g4.5dwarf-4.log  \
  $b/action-l-irix64-libc.so.log \
  $b/test10 \
  $b/test11 \
  $b/test11b \
  $b/test11c \
  $b/test18 \
  $b/test32 \
  $b/test547 \
  $b/test6 \
  $b/test64 \
  $b/test8 \
  $b/test9
  do
    for app  in  ./binarysearch ./eppingerdel ./balancedsearch ./redblack ./gnusearch ./hashsearch
    do
      name=`basename $app`
      echo '====' $name $opts $test  '====' >>testpass 
      $app $opts $test                      >>testpass
    done
  done
done

fixpointers $b/testpass.base junkbase
fixpointers testpass         junknew
diff junkbase junknew  >junkdiffout
r=$?
head -12 junkdiffout
if [ $r -ne 0 ]
then
   echo "FAIL because testpass got unexpected output."
   echo "Just showing the first 12 lines of the diff."
   echo "To see the detailed diff"
   echo "try fldiff $b/testpass.base testpass"
   echo "To update baseline mv testpass $b/testpass.base"
   myexit=1
else
   echo "PASS the testpass set of tests. Congratulations."
   echo "Note that we turned all hex values into 0xyyy"
   echo "as pointers rarely match."
fi
lcb=`wc -l < $b/testpass.base`
lcn=`wc -l < testpass`
if [ $lcb -eq $lcn ]
then
    echo "Line counts match:  $b/testpass.base testpass"
else
    echo "Line counts NO NOT match:  $b/testpass.base testpass"
fi
ndsecs=`date '+%s'`
showminutes() {
   t=`expr  \( $2 \- $1 \+ 29  \) \/ 60`
   echo "Run time in minutes: $t"
}
showminutes $stsecs $ndsecs
# An 'error' reported in testpass does not mean anything
# is wrong.  A FAIL does.
#grep error testpass >testpasserrs
grep FAIL  testpass >>testpasserrs
ct=`wc -l <testpasserrs`
if [ $ct -ne 0 ]
then
    echo "FAIL due to FAIL lines in testpass"
    myexit=1
fi
echo "Done at " `date` 
if [ $myexit -ne 0 ]
then
   echo "FAIL the search tests, but if after examining"
   echo "testpass and testfail you determine it really"
   echo "passes then just consider it a pass"
   exit 1
else
   echo "Overall PASS. Congratulations."
fi
if [ $savetemps = "n" ]
then
   rmtemps
fi
exit 0
