#!/bin/sh
#
# Testing RNAfold (MFE and general features)

echo 1..14 # Number of tests to be executed.

RETURN=0

failed () {
    RETURN=1
    if [ "x$1" != "x" ]
    then
        echo "not ok - $1"
    else
        echo "not ok"
    fi
}

passed () {
    if [ "x$1" != "x" ]
    then
        echo "ok - $1"
    else
        echo "ok"
    fi
}


# simple version test
testname="correct version number"
if [ "x$(RNAfold --version)" != "xRNAfold $CURRENT_VERSION" ] ; then failed "$testname"; else passed "$testname"; fi

# Test dangle models (d0, d1, d2, d3)
for dangles in 0 1 2 3
do
  testname="MFE prediction (RNAfold -d${dangles})"
  RNAfold --noPS -d ${dangles} < ${DATADIR}/rnafold.small.seq > rnafold.fold
  diff=$(${DIFF} -ru ${RNAFOLD_RESULTSDIR}/rnafold.small.d${dangles}.mfe.gold rnafold.fold)
  if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi
done

# Test --noLP
testname="MFE prediction (RNAfold --noLP)"
RNAfold --noPS --noLP < ${DATADIR}/rnafold.small.seq > rnafold.fold
diff=$(${DIFF} -ru ${RNAFOLD_RESULTSDIR}/rnafold.small.noLP.mfe.gold rnafold.fold)
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

# Test temperatures (25degC, 40degC)
for T in 25 40
do
  testname="MFE prediction (RNAfold -T ${T})"
  RNAfold --noPS -T ${T} < ${DATADIR}/rnafold.small.seq > rnafold.fold
  diff=$(${DIFF} -ru ${RNAFOLD_RESULTSDIR}/rnafold.small.T${T}.mfe.gold rnafold.fold)
  if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi
done

# Test maximum base pair span
testname="MFE prediction (RNAfold --maxBPspan=30)"
RNAfold --noPS --maxBPspan=30 -p0 < ${DATADIR}/rnafold.small.seq > rnafold.fold
diff=$(${DIFF} -ru -I frequency ${RNAFOLD_RESULTSDIR}/rnafold.small.span30.gold rnafold.fold)
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

# Test automatic ID generation
testname="MFE prediction and automatic sequence ID feature"
RNAfold --noPS --auto-id --id-prefix="blabla" --id-start=40 < ${DATADIR}/rnafold.small.seq > rnafold.fold
diff=$(${DIFF} -ru ${RNAFOLD_RESULTSDIR}/rnafold.small.id.mfe.gold rnafold.fold)
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

# Test parameter file loading
for par in dna_mathews1999.par dna_mathews2004.par rna_andronescu2007.par rna_turner1999.par
do
  testname="MFE prediction (-P ${par})"
  RNAfold --noPS -P ${MISC_DIR}/${par} < ${DATADIR}/rnafold.small.seq > rnafold.fold
  diff=$(${DIFF} -ru ${RNAFOLD_RESULTSDIR}/rnafold.small.${par}.mfe.gold rnafold.fold)
  if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi
done

# clean up
rm rnafold.fold

exit ${RETURN}
