Skip to content

Commit

Permalink
Improve consistency of boolean casts
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 15, 2023
1 parent da47075 commit fb678e2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions convex-core/src/main/java/convex/core/data/Blob.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
*/
public class Blob extends AArrayBlob {
public static final Blob EMPTY = wrap(Utils.EMPTY_BYTES);
public static final Blob ZERO = wrap(new byte[] {0});
public static final Blob ONE = wrap(new byte[] {1});;

public static final Blob NULL_ENCODING = Blob.wrap(new byte[] {Tag.NULL});

public static final int CHUNK_LENGTH = 4096;
Expand Down
5 changes: 5 additions & 0 deletions convex-core/src/main/java/convex/core/data/prim/CVMBool.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import convex.core.data.ACell;
import convex.core.data.AString;
import convex.core.data.Blob;
import convex.core.data.BlobBuilder;
import convex.core.data.Strings;
import convex.core.data.Tag;
Expand Down Expand Up @@ -109,4 +110,8 @@ public AString toCVMString(long limit) {
return this==a;
}

public Blob toBlob() {
return value?Blob.ONE:Blob.ZERO;
}

}
2 changes: 2 additions & 0 deletions convex-core/src/main/java/convex/core/lang/RT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,8 @@ public static ABlob castBlob(ACell a) {
return ((AInteger)a).toBlob();
if (a instanceof AString)
return Blobs.fromHex((AString)a);
if (a instanceof CVMBool)
return ((CVMBool)a).toBlob();
return null;
}

Expand Down
6 changes: 6 additions & 0 deletions convex-core/src/test/java/convex/core/lang/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ public void testBlob() {

// Address converts to regular Blob
assertEquals(eval("0x0000000000000013"),eval("(blob #19)"));

// Booleans become 0/1 bytes
assertEquals(eval("0x00"),eval("(blob false)"));
assertEquals(eval("0x01"),eval("(blob true)"));

// Account key should be a Blob
assertEquals(eval("*key*"),eval("(blob *key*)"));
Expand Down Expand Up @@ -464,6 +468,8 @@ public void testChar() {
public void testBoolean() {
// test precise values
assertSame(CVMBool.TRUE, eval("(boolean 1)"));
assertSame(CVMBool.TRUE, eval("(boolean 0)"));
assertSame(CVMBool.TRUE, eval("(boolean 0x00)"));
assertSame(CVMBool.FALSE, eval("(boolean nil)"));

// nil and false should be falsey
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/test/java/convex/test/Samples.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public class Samples {
public static final ASet<CVMLong> LONG_SET_10 = Sets.create(INT_VECTOR_10);
public static final ASet<CVMLong> LONG_SET_100 = Sets.create(INT_VECTOR_300);

public static final Blob ONE_ZERO_BYTE_DATA = Blob.fromHex("00");
public static final Blob ONE_ZERO_BYTE_DATA = Blob.ZERO;

public static final Keyword FOO = Keyword.create("foo");
public static final Keyword BAR = Keyword.create("bar");
Expand Down

0 comments on commit fb678e2

Please sign in to comment.