#!/bin/zsh
set -euo pipefail


out=${1:-output}

echo -e "" > $out

echo `date` >> $out
echo -e "Below is the feedback and provisional marks for your submission" >> $out
echo -e "of the Core Part 3 (Scala).  Please note all marks are provisional until" >> $out
echo -e "ratified by the assessment board -- this is not an official" >> $out
echo -e "results transcript." >> $out
echo -e "" >> $out

echo -e "Below is the feedback for your submission docdiff.scala" >> $out
echo -e "" >> $out


# marks for CW9 preliminary
marks=$(( 0.0 ))


# compilation tests

function scala_compile {
  (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -Xprint:parser "$1" 2> c$out 1> c$out)
}

# functional tests

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

# purity test
function scala_vars {
   (sed 's/immutable/ok/g' c$out > cb$out;
    egrep '\bvar\b|\breturn\b|\.par\.|\.par |ListBuffer|AtomicInteger|mutable|util.control|new Array' cb$out 2> /dev/null 1> /dev/null)
}


# compilation test

echo -e "postfix.scala runs?" | tee -a $out

if (scala_compile postfix.scala)
then
   echo -e "  --> success" | tee -a $out
   tsts=$(( 0 ))
else
   echo -e "  --> SCALA DID NOT RUN postfix.scala\n" | tee -a $out
   tsts=$(( 1 ))
fi



# var, return, ListBuffer test
#
if  [ $tsts -eq 0 ]
  then   
  echo -e "postfix.scala does not contain VARS, RETURNS etc?" | tee -a $out

  if (scala_vars postfix.scala)
  then
    echo -e "  --> FAIL\n" | tee -a $out  
    tsts=$(( 1 ))
  else
    echo -e "  --> success" | tee -a $out  
    tsts=$(( 0 )) 
  fi
else
  tsts=$(( 1 ))  
fi  



### postfix tests

if [ $tsts -eq 0 ]
then
  echo -e " syard(split(\"3 + 4 * ( 2 - 1 )\")) == List(\"3\", \"4\", \"2\", \"1\", \"-\", \"*\", \"+\")" | tee -a $out
  echo -e " syard(split(\"( ( ( 3 ) ) + ( ( 4 + ( 5 ) ) ) )\")) == List(\"3\", \"4\", \"5\", \"+\", \"+\")" | tee -a $out
  echo -e " syard(split(\"5 + 7 / 2\")) == List(\"5\", \"7\", \"2\", \"/\", \"+\")" | tee -a $out
  echo -e " syard(split(\"5 * 7 / 2\")) == List(\"5\", \"7\", \"*\", \"2\", \"/\")" | tee -a $out
  
  if (scala_assert "postfix.scala" "postfix_test1.scala")
  then
    echo -e "  --> success (+ 1 Mark)\n" | tee -a $out
    marks=$(( marks + 1.0 ))
  else
    echo -e "  --> ONE OF THE TESTS FAILED\n" | tee -a $out
  fi
fi



if [ $tsts -eq 0 ]
then
  echo -e " compute(syard(split(\"3 + 4 * ( 2 - 1 )\"))) == 7" | tee -a $out
  echo -e " compute(syard(split(\"10 + 12 * 33\"))) == 406" | tee -a $out
  echo -e " compute(syard(split(\"( 5 + 7 ) * 2\"))) == 24" | tee -a $out
  echo -e " compute(syard(split(\"5 + 7 / 2\"))) == 8" | tee -a $out
  echo -e " compute(syard(split(\"5 * 7 / 2\"))) == 17" | tee -a $out
  echo -e " compute(syard(split(\"9 + 24 / ( 7 - 3 )\"))) == 15" | tee -a $out
  
  if (scala_assert "postfix.scala" "postfix_test2.scala")
  then
     echo -e "  --> success (+ 1 Mark)\n" | tee -a $out
     marks=$(( marks + 1.0 ))
  else
     echo -e "  --> ONE OF THE TESTS FAILED\n" | tee -a $out
  fi
fi



### postfix2 tests

echo -e "Below is the feedback for your submission postfix2.scala" >> $out
echo -e "" >> $out

# compilation test

echo -e "postfix2.scala runs?" | tee -a $out

if (scala_compile postfix2.scala)
then
   echo -e "  --> success\n" | tee -a $out
   tsts=$(( 0 ))
else
   echo -e "  --> SCALA DID NOT RUN postfix2.scala\n" | tee -a $out
   tsts=$(( 1 )) 
fi


# var, return, ListBuffer test
#
if  [ $tsts -eq 0 ]
then     
  echo -e "postfix2.scala does not contain VARS, RETURNS etc?" | tee -a $out

  if (scala_vars postfix2.scala)
  then
    echo -e "  --> FAIL\n" | tee -a $out  
    tsts=$(( 1 ))
  else
    echo -e "  --> success\n" | tee -a $out  
    tsts=$(( 0 )) 
  fi
else
  tsts=$(( 1 ))  
fi




if [ $tsts -eq 0 ]
then
  echo -e " syard(split(\"3 + 4 * ( 2 - 1 )\")) == List(\"3\", \"4\", \"2\", \"1\", \"-\", \"*\", \"+\")" | tee -a $out
  echo -e " syard(split(\"( ( ( 3 ) ) + ( ( 4 + ( 5 ) ) ) )\")) == List(\"3\", \"4\", \"5\", \"+\", \"+\")" | tee -a $out
  echo -e " syard(split(\"5 + 7 / 2\")) == List(\"5\", \"7\", \"2\", \"/\", \"+\")" | tee -a $out
  echo -e " syard(split(\"5 * 7 / 2\")) == List(\"5\", \"7\", \"*\", \"2\", \"/\")" | tee -a $out
  echo -e " syard(split(\"3 + 4 * 8 / ( 5 - 1 ) ^ 2 ^ 3\")) == " | tee -a $out
  echo -e "         List(\"3\", \"4\", \"8\", \"*\", \"5\", \"1\", \"-\", \"2\", \"3\", \"^\", \"^\", \"/\", \"+\")" | tee -a $out
  
  if (scala_assert "postfix2.scala" "postfix_test3.scala")
  then
      echo -e "  --> success (+ 0.5 Marks)\n" | tee -a $out
      marks=$(( marks + 0.5 ))
  else
      echo -e "  --> ONE OF THE TESTS FAILED\n" | tee -a $out
  fi
fi

if [ $tsts -eq 0 ]
then
  echo -e " compute(syard(split(\"3 + 4 * ( 2 - 1 )\"))) == 7" | tee -a $out
  echo -e " compute(syard(split(\"10 + 12 * 33\"))) == 406" | tee -a $out
  echo -e " compute(syard(split(\"( 5 + 7 ) * 2\"))) == 24" | tee -a $out
  echo -e " compute(syard(split(\"5 + 7 / 2\"))) == 8" | tee -a $out
  echo -e " compute(syard(split(\"5 * 7 / 2\"))) == 17" | tee -a $out
  echo -e " compute(syard(split(\"9 + 24 / ( 7 - 3 )\"))) == 15" | tee -a $out
  echo -e " compute(syard(split(\"4 ^ 3 ^ 2\"))) == 262144" | tee -a $out
  echo -e " compute(syard(split(\"4 ^ ( 3 ^ 2 )\"))) == 262144" | tee -a $out
  echo -e " compute(syard(split(\"( 4 ^ 3 ) ^ 2\"))) == 4096" | tee -a $out
  echo -e " compute(syard(split(\"( 3 + 1 ) ^ 2 ^ 3\"))) == 65536" | tee -a $out
  
  if (scala_assert "postfix2.scala" "postfix_test4.scala")
  then
      echo -e "  --> success (+ 0.5 Marks)\n" | tee -a $out
      marks=$(( marks + 0.5 ))
  else
      echo -e "  --> ONE OF THE TESTS FAILED\n" | tee -a $out
  fi
fi

## final marks
echo -e "Overall mark for the Core Part 3 (Scala)" | tee -a $out
printf " %0.1f\n" $marks | tee -a $out

