Skip to content
Snippets Groups Projects
Commit f73758a1 authored by Guoxin Xuan's avatar Guoxin Xuan
Browse files

Update PartitioningTests.java

parent e9df6264
No related branches found
No related tags found
No related merge requests found
......@@ -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()
{
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 EC2Test()
{
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
public void EC3Test()
{
String a ="RET R1;";
final List<String> lines =new ArrayList<String>();
lines.add(a);
Machine m = new Machine();
assertEquals(m.execute(lines), 0);
}
@Test (expected= InvalidInstructionException.class)
public void EC4Test()
{
String a ="RET R-1;";
final List<String> lines =new ArrayList<String>();
lines.add(a);
Machine m = new Machine();
m.execute(lines);
}
@Test
public void EC5Test()
{
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);
}
@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()
{
//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 ="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 public void anotherTest()
@Test (expected= InvalidInstructionException.class)
public void EC15Test()
{
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
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);
//the assertTrue method is used to check whether something holds.
assertTrue(list.contains("a"));
}
//Test test opens a file and executes the machine
@Test public void aFileOpenTest()
@Test (expected= InvalidInstructionException.class)
public void EC16Test()
{
final List<String> lines = readInstructions("examples/array.s");
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();
assertEquals(m.execute(lines), 45);
m.execute(lines);
}
//To test an exception, specify the expected exception after the @Test
@Test(expected = java.io.IOException.class)
public void anExceptionTest()
throws Throwable
@Test
public void EC18Test()
{
throw new java.io.IOException();
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);
}
//This test should fail.
//To provide additional feedback when a test fails, an error message
//can be included
@Test public void aFailedTest()
@Test (expected= NoReturnValueException.class)
public void EC19Test()
{
//include a message for better feedback
final int expected = 2;
final int actual = 1 + 2;
assertEquals("Some failure message", expected, actual);
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);
}
//Read in a file containing a program and convert into a list of
//string instructions
private List<String> readInstructions(String file)
{
Charset charset = Charset.forName("UTF-8");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment