Java Reference
In-Depth Information
public void
simulate(
int
numSteps){
int
step;
for
(step
#
0; step<numSteps && !cpu.halted();
!!
step){
cpu.execute();
ram.execute();
}
}
}
5.4.4
Test
The test of this prototype has to check the behaviour of the components of
the computer. The RAM can be tested for the initialization and for the inter-
action with the CPU through the bus. The CPU can be tested by executing
the simulation. In particular the CPU behaviour is correct if:
the CPU terminates the program in a finite time;
■
the memory contains the correct result of the operations.
■
The termination is an important test since it ensures that the system
has not entered some deadlock, i.e. for bad synchronization among the
elements. It can be tested checking the
halted()
method.
The correct result in the RAM ensures that the information has been
correctly transferred between the components and that it has been correctly
computed. It can be checked comparing the RAM at the end of the simu-
lation with the expected memory contents.
The test case
TestComputer
performs these basic tests.
public class
TestComputer
extends
TestCase {
private void
compareMemory(RAM ram, String[] expected){
for
(
int
i
#
0; i<expected.length;
!!
i)
assertEquals(expected[i],ram.cell(i));
}
public void
testMemoryInitialization() {
String initialMemory[]
#
{"123","432","555"};
Computer computer
#
new
Computer(initialMemory);
compareMemory(computer.ram,initialMemory);
}
public void
testExecution() {
String initialMemory[]
#
{"123","432","0"};
Computer computer
#
new
Computer(initialMemory);
computer.simulate(10);
assertTrue("CPU not halted",computer.cpu.halted());
String expectedMemory[]
#
{"123","432","555"};
compareMemory(computer.ram,expectedMemory);
}
}