#!/bin/sh

echo Starting the trap tests...

# integer overflow (no longer checked)

#if [ `echo '
#i=1;while(i>0){i+=i;};
#i=1;while(i>0){i+=i;};
#' | ../src/algae -V -i -S 2>&1 | grep -i -c 'overflow'` -lt 2 ]
#then
#    if [ `echo '
#    i=1;while(i>0){i+=i;};
#    i=1;while(i>0){i+=i;};
#    ' | ../src/algae -V -i -S 2>&1 | grep -i -c 'run time error'` -lt 2 ]
#    then
#	echo ...failed to trap integer overflow
#    else
#	echo ...sloppy handling of integer overflow
#    fi
#fi

# floating point overflow

if [ `echo '
i=1.0;while(i<i*1e6){i=i*1e6;};
i=1.0;while(i<i*1e6){i=i*1e6;};
' | ../src/algae -V -i -S 2>&1 | grep -i -c 'point overflow'` -lt 2 ]
then
    if [ `echo '
    i=1.0;while(i<i*1e6){i=i*1e6;};
    i=1.0;while(i<i*1e6){i=i*1e6;};
    ' | ../src/algae -V -i -S 2>&1 | grep -i -c 'run time error'` -lt 2 ]
    then
	echo ...failed to trap floating point overflow
    else
	echo '...sloppy (but acceptable) handling of floating point overflow'
    fi
fi

# floating point divide by zero

if [ `echo '
[1.0,2.0]/[0.0,1.0]
[1.0,2.0]/[0.0,1.0]
' | ../src/algae -V -i -S 2>&1 | grep -i -c 'Divide by zero'` -lt 2 ]
then
    if [ `echo '
    [1.0,2.0]/[0.0,1.0]
    [1.0,2.0]/[0.0,1.0]
    ' | ../src/algae -V -i -S 2>&1 | grep -i -c 'run time error'` -lt 2 ]
    then
	echo ...failed to trap floating point divide by zero
    else
	echo '...sloppy (but acceptable) handling of floating point divide by zero'
    fi
fi

# range error

if [ `echo '
d=1.0;while(d<exp(d)){d=exp(d);}
d=1.0;while(d<exp(d)){d=exp(d);}
' | ../src/algae -V -i -S 2>&1 | grep -i -c 'out of range'` -lt 2 ]
then
    if [ `echo '
    d=1.0;while(d<exp(d)){d=exp(d);}
    d=1.0;while(d<exp(d)){d=exp(d);}
    ' | ../src/algae -V -i -S 2>&1 | grep -i -c 'run time error'` -lt 2 ]
    then
        echo ...failed to trap range error in math library
    else
        echo '...sloppy (but acceptable) handling of range error in math library'
    fi
fi

# domain error

if [ `echo '
log(0)
log(0)
' | ../src/algae -V -i -S 2>&1 | grep -i -c 'out of domain'` -lt 2 ]
then
    if [ `echo '
    log(0)
    log(0)
    ' | ../src/algae -V -i -S 2>&1 | grep -i -c 'run time error'` -lt 2 ]
    then
        echo ...failed to trap domain error in math library
    else
        echo '...sloppy (but acceptable) handling of domain error in math library'
    fi
fi

echo ...done.
