Skip to content

Commit

Permalink
More CAD3 refactoring and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 1, 2024
1 parent 79635f5 commit 540defd
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 23 deletions.
12 changes: 7 additions & 5 deletions convex-core/src/main/java/convex/core/cvm/CVMTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ public class CVMTag {
*/
public static final byte ADDRESS = (byte) 0xEA;

/**
* CVM Core definitions
*/
public static final byte CORE_DEF = (byte) 0xED;

public static final byte SYNTAX = (byte) 0x88;

Expand Down Expand Up @@ -61,7 +57,7 @@ public class CVMTag {
public static final byte BLOCK_RESULT = (byte) 0xAE;

// ===============================================
// CVM Ops
// CVM Ops and Code

/**
* Special Ops as extension value
Expand All @@ -72,6 +68,12 @@ public class CVMTag {
* Local is extension value, position on local stack
*/
public static final byte OP_LOCAL = (byte) 0xE6;

/**
* CVM Core definitions
*/
public static final byte CORE_DEF = (byte) 0xED;



}
1 change: 1 addition & 0 deletions convex-core/src/main/java/convex/core/data/MapTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ private boolean isValidStructure() {

@Override
public void validateCell() throws InvalidDataException {
if (shift<0) throw new InvalidDataException("Negaitive shift!", this);
if (!isValidStructure()) throw new InvalidDataException("Bad structure", this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static AByteFlag read(byte tag) {
case 0: return CVMBool.FALSE;
case 1: return CVMBool.TRUE;
}
return ByteFlagExtended.unsafeCreate(tag); // we assume tag is valid
return ByteFlag.unsafeCreate(tag); // we assume tag is valid
}

@Override
Expand Down Expand Up @@ -57,6 +57,6 @@ public int encodeRaw(byte[] bs, int pos) {
public static AByteFlag create(long value) {
if (value==1) return CVMBool.TRUE;
if (value==0) return CVMBool.FALSE;
return ByteFlagExtended.create(value); // may be null
return ByteFlag.create(value); // may be null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
/**
* Class implementing the CAD3 extended byte flags `0xB2` to `0xBF`
*/
public class ByteFlagExtended extends AByteFlag {
public class ByteFlag extends AByteFlag {

private static final ByteFlagExtended[] cache = new ByteFlagExtended[16];
private static final ByteFlag[] cache = new ByteFlag[16];

static {
for (int i=0; i<16;i++) {
byte tag=(byte)(Tag.BYTE_FLAG_BASE+i);
cache[i] = Cells.intern(new ByteFlagExtended(tag));
cache[i] = Cells.intern(new ByteFlag(tag));
}
}

Expand All @@ -27,22 +27,22 @@ public class ByteFlagExtended extends AByteFlag {
* Private constructor, to enforce singleton instances
* @param tag
*/
private ByteFlagExtended(byte tag) {
private ByteFlag(byte tag) {
this.tag=tag;
}

/**
* Creates an extended byte flag for the given value 2-15
* Creates an extended byte flag for the given value 0-15
* @param value
* @return
*/
public static ByteFlagExtended create(long value) {
public static ByteFlag create(long value) {
int m=(int)(value&0xf);
if (m!=value) return null; // out of range
return unsafeCreate(m);
}

static ByteFlagExtended unsafeCreate(int value) {
static ByteFlag unsafeCreate(int value) {
return cache[value];
}

Expand Down
4 changes: 2 additions & 2 deletions convex-core/src/main/java/convex/core/data/type/CAD3Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import convex.core.cvm.Address;
import convex.core.data.ACell;
import convex.core.data.prim.ByteFlagExtended;
import convex.core.data.prim.ByteFlag;

/**
* Type for CAD3 Values not recognised by CVM
Expand All @@ -29,7 +29,7 @@ public String toString () {

@Override
public ACell defaultValue() {
return ByteFlagExtended.create(15);
return ByteFlag.create(15);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import convex.core.cvm.transactions.Call;
import convex.core.cvm.transactions.Transfer;
import convex.core.data.impl.DummyCell;
import convex.core.data.prim.ByteFlagExtended;
import convex.core.data.prim.ByteFlag;
import convex.core.data.prim.CVMDouble;
import convex.core.data.prim.CVMLong;
import convex.core.exceptions.BadFormatException;
Expand All @@ -35,7 +35,7 @@
public class AdversarialDataTest {

// A value that is valid CAD3, but not a first class CVM value
public static final ACell NON_CVM=ByteFlagExtended.create(15);
public static final ACell NON_CVM=ByteFlag.create(15);

// A value that is non-canonical but otherwise valid CVM value
public static final Blob NON_CANONICAL=Blob.createRandom(new Random(), Blob.CHUNK_LENGTH+1);
Expand Down
6 changes: 3 additions & 3 deletions convex-core/src/test/java/convex/core/data/ByteFlagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import convex.core.cvm.CVMTag;
import convex.core.data.prim.AByteFlag;
import convex.core.data.prim.ByteFlagExtended;
import convex.core.data.prim.ByteFlag;
import convex.core.data.prim.CVMBool;
import convex.core.exceptions.BadFormatException;
import convex.core.lang.RT;
Expand All @@ -36,8 +36,8 @@ public void testInavlidValues() {
}

@Test public void testBooleans() {
ByteFlagExtended bf=ByteFlagExtended.create(0);
ByteFlagExtended bt=ByteFlagExtended.create(1);
ByteFlag bf=ByteFlag.create(0);
ByteFlag bt=ByteFlag.create(1);

assertNotSame(bf,CVMBool.FALSE);
assertEquals(bf,CVMBool.FALSE);
Expand Down
18 changes: 16 additions & 2 deletions convex-core/src/test/java/convex/core/data/CAD3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import convex.core.cvm.Address;
import convex.core.cvm.CVMTag;
import convex.core.cvm.Context;
import convex.core.data.prim.ByteFlag;
import convex.core.data.prim.CVMBool;
import convex.core.data.prim.CVMLong;
import convex.core.lang.ACVMTest;
Expand Down Expand Up @@ -47,6 +48,14 @@ public class CAD3Test extends ACVMTest {
assertEquals(e,a);
}

@Test public void testByteFlags() {
ByteFlag bf=ByteFlag.create(2);
assertEquals(bf,Reader.read("#[b2]"));
ObjectsTest.doAnyValueTests(bf);

}


@Test public void testReadEncodings() {
assertSame(Address.ZERO,Reader.read("#[EA00]"));
assertSame(CVMLong.ZERO,Reader.read("#[10]"));
Expand All @@ -64,8 +73,12 @@ public class CAD3Test extends ACVMTest {

assertEquals(3,dr.count);
assertSame(v,dr.values());
assertEquals("#[df03110111021103]",dr.toString());

String ds="#[df03110111021103]";
assertEquals(ds,dr.toString());
assertEquals(dr,Reader.read(ds));

assertTrue(dr.isCompletelyEncoded());

ObjectsTest.doAnyValueTests(dr);
}

Expand All @@ -80,6 +93,7 @@ public class CAD3Test extends ACVMTest {
AVector<CVMLong> v=Samples.INT_VECTOR_300;
DenseRecord dr=DenseRecord.create(0xDF,v);
assertEquals((byte)0xdf,dr.getTag());
assertFalse(dr.isCompletelyEncoded());

assertEquals(300,dr.count());
assertSame(v,dr.values());
Expand Down

0 comments on commit 540defd

Please sign in to comment.