#!/usr/local/bin/kermit
#
# Torture-test for new combined IF/XIF command
#
# First: compound Boolean conditions

.\%z = 0
.\%m = 1
.\%b = 0
.\%g = 0

define GOOD { increment \%g, echo GOOD:\%g  }
define BAD { increment \%b, echo BAD:\%b  }

echo Executing \%0...
set tak echo on
if ( = \%m 1 || = \%m 0 ) { GOOD } else { BAD }
if ( = \%m 1 && = \%m 0 ) { BAD } else { GOOD }
if ( \%z || 0 ) { BAD } else { GOOD }
if ( \%m || 0 ) { GOOD } else { BAD }
if ( exist \%0 && > \%m 0 )  { GOOD } else { BAD }
if ( = \%m 1 && ( = \%z 0 || = \%z 999 ) ) { GOOD } else { BAD }
if 1 { GOOD } else { BAD }
if \%m { GOOD } else { BAD }
if ( 1 )  { GOOD } else { BAD }
if ( \%m )  { GOOD } else { BAD }
if ( not 1 ) { BAD } else { GOOD }
if ( ( 1 & \%m ) ) { GOOD } else { BAD }
if ( not ( 1 & \%m ) ) { BAD } else { GOOD }
if ( true ) { GOOD } else { BAD }
if ( not true ) { BAD } else { GOOD }
if ( false ) { BAD } else { GOOD }
if ( not false ) { GOOD } else { BAD }
if ( ( 5 & 1 ) && ( 6 & 2 ) ) { GOOD } else { BAD }
if ( (5&1) && (6&2) ) { GOOD } else { BAD }
if ( not ( 5 & 1 ) && ( 6 & 2 ) ) { BAD } else { GOOD }
if ( not ( ( 5 & 1 ) && ( 6 & 2 ) ) ) { BAD } else { GOOD }
if ( not not ( ( 5 & 1 ) && ( 6 & 2 ) ) ) { GOOD } else { BAD }
if ( > \%z 0 ) { BAD } else { GOOD }
if ( > \%z+1 0 ) { GOOD } else { BAD }
if ( > (\%z+1) 0 ) { GOOD } else { BAD }
if ( > \%z 0 || > \%m \%z && = \%m 0 ) { BAD } else { GOOD }
if ( > \%z 0 || ( > \%m \%z && = \%m 0 ) ) { BAD } else { GOOD }
if ( ( > \%z 0 || > \%m \%z ) && = \%m 0 ) { BAD } else { GOOD }
echo {----}
if ( false || false && false || true ) { GOOD } else { BAD }
if ( false || ( false && false ) || true ) { GOOD } else { BAD }
if ( ( false || false ) && ( false || true ) ) { BAD } else { GOOD }
echo {----}
{ assert = \%z \%m, status, if ok { BAD } else { GOOD } }
{ assert != \%z \%m, status, if not ok { BAD } else { GOOD } }
{ assert true, status, if not ok { BAD } else { GOOD } }
{ assert false, status, if ok { BAD } else { GOOD } }
{ assert not true, status, if ok { BAD } else { GOOD } }
{ assert not false, status, if not ok { BAD } else { GOOD } }
{ assert ( true ), status, if not ok { BAD } else { GOOD } }
{ assert ( false ), status, if ok { BAD } else { GOOD } }
{ assert ( not true ), status, if ok { BAD } else { GOOD } }
{ assert ( not false ), status, if not ok { BAD } else { GOOD } }
{ assert ( not not true ), status, if not ok { BAD } else { GOOD } }
{ assert ( not not false ), status, if ok { BAD } else { GOOD } }
{ assert ( not ( not true ) ), status, if not ok { BAD } else { GOOD } }
{ assert ( not ( not false ) ), status, if ok { BAD } else { GOOD } }
{ assert ( not ( not true ) ), status, if not ok { BAD } else { GOOD } }
{ assert ( not ( not false ) ), status, if ok { BAD } else { GOOD } }
{ assert ( not ( not ( not false ) ) ), status, -
  if not ok { BAD } else { GOOD } }
{ assert ( not not not false ), status, if not ok { BAD } else { GOOD } }
echo {----}
{ assert ( true || false ), status, if not ok  { BAD } else { GOOD } }
{ assert ( not true || false ), status, if ok  { BAD } else { GOOD } }
{ assert ( not ( true || false ) ), status, if ok { BAD } else { GOOD } }
{ assert ( not ( true || false ) && ( false || true ) ), -
  status, -
  if ok  { BAD } else { GOOD } }
echo {----}
.\%i = 0
.\%j = 1
while ( < \%i 10 && < \%j 10 ) { echo \%i \%j, incr \%i, incr \%j }
if ok { GOOD } else { BAD }
set take echo off
echo {----}
echo Summary: \%g { GOOD }, \%b { BAD }.
echo
echo Testing if--else if--else if--else...
echo You should see the names of the digits 1 through 10 in order
echo and then "None of the above".
echo
for \%i 1 11 1 {
    if = \%i 1 {
        echo One
    } else if = \%i 2 {
	echo Two
    } else if = \%i 3 {
	echo Three
    } else if = \%i 4 {
	echo Four
    } else if = \%i 5 {
	echo Five
    } else if = \%i 6 {
	echo Six
    } else if = \%i 7 {
	echo Seven
    } else if = \%i 8 {
	echo Eight
    } else if = \%i 9 {
	echo Nine
    } else if = \%i 10 {
	echo Ten
    } else {
        echo None of the above.
    }
}

echo Same thing again with separated ELSE parts:
echo
for \%i 1 11 1 {
    if = \%i 1 echo Uno
    else if = \%i 2 echo Dos
    else if = \%i 3 echo Tres
    else if = \%i 4 echo Cuatro
    else if = \%i 5 echo Cinco
    else if = \%i 6 echo Seis
    else if = \%i 7 echo Siete
    else if = \%i 8 echo Ocho
    else if = \%i 9 echo Nueve
    else if = \%i 10 echo Diez
    else echo Ninguna de las anteriores.
}

echo
echo Checking preservation of macro args in compound IF statements...
define yy {
    if def \%3 {
        echo Three args:
        for \%i 1 3 1 { echo { "\&_[\%i]" } }
    } else if def \%2 {
        echo Two args:
        for \%i 1 2 1 { echo { "\&_[\%i]" } }
    } else if def \%1 {
        echo One arg:
        for \%i 1 1 1 { echo { "\&_[\%i]" } }
    } else {
        echo No args.
    }
}
yy one two three
yy alpha beta
yy justme
yy


echo
define xx {
   echo Checking \\v(return) from inside IF/FOR/WHILE...
   return 888
}
do xx
if true {
    for \%i 1 1 1 {
        while true {
            if false {
                echo BAD
            } else {
                if equal "\v(return)" "888" echo GOOD
                break
            }
        }
    }
}
echo {----}
echo Testing separated compound ELSE parts...
echo You should see the names of the digits 1-4.
if true {
    echo "  one"
    echo "  two"
}
else {
    echo "  three"
    echo "  four"
}
if false {
    echo "  one"
    echo "  two"
}
else {
    echo "  three"
    echo "  four"
}
echo
echo In each of the following tests you should see the names
echo of the first 4 Greek letters.

def xx {
    if \%1 {
	echo "  \%2"
	echo "  \%3"
    } else {
	echo "  \%4"
	echo "  \%5"
    }
}
xx true  alpha beta gamma delta
xx false alpha beta gamma delta

echo
echo Again...

def xx {
    if \%1 {
	echo "  \%2"
	echo "  \%3"
    }
    else {
	echo "  \%4"
	echo "  \%5"
    }
}
xx true  alpha beta gamma delta
xx false alpha beta gamma delta

echo
echo And again...

def xx {
    if \%1 { echo "  \%2", echo "  \%3"  } else { echo "  \%4",	echo "  \%5" }
}
xx true  alpha beta gamma delta
xx false alpha beta gamma delta

echo
echo And again...

def xx {
    if \%1 { echo "  \%2", echo "  \%3"  }
    else { echo "  \%4", echo "  \%5" }
}
xx true  alpha beta gamma delta
xx false alpha beta gamma delta

echo
echo And once more...

def xx {
    if \%1 echo "  \%2\v(newline)  \%3"
    else   echo "  \%4\v(newline)  \%5"
}
xx true  alpha beta gamma delta
xx false alpha beta gamma delta

echo
echo Last one...

def yy {
    { echo "  \%1", echo "  \%2", echo "  \%3", echo "  \%4" }
}
yy alpha beta gamma delta

echo
exit 0 Done.
