core_testing1/collatz_test.sh
changeset 399 b17a98b0c52f
parent 376 6cc36d0ef79e
child 472 6a77c260c8a5
equal deleted inserted replaced
398:7d9b765d4012 399:b17a98b0c52f
       
     1 #!/bin/bash
       
     2 
       
     3 # to make the script fail safely
       
     4 set -euo pipefail
       
     5 
       
     6 
       
     7 out=${1:-output}
       
     8 
       
     9 echo -e "" > $out
       
    10 
       
    11 echo -e "Below is the feedback for your submission collatz.scala" >> $out
       
    12 echo -e "" >> $out
       
    13 
       
    14 
       
    15 # compilation tests
       
    16 
       
    17 function scala_compile {
       
    18   (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -Xprint:parser "$1" 2> c$out 1> c$out)
       
    19 }
       
    20 
       
    21 # functional tests
       
    22 
       
    23 function scala_assert {
       
    24   (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -nc -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\.|\.par |ListBuffer|AtomicInteger|mutable|util.control|new Array' c$out 2> /dev/null 1> /dev/null)
       
    31 }
       
    32 
       
    33 
       
    34 ### compilation test
       
    35 
       
    36 echo -e "collatz.scala runs?" >> $out
       
    37 
       
    38 if (scala_compile collatz.scala)
       
    39 then
       
    40     echo -e "  --> success" >> $out
       
    41     tsts=$(( 0 ))
       
    42 else
       
    43     echo -e "  --> SCALA DID NOT RUN collatz.scala\n" >> $out
       
    44     tsts=$(( 1 )) 
       
    45 fi
       
    46 
       
    47 
       
    48 
       
    49 # var, .par return, ListBuffer test
       
    50 #
       
    51 
       
    52 if  [ $tsts -eq 0 ]
       
    53 then
       
    54    echo -e "collatz.scala does not contain VARS, RETURNS etc?" >> $out
       
    55     
       
    56    if (scala_vars collatz.scala)
       
    57    then
       
    58       echo -e "  --> FAIL (make triple-sure your program conforms to the required format)\n" >> $out
       
    59       tsts=$(( 1 ))
       
    60    else
       
    61       echo -e "  --> success" >> $out
       
    62       tsts=$(( 0 )) 
       
    63    fi
       
    64 fi    
       
    65 
       
    66 
       
    67 ### collatz tests
       
    68 
       
    69 if [ $tsts -eq 0 ]
       
    70 then
       
    71   echo -e "collatz.scala tests:" >> $out
       
    72   echo -e "  collatz(1) == 0" >> $out
       
    73   echo -e "  collatz(6) == 8" >> $out
       
    74   echo -e "  collatz(9) == 19" >> $out
       
    75 
       
    76   if (scala_assert "collatz.scala" "collatz_test1.scala")
       
    77   then
       
    78     echo -e "  --> success" >> $out
       
    79   else
       
    80     echo -e "  --> ONE OF THE TESTS FAILED\n" >> $out
       
    81   fi
       
    82 fi
       
    83 
       
    84 ### collatz-max tests
       
    85 
       
    86 if [ $tsts -eq 0 ]
       
    87 then
       
    88   echo -e "  collatz_max(10) == (19, 9)" >> $out
       
    89   echo -e "  collatz_max(100) == (118, 97)" >> $out
       
    90   echo -e "  collatz_max(1000) == (178, 871)" >> $out
       
    91   echo -e "  collatz_max(10000) == (261, 6171)" >> $out
       
    92   echo -e "  collatz_max(100000) == (350, 77031)" >> $out
       
    93   echo -e "  collatz_max(1000000) == (524, 837799)" >> $out
       
    94 
       
    95   if (scala_assert "collatz.scala" "collatz_test2.scala") 
       
    96   then
       
    97     echo -e "  --> success" >> $out
       
    98   else
       
    99     echo -e "  --> ONE OF THE TESTS FAILED\n" >> $out
       
   100   fi
       
   101 fi
       
   102 
       
   103 
       
   104 ### last-odd tests
       
   105 
       
   106 if [ $tsts -eq 0 ]
       
   107 then
       
   108   echo -e "  last_odd(113) == 85" >> $out
       
   109   echo -e "  last_odd(84) == 21" >> $out
       
   110   echo -e "  last_odd(605) == 341" >> $out
       
   111 
       
   112   if (scala_assert "collatz.scala" "collatz_test3.scala") 
       
   113   then
       
   114     echo -e "  --> success" >> $out
       
   115   else
       
   116     echo -e "  --> ONE OF THE TESTS FAILED\n" >> $out
       
   117   fi
       
   118 fi