#!/bin/bash
set -e

out=${1:-output}

echo "" > $out

echo "Below is the feedback and provisional mark for your submission" >> $out
echo "for CW 8, Part 1.  Please note all marks are provisional until" >> $out
echo "ratified by the assessment board -- this is not an official" >> $out
echo "results transcript." >> $out
echo "" >> $out

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


# compilation tests

function scala_compile {
  (ulimit -t 1 ; scala "$1" 1> $out 2> $out) 
}


# functional tests

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


# marks for CW
marks=$(( 0 ))


# 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
  echo " nullable(ZERO) == false" | tee -a $out
  echo " nullable(ONE) == true" | tee -a $out
  echo " nullable(CHAR('a')) == false" | tee -a $out
  echo " nullable(ZERO | ONE) == true" | tee -a $out
  echo " nullable(ZERO | CHAR('a')) == false" | tee -a $out
  echo " nullable(ONE ~  ONE) == true" | tee -a $out
  echo " nullable(ONE ~ CHAR('a')) == false" | tee -a $out
  echo " nullable(STAR(ZERO)) == true" | tee -a $out
  
  if (scala_assert "re.scala.bak" "../../../marking/re1a_test.scala")
  then
    echo "  --> success" | tee -a $out
    marks=$(( marks + 1 ))
  else
    echo "  --> test failed" | tee -a $out
  fi
fi

if [ $tsts1 -eq 0 ]
then
  echo " der('a', ZERO | ONE) == (ZERO | ZERO)" | tee -a $out
  echo " der('a', (CHAR('a') | ONE) ~ CHAR('a')) == ALT((ONE | ZERO) ~ CHAR('a'), ONE)" | tee -a $out
  echo " der('a', STAR(CHAR('a'))) == (ONE ~ STAR(CHAR('a')))" | tee -a $out
  echo " der('b', STAR(CHAR('a'))) == (ZERO ~ STAR(CHAR('a')))" | tee -a $out
  
  if (scala_assert "re.scala.bak" "../../../marking/re1b_test.scala")
  then
    echo "  --> success" | tee -a $out
    marks=$(( marks + 1 ))
  else
    echo "  --> test failed" | tee -a $out
  fi
fi

if [ $tsts1 -eq 0 ]
then
  echo " simp(ZERO | ONE) == ONE" | tee -a $out
  echo " simp(STAR(ZERO | ONE)) == STAR(ZERO | ONE)" | tee -a $out
  echo " simp(ONE ~ (ONE ~ (ONE ~ CHAR('a')))) == CHAR('a')" | tee -a $out
  echo " simp(ONE ~ (ONE ~ (ONE ~ ZERO))) == ZERO" | tee -a $out
  echo " simp(ALT(ONE ~ (ONE ~ (ONE ~ ZERO)), CHAR('a'))) == CHAR('a')" | tee -a $out
  echo " simp(CHAR('a') | CHAR('a')) == CHAR('a')" | tee -a $out
  echo " simp(ONE | CHAR('a')) == (ONE | CHAR('a'))" | tee -a $out
  
  if (scala_assert "re.scala.bak" "../../../marking/re1c_test.scala")
  then
    echo "  --> success" | tee -a $out
    marks=$(( marks + 1 ))
  else
    echo "  --> test failed" | tee -a $out
  fi
fi

if [ $tsts1 -eq 0 ]
then
  echo " EVIL = (a*)* b" | tee -a $out
  echo " ders(List.fill(5)('a'),EVIL) == SEQ(SEQ(STAR(CHAR('a')),STAR(STAR(CHAR('a')))),CHAR('b'))" | tee -a $out
  echo " ders(List('b'),EVIL) == ONE" | tee -a $out
  echo " ders(List('b','b'),EVIL) == ZERO" | tee -a $out
  echo " matcher(EVIL, \"a\" * 5 ++ \"b\") == true" | tee -a $out
  echo " matcher(EVIL, \"b\") == true" | tee -a $out
  echo " matcher(EVIL, \"bb\") == false" | tee -a $out
  echo " matcher(\"abc\", \"abc\") == true" | tee -a $out
  echo " matcher((\"ab\" | \"a\") ~ (ONE | \"bc\"), \"abc\") == true" | tee -a $out
  echo " matcher(ONE, \"\") == true" | tee -a $out
  echo " matcher(ZERO, \"\") == false" | tee -a $out
  echo " matcher(ONE | CHAR('a'), \"\") == true" | tee -a $out
  echo " matcher(ONE | CHAR('a'), \"a\") == true" | tee -a $out
  
  if (scala_assert "re.scala.bak" "../../../marking/re1d_test.scala")
  then
    echo "  --> success" | tee -a $out
    marks=$(( marks + 1 ))
  else
    echo "  --> test failed" | tee -a $out
  fi
fi

if [ $tsts1 -eq 0 ]
then
  echo " replace(\"aa\".% | \"bb\", \"aabbbaaaaaaabaaaaabbaaaabb\" , \"c\") == \"ccbcabcaccc\"" | tee -a $out
  echo " replace(\"aa\".% | \"bb\", \"abba\" , \"\") == \"aa\"" | tee -a $out
  
  if (scala_assert "re.scala.bak" "../../../marking/re1e_test.scala")
  then
    echo "  --> success" | tee -a $out
    marks=$(( marks + 2 ))
  else
    echo "  --> test failed" | tee -a $out
  fi
fi


## final marks
echo "Overall mark for CW 8, Part 1" | tee -a $out
echo "$marks" | tee -a $out
