#!/bin/bash
set -e

out=${1:-output-test}


function scala_vars {
   (egrep '\bvar\b|\breturn\b|ListBuffer|mutable' "$1" 2> /dev/null 1> /dev/null)
}


# compilation tests

function scala_compile {
  (scala "$1" 2> /dev/null 1> /dev/null) 
}


# functional tests

function scala_assert {
  (scala -i "$1" "$2" -e "" 2> /dev/null 1> /dev/null)
}



# var, return, ListBuffer test
#
#echo "re.scala does not contain vars, returns etc?" | tee -a $out

if (scala_vars re.scala)
then
  #echo "  --> fail" | tee -a $out
  tsts0=$(( 1 ))
else
  #echo "  --> yes" | tee -a $out
  tsts0=$(( 0 )) 
fi


# compilation test
if  [ $tsts0 -eq 0 ]
then    
  #echo "re.scala runs?" | tee -a $out

  if (scala_compile re.scala.bak)
  then
    #echo "  --> yes" | tee -a $out
    tsts1=$(( 0 ))
  else
    #echo "  --> scala re.scala did not run successfully" | tee -a $out
    tsts1=$(( 1 )) 
  fi
else
  tsts1=$(( 1 ))     
fi


if [ $tsts1 -eq 0 ]
then
  if (scala_assert "re.scala.bak" "../../../marking/re1d_test.scala")
  then
    echo "  --> success" | tee -a $out
    t1=$(( 1 ))
  else
    echo "  --> test failed" | tee -a $out
    t1=$(( 0 ))   
  fi
else  
  t1=$(( 0 ))
fi

if [ $tsts1 -eq 0 ]
then
  if (scala_assert "re.scala.bak" "../../../marking/re1d_bug_test.scala")
  then
    echo "  bug --> success" | tee -a $out
    t2=$(( 1 ))
  else
    echo "  bug --> test failed" | tee -a $out
    t2=$(( 0 ))  
  fi
else  
  t2=$(( 0 ))
fi

if [ $t1 -ne $t2 ]
then
  echo "disagree"
  pwd
fi

