diff --git a/TEST-swen90006.machine.BoundaryTests.txt b/TEST-swen90006.machine.BoundaryTests.txt index 8e1f85f96ac02c33ff37256b894a8fdd1139fbf8..c340653a0beba3ed44972bd07a24cfe0a4fb8ce2 100644 --- a/TEST-swen90006.machine.BoundaryTests.txt +++ b/TEST-swen90006.machine.BoundaryTests.txt @@ -1,16 +1,31 @@ Testsuite: swen90006.machine.BoundaryTests -Tests run: 5, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.045 sec +Tests run: 18, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.35 sec -Testcase: anotherTest took 0.002 sec -Testcase: aFileOpenTest took 0.015 sec -Testcase: aFailedTest took 0.002 sec - FAILED -Some failure message expected:<2> but was:<3> -junit.framework.AssertionFailedError: Some failure message expected:<2> but was:<3> - at swen90006.machine.BoundaryTests.aFailedTest(Unknown Source) +Testcase: JmpNeg took 0.004 sec +Testcase: JzLast took 0.001 sec +Testcase: JzPast took 0.001 sec +Testcase: RiHigh took 0.001 sec +Testcase: StrNeg took 0.001 sec +Testcase: valueLow took 0.001 sec +Testcase: valueHigh took 0.001 sec +Testcase: StrHigh took 0.001 sec +Testcase: Div took 0.002 sec + Caused an ERROR +/ by zero +java.lang.ArithmeticException: / by zero + at swen90006.machine.Machine.do_div(Unknown Source) + at swen90006.machine.Machine.execute(Unknown Source) + at swen90006.machine.BoundaryTests.Div(Unknown Source) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) -Testcase: aTest took 0 sec -Testcase: anExceptionTest took 0.001 sec +Testcase: JzNeg took 0.001 sec +Testcase: RiNeg took 0.001 sec +Testcase: Valid took 0.004 sec +Testcase: JmpLast took 0.001 sec +Testcase: JmpPast took 0.001 sec +Testcase: JzFirst took 0.001 sec +Testcase: NoReturn took 0.002 sec +Testcase: RiValueHigh took 0.002 sec +Testcase: JmpFirst took 0.289 sec diff --git a/TEST-swen90006.machine.PartitioningTests.txt b/TEST-swen90006.machine.PartitioningTests.txt index 3f905918b96e507157c7e933d77a7dcd8c8d95bc..a63c54d32680034b8d3a7c295f1a9c908e612060 100644 --- a/TEST-swen90006.machine.PartitioningTests.txt +++ b/TEST-swen90006.machine.PartitioningTests.txt @@ -1,7 +1,17 @@ Testsuite: swen90006.machine.PartitioningTests -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.039 sec +Tests run: 14, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.068 sec -Testcase: anotherTest took 0.001 sec -Testcase: aFileOpenTest took 0.015 sec -Testcase: aTest took 0 sec -Testcase: anExceptionTest took 0 sec +Testcase: JmpNeg took 0.009 sec +Testcase: strHigh took 0.006 sec +Testcase: RiHigh took 0.001 sec +Testcase: ValueLow took 0.001 sec +Testcase: jzHigh took 0.005 sec +Testcase: strNeg took 0.002 sec +Testcase: Div took 0 sec +Testcase: RiNeg took 0.001 sec +Testcase: Valid took 0.004 sec +Testcase: jzNeg took 0.003 sec +Testcase: JmpHigh took 0.002 sec +Testcase: NoReturn took 0 sec +Testcase: RiMaxValue took 0.001 sec +Testcase: ValueHigh took 0 sec diff --git a/build.xml b/build.xml index 658996f9b1fa3d00605af37f0e167f0d8411ef3d..a297973a2c12f6237f87b4881acd53da93f492d1 100644 --- a/build.xml +++ b/build.xml @@ -1,7 +1,7 @@ <project name="Project" default="classes"> <target name="classes"> <mkdir dir="classes" /> - <javac srcdir="src:test" destdir="classes" + <javac srcdir="src:test" destdir="classes" classpath="lib/junit-4.11.jar;lib/hamcrest-core-1.3.jar" includeantruntime="false"/> </target> @@ -10,7 +10,7 @@ </target> <target name="run" depends="classes"> - <java classname="swen90006.machine.SimpleDriver" + <java classname="swen90006.machine.SimpleDriver" classpath="classes/;lib/junit-4.11.jar;lib/hamcrest-core-1.3.jar"> </java> </target> diff --git a/classes/swen90006/machine/BoundaryTests.class b/classes/swen90006/machine/BoundaryTests.class index 75ca15dbf4f906cd2b7beec235fa91a8de079579..9706ee5ecfa7f13e4e52ac056aa099fd5102bc90 100644 Binary files a/classes/swen90006/machine/BoundaryTests.class and b/classes/swen90006/machine/BoundaryTests.class differ diff --git a/classes/swen90006/machine/Machine.class b/classes/swen90006/machine/Machine.class index bc67cdff3d66160515599de443f4aea563326bac..7016ffa225790cdf3bdffe9762ca3de4168e1569 100644 Binary files a/classes/swen90006/machine/Machine.class and b/classes/swen90006/machine/Machine.class differ diff --git a/classes/swen90006/machine/PartitioningTests.class b/classes/swen90006/machine/PartitioningTests.class index f18ec47fe14f0f146b38fe5def6490170faa1be1..4310f50cb96220db21977273bfad9ca25cd6b386 100644 Binary files a/classes/swen90006/machine/PartitioningTests.class and b/classes/swen90006/machine/PartitioningTests.class differ diff --git a/examples/NoReturn.s b/examples/NoReturn.s new file mode 100644 index 0000000000000000000000000000000000000000..754bb156eef3ed3588b8b61d29a8c131c72b631f --- /dev/null +++ b/examples/NoReturn.s @@ -0,0 +1,2 @@ +MOV R0 1 +ADD R0 R0 R0 diff --git a/examples/RiHigh.s b/examples/RiHigh.s new file mode 100644 index 0000000000000000000000000000000000000000..ab167c7a0395a4a06e9bd22685b4c098573e9460 --- /dev/null +++ b/examples/RiHigh.s @@ -0,0 +1,4 @@ +MOV R0 0 +MOV R1 1 +ADD R32 R0 R1 ; Exception +RET 32 diff --git a/examples/RiNeg.s b/examples/RiNeg.s new file mode 100644 index 0000000000000000000000000000000000000000..8b21e652d4f92ff2d528916fcc7c6ae6171afb20 --- /dev/null +++ b/examples/RiNeg.s @@ -0,0 +1,4 @@ +MOV R0 0 +MOV R1 1 +MUL R-1 R0 R1 +RET R1 diff --git a/examples/RiValueHigh.s b/examples/RiValueHigh.s new file mode 100644 index 0000000000000000000000000000000000000000..6ac9bb2cbcd5d6a17765020fd2d65cc182bc1b1e --- /dev/null +++ b/examples/RiValueHigh.s @@ -0,0 +1,13 @@ + +MOV R1 46340; +MOV R2 296; +MOV R3 432 + +MUL R6 r1 r1 +MUL R2 r2 r2 +ADD R2 R2 R3 + +;Ri > 32-bit signed +Add r8 r6 r2 + +RET R8; diff --git a/examples/Valid.s b/examples/Valid.s new file mode 100644 index 0000000000000000000000000000000000000000..d89e8a6bae4877739bd0342da85b1de57429333e --- /dev/null +++ b/examples/Valid.s @@ -0,0 +1,103 @@ + +MOV R0 46340; +MOV R31 296; +MOV R3 431 + +MUL R6 R0 R0 +MUL R31 R31 R31 +ADD R31 R31 R3 + +;Ri > 32-bit signed +Add R8 R6 R31 ; -----------------------EC3 + + +;Checking simple math + + +MOV R1 65535; -----------------------EC6 +MOV R2 100 +MOV R3 -65535; -----------------------EC6 +MOV R6 12345 +ADD R4 R2 R3; R4 = 100 + -65535 +ADD R5 R3 R6; R4 = -65535 + 12345 + +ADD R5 R5 R4 ; R5 = -118625 +ADD R8 R5 R8 ;2147483647 + + +MOV R1 -1000 +MOV R2 123 +MOV R3 312 +SUB R5 R2 R1; R5 = 123 - -1000 +SUB R5 R5 R3; R5 = - ... - 312 + +ADD R8 R8 R5; 2147365833 + + +MOV R0 -2 +MOV R1 3 +MOV R4 212 +MUL R5 R1 R0 ; 3 * -2 +MUL R5 R5 R4; -6 * 212 + +ADD R8 R5 R8 ; 2147364561 + +MOV R0 -12 +MOV R1 200 +MOV R2 4 +DIV R5 R1 R0 ;200/-12 = -16 +DIV R5 R5 R2 ; -16/ + +ADD R8 R8 R5; 2147364557 + + + +;;testing STR and LDR +MOV R10 2018; +MOV R0 0 +STR R0 0 R10; -----------------------EC15 +LDR R1 R0 0; R1 = 2018 + +ADD R8 R8 R1; 2147366575 +;;testing STR and LDR +MOV R10 -63123; +MOV R0 65533; +MOV R2 65532 +STR R0 2 R10; memory[65535] -----------------------EC15 +LDR R1 R2 3; R1 = -63123 + +ADD R8 R8 R1; 2147303452 + + +;;testing STR and LDR with negative inputs +MOV R10 10; +MOV R0 -2; +MOV R2 -4 +STR R0 5 R10; memory[65535] +LDR R1 R2 7; R1 = 10 +ADD R8 R8 R1; 2147303462 + +;;testing STR and LDR with negative inputs +MOV R10 55; +MOV R0 7; +MOV R2 5 +STR R0 -4 R10; memory[65535] +LDR R1 R2 -2; R1 = 55 +ADD R8 R8 R1; 2147303517 + +;;testing STR and LDR +MOV R10 -212 +MOV R1 20 +MOV R0 65535; +ADD R0 R0 R1 +STR R0 -20 R10; memory[65535] +LDR R1 R0 -20; R1 = -212 +ADD R8 R8 R1; + + +MOV R0 -1 +MOV R1 1 +DIV R8 R8 R0 ; -2147303305 +DIV R8 R8 R1 + +RET R8; diff --git a/examples/div.s b/examples/div.s new file mode 100644 index 0000000000000000000000000000000000000000..c803095c8215b73633efd4ac4de752cd630cede4 --- /dev/null +++ b/examples/div.s @@ -0,0 +1,5 @@ +MOV R0 0 +MOV R1 10 +MOV R2 100 +DIV R2 R1 R0 +RET R2 diff --git a/examples/jmpFirst.s b/examples/jmpFirst.s new file mode 100644 index 0000000000000000000000000000000000000000..c47097c0ca4cec689005e8b2edd8032cb4115e01 --- /dev/null +++ b/examples/jmpFirst.s @@ -0,0 +1,7 @@ +MOV R6 10000; looping 10,000 times, sometimes causes random error +MOV R5 1; +ADD R0 R5 R5 +SUB R6 R6 R5 ; +JZ R6 2 ;{ +JMP -4 +RET R0 ; return R0 = 2; diff --git a/examples/jmpLast.s b/examples/jmpLast.s new file mode 100644 index 0000000000000000000000000000000000000000..b774cbd35f2d4aa4fa969a4fe00490a1340693a9 --- /dev/null +++ b/examples/jmpLast.s @@ -0,0 +1,8 @@ +MOV R6 2; +MOV R5 1; +MOV R0 100 +SUB R6 R6 R5 ; +JZ R6 2 ;{ +JMP 2 +ADD R0 R0 R5 +RET R0 ; return R6 = 100; diff --git a/examples/jmpNeg.s b/examples/jmpNeg.s new file mode 100644 index 0000000000000000000000000000000000000000..8ef9421a3eb2641b164c458cc71d013dc10a0556 --- /dev/null +++ b/examples/jmpNeg.s @@ -0,0 +1,7 @@ +MOV R6 2; +MOV R5 1; +ADD R0 R5 R5 +SUB R6 R6 R5 ; +JZ R6 2 ;{ +JMP -6 +RET R0 ; return R6 = -1; diff --git a/examples/jmpPast.s b/examples/jmpPast.s new file mode 100644 index 0000000000000000000000000000000000000000..b8a151fce215522865d8a56590d5f669e8776257 --- /dev/null +++ b/examples/jmpPast.s @@ -0,0 +1,8 @@ +MOV R6 2; +MOV R5 1; +MOV R0 100 +SUB R6 R6 R5 ; +JZ R6 2 ;{ +JMP 3 +ADD R0 R0 R5 +RET R0 ; return R6 = 100; diff --git a/examples/jzLast.s b/examples/jzLast.s new file mode 100644 index 0000000000000000000000000000000000000000..ede56283c5c28b8f62937c5e1a25cf32c38e1e2c --- /dev/null +++ b/examples/jzLast.s @@ -0,0 +1,7 @@ +MOV R6 1; +MOV R5 1; +MOV R0 100 +SUB R6 R6 R5 ; +JZ R6 2 ;{ +ADD R0 R0 R5 +RET R0 ; return R6 = -1; diff --git a/examples/jzNeg.s b/examples/jzNeg.s new file mode 100644 index 0000000000000000000000000000000000000000..c47c9fbddec07f9e2c8169e8a271c2aacdbe26eb --- /dev/null +++ b/examples/jzNeg.s @@ -0,0 +1,5 @@ +MOV R6 1; +MOV R5 1; +SUB R6 R6 R5 ; +JZ R6 -4 ;{ +RET R6 ; return R6 = -1; diff --git a/examples/jzPast.s b/examples/jzPast.s new file mode 100644 index 0000000000000000000000000000000000000000..a083e345b5e1da063a0410281de49029876309b1 --- /dev/null +++ b/examples/jzPast.s @@ -0,0 +1,7 @@ +MOV R6 1; +MOV R5 1; +MOV R0 100 +SUB R6 R6 R5 ; +JZ R6 3 ;{ +ADD R0 R0 R5 +RET R0 ; return R6 = -1; diff --git a/examples/jzfirst.s b/examples/jzfirst.s new file mode 100644 index 0000000000000000000000000000000000000000..7c3bb8cc1e9c2919aa529bf2f604cef0d68cfe30 --- /dev/null +++ b/examples/jzfirst.s @@ -0,0 +1,5 @@ +MOV R6 1; +MOV R5 1; +SUB R6 R6 R5 ; +JZ R6 -2 ;{ +RET R6 ; return R6 = -1; diff --git a/examples/partitionTests/RiHigh.s b/examples/partitionTests/RiHigh.s new file mode 100644 index 0000000000000000000000000000000000000000..1e9538886ff6b5bd7ed8237824181c3a6e70531a --- /dev/null +++ b/examples/partitionTests/RiHigh.s @@ -0,0 +1,9 @@ + +MOV R1 0; +MOV R2 1000 +MOV R3 -65535; + +;;Ri where i > 31 +ADD R32 R1 R3; + +RET R32; diff --git a/examples/partitionTests/RiMaxValue.s b/examples/partitionTests/RiMaxValue.s new file mode 100644 index 0000000000000000000000000000000000000000..6ac9bb2cbcd5d6a17765020fd2d65cc182bc1b1e --- /dev/null +++ b/examples/partitionTests/RiMaxValue.s @@ -0,0 +1,13 @@ + +MOV R1 46340; +MOV R2 296; +MOV R3 432 + +MUL R6 r1 r1 +MUL R2 r2 r2 +ADD R2 R2 R3 + +;Ri > 32-bit signed +Add r8 r6 r2 + +RET R8; diff --git a/examples/partitionTests/RiNeg.s b/examples/partitionTests/RiNeg.s new file mode 100644 index 0000000000000000000000000000000000000000..957fed65e2c25e07d85d914d00a635b55e63c41e --- /dev/null +++ b/examples/partitionTests/RiNeg.s @@ -0,0 +1,10 @@ + +MOV R1 0; +MOV R2 1000 +MOV R3 -65535; + +;Ri where i < 0 == -1 +ADD R-1 R1 R3; + + +RET R-1; diff --git a/examples/partitionTests/jmpHigh.s b/examples/partitionTests/jmpHigh.s new file mode 100644 index 0000000000000000000000000000000000000000..e720d61e355bac9efadfd4c6c280b38079278130 --- /dev/null +++ b/examples/partitionTests/jmpHigh.s @@ -0,0 +1,35 @@ +;;testing simple +MOV R1 65535; +MOV R2 100 +MOV R3 -65535; +ADD R4 R2 R3; +SUB R5 R4 R1; +MUL R6 R4 R5 +DIV R7 R6 R5; R7 = 152 + + + + +;;testing STR and LDR +MOV R10 50; +MOV R11 20 +STR R10 1 R11; +MOV R13 21 +LDR R12 R13 30; R12 = 20 + +ADD R13 R12 R7; R13 = 172 + + +;;testing loop jz and jmp +MOV R14 10; +MOV R15 1; +MOV R18 1; +MOV R17 1; +SUB R16 R14 R15 +JZ R16 4 +ADD R17 R17 R16 +ADD R15 R15 R18 +JMP 4 ;R17 = 46 + +ADD R19 R13 R17 ; 172 + 46 +RET R19 ; return sum; diff --git a/examples/partitionTests/jmpNeg.s b/examples/partitionTests/jmpNeg.s new file mode 100644 index 0000000000000000000000000000000000000000..92a65cbe1b30ba13bcb54ad2bfd637f21865e254 --- /dev/null +++ b/examples/partitionTests/jmpNeg.s @@ -0,0 +1,35 @@ +;;testing simple +MOV R1 65535; +MOV R2 100 +MOV R3 -65535; +ADD R4 R2 R3; +SUB R5 R4 R1; +MUL R6 R4 R5 +DIV R7 R6 R5; R7 = 152 + + + + +;;testing STR and LDR +MOV R10 50; +MOV R11 20 +STR R10 1 R11; +MOV R13 21 +LDR R12 R13 30; R12 = 20 + +ADD R13 R12 R7; R13 = 172 + + +;;testing loop jz and jmp +MOV R14 10; +MOV R15 1; +MOV R18 1; +MOV R17 1; +SUB R16 R14 R15 +JZ R16 4 +ADD R17 R17 R16 +ADD R15 R15 R18 +JMP -32 ;R17 = 46 + +ADD R19 R13 R17 ; 172 + 46 +RET R19 ; return sum; diff --git a/examples/partitionTests/jzHigh.s b/examples/partitionTests/jzHigh.s new file mode 100644 index 0000000000000000000000000000000000000000..93e38ad3625d91abc700ab3a9abb53a19f17ca27 --- /dev/null +++ b/examples/partitionTests/jzHigh.s @@ -0,0 +1,35 @@ +;;testing simple +MOV R1 65535; +MOV R2 100 +MOV R3 -65535; +ADD R4 R2 R3; +SUB R5 R4 R1; +MUL R6 R4 R5 +DIV R7 R6 R5; R7 = 152 + + + + +;;testing STR and LDR +MOV R10 50; +MOV R11 20 +STR R10 1 R11; +MOV R13 21 +LDR R12 R13 30; R12 = 20 + +ADD R13 R12 R7; R13 = 172 + + +;;testing loop jz and jmp +MOV R14 10; +MOV R15 1; +MOV R18 1; +MOV R17 1; +SUB R16 R14 R15 +JZ R16 7 +ADD R17 R17 R16 +ADD R15 R15 R18 +JMP -4 ;R17 = 46 + +ADD R19 R13 R17 ; 172 + 46 +RET R19 ; return sum; diff --git a/examples/partitionTests/jzNeg.s b/examples/partitionTests/jzNeg.s new file mode 100644 index 0000000000000000000000000000000000000000..a3312a822125686954b7759c9b6233810a85ae0e --- /dev/null +++ b/examples/partitionTests/jzNeg.s @@ -0,0 +1,35 @@ +;;testing simple +MOV R1 65535; +MOV R2 100 +MOV R3 -65535; +ADD R4 R2 R3; +SUB R5 R4 R1; +MUL R6 R4 R5 +DIV R7 R6 R5; R7 = 152 + + + + +;;testing STR and LDR +MOV R10 50; +MOV R11 20 +STR R10 1 R11; +MOV R13 21 +LDR R12 R13 30; R12 = 20 + +ADD R13 R12 R7; R13 = 172 + + +;;testing loop jz and jmp +MOV R14 10; +MOV R15 1; +MOV R18 1; +MOV R17 1; +SUB R16 R14 R15 +JZ R16 -29 +ADD R17 R17 R16 +ADD R15 R15 R18 +JMP -4 ;R17 = 46 + +ADD R19 R13 R17 ; 172 + 46 +RET R19 ; return sum; diff --git a/examples/partitionTests/noReturn.s b/examples/partitionTests/noReturn.s new file mode 100644 index 0000000000000000000000000000000000000000..754bb156eef3ed3588b8b61d29a8c131c72b631f --- /dev/null +++ b/examples/partitionTests/noReturn.s @@ -0,0 +1,2 @@ +MOV R0 1 +ADD R0 R0 R0 diff --git a/examples/partitionTests/strMaxMem.s b/examples/partitionTests/strMaxMem.s new file mode 100644 index 0000000000000000000000000000000000000000..cd0a03162e1e35081859d2cf1f4e617601dd54ae --- /dev/null +++ b/examples/partitionTests/strMaxMem.s @@ -0,0 +1,45 @@ +;;testing simple +MOV R1 65535; +MOV R2 100 +MOV R3 -65535; +;ADD R4 R2 R3; R4 = 100 + -65535 +;ADD R4 R4 R1; R4 = -65435 + 65535 +;SUB R5 R4 R1; R5 = 100 - 65535 +;SUB R5 R5 R3; R5 = -65435 - - 65535 + +;;testing simple +MOV R1 65535; +MOV R2 100 +MOV R3 -65535; +ADD R4 R2 R3; +SUB R5 R4 R1; +MUL R6 R4 R5 +DIV R7 R6 R5; R7 = 152 + + + + +;;testing STR and LDR +MOV R10 1; +ADD R20 R10 R1; +MOV R11 20 +STR R10 65535 R11; +MOV R13 21 +LDR R12 R10 65535; R12 = 20 + +ADD R13 R12 R7; R13 = 152 + + +;;testing loop jz and jmp +MOV R14 10; +MOV R15 1; +MOV R18 1; +MOV R17 1; +SUB R16 R14 R15 +JZ R16 4 +ADD R17 R17 R16 +ADD R15 R15 R18 +JMP -4 ;R17 = 46 + +ADD R19 R13 R17 ; 172 + 46 +RET R12 ; return sum; diff --git a/examples/partitionTests/strMemNeg.s b/examples/partitionTests/strMemNeg.s new file mode 100644 index 0000000000000000000000000000000000000000..ab56ec8ec70bb6be1af661a7c760202980faee30 --- /dev/null +++ b/examples/partitionTests/strMemNeg.s @@ -0,0 +1,42 @@ +;;testing simple +MOV R1 65535; +MOV R2 100 +MOV R3 -65535; +;ADD R4 R2 R3; R4 = 100 + -65535 +;ADD R4 R4 R1; R4 = -65435 + 65535 +;SUB R5 R4 R1; R5 = 100 - 65535 +;SUB R5 R5 R3; R5 = -65435 - - 65535 + +;;testing simple +MOV R1 65535; +MOV R2 100 +MOV R3 -65535; +ADD R4 R2 R3; +SUB R5 R4 R1; +MUL R6 R4 R5 +DIV R7 R6 R5; R7 = 152 + + + + +;;testing STR and LDR +MOV R10 -1; +STR R10 0 R10; ;;does nothing as b + v < -1 +LDR R12 0 R10; R12 = 0 + +ADD R13 R12 R7; R13 = 152 + + +;;testing loop jz and jmp +MOV R14 10; +MOV R15 1; +MOV R18 1; +MOV R17 1; +SUB R16 R14 R15 +JZ R16 4 +ADD R17 R17 R16 +ADD R15 R15 R18 +JMP -4 ;R17 = 46 + +ADD R19 R13 R17 ; 172 + 46 +RET R19 ; return sum; diff --git a/examples/partitionTests/validfile.s b/examples/partitionTests/validfile.s new file mode 100644 index 0000000000000000000000000000000000000000..6195e6a890bc7ef7bca86df726ec973179622551 --- /dev/null +++ b/examples/partitionTests/validfile.s @@ -0,0 +1,35 @@ +;;testing simple +MOV R1 65535; +MOV R2 100 +MOV R3 -65535; +ADD R4 R2 R3; +SUB R5 R4 R1; +MUL R6 R4 R5 +DIV R7 R6 R5; R7 = 152 + + + + +;;testing STR and LDR +MOV R10 50; +MOV R11 20 +STR R10 1 R11; momeory[51] = 20 +MOV R13 21 +LDR R12 R13 30; R12 = 20 + +ADD R13 R12 R7; R13 = 172 + + +;;testing loop jz and jmp +MOV R14 10; +MOV R15 1; +MOV R18 1; +MOV R17 1; +SUB R16 R14 R15 +JZ R16 4 +ADD R17 R17 R16 +ADD R15 R15 R18 +JMP -4 ;R17 = 46 + +ADD R19 R13 R17 ; 172 + 46 +RET R19 ; return sum; diff --git a/examples/partitionTests/valueHigh.s b/examples/partitionTests/valueHigh.s new file mode 100644 index 0000000000000000000000000000000000000000..722fbac4a1ed438c0131247ec3673e3492d85d85 --- /dev/null +++ b/examples/partitionTests/valueHigh.s @@ -0,0 +1,9 @@ +;value > 65535 +MOV R1 65536; +MOV R2 1000 +MOV R3 -65535; +ADD R4 R2 R3; +SUB R5 R4 R1; +MUL R6 R4 R5 +DIV R7 R6 R5 +RET R7; diff --git a/examples/partitionTests/valueLow.s b/examples/partitionTests/valueLow.s new file mode 100644 index 0000000000000000000000000000000000000000..a87c144ab9ea72bcd621ca2e3eeb79d71e9a49b5 --- /dev/null +++ b/examples/partitionTests/valueLow.s @@ -0,0 +1,12 @@ + +MOV R1 65535; +MOV R2 1000 + +;Value < 65535 +MOV R3 -65536; + +ADD R4 R2 R3; +SUB R5 R4 R1; +MUL R6 R4 R5 +DIV R7 R6 R5 +RET R7; diff --git a/examples/strHigh.s b/examples/strHigh.s new file mode 100644 index 0000000000000000000000000000000000000000..2071ecf27df0d09494d9e49272ffeb68fadc4592 --- /dev/null +++ b/examples/strHigh.s @@ -0,0 +1,6 @@ +;;testing STR and LDR +MOV R10 12; +MOV R0 65535; +STR R0 1 R10; memory[65536] = 12 +LDR R1 R0 1; R1 = 12 +RET R1; diff --git a/examples/strMax.s b/examples/strMax.s new file mode 100644 index 0000000000000000000000000000000000000000..7a1029f8b98ddaafb75b7cd84bf19a0db6de22fc --- /dev/null +++ b/examples/strMax.s @@ -0,0 +1,7 @@ +;;testing STR and LDR +MOV R10 12; +MOV R0 65533; +MOV R2 65532 +STR R0 2 R10; memory[65535] +LDR R1 R2 3; R1 = 12 +RET R1; diff --git a/examples/strNeg.s b/examples/strNeg.s new file mode 100644 index 0000000000000000000000000000000000000000..1ae399f07e57c8ec644627c7dfb822c5a25526f4 --- /dev/null +++ b/examples/strNeg.s @@ -0,0 +1,6 @@ +;;testing STR and LDR +MOV R10 12; +MOV R0 -1 +STR R0 0 R10; +LDR R1 R0 0; R1 = 12 +RET R1; diff --git a/examples/strZero.s b/examples/strZero.s new file mode 100644 index 0000000000000000000000000000000000000000..35132bb00f0eaecacaad6c844f26915a41e72990 --- /dev/null +++ b/examples/strZero.s @@ -0,0 +1,6 @@ +;;testing STR and LDR +MOV R10 12; +MOV R0 0 +STR R0 0 R10; +LDR R1 R0 0; R1 = 12 +RET R1; diff --git a/examples/valueHigh.s b/examples/valueHigh.s new file mode 100644 index 0000000000000000000000000000000000000000..9dd4be1f1516d75469fea86d396c2fd44e9cc604 --- /dev/null +++ b/examples/valueHigh.s @@ -0,0 +1,2 @@ +MOV R1 65536 ; Exception +RET R1 diff --git a/examples/valueLow.s b/examples/valueLow.s new file mode 100644 index 0000000000000000000000000000000000000000..293c45025bef3f718a9bba9271cd1430394e072f --- /dev/null +++ b/examples/valueLow.s @@ -0,0 +1,2 @@ +MOV R1 -65536 ; Exception +RET R1 diff --git a/mutants/mutant-1/swen90006/machine/Machine.java b/mutants/mutant-1/swen90006/machine/Machine.java index 9bf57316d1c736ea2bd1c62bd6df3c6c0282f331..6f5be6192d10121231f3d7a3d965e9c3f24a491d 100644 --- a/mutants/mutant-1/swen90006/machine/Machine.java +++ b/mutants/mutant-1/swen90006/machine/Machine.java @@ -3,7 +3,7 @@ package swen90006.machine; import java.util.Arrays; import java.util.List; -public class Machine +public class Machine { /** arithmetic instructions each take three registers as arguments with the * destination register appearing first @@ -12,13 +12,13 @@ public class Machine /** add rd rs1 rs2 =~ rd = rs1 + rs2 */ public static final String INSTRUCTION_ADD = "add"; - /** sub rd rs1 rs2 =~ rd = rs1 - rs2 */ + /** sub rd rs1 rs2 =~ rd = rs1 - rs2 */ public static final String INSTRUCTION_SUBTRACT = "sub"; - /** mul rd rs1 rs2 =~ rd = rs1 * rs2 */ + /** mul rd rs1 rs2 =~ rd = rs1 * rs2 */ public static final String INSTRUCTION_MULT = "mul"; - /** div rd rs1 rs2 =~ rd = rs1 / rs2 */ + /** div rd rs1 rs2 =~ rd = rs1 / rs2 */ public static final String INSTRUCTION_DIVIDE = "div"; /** ret rs =~ return rs */ @@ -38,17 +38,17 @@ public class Machine /** jz ra offs =~ if (ra == 0) pc = pc + offs else pc = pc + 1 */ public static final String INSTRUCTION_JZ = "jz"; - + public static final int NUM_REGS = 32; public static final int MAX_REG = (NUM_REGS - 1); public static final int MEMORY_SIZE = 65536; /* 4 x as much memory as a 64 */ public static final int MAX_ADDR = MEMORY_SIZE-1; - + private int[] memory; private int[] regs; private int count = 0; /* counts number of instructions executed so far */ - + public Machine() { memory = new int[MEMORY_SIZE]; @@ -60,17 +60,17 @@ public class Machine { regs[dest] = regs[src1] + regs[src2]; } - + private void do_sub(int dest, int src1, int src2) { regs[dest] = regs[src1] - regs[src2]; } - + private void do_mult(int dest, int src1, int src2) { regs[dest] = regs[src1] * regs[src2]; } - + private void do_div(int dest, int src1, int src2) { if (regs[src2] == 0){ @@ -86,7 +86,7 @@ public class Machine }else if(regs[src] + offs < 0){ /* no op */ }else{ - regs[dest] = memory[regs[src] + offs]; + regs[dest] = memory[Math.abs(regs[src]) + offs]; } } @@ -152,16 +152,16 @@ public class Machine throw new InvalidInstructionException(); } } - + /** Execute an assembly program. * - * @param prog is the program to execute as an iterable collection of strings, + * @param prog is the program to execute as an iterable collection of strings, * each of which is a single instruction. * @return the program's return value. - * @throws Exception when program has unrecognised or + * @throws Exception when program has unrecognised or * invalid instructions, or when it returns no result when it finishes */ - int execute(List<String> instructions) + int execute(List<String> instructions) throws InvalidInstructionException, NoReturnValueException { @@ -180,7 +180,7 @@ public class Machine break; } String inst = instructions.get(pc); - /* strip leading and trailing whitespace */ + /* strip leading and trailing whitespace */ inst = inst.toLowerCase().replaceAll("^\\s+","").replaceAll("\\s+$",""); /* strip out any comments */ String[] toks = inst.split(";"); diff --git a/mutants/mutant-2/swen90006/machine/Machine.java b/mutants/mutant-2/swen90006/machine/Machine.java index 9bf57316d1c736ea2bd1c62bd6df3c6c0282f331..68e57734b8ff5670f263f72011bafec88071b5a4 100644 --- a/mutants/mutant-2/swen90006/machine/Machine.java +++ b/mutants/mutant-2/swen90006/machine/Machine.java @@ -3,7 +3,7 @@ package swen90006.machine; import java.util.Arrays; import java.util.List; -public class Machine +public class Machine { /** arithmetic instructions each take three registers as arguments with the * destination register appearing first @@ -12,13 +12,13 @@ public class Machine /** add rd rs1 rs2 =~ rd = rs1 + rs2 */ public static final String INSTRUCTION_ADD = "add"; - /** sub rd rs1 rs2 =~ rd = rs1 - rs2 */ + /** sub rd rs1 rs2 =~ rd = rs1 - rs2 */ public static final String INSTRUCTION_SUBTRACT = "sub"; - /** mul rd rs1 rs2 =~ rd = rs1 * rs2 */ + /** mul rd rs1 rs2 =~ rd = rs1 * rs2 */ public static final String INSTRUCTION_MULT = "mul"; - /** div rd rs1 rs2 =~ rd = rs1 / rs2 */ + /** div rd rs1 rs2 =~ rd = rs1 / rs2 */ public static final String INSTRUCTION_DIVIDE = "div"; /** ret rs =~ return rs */ @@ -38,17 +38,17 @@ public class Machine /** jz ra offs =~ if (ra == 0) pc = pc + offs else pc = pc + 1 */ public static final String INSTRUCTION_JZ = "jz"; - + public static final int NUM_REGS = 32; public static final int MAX_REG = (NUM_REGS - 1); public static final int MEMORY_SIZE = 65536; /* 4 x as much memory as a 64 */ public static final int MAX_ADDR = MEMORY_SIZE-1; - + private int[] memory; private int[] regs; private int count = 0; /* counts number of instructions executed so far */ - + public Machine() { memory = new int[MEMORY_SIZE]; @@ -60,17 +60,17 @@ public class Machine { regs[dest] = regs[src1] + regs[src2]; } - + private void do_sub(int dest, int src1, int src2) { regs[dest] = regs[src1] - regs[src2]; } - + private void do_mult(int dest, int src1, int src2) { regs[dest] = regs[src1] * regs[src2]; } - + private void do_div(int dest, int src1, int src2) { if (regs[src2] == 0){ @@ -81,7 +81,7 @@ public class Machine } private void do_load(int dest, int src, int offs) { - if (regs[src] + offs > MAX_ADDR){ + if (regs[src] + Math.abs(offs) > MAX_ADDR){ /* no op */ }else if(regs[src] + offs < 0){ /* no op */ @@ -152,16 +152,16 @@ public class Machine throw new InvalidInstructionException(); } } - + /** Execute an assembly program. * - * @param prog is the program to execute as an iterable collection of strings, + * @param prog is the program to execute as an iterable collection of strings, * each of which is a single instruction. * @return the program's return value. - * @throws Exception when program has unrecognised or + * @throws Exception when program has unrecognised or * invalid instructions, or when it returns no result when it finishes */ - int execute(List<String> instructions) + int execute(List<String> instructions) throws InvalidInstructionException, NoReturnValueException { @@ -180,7 +180,7 @@ public class Machine break; } String inst = instructions.get(pc); - /* strip leading and trailing whitespace */ + /* strip leading and trailing whitespace */ inst = inst.toLowerCase().replaceAll("^\\s+","").replaceAll("\\s+$",""); /* strip out any comments */ String[] toks = inst.split(";"); diff --git a/mutants/mutant-3/swen90006/machine/Machine.java b/mutants/mutant-3/swen90006/machine/Machine.java index 9bf57316d1c736ea2bd1c62bd6df3c6c0282f331..9c4a583d3da909baeb17dcb0eaa3891d839a9a4e 100644 --- a/mutants/mutant-3/swen90006/machine/Machine.java +++ b/mutants/mutant-3/swen90006/machine/Machine.java @@ -3,7 +3,7 @@ package swen90006.machine; import java.util.Arrays; import java.util.List; -public class Machine +public class Machine { /** arithmetic instructions each take three registers as arguments with the * destination register appearing first @@ -12,13 +12,13 @@ public class Machine /** add rd rs1 rs2 =~ rd = rs1 + rs2 */ public static final String INSTRUCTION_ADD = "add"; - /** sub rd rs1 rs2 =~ rd = rs1 - rs2 */ + /** sub rd rs1 rs2 =~ rd = rs1 - rs2 */ public static final String INSTRUCTION_SUBTRACT = "sub"; - /** mul rd rs1 rs2 =~ rd = rs1 * rs2 */ + /** mul rd rs1 rs2 =~ rd = rs1 * rs2 */ public static final String INSTRUCTION_MULT = "mul"; - /** div rd rs1 rs2 =~ rd = rs1 / rs2 */ + /** div rd rs1 rs2 =~ rd = rs1 / rs2 */ public static final String INSTRUCTION_DIVIDE = "div"; /** ret rs =~ return rs */ @@ -38,17 +38,17 @@ public class Machine /** jz ra offs =~ if (ra == 0) pc = pc + offs else pc = pc + 1 */ public static final String INSTRUCTION_JZ = "jz"; - + public static final int NUM_REGS = 32; public static final int MAX_REG = (NUM_REGS - 1); public static final int MEMORY_SIZE = 65536; /* 4 x as much memory as a 64 */ public static final int MAX_ADDR = MEMORY_SIZE-1; - + private int[] memory; private int[] regs; private int count = 0; /* counts number of instructions executed so far */ - + public Machine() { memory = new int[MEMORY_SIZE]; @@ -60,17 +60,17 @@ public class Machine { regs[dest] = regs[src1] + regs[src2]; } - + private void do_sub(int dest, int src1, int src2) { regs[dest] = regs[src1] - regs[src2]; } - + private void do_mult(int dest, int src1, int src2) { regs[dest] = regs[src1] * regs[src2]; } - + private void do_div(int dest, int src1, int src2) { if (regs[src2] == 0){ @@ -91,7 +91,7 @@ public class Machine } private void do_store(int a, int offs, int b) { - if (regs[a] + offs > MAX_ADDR){ + if (regs[a] + Math.abs(offs) > MAX_ADDR){ /* no op */ }else if(regs[a] + offs < 0){ /* no op */ @@ -152,16 +152,16 @@ public class Machine throw new InvalidInstructionException(); } } - + /** Execute an assembly program. * - * @param prog is the program to execute as an iterable collection of strings, + * @param prog is the program to execute as an iterable collection of strings, * each of which is a single instruction. * @return the program's return value. - * @throws Exception when program has unrecognised or + * @throws Exception when program has unrecognised or * invalid instructions, or when it returns no result when it finishes */ - int execute(List<String> instructions) + int execute(List<String> instructions) throws InvalidInstructionException, NoReturnValueException { @@ -180,7 +180,7 @@ public class Machine break; } String inst = instructions.get(pc); - /* strip leading and trailing whitespace */ + /* strip leading and trailing whitespace */ inst = inst.toLowerCase().replaceAll("^\\s+","").replaceAll("\\s+$",""); /* strip out any comments */ String[] toks = inst.split(";"); diff --git a/mutants/mutant-4/swen90006/machine/Machine.java b/mutants/mutant-4/swen90006/machine/Machine.java index 9bf57316d1c736ea2bd1c62bd6df3c6c0282f331..53bdfe9fe7ce467f719e5b0dfd587df621387905 100644 --- a/mutants/mutant-4/swen90006/machine/Machine.java +++ b/mutants/mutant-4/swen90006/machine/Machine.java @@ -3,7 +3,7 @@ package swen90006.machine; import java.util.Arrays; import java.util.List; -public class Machine +public class Machine { /** arithmetic instructions each take three registers as arguments with the * destination register appearing first @@ -12,13 +12,13 @@ public class Machine /** add rd rs1 rs2 =~ rd = rs1 + rs2 */ public static final String INSTRUCTION_ADD = "add"; - /** sub rd rs1 rs2 =~ rd = rs1 - rs2 */ + /** sub rd rs1 rs2 =~ rd = rs1 - rs2 */ public static final String INSTRUCTION_SUBTRACT = "sub"; - /** mul rd rs1 rs2 =~ rd = rs1 * rs2 */ + /** mul rd rs1 rs2 =~ rd = rs1 * rs2 */ public static final String INSTRUCTION_MULT = "mul"; - /** div rd rs1 rs2 =~ rd = rs1 / rs2 */ + /** div rd rs1 rs2 =~ rd = rs1 / rs2 */ public static final String INSTRUCTION_DIVIDE = "div"; /** ret rs =~ return rs */ @@ -38,17 +38,17 @@ public class Machine /** jz ra offs =~ if (ra == 0) pc = pc + offs else pc = pc + 1 */ public static final String INSTRUCTION_JZ = "jz"; - + public static final int NUM_REGS = 32; public static final int MAX_REG = (NUM_REGS - 1); public static final int MEMORY_SIZE = 65536; /* 4 x as much memory as a 64 */ public static final int MAX_ADDR = MEMORY_SIZE-1; - + private int[] memory; private int[] regs; private int count = 0; /* counts number of instructions executed so far */ - + public Machine() { memory = new int[MEMORY_SIZE]; @@ -60,17 +60,17 @@ public class Machine { regs[dest] = regs[src1] + regs[src2]; } - + private void do_sub(int dest, int src1, int src2) { regs[dest] = regs[src1] - regs[src2]; } - + private void do_mult(int dest, int src1, int src2) { regs[dest] = regs[src1] * regs[src2]; } - + private void do_div(int dest, int src1, int src2) { if (regs[src2] == 0){ @@ -96,7 +96,7 @@ public class Machine }else if(regs[a] + offs < 0){ /* no op */ }else{ - memory[regs[a] + offs] = regs[b]; + memory[Math.abs(regs[a]) + offs] = regs[b]; } } @@ -152,16 +152,16 @@ public class Machine throw new InvalidInstructionException(); } } - + /** Execute an assembly program. * - * @param prog is the program to execute as an iterable collection of strings, + * @param prog is the program to execute as an iterable collection of strings, * each of which is a single instruction. * @return the program's return value. - * @throws Exception when program has unrecognised or + * @throws Exception when program has unrecognised or * invalid instructions, or when it returns no result when it finishes */ - int execute(List<String> instructions) + int execute(List<String> instructions) throws InvalidInstructionException, NoReturnValueException { @@ -180,7 +180,7 @@ public class Machine break; } String inst = instructions.get(pc); - /* strip leading and trailing whitespace */ + /* strip leading and trailing whitespace */ inst = inst.toLowerCase().replaceAll("^\\s+","").replaceAll("\\s+$",""); /* strip out any comments */ String[] toks = inst.split(";"); diff --git a/mutants/mutant-5/swen90006/machine/Machine.java b/mutants/mutant-5/swen90006/machine/Machine.java index 9bf57316d1c736ea2bd1c62bd6df3c6c0282f331..4714745e67346f5beec257a8d6d004e50f7a4332 100644 --- a/mutants/mutant-5/swen90006/machine/Machine.java +++ b/mutants/mutant-5/swen90006/machine/Machine.java @@ -3,7 +3,7 @@ package swen90006.machine; import java.util.Arrays; import java.util.List; -public class Machine +public class Machine { /** arithmetic instructions each take three registers as arguments with the * destination register appearing first @@ -12,13 +12,13 @@ public class Machine /** add rd rs1 rs2 =~ rd = rs1 + rs2 */ public static final String INSTRUCTION_ADD = "add"; - /** sub rd rs1 rs2 =~ rd = rs1 - rs2 */ + /** sub rd rs1 rs2 =~ rd = rs1 - rs2 */ public static final String INSTRUCTION_SUBTRACT = "sub"; - /** mul rd rs1 rs2 =~ rd = rs1 * rs2 */ + /** mul rd rs1 rs2 =~ rd = rs1 * rs2 */ public static final String INSTRUCTION_MULT = "mul"; - /** div rd rs1 rs2 =~ rd = rs1 / rs2 */ + /** div rd rs1 rs2 =~ rd = rs1 / rs2 */ public static final String INSTRUCTION_DIVIDE = "div"; /** ret rs =~ return rs */ @@ -38,17 +38,17 @@ public class Machine /** jz ra offs =~ if (ra == 0) pc = pc + offs else pc = pc + 1 */ public static final String INSTRUCTION_JZ = "jz"; - + public static final int NUM_REGS = 32; public static final int MAX_REG = (NUM_REGS - 1); public static final int MEMORY_SIZE = 65536; /* 4 x as much memory as a 64 */ public static final int MAX_ADDR = MEMORY_SIZE-1; - + private int[] memory; private int[] regs; private int count = 0; /* counts number of instructions executed so far */ - + public Machine() { memory = new int[MEMORY_SIZE]; @@ -60,20 +60,20 @@ public class Machine { regs[dest] = regs[src1] + regs[src2]; } - + private void do_sub(int dest, int src1, int src2) { regs[dest] = regs[src1] - regs[src2]; } - + private void do_mult(int dest, int src1, int src2) { regs[dest] = regs[src1] * regs[src2]; } - + private void do_div(int dest, int src1, int src2) { - if (regs[src2] == 0){ + if (regs[src2] == 921231){ /* no op */ }else{ regs[dest] = regs[src1] / regs[src2]; @@ -152,16 +152,16 @@ public class Machine throw new InvalidInstructionException(); } } - + /** Execute an assembly program. * - * @param prog is the program to execute as an iterable collection of strings, + * @param prog is the program to execute as an iterable collection of strings, * each of which is a single instruction. * @return the program's return value. - * @throws Exception when program has unrecognised or + * @throws Exception when program has unrecognised or * invalid instructions, or when it returns no result when it finishes */ - int execute(List<String> instructions) + int execute(List<String> instructions) throws InvalidInstructionException, NoReturnValueException { @@ -180,7 +180,7 @@ public class Machine break; } String inst = instructions.get(pc); - /* strip leading and trailing whitespace */ + /* strip leading and trailing whitespace */ inst = inst.toLowerCase().replaceAll("^\\s+","").replaceAll("\\s+$",""); /* strip out any comments */ String[] toks = inst.split(";"); diff --git a/src/swen90006/machine/Machine.java b/src/swen90006/machine/Machine.java index 9bf57316d1c736ea2bd1c62bd6df3c6c0282f331..61c7bf885ef13a818f241b2627a49c206f74ee51 100644 --- a/src/swen90006/machine/Machine.java +++ b/src/swen90006/machine/Machine.java @@ -3,7 +3,7 @@ package swen90006.machine; import java.util.Arrays; import java.util.List; -public class Machine +public class Machine { /** arithmetic instructions each take three registers as arguments with the * destination register appearing first @@ -12,13 +12,13 @@ public class Machine /** add rd rs1 rs2 =~ rd = rs1 + rs2 */ public static final String INSTRUCTION_ADD = "add"; - /** sub rd rs1 rs2 =~ rd = rs1 - rs2 */ + /** sub rd rs1 rs2 =~ rd = rs1 - rs2 */ public static final String INSTRUCTION_SUBTRACT = "sub"; - /** mul rd rs1 rs2 =~ rd = rs1 * rs2 */ + /** mul rd rs1 rs2 =~ rd = rs1 * rs2 */ public static final String INSTRUCTION_MULT = "mul"; - /** div rd rs1 rs2 =~ rd = rs1 / rs2 */ + /** div rd rs1 rs2 =~ rd = rs1 / rs2 */ public static final String INSTRUCTION_DIVIDE = "div"; /** ret rs =~ return rs */ @@ -38,17 +38,17 @@ public class Machine /** jz ra offs =~ if (ra == 0) pc = pc + offs else pc = pc + 1 */ public static final String INSTRUCTION_JZ = "jz"; - + public static final int NUM_REGS = 32; public static final int MAX_REG = (NUM_REGS - 1); public static final int MEMORY_SIZE = 65536; /* 4 x as much memory as a 64 */ public static final int MAX_ADDR = MEMORY_SIZE-1; - + private int[] memory; private int[] regs; private int count = 0; /* counts number of instructions executed so far */ - + public Machine() { memory = new int[MEMORY_SIZE]; @@ -60,20 +60,20 @@ public class Machine { regs[dest] = regs[src1] + regs[src2]; } - + private void do_sub(int dest, int src1, int src2) { regs[dest] = regs[src1] - regs[src2]; } - + private void do_mult(int dest, int src1, int src2) { regs[dest] = regs[src1] * regs[src2]; } - + private void do_div(int dest, int src1, int src2) { - if (regs[src2] == 0){ + if (regs[src2] == 100){ /* no op */ }else{ regs[dest] = regs[src1] / regs[src2]; @@ -152,20 +152,19 @@ public class Machine throw new InvalidInstructionException(); } } - + /** Execute an assembly program. * - * @param prog is the program to execute as an iterable collection of strings, + * @param prog is the program to execute as an iterable collection of strings, * each of which is a single instruction. * @return the program's return value. - * @throws Exception when program has unrecognised or + * @throws Exception when program has unrecognised or * invalid instructions, or when it returns no result when it finishes */ - int execute(List<String> instructions) + int execute(List<String> instructions) throws InvalidInstructionException, NoReturnValueException { - int instructionsExecuted = 0; int pc = 0; final int progLength = instructions.size(); @@ -180,7 +179,7 @@ public class Machine break; } String inst = instructions.get(pc); - /* strip leading and trailing whitespace */ + /* strip leading and trailing whitespace */ inst = inst.toLowerCase().replaceAll("^\\s+","").replaceAll("\\s+$",""); /* strip out any comments */ String[] toks = inst.split(";"); @@ -188,9 +187,9 @@ public class Machine /* check for blank lines */ if (inst.equals("")){ - pc = pc + 1; - count++; - continue; + pc = pc + 1; + count++; + continue; } instructionsExecuted++; @@ -199,100 +198,111 @@ public class Machine /* check minimum number of tokens */ if (toks.length < 2){ - throw new InvalidInstructionException(); + throw new InvalidInstructionException(); } if (toks[0].equals(INSTRUCTION_ADD)){ - if (toks.length != 4){ - throw new InvalidInstructionException(); - } - int rd = parseReg(toks[1]); - int rs1 = parseReg(toks[2]); - int rs2 = parseReg(toks[3]); - do_add(rd,rs1,rs2); - } else if (toks[0].equals(INSTRUCTION_SUBTRACT)){ - if (toks.length != 4){ - throw new InvalidInstructionException(); - } - int rd = parseReg(toks[1]); - int rs1 = parseReg(toks[2]); - int rs2 = parseReg(toks[3]); - do_sub(rd,rs1,rs2); - } else if (toks[0].equals(INSTRUCTION_MULT)){ - if (toks.length != 4){ - throw new InvalidInstructionException(); - } - int rd = parseReg(toks[1]); - int rs1 = parseReg(toks[2]); - int rs2 = parseReg(toks[3]); - do_mult(rd,rs1,rs2); - } else if (toks[0].equals(INSTRUCTION_DIVIDE)){ - if (toks.length != 4){ - throw new InvalidInstructionException(); - } - int rd = parseReg(toks[1]); - int rs1 = parseReg(toks[2]); - int rs2 = parseReg(toks[3]); - do_div(rd,rs1,rs2); - } else if (toks[0].equals(INSTRUCTION_RETURN)){ - int rs = parseReg(toks[1]); - count++; - return regs[rs]; - } else if (toks[0].equals(INSTRUCTION_LOAD)){ - if (toks.length != 4){ - throw new InvalidInstructionException(); - } - int rd = parseReg(toks[1]); - int rs = parseReg(toks[2]); - int offs = parseOffset(toks[3]); - do_load(rd,rs,offs); - } else if (toks[0].equals(INSTRUCTION_STORE)){ - if (toks.length != 4){ - throw new InvalidInstructionException(); - } - int ra = parseReg(toks[1]); - int offs = parseOffset(toks[2]); - int rb = parseReg(toks[3]); - do_store(ra,offs,rb); - } else if (toks[0].equals(INSTRUCTION_MOVE)){ - if (toks.length != 3){ - throw new InvalidInstructionException(); - } - int rd = parseReg(toks[1]); - int offs = parseOffset(toks[2]); - do_move(rd,offs); - } else if (toks[0].equals(INSTRUCTION_JUMP)){ - if (toks.length != 2){ - throw new InvalidInstructionException(); - } - int offs = parseOffset(toks[1]); - pc = pc + offs; - count++; - continue; /* avoid default increment of pc below */ - } else if (toks[0].equals(INSTRUCTION_JZ)){ - if (toks.length != 3){ - throw new InvalidInstructionException(); - } - int ra = parseReg(toks[1]); - int offs = parseOffset(toks[2]); - if (regs[ra] == 0){ - pc = pc + offs; - }else{ - pc = pc + 1; - } - count++; - continue; /* avoid default increment the pc below */ - } else { - System.err.println("Unrecognised instruction: " + inst); - throw new InvalidInstructionException(); + if (toks.length != 4){ + throw new InvalidInstructionException(); + } + int rd = parseReg(toks[1]); + int rs1 = parseReg(toks[2]); + int rs2 = parseReg(toks[3]); + do_add(rd,rs1,rs2); } - count++; - pc = pc + 1; + else if (toks[0].equals(INSTRUCTION_SUBTRACT)){ + if (toks.length != 4){ + throw new InvalidInstructionException(); + } + int rd = parseReg(toks[1]); + int rs1 = parseReg(toks[2]); + int rs2 = parseReg(toks[3]); + do_sub(rd,rs1,rs2); + } + else if (toks[0].equals(INSTRUCTION_MULT)){ + if (toks.length != 4){ + throw new InvalidInstructionException(); + } + int rd = parseReg(toks[1]); + int rs1 = parseReg(toks[2]); + int rs2 = parseReg(toks[3]); + do_mult(rd,rs1,rs2); + } + else if (toks[0].equals(INSTRUCTION_DIVIDE)){ + if (toks.length != 4){ + throw new InvalidInstructionException(); + } + int rd = parseReg(toks[1]); + int rs1 = parseReg(toks[2]); + int rs2 = parseReg(toks[3]); + do_div(rd,rs1,rs2); + } + else if (toks[0].equals(INSTRUCTION_RETURN)){ + int rs = parseReg(toks[1]); + count++; + return regs[rs]; } + else if (toks[0].equals(INSTRUCTION_LOAD)){ + if (toks.length != 4){ + throw new InvalidInstructionException(); + } + int rd = parseReg(toks[1]); + int rs = parseReg(toks[2]); + int offs = parseOffset(toks[3]); + do_load(rd,rs,offs); + } + else if (toks[0].equals(INSTRUCTION_STORE)){ + if (toks.length != 4){ + throw new InvalidInstructionException(); + } + int ra = parseReg(toks[1]); + int offs = parseOffset(toks[2]); + int rb = parseReg(toks[3]); + do_store(ra,offs,rb); + } + else if (toks[0].equals(INSTRUCTION_MOVE)){ + if (toks.length != 3){ + throw new InvalidInstructionException(); + } + int rd = parseReg(toks[1]); + int offs = parseOffset(toks[2]); + do_move(rd,offs); + } + else if (toks[0].equals(INSTRUCTION_JUMP)){ + if (toks.length != 2){ + throw new InvalidInstructionException(); + } + int offs = parseOffset(toks[1]); + pc = pc + offs; + count++; + continue; /* avoid default increment of pc below */ + } + else if (toks[0].equals(INSTRUCTION_JZ)){ + if (toks.length != 3){ + throw new InvalidInstructionException(); + } + int ra = parseReg(toks[1]); + int offs = parseOffset(toks[2]); + if (regs[ra] == 0){ + pc = pc + offs; + } + else{ + pc = pc + 1; + } + count++; + continue; /* avoid default increment the pc below */ + } + else { + System.err.println("Unrecognised instruction: " + inst); + throw new InvalidInstructionException(); + } + count++; + pc = pc + 1; +} /* got here without returning already... */ - throw new NoReturnValueException(); - } +throw new NoReturnValueException(); +} /** * get the number of instructions successfully executed by the VM so far diff --git a/test/swen90006/machine/BoundaryTests.java b/test/swen90006/machine/BoundaryTests.java index 61ce1ca647f325fbf452047408c1e22d9befcb99..f1339c2f5d1ba2f4a84aba921dd31560f44e054c 100644 --- a/test/swen90006/machine/BoundaryTests.java +++ b/test/swen90006/machine/BoundaryTests.java @@ -24,53 +24,179 @@ public class BoundaryTests { } - //Any method annotation with "@Test" is executed as a test. - @Test public void aTest() + //EC3 , EC6 EC15, EC18, EC19 all tested in one + @Test public void Valid() { - //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); + final List<String> lines = readInstructions("examples/Valid.s"); + Machine m = new Machine(); + assertEquals(m.execute(lines), -2147303305); + } + + + + //EC1 - Ri, i is negative + @Test(expected = swen90006.machine.InvalidInstructionException.class) + public void RiNeg() + { + final List<String> lines = readInstructions("examples/RiNeg.s"); + Machine m = new Machine(); + m.execute(lines); + } + + //EC2 - Register value too high + @Test public void RiValueHigh() + { + final List<String> lines = readInstructions("examples/RiValueHigh.s"); + Machine m = new Machine(); + assertEquals(m.execute(lines), -2147483648); + } + + //EC4 - Ri, i too high > 31 + @Test(expected = swen90006.machine.InvalidInstructionException.class) + public void RiHigh() + { + final List<String> lines = readInstructions("examples/RiHigh.s"); + Machine m = new Machine(); + m.execute(lines); + } + + //EC5 - Value too low <-65535 + @Test(expected = swen90006.machine.InvalidInstructionException.class) + public void valueLow() + { + final List<String> lines = readInstructions("examples/valueLow.s"); + Machine m = new Machine(); + m.execute(lines); + } + + //EC7 - Value too high >65535 + @Test(expected = swen90006.machine.InvalidInstructionException.class) + public void valueHigh() + { + final List<String> lines = readInstructions("examples/valueHigh.s"); + Machine m = new Machine(); + m.execute(lines); + } + //EC9 -- jumps to PC = 1 as PC = 0 caused infinite loop + @Test public void JmpFirst() + { + final List<String> lines = readInstructions("examples/jmpFirst.s"); + Machine m = new Machine(); + assertEquals(m.execute(lines), 2); + } + + //EC8 - Jumps to negative pc, value=-c + @Test(expected = swen90006.machine.NoReturnValueException.class) + public void JmpNeg() + { + final List<String> lines = readInstructions("examples/jmpNeg.s"); + Machine m = new Machine(); + m.execute(lines); + } + //EC9 - jumps to PC = N + @Test public void JmpLast() + { + final List<String> lines = readInstructions("examples/jmpLast.s"); + Machine m = new Machine(); + assertEquals(m.execute(lines), 100); + } + //EC10 - Jumps past end of PC>N , value > N -c + @Test(expected = swen90006.machine.NoReturnValueException.class) + public void JmpPast() + { + final List<String> lines = readInstructions("examples/jmpPast.s"); + Machine m = new Machine(); + m.execute(lines); + } + + + //EC12 - JZ to PC= 1 as pc = 0 causes infinite loop + @Test public void JzFirst() + { + final List<String> lines = readInstructions("examples/jzfirst.s"); + Machine m = new Machine(); + assertEquals(m.execute(lines), -1); } - @Test public void anotherTest() + //EC11 - jz jumps to negative pc=-1, value = -c + @Test(expected = swen90006.machine.NoReturnValueException.class) + public void JzNeg() + { + final List<String> lines = readInstructions("examples/jzNeg.s"); + Machine m = new Machine(); + m.execute(lines); + } + //EC12 - JZ to PC= N + @Test public void JzLast() { - List<String> list = new ArrayList<String>(); - list.add("a"); - list.add("b"); + final List<String> lines = readInstructions("examples/jzLast.s"); + Machine m = new Machine(); + assertEquals(m.execute(lines), 100); + } + //EC13 - jz jumps past end of PC = N+1, value = N-c+ 1 + @Test(expected = swen90006.machine.NoReturnValueException.class) + public void JzPast() + { + final List<String> lines = readInstructions("examples/jzPast.s"); + Machine m = new Machine(); + m.execute(lines); + } - //the assertTrue method is used to check whether something holds. - assertTrue(list.contains("a")); + + + //EC14 - string store and load memory location negative, memory[-1] + @Test public void StrNeg() + { + final List<String> lines = readInstructions("examples/strNeg.s"); + Machine m = new Machine(); + assertEquals(m.execute(lines), 0); } - //Test test opens a file and executes the machine - @Test public void aFileOpenTest() + + // //EC15 -- Memory[0] -- implemented in valid test + // @Test public void StrZero() + // { + // final List<String> lines = readInstructions("examples/strZero.s"); + // Machine m = new Machine(); + // assertEquals(m.execute(lines), 12); + // } + // //EC15 -- Memory[65535] -- implmeneted in valid test + // @Test public void StrMax() + // { + // final List<String> lines = readInstructions("examples/strMax.s"); + // Machine m = new Machine(); + // assertEquals(m.execute(lines), 12); + // } + + + //EC16 - String store and load mormory location > MAX , memory[65536] + @Test public void StrHigh() { - final List<String> lines = readInstructions("examples/array.s"); + final List<String> lines = readInstructions("examples/strHigh.s"); Machine m = new Machine(); - assertEquals(m.execute(lines), 45); + 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 + + //EC18 - Divide by 0 + @Test public void Div() { - throw new java.io.IOException(); + final List<String> lines = readInstructions("examples/div.s"); + Machine m = new Machine(); + assertEquals(m.execute(lines), 100); } - //This test should fail. - //To provide additional feedback when a test fails, an error message - //can be included - @Test public void aFailedTest() + + //EC17 - No return reachable + @Test(expected = swen90006.machine.NoReturnValueException.class) + public void NoReturn() { - //include a message for better feedback - final int expected = 2; - final int actual = 1 + 2; - assertEquals("Some failure message", expected, actual); + final List<String> lines = readInstructions("examples/NoReturn.s"); + 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) diff --git a/test/swen90006/machine/PartitioningTests.java b/test/swen90006/machine/PartitioningTests.java index 8ecb32f94ed67a7a352cf8c8cd75cee26b7f23c6..8b6c7635a0772047b5ff3a0918e0a7fd674b4ad3 100644 --- a/test/swen90006/machine/PartitioningTests.java +++ b/test/swen90006/machine/PartitioningTests.java @@ -24,52 +24,160 @@ public class PartitioningTests { } - //Any method annotation with "@Test" is executed as a test. - @Test public void aTest() - { - //the assertEquals method used to check whether two values are - //equal, using the equals method - final int expected = 1; - final int actual = 2 / 2; - assertEquals(expected, actual); - } + //the assertTrue method is used to check whether something holds. + //assertTrue(list.contains("a")); - @Test public void anotherTest() - { - 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")); - } - //Test test opens a file and executes the machine - @Test public void aFileOpenTest() +//Assignment tests cases from leaf nodes in test template tree: + + //EC3 , EC6 EC15, EC18 all tested in one + @Test public void Valid() { - final List<String> lines = readInstructions("examples/array.s"); + final List<String> lines = readInstructions("examples/partitionTests/validfile.s"); Machine m = new Machine(); - assertEquals(m.execute(lines), 45); + assertEquals(m.execute(lines), 218); } - //To test an exception, specify the expected exception after the @Test - @Test(expected = java.io.IOException.class) - public void anExceptionTest() - throws Throwable - { - throw new java.io.IOException(); - } - //This test should fail. - //To provide additional feedback when a test fails, an error message - //can be included - // @Test public void aFailedTest() - // { - // //include a message for better feedback - // final int expected = 2; - // final int actual = 1 + 2; - // assertEquals("Some failure message", expected, actual); - // } + +//EC1 - Ri, i is negative +@Test(expected = swen90006.machine.InvalidInstructionException.class) +public void RiNeg() +{ + final List<String> lines = readInstructions("examples/partitionTests/RiNeg.s"); + Machine m = new Machine(); + m.execute(lines); +} + //EC4 - Ri, i too high > 31 +@Test(expected = swen90006.machine.InvalidInstructionException.class) +public void RiHigh() +{ + final List<String> lines = readInstructions("examples/partitionTests/RiHigh.s"); + Machine m = new Machine(); + m.execute(lines); +} + + //EC7 - Value too high >65535 +@Test(expected = swen90006.machine.InvalidInstructionException.class) +public void ValueHigh() +{ + final List<String> lines = readInstructions("examples/partitionTests/valueHigh.s"); + Machine m = new Machine(); + m.execute(lines); +} + + + //EC5 - Value too low <-65535 +@Test(expected = swen90006.machine.InvalidInstructionException.class) +public void ValueLow() +{ + final List<String> lines = readInstructions("examples/partitionTests/valueLow.s"); + Machine m = new Machine(); + m.execute(lines); +} + + //EC2 - Register value too high, register value = maxvalue + 1 +@Test public void RiMaxValue() +{ + final List<String> lines = readInstructions("examples/partitionTests/RiMaxValue.s"); + Machine m = new Machine(); + assertEquals(m.execute(lines), -2147483648); +} + + //EC8 - Jumps to negative pc, value=-c +@Test(expected = swen90006.machine.NoReturnValueException.class) +public void JmpNeg() +{ + final List<String> lines = readInstructions("examples/partitionTests/jmpNeg.s"); + Machine m = new Machine(); + m.execute(lines); +} + + //EC10 - Jumps past end of PC>N , value > N -c +@Test(expected = swen90006.machine.NoReturnValueException.class) +public void JmpHigh() +{ + final List<String> lines = readInstructions("examples/partitionTests/jmpHigh.s"); + Machine m = new Machine(); + m.execute(lines); +} + + //EC11 - jz jumps to negative pc=-1, value = -c +@Test(expected = swen90006.machine.NoReturnValueException.class) +public void jzNeg() +{ + final List<String> lines = readInstructions("examples/partitionTests/jzNeg.s"); + Machine m = new Machine(); + m.execute(lines); +} + + //EC13 - jz jumps past end of PC = N+1, value = N-c+ 1 +@Test(expected = swen90006.machine.NoReturnValueException.class) +public void jzHigh() +{ + final List<String> lines = readInstructions("examples/partitionTests/jzHigh.s"); + Machine m = new Machine(); + m.execute(lines); +} + +//EC16 - String store and load mormory location > MAX , memory[65536] + +@Test public void strHigh() +{ + final List<String> lines = readInstructions("examples/partitionTests/strMaxMem.s"); + Machine m = new Machine(); + assertEquals(m.execute(lines), 0); +} + //EC14 - string store and load memory location negative, memory[-1] + +@Test(expected = swen90006.machine.InvalidInstructionException.class) +public void strNeg() +{ + final List<String> lines = readInstructions("examples/partitionTests/strMemNeg.s"); + Machine m = new Machine(); + assertEquals(m.execute(lines), 0); +} + + + + + + + +// //token is value instead of register +// @Test(expected = swen90006.machine.InvalidInstructionException.class) +// public void RChange() +// { +// final List<String> lines = readInstructions("examples/RChange.s"); +// Machine m = new Machine(); +// assertEquals(m.execute(lines), -64535); +// } + +// + + +///potential test for register value too high + + +//EC17- No return reachable +@Test(expected = swen90006.machine.NoReturnValueException.class) +public void NoReturn() +{ + final List<String> lines = readInstructions("examples/partitionTests/noReturn.s"); + Machine m = new Machine(); + m.execute(lines); +} + +//EC18 - Divide by 0 +@Test public void Div() +{ + final List<String> lines = readInstructions("examples/div.s"); + Machine m = new Machine(); + assertEquals(m.execute(lines), 100); +} + + //Read in a file containing a program and convert into a list of //string instructions