author | Christian Urban <christian.urban@kcl.ac.uk> |
Mon, 03 Feb 2025 12:34:38 +0000 | |
changeset 978 | 8778d23fef92 |
permissions | -rw-r--r-- |
978
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// for testing evaluation |
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
|
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
import scala.util.{Try, Success, Failure} |
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
import $file.cw034_add |
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
import cw034_add._ |
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
|
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
val str = os.read(os.pwd / "primes.while") |
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
val resmap = Map("end" -> 100, "n" -> 100, "f" -> 4, "tmp" -> 1) |
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
|
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
def mapeq[A, B](m1: Map[A, B], m2: Map[A, B]) : Boolean = { |
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
m1.keySet.forall(k => m1(k) == m2(k)) |
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
} |
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
|
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
|
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
Try(test_string(str)) match { |
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
case Success(v) if mapeq(resmap, v) => Console.println(s" Generated the correct result $v") |
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
case Success(v) => Console.println(s" Generated $v\n instead of\n $resmap\n.") ; throw new Exception("Different") |
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
case Failure(e) => Console.println(s" Exception raised.") ; throw(e) |
8778d23fef92
updated marking scripts
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
} |