Browse Source

Fix constructing MachineState from creationBytes. Bump to v1.3.1

NOTE: this constructor's args have changed from:
  public MachineState(byte[] creationBytes)
to:
  public MachineState(API api, byte[] creationBytes)

The bug was miscalculating 'expectedLength'.

Added unit test to cover above.

Also added unit test to cover branching backwards.

Bumped version to 1.3.1 and also fixed pom.xml so it's possible
to call 'mvn clean package' from command line without errors.
master v1.3.1
catbref 5 years ago
parent
commit
3e0699f399
  1. 8
      Java/pom.xml
  2. 6
      Java/src/main/java/org/ciyam/at/MachineState.java
  3. 27
      Java/src/test/java/BranchingOpCodeTests.java
  4. 18
      Java/src/test/java/SerializationTests.java

8
Java/pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.ciyam</groupId>
<artifactId>AT</artifactId>
<version>1.3</version>
<version>1.3.1</version>
<packaging>jar</packaging>
<properties>
<skipTests>true</skipTests>
@ -38,5 +38,11 @@
<version>${bouncycastle.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

6
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");

27
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;

18
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));
}
}

Loading…
Cancel
Save