#!/bin/sh 
#
# Testsuite for the modules

usage() {
    echo "megawave_checkmodules (no option)"
    echo "input:  none"
    echo "output: unittests results"
}

if [ $# -ne 0 ]; then
    usage
    exit 1
fi

# used for integers
exact() {
    if [ "$1" = "$2" ]; then
	return 0
    else
	return 1
    fi
}

# used for float numbers (threshold at 1% error)
approx() {
    X=`echo "a=$1; b=$2; c=(a-b)^2; d=0.0001*(a^2+b^2); if (c<=d) 1" \
	| bc -l 2> /dev/null`
    if [ "$X" = "1" ]; then
	return 0
    else
	return 1
    fi
}

# used for random variables (threshold at 10% error)
rand_approx() {
    X=`echo "a=$1; b=$2; c=(a-b)^2; d=0.01*(a^2+b^2); if (c<=d) 1" \
	| bc -l 2> /dev/null`
    if [ "$X" = "1" ]; then
	return 0
    else
	return 1
    fi
}

fail() {
    FAIL=$(($FAIL + 1))
    echo -n "X"
}
    
pass() {
    OK=$(($OK + 1))
    echo -n "."
}

results() {
    echo "test completed: $OK success, $FAIL failures"
    rm -rf $TMP
    exit $FAIL
}

OK=0
FAIL=0
TMP=/tmp/megawave_checkmodules.$$.tmp
mkdir $TMP

SHARE=/usr/share/megawave
DATA=$SHARE/data
SAMPLES=$SHARE/samples
SCRIPTS=$SHARE/scripts

# check modules
echo "checking megawave modules"

# common modules
echo -n 'common modules: '

megawave fmean $SAMPLES/images/cimage > $TMP/1 \
    && VAL=`cut -d"=" -f2 $TMP/1` \
    && approx $VAL 127.156 \
    && pass || fail

megawave fsize $SAMPLES/images/cimage > $TMP/1 \
    && VAL=`cat $TMP/1` \
    && exact "$VAL" "256 256" \
    && pass || fail

megawave fnorm -v $SAMPLES/images/cimage > $TMP/1 \
    && VAL=`cut -d"=" -f2 $TMP/1` \
    && approx $VAL 8.73267 \
    && pass || fail

megawave funzoom -z 4 -o 0 $SAMPLES/images/cimage $TMP/1 > /dev/null \
    && VAL=`megawave fnorm -v $TMP/1  | cut -d"=" -f2` \
    && approx $VAL 16.5741 \
    && pass || fail

megawave cfunzoom -z 4 -o 0 $SAMPLES/images/ccimage $TMP/1 \
    && megawave cfgetchannels $TMP/1 $TMP/2 $TMP/3 $TMP/4 \
    && megawave fdiff $TMP/2 $TMP/3 $TMP/4 \
    && VAL=`megawave fnorm -v $TMP/4 | cut -d"=" -f2` \
    && approx $VAL 12.6513 \
    && pass || fail

megawave cfgetchannels $SAMPLES/images/ccimage \
    $TMP/1 $TMP/2 $TMP/3 > /dev/null \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 14.6397 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 17.8047 \
    && VAL=`megawave fnorm -v $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 18.248 \
    && pass || fail

megawave fdiff $SAMPLES/images/cimage $SAMPLES/images/fimage $TMP/1 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 15.9042 \
    && pass || fail

megawave cfdiff $SAMPLES/images/ccimage $SAMPLES/images/cimage $TMP/1 \
    && megawave cfgetchannels $TMP/1 $TMP/2 $TMP/3 $TMP/4 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 18.6356 \
    && VAL=`megawave fnorm -v $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 21.3019 \
    && VAL=`megawave fnorm -v $TMP/4 | cut -d"=" -f2` \
    && approx $VAL 21.7275 \
    && pass || fail

megawave fadd $SAMPLES/images/cimage $SAMPLES/images/cimage $TMP/1 \
    && megawave fdiff $TMP/1 $SAMPLES/images/cimage $TMP/2 \
    && VAL=`megawave fnorm -p 2 -c $SAMPLES/images/cimage $TMP/2 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

megawave fquant $SAMPLES/images/cimage $TMP/1 5 > /dev/null \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 8.42627 \
    && pass || fail

echo "1 5 2 3" | megawave freadasc $TMP/1 2 2 \
    && VAL=`megawave fmean $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 2.75 \
    && VAL=`megawave fvar $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 2.91667 \
    && pass || fail

megawave snorm -v $SAMPLES/signals/fsignal > $TMP/1 \
    && VAL=`cut -d"=" -f2 $TMP/1` \
    && approx $VAL 37.827 \
    && pass || fail

megawave ccopy $SAMPLES/images/cimage $TMP/1_1 \
    && megawave ccopy $SAMPLES/images/fimage $TMP/1_2 \
    && $SCRIPTS/megawave_mkmovie Cmovie $TMP/1 1 2 \
    && VAL=`grep nimage $TMP/1 | cut -d":" -f2` \
    && exact $VAL 2 \
    && pass || fail

megawave fconst $TMP/1 10 100 100 \
    && VAL=`megawave fnorm -p 1 $TMP/1 | cut -d"=" -f2` \
    && exact $VAL 10 \
    && pass || fail

VAL=`megawave fmean $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 10 \
    && pass || fail

megawave dkinfo $SAMPLES/curves/curve > $TMP/1 \
    && VAL=`grep "Average step distance" $TMP/1 | cut -d":" -f2` \
    && approx $VAL 2.04765 \
    && pass || fail

megawave faxpb -a 2 -b 10 $SAMPLES/images/cimage $TMP/1 \
    && megawave faxpb -a 0.5 -b -5 $TMP/1 $TMP/2 \
    && megawave fdiff $TMP/2 $SAMPLES/images/cimage $TMP/3 \
    && VAL=`megawave fnorm -p 2 $TMP/3 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

megawave fconst $TMP/1 0 10 10 \
    && megawave fpset $TMP/1 5 5 100 $TMP/1 || fail \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 2 \
    && pass || fail

echo

# compression/ezwave
echo -n "compression/ezwave: "

megawave cfunzoom -z 4 -o 0 $SAMPLES/images/ccimage $TMP/1

megawave cfezw -R 0.5 -o $TMP/3 \
    $TMP/1 $DATA/wave/biortho/h/sd07.ir $TMP/2 > /dev/null \
    && VAL=`megawave fnorm -v $TMP/2 2> /dev/null | cut -d"=" -f2` \
    && approx $VAL 11.0831 \
    && pass || fail

megawave cfiezw $TMP/3 $DATA/wave/biortho/h/sd07.ir $TMP/4 > /dev/null \
    && VAL=`megawave fnorm -p 2 -c $TMP/4 $TMP/2 2> /dev/null | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

$SCRIPTS/megawave_cfezw $TMP/1 > $TMP/2 \
    && VAL=`tail -1 $TMP/2 | cut -f2` \
    && approx $VAL 31.18 \
    && pass || fail

megawave funzoom -z 4 -o 0 $SAMPLES/images/cimage $TMP/1

megawave fezw -R 0.5 -o $TMP/3 \
    $TMP/1 $DATA/wave/biortho/h/sd07.ir $TMP/2 > /dev/null \
    && VAL=`megawave fnorm -v $TMP/2 2> /dev/null | cut -d"=" -f2` \
    && approx $VAL 18.1235 \
    && pass || fail

megawave fiezw $TMP/3 $DATA/wave/biortho/h/sd07.ir $TMP/4 > /dev/null \
    && VAL=`megawave fnorm -p 2 -c $TMP/4 $TMP/2 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

$SCRIPTS/megawave_fezw $TMP/1 > $TMP/2 \
    && VAL=`tail -1 $TMP/2 | cut -f2` \
    && approx $VAL 33.62 \
    && pass || fail

echo

# compression/lossless
echo -n "compression/lossless: "

megawave funzoom -ftype IMG -z 4 -o 0 $SAMPLES/images/cimage $TMP/1 2> /dev/null \
    && megawave arencode2 $TMP/1 $TMP/2 > $TMP/3 \
    && VAL=`grep Rate $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 7.57324 \
    && VAL=`megawave fsize $TMP/2` \
    && exact "$VAL" "3878 1" \
    && pass || fail

megawave arencode2 -H $TMP/1 $TMP/2 > /dev/null \
    && megawave ardecode2 -r 256 $TMP/2 $TMP/3 > /dev/null \
    && pass || fail

megawave cvsencode $SAMPLES/curves/france.crv > $TMP/1 \
    && VAL=`grep N $TMP/1 | cut -d"=" -f2` \
    && exact $VAL 7887 \
    && VAL=`grep B $TMP/1 | cut -d"=" -f2` \
    && exact $VAL 10098 \
    && pass || fail

megawave cvsfrecode $SAMPLES/curves/france.crv > $TMP/1 \
    && VAL=`grep N $TMP/1 | cut -d"=" -f2` \
    && exact $VAL 7887 \
    && VAL=`grep B $TMP/1 | cut -d"=" -f2` \
    && exact $VAL 10098 \
    && pass || fail

megawave cvsorgcode $SAMPLES/curves/france.crv > $TMP/1 \
    && VAL=`grep N $TMP/1 | cut -d"=" -f2` \
    && exact $VAL 7887 \
    && pass || fail

megawave funzoom -ftype IMG -z 4 -o 0 $SAMPLES/images/cimage $TMP/1 2> /dev/null \
    && megawave fencode $TMP/1 > $TMP/2 \
    && VAL=`grep brate $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 7.34399 \
    && pass || fail

echo

# compression/scalar
echo -n "compression/scalar: "

megawave fscalq -p -n 10 $SAMPLES/images/cimage $TMP/1 > $TMP/2 \
    && VAL=`cat $TMP/2` \
    && VAL=`megawave fentropy $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 2.91987 \
    && pass || fail

megawave fscalq -p -n 10 -o $TMP/2 $SAMPLES/images/cimage $TMP/1 > /dev/null \
    && megawave fiscalq $TMP/2 $TMP/3 > /dev/null \
    && megawave fdiff $TMP/1 $TMP/3 $TMP/2 \
    && VAL=`megawave fnorm -p 2 $TMP/2 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

echo

# compression/vector
echo -n "compression/vector: "

megawave mk_codebook $TMP/1 \
    && VAL=`megawave fsize $TMP/1` \
    && exact "$VAL" "4 6" \
    && pass || fail

# TODO
# fivq
# flbg_adap
# flbg
# flbg_train
# fvq
# mk_trainset

echo

# compression/vqwave

echo -n "compression/vqwave: "

# TODO
# Cfwivq
# Cfwvq
# Fwivq
# fwivq
# Fwlbg_adap
# fwlbg_adap
# Fwvq
# fwvq
# wlbg_adap

echo

# $SAMPLES/curves/curve

echo -n "curve: "

megawave area $SAMPLES/curves/curve > $TMP/1 \
    && VAL=`cut -d"=" -f2 $TMP/1` \
    && exact $VAL 209718 \
    && pass || fail

megawave perimeter $SAMPLES/curves/curve > $TMP/1 \
    && VAL=`cut -d"=" -f2 $TMP/1` \
    && exact $VAL 8422 \
    && pass || fail

megawave circle -r 10 -n 100 $TMP/1 \
    && VAL=`megawave area $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 313.953 \
    && VAL=`megawave perimeter $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 62.8215 \
    && pass || fail

megawave disc $TMP/1 2.5 \
    && VAL=`megawave perimeter $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 30.5708 \
    && pass || fail

megawave dsplit_convex -c . $SAMPLES/curves/curve $TMP/1 > $TMP/2 \
    && VAL=`cut -d"=" -f2 $TMP/2` \
    && exact $VAL 1866 \
    && pass || fail

megawave fsplit_convex -c . $SAMPLES/curves/curve $TMP/1 > $TMP/2 \
    && VAL=`cut -d"=" -f2 $TMP/2` \
    && exact $VAL 1866 \
    && pass || fail

megawave extract_connex $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave dkinfo $TMP/1 | grep "Number of curves" | cut -d":" -f2` \
    && exact $VAL 122 \
    && pass || fail

megawave fillpoly -x 1100 -y 1100 $SAMPLES/curves/france.crv $TMP/1 \
    && VAL=`megawave fmean $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 136.473 \
    && pass || fail

echo "10 10 60 20 30 50 e 100 10 110 50 150 20 q" \
    | megawave flreadasc 2 $TMP/1 > /dev/null \
    && megawave fillpolys -x 160 -y 60 $TMP/1 $TMP/1 \
    && VAL=`megawave fmean $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 201.423 \
    && pass || fail

megawave fkbox $SAMPLES/curves/curve > $TMP/1 \
    && VAL=`grep xmin $TMP/1 | cut -d"=" -f2` \
    && exact $VAL 63 \
    && VAL=`grep ymin $TMP/1 | cut -d"=" -f2` \
    && exact $VAL -912 \
    && VAL=`grep xmax $TMP/1 | cut -d"=" -f2` \
    && exact $VAL 692 \
    && VAL=`grep ymax $TMP/1 | cut -d"=" -f2` \
    && exact $VAL -123 \
    && pass || fail

megawave fkcenter $SAMPLES/curves/curve > $TMP/1 \
    && VAL=`grep xg $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 336.298 \
    && VAL=`grep yg $TMP/1 | cut -d"=" -f2` \
    && approx $VAL -467.331 \
    && pass || fail

megawave fkcrop 0 -100 700 -200 $SAMPLES/curves/curve $TMP/1 \
    && VAL=`megawave dkinfo $TMP/1 | grep "Number of curves" | cut -d":" -f2` \
    && exact $VAL 5 \
    && VAL=`megawave fkcenter $TMP/1 | grep xg | cut -d"=" -f2` \
    && approx $VAL 324.922 \
    && pass || fail

megawave fkzrt $SAMPLES/curves/curve  $TMP/1 0.5 30 20 20 \
    && VAL=`megawave fkcenter $TMP/1 | grep xg | cut -d"=" -f2` \
    && approx $VAL 282.455 \
    && pass || fail

echo "40 40 60 30 100 100 200 80 80 240 e 240 20 280 20 260 240 200
150 240 40 q" \
    | megawave fkreadasc $TMP/1 > /dev/null \
    && megawave ksplines -j 3 $TMP/1 $TMP/2 \
    && VAL=`megawave dkinfo $TMP/2 | grep "Number of curves" | cut -d":" -f2` \
    && exact $VAL 2 \
    && pass || fail

echo "40 40 60 30 100 100 200 80 80 240 q" \
    | megawave fkreadasc $TMP/1 > /dev/null \
    && megawave kspline -j 3 $TMP/1 $TMP/2 \
    && VAL=`megawave perimeter $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 318.806 \
    && pass || fail

echo "0.1 0.2" | megawave sreadasc $TMP/1 2 \
    && megawave flscale -ftype MW2_FCURVE $SAMPLES/curves/curve $TMP/1 $TMP/2 \
    && megawave perimeter $TMP/2 > $TMP/3 \
    && VAL=`cut -d"=" -f2 $TMP/3` \
    && exact $VAL 1313 \
    && megawave area $TMP/2 > $TMP/3 \
    && VAL=`cut -d"=" -f2 $TMP/3` \
    && exact $VAL 4194.36 \
    && pass || fail

megawave flconcat -ftype MW2_FCURVES $TMP/2 $SAMPLES/curves/curve $TMP/3 \
    && megawave fkbox $TMP/3 | grep ymax > $TMP/4 \
    && VAL=`cut -d"=" -f2 $TMP/4` \
    && exact $VAL -24.6 \
    && pass || fail

echo

# $SAMPLES/curves/curve/io
echo -n "curve/io: "

echo "10 10 60 20 30 50 e 40 30 41 31 41 20 q" \
    | megawave fkreadasc $TMP/1 > /dev/null \
    && VAL=`megawave dkinfo $TMP/1 | grep "Number of curves" | cut -d":" -f2` \
    && exact $VAL 2 \
    && VAL=`megawave perimeter $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 93.4166 \
    && pass || fail

echo "10 10 60 20 30 50 e 40 30 41 31 41 20 q" \
    | megawave flreadasc 2 $TMP/1 > /dev/null \
    && VAL=`megawave dkinfo $TMP/1 | grep "Number of curves" | cut -d":" -f2` \
    && exact $VAL 2 \
    && VAL=`megawave perimeter $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 93.4166 \
    && pass || fail

megawave kplot $SAMPLES/curves/curve $TMP/1 \
    && VAL=`megawave fsize $TMP/1` \
    && exact "$VAL" "630 790" \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 1.90955 \
    && pass || fail

megawave fkplot -s $SAMPLES/curves/curve $TMP/1 \
    && VAL=`megawave fsize $TMP/1` \
    && exact "$VAL" "630 790" \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 1.90955 \
    && pass || fail

megawave fkprintasc $SAMPLES/curves/curve > /dev/null \
    && VAL=`megawave fkprintasc $SAMPLES/curves/curve | wc -l` \
    && exact $VAL 4115 \
    && pass || fail

megawave flprintasc $SAMPLES/curves/curve > /dev/null \
    && VAL=`megawave flprintasc $SAMPLES/curves/curve | wc -l` \
    && exact $VAL 4115 \
    && pass || fail

megawave fkprintfig $SAMPLES/curves/curve > /dev/null \
    && VAL=`megawave fkprintfig $SAMPLES/curves/curve | wc -w` \
    && exact $VAL 8255 \
    && pass || fail

echo "10 10 60 20 30 50 e 40 30 41 31 41 20 q" \
    | megawave flreadasc 2 $TMP/1 > /dev/null \
    && megawave fkprintfig $TMP/1 | megawave kreadfig $TMP/2 > /dev/null \
    && megawave fkzrt $TMP/2 $TMP/2 0.001 0 0 0 \
    && VAL=`megawave dkinfo $TMP/2 | grep "Number of curves" | cut -d":" -f2` \
    && exact $VAL 2 \
    && VAL=`megawave area $TMP/2 | cut -d"=" -f2` \
    && exact $VAL 3600 \
    && pass || fail

megawave fkview -o $TMP/1 -n $SAMPLES/curves/curve \
    && VAL=`megawave fnorm -v $TMP/1 2> /dev/null | cut -d"=" -f2` \
    && approx $VAL 2.61157 \
    && pass || fail

echo "1 1 6 2 3 5 4 3 4 4 4 2 q" | megawave fkreadasc $TMP/1 > /dev/null \
    && megawave kplotasc $TMP/1 > /dev/null \
    && VAL=`megawave kplotasc $TMP/1 | wc -c` \
    && exact $VAL 84 \
    && pass || fail

# TODO
# readpoly
# dkinfo

echo

# curve/smooth
echo -n "curve/smooth: "

megawave fksmooth -n 20 -s 10 $SAMPLES/curves/curve $TMP/1 \
    && VAL=`megawave area $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 204306 \
    && VAL=`megawave perimeter $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 3128.84 \
    && pass || fail

megawave iter_fksmooth -N 2 -n 10 -s 10 $SAMPLES/curves/curve $TMP/1 \
    && VAL=`megawave dkinfo $TMP/1 | grep "Number of curves" | cut -d":" -f2` \
    && exact $VAL 3 \
    && pass || fail

megawave gass -l 10 $SAMPLES/curves/curve $TMP/1 \
    && VAL=`megawave area $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 207081 \
    && VAL=`megawave perimeter $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 3914.79 \
    && pass || fail

megawave iter_gass -N 2 -S 10 $SAMPLES/curves/curve $TMP/1 \
    && VAL=`megawave dkinfo $TMP/1 | grep "Number of curves" | cut -d":" -f2` \
    && exact $VAL 3 \
    && pass || fail

megawave gcsf -l 10 $SAMPLES/curves/curve $TMP/1 \
    && VAL=`megawave area $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 209003 \
    && VAL=`megawave perimeter $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 3929.08 \
    && pass || fail

megawave iter_gcsf -N 2 -l 20 $SAMPLES/curves/curve $TMP/1 \
    && VAL=`megawave dkinfo $TMP/1 | grep "Number of curves" | cut -d":" -f2` \
    && exact $VAL 3 \
    && pass || fail

echo

# $SAMPLES/curves/curve/matching
echo -n "curve/matching: "

megawave gass -l 3 -e 2 $SAMPLES/curves/curve $TMP/1 \
    && megawave km_inflexionpoints $TMP/1 $TMP/2 \
    && VAL=`megawave flprintasc $TMP/2 | wc -l` \
    && exact $VAL 43 \
    && pass || fail

megawave km_bitangents $TMP/1 $TMP/2 $TMP/3 \
    && VAL=`megawave flprintasc $TMP/3 | wc -l` \
    && exact $VAL 20 \
    && pass || fail

megawave km_flatpoints $TMP/1 $TMP/2 $TMP/4 0.1 0.1 \
    && VAL=`megawave flprintasc $TMP/4 | wc -l` \
    && exact $VAL 25 \
    && pass || fail

# TODO
# km_codecurve_ai
# km_codecurve_si
# km_createdict_ai
# km_createdict_si
# KM_DEMO
# km_match_ai
# km_match_si
# km_prematchings
# km_savematchings

echo

# examples
echo -n "examples: "

megawave demohead1 $SAMPLES/images/cimage $TMP/1 > /dev/null \
    && VAL=`megawave fsize $TMP/1` \
    && exact "$VAL" "1 1" \
    && pass || fail

megawave demohead2 > /dev/null \
    && pass || fail

megawave demohead3 $SAMPLES/images/cimage $TMP/1 > /dev/null \
    && VAL=`megawave fnorm -p 2 -c $SAMPLES/images/cimage $TMP/1 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

megawave fadd $SAMPLES/images/cimage $SAMPLES/images/fimage $TMP/1 \
    && megawave fadd1 $SAMPLES/images/cimage $SAMPLES/images/fimage $TMP/2 \
    && VAL=`megawave fnorm -p 2 -c $TMP/1 $TMP/2 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

megawave fadd2 $SAMPLES/images/cimage $SAMPLES/images/fimage $TMP/2 \
    && VAL=`megawave fnorm -p 2 -c $TMP/1 $TMP/2 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

megawave fadd3 $SAMPLES/images/cimage $SAMPLES/images/fimage $TMP/2 \
    && VAL=`megawave fnorm -p 2 -c $TMP/1 $TMP/2 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

megawave fadd4 $SAMPLES/images/cimage $SAMPLES/images/fimage $TMP/2 \
    && VAL=`megawave fnorm -p 2 -c $TMP/1 $TMP/2 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

megawave make_cmovie $TMP/1 \
    && VAL=`megawave fsize $TMP/1_01` \
    && exact "$VAL" "256 256" \
    && VAL=`grep nimage $TMP/1 | cut -d":" -f2` \
    && exact $VAL 20 \
    && pass || fail

megawave make_fmovie $TMP/1 \
    && VAL=`megawave fsize $TMP/1_01` \
    && exact "$VAL" "256 256" \
    && VAL=`grep nimage $TMP/1 | cut -d":" -f2` \
    && exact $VAL 21 \
    && pass || fail

megawave make_ccmovie $TMP/1 \
    && megawave ccopy $TMP/1_01 $TMP/2 2> /dev/null \
    && VAL=`megawave fsize $TMP/2` \
    && exact "$VAL" "256 256" \
    && VAL=`grep nimage $TMP/1 | cut -d":" -f2` \
    && exact $VAL 20 \
    && pass || fail

megawave make_cfmovie $TMP/1 \
    && megawave ccopy $TMP/1_01 $TMP/2 2> /dev/null \
    && VAL=`megawave fsize $TMP/2` \
    && exact "$VAL" "256 256" \
    && VAL=`grep nimage $TMP/1 | cut -d":" -f2` \
    && exact $VAL 20 \
    && pass || fail

megawave make_cimage $TMP/1 \
    && VAL=`megawave fsize $TMP/1` \
    && exact "$VAL" "256 256" \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 0.992188 \
    && pass || fail

# TODO
# view_demo

echo

# image/detection
echo -n "image/detection: "

megawave funzoom -o 0 -z 4 $SAMPLES/images/cimage $TMP/1

megawave canny $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 13.5183 \
    && pass || fail

megawave falign -e 4.3 $TMP/1 $TMP/2 > /dev/null \
    && VAL=`megawave flprintasc $TMP/2 | wc -l` \
    && exact $VAL 14 \
    && pass || fail

megawave falign_mdl -e -2 -l 1 -n 30 $SAMPLES/images/cimage $TMP/2 > /dev/null \
    && VAL=`megawave fsize $TMP/2` \
    && exact "$VAL" "6 43" \
    && pass || fail

megawave vpoint $SAMPLES/images/cimage $TMP/2 $TMP/3 > /dev/null \
    && VAL=`megawave flprintasc $TMP/3 | cut -d" " -f2` \
    && approx $VAL 17.9527 \
    && pass || fail

megawave ll_boundaries -e 11 $TMP/1 $TMP/2 > /dev/null \
    && VAL=`megawave flprintasc $TMP/2 | wc -l` \
    && exact $VAL 1139 \
    && pass || fail

megawave ll_boundaries2 -e 11 $TMP/1 $TMP/2 > /dev/null \
    && VAL=`megawave flprintasc $TMP/2 | wc -l` \
    && exact $VAL 1073 \
    && pass || fail

megawave ll_edges -e 17 $TMP/1 $TMP/2 > /dev/null \
    && VAL=`megawave flprintasc $TMP/2 | wc -l` \
    && exact $VAL 526 \
    && pass || fail

megawave harris $TMP/1 $TMP/2 \
    && VAL=`megawave flprintasc $TMP/2 | wc -l` \
    && approx $VAL 55 \
    && pass || fail

# TODO
# vpsegplot
# VP_DEMO

echo

# image/domain
echo -n "image/domain: "

megawave fcrop -x 20 -y 20 -o 3 $SAMPLES/images/cimage $TMP/1 40 100 50 110 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 3.21633 \
    && pass || fail

megawave cccrop -x 20 -y 20 -o 3 $SAMPLES/images/ccimage $TMP/1 40 100 50 110 2> /dev/null \
    && megawave cfgetchannels $TMP/1 $TMP/2 $TMP/3 $TMP/4 \
    && megawave fdiff $TMP/3 $TMP/2 $TMP/4 \
    && VAL=`megawave fnorm -v $TMP/4 | cut -d"=" -f2` \
    && approx $VAL 6.01568 \
    && pass || fail

megawave cextract $SAMPLES/images/cimage $TMP/1 40 100 50 110 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 5.06073 \
    && pass || fail

megawave fextract $SAMPLES/images/cimage $TMP/1 40 100 50 110 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 5.06073 \
    && pass || fail

megawave ccextract $SAMPLES/images/ccimage $TMP/1 40 100 50 110 \
    && megawave cfgetchannels $TMP/1 $TMP/2 $TMP/3 $TMP/4 \
    && megawave fdiff $TMP/3 $TMP/2 $TMP/4 \
    && VAL=`megawave fnorm -v $TMP/4 | cut -d"=" -f2` \
    && approx $VAL 6.9226 \
    && pass || fail

megawave clocal_zoom -x 100 -y 150 -W 64 -X 3 $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 8.07492 \
    && pass || fail

megawave flocal_zoom -x 100 -y 150 -W 64 -X 3 $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 8.07492 \
    && pass || fail

megawave cclocal_zoom -x 100 -y 150 -W 64 -X 3 $SAMPLES/images/ccimage $TMP/1 \
    && megawave cfgetchannels $TMP/1 $TMP/2 $TMP/3 $TMP/4 \
    && megawave fdiff $TMP/3 $TMP/2 $TMP/4 \
    && VAL=`megawave fnorm -v $TMP/4 | cut -d"=" -f2` \
    && approx $VAL 8.50785 \
    && pass || fail

megawave funzoom -o 0 -z 4 $SAMPLES/images/cimage $TMP/1 \
    && megawave fshift -h $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -p 2 -c $TMP/1 $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 110.12 \
    && pass || fail

megawave czoom -X 4 -o 5 $TMP/1 $TMP/2 2> /dev/null \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 5.97412 \
    && pass || fail

megawave cczoom -X 4 -o 5 $TMP/1 $TMP/2 2> /dev/null \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 5.97412 \
    && pass || fail

megawave fzoom -X 4 -o 5 $TMP/1 $TMP/2 2> /dev/null \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 5.969 \
    && pass || fail

megawave csample $SAMPLES/images/cimage $TMP/1 4 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 19.2588 \
    && pass || fail

megawave fsample $SAMPLES/images/cimage $TMP/1 4 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 19.2588 \
    && pass || fail

megawave cextcenter -f 27 $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave fsize $TMP/1` \
    && exact "$VAL" "243 243" \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 8.76071 \
    && pass || fail

megawave cfextcenter -ftype IMG -f 27 $SAMPLES/images/ccimage $TMP/1 2> /dev/null \
    && VAL=`megawave fsize $TMP/1` \
    && exact "$VAL" "243 243" \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 15.6567 \
    && pass || fail

megawave fmaskrot -s 30 -b 10 $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 6.09773 \
    && pass || fail

megawave fproj -x 100 -y 120 -o 3 $SAMPLES/images/cimage $TMP/1 10 20 250 40 80 210 130 200 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 9.06184 \
    && pass || fail

megawave fzrt -o 3 $SAMPLES/images/cimage $TMP/1 1.1 57 -10 -20 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 7.30864 \
    && pass || fail

megawave frot -a 35 $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 5.02289 \
    && pass || fail

megawave fdirspline $SAMPLES/images/cimage 5 $TMP/1 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 6.31651 \
    && pass || fail

megawave finvspline $SAMPLES/images/cimage 5 $TMP/1 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 41.7204 \
    && pass || fail

megawave ccopy $SAMPLES/images/cimage $TMP/1_001 \
    && megawave ccopy $SAMPLES/images/fimage $TMP/1_002 \
    && $SCRIPTS/megawave_mkmovie Cmovie $TMP/1 1 2 \
    && pass || fail

megawave cmzoom -o 3 -X 2 $TMP/1 $TMP/2 2> /dev/null \
    && megawave fdiff $TMP/2_01 $TMP/2_02 $TMP/3 \
    && VAL=`megawave fnorm -v $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 9.81426 \
    && pass || fail

megawave ccmzoom -o 3 -X 2 $TMP/1 $TMP/2 2> /dev/null \
    && megawave fdiff $TMP/2_01 $TMP/2_02 $TMP/3 \
    && VAL=`megawave fnorm -v $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 9.81426 \
    && pass || fail

megawave cmextract -b 0 $TMP/2 $TMP/3 30 30 1 170 170 1 $TMP/1 50 50 2 \
    && megawave fdiff $TMP/3_01 $TMP/3_02 $TMP/4 \
    && VAL=`megawave fnorm -v $TMP/4 | cut -d"=" -f2` \
    && approx $VAL 8.09011 \
    && pass || fail

megawave cmparitysep -l $TMP/1 $TMP/2 \
    && megawave fdiff $TMP/2_01 $TMP/2_03 $TMP/3 \
    && megawave fdiff $TMP/2_02 $TMP/2_04 $TMP/4 \
    && VAL=`megawave fnorm -v $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 14.6972 \
    && VAL=`megawave fnorm -v $TMP/4 | cut -d"=" -f2` \
    && approx $VAL 14.74 \
    && pass || fail

# TODO
# cmcollect
# ccmcollect

# common: funzoom cfunzoom

echo

# image/filter
echo -n "image/filter: "

megawave cfunzoom -z 4 -o 0 $SAMPLES/images/ccimage $TMP/1 \
    && megawave cfdiffuse $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 2> /dev/null | cut -d"=" -f2` \
    && approx $VAL 9.08767 \
    && pass || fail

megawave cfmdiffuse -n 2 $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2_01 2> /dev/null | cut -d"=" -f2` \
    && approx $VAL 9.08767 \
    && VAL=`megawave fnorm -v $TMP/2_02 2> /dev/null | cut -d"=" -f2` \
    && approx $VAL 7.25923 \
    && pass || fail

megawave funzoom -z 4 -o 0 -ftype IMG $SAMPLES/images/cimage $TMP/1 2> /dev/null \
    && megawave erosion -r 1.5 -n 1 $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 13.8314 \
    && pass || fail

megawave opening -r 1.5 -n 1 $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 13.77 \
    && pass || fail

megawave median -r 1.5 -n 1 $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 13.7447 \
    && pass || fail

megawave amss -l 2 -d $TMP/2 $TMP/1 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 11.838 \
    && pass || fail

megawave fquant $TMP/1 $TMP/2 5 > /dev/null \
    && megawave osamss -l 2 $TMP/2 $TMP/3 \
    && VAL=`megawave fnorm -v $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 11.8249 \
    && pass || fail

megawave heat -n 10 -s 0.1 $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 10.777 \
    && pass || fail

megawave fsmooth -S 2 -W 1 $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 7.20909 \
    && pass || fail

echo "-1 1 -1 1" | megawave freadasc $TMP/2 2 2 \
    && megawave fconvol $TMP/1 $TMP/2 $TMP/3 \
    && VAL=`megawave fnorm -v $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 26.6814 \
    && pass || fail

megawave fsepconvol -g 2 $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 9.13969 \
    && pass || fail

megawave fgrain -a 20 $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 13.8573 \
    && pass || fail

megawave forder -e 5 -n 1 $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 13.697 \
    && pass || fail

megawave fsharpen -p 50 $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 22.7716 \
    && pass || fail

megawave rotaffin -r 5 -a 3 -t 3 -T 0 -A 5 $TMP/2 \
    && VAL=`grep nimage $TMP/2 | cut -d":" -f2` \
    && exact $VAL 15 \
    && VAL=`megawave fnorm -v $TMP/2_10 | cut -d"=" -f2` \
    && approx $VAL 29.1366 \
    && pass || fail

megawave infsup -n 2 $TMP/1 $TMP/2 $TMP/3 \
    && VAL=`megawave fnorm -v $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 11.2894 \
    && pass || fail

megawave ll_sharp -p 20 $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 16.9317 \
    && pass || fail

megawave resthline $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 16.5496 \
    && pass || fail

megawave shock -n 10 -s 0.1 $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 19.0514 \
    && pass || fail

megawave tvdenoise $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 14.5648 \
    && pass || fail

megawave tvdenoise2 $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 14.4665 \
    && pass || fail

megawave nlmeans -s 3 -d 5 $SAMPLES/images/cimage $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 6.45733 \
    && pass || fail

megawave fconvol $TMP/1 $DATA/image/blur3x3.ir $TMP/2 \
    && megawave tvdeblur -n 30 $TMP/2 $DATA/image/blur3x3.ir $TMP/3 \
    && VAL=`megawave fnorm -p 2 -c $TMP/1 $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 7.51181 \
    && pass || fail

megawave cmextract $SAMPLES/movies/cmovie $TMP/1 128 128 3 140 140 7 \
    && megawave mam -n 20 -a 0 $TMP/1 $TMP/2 > /dev/null \
    && VAL=`megawave fnorm -v $TMP/2_03 | cut -d"=" -f2` \
    && approx $VAL 8.49737 \
    && pass || fail

megawave prolate -n 128 3 0.5 $TMP/1 > /dev/null \
    && VAL=`megawave fsize $TMP/1` \
    && exact "$VAL" "3 3" \
    && VAL=`megawave fmean $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 0.111111 \
    && pass || fail

# TODO
# cfsharpen
# flipschitz
# prolatef

echo

# image/fourier
echo -n "image/fourier: "

megawave fft2d -A $TMP/1 -B $TMP/2 $SAMPLES/images/cimage  \
    && megawave fextract $TMP/1 $TMP/1 20 20 230 230 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 1234.21 \
    && megawave fextract $TMP/2 $TMP/2 20 20 230 230 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 1230.29 \
    && pass || fail

megawave fft2dpol -M $TMP/1 -P $TMP/2 $SAMPLES/images/cimage  \
    && megawave fextract $TMP/1 $TMP/1 20 20 230 230 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 784.174 \
    && megawave fextract $TMP/2 $TMP/2 20 20 230 230 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 1.65156 \
    && pass || fail

megawave fft2drad -l -s 100 $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave snorm -b 5 -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 0.0241473 \
    && VAL=`megawave snorm -b 5 -p 2 $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 3.48635 \
    && pass || fail

megawave fft2dview -t 0 -o $TMP/1 $SAMPLES/images/cimage \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 1923.05 \
    && pass || fail

megawave fftgrad -n $TMP/1 $SAMPLES/images/cimage \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 8.00567 \
    && pass || fail

megawave fftrot -a 33 $SAMPLES/images/cimage $TMP/1  \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 9.72568 \
    && pass || fail

megawave funzoom -z 4 -o 0 $SAMPLES/images/cimage $TMP/1 \
    && megawave fftzoom -z 2 $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 10.8767 \
    && pass || fail

megawave fhamming $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 2.74239 \
    && pass || fail

megawave frandphase $SAMPLES/images/fimage $TMP/1 \
    && megawave fft2dpol -P $TMP/2 $TMP/1 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && rand_approx $VAL 1.63 \
    && pass || fail

megawave fextract $SAMPLES/images/cimage $TMP/1 10 10 200 210 \
    && megawave fft2dshrink $TMP/1 $TMP/2 \
    && VAL=`megawave fsize $TMP/2` \
    && exact "$VAL" "189 200" \
    && pass || fail

megawave fshrink2 $TMP/1 $TMP/2 \
    && VAL=`megawave fsize $TMP/2` \
    && exact "$VAL" "128 128" \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 8.33146 \
    && pass || fail

megawave fsym2 $TMP/2 $TMP/2 \
    && VAL=`megawave fsize $TMP/2` \
    && exact "$VAL" "256 256" \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 8.36696 \
    && pass || fail

megawave wiener -W 0.1 -g 1 $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave fnorm -b 10 -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 11.1962 \
    && pass || fail

megawave fkeepphase $SAMPLES/images/cimage $SAMPLES/images/fimage $TMP/1 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 14.041 \
    && pass || fail

megawave faxpb -a 0 -b 0 $SAMPLES/images/cimage $TMP/1 \
    && megawave fpset $TMP/1 0 0 1 $TMP/1 \
    && megawave fsepconvol -b 2 -g 3 $TMP/1 $TMP/2 \
    && megawave fft2d -A $TMP/3 $TMP/2 \
    && megawave fftconvol $SAMPLES/images/cimage $TMP/3 $TMP/4 \
    && megawave fsepconvol -b 2 -g 3 $SAMPLES/images/cimage $TMP/5 \
    && VAL=`megawave fnorm -t 0.0001 -p 2 -c $TMP/4 $TMP/5 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

echo

# image/io
echo -n "image/io: "

megawave ccopy $SAMPLES/images/cimage $TMP/1 \
    && megawave fdiff $SAMPLES/images/cimage $TMP/1 $TMP/1 \
    && VAL=`megawave fnorm -p 2 $TMP/1 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

megawave fcopy $SAMPLES/images/fimage $TMP/1 \
    && megawave fdiff $SAMPLES/images/fimage $TMP/1 $TMP/1 \
    && VAL=`megawave fnorm -p 2 $TMP/1 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

megawave cccopy $SAMPLES/images/ccimage $TMP/1 \
    && megawave cfdiff $SAMPLES/images/ccimage $TMP/1 $TMP/1 \
    && megawave cfgetchannels $TMP/1 $TMP/2 $TMP/3 $TMP/4 \
    && VAL=`megawave fnorm -p 2 $TMP/2 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && VAL=`megawave fnorm -p 2 $TMP/3 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && VAL=`megawave fnorm -p 2 $TMP/4 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

# TODO
# cview
# fview
# ccview
# cmview
# ccmview
# flip

megawave fconst $TMP/1 0 60 20 \
    && echo "hi guys..." | \
    megawave ccputstring -r 3 -c 900 -C 90 $TMP/1 10 1 $TMP/1 \
    && megawave ccopy $TMP/1 $TMP/1 2> /dev/null \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 19.8406 \
    && pass || fail

megawave cfgetchannels $SAMPLES/images/ccimage $TMP/1 $TMP/2 $TMP/3 \
    && megawave cfputchannels $TMP/1 $TMP/2 $TMP/3 $TMP/4 \
    && megawave cfdiff -ftype IMG \
    $SAMPLES/images/ccimage $TMP/4 $TMP/1 2> /dev/null \
    && VAL=`megawave fnorm -p 2 $TMP/1 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

megawave cfchgchannels $SAMPLES/images/ccimage $TMP/1 \
    && megawave cfgetchannels $TMP/1 $TMP/2 $TMP/3 $TMP/4 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 7.36361 \
    && VAL=`megawave fnorm -v $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 15.9369 \
    && VAL=`megawave fnorm -v $TMP/4 | cut -d"=" -f2` \
    && approx $VAL 9.98046 \
    && pass || fail

megawave cline_extract $SAMPLES/images/cimage $TMP/1 30 \
    && VAL=`megawave snorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 11.1133 \
    && pass || fail

megawave fline_extract $SAMPLES/images/cimage $TMP/1 30 \
    && VAL=`megawave snorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 11.1133 \
    && pass || fail

echo "1 5 2 3" | megawave creadasc $TMP/1 2 2 \
    && VAL=`megawave fmean $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 2.75 \
    && VAL=`megawave fvar $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 2.91667 \
    && pass || fail

megawave cprintasc $TMP/1 > $TMP/2 \
    && VAL=`cat $TMP/2 | wc -l` \
    && exact $VAL 1 \
    && pass || fail

megawave fprintasc $TMP/1 > $TMP/2 \
    && VAL=`cat $TMP/2 | wc -l` \
    && exact $VAL 1 \
    && pass || fail

# common: freadasc Mkmovie cfgetchannels

echo

# image/level_lines
echo -n "image/level_lines: "

# TODO
# flst
# flst_pixels
# flst_reconstruct
# flst_boundary

# TODO
# flst_bilinear
# flstb_boundary
# flstb_dual
# flstb_dualchain
# flstb_quantize
# flstb_tv

# TODO
# fml_ml
# fsaddles
# ll_distance
# ll_extract
# llmap
# ll_remove
# llremove
# llview

# TODO
# ml_decompose
# ml_draw
# ml_extract
# ml_fml
# ml_reconstruct

# TODO
# mscarea
# tjmap
# tjpoint

# TODO
# cll_remove
# cml_decompose
# cml_draw
# cml_reconstruct

echo

# image/misc
echo -n "image/misc: "

megawave cdisc $TMP/1 100 100 \
    && VAL=`megawave fmean $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 126.888 \
    && pass || fail

megawave funzoom -z 4 -o 0 -ftype IMG \
    $SAMPLES/images/cimage $TMP/1 2> /dev/null \
    && megawave cdisc -r 16 $TMP/2 64 64 \
    && megawave binarize -i $TMP/2 $TMP/2 \
    && megawave fmask $TMP/3 $TMP/2 $TMP/2 $TMP/1 \
    && megawave fmask -i -c 1 $TMP/2 $TMP/2 $TMP/2 \
    && megawave disocclusion $TMP/3 $TMP/2 $TMP/4 > $TMP/1 \
    && VAL=`grep energy $TMP/1 | cut -d"=" -f5` \
    && approx $VAL 980.02 \
    && VAL=`megawave fnorm -v $TMP/4 | cut -d"=" -f2` \
    && approx $VAL 14.66 \
    && pass || fail

# TODO
# drawocclusion

echo "10 10 60 20 30 50 e 100 10 110 50 150 20 q" \
    | megawave flreadasc 2 $TMP/1 > /dev/null  \
    && megawave fillpolys -x 160 -y 60 $TMP/1 $TMP/1 \
    && megawave emptypoly $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 12.4411 \
    && pass || fail

megawave binarize -i -t 120 $SAMPLES/images/cimage $TMP/1 \
    && megawave thinning $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 17.7433 \
    && pass || fail

megawave funzoom -z 4 -o 0 -ftype IMG $SAMPLES/images/cimage $TMP/1 2> /dev/null \
    && megawave binarize -i -t 120 $TMP/1 $TMP/1 \
    && megawave skeleton -n 10 $TMP/1 $DATA/image/seg_mask $TMP/2 > /dev/null \
    && VAL=`grep nimage $TMP/2 | cut -d":" -f2` \
    && exact $VAL 11 \
    && VAL=`megawave fnorm -v $TMP/2_11 | cut -d"=" -f2` \
    && approx $VAL 32.5981 \
    && pass || fail

# TODO
# lsnakes
# lsnakes_demo
# mac_snakes
# ccdisocclusion

echo

# image/operations
echo -n "image/operations: "

megawave fop -p -A $SAMPLES/images/cimage $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave fnorm -p 2 $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 287.596 \
    && pass || fail

megawave faxpb -a -1 $SAMPLES/images/cimage $TMP/1 \
    && megawave fabso $TMP/1 $TMP/2 \
    && megawave fdiff $TMP/2 $SAMPLES/images/cimage $TMP/3 \
    && VAL=`megawave fnorm -p 2 $TMP/3 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

megawave fentropy $SAMPLES/images/cimage > $TMP/1 \
    && VAL=`cat $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 7.51668 \
    && pass || fail

megawave fderiv -n $TMP/1 $SAMPLES/images/cimage \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 3.85916 \
    && pass || fail

megawave finfo $SAMPLES/images/cimage > $TMP/1 \
    && VAL=`grep "bv norm" $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 8.732667 \
    && pass || fail

megawave fmse -n $SAMPLES/images/cimage $SAMPLES/images/fimage > $TMP/1 \
    && VAL=`grep "^SNR" $TMP/1 | cut -d"=" -f2` \
    && approx $VAL -3.48774 \
    && VAL=`grep PSNR $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 8.10192 \
    && VAL=`grep MSE $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 2.23241 \
    && VAL=`grep MRD $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 89.7111 \
    && pass || fail

megawave cdisc $TMP/1 256 256 \
    && megawave fmask $TMP/2 $TMP/1 $SAMPLES/images/cimage $SAMPLES/images/fimage \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 11.5658 \
    && pass || fail

megawave fpsnr255 $SAMPLES/images/fimage > $TMP/1 \
    && VAL=`cat $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 0.347482 \
    && pass || fail

megawave frthre -l 100 $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave fmean $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 44.9395 \
    && pass || fail

# common: faxpb fpset cfdiff fadd fconst fdiff fmean fnorm fsize fvar

echo

# image/seg
echo -n "image/seg: "

echo

# image/shape_recognition
echo -n "image/shape_recognition: "

echo

# image/values
echo -n "image/values: "

megawave binarize -t 150 $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave fmean $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 77.9132 \
    && pass || fail

megawave funzoom -z 8 $SAMPLES/images/cimage $TMP/1 \
    && megawave fquant $TMP/1 $TMP/1 5 > $TMP/2 \
    && VAL=`cut -d"=" -f2 $TMP/2` \
    && megawave amle_init $TMP/1 $VAL $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 48.7051 \
    && pass || fail

megawave amle $TMP/2 $TMP/3 2> /dev/null \
    && VAL=`megawave fnorm -v $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 17.4285 \
    && pass || fail

megawave cmextract $SAMPLES/movies/cmovie $TMP/1 40 170 3 80 210 7 \
    && for I in 1 2 3 4 5; do
        megawave faxpb -ftype IMG -a 0.1 \
	    $TMP/1_0$I $TMP/1_0$I 2> /dev/null
	megawave faxpb -ftype IMG -a 10 -b 5 \
	    $TMP/1_0$I $TMP/1_0$I 2> /dev/null
    done \
    && megawave amle3d_init $TMP/1 10 $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2_03 | cut -d"=" -f2` \
    && approx $VAL  26.1018 \
    && pass || fail

megawave amle3d $TMP/2 $TMP/3 \
    && VAL=`megawave fnorm -v $TMP/3_03 | cut -d"=" -f2` \
    && approx $VAL 4.03325 \
    && pass || fail

megawave fvalues -r $TMP/1 $SAMPLES/images/cimage $TMP/2 \
    && VAL=`grep size $TMP/2 | cut -d":" -f2` \
    && approx $VAL 256 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 8.73267 \
    && pass || fail

megawave ccontrast $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 12.5844 \
    && pass || fail

megawave ccontrast_local -d 2 $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 13.2157 \
    && pass || fail

megawave fconst $TMP/1 0 100 100 \
    && megawave cnoise -i 50 $TMP/1 $TMP/1 \
    && VAL=`megawave fmean $TMP/1 | cut -d"=" -f2` \
    && rand_approx $VAL 64 \
    && pass || fail

megawave fconst $TMP/1 0 100 100 \
    && megawave fnoise -g 10 $TMP/1 $TMP/1 \
    && VAL=`megawave fvar $TMP/1 | cut -d"=" -f2` \
    && rand_approx $VAL 100 \
    && pass || fail

megawave cmextract $SAMPLES/movies/cmovie $TMP/1 10 10 1 210 210 10 \
    && megawave cmnoise -i 50 $TMP/1 $TMP/2 \
    && VAL=`grep nimage $TMP/2 | cut -d":" -f2` \
    && exact $VAL 10 \
    && VAL=`megawave fmean $TMP/2_05 | cut -d"=" -f2` \
    && rand_approx $VAL 116 \
    && pass || fail

megawave chisto $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave snorm -p 2 $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 366.222 \
    && pass || fail

megawave fhisto $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave snorm -p 2 $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 929.198 \
    && pass || fail

megawave flgamma -f 256 $TMP/1 \
    && VAL=`megawave flprintasc $TMP/1 | grep "^246" | cut -d" " -f2` \
    && approx $VAL 236.391 \
    && pass || fail

megawave fcontrast $SAMPLES/images/cimage $TMP/1 $TMP/2 \
    && VAL=`megawave fnorm -p 2 $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 103.585 \
    && pass || fail

megawave frank -r $TMP/1 $SAMPLES/images/cimage \
    && VAL=`megawave fnorm -p 2 $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 0.577342 \
    && pass || fail

megawave fthre -N $SAMPLES/images/cimage $TMP/1 \
    && VAL=`megawave fnorm -p 2 $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 143.798 \
    && pass || fail

# TODO
# cfquant
# bicontrast

# common: fquant

echo

# signal
echo -n "signal: "

megawave entropy $SAMPLES/signals/fsignal > $TMP/1 \
    && VAL=`cut -d"=" -f2 $TMP/1` \
    && approx $VAL 11.0934 \
    && pass || fail

megawave sprintasc $SAMPLES/signals/fsignal 101 101 > $TMP/1 \
    && exact `cat $TMP/1` 3014 \
    && pass || fail

megawave sprintasc $SAMPLES/signals/fsignal 1 123 | megawave sreadasc $TMP/1 123 \
    && megawave fft1dshrink $TMP/1 $TMP/2 \
    && VAL=`grep "size:" $TMP/2 | cut -d":" -f2` \
    && exact $VAL 121 \
    && pass || fail

megawave sshrink2 $SAMPLES/signals/fsignal $TMP/1 \
    && VAL=`grep "size:" $TMP/1 | cut -d":" -f2` \
    && exact $VAL 2048 \
    && pass || fail

megawave fct1d $TMP/1 $TMP/2 \
    && VAL=`megawave snorm -b 20 -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 3636.57 \
    && pass || fail

megawave fft1d -A $TMP/2 $TMP/1 \
    && VAL=`megawave snorm -b 20 -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 1888.35 \
    && pass || fail

megawave sconst -s 256 -a 0.1 $TMP/1 \
    && VAL=`grep "size:" $TMP/1 | cut -d":" -f2` \
    && exact $VAL 256 \
    && VAL=`megawave snorm -b 20 -v $TMP/1 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

megawave sderiv $SAMPLES/signals/fsignal $TMP/1 \
    && VAL=`megawave snorm -b 20 -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 42.9396 \
    && pass || fail

megawave sdirac -s 100 -a 100 $TMP/1 \
    && VAL=`megawave snorm -b 0 -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 2 \
    && VAL=`megawave snorm -b 0 -p 2 $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 10 \
    && pass || fail

megawave sgauss -s 20 $TMP/1 3 || lls \
    && VAL=`megawave snorm -b 0 -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 0.0130369 \
    && VAL=`megawave snorm -b 0 -p 2 $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 0.0686237 \
    && pass || fail

megawave sintegral $TMP/1 $TMP/2 \
    && VAL=`megawave snorm -b 0 -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 0.05 \
    && pass || fail

megawave sderiv $TMP/1 $TMP/2 \
    && megawave smse -n $TMP/1 $TMP/2 > $TMP/3 \
    && VAL=`grep "^SNR" $TMP/3 | cut -d"=" -f2` \
    && approx $VAL -1.9738 \
    && VAL=`grep PSNR $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 6.66453 \
    && VAL=`grep MSE $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 1.57536 \
    && VAL=`grep MRD $TMP/3 | cut -d"=" -f2` \
    && approx $VAL 70.6568 \
    && pass || fail

megawave sconst -s 1000 -a 0 $TMP/1 \
    && megawave snoise -g 1 $TMP/1 $TMP/2 \
    && VAL=`megawave snorm -p 2 $TMP/2 | cut -d"=" -f2` \
    && rand_approx $VAL 1 \
    && pass || fail

megawave sop -p -A $SAMPLES/signals/fsignal $SAMPLES/signals/fsignal $TMP/1 \
    && VAL=`megawave snorm -p 2 $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 5349.16 \
    && pass || fail

megawave saxpb -a 2 $SAMPLES/signals/fsignal $TMP/1 \
    && VAL=`megawave snorm -p 2 $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 5349.16 \
    && pass || fail

megawave splot -ftype RIM -o $TMP/1 -n $SAMPLES/signals/fsignal 2> /dev/null \
    && VAL=`megawave fnorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 15.6218 \
    && pass || fail

echo "0 2 3 4 5 4 3 2 3 4 5 4 3 2 1 0" \
    | megawave sreadasc $TMP/1 16 \
    && VAL=`megawave snorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 1 \
    && pass || fail

megawave ssinus -s 100 -a 1 -d 1 $TMP/1  \
    && VAL=`megawave snorm -v $TMP/1 | cut -d"=" -f2` \
    && approx $VAL 0.0785888 \
    && pass || fail

megawave sprintasc $SAMPLES/signals/fsignal 1 1024 \
    | megawave sreadasc $TMP/1 1024 \
    && $SCRIPTS/megawave_swtvdenoise -D 10 -N 200 $TMP/1 $TMP/2 > /dev/null \
    && VAL=`megawave snorm -p 2 -c $TMP/1 $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 11.52 \
    && pass || fail

# TODO
# stvrestore
# w1threshold
# sinfo

# common: snorm

echo

# wave
echo -n "wave: "

megawave owave1 -e 0 $SAMPLES/signals/fsignal $TMP/1 $DATA/wave/ortho/da02.ir \
    && VAL=`grep size $TMP/1_01_A.wtrans1d | cut -d":" -f2` \
    && exact $VAL 1104 \
    && VAL=`grep size $TMP/1_01_D.wtrans1d | cut -d":" -f2` \
    && exact $VAL 1104 \
    && pass || fail

megawave iowave1 -e 0 $TMP/1 $TMP/2 $DATA/wave/ortho/da02.ir \
    && VAL=`megawave snorm -t 0.001 -b 2 -p 2 -c $SAMPLES/signals/fsignal $TMP/2 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

megawave biowave1 $SAMPLES/signals/fsignal $TMP/1 \
    $DATA/wave/biortho/h/sp02.ir $DATA/wave/biortho/htilde/sl05.ir \
    && VAL=`grep size $TMP/1_01_A.wtrans1d | cut -d":" -f2` \
    && exact $VAL 1104 \
    && VAL=`grep size $TMP/1_01_D.wtrans1d | cut -d":" -f2` \
    && exact $VAL 1104 \
    && pass || fail

megawave ibiowave1 -e 0 $TMP/1 $TMP/2 \
    $DATA/wave/biortho/h/sp02.ir $DATA/wave/biortho/htilde/sl05.ir \
    && VAL=`megawave snorm -t 0.001 -b 2 -p 2 -c $SAMPLES/signals/fsignal $TMP/2 | cut -d"=" -f2` \
    && exact $VAL 0 \
    && pass || fail

# TODO
# biowave2
# dybiowave2
# dyowave2
# ibiowave2
# iowave2
# owave2
# owtrans_fimage
# precond1d
# precond2d
# sconvolve

echo

# wave/packets
echo -n "wave/packets: "

megawave wp2dmktree -w 4 $TMP/1 \
    && VAL=`megawave fsize $TMP/1` \
    && exact "$VAL" "16 16" \
    && VAL=`megawave fmean $TMP/1 | cut -d"=" -f2` \
    && exact $VAL 1.32812 \
    && pass || fail

megawave wp2doperate -t 2 -s 15 -b $DATA/wave/packets/biortho/htilde/sd09.ir \
    $TMP/1 $DATA/wave/packets/biortho/h/sd07.ir $SAMPLES/images/cimage $TMP/2 \
    && VAL=`megawave fnorm -v $TMP/2 | cut -d"=" -f2` \
    && approx $VAL 6.15582 \
    && pass || fail

megawave wp2ddecomp $TMP/1 $SAMPLES/images/cimage $DATA/wave/ortho/da05.ir $TMP/2 \
    && pass || fail

# TODO
# wp2dchangepack
# wp2dchangetree
# Wp2dcheck
# wp2dchecktree
# wp2deigenval
# wp2dfreqorder
# wp2drecomp
# wp2dview
# wpsconvolve

echo

# wave/ridgelet
echo -n "wave/ridgelet: "

# TODO
# iridgelet
# istkwave1
# ridgelet
# ridgpolrec
# ridgrecpol
# ridgthres
# stkwave1

echo

results
