125
|
1 |
#!/bin/bash
|
|
2 |
|
|
3 |
# to make the script fail safely
|
|
4 |
set -euo pipefail
|
|
5 |
|
|
6 |
|
|
7 |
out=${1:-output}
|
|
8 |
|
302
|
9 |
echo -e "" > $out
|
125
|
10 |
|
302
|
11 |
echo -e "Below is the feedback for your submission collatz.scala" >> $out
|
|
12 |
echo -e "" >> $out
|
125
|
13 |
|
|
14 |
|
|
15 |
# compilation tests
|
|
16 |
|
|
17 |
function scala_compile {
|
360
|
18 |
(ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -Xprint:parser "$1" 2> c$out 1> c$out)
|
125
|
19 |
}
|
|
20 |
|
|
21 |
# functional tests
|
|
22 |
|
|
23 |
function scala_assert {
|
360
|
24 |
(ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -nc -i "$1" -- "$2" -e "" 2> /dev/null 1> /dev/null)
|
125
|
25 |
}
|
|
26 |
|
|
27 |
# purity test
|
|
28 |
|
|
29 |
function scala_vars {
|
346
|
30 |
(egrep '\bvar\b|\breturn\b|\.par\.|\.par |ListBuffer|mutable|util.control|new Array' c$out 2> /dev/null 1> /dev/null)
|
125
|
31 |
}
|
|
32 |
|
|
33 |
|
346
|
34 |
### compilation test
|
|
35 |
|
|
36 |
echo -e "collatz.scala runs?" >> $out
|
|
37 |
|
|
38 |
if (scala_compile collatz.scala)
|
|
39 |
then
|
360
|
40 |
echo -e " --> success" >> $out
|
346
|
41 |
tsts=$(( 0 ))
|
|
42 |
else
|
|
43 |
echo -e " --> SCALA DID NOT RUN collatz.scala\n" >> $out
|
|
44 |
tsts=$(( 1 ))
|
|
45 |
fi
|
|
46 |
|
|
47 |
|
|
48 |
|
125
|
49 |
# var, .par return, ListBuffer test
|
|
50 |
#
|
|
51 |
|
346
|
52 |
if [ $tsts -eq 0 ]
|
356
|
53 |
then
|
|
54 |
echo -e "collatz.scala does not contain VARS, RETURNS etc?" >> $out
|
|
55 |
|
346
|
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
|
360
|
61 |
echo -e " --> success" >> $out
|
346
|
62 |
tsts=$(( 0 ))
|
|
63 |
fi
|
|
64 |
fi
|
125
|
65 |
|
|
66 |
|
|
67 |
### collatz tests
|
|
68 |
|
|
69 |
if [ $tsts -eq 0 ]
|
|
70 |
then
|
302
|
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
|
125
|
75 |
|
|
76 |
if (scala_assert "collatz.scala" "collatz_test1.scala")
|
|
77 |
then
|
302
|
78 |
echo -e " --> success" >> $out
|
125
|
79 |
else
|
302
|
80 |
echo -e " --> ONE OF THE TESTS FAILED\n" >> $out
|
125
|
81 |
fi
|
|
82 |
fi
|
|
83 |
|
|
84 |
### collatz-max tests
|
|
85 |
|
|
86 |
if [ $tsts -eq 0 ]
|
|
87 |
then
|
302
|
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
|
125
|
94 |
|
|
95 |
if (scala_assert "collatz.scala" "collatz_test2.scala")
|
|
96 |
then
|
302
|
97 |
echo -e " --> success" >> $out
|
125
|
98 |
else
|
302
|
99 |
echo -e " --> ONE OF THE TESTS FAILED\n" >> $out
|
125
|
100 |
fi
|
|
101 |
fi
|
|
102 |
|
|
103 |
|
343
|
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
|