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


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))
test -f $f || ./gen_cvs.exe

true <$f || exit

if ! test -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`

xcalc() { gawk "BEGIN { print $1; }"; }
xtime() { /usr/bin/time -f%e $1 2>&1 >/dev/null; }

proglist=(test_mikro77_gawk.sh test_foobar_lua.sh test_mh_python.sh)
proglist=(${proglist[@]} test_hans.exe test_heinzel.exe test_heinzel2.exe)
proglist=(${proglist[@]} test_foobar3.exe test_foobar2.exe)

if [ `uname` != Linux ]
then
    proglist=(${proglist[@]} test_win_native.exe)
fi

for prog in "${proglist[@]}"
do
    if [ `uname` == Linux ]
    then
        # does not work with mingw lua
        tm=`xtime ./$prog`
    else
        # needs bash v5
        n1=$EPOCHREALTIME; ./$prog; n2=$EPOCHREALTIME
        tm=`xcalc "$n2 - $n1"`
    fi

    bps=`xcalc "$bytes / $tm * 1e-6"`
    lps=`xcalc "$lines / $tm * 1e0"`

    diff -q nodup.txt nodup_soll.txt

    progbytes=`wc -c < $prog`

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