-
Notifications
You must be signed in to change notification settings - Fork 0
/
MutExample.scala
77 lines (68 loc) · 1.58 KB
/
MutExample.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Overheating extends Exception
/**
* This class is to be unit tested.
*/
class CPU {
def fetch(): Boolean = false
def decode(): Boolean = true
def execute(): Boolean = { throw new Overheating }
def writeback(): Boolean = true
var core1 = "off"
var core2 = "off"
}
/**
* MUT test for CPU.
*/
class CpuMUT extends MUT {
val cpu = new CPU
override def setup {
// turn on one CPU core
cpu.core1 = "on"
}
override def cleanup {
// turn CPU cores off
cpu.core1 = "off"
cpu.core2 = "off"
}
test("Fetching instruction") {
if (cpu.fetch())
true
else
false
} otherwise { println("CPU cannot fetch an instruction\n") }
next("Decode instruction") {
if (cpu.decode())
true
else
false
} otherwise { println("CPU cannot decode an instruction\n") }
timeNext("Execute instruction") {
try {
if (cpu.execute())
true
else
false
} catch {
case e: Overheating =>
{
halt("\nCPU overheated!")
println("CPU automatically shuts down")
}
false
}
} otherwise { println("CPU cannot execute an instruction\n") }
timeNext("Write result back into memory") {
cpu.writeback()
} otherwise { println("CPU cannot write result back into memory") }
timeTest("CPU has been reset. Core 2 is on. Fetching instruction") {
cpu.core2 = "on"
if (cpu.fetch())
true
else
false
} otherwise { println("CPU cannot fetch an instruction") }
cleanup
}
object MutExample {
def main(args: Array[String]): Unit = { new CpuMUT() }
}