diff --git a/test/swen90006/machine/PartitioningTests.java b/test/swen90006/machine/PartitioningTests.java index 5494b44f4615351a610fc7b0b649d30b4b2d0a40..3f658784b6617438608de21403aba77069d70d49 100644 --- a/test/swen90006/machine/PartitioningTests.java +++ b/test/swen90006/machine/PartitioningTests.java @@ -8,10 +8,14 @@ import java.nio.file.Files; import java.nio.file.FileSystems; import org.junit.*; +import org.junit.rules.ExpectedException; + import static org.junit.Assert.*; public class PartitioningTests { + + //Any method annotated with "@Before" will be executed before each test, //allowing the tester to set up some shared resources. @Before public void setUp() @@ -24,55 +28,386 @@ public class PartitioningTests { } - //Any method annotation with "@Test" is executed as a test. - @Test public void aTest() + + @Test (expected= NoReturnValueException.class) + public void EC1Test() { - //the assertEquals method used to check whether two values are - //equal, using the equals method - final int expected = 2; - final int actual = 1 + 1; - assertEquals(expected, actual); + String a ="MOV R3 2;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + Machine m = new Machine(); + m.execute(lines); + } - - @Test public void anotherTest() + + @Test + public void EC2Test() { - List<String> list = new ArrayList<String>(); - list.add("a"); - list.add("b"); - - //the assertTrue method is used to check whether something holds. - assertTrue(list.contains("a")); + String a =" ;;white space line"; + String b ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + assertEquals(m.execute(lines), 0); + } - - //Test test opens a file and executes the machine - @Test public void aFileOpenTest() + + @Test + public void EC3Test() { - final List<String> lines = readInstructions("examples/array.s"); - Machine m = new Machine(); - assertEquals(m.execute(lines), 45); + String a ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + Machine m = new Machine(); + assertEquals(m.execute(lines), 0); + } - //To test an exception, specify the expected exception after the @Test - @Test(expected = java.io.IOException.class) - public void anExceptionTest() - throws Throwable + @Test (expected= InvalidInstructionException.class) + public void EC4Test() { - throw new java.io.IOException(); + String a ="RET R-1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + Machine m = new Machine(); + m.execute(lines); + } - - //This test should fail. - //To provide additional feedback when a test fails, an error message - //can be included - @Test public void aFailedTest() + + @Test + public void EC5Test() { - //include a message for better feedback - final int expected = 2; - final int actual = 1 + 2; - assertEquals("Some failure message", expected, actual); + String a ="ADD R1 R2 R3;"; + String b ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + assertEquals(m.execute(lines), 0); + } - - //Read in a file containing a program and convert into a list of - //string instructions + + @Test (expected= InvalidInstructionException.class) + public void EC6Test() + { + String a ="ADD R1 R2 R33;"; + String b ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test (expected= InvalidInstructionException.class) + public void EC7Test() + { + String a ="ADD R1 R35 R2;"; + String b ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test (expected= InvalidInstructionException.class) + public void EC8Test() + { + String a ="ADD R38 R2 R3;"; + String b ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test + public void EC9Test() + { + String a ="LDR R1 R2 65533;"; + String b ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + assertEquals(m.execute(lines), 0); + + } + + @Test (expected= InvalidInstructionException.class) + public void EC10Test() + { + String a ="LDR R1 R2 65536;"; + String b ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test (expected= InvalidInstructionException.class) + public void EC11Test() + { + String a ="LDR R3 R255 65532;"; + String b ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test (expected= InvalidInstructionException.class) + public void EC12Test() + { + String a ="LDR R39 R2 65532;"; + String b ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test + public void EC13Test() + { + String a ="STR R1 65533 R3;"; + String b ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + assertEquals(m.execute(lines), 0); + + } + + @Test (expected= InvalidInstructionException.class) + public void EC14Test() + { + String a ="STR R1 -65533 R36;"; + String b ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test (expected= InvalidInstructionException.class) + public void EC15Test() + { + String a ="STR R1 65536 R3;"; + String b ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test (expected= InvalidInstructionException.class) + public void EC16Test() + { + String a ="STR R111 65530 R3;"; + String b ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test + public void EC18Test() + { + String a ="JMP 2;"; + String b ="ADD R1 R2 R3"; + String c ="ADD R4 R5 R6;"; + String d ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + lines.add(c); + lines.add(d); + Machine m = new Machine(); + assertEquals(m.execute(lines), 0); + + } + + @Test (expected= NoReturnValueException.class) + public void EC19Test() + { + String a ="JMP 4;"; + String b ="ADD R1 R2 R3"; + String c ="ADD R4 R5 R6;"; + String d ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + lines.add(c); + lines.add(d); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test (expected= InvalidInstructionException.class) + public void EC20Test() + { + String a ="JMP 65536;"; + String b ="ADD R1 R2 R3"; + String c ="ADD R4 R5 R6;"; + String d ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + lines.add(c); + lines.add(d); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test + public void EC22Test() + { + String a ="JZ R1 2;"; + String b ="ADD R1 R2 R3"; + String c ="ADD R4 R5 R6;"; + String d ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + lines.add(c); + lines.add(d); + Machine m = new Machine(); + assertEquals(m.execute(lines), 0); + + } + + @Test (expected= NoReturnValueException.class) + public void EC23Test() + { + String a ="JZ R1 4;"; + String b ="ADD R1 R2 R3"; + String c ="ADD R4 R5 R6;"; + String d ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + lines.add(c); + lines.add(d); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test + public void EC24Test() + { + String e ="MOV R10 10"; + String a ="JZ R10 5;"; + String b ="ADD R1 R2 R3"; + String c ="ADD R4 R5 R6;"; + String d ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(e); + lines.add(a); + lines.add(b); + lines.add(c); + lines.add(d); + Machine m = new Machine(); + assertEquals(m.execute(lines), 0); + + } + + @Test (expected= InvalidInstructionException.class) + public void EC25Test() + { + String a ="JZ R1 65537;"; + String b ="ADD R1 R2 R3"; + String c ="ADD R4 R5 R6;"; + String d ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + lines.add(c); + lines.add(d); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test (expected= InvalidInstructionException.class) + public void EC26Test() + { + String a ="JZ R35 65531;"; + String b ="ADD R1 R2 R3"; + String c ="ADD R4 R5 R6;"; + String d ="RET R1;"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + lines.add(c); + lines.add(d); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test (expected= InvalidInstructionException.class) + public void EC27Test() + { + String a ="MOV R-2 5;"; + String b ="RET 2"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + m.execute(lines); + + } + + @Test + public void EC28Test() + { + String e ="MOV R15 150"; + String a ="RET R15"; + final List<String> lines =new ArrayList<String>(); + lines.add(e); + lines.add(a); + Machine m = new Machine(); + assertEquals(m.execute(lines), 150); + + } + + @Test (expected= InvalidInstructionException.class) + public void EC29Test() + { + String a ="MOV R20 -65536;"; + String b ="RET R20"; + final List<String> lines =new ArrayList<String>(); + lines.add(a); + lines.add(b); + Machine m = new Machine(); + m.execute(lines); + + } + private List<String> readInstructions(String file) { Charset charset = Charset.forName("UTF-8");