1 #!/bin/bash |
|
2 |
|
3 # to make the script fail safely |
|
4 set -euo pipefail |
|
5 |
|
6 |
|
7 |
|
8 scalafile=${1:-danube.scala} |
|
9 out=${2:-output} |
|
10 |
|
11 |
|
12 |
|
13 echo "" > $out |
|
14 |
|
15 echo "Below is the feedback for your submission danube.scala" >> $out |
|
16 echo "" >> $out |
|
17 |
|
18 |
|
19 # compilation tests |
|
20 |
|
21 function scala_compile { |
|
22 (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -Xprint:parser "$1" 2> c$out 1> c$out) |
|
23 } |
|
24 |
|
25 # functional tests |
|
26 |
|
27 function scala_assert { |
|
28 (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -i "$1" -- "$2" -e "" 2> /dev/null 1> /dev/null) |
|
29 } |
|
30 |
|
31 # purity test |
|
32 |
|
33 function scala_vars { |
|
34 (egrep '\bvar\b|\breturn\b|\.par\.|\.par |ListBuffer|AtomicInteger|mutable|util.control|new Array' c$out 2> /dev/null 1> /dev/null) |
|
35 } |
|
36 |
|
37 |
|
38 |
|
39 ### compilation test |
|
40 |
|
41 echo "danube.scala runs?" >> $out |
|
42 |
|
43 if (scala_compile $scalafile) |
|
44 then |
|
45 echo " --> passed" >> $out |
|
46 tsts=$(( 0 )) |
|
47 else |
|
48 echo " --> SCALA DID NOT RUN danube.scala" >> $out |
|
49 tsts=$(( 1 )) |
|
50 fi |
|
51 |
|
52 # var, .par return, ListBuffer test |
|
53 # |
|
54 |
|
55 if [ $tsts -eq 0 ] |
|
56 then |
|
57 echo "danube.scala does not contain vars, returns etc?" >> $out |
|
58 |
|
59 if (scala_vars $scalafile) |
|
60 then |
|
61 echo " --> FAIL (make triple-sure your program conforms to the required format)" >> $out |
|
62 tsts=$(( 1 )) |
|
63 else |
|
64 echo " --> passed" >> $out |
|
65 tsts=$(( 0 )) |
|
66 fi |
|
67 fi |
|
68 |
|
69 |
|
70 ### danube get_cvs_url tests |
|
71 |
|
72 if [ $tsts -eq 0 ] |
|
73 then |
|
74 echo "danube.scala tests:" >> $out |
|
75 echo " val movies_url = \"\"\"https://nms.kcl.ac.uk/christian.urban/movies.csv\"\"\"" >> $out |
|
76 echo " get_csv_url(movies_url).length == 9742" >> $out |
|
77 |
|
78 if (scala_assert $scalafile "danube_test1.scala") |
|
79 then |
|
80 echo -e " --> success" >> $out |
|
81 else |
|
82 echo -e " --> ONE OF THE TESTS FAILED\n" >> $out |
|
83 fi |
|
84 fi |
|
85 |
|
86 ### danube processing tests |
|
87 |
|
88 if [ $tsts -eq 0 ] |
|
89 then |
|
90 echo " val good_ratings = process_ratings(ratings)" >> $out |
|
91 echo " val movie_names = process_movies(movies)" >> $out |
|
92 echo " " >> $out |
|
93 echo " good_ratings.length == 48580 " >> $out |
|
94 echo " movie_names.length == 9742 " >> $out |
|
95 echo " " >> $out |
|
96 echo " val r_elems = List((\"1\",\"1\"), (\"1\",\"3\"), (\"1\",\"6\"), (\"1\",\"47\")) " >> $out |
|
97 echo " r_elems.forall(good_ratings.contains(_)) == true" >> $out |
|
98 |
|
99 if (scala_assert $scalafile "danube_test2.scala") |
|
100 then |
|
101 echo -e " --> success" >> $out |
|
102 else |
|
103 echo -e " --> ONE OF THE TESTS FAILED\n" >> $out |
|
104 fi |
|
105 fi |
|
106 |
|
107 ### danube groupById test |
|
108 |
|
109 if [ $tsts -eq 0 ] |
|
110 then |
|
111 echo -e " val ls1 = List((\"1\", \"a\"), (\"2\", \"a\"), (\"1\", \"c\"), (\"2\", \"a\"), (\"1\", \"c\"))" >> $out |
|
112 echo -e " val ls2 = List((\"1\", \"a\"), (\"1\", \"b\"), (\"2\", \"x\"), (\"3\", \"a\"), (\"2\", \"y\"), (\"3\", \"c\"))" >> $out |
|
113 echo -e " groupById(ls1, Map()) == Map(1 -> List(c, c, a), 2 -> List(a, a))" >> $out |
|
114 echo -e " groupById(ls2, Map()) == Map(1 -> List(b, a), 2 -> List(x, y), 3 -> List(c, a))" >> $out |
|
115 echo -e " where the order in the lists is unimportant" >> $out |
|
116 echo -e " val ls3 = (1 to 1000).map(_.toString).toList" >> $out |
|
117 echo -e " val ls4 = ls3 zip ls3.tail" >> $out |
|
118 echo -e " val ls5 = ls4 ::: ls4.reverse" >> $out |
|
119 echo -e " groupById(ls5, Map()) == Map(1 -> List(2,2), 2 -> List(3,3), ....)" >> $out |
|
120 |
|
121 if (scala_assert $scalafile "danube_test3.scala") |
|
122 then |
|
123 echo -e " --> success" >> $out |
|
124 else |
|
125 echo -e " --> ONE OF THE TESTS FAILED\n" >> $out |
|
126 fi |
|
127 fi |
|
128 |
|
129 ### danube favourites tests |
|
130 |
|
131 if [ $tsts -eq 0 ] |
|
132 then |
|
133 echo -e " val good_ratings = process_ratings(ratings)" >> $out |
|
134 echo -e " val ratings_map = groupById(good_ratings, Map())" >> $out |
|
135 echo -e " favourites(ratings_map, \"912\").length == 80 " >> $out |
|
136 echo -e " favourites(ratings_map, \"858\").length == 158 " >> $out |
|
137 echo -e " favourites(ratings_map, \"260\").length == 201 " >> $out |
|
138 |
|
139 if (scala_assert $scalafile "danube_test4.scala") |
|
140 then |
|
141 echo -e " --> success" >> $out |
|
142 else |
|
143 echo -e " --> ONE OF THE TESTS FAILED\n" >> $out |
|
144 fi |
|
145 fi |
|
146 |
|
147 ### danube suggestions tests |
|
148 |
|
149 if [ $tsts -eq 0 ] |
|
150 then |
|
151 echo -e " val good_ratings = process_ratings(ratings)" >> $out |
|
152 echo -e " val ratings_map = groupById(good_ratings, Map())" >> $out |
|
153 echo -e " suggestions(ratings_map, \"912\").length == 4110 " >> $out |
|
154 echo -e " suggestions(ratings_map, \"858\").length == 4883 " >> $out |
|
155 echo -e " suggestions(ratings_map, \"260\").length == 4970 " >> $out |
|
156 |
|
157 if (scala_assert $scalafile "danube_test5.scala") |
|
158 then |
|
159 echo -e " --> success" >> $out |
|
160 else |
|
161 echo -e " --> ONE OF THE TESTS FAILED\n" >> $out |
|
162 fi |
|
163 fi |
|
164 |
|
165 ### danube recommendation tests |
|
166 |
|
167 if [ $tsts -eq 0 ] |
|
168 then |
|
169 echo -e " recommendations(ratings_map, movies_map, \"1\").length == 2 " >> $out |
|
170 echo -e " recommendations(ratings_map, movies_map, \"2\").length == 2 " >> $out |
|
171 echo -e " recommendations(ratings_map, movies_map, \"3\").length == 2 " >> $out |
|
172 echo -e " recommendations(ratings_map, movies_map, \"4\").length == 0 " >> $out |
|
173 echo -e " recommendations(ratings_map, movies_map, \"5\").length == 2 " >> $out |
|
174 |
|
175 if (scala_assert $scalafile "danube_test6.scala") |
|
176 then |
|
177 echo -e " --> success" >> $out |
|
178 else |
|
179 echo -e " --> ONE OF THE TESTS FAILED\n" >> $out |
|
180 fi |
|
181 fi |
|
182 |
|
183 |
|
184 |
|