| 243 |      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 and provisional marks for your submission" >> $out
 | 
| 331 |     12 | echo "for core assignment 8.  Please note all marks are provisional until" >> $out
 | 
| 243 |     13 | echo "ratified by the assessment board -- this is not an official" >> $out
 | 
|  |     14 | echo "results transcript." >> $out
 | 
|  |     15 | echo "" >> $out
 | 
|  |     16 | 
 | 
| 331 |     17 | # marks for core CW8
 | 
| 243 |     18 | marks=$(( 0 ))
 | 
|  |     19 | 
 | 
|  |     20 | # compilation tests
 | 
|  |     21 | 
 | 
|  |     22 | function scala_compile {
 | 
| 332 |     23 |     (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala "$1" 2>> $out 1>> $out)
 | 
| 243 |     24 | }
 | 
|  |     25 | 
 | 
|  |     26 | # functional tests
 | 
|  |     27 | 
 | 
|  |     28 | function scala_assert {
 | 
| 332 |     29 |     (ulimit -t 40; JAVA_OPTS="-Xmx1g" scala -i "$1" -- "$2" 2> /dev/null 1> /dev/null)
 | 
| 243 |     30 | }
 | 
|  |     31 | 
 | 
| 258 |     32 | function scala_assert_long {
 | 
| 332 |     33 |   (ulimit -t 60; JAVA_OPTS="-Xmx1g" scala -i "$1" -- "$2" 2> /dev/null 1> /dev/null)
 | 
| 258 |     34 | }
 | 
|  |     35 | 
 | 
| 331 |     36 | #function scala_assert_elong {
 | 
| 332 |     37 | #  (ulimit -t 90; JAVA_OPTS="-Xmx1g" scala -i "$1" -- "$2" 2> /dev/null 1> /dev/null)
 | 
| 331 |     38 | #}
 | 
| 243 |     39 | 
 | 
|  |     40 | # purity test
 | 
|  |     41 | 
 | 
|  |     42 | function scala_vars {
 | 
|  |     43 |    (egrep '\bvar\b|\breturn\b|\.par|ListBuffer|mutable|new Array' "$1" 2> /dev/null 1> /dev/null)
 | 
|  |     44 | }
 | 
|  |     45 | 
 | 
| 331 |     46 | # knights1: purity test
 | 
|  |     47 | 
 | 
|  |     48 | echo -e "knight1.scala does not contain vars, returns,  Arrays, ListBuffers etc?" | tee -a $out
 | 
|  |     49 | 
 | 
|  |     50 | if (scala_vars knight1.scala)
 | 
|  |     51 | then
 | 
|  |     52 |   echo -e "  --> TEST FAILED\n" | tee -a $out
 | 
|  |     53 |   tsts0=$(( 1 ))
 | 
|  |     54 | else
 | 
|  |     55 |   echo -e "  --> success" | tee -a $out
 | 
|  |     56 |   tsts0=$(( 0 )) 
 | 
|  |     57 | fi
 | 
|  |     58 | 
 | 
|  |     59 | 
 | 
|  |     60 | # compilation test
 | 
|  |     61 | 
 | 
|  |     62 | if [ $tsts0 -eq 0 ]
 | 
|  |     63 | then    
 | 
|  |     64 |   echo -e "knight1.scala runs?" | tee -a $out
 | 
|  |     65 | 
 | 
|  |     66 |   if (scala_compile knight1.scala)
 | 
|  |     67 |   then
 | 
|  |     68 |     echo -e "  --> success " | tee -a $out
 | 
|  |     69 |     tsts1=$(( 0 ))
 | 
|  |     70 |   else
 | 
|  |     71 |     echo -e "  --> SCALA DID NOT RUN knight1.scala\n" | tee -a $out
 | 
|  |     72 |     tsts1=$(( 1 )) 
 | 
|  |     73 |   fi
 | 
|  |     74 | else
 | 
|  |     75 |   tsts1=$(( 1 ))   
 | 
|  |     76 | fi
 | 
|  |     77 | 
 | 
|  |     78 | 
 | 
|  |     79 | ### knight4 test
 | 
|  |     80 | 
 | 
|  |     81 | if [ $tsts1 -eq 0 ]
 | 
|  |     82 | then
 | 
|  |     83 |   echo -e " Let f = (x:(Int, Int)) => if (x._1 > 3) Some(List(x)) else None " | tee -a $out
 | 
|  |     84 |   echo -e "   first(List((1,0),(2,0),(3,0),(4,0)), f) == Some(List((4,0)))" | tee -a $out
 | 
|  |     85 |   echo -e "   first(List((1,0),(2,0),(3,0)), f) == None" | tee -a $out  
 | 
|  |     86 |   
 | 
|  |     87 |   if (scala_assert "knight1.scala" "knight1_test4.scala") 
 | 
|  |     88 |   then
 | 
|  |     89 |       echo -e "  --> success" | tee -a $out
 | 
|  |     90 |       marks=$(( marks + 1 ))
 | 
|  |     91 |   else
 | 
|  |     92 |       echo -e "  --> TEST FAILED\n" | tee -a $out
 | 
|  |     93 |   fi
 | 
|  |     94 | fi
 | 
|  |     95 | 
 | 
|  |     96 | ### knight5 test
 | 
|  |     97 | 
 | 
|  |     98 | if [ $tsts1 -eq 0 ]
 | 
|  |     99 | then
 | 
|  |    100 |   echo -e " is first_tour(6, List((0, 0))) ok? " | tee -a $out
 | 
|  |    101 |   echo -e " is first_tour(4, List((0, 0))) == None " | tee -a $out
 | 
|  |    102 |   START=$(date +%s)
 | 
|  |    103 |   
 | 
|  |    104 |   if (scala_assert_long "knight1.scala" "knight1_test5.scala") 
 | 
|  |    105 |   then
 | 
|  |    106 |       END=$(date +%s)
 | 
|  |    107 |       DIFF=$(( $END - $START ))
 | 
|  |    108 |       echo "  It took $DIFF seconds" | tee -a $out  
 | 
|  |    109 |       echo -e "  --> success" | tee -a $out
 | 
|  |    110 |       marks=$(( marks + 1 ))
 | 
|  |    111 |   else
 | 
|  |    112 |       END=$(date +%s)
 | 
|  |    113 |       DIFF=$(( $END - $START ))
 | 
|  |    114 |       echo "  It took $DIFF seconds" | tee -a $out 
 | 
|  |    115 |       echo -e "  --> TEST FAILED\n" | tee -a $out
 | 
|  |    116 |   fi
 | 
|  |    117 | fi
 | 
|  |    118 | 
 | 
|  |    119 | 
 | 
| 243 |    120 | 
 | 
| 258 |    121 | # knights2: purity test
 | 
| 243 |    122 | #
 | 
| 258 |    123 | echo "knight2.scala does not contain vars, returns, Arrays, ListBuffers etc?" | tee -a $out
 | 
| 243 |    124 | 
 | 
|  |    125 | 
 | 
| 258 |    126 | if (scala_vars knight2.scala)
 | 
| 243 |    127 | then
 | 
| 331 |    128 |   echo -e "  --> TEST FAILED\n" | tee -a $out
 | 
| 243 |    129 |   tsts0=$(( 1 ))
 | 
|  |    130 | else
 | 
| 331 |    131 |   echo -e "  --> success" | tee -a $out
 | 
| 243 |    132 |   tsts0=$(( 0 )) 
 | 
|  |    133 | fi
 | 
|  |    134 | 
 | 
|  |    135 | 
 | 
|  |    136 | # compilation test
 | 
|  |    137 | if  [ $tsts0 -eq 0 ]
 | 
|  |    138 | then    
 | 
| 258 |    139 |   echo "knight2.scala runs?" | tee -a $out
 | 
| 243 |    140 | 
 | 
| 258 |    141 |   if (scala_compile knight2.scala)
 | 
| 243 |    142 |   then
 | 
| 331 |    143 |     echo -e "  --> success" | tee -a $out
 | 
| 243 |    144 |     tsts1=$(( 0 ))
 | 
|  |    145 |   else
 | 
| 331 |    146 |     echo -e "  --> SCALA DID NOT RUN knight2.scala\n" | tee -a $out
 | 
| 243 |    147 |     tsts1=$(( 1 )) 
 | 
|  |    148 |   fi
 | 
|  |    149 | else
 | 
|  |    150 |   tsts1=$(( 1 ))     
 | 
|  |    151 | fi
 | 
|  |    152 | 
 | 
|  |    153 | # ordered move test
 | 
|  |    154 | 
 | 
|  |    155 | if [ $tsts1 -eq 0 ]
 | 
|  |    156 | then
 | 
| 331 |    157 |   echo -e " ordered_moves(8, List((3,4), (3,2)), (1,3)) == List((0,1), (0,5), (2,1), (2,5))" | tee -a $out
 | 
|  |    158 |   echo -e " ordered_moves(8, List((4,0)), (0,0)) == List((2,1), (1,2))" | tee -a $out
 | 
|  |    159 |   echo -e " ordered_moves(8, List((0,4)), (0,0)) == List((1,2), (2,1))" | tee -a $out
 | 
| 243 |    160 |   
 | 
| 331 |    161 |   if (scala_assert "knight2.scala" "knight2_test6.scala")
 | 
| 243 |    162 |   then
 | 
| 331 |    163 |       echo -e "  --> success" | tee -a $out
 | 
| 243 |    164 |       marks=$(( marks + 1 ))
 | 
|  |    165 |   else
 | 
| 331 |    166 |       echo -e "  --> TEST FAILED\n" | tee -a $out
 | 
| 243 |    167 |   fi
 | 
|  |    168 | fi
 | 
|  |    169 | 
 | 
|  |    170 | 
 | 
|  |    171 | # first-closed-tour test
 | 
|  |    172 | 
 | 
|  |    173 | if [ $tsts1 -eq 0 ]
 | 
|  |    174 | then
 | 
| 331 |    175 |   echo -e " first_closed_tour_heuristics(6, List((3,3))) found and correct?" | tee -a $out
 | 
|  |    176 |   START=$(date +%s)
 | 
| 243 |    177 |   
 | 
| 331 |    178 |   if (scala_assert "knight2.scala" "knight2_test7.scala")
 | 
| 243 |    179 |   then
 | 
| 331 |    180 |       END=$(date +%s)
 | 
|  |    181 |       DIFF=$(( $END - $START ))
 | 
|  |    182 |       echo "  It took $DIFF seconds" | tee -a $out 
 | 
|  |    183 |       echo -e "  --> success" | tee -a $out
 | 
| 243 |    184 |       marks=$(( marks + 1 ))
 | 
|  |    185 |   else
 | 
| 331 |    186 |       END=$(date +%s)
 | 
|  |    187 |       DIFF=$(( $END - $START ))
 | 
|  |    188 |       echo "  It took $DIFF seconds" | tee -a $out 
 | 
|  |    189 |       echo -e "  --> TEST FAILED\n" | tee -a $out
 | 
| 243 |    190 |   fi
 | 
|  |    191 | fi
 | 
|  |    192 | 
 | 
|  |    193 | 
 | 
| 331 |    194 | # first-tour test
 | 
| 243 |    195 | 
 | 
|  |    196 | if [ $tsts1 -eq 0 ]
 | 
|  |    197 | then
 | 
| 331 |    198 |   echo -e " first_tour_heuristics(8, List((0,0))) found and correct?" | tee -a $out
 | 
|  |    199 |   echo -e " first_tour_heuristics(30, List((0,0))) found and correct?" | tee -a $out
 | 
|  |    200 |   START=$(date +%s)
 | 
| 243 |    201 |   
 | 
| 331 |    202 |   if (scala_assert_long "knight2.scala" "knight2_test8.scala")
 | 
| 243 |    203 |   then
 | 
| 331 |    204 |       END=$(date +%s)
 | 
|  |    205 |       DIFF=$(( $END - $START ))
 | 
|  |    206 |       echo "  It took $DIFF seconds" | tee -a $out
 | 
|  |    207 |       echo -e "  --> success" | tee -a $out
 | 
| 243 |    208 |       marks=$(( marks + 1 ))
 | 
|  |    209 |   else
 | 
| 331 |    210 |       END=$(date +%s)
 | 
|  |    211 |       DIFF=$(( $END - $START ))
 | 
|  |    212 |       echo "  It took $DIFF seconds" | tee -a $out
 | 
|  |    213 |       echo -e "  --> TEST FAILED\n" | tee -a $out
 | 
| 243 |    214 |   fi
 | 
|  |    215 | fi
 | 
|  |    216 | 
 | 
|  |    217 | 
 | 
| 258 |    218 | 
 | 
|  |    219 | # knights3: purity test
 | 
|  |    220 | #
 | 
|  |    221 | echo -e "knight3.scala does not contain vars, returns, Arrays, ListBuffers etc?" | tee -a $out
 | 
|  |    222 | 
 | 
|  |    223 | 
 | 
|  |    224 | if (scala_vars knight3.scala)
 | 
|  |    225 | then
 | 
| 331 |    226 |   echo "  --> TEST FAILED\n" | tee -a $out
 | 
| 259 |    227 |   tsts0=$(( 1 ))
 | 
| 258 |    228 | else
 | 
|  |    229 |   echo "  --> success" | tee -a $out
 | 
|  |    230 |   tsts0=$(( 0 )) 
 | 
|  |    231 | fi
 | 
|  |    232 | 
 | 
|  |    233 | # compilation test
 | 
|  |    234 | if  [ $tsts0 -eq 0 ]
 | 
|  |    235 | then    
 | 
|  |    236 |   echo "knight3.scala runs?" | tee -a $out
 | 
|  |    237 | 
 | 
|  |    238 |   if (scala_compile knight3.scala)
 | 
|  |    239 |   then
 | 
|  |    240 |     echo "  --> success" | tee -a $out
 | 
|  |    241 |     tsts1=$(( 0 ))
 | 
|  |    242 |   else
 | 
| 331 |    243 |     echo -e "  --> SCALA DID NOT RUN knight3.scala\n" | tee -a $out  
 | 
| 258 |    244 |     tsts1=$(( 1 )) 
 | 
|  |    245 |   fi
 | 
|  |    246 | else
 | 
|  |    247 |   tsts1=$(( 1 ))     
 | 
|  |    248 | fi
 | 
|  |    249 | 
 | 
|  |    250 | 
 | 
|  |    251 | if [ $tsts1 -eq 0 ]
 | 
|  |    252 | then
 | 
|  |    253 |   echo -e " tour_on_mega_board(70, List((0,0))) found and correct?" | tee -a $out
 | 
| 331 |    254 |   START=$(date +%s)
 | 
| 258 |    255 |   
 | 
| 332 |    256 |   if (scala_assert_long "knight3.scala" "knight3_test9.scala")
 | 
| 258 |    257 |   then
 | 
| 331 |    258 |       END=$(date +%s)
 | 
|  |    259 |       DIFF=$(( $END - $START ))
 | 
|  |    260 |       echo "  It took $DIFF seconds" | tee -a $out
 | 
| 258 |    261 |       echo -e "  --> success" | tee -a $out
 | 
|  |    262 |       marks=$(( marks + 1 ))
 | 
|  |    263 |   else
 | 
| 331 |    264 |       END=$(date +%s)
 | 
|  |    265 |       DIFF=$(( $END - $START ))
 | 
|  |    266 |       echo "  It took $DIFF seconds" | tee -a $out
 | 
| 332 |    267 |       echo -e "  --> TEST FAILED\n" | tee -a $out 
 | 
| 258 |    268 |   fi
 | 
|  |    269 | fi
 | 
|  |    270 | 
 | 
|  |    271 | 
 | 
| 243 |    272 | ## final marks
 | 
| 331 |    273 | echo -e "" >> $out
 | 
|  |    274 | echo -e "Overall mark for CW 8 Core Part" | tee -a $out
 | 
|  |    275 | echo -e "$marks" | tee -a $out
 |