#!/bin/sh

# Do an automated test which involves building and regtesting version
# 1.9 of the GNU Scientific Library (gsl).  This has proven to be a 
# very thorough test of Vex's CPU simulations and has exposed bugs 
# which had not been previously discovered.  Gsl 1.9 contains more
# than 6 million tests as part of its regression suite, and so this
# script's purpose is to runs those tests using valgrind and compare 
# against the same tests run natively.  Note that it produces a
# huge amount of output (about 2 x 620 MByte), so be careful.
# The older gsl16test script produces only about 2 x 7 MByte per run.
# 
# You can download gsl and get more info about it at 
# http://www.gnu.org/software/gsl



# Args:
#     absolute name of gsl-1.9.tar.gz file
#     name of C compiler
#     args for C compiler
#     name of Valgrind
#     args for Valgrind

# Results:  3.7.0   --tool=none
#     x86   1 failure  Ubuntu 10.10
#           FAIL: qawo(f456) elist (7.25063790881233303e-15 observed vs 7.25922435194575979e-15 expected)
#           same failure was also present in 3.6.1
#     s390x 0 failures  on z196 running SLES11

if [ $# != 5 ]
then 
   echo "usage: gsl19test /absolute/name/of/gsl-1.9.tar.gz"
   echo "                 C-compiler-command"      
   echo "                 flags-for-C-compiler"     
   echo "                 Valgrind-command"
   echo "                 flags-for-Valgrind"
   exit 1
fi


runcmd () {
   echo -n "   $1  ... "
   shift

   (eval "$*") >> log.verbose 2>&1

   if [ $? = 0 ]
   then
      echo "done"
      return 0
   else
      echo "failed"
      return 1
   fi
}

GSL_FILE=$1
GSL_CC=$2
GSL_CFLAGS=$3
GSL_VV=$4
GSL_VFLAGS=$5

TESTS1="block/test bspline/test cblas/test cdf/test cheb/test"
TESTS2="combination/test complex/test const/test deriv/test dht/test"
TESTS3="diff/test eigen/test err/test fft/test fit/test histogram/test"
TESTS4="ieee-utils/test integration/test interpolation/test linalg/test"
TESTS5="matrix/test min/test monte/test multifit/test multimin/test"
TESTS6="multiroots/test ntuple/test ode-initval/test permutation/test"
TESTS7="poly/test qrng/test randist/test rng/test roots/test siman/test"
TESTS8="sort/test specfunc/test statistics/test sum/test sys/test"
TESTS9="vector/test wavelet/test"

ALL_TESTS="$TESTS1 $TESTS2 $TESTS3 $TESTS4 $TESTS5 $TESTS6 $TESTS7 $TESTS8 $TESTS9"


echo "gsl19test: src:      " $GSL_FILE
echo "gsl19test: cc:       " $GSL_CC
echo "gsl19test: cflags:   " $GSL_CFLAGS
echo "gsl19test: valgrind: " $GSL_VV
echo "gsl19test: vflags:   " $GSL_VFLAGS

rm -rf log.verbose gsl-1.9 summary.txt

echo > log.verbose

echo > summary.txt
echo $0  $1  \"$2\"  \"$3\"  \"$4\"  \"$5\" >> summary.txt
echo >> summary.txt

runcmd "Untarring                     " \
       "rm -rf gsl-1.9 && tar xzf $GSL_FILE" && \
\
runcmd "Configuring                   " \
       "(cd gsl-1.9 && CC=$GSL_CC CFLAGS=\"$GSL_CFLAGS\" ./configure)" && \
\
runcmd "Building                      " \
       "(cd gsl-1.9 && make && make -k check)"

echo -n "   Collecting reference results  "
rm -f out-REF
(cd gsl-1.9 && for f in $ALL_TESTS ; \
               do GSL_TEST_VERBOSE=1 ./$f ; done) &> out-REF
echo "  ... done"

echo -n "   Collecting valgrinded results "
rm -f out-V
(cd gsl-1.9 && for f in $ALL_TESTS ; \
               do GSL_TEST_VERBOSE=1 eval $GSL_VV -v --trace-children=yes "$GSL_VFLAGS" ./$f ; done) &> out-V
echo "  ... done"

echo -n "   Native fails:    " && (grep FAIL: out-REF | wc -l)
echo -n "   Native passes:   " && (grep PASS: out-REF | wc -l)
echo -n "   Valgrind fails:  " && (grep FAIL: out-V | wc -l)
echo -n "   Valgrind passes: " && (grep PASS: out-V | wc -l)

(echo -n "   Native fails:    " && (grep FAIL: out-REF | wc -l)) >> summary.txt
(echo -n "   Native passes:   " && (grep PASS: out-REF | wc -l)) >> summary.txt
(echo -n "   Valgrind fails:  " && (grep FAIL: out-V | wc -l)) >> summary.txt
(echo -n "   Valgrind passes: " && (grep PASS: out-V | wc -l)) >> summary.txt
echo >> summary.txt

echo