#!/bin/sh
# Random test cases that require special GCC command line options to
# perform the desired test.
# Please add more cases here in a similar fashion.

cleanup()
{
  rm -f *.s *.o core tempfile x.c x.s a.out *.c.* err
}

if [ "x$1" = x-clean ]
  then
    shift
    cleanup
    if [ $# = 0 ]
      then exit 0
      fi
  fi

if [ $# = 0 ]
  then exec $0 gcc
  fi

if [ $# -gt 1 ]
  then
    for i in "$@"
      do ./$0 "$i"
      done
    exit 0
  fi

if [ -d "$1" ]
  then GCC="$1/xgcc -B$1/"
  else GCC="$1"
  fi
echo "TESTING WITH $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 SC=-c
  else
    echo "Some tests omitted because of cross-testing"
    SC=-S
  fi

FAILURES=0

testit()
{
  prog=$1
  shift
  $GCC $prog $@ -w 2> err
  status=$?
  if [ $status != 0 ]
    then
      if grep "fatal signal" < err > tempfile
        then
          signal=`sed 's;.*signal \(.*\)$;\1;' < tempfile`
          echo "ERROR: compiler got signal $signal with: $@"
          FAILURES=`expr $FAILURES + 1`
        else
          echo "ERROR: compiler returns exit status $status with: $@"
          FAILURES=`expr $FAILURES + 1`
        fi
    else	# Compilation OK.  Try to run the result.
      sh -c ./a.out 2> /dev/null
      status=$?
      if [ $status != 0 ]
        then
          if [ $status -ge 128 ]
            then
              echo "ERROR: compiled program got signal `expr $status - 128` with: $@"
              FAILURES=`expr $FAILURES + 1`
            else
              echo "ERROR: compiled program returns exit status $status with: $@"
              FAILURES=`expr $FAILURES + 1`
            fi
        fi
    fi
}

################## ADD NEXT CASE HERE (NOT AT THE END) ##################

echo 921210-1
echo '#define a1(y) (y+1)' > x.c
echo '#define a2(y) a1(y)+1' >> x.c
echo '#define f a->f' >> x.c
echo 'a2(f)' >> x.c
$GCC -E x.c 2>/dev/null | tail -1  | sed 's; ;;g' >tempfile
if echo '(a->f+1)+1' | cmp -s - tempfile
  then true
  else
    echo "Problems with 921210-1"
    FAILURES==`expr $FAILURES + 1`
  fi

#echo 920821-1
#echo 'f(c){asm("%i0"::"r"(c));}' > x.c
#$GCC -S x.c 2> /dev/null
#stat=$?
#if [ $stat != 0 ]
#  then
#    echo "Problems with 920821-1"
#    FAILURES=`expr $FAILURES + 1`
#  fi

echo 920521-1
echo 'f(){asm("f":::"cc");}g(x){asm("g"::"%r"(x));}' > x.c
$GCC -S x.c 2> /dev/null
stat=$?
if [ $stat != 0 ]
  then
    echo "Problems with 920521-1"
    FAILURES=`expr $FAILURES + 1`
  fi

echo 920520-1
echo 'f(){asm("%0"::"r"(1.5F));}g(){asm("%0"::"r"(1.5));}' > x.c
$GCC -S x.c 2> /dev/null
stat=$?
if [ $stat != 0 ]
  then
    echo "Problems with 920520-1"
    FAILURES=`expr $FAILURES + 1`
  fi

echo 920717-1
if $NATIVE; then
  echo 'const char s[]="foo";' > x.c
  echo 'extern const char s[];main(){puts(s);}' > y.c
  $GCC x.c -c
  cc y.c -c 2> /dev/null > /dev/null
  stat=$?
  if [ $stat != 0 ]
    then
      sh -c c89 y.c -c 2> /dev/null > /dev/null
      stat=$?
    fi
  if [ $stat = 0 ]
    then
      $GCC x.o y.o 2> /dev/null
      if [ $? != 0 ]
        then
          echo "Problems with 920717-1"
	  FAILURES=`expr $FAILURES + 1`
        fi
    fi
  rm x.? y.?
fi

echo 920730-1
if $NATIVE; then
  echo "f1(){int b=0x80000000;return b>=0x80000000;} \
        f2(){int b=0x80000001;return b>=0x80000001;} \
        f3(){int b=0x7fffffff;return b>=0x7fffffff;} \
        f4(){int b=0xffffffff;return b>=0xffffffff;} \
        main (){if((f1()&f2()&f3()&f4())!=1)abort();exit(0);}" > x.c
  testit x.c
  testit x.c -traditional
  testit x.c -O
  testit x.c -traditional -O
fi

echo 920413-1
echo "x(b){unsigned long c;c=4294967295U/(unsigned long)b;}" > x.c
$GCC $SC -w -o tempfile -Wtraditional x.c

echo 920411-1
$GCC -E 920411-1.c > /dev/null

cleanup

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
