#! @SHELL@
echo 1..4 # Number of tests to be executed.
#
test -n "$CDO"      || CDO=cdo
test -n "$DATAPATH" || DATAPATH=./data
#
CDOOUT=cout
CDOERR=cerr
FORMAT="-f srv -b 32"
STATS="add sub mul div"
#
IFILE=$DATAPATH/pl_data
NTEST=1
#
for STAT in $STATS; do
  RSTAT=0

  OFILE=stat${STAT}_res

  for VAR in -777 -1 0 1 777; do
    VAL=1
    CFILE=constval
    $CDO -f srv -b 64 const,$VAL,$IFILE $CFILE

# stat var const

    CDOTEST="$STAT"
    CDOCOMMAND="$CDO $FORMAT ${STAT} $IFILE $CFILE $OFILE"

    echo "Running test: $NTEST - $CDOTEST"
    echo "$CDOCOMMAND"

    $CDOCOMMAND
    test $? -eq 0 || let RSTAT+=1

# stat,const var

    OFILE2=stat${STAT}c_res
    CDOCOMMAND="$CDO $FORMAT ${STAT}c,$VAL $IFILE $OFILE2"

    $CDOCOMMAND
    test $? -eq 0 || let RSTAT+=1

    $CDO diff $OFILE $OFILE2 > $CDOOUT 2> $CDOERR
    test $? -eq 0 || let RSTAT+=1
    test -s $CDOOUT && let RSTAT+=1
    cat $CDOOUT $CDOERR

# expr,instr var

    OP=""
    if [ "$STAT" = add ] ; then OP='+' ; fi
    if [ "$STAT" = sub ] ; then OP='-' ; fi
    if [ "$STAT" = mul ] ; then OP='*' ; fi
    if [ "$STAT" = div ] ; then OP='/' ; fi
    OFILE3=expr${STAT}_res
    INSTR="var130=var130${OP}${VAL};var152=var152${OP}${VAL};var129=var129${OP}${VAL};"
    echo $INSTR
    CDOCOMMAND="$CDO $FORMAT expr,$INSTR $IFILE $OFILE3"

    $CDOCOMMAND
    test $? -eq 0 || let RSTAT+=1

    $CDO diff $OFILE $OFILE3 > $CDOOUT 2> $CDOERR
    test $? -eq 0 || let RSTAT+=1
    test -s $CDOOUT && let RSTAT+=1
    cat $CDOOUT $CDOERR

    rm -f $OFILE $OFILE2 $OFILE3 $CFILE
  done

  test $RSTAT -eq 0 && echo "ok $NTEST - $CDOTEST"
  test $RSTAT -eq 0 || echo "not ok $NTEST - $CDOTEST"

  let NTEST+=1
done
#
rm -f $CDOOUT $CDOERR
#
exit 0
