testing2/knight1_test.sh
changeset 145 d306102fd33b
child 152 114a89518aea
equal deleted inserted replaced
144:716042628398 145:d306102fd33b
       
     1 #!/bin/bash
       
     2 set -e
       
     3 
       
     4 out=${1:-output}
       
     5 
       
     6 echo "" > $out
       
     7 
       
     8 echo "Below is the feedback for your submission of CW 7, Part 1 and 2." >> $out
       
     9 echo "" >> $out
       
    10 
       
    11 
       
    12 # compilation tests
       
    13 
       
    14 function scala_compile {
       
    15   (ulimit -t 30 -m 1024000 ; scala "$1" 2>> $out 1>> $out) 
       
    16 }
       
    17 
       
    18 # functional tests
       
    19 
       
    20 function scala_assert {
       
    21   (ulimit -t 300 -m 1024000 ; scala -i "$1" "$2" -e "" 2> /dev/null 1> /dev/null)
       
    22 }
       
    23 
       
    24 # purity test
       
    25 
       
    26 function scala_vars {
       
    27    (egrep '\bvar\b|\breturn\b|\.par|ListBuffer|mutable' "$1" 2> /dev/null 1> /dev/null)
       
    28 }
       
    29 
       
    30 
       
    31 # knights1: purity test
       
    32 
       
    33 echo "knight1.scala does not contain vars, returns etc?" >> $out
       
    34 
       
    35 if (scala_vars knight1.scala)
       
    36 then
       
    37   echo "  --> fail" >> $out
       
    38   tsts0=$(( 1 ))
       
    39 else
       
    40   echo "  --> success" >> $out
       
    41   tsts0=$(( 0 )) 
       
    42 fi
       
    43 
       
    44 
       
    45 # compilation test
       
    46 
       
    47 if [ $tsts0 -eq 0 ]
       
    48 then    
       
    49   echo "knight1.scala runs?" >> $out
       
    50 
       
    51   if (scala_compile knight1.scala)
       
    52   then
       
    53     echo "  --> success " >> $out
       
    54     tsts1=$(( 0 ))
       
    55   else
       
    56     echo "  --> scala did not run knight1.scala" >> $out
       
    57     tsts1=$(( 1 )) 
       
    58   fi
       
    59 else
       
    60   tsts1=$(( 1 ))   
       
    61 fi
       
    62 
       
    63 ### knight1a test
       
    64 
       
    65 if [ $tsts1 -eq 0 ]
       
    66 then
       
    67     echo " is_legal(8, Nil) (3, 4) == true " >> $out
       
    68     echo " is_legal(8, List((4, 1), (1, 0))) (4, 1) == false " >> $out
       
    69     echo " is_legal(2, Nil) (0, 0) == true" >> $out                          
       
    70 
       
    71     if (scala_assert "knight1.scala" "knight1a_test.scala")
       
    72     then
       
    73         echo "  --> success" >> $out
       
    74     else
       
    75         echo "  --> test failed" >> $out
       
    76     fi
       
    77 fi
       
    78 
       
    79 ### knight1b test
       
    80 
       
    81 if [ $tsts1 -eq 0 ]
       
    82 then
       
    83   echo " legal_moves(8, Nil, (2,2)) == List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4))" >> $out
       
    84   echo " legal_moves(8, Nil, (7,7)) == List((6,5), (5,6))" >> $out
       
    85   echo " legal_moves(8, List((4,1), (1,0)), (2,2)) == List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4))" >> $out
       
    86   echo " legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6))" >> $out
       
    87   echo " legal_moves(1, Nil, (0,0)) == Nil" >> $out
       
    88   echo " legal_moves(2, Nil, (0,0)) == Nil" >> $out
       
    89   echo " legal_moves(3, Nil, (0,0)) == List((1,2), (2,1))" >> $out
       
    90   
       
    91   if (scala_assert "knight1.scala" "knight1b_test.scala")
       
    92   then
       
    93     echo "  --> success" >> $out
       
    94   else
       
    95     echo "  --> test failed" >> $out
       
    96   fi
       
    97 fi
       
    98 
       
    99 
       
   100 ### knight1c test
       
   101 
       
   102 if [ $tsts1 -eq 0 ]
       
   103 then
       
   104   echo " all_tours from every position on the board" >> $out
       
   105   echo " dim = 1: 1" >> $out
       
   106   echo "       2: 0,0,0,0" >>  $out
       
   107   echo "       3: 0,0,0,0,0,0,0,0,0" >>  $out
       
   108   echo "       4: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0" >> $out
       
   109   echo "       5: 304,0,56,0,304,0,56,0,56,0,56,0,64,0,56,0,56,0,56,0,304,0,56,0,304" >> $out
       
   110   echo " enum_tours(5, List((0,2)) ) == 56 and all correct?" >> $out
       
   111   
       
   112   if (scala_assert "knight1.scala" "knight1c_test.scala") 
       
   113   then
       
   114     echo "  --> success" >> $out
       
   115   else
       
   116     echo "  --> test failed" >> $out
       
   117   fi
       
   118 fi
       
   119 
       
   120 
       
   121 # knights2: var test
       
   122 
       
   123 echo "knight2.scala does not contain vars, returns etc?" >> $out
       
   124 
       
   125 if (scala_vars knight2.scala)
       
   126 then
       
   127   echo "  --> fail" >> $out
       
   128   tsts0=$(( 1 ))
       
   129 else
       
   130   echo "  --> success" >> $out
       
   131   tsts0=$(( 0 )) 
       
   132 fi
       
   133 
       
   134 
       
   135 # compilation test
       
   136 if  [ $tsts0 -eq 0 ]
       
   137 then    
       
   138   echo "knight2.scala runs?" >> $out
       
   139 
       
   140   if (scala_compile knight2.scala)
       
   141   then
       
   142     echo "  --> success" >> $out
       
   143     tsts1=$(( 0 ))
       
   144   else
       
   145     echo "  --> scala did not run knight2.scala" >> $out
       
   146     tsts1=$(( 1 )) 
       
   147   fi
       
   148 else
       
   149   tsts1=$(( 1 ))     
       
   150 fi
       
   151 
       
   152 ### knight2a test
       
   153 
       
   154 if [ $tsts1 -eq 0 ]
       
   155 then
       
   156   echo " Let f = (x:(Int, Int)) => if (x._1 > 3) Some(List(x)) else None " >> $out
       
   157   echo "   first(List((1,0),(2,0),(3,0),(4,0)), f) == Some(List((4,0)))" >> $out
       
   158   echo "   first(List((1,0),(2,0),(3,0)), f) == None" >> $out
       
   159 
       
   160   if (scala_assert "knight2.scala" "knight2a_test.scala") 
       
   161   then
       
   162     echo "  --> success" >> $out
       
   163   else
       
   164     echo "  --> test failed" >> $out
       
   165   fi
       
   166 fi
       
   167 
       
   168 
       
   169 ### knight2b test
       
   170 
       
   171 if [ $tsts1 -eq 0 ]
       
   172 then
       
   173   echo " is first_tour(8, (0, 0)) ok? " >> $out
       
   174   echo " is first_tour(4, (0, 0)) == None " >> $out
       
   175 
       
   176   if (scala_assert "knight2.scala" "knight2b_test.scala") 
       
   177   then
       
   178     echo "  --> success" >> $out
       
   179   else
       
   180     echo "  --> test failed" >> $out
       
   181   fi
       
   182 fi
       
   183