| 
     1 #!/bin/bash  | 
         | 
     2   | 
         | 
     3 # to make the script fail safely  | 
         | 
     4 set -euo pipefail  | 
         | 
     5   | 
         | 
     6   | 
         | 
     7 out=${1:-output} | 
         | 
     8   | 
         | 
     9 echo "" > $out  | 
         | 
    10   | 
         | 
    11 echo "Below is the feedback for your submission docdiff.scala" >> $out  | 
         | 
    12 echo "" >> $out  | 
         | 
    13   | 
         | 
    14   | 
         | 
    15 # compilation tests  | 
         | 
    16   | 
         | 
    17 function scala_compile { | 
         | 
    18   (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala "$1" 2>> $out 1>> $out)   | 
         | 
    19 }  | 
         | 
    20   | 
         | 
    21 # functional tests  | 
         | 
    22   | 
         | 
    23 function scala_assert { | 
         | 
    24   (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -i "$1" "$2" -e "" 2> /dev/null 1> /dev/null)  | 
         | 
    25 }  | 
         | 
    26   | 
         | 
    27 # purity test  | 
         | 
    28   | 
         | 
    29 function scala_vars { | 
         | 
    30    (egrep '\bvar\b|\breturn\b|\.par|ListBuffer|mutable' "$1" 2> /dev/null 1> /dev/null)  | 
         | 
    31 }  | 
         | 
    32   | 
         | 
    33   | 
         | 
    34 # var, .par return, ListBuffer test  | 
         | 
    35 #  | 
         | 
    36 echo "docdiff.scala does not contain vars, returns etc?" >> $out  | 
         | 
    37   | 
         | 
    38 if (scala_vars docdiff.scala)  | 
         | 
    39 then  | 
         | 
    40   echo "  --> fail (make triple-sure your program conforms to the required format)" >> $out  | 
         | 
    41   tsts0=$(( 0 ))  | 
         | 
    42 else  | 
         | 
    43   echo "  --> success" >> $out  | 
         | 
    44   tsts0=$(( 0 ))   | 
         | 
    45 fi  | 
         | 
    46   | 
         | 
    47 ### compilation test  | 
         | 
    48   | 
         | 
    49   | 
         | 
    50 if  [ $tsts0 -eq 0 ]  | 
         | 
    51 then   | 
         | 
    52   echo "docdiff.scala runs?" >> $out  | 
         | 
    53   | 
         | 
    54   if (scala_compile docdiff.scala)  | 
         | 
    55   then  | 
         | 
    56     echo "  --> success" >> $out  | 
         | 
    57     tsts=$(( 0 ))  | 
         | 
    58   else  | 
         | 
    59     echo "  --> scala did not run docdiff.scala" >> $out  | 
         | 
    60     tsts=$(( 1 ))   | 
         | 
    61   fi  | 
         | 
    62 else  | 
         | 
    63   tsts=$(( 1 ))       | 
         | 
    64 fi  | 
         | 
    65   | 
         | 
    66 ### docdiff clean tests  | 
         | 
    67   | 
         | 
    68 if [ $tsts -eq 0 ]  | 
         | 
    69 then  | 
         | 
    70   echo "docdiff.scala tests:" >> $out  | 
         | 
    71   echo "  clean(\"ab a abc\") == List(\"ab\", \"a\", \"abc\")" >> $out  | 
         | 
    72   echo "  clean(\"ab*a abc1\") == List(\"ab\", \"a\", \"abc1\")" >> $out  | 
         | 
    73   | 
         | 
    74   if (scala_assert "docdiff.scala" "docdiff_test1.scala")  | 
         | 
    75   then  | 
         | 
    76     echo "  --> success" >> $out  | 
         | 
    77   else  | 
         | 
    78     echo "  --> one of the tests failed" >> $out  | 
         | 
    79   fi  | 
         | 
    80 fi  | 
         | 
    81   | 
         | 
    82 ### docdiff occurrences tests  | 
         | 
    83   | 
         | 
    84 if [ $tsts -eq 0 ]  | 
         | 
    85 then  | 
         | 
    86   echo "  occurrences(List(\"a\", \"b\", \"b\", \"c\", \"d\")) == " >> $out  | 
         | 
    87   echo "      Map(\"a\" -> 1, \"b\" -> 2, \"c\" -> 1, \"d\" -> 1)" >> $out  | 
         | 
    88   echo "  " >> $out  | 
         | 
    89   echo "  occurrences(List(\"d\", \"b\", \"d\", \"b\", \"d\")) == " >> $out  | 
         | 
    90   echo "      Map(\"d\" -> 3, \"b\" -> 2)" >> $out  | 
         | 
    91   | 
         | 
    92   if (scala_assert "docdiff.scala" "docdiff_test2.scala")   | 
         | 
    93   then  | 
         | 
    94     echo "  --> success" >> $out  | 
         | 
    95   else  | 
         | 
    96     echo "  --> one of the tests failed" >> $out  | 
         | 
    97   fi  | 
         | 
    98 fi  | 
         | 
    99   | 
         | 
   100 ### docdiff prod tests  | 
         | 
   101   | 
         | 
   102 if [ $tsts -eq 0 ]  | 
         | 
   103 then  | 
         | 
   104   echo "  val l1 = List(\"a\", \"b\", \"b\", \"c\", \"d\")" >> $out  | 
         | 
   105   echo "  val l2 = List(\"d\", \"b\", \"d\", \"b\", \"d\")" >> $out  | 
         | 
   106   echo "  " >> $out  | 
         | 
   107   echo "  prod(l1, l2) == 7 " >> $out  | 
         | 
   108   echo "  prod(l1, l1) == 7 " >> $out  | 
         | 
   109   echo "  prod(l2, l2) == 13 " >> $out  | 
         | 
   110   | 
         | 
   111   if (scala_assert "docdiff.scala" "docdiff_test3.scala")   | 
         | 
   112   then  | 
         | 
   113     echo "  --> success" >> $out  | 
         | 
   114   else  | 
         | 
   115     echo "  --> one of the tests failed" >> $out  | 
         | 
   116   fi  | 
         | 
   117 fi  | 
         | 
   118   | 
         | 
   119 ### docdiff overlap tests  | 
         | 
   120   | 
         | 
   121 if [ $tsts -eq 0 ]  | 
         | 
   122 then  | 
         | 
   123   echo "  val l1 = List(\"a\", \"b\", \"b\", \"c\", \"d\")" >> $out  | 
         | 
   124   echo "  val l2 = List(\"d\", \"b\", \"d\", \"b\", \"d\")" >> $out  | 
         | 
   125   echo "  " >> $out  | 
         | 
   126   echo "  overlap(l1, l2) == 0.5384615384615384 " >> $out  | 
         | 
   127   echo "  overlap(l1, l1) == 1.0 " >> $out  | 
         | 
   128   echo "  overlap(l2, l2) == 1.0 " >> $out  | 
         | 
   129   | 
         | 
   130   if (scala_assert "docdiff.scala" "docdiff_test4.scala")   | 
         | 
   131   then  | 
         | 
   132     echo "  --> success" >> $out  | 
         | 
   133   else  | 
         | 
   134     echo "  --> one of the tests failed" >> $out  | 
         | 
   135   fi  | 
         | 
   136 fi  |