#!/bin/bash

# try this to make it fail safely
set -eu -o pipefail -posix
#set -e

out=${1:-output}

echo "" > $out

echo "Below is the feedback and provisional mark for your submission" >> $out
echo "for assignment 6.  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


#alarm() { perl -e 'alarm shift; exec @ARGV' "$@"; }
#alarm 20 foo arg1

# 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)
}


# marks for CW1 parts 1 + 2
marks=$(( 0 ))


# compilation test
echo "collatz.scala runs?" | tee -a $out

if (scala_compile collatz.scala)
then
  echo "  --> success" | tee -a $out
  tsts=$(( 0 ))
else
  echo "  --> scala did not run collatz.scala" | tee -a $out
  tsts=$(( 1 )) 
fi


### collatz tests

if [ $tsts -eq 0 ]
then
  echo "  collatz(1) == 1" | tee -a $out
  echo "  collatz(2) == 2" | tee -a $out
  echo "  collatz(9) == 20" | tee -a $out
  echo "  collatz(9000) == 48" | tee -a $out

  if (scala_assert "collatz.scala" "../collatz_test1.scala")
  then
    echo "  --> success" | tee -a $out
    marks=$(( marks + 2 ))
  else
    echo "  --> one of the tests failed" | tee -a $out
  fi
fi

### collatz max tests

if [ $tsts -eq 0 ]
then
  echo "  collatz_max(2) == (2, 2)" | tee -a $out
  echo "  collatz_max(77000) == (340, 52527)" | tee -a $out

  if (scala_assert "collatz.scala" "../collatz_test2.scala") 
  then
    echo "  --> success" | tee -a $out
    marks=$(( marks + 1 ))
  else
    echo "  --> one of the tests failed" | tee -a $out
  fi
fi





# compilation test

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

if (scala_compile trade.scala)
then
  echo "  --> success" | tee -a $out
  tsts=$(( 0 ))
else
  echo "  --> scala did not run trade.scala" | tee -a $out
  tsts=$(( 1 ))
fi


### trade times tests

if [ $tsts -eq 0 ]
then
  echo "  trade_times(List(3.0, 7.0, 2.0, 4.0)) == (2, 3)" | tee -a $out
  echo "  trade_times(List(28.0, 18.0, 20.0, 26.0, 24.0)) == (1, 3)" | tee -a $out

  if (scala_assert "trade.scala" "../trade_test1.scala") 
  then
    echo "  --> success" | tee -a $out
    marks=$(( marks + 1 ))
  else
    echo "  --> one of the tests failed" | tee -a $out
  fi
fi

if [ $tsts -eq 0 ]
then
  echo "  get_page(\"GOOG\").length >= 3088" | tee -a $out
  echo "  get_page(\"AAPL\").length >= 9065" | tee -a $out
  echo "  get_page(\"FB\").length >= 1136" | tee -a $out

  if (scala_assert "trade.scala" "../trade_test2.scala") 
  then
    echo "  --> success" | tee -a $out
    marks=$(( marks + 1 ))
  else
    echo "  --> one of the tests failed" | tee -a $out
  fi
fi

if [ $tsts -eq 0 ]
then
  echo "  get_page(\"IBM\").last or head == \"1962-01-02,578.499985,578.499985,571.999991,571.999991,387200,2.260487\")" | tee -a $out
  echo "  process_page(\"IBM\").length == get_page(\"IBM\").length - 1" | tee -a $out

  if (scala_assert "trade.scala" "../trade_test3.scala") 
  then
    echo "  --> success" | tee -a $out
    marks=$(( marks + 1 ))
  else
    echo "  --> one of the tests failed" | tee -a $out
  fi
fi

if [ $tsts -eq 0 ]
then
  echo "  query_company(\"YHOO\") == (\"1996-07-24\", \"2000-01-03\")" | tee -a $out
  echo "  query_company(\"IBM\") == (\"1962-06-14\", \"2013-03-14\")" | tee -a $out
  echo "  query_company(\"BIDU\") == (\"2006-02-07\", \"2014-11-11\")" | tee -a $out

 if (scala_assert "trade.scala" "../trade_test4.scala") 
  then
    echo "  --> success" | tee -a $out
    marks=$(( marks + 1 ))
  else
    echo "  --> one of the tests failed" | tee -a $out
  fi
fi


## final marks
echo "Overall mark for Parts 1 and 2:" | tee -a $out
echo "$marks" | tee -a $out
