From 564dfcde326c8828744fe3174bd82676b1bb7c03 Mon Sep 17 00:00:00 2001 From: Hongyi Chen <h.chen74@student.unimelb.edu.au> Date: Mon, 3 Sep 2018 14:38:58 +1000 Subject: [PATCH] Replace BoundaryTests.java --- test/swen90006/machine/BoundaryTests.java | 215 +++++++++++++++++++--- 1 file changed, 187 insertions(+), 28 deletions(-) diff --git a/test/swen90006/machine/BoundaryTests.java b/test/swen90006/machine/BoundaryTests.java index 61ce1ca..86feef8 100644 --- a/test/swen90006/machine/BoundaryTests.java +++ b/test/swen90006/machine/BoundaryTests.java @@ -24,53 +24,212 @@ public class BoundaryTests { } - //Any method annotation with "@Test" is executed as a test. - @Test public void aTest() + + @Test public void B1TestCase() { //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); + List<String> list = new ArrayList<String>(); + list.add("MOV R0 0"); + list.add("RET R0"); + Machine machine = new Machine(); + int actual = 0; // expected result + assertEquals(machine.execute(list), actual); } - @Test public void anotherTest() + @Test public void B2TestCase() { + //the assertEquals method used to check whether two values are + //equal, using the equals method List<String> list = new ArrayList<String>(); - list.add("a"); - list.add("b"); + list.add("MOV R31 31"); + list.add("RET R31"); + Machine machine = new Machine(); + int actual = 31; + assertEquals(machine.execute(list), actual); + } - //the assertTrue method is used to check whether something holds. - assertTrue(list.contains("a")); + @Test public void B5TestCase() + { + //the assertEquals method used to check whether two values are + //equal, using the equals method + List<String> list = new ArrayList<String>(); + list.add("MOV R1 -65535"); + list.add("RET R1"); + Machine machine = new Machine(); + int actual = -65535; + assertEquals(machine.execute(list), actual); } - //Test test opens a file and executes the machine - @Test public void aFileOpenTest() + @Test public void B6TestCase() { - final List<String> lines = readInstructions("examples/array.s"); - Machine m = new Machine(); - assertEquals(m.execute(lines), 45); + //the assertEquals method used to check whether two values are + //equal, using the equals method + List<String> list = new ArrayList<String>(); + list.add("MOV R1 65535"); + list.add("RET R1"); + Machine machine = new Machine(); + int actual = 65535; + assertEquals(machine.execute(list), actual); } - + + @Test public void B9TestCase() + { + //the assertEquals method used to check whether two values are + //equal, using the equals method + List<String> list = new ArrayList<String>(); + list.add("JMP 0"); + list.add("MOV R1 0"); + list.add("RET R1"); + Machine machine = new Machine(); + int actual = 0; // It should be a infinite loop. + assertEquals(machine.execute(list), actual); + } + + @Test public void B10TestCase() + { + List<String> list = new ArrayList<String>(); + list.add("MOV R1 10"); + list.add("MOV R2 12"); + list.add("JMP 2"); + list.add("RET R1"); + list.add("RET R2"); + Machine machine = new Machine(); + int actual = 12; + assertEquals(machine.execute(list), actual); + } + + @Test public void B13TestCase() + { + List<String> list = new ArrayList<String>(); + list.add("MOV R1 0"); + list.add("MOV R2 100"); + list.add("STR R1 0 R2"); + list.add("LDR R3 R1 0"); + list.add("RET R3"); + Machine machine = new Machine(); + int actual = 100; + assertEquals(machine.execute(list), actual); + } + + @Test public void B14TestCase() + { + List<String> list = new ArrayList<String>(); + list.add("MOV R1 60000"); + list.add("MOV R2 100"); + list.add("STR R1 5535 R2"); + list.add("LDR R3 R1 5535"); + list.add("RET R3"); + Machine machine = new Machine(); + int actual = 100; + assertEquals(machine.execute(list), actual); + } + + @Test public void B15TestCase() + { + List<String> list = new ArrayList<String>(); + list.add("MOV R3 0"); + list.add("MOV R1 0"); + list.add("MOV R2 100"); + list.add("STR R1 -1 R2");// instruction does nothing + list.add("LDR R3 R1 -1");// instruction does nothing + list.add("RET R3"); + Machine machine = new Machine(); + int actual = 0; + assertEquals(machine.execute(list), actual); + } + + @Test public void B16TestCase() + { + List<String> list = new ArrayList<String>(); + list.add("MOV R3 2"); + list.add("MOV R1 60000"); + list.add("MOV R2 100"); + list.add("STR R1 5536 R2");// instruction does nothing + list.add("LDR R3 R1 5536");// instruction does nothing + list.add("RET R3"); + Machine machine = new Machine(); + int actual = 2; + assertEquals(machine.execute(list), actual); + } +//------------------------------- //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 B3anInvalidInstructionExceptionTest() + { + List<String> list = new ArrayList<String>(); + list.add("MOV R-1 -1"); + list.add("RET R-1"); + Machine machine = new Machine(); + machine.execute(list); + } + + @Test(expected = InvalidInstructionException.class) + public void B4anInvalidInstructionExceptionTest() + { + List<String> list = new ArrayList<String>(); + list.add("MOV R32 32"); + list.add("RET R32"); + Machine machine = new Machine(); + machine.execute(list); + } + + @Test(expected = InvalidInstructionException.class) + public void B7anInvalidInstructionExceptionTest() + { + List<String> list = new ArrayList<String>(); + list.add("MOV R2 -65536"); + list.add("RET R2"); + Machine machine = new Machine(); + machine.execute(list); + } + + @Test(expected = InvalidInstructionException.class) + public void B8anInvalidInstructionExceptionTest() + { + List<String> list = new ArrayList<String>(); + list.add("MOV R3 65536"); + list.add("RET R3"); + Machine machine = new Machine(); + machine.execute(list); + } + + @Test(expected = NoReturnValueException.class) + public void B11anNoReturnValueExceptionTest() + { + List<String> list = new ArrayList<String>(); + list.add("MOV R1 10"); + list.add("JMP -2"); + list.add("RET R1"); + Machine machine = new Machine(); + machine.execute(list); + } + + + @Test(expected = NoReturnValueException.class) + public void B12anNoReturnValueExceptionTest() { - throw new java.io.IOException(); + List<String> list = new ArrayList<String>(); + list.add("MOV R1 10"); + list.add("JMP 2"); + list.add("RET R1"); + Machine machine = new Machine(); + machine.execute(list); } - //This test should fail. - //To provide additional feedback when a test fails, an error message - //can be included - @Test public void aFailedTest() + //To test an exception, specify the expected exception after the @Test + @Test(expected = InvalidInstructionException.class) + public void EC3anInvalidInstructionException() { - //include a message for better feedback - final int expected = 2; - final int actual = 1 + 2; - assertEquals("Some failure message", expected, actual); + List<String> list = new ArrayList<String>(); + list.add("CMP R1 R2"); + Machine machine = new Machine(); + machine.execute(list); } + + + //Read in a file containing a program and convert into a list of //string instructions private List<String> readInstructions(String file) -- GitLab