#!/bin/sh
# Compile and execute the test cases in the current working directory

GCC=${1:-gcc}

echo "main(){exit(0);}" > x.c
NATIVE=false
if $GCC x.c 2> /dev/null
  then if ./a.out; then NATIVE=true; fi
  fi
rm -f x.c

if $NATIVE; then true; else
  echo "Can't run these tests with that compiler"
  exit 0
  fi

FAILURES=0

testit()
{
  prog=$1
  shift
  if $GCC $prog $@ -w 2> /dev/null
    then
      if sh -c ./a.out 2> /dev/null
	then true
	else
	  echo; echo -n "Problems to run when compiled with \"$@\""
	  FAILURES=`expr $FAILURES + 1`
	fi
    else echo; echo -n "Problems to compile with \"$@\""
  fi
}

for i in `ls -r *.c`; do
  echo -n $i
  testit $i
  testit $i -O
  testit $i -O -fomit-frame-pointer
  testit $i -O -fstrength-reduce
  testit $i -O -fomit-frame-pointer -fstrength-reduce
  echo
done

rm -f *.s *.o a.out core

if [ $FAILURES = 0 ]
  then echo "Test completed successfully."
else if [ $FAILURES = 1 ]
  then echo "Test completed with 1 failure."
else echo "Test completed with $FAILURES failures."
fi; fi
