Skip to content
Snippets Groups Projects
Commit 2e9729f7 authored by Zhuolun Lang's avatar Zhuolun Lang
Browse files

Update PartitioningTests.java

parent a671ccc6
No related branches found
No related tags found
No related merge requests found
...@@ -8,83 +8,132 @@ import java.nio.file.Files; ...@@ -8,83 +8,132 @@ import java.nio.file.Files;
import java.nio.file.FileSystems; import java.nio.file.FileSystems;
import org.junit.*; import org.junit.*;
import com.sun.xml.internal.ws.util.StringUtils;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class PartitioningTests public class PartitioningTests {
{
//Any method annotated with "@Before" will be executed before each test, @Test(expected = InvalidInstructionException.class)
//allowing the tester to set up some shared resources. public void EC1() throws Throwable {
@Before public void setUp() Machine machine = new Machine();
{ List<String> instruction = new ArrayList<>();
instruction.add("add R-1 R0");
machine.execute(instruction);
}
@Test(expected = InvalidInstructionException.class)
public void EC2() throws Throwable {
Machine machine = new Machine();
List<String> instruction = new ArrayList<>();
instruction.add("sub R32 R0");
machine.execute(instruction);
}
@Test(expected = InvalidInstructionException.class)
public void EC3() throws Throwable {
Machine machine = new Machine();
List<String> instruction = new ArrayList<>();
instruction.add("mov R0 -65536");
machine.execute(instruction);
}
@Test(expected = InvalidInstructionException.class)
public void EC4() throws Throwable {
Machine machine = new Machine();
List<String> instruction = new ArrayList<>();
instruction.add("ldr R0 R1 65536");
machine.execute(instruction);
} }
//Any method annotated with "@After" will be executed after each test, @Test
//allowing the tester to release any shared resources used in the setup. public void EC5() {
@After public void tearDown() Machine machine = new Machine();
{ List<String> instruction = new ArrayList<>();
instruction.add("ret R0");
assertEquals(machine.execute(instruction), 0);
} }
//Any method annotation with "@Test" is executed as a test. @Test(expected = NoReturnValueException.class)
@Test public void aTest() public void EC6() throws Throwable {
{ Machine machine = new Machine();
//the assertEquals method used to check whether two values are List<String> instruction = new ArrayList<>();
//equal, using the equals method instruction.add("jmp -1");
final int expected = 2; machine.execute(instruction);
final int actual = 1 + 1;
assertEquals(expected, actual);
} }
@Test public void anotherTest() @Test(expected = InvalidInstructionException.class)
{ public void EC7() throws Throwable {
List<String> list = new ArrayList<String>(); Machine machine = new Machine();
list.add("a"); List<String> instruction = new ArrayList<>();
list.add("b"); instruction.add("div R-1 R0 R1");
instruction.add("ret R0");
machine.execute(instruction);
}
//the assertTrue method is used to check whether something holds. @Test(expected = InvalidInstructionException.class)
assertTrue(list.contains("a")); public void EC8() throws Throwable {
Machine machine = new Machine();
List<String> instruction = new ArrayList<>();
instruction.add("str R32 1 R0");
instruction.add("ret R0");
machine.execute(instruction);
} }
//Test test opens a file and executes the machine @Test(expected = InvalidInstructionException.class)
@Test public void aFileOpenTest() public void EC9() throws Throwable {
{ Machine machine = new Machine();
final List<String> lines = readInstructions("examples/array.s"); List<String> instruction = new ArrayList<>();
Machine m = new Machine(); instruction.add("jz R0 -65536");
assertEquals(m.execute(lines), 45); instruction.add("ret R0");
machine.execute(instruction);
} }
//To test an exception, specify the expected exception after the @Test @Test(expected = InvalidInstructionException.class)
@Test(expected = java.io.IOException.class) public void EC10() throws Throwable {
public void anExceptionTest() Machine machine = new Machine();
throws Throwable List<String> instruction = new ArrayList<>();
{ instruction.add("mov R0 65536");
throw new java.io.IOException(); instruction.add("jz R0 1");
instruction.add("ret R0");
machine.execute(instruction);
} }
//This test should fail. @Test
//To provide additional feedback when a test fails, an error message public void EC11() {
//can be included Machine machine = new Machine();
@Test public void aFailedTest() List<String> instruction = new ArrayList<>();
{ instruction.add("mov R0 -65535");
//include a message for better feedback instruction.add("mov R31 65535");
final int expected = 2; instruction.add("mul R1 R0 R31");
final int actual = 1 + 2; instruction.add("ret R0");
assertEquals("Some failure message", expected, actual); assertEquals(machine.execute(instruction), -65535);
} }
//Read in a file containing a program and convert into a list of @Test(expected = NoReturnValueException.class)
//string instructions public void EC12() {
private List<String> readInstructions(String file) Machine machine = new Machine();
{ List<String> instruction = new ArrayList<>();
Charset charset = Charset.forName("UTF-8"); instruction.add("mov R0 -65535");
List<String> lines = null; instruction.add("mov R31 65535");
try { instruction.add("jmp 2");
lines = Files.readAllLines(FileSystems.getDefault().getPath(file), charset); machine.execute(instruction);
} }
catch (Exception e){
System.err.println("Invalid input file! (stacktrace follows)"); @Test(expected = InvalidInstructionException.class)
e.printStackTrace(System.err); public void EC13() {
System.exit(1); Machine machine = new Machine();
List<String> instruction = new ArrayList<>();
instruction.add("");
instruction.add("abc");
machine.execute(instruction);
} }
return lines;
@Test(expected = NoReturnValueException.class)
public void EC14() {
Machine machine = new Machine();
List<String> instruction = new ArrayList<>();
machine.execute(instruction);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment