#!/bin/bash -e
#
# generate list.txt and run the test programs
#

# List of test programs

proglist=(
    test_mikro77_gawk.sh
    test_foobar_lua.sh
    test_mh_python3.sh
    test_hans.exe
    test_heinzel.exe
    test_heinzel2.exe
    test_foobar3.exe
    test_foobar2.exe
)

[[ `uname` == MINGW* ]] && proglist+=(test_win_native.exe)


# number of test progam runs
runs=3

# test program input file
f=list.txt

# generate list.txt using random data
#test -f $f || ./gen_cvs2.awk

# generate list.txt using deterministic data
#test -f $f || ./gen_cvs.exe $((1024*1024))
[[ -f $f ]] || ./gen_cvs.exe

true <$f || exit

if [[ ! -f nodup_soll.txt ]]
then
    # generate nodup_soll.txt for comparison
    ./test_hans.exe
    cp nodup.txt nodup_soll.txt
fi

lines=`wc -l < $f`
bytes=`wc -c < $f`

# floating point calculator
xcalc() { gawk "BEGIN { print $1; }"; }

# return minimum of first and second floating point number
xmin() { gawk "BEGIN { if ($1 < $2) print $1; else print $2; }"; }

# run program $runs times and return minimum runtime as floating point number
xtime() {
    tmin=1e99
    for i in `seq $runs`
    do
        t=`/usr/bin/time -f%e $1 2>&1`
        tmin=`xmin $tmin $t`
    done
    echo $tmin
}

# for each program...
for prog in "${proglist[@]}"
do
    sec=`xtime ./$prog`

    # check result
    diff -q nodup.txt nodup_soll.txt

    bps=`xcalc "$bytes / $sec * 1e-6"`
    lps=`xcalc "$lines / $sec * 1e0"`
    progbytes=`wc -c < $prog`

    printf '%-25s Progsize=%-7d Seconds=%-6.2f MB/sec=%-6.2f Lines/sec=%-6.0f\n' \
        $prog $progbytes $sec $bps $lps
done
