#!/bin/bash
set -e

out=${1:-output}

echo "" > $out

echo "Below is the feedback for your submission for drumb.scala" >> $out
echo "" >> $out

# compilation tests

function scala_compile {
  (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala "$1" 2>> $out 1>> $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 {
   (egrep '\bvar\b|\breturn\b|\.par|ListBuffer|mutable' "$1" 2> /dev/null 1> /dev/null)
}


# var, .par return, ListBuffer test
#
echo "drumb.scala does not contain vars, returns etc?" >> $out

if (scala_vars drumb.scala)
then
  echo "  --> fail (make tripply-sure your program conforms to the required format)" >> $out
  tsts0=$(( 0 ))
else
  echo "  --> success" >> $out
  tsts0=$(( 0 )) 
fi


# compilation test
if  [ $tsts0 -eq 0 ]
then 
  echo "drumb.scala runs?" >> $out

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

### get january data

if [ $tsts -eq 0 ]
then
  echo "  get_january_data(\"GOOG\", 1980) == List()" >> $out
  echo "  get_january_data(\"GOOG\", 2010).head == \"2010-01-04,311.349976\"" >> $out

  if (scala_assert "drumb.scala" "drumb_test1.scala")
  then
    echo "  --> success" >> $out
  else
    echo "  --> test failed" >> $out
  fi
fi

### get first price

if [ $tsts -eq 0 ]
then
  echo "  get_first_price(\"GOOG\", 1980) == None" >> $out
  echo "  get_first_price(\"GOOG\", 2010) == Some(311.349976)" >> $out

  if (scala_assert "drumb.scala" "drumb_test2.scala")
  then
    echo "  --> success" >> $out
  else
    echo "  --> test failed" >> $out
  fi
fi

### get prices tests

if [ $tsts -eq 0 ]
then
  echo "  get_prices(List(\"GOOG\", \"AAPL\"), 2010 to 2012) ==" >> $out
  echo "       List(List(Some(311.349976), Some(20.544939))," >> $out
  echo "            List(Some(300.222351), Some(31.638695))," >> $out
  echo "            List(Some(330.555054), Some(39.478039)))" >> $out

  if (scala_assert "drumb.scala" "drumb_test3.scala")
  then
    echo "  --> success" >> $out
  else
    echo "  --> test failed" >> $out
  fi
fi

### get_deltas_test

#if [ $tsts -eq 0 ]
#then
#  echo "  get_deltas(get_prices(List(\"GOOG\", \"AAPL\"), 2010 to 2012)) == " >> $out
#  echo "    List(List(Some(-0.03573992567129673), Some(0.5399749442411563)), " >> $out
#  echo "         List(Some(0.10103412653643493), Some(0.2477771728154912)))" >> $out
#  
#  if (scala_assert "drumb.scala" "drumb_test2.scala") 
#  then
#    echo "  --> success" >> $out
#  else
#    echo "  --> test failed" >> $out
#  fi
#fi


### yield_tests, investment_test

#if [ $tsts -eq 0 ]
#then
#  echo "  yearly_yield(get_deltas(<<GOOG+AAPL 2010 - 2012>>), 100, 0) == 125" >> $out
#  echo "" >> $out
#  echo "  investment(rstate_portfolio, 1978 to 2017, 100) == 30895" >> $out
#  echo "  investment(bchips_portfolio, 1978 to 2017, 100) == 349597" >> $out
#  
#  if (scala_assert "drumb.scala" "drumb_test3.scala") 
#  then
#    echo "  --> success" >> $out
#  else
#    echo "  --> test failed" >> $out
#  fi
#fi

