testing1/collatz_test.sh
changeset 144 716042628398
parent 130 7f3f01dfe738
child 199 54befaf23648
equal deleted inserted replaced
143:11396c17cd8b 144:716042628398
       
     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 collatz.scala" >> $out
       
    12 echo "" >> $out
       
    13 
       
    14 
       
    15 # compilation tests
       
    16 
       
    17 function scala_compile {
       
    18   (ulimit -t 30 -m 1024000 ; scala "$1" 2>> $out 1>> $out) 
       
    19 }
       
    20 
       
    21 # functional tests
       
    22 
       
    23 function scala_assert {
       
    24   (ulimit -t 30 -m 1024000 ; 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 "collatz.scala does not contain vars, returns etc?" >> $out
       
    37 
       
    38 if (scala_vars collatz.scala)
       
    39 then
       
    40   echo "  --> fail" >> $out
       
    41   tsts0=$(( 1 ))
       
    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 "collatz.scala runs?" >> $out
       
    53 
       
    54   if (scala_compile collatz.scala)
       
    55   then
       
    56     echo "  --> success" >> $out
       
    57     tsts=$(( 0 ))
       
    58   else
       
    59     echo "  --> scala did not run collatz.scala" >> $out
       
    60     tsts=$(( 1 )) 
       
    61   fi
       
    62 else
       
    63   tsts=$(( 1 ))     
       
    64 fi
       
    65 
       
    66 ### collatz tests
       
    67 
       
    68 if [ $tsts -eq 0 ]
       
    69 then
       
    70   echo "collatz.scala tests:" >> $out
       
    71   echo "  collatz(1) == 0,1 or 4" >> $out
       
    72   echo "  collatz(6) == 9" >> $out
       
    73   echo "  collatz(9) == 20" >> $out
       
    74 
       
    75   if (scala_assert "collatz.scala" "collatz_test1.scala")
       
    76   then
       
    77     echo "  --> success" >> $out
       
    78   else
       
    79     echo "  --> one of the tests failed" >> $out
       
    80   fi
       
    81 fi
       
    82 
       
    83 ### collatz-max tests
       
    84 
       
    85 if [ $tsts -eq 0 ]
       
    86 then
       
    87   echo "  collatz_max(10) == (20, 9)" >> $out
       
    88   echo "  collatz_max(100) == (119, 97)" >> $out
       
    89   echo "  collatz_max(1000) == (179, 871)" >> $out
       
    90   echo "  collatz_max(10000) == (262, 6171)" >> $out
       
    91   echo "  collatz_max(100000) == (351, 77031)" >> $out
       
    92   echo "  collatz_max(1000000) == (525, 837799)" >> $out
       
    93 
       
    94   if (scala_assert "collatz.scala" "collatz_test2.scala") 
       
    95   then
       
    96     echo "  --> success" >> $out
       
    97   else
       
    98     echo "  --> one of the tests failed" >> $out
       
    99   fi
       
   100 fi
       
   101 
       
   102