#=======================================================#
#      TUTORIAL AND GUIDE FOR WRITING CDO TESTS!        #
#=======================================================#
How to write Tests for CDO:
    Important variables needed for test coverage checks:
        - STATS: contains simple operators. Needs to be inside a test. Otherwise the test is not recognized
        - OPERTYPES: contains a prefix for operators e.g: monmean -> mon is in OPERTYPES and mean is in STATS, muss eine string variable sein
        - FORMATS: cdo format string which we can use to know what module is tested for which  formats
    General Variables:
        - RSTAT: is used to hold the the test status
    General:
        Please use tabwidth of 4
        Empty lines can be marked with a '#'
        One operator test execution should be a single line in the test results
            so that NTEST is increased for each distinct operator call.

    General Structure/Example:

    #autoconf will replace this with a fitting bash premable
    #! @BASH@
    #needed by autoconf to know how many tests are to be expected
    echo 1..8 # Number of tests to be executed.
    #
    #automatically sets the paths if we are inside the test folder
    #for make check inside the top build folder CDO and DATAPATH are set throuch ./configure
    #here we also enable us to use debug variables when executing tests in the test folder 
    #e.g: CDO_DEBUG="-d" ./TestFileName.test <NTEST>  executes a test with given testn with debug option "-d"
    test -n "$CDO"      || CDO="@abs_top_builddir@/src/cdo $CDO_DEBUG"
    test -n "$DATAPATH" || DATAPATH="@abs_top_srcdir@/test/data/"

    for FORMAT in $FORMATS; do
        for TYPE in $OPERTYPES ; do
            for OPER in $STATS; do
                #this is if enables us to rerun a single test within the test folder
                #./TestFileName.test <NTEST> runs the test
                if [ -z "$1" ] || [ $NTEST == $1 ]; then
                    #do stuff
                    #check stuff

                    # autoconf needs the ok/not ok to know what the result of the test was
                    test $RSTAT -eq 0 && echo "ok $NTEST - $CDOTEST"
                    test $RSTAT -eq 0 || echo "not ok $NTEST - $CDOTEST"
                    # there are options for SKIPING a test instead of ok/not ok example follows:
                    test $RSTAT -eq 0 && echo "ok $NTEST - $CDOTEST # SKIP POSIX threads not enabled"
                fi
            done
        done
    done
    ALTERNATIVE:

    in the file cdoTestFunctions we defined the function runTest which takes the following parameters:
    - NTEST: which number within this test of the operator
    - CDOTEST: a short description
    - CDOCOMMAND eg. "CDO $STAT $IFILE $OUTFILE"
    - TESTCOMMAND: e.g diff command like "$CDO $FORMAT diff FILE1 FILE2"
        this function handles the ok/not ok printing for given for CDOCOMMAND and TESTCOMMAND

    HINTS:
        let NTEST += 1 #Needs to be outside the if statement that enables single test execution

#=======================================================#
#      Complete example for operator monstat            #
#=======================================================#
# contains: naming conventions
#           naming conventions
#           looping oder opers
#           single test execution if statement
#           setting paths
#-------------------------------------------------------#
#! @BASH@
echo 1..4 # Number of tests to be executed.
#
test -n "$CDO"      || CDO="@abs_top_builddir@/src/cdo $CDO_DEBUG"
test -n "$DATAPATH" || DATAPATH="@abs_top_srcdir@/test/data/"
#
IFILE1=$DATAPATH/ts_1d_1year
IFILE2=$DATAPATH/ts_mm_1991
#
FORMAT="-f srv -b 32"
STATS="monadd monsub monmul mondiv"
#
NTEST=1
#
for STAT in $STATS; do
    if [ -z "$1" ] || [ $NTEST == $1 ]; then
        RSTAT=0

        RFILE=$DATAPATH/${STAT}_ref

        CDOTEST="$STAT"
        echo "Running test: $NTEST - $CDOTEST"

        OFILE=${STAT}_res

        CDOCOMMAND="$CDO  $FORMAT ${STAT} $IFILE1 $IFILE2 $OFILE"

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

        $CDO  diff $OFILE $RFILE
        test $? -eq 0 || let RSTAT+=1

        rm -f $OFILE

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

    let NTEST+=1
done
#
exit 0
#=======================================================#
#   ALTERNATIVE:  Complete example for operator monstat #
#=======================================================#
# contains: usage of OPERTYPES
#           usage of runTest()
#-------------------------------------------------------#
#! @BASH@
echo 1..4 # Number of tests to be executed.
#
test -n "$CDO"      || CDO="@abs_top_builddir@/src/cdo $CDO_DEBUG"
test -n "$DATAPATH" || DATAPATH="@abs_top_srcdir@/test/data/"
#
IFILE1=$DATAPATH/ts_1d_1year
IFILE2=$DATAPATH/ts_mm_1991
#
FORMAT="-f srv -b 32"
OPERTYPES="mon"
STATS="add sub mul div"
#
NTEST=1
#
for OPERTYPE in $OPERTYPES; do
    for STAT in $STATS; do
        if [ -z "$1" ] || [ $NTEST == $1 ]; then
            RSTAT=0
            RFILE=$DATAPATH/${STAT}_ref
            CDOTEST="$STAT"
            OFILE=${STAT}_res

            CDOCOMMAND="$CDO  $FORMAT ${OPERTYPE}${STAT} $IFILE1 $IFILE2 $OFILE"
            TESTCOMMAND="$CDO  diff $OFILE $RFILE"

            runTest "$NTEST" "$CDOTEST" "$CDOCOMMAND" "$TESTCOMMAND"
        fi

        let NTEST+=1
    done
done
#
exit 0
