| 168 |      1 | #!/bin/bash
 | 
|  |      2 | 
 | 
|  |      3 | # to make the script fail safely
 | 
|  |      4 | set -euo pipefail
 | 
|  |      5 | 
 | 
|  |      6 | out=${1:-output}
 | 
|  |      7 | 
 | 
| 288 |      8 | echo -e "" > $out
 | 
| 168 |      9 | 
 | 
|  |     10 | 
 | 
| 288 |     11 | echo -e "Below is the feedback and provisional marks for your submission" >> $out
 | 
|  |     12 | echo -e "for the core part of assignment 9.  Please note all marks are provisional until" >> $out
 | 
|  |     13 | echo -e "ratified by the assessment board -- this is not an official" >> $out
 | 
|  |     14 | echo -e "results transcript." >> $out
 | 
|  |     15 | echo -e "" >> $out
 | 
| 168 |     16 | 
 | 
| 288 |     17 | # marks for CW9
 | 
| 168 |     18 | marks=$(( 0 ))
 | 
|  |     19 | 
 | 
|  |     20 | # compilation tests
 | 
|  |     21 | 
 | 
|  |     22 | function scala_compile {
 | 
| 288 |     23 |     (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala "$1" 2> /dev/null 1> /dev/null)
 | 
| 168 |     24 | }
 | 
|  |     25 | 
 | 
|  |     26 | # functional tests
 | 
|  |     27 | 
 | 
|  |     28 | function scala_assert {
 | 
| 288 |     29 |     (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -i "$1" -- "$2" 2> /dev/null 1> /dev/null)
 | 
| 245 |     30 | }
 | 
|  |     31 | 
 | 
|  |     32 | function scala_assert_long {
 | 
| 288 |     33 |     (ulimit -t 60; JAVA_OPTS="-Xmx1g" scala -i "$1" -- "$2" 2> /dev/null 1> /dev/null)
 | 
| 168 |     34 | }
 | 
|  |     35 | 
 | 
|  |     36 | # purity test
 | 
|  |     37 | 
 | 
|  |     38 | function scala_vars {
 | 
|  |     39 |    (egrep '\bvar\b|\breturn\b|\.par|ListBuffer|mutable|new Array' "$1" 2> /dev/null 1> /dev/null)
 | 
|  |     40 | }
 | 
|  |     41 | 
 | 
|  |     42 | 
 | 
|  |     43 | # var, return, ListBuffer test
 | 
|  |     44 | #
 | 
| 288 |     45 | echo -e "re.scala does not contain vars, returns, Arrays, ListBuffers etc?" | tee -a $out
 | 
| 168 |     46 | 
 | 
|  |     47 | if (scala_vars re.scala)
 | 
|  |     48 | then
 | 
| 288 |     49 |   echo -e "  --> FAIL (make triple-sure your program conforms to the required format)\n" | tee -a $out
 | 
| 168 |     50 |   tsts0=$(( 1 ))
 | 
|  |     51 | else
 | 
| 288 |     52 |   echo -e "  --> success" | tee -a $out
 | 
| 168 |     53 |   tsts0=$(( 0 )) 
 | 
|  |     54 | fi
 | 
|  |     55 | 
 | 
|  |     56 | 
 | 
|  |     57 | # compilation test
 | 
|  |     58 | if  [ $tsts0 -eq 0 ]
 | 
|  |     59 | then    
 | 
| 288 |     60 |   echo -e "re.scala runs?" | tee -a $out
 | 
| 168 |     61 | 
 | 
|  |     62 |   if (scala_compile re.scala)
 | 
|  |     63 |   then
 | 
| 288 |     64 |     echo -e "  --> success" | tee -a $out
 | 
| 168 |     65 |     tsts1=$(( 0 ))
 | 
|  |     66 |   else
 | 
| 288 |     67 |     echo -e "  --> SCALA DID NOT RUN re.scala" | tee -a $out
 | 
| 168 |     68 |     tsts1=$(( 1 )) 
 | 
|  |     69 |   fi
 | 
|  |     70 | else
 | 
|  |     71 |   tsts1=$(( 1 ))     
 | 
|  |     72 | fi
 | 
|  |     73 | 
 | 
| 245 |     74 | ### re1 test
 | 
| 168 |     75 | 
 | 
|  |     76 | if [ $tsts1 -eq 0 ]
 | 
|  |     77 | then
 | 
| 288 |     78 |   echo -e " nullable(ZERO) == false" | tee -a $out
 | 
|  |     79 |   echo -e " nullable(ONE) == true" | tee -a $out
 | 
|  |     80 |   echo -e " nullable(CHAR('a')) == false" | tee -a $out
 | 
|  |     81 |   echo -e " nullable(ZERO | ONE) == true" | tee -a $out
 | 
|  |     82 |   echo -e " nullable(ZERO | CHAR('a')) == false" | tee -a $out
 | 
|  |     83 |   echo -e " nullable(ONE ~  ONE) == true" | tee -a $out
 | 
|  |     84 |   echo -e " nullable(ONE ~ CHAR('a')) == false" | tee -a $out
 | 
|  |     85 |   echo -e " nullable(STAR(ZERO)) == true" | tee -a $out
 | 
| 168 |     86 |   
 | 
| 245 |     87 |   if (scala_assert "re.scala" "re_test1.scala")
 | 
| 168 |     88 |   then
 | 
| 288 |     89 |       echo -e "  --> success" | tee -a $out
 | 
| 168 |     90 |       marks=$(( marks + 1 ))
 | 
|  |     91 |   else
 | 
| 288 |     92 |       echo -e "  --> ONE OF THE TESTS FAILED\n" | tee -a $out
 | 
| 168 |     93 |   fi
 | 
|  |     94 | fi
 | 
|  |     95 | 
 | 
| 245 |     96 | ### re2 test
 | 
| 168 |     97 | 
 | 
|  |     98 | if [ $tsts1 -eq 0 ]
 | 
|  |     99 | then
 | 
| 288 |    100 |   echo -e " der('a', ZERO | ONE) == (ZERO | ZERO)" | tee -a $out
 | 
|  |    101 |   echo -e " der('a', (CHAR('a') | ONE) ~ CHAR('a')) == ALT((ONE | ZERO) ~ CHAR('a'), ONE)" | tee -a $out
 | 
|  |    102 |   echo -e " der('a', (CHAR('a') | CHAR('a')) ~ CHAR('a')) == (ONE | ONE) ~ CHAR('a')" | tee -a $out
 | 
|  |    103 |   echo -e " der('a', STAR(CHAR('a'))) == (ONE ~ STAR(CHAR('a')))" | tee -a $out
 | 
|  |    104 |   echo -e " der('b', STAR(CHAR('a'))) == (ZERO ~ STAR(CHAR('a')))" | tee -a $out
 | 
|  |    105 |   echo -e "" | tee -a $out
 | 
|  |    106 |   echo -e " val r0 = \"a\" ~ \"b\" ~ \"c\"" | tee -a $out
 | 
|  |    107 |   echo -e " assert(der('a', r0) == (ONE ~ \"b\") ~ \"c\")" | tee -a $out
 | 
|  |    108 |   echo -e " assert(der('b', r0) == (ZERO ~ \"b\") ~ \"c\")" | tee -a $out
 | 
|  |    109 |   echo -e " assert(der('c', r0) == (ZERO ~ \"b\") ~ \"c\")" | tee -a $out
 | 
|  |    110 |   echo -e "" | tee -a $out
 | 
|  |    111 |   echo -e " val r1 = (ONE ~ \"b\") ~ \"c\"" | tee -a $out
 | 
|  |    112 |   echo -e " assert(der('a', r1) == ((ZERO ~ \"b\") | ZERO) ~ \"c\")" | tee -a $out
 | 
|  |    113 |   echo -e " assert(der('b', r1) == ((ZERO ~ \"b\") | ONE) ~ \"c\")" | tee -a $out
 | 
|  |    114 |   echo -e " assert(der('c', r1) == ((ZERO ~ \"b\") | ZERO) ~ \"c\")" | tee -a $out
 | 
|  |    115 |   echo -e "" | tee -a $out
 | 
|  |    116 |   echo -e " val r2 = ((ZERO ~ \"b\") | ONE) ~ \"c\"" | tee -a $out
 | 
|  |    117 |   echo -e " assert(der('a', r2) == ((((ZERO ~ \"b\") | ZERO) ~ \"c\") | ZERO))" | tee -a $out
 | 
|  |    118 |   echo -e " assert(der('b', r2) == ((((ZERO ~ \"b\") | ZERO) ~ \"c\") | ZERO))" | tee -a $out
 | 
|  |    119 |   echo -e " assert(der('c', r2) == ((((ZERO ~ \"b\") | ZERO) ~ \"c\") | ONE))" | tee -a $out
 | 
| 245 |    120 | 
 | 
|  |    121 |   if (scala_assert "re.scala" "re_test2.scala")
 | 
| 168 |    122 |   then
 | 
| 288 |    123 |       echo -e "  --> success" | tee -a $out
 | 
| 168 |    124 |       marks=$(( marks + 1 ))
 | 
|  |    125 |   else
 | 
| 288 |    126 |       echo -e "  --> ONE OF THE TESTS FAILED\n" | tee -a $out
 | 
| 168 |    127 |   fi
 | 
|  |    128 | fi
 | 
|  |    129 | 
 | 
| 245 |    130 | ### re3 test
 | 
| 168 |    131 | 
 | 
|  |    132 | if [ $tsts1 -eq 0 ]
 | 
|  |    133 | then
 | 
| 288 |    134 |   echo -e " simp(ZERO | ONE) == ONE" | tee -a $out
 | 
|  |    135 |   echo -e " simp(STAR(ZERO | ONE)) == STAR(ZERO | ONE)" | tee -a $out
 | 
|  |    136 |   echo -e " simp(ONE ~ (ONE ~ (ONE ~ CHAR('a')))) == CHAR('a')" | tee -a $out
 | 
|  |    137 |   echo -e " simp(((ONE ~ ONE) ~ ONE) ~ CHAR('a')) == CHAR('a')" | tee -a $out
 | 
|  |    138 |   echo -e " simp(((ONE | ONE) ~ ONE) ~ CHAR('a')) == CHAR('a')" | tee -a $out
 | 
|  |    139 |   echo -e " simp(ONE ~ (ONE ~ (ONE ~ ZERO))) == ZERO" | tee -a $out
 | 
|  |    140 |   echo -e " simp(ALT(ONE ~ (ONE ~ (ONE ~ ZERO)), CHAR('a'))) == CHAR('a')" | tee -a $out
 | 
|  |    141 |   echo -e " simp(CHAR('a') | CHAR('a')) == CHAR('a')" | tee -a $out
 | 
|  |    142 |   echo -e " simp(CHAR('a') ~ CHAR('a')) == CHAR('a') ~ CHAR('a')" | tee -a $out
 | 
|  |    143 |   echo -e " simp(ONE | CHAR('a')) == (ONE | CHAR('a'))" | tee -a $out
 | 
|  |    144 |   echo -e " simp(ALT((CHAR('a') | ZERO) ~ ONE," | tee -a $out
 | 
|  |    145 |   echo -e "          ((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO))) == CHAR('a')" | tee -a $out
 | 
|  |    146 |   echo -e " simp((ZERO | ((ZERO | ZERO) | (ZERO | ZERO))) ~ ((ONE | ZERO) | ONE ) ~ (CHAR('a'))) == ZERO" | tee -a $out
 | 
|  |    147 |   echo -e " simp(ALT(ONE | ONE, ONE | ONE)) == ONE" | tee -a $out
 | 
|  |    148 |   echo -e " simp(ALT(ZERO | CHAR('a'), CHAR('a') | ZERO)) == CHAR('a')" | tee -a $out
 | 
|  |    149 |   echo -e " simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE)) == ALT(ONE | CHAR('a'), CHAR('a') | ONE)" tee -a $out
 | 
| 245 |    150 |   
 | 
|  |    151 |   if (scala_assert "re.scala" "re_test3.scala")
 | 
| 168 |    152 |   then
 | 
| 288 |    153 |       echo -e "  --> success" | tee -a $out
 | 
| 245 |    154 |       marks=$(( marks + 1 ))
 | 
| 168 |    155 |   else
 | 
| 288 |    156 |       echo -e "  --> ONE OF THE TESTS FAILED\n" | tee -a $out
 | 
| 168 |    157 |   fi
 | 
|  |    158 | fi
 | 
|  |    159 | 
 | 
| 245 |    160 | ### re4 test
 | 
| 168 |    161 | 
 | 
|  |    162 | if [ $tsts1 -eq 0 ]
 | 
|  |    163 | then
 | 
| 288 |    164 |   echo -e " val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))" | tee -a $out
 | 
|  |    165 |   echo -e " ders((\"a\" * 5).toList,EVIL) == SEQ(SEQ(STAR(CHAR('a')),STAR(STAR(CHAR('a')))),CHAR('b'))" | tee -a $out
 | 
|  |    166 |   echo -e " ders(List('b'),EVIL) == ONE" | tee -a $out
 | 
|  |    167 |   echo -e " ders(List('b','b'),EVIL) == ZERO" | tee -a $out
 | 
|  |    168 |   echo -e " matcher(EVIL, \"a\" * 5 ++ \"b\") == true" | tee -a $out
 | 
|  |    169 |   echo -e " matcher(EVIL, \"a\" * 50 ++ \"b\") == true" | tee -a $out
 | 
|  |    170 |   echo -e " matcher(EVIL, \"a\" * 50) == false" | tee -a $out
 | 
|  |    171 |   echo -e " matcher(EVIL, \"b\") == true" | tee -a $out
 | 
|  |    172 |   echo -e " matcher(EVIL, \"bb\") == false" | tee -a $out
 | 
|  |    173 |   echo -e " matcher(\"abc\", \"abc\") == true" | tee -a $out
 | 
|  |    174 |   echo -e " matcher(\"abc\", \"ab\") == true" | tee -a $out
 | 
|  |    175 |   echo -e " matcher((\"ab\" | \"a\") ~ (ONE | \"bc\"), \"abc\") == true" | tee -a $out
 | 
|  |    176 |   echo -e " matcher(ONE, \"\") == true" | tee -a $out
 | 
|  |    177 |   echo -e " matcher(ZERO, \"\") == false" | tee -a $out
 | 
|  |    178 |   echo -e " matcher(ONE | CHAR('a'), \"\") == true" | tee -a $out
 | 
|  |    179 |   echo -e " matcher(ONE | CHAR('a'), \"a\") == true" | tee -a $out
 | 
| 168 |    180 |   
 | 
| 245 |    181 |   if (scala_assert "re.scala" "re_test4.scala")
 | 
| 168 |    182 |   then
 | 
| 288 |    183 |       echo -e "  --> success" | tee -a $out
 | 
| 168 |    184 |       marks=$(( marks + 1 ))
 | 
|  |    185 |   else
 | 
| 288 |    186 |       echo -e "  --> ONE OF THE TESTS FAILED\n" | tee -a $out
 | 
| 168 |    187 |   fi
 | 
|  |    188 | fi
 | 
|  |    189 | 
 | 
| 245 |    190 | ### re5 test
 | 
|  |    191 | 
 | 
| 168 |    192 | 
 | 
|  |    193 | if [ $tsts1 -eq 0 ]
 | 
|  |    194 | then
 | 
| 288 |    195 |   echo -e " val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))" | tee -a $out  
 | 
|  |    196 |   echo -e " size(der('a', der('a', EVIL))) == 28" | tee -a $out
 | 
|  |    197 |   echo -e " size(der('a', der('a', der('a', EVIL)))) == 58" | tee -a $out
 | 
|  |    198 |   echo -e " size(ders(\"aaaaaa\".toList, EVIL)) == 8" | tee -a $out
 | 
|  |    199 |   echo -e " size(ders((\"a\" * 50).toList, EVIL)) == 8" | tee -a $out
 | 
| 168 |    200 |   
 | 
| 245 |    201 |   if (scala_assert "re.scala" "re_test5.scala")
 | 
|  |    202 |   then
 | 
| 288 |    203 |       echo -e "  --> success" | tee -a $out
 | 
| 245 |    204 |       marks=$(( marks + 1 ))
 | 
|  |    205 |   else
 | 
| 288 |    206 |       echo -e "  --> ONE OF THE TESTS FAILED\n" | tee -a $out
 | 
| 245 |    207 |   fi
 | 
|  |    208 | fi
 | 
|  |    209 | 
 | 
|  |    210 | ### re6 'power' test
 | 
|  |    211 | 
 | 
|  |    212 | 
 | 
|  |    213 | 
 | 
|  |    214 | if [ $tsts1 -eq 0 ]
 | 
|  |    215 | then
 | 
| 288 |    216 |   echo -e " simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(50).next) == ONE" | tee -a $out
 | 
|  |    217 |   echo -e "    ...the Iterator produces the rexp" | tee -a $out
 | 
|  |    218 |   echo -e "" | tee -a $out
 | 
|  |    219 |   echo -e "      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)" | tee -a $out
 | 
|  |    220 |   echo -e "" | tee -a $out
 | 
|  |    221 |   echo -e "    where SEQ is nested 50 times." | tee -a $out  
 | 
|  |    222 |   echo -e "" | tee -a $out
 | 
|  |    223 |   echo -e " simp(Iterator.iterate(ONE:Rexp)(r => ALT(r, r)).drop(20).next) == ONE" | tee -a $out
 | 
|  |    224 |   echo -e "    ... the Iterator produces a rexp of size 2097151" | tee -a $out
 | 
|  |    225 |   echo -e "" | tee -a $out
 | 
|  |    226 |   echo -e " val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))" | tee -a $out
 | 
|  |    227 |   echo -e " matcher(EVIL, \"a\" * 1000000 ++ \"b\") == true" | tee -a $out
 | 
|  |    228 |   echo -e " matcher(EVIL, \"a\" * 1000000) == false" | tee -a $out
 | 
| 245 |    229 | 
 | 
|  |    230 |   
 | 
|  |    231 |   if (time scala_assert_long "re.scala" "re_test6.scala")
 | 
| 168 |    232 |   then
 | 
| 288 |    233 |       echo -e "  --> success" | tee -a $out
 | 
| 168 |    234 |       marks=$(( marks + 1 ))
 | 
|  |    235 |   else
 | 
| 288 |    236 |       echo -e "  --> ONE OF THE TESTS FAILED\n" | tee -a $out
 | 
| 168 |    237 |   fi
 | 
|  |    238 | fi
 | 
|  |    239 | 
 | 
|  |    240 | 
 | 
|  |    241 | ## final marks
 | 
| 288 |    242 | echo -e "Overall mark for CW 9, Core Part" | tee -a $out
 | 
|  |    243 | echo -e "$marks" | tee -a $out
 | 
| 168 |    244 | 
 | 
|  |    245 | 
 |