diff --git a/Java/pom.xml b/Java/pom.xml index 2076887..2113b72 100644 --- a/Java/pom.xml +++ b/Java/pom.xml @@ -4,7 +4,7 @@ 4.0.0 org.ciyam AT - 1.3 + 1.3.1 jar true @@ -38,5 +38,11 @@ ${bouncycastle.version} test + + junit + junit + 4.13 + test + diff --git a/Java/src/main/java/org/ciyam/at/MachineState.java b/Java/src/main/java/org/ciyam/at/MachineState.java index 61e8ba6..37a494b 100644 --- a/Java/src/main/java/org/ciyam/at/MachineState.java +++ b/Java/src/main/java/org/ciyam/at/MachineState.java @@ -194,10 +194,10 @@ public class MachineState { } /** For creating a new machine state */ - public MachineState(byte[] creationBytes) { - this(null, null, Arrays.copyOfRange(creationBytes, 0, HEADER_LENGTH)); + public MachineState(API api, byte[] creationBytes) { + this(api, null, Arrays.copyOfRange(creationBytes, 0, HEADER_LENGTH)); - int expectedLength = HEADER_LENGTH + this.numCodePages * this.constants.CODE_PAGE_SIZE + this.numDataPages + this.constants.DATA_PAGE_SIZE; + int expectedLength = HEADER_LENGTH + this.numCodePages * this.constants.CODE_PAGE_SIZE + this.numDataPages * this.constants.DATA_PAGE_SIZE; if (creationBytes.length != expectedLength) throw new IllegalArgumentException("Creation bytes length does not match header values"); diff --git a/Java/src/test/java/BranchingOpCodeTests.java b/Java/src/test/java/BranchingOpCodeTests.java index 87ab5e2..063c7c5 100644 --- a/Java/src/test/java/BranchingOpCodeTests.java +++ b/Java/src/test/java/BranchingOpCodeTests.java @@ -8,6 +8,33 @@ import common.ExecutableTest; public class BranchingOpCodeTests extends ExecutableTest { + @Test + public void testBackwardsBranch() throws ExecutionException { + int backwardsAddr = 0x05; + int forwardAddr = 0x13; + + codeByteBuffer.put(OpCode.JMP_ADR.value).putInt(forwardAddr); + + // backwardsAddr: + assertEquals(backwardsAddr, codeByteBuffer.position()); + codeByteBuffer.put(OpCode.SET_VAL.value).putInt(1).putLong(2L); + codeByteBuffer.put(OpCode.FIN_IMD.value); + + // forwardAddr: + assertEquals(forwardAddr, codeByteBuffer.position()); + codeByteBuffer.put(OpCode.SET_VAL.value).putInt(0).putLong(0L); + int tempPC = codeByteBuffer.position(); + codeByteBuffer.put(OpCode.BZR_DAT.value).putInt(0).put((byte) (backwardsAddr - tempPC)); + codeByteBuffer.put(OpCode.SET_VAL.value).putInt(1).putLong(1L); + codeByteBuffer.put(OpCode.FIN_IMD.value); + + execute(true); + + assertTrue(state.getIsFinished()); + assertFalse(state.getHadFatalError()); + assertEquals("Data does not match", 2L, getData(1)); + } + @Test public void testBZR_DATtrue() throws ExecutionException { int targetAddr = 0x21; diff --git a/Java/src/test/java/SerializationTests.java b/Java/src/test/java/SerializationTests.java index 4edb668..174c6d5 100644 --- a/Java/src/test/java/SerializationTests.java +++ b/Java/src/test/java/SerializationTests.java @@ -118,4 +118,22 @@ public class SerializationTests extends ExecutableTest { assertEquals(expectedValue, getData(0)); } + /** Test serialization to/from creation bytes. */ + @Test + public void testCreationBytes() { + byte[] headerBytes = TestUtils.HEADER_BYTES; + byte[] codeBytes = codeByteBuffer.array(); + byte[] dataBytes = dataByteBuffer.array(); + + state = new MachineState(api, logger, headerBytes, codeBytes, dataBytes); + packedState = state.toBytes(); + + byte[] creationBytes = MachineState.toCreationBytes(TestUtils.VERSION, codeBytes, dataBytes, TestUtils.NUM_CALL_STACK_PAGES, TestUtils.NUM_USER_STACK_PAGES, TestUtils.MIN_ACTIVATION_AMOUNT); + + MachineState restoredState = new MachineState(api, creationBytes); + byte[] packedRestoredSate = restoredState.toBytes(); + + assertTrue(Arrays.equals(packedState, packedRestoredSate)); + } + }