diff --git a/classes/swen90006/machine/BoundaryTests.class b/classes/swen90006/machine/BoundaryTests.class index 75ca15dbf4f906cd2b7beec235fa91a8de079579..9ff45bef03d593152e582e0083f6f1217170d11c 100644 Binary files a/classes/swen90006/machine/BoundaryTests.class and b/classes/swen90006/machine/BoundaryTests.class differ diff --git a/test/swen90006/machine/BoundaryTests.java b/test/swen90006/machine/BoundaryTests.java index 61ce1ca647f325fbf452047408c1e22d9befcb99..eb147f8678d9c02dc3e7ef07037f2dc752e3acf0 100644 --- a/test/swen90006/machine/BoundaryTests.java +++ b/test/swen90006/machine/BoundaryTests.java @@ -1,5 +1,15 @@ package swen90006.machine; +/* +* University of Melbourne +* SWEN90006 Software Testing and Reliability +* Semester 2 - 2018 +* Assignment 1 +* Author: Junhan Yang +* Login: junhany +* StudentID: 777738 +*/ + import java.util.List; import java.util.ArrayList; import java.nio.charset.Charset; @@ -12,6 +22,12 @@ import static org.junit.Assert.*; public class BoundaryTests { + + // EC refers to equivalent class. + // These test cases are generated by using boundary-value analysis. + // Note that some ECs have more than one test cases and some have none + //after applying the 4 guidelines in the notes. + //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,67 +40,609 @@ public class BoundaryTests { } - //Any method annotation with "@Test" is executed as a test. - @Test public void aTest() + /* ================================ For EC1 ================================ */ + // There are 2 test cases generated from EC1 by guideline 1. + // Boundary: Number(RET) = 0 + // 1. On point: 0 + // The Machine's expected behaviour is to raise a NoReturnValueException. + @Test(expected = NoReturnValueException.class) + public void EC1OnPointTest() + throws Throwable { - //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> lines = new ArrayList<String>(); + lines.add("MOV R1 1"); + Machine m = new Machine(); + m.execute(lines); } - @Test public void anotherTest() + // 2. Off point: 1 + // The Machine's expected behaviour is to the default value 0 of a register. + @Test public void EC1OffPointTest() { - List<String> list = new ArrayList<String>(); - list.add("a"); - list.add("b"); + String err_msg = "EC1 off point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("RET R0"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 0); + } + /* ========================================================================= */ + + + /* ================================ For EC2 ================================ */ + // There are 1 test cases generated from EC2 by guideline 2. + // Boundary: Number(RET) > 0 - //the assertTrue method is used to check whether something holds. - assertTrue(list.contains("a")); + // 1. Off point: 2 + // The Machine's expected behaviour is to return R0 value. + @Test public void EC2OffPointTest() + { + String err_msg = "EC2 off point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("RET R0"); + lines.add("MOV R1 1"); + lines.add("RET R1"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 0); } + /* ========================================================================= */ - //Test test opens a file and executes the machine - @Test public void aFileOpenTest() + + /* ================================ For EC3 ================================ */ + // There are 1 test cases generated from EC2 by guideline 2. + // Boundary: Number(unknow instructions execute) > 0 + + // 1. On point: 0 + // The Machine's expected behaviour is to return R0 value. + @Test public void EC3OnPointTest() { - final List<String> lines = readInstructions("examples/array.s"); + String unknown_ins = "FOO 1"; + String err_msg = "EC3 on point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("RET R0"); + lines.add(unknown_ins); Machine m = new Machine(); - assertEquals(m.execute(lines), 45); + assertEquals(err_msg, m.execute(lines), 0); } - - //To test an exception, specify the expected exception after the @Test - @Test(expected = java.io.IOException.class) - public void anExceptionTest() + + // 2. Off point: 1 + // The Machine's expected behaviour is to raise an InvalidInstructionException. + @Test(expected = InvalidInstructionException.class) + public void EC3OffPointTest() throws Throwable { - throw new java.io.IOException(); + String unknown_ins = "FOO 1"; + List<String> lines = new ArrayList<String>(); + lines.add(unknown_ins); + Machine m = new Machine(); + m.execute(lines); } + /* ========================================================================= */ + + + /* ================================ For EC5 ================================ */ + // There are 2 test cases generated from EC5 by guideline 2. + // Boundary: {Rx | x is integer and x < 0} - //This test should fail. - //To provide additional feedback when a test fails, an error message - //can be included - @Test public void aFailedTest() + + // 1. On point: 0 + // The Machine's expected behaviour is to return R0 value. + @Test public void EC5OnPointTest() { - //include a message for better feedback - final int expected = 2; - final int actual = 1 + 2; - assertEquals("Some failure message", expected, actual); + String err_msg = "EC5 on point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("RET R0"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 0); } - //Read in a file containing a program and convert into a list of - //string instructions - private List<String> readInstructions(String file) + // 2. Off point: -1 + // The Machine's expected behaviour is to raise an InvalidInstructionException. + @Test(expected = InvalidInstructionException.class) + public void EC5OffPointTest() + throws Throwable { - Charset charset = Charset.forName("UTF-8"); - List<String> lines = null; - try { - lines = Files.readAllLines(FileSystems.getDefault().getPath(file), charset); - } - catch (Exception e){ - System.err.println("Invalid input file! (stacktrace follows)"); - e.printStackTrace(System.err); - System.exit(1); + List<String> lines = new ArrayList<String>(); + lines.add("RET R-1"); + Machine m = new Machine(); + m.execute(lines); + } + /* ========================================================================= */ + + + /* ================================ For EC6 ================================ */ + // There are 2 test cases generated from EC6 by guideline 2. + // Boundary: {Rx | x is integer and x > 31} + + // 1. On point: 31 + // The Machine's expected behaviour is to return R0 value. + @Test public void EC6OnPointTest() + { + String err_msg = "EC6 on point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("RET R31"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 0); + } + + // 2. Off point: 32 + // The Machine's expected behaviour is to raise an InvalidInstructionException. + @Test(expected = InvalidInstructionException.class) + public void EC6OffPointTest() + throws Throwable + { + List<String> lines = new ArrayList<String>(); + lines.add("RET R32"); + Machine m = new Machine(); + m.execute(lines); + } + /* ========================================================================= */ + + + /* ================================ For EC7 ================================ */ + // There are 2 test cases generated from EC7 by guideline 2. + // Boundary: {val | val is integer and val > 65535} + + // 1. On point: 65535 + // The Machine's expected behaviour is to return R0 value. + @Test public void EC7OnPointTest() + { + String err_msg = "EC7 on point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 65535"); + lines.add("RET R0"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 65535); + } + + // 2. Off point: 65536 + // The Machine's expected behaviour is to raise an InvalidInstructionException. + @Test(expected = InvalidInstructionException.class) + public void EC7OffPointTest() + throws Throwable + { + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 65536"); + lines.add("RET R0"); + Machine m = new Machine(); + m.execute(lines); + } + /* ========================================================================= */ + + + /* ================================ For EC8 ================================ */ + // There are 2 test cases generated from EC8 by guideline 2. + // Boundary: {val | val is integer and val < -65535} + + // 1. On point: -65535 + // The Machine's expected behaviour is to return R0 value. + @Test public void EC8OnPointTest() + { + String err_msg = "EC8 on point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 -65535"); + lines.add("RET R0"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), -65535); + } + + // 2. Off point: -65536 + // The Machine's expected behaviour is to raise an InvalidInstructionException. + @Test(expected = InvalidInstructionException.class) + public void EC8OffPointTest() + throws Throwable + { + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 -65536"); + lines.add("RET R0"); + Machine m = new Machine(); + m.execute(lines); + } + /* ========================================================================== */ + + + /* ================================ For EC10 ================================ */ + // There are 1 test cases generated from EC10 by guideline 2&4. + // Boundary: Number(ADD) > 0 + + // 1. Off point: 1 + // The Machine's expected behaviour is to return R2 value. + @Test public void EC10OffPointTest() + { + String err_msg = "EC10 off point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 1"); + lines.add("MOV R1 2"); + lines.add("ADD R2 R1 R0"); + lines.add("RET R2"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 3); + } + /* ========================================================================== */ + + + /* ================================ For EC11 ================================ */ + // There are 1 test cases generated from EC11 by guideline 2&4. + // Boundary: Number(SUB) > 0 + + // 1. Off point: 1 + // The Machine's expected behaviour is to return R2 value. + @Test public void EC11OffPointTest() + { + String err_msg = "EC11 off point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 -1"); + lines.add("MOV R1 2"); + lines.add("SUB R2 R1 R0"); + lines.add("RET R2"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 3); + } + /* ========================================================================== */ + + + /* ================================ For EC12 ================================ */ + // There are 1 test cases generated from EC12 by guideline 2&4. + // Boundary: Number(MUL) > 0 + + // 1. Off point: 1 + // The Machine's expected behaviour is to return R2 value. + @Test public void EC12OffPointTest() + { + String err_msg = "EC12 off point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 -1"); + lines.add("MOV R1 -3"); + lines.add("MUL R2 R1 R0"); + lines.add("RET R2"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 3); + } + /* ========================================================================== */ + + + /* ================================ For EC13 ================================ */ + // There are 2 test cases generated from EC13 by guideline 1. + // Boundary: For some JMP x, {x | x = 1} + + // 1. On point: 1 + // The Machine's expected behaviour is to return R0 value. + @Test public void EC13OnPointTest() + { + String err_msg = "EC13 on point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("JMP 1"); + lines.add("MOV R0 1"); + lines.add("RET R0"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 1); + } + + // 2. Off point: 2 + // The Machine's expected behaviour is to return R0 default value. + @Test public void EC13OffPointTest() + { + String err_msg = "EC13 off point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("JMP 2"); + lines.add("MOV R0 1"); + lines.add("RET R0"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 0); + } + + /* ========================================================================== */ + + + /* ================================ For EC14 ================================ */ + // There are 1 test cases generated from EC14 by guideline 2. + // Boundary: For some JMP x and n_pc which is updated ‘pc’ after executing this JMP, + // {JMP x, n_pc | x is int and x < 0 and n_pc < 0} + + // 1. Off point for x < 0: -1 + // Off point for n_pc < 0 : -1 + // The Machine's expected behaviour is to raise a NoReturnValueException. + @Test(expected = NoReturnValueException.class) + public void EC14OffPointTest() + throws Throwable + { + List<String> lines = new ArrayList<String>(); + lines.add("JMP -1"); + lines.add("MOV R0 1"); + lines.add("RET R0"); + Machine m = new Machine(); + m.execute(lines); + } + /* ========================================================================== */ + + + /* ================================ For EC15 ================================ */ + // There are 2 test cases generated from EC15 by guideline 2&4. + // Boundary: For some JMP x and n_pc which is updated ‘pc’ after executing this JMP, + // {JMP x, n_pc | x is int and x > 1 and n_pc <= progLength} + + // 1. On point for x > 1: 1 + // On point for n_pc <= progLength : progLength + // The Machine's expected behaviour is to return R0 value. + @Test public void EC15XOnNpcOnTest() + { + String err_msg = "EC15 on point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 1"); + lines.add("JMP 1"); + lines.add("RET R0"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 1); + } + + // 1. Off point for x > 1: 2 + // Off point for n_pc <= progLength : progLength + 1 + // The Machine's expected behaviour is to raise a NoReturnValueException. + @Test(expected = NoReturnValueException.class) + public void EC15XOffNpcOffTest() + throws Throwable + { + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 1"); + lines.add("JMP 2"); + lines.add("RET R0"); + Machine m = new Machine(); + m.execute(lines); } - return lines; + /* ========================================================================== */ + + + /* ================================ For EC17 ================================ */ + // There are 3 test cases generated from EC17 by guideline 2&4. + // Boundary: {JZ Ra val | Ra is int and Ra != 0} + + // 1. On point: 0 + // The Machine's expected behaviour is to return R1 default value. + @Test public void EC17OnPointTest() + { + String err_msg = "EC17 on point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("JZ R0 2"); + lines.add("MOV R1 1"); + lines.add("RET R1"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 0); + } + + // 1. Off point: -1 + // The Machine's expected behaviour is to return R0 value. + @Test public void EC17NegOffPointTest() + { + String err_msg = "EC17 negative off point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 -1"); + lines.add("JZ R0 2"); + lines.add("RET R0"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), -1); + } + + // 1. On point: 1 + // The Machine's expected behaviour is to return R0 value. + @Test public void EC17PosOffPointTest() + { + String err_msg = "EC17 positive off point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 1"); + lines.add("JZ R0 2"); + lines.add("RET R0"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 1); + } + /* ========================================================================== */ + + + /* ================================ For EC18 ================================ */ + // There are 6 test cases generated from EC18 by guideline 1&2. + // Boundary: For some n_pc which is updated ‘pc’ after executing this JZ, + // {JZ Ra val, n_pc | Ra is int and Ra = 0 and 0 <= n_pc <= progLength} + + // 1. Off point for Ra: 0 + // Off point for n_pc: -1 + // The Machine's expected behaviour is to raise a NoReturnValueException. + @Test(expected = NoReturnValueException.class) + public void EC18RaOnNpcNegOneTest() + throws Throwable + { + List<String> lines = new ArrayList<String>(); + lines.add("JZ R0 -1"); + lines.add("MOV R0 1"); + lines.add("RET R0"); + Machine m = new Machine(); + m.execute(lines); + } + + // 2. On point for Ra: 0 + // Off point for n_pc: progLength + 1 + // The Machine's expected behaviour is to raise a NoReturnValueException. + @Test (expected = NoReturnValueException.class) + public void EC18RaOnNpcProLenPlusOneTest() + throws Throwable + { + List<String> lines = new ArrayList<String>(); + lines.add("JZ R0 3"); + lines.add("MOV R0 1"); + lines.add("RET R0"); + Machine m = new Machine(); + m.execute(lines); + } + + // 3. Off point for Ra: -1 + // Off point for n_pc: -1 + // The Machine's expected behaviour is to return R0 value. + @Test public void EC18RaNegOffNpcNegOneTest() + { + String err_msg = "EC18 'Ra' negative off point test case fails with 'n_pc' off point -1."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 -1"); + lines.add("JZ R0 -2"); + lines.add("RET R0"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), -1); + } + + // 4. Off point for Ra: -1 + // Off point for n_pc: progLength + // The Machine's expected behaviour is to return R0 value. + @Test public void EC18RaNegOffNpcProLenTest() + { + String err_msg = "EC18 'Ra' negative off point test case fails with 'n_pc' off point progLength."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 -1"); + lines.add("JZ R0 1"); + lines.add("RET R0"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), -1); + } + + // 5. Off point for Ra: 1 + // Off point for n_pc: -1 + // The Machine's expected behaviour is to return R0 value. + @Test public void EC18RaPosOffNpcNegOneTest() + { + String err_msg = "EC18 'Ra' positive off point test case fails with 'n_pc' off point -1."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 1"); + lines.add("JZ R0 -2"); + lines.add("RET R0"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 1); + } + + // 6. Off point for Ra: 1 + // Off point for n_pc: progLen + // The Machine's expected behaviour is to return R0 value. + @Test public void EC18RaPosOffNpcProLenTest() + { + String err_msg = "EC18 'Ra' positive off point test case fails with 'n_pc' off point progLength."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 1"); + lines.add("JZ R0 1"); + lines.add("RET R0"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 1); + } + /* ========================================================================== */ + + + /* ================================ For EC21 ================================ */ + // There are 3 test cases generated from EC21 by guideline 1. + // Boundary: {DIV Ra Rb Rc | Rc = 0} + + // 1. On point: 1 + // The Machine's expected behaviour is to return R2 default value. + @Test public void EC21OnPointTest() + { + String err_msg = "EC21 on point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R1 1"); + lines.add("DIV R2 R1 R0"); + lines.add("RET R2"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 0); + } + + // 2. Off point: 1 + // The Machine's expected behaviour is to return R2 value. + @Test public void EC21PosOffPointTest() + { + String err_msg = "EC21 positive off point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R1 1"); + lines.add("MOV R0 1"); + lines.add("DIV R2 R1 R0"); + lines.add("RET R2"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 1); + } + + // 3. Off point: -1 + // The Machine's expected behaviour is to return R2 value. + @Test public void EC21NegOffPointTest() + { + String err_msg = "EC21 negative off point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R1 1"); + lines.add("MOV R0 -1"); + lines.add("DIV R2 R1 R0"); + lines.add("RET R2"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), -1); + } + /* ========================================================================== */ + + + /* ================================ For EC23 ================================ */ + // There are 2 test cases generated from EC23 by guideline 2. + // Boundary: {STR Rb val Ra | Rb + val > 65535} + + // 1. On point: 65535 + // The Machine's expected behaviour is to return R2 value. + @Test public void EC23OnPointTest() + { + String err_msg = "EC23 on point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 65535"); + lines.add("MOV R1 1"); + lines.add("STR R0 0 R1"); + lines.add("LDR R2 R0 0"); + lines.add("RET R2"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 1); + } + + // 2. Off point: 65536 + // The Machine's expected behaviour is to return R2 default value. + @Test public void EC23OffPointTest() + { + String err_msg = "EC23 off point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R0 65535"); + lines.add("MOV R1 1"); + lines.add("STR R0 1 R1"); + lines.add("LDR R2 R0 1"); + lines.add("RET R2"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 0); + } + /* ========================================================================== */ + + + /* ================================ For EC24 ================================ */ + // There are 2 test cases generated from EC24 by guideline 2. + // Boundary: {STR Rb val Ra | Rb + val < 0} + + // 1. On point: 0 + // The Machine's expected behaviour is to return R2 value. + @Test public void EC24OnPointTest() + { + String err_msg = "EC24 on point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R1 1"); + lines.add("STR R0 0 R1"); + lines.add("LDR R2 R0 0"); + lines.add("RET R2"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 1); + } + + // 2. Off point: -1 + // The Machine's expected behaviour is to return R2 default value. + @Test public void EC24OffPointTest() + { + String err_msg = "EC24 off point test case fails."; + List<String> lines = new ArrayList<String>(); + lines.add("MOV R1 1"); + lines.add("STR R0 -1 R1"); + lines.add("LDR R2 R0 -1"); + lines.add("RET R2"); + Machine m = new Machine(); + assertEquals(err_msg, m.execute(lines), 0); } + /* ========================================================================== */ } +/*---------------------------- END of Boundary Tests ---------------------------*/