Skip to content

Commit

Permalink
More big integer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 15, 2023
1 parent e30ac12 commit bfbd52b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,22 @@ public void testMaxSize() {
bs[0]=-128; // set sign bit for max sized negative number
ABlob blob=Blob.wrap(bs);
CVMBigInteger b=CVMBigInteger.create(blob);

assertEquals(b,Samples.MIN_BIGINT);

assertTrue(b.isCanonical());
assertEquals(Constants.MAX_BIG_INTEGER_LENGTH,b.blob().count());

assertNull(b.negate());
assertNull(b.dec()); // overflow
assertNull(b.multiply(CVMLong.create(2))); // overflow
assertNotNull(b.inc().negate());

// Negate to get maximum possible bigint
CVMBigInteger bn=(CVMBigInteger) b.inc().negate();
assertNotNull(bn);

doBigTest(b);
doBigTest(bn);
}

@Test
Expand Down Expand Up @@ -163,7 +171,10 @@ public static void doBigTest(CVMBigInteger bi) {
assertEquals(bi.getEncoding(),bi2.getEncoding());
assertEquals(bi,bi2);

assertEquals(bi,bi.inc().dec());
AInteger biplus=bi.inc();
if (biplus!=null) {
assertEquals(bi,biplus.dec());
}

if (bi.isCanonical()) {
assertTrue(bi.byteLength()>8);
Expand Down
5 changes: 5 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 @@ -3295,7 +3295,12 @@ public void testQuot() {
@Test
public void testDiv() {
assertEquals(0L, evalL("(div 4 10)"));
assertEquals(0L, evalL("(div 0 -1)"));
assertEquals(-4L, evalL("(div -10 3)"));

assertArgumentError(step("(div 3 0)"));
assertArgumentError(step("(div 0 0)"));
assertArgumentError(step("(div -1567567567567564756767586786785688 0)"));
// TODO: more tests
}

Expand Down
14 changes: 14 additions & 0 deletions convex-core/src/test/java/convex/test/Samples.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;

import convex.core.Constants;
import convex.core.crypto.AKeyPair;
import convex.core.crypto.ASignature;
import convex.core.crypto.Ed25519Signature;
Expand Down Expand Up @@ -47,6 +48,7 @@
import convex.core.data.VectorLeaf;
import convex.core.data.VectorTree;
import convex.core.data.Vectors;
import convex.core.data.prim.CVMBigInteger;
import convex.core.data.prim.CVMBool;
import convex.core.data.prim.CVMChar;
import convex.core.data.prim.CVMDouble;
Expand Down Expand Up @@ -156,12 +158,24 @@ public class Samples {

public static final Blob SMALL_BLOB = Blob.fromHex("cafebabe");

public static final CVMBigInteger MAX_BIGINT;
public static final CVMBigInteger MIN_BIGINT;

static {
// we should be able to actually build these, thanks to structural sharing.
DIABOLICAL_VECTOR_30_30 = createNastyNestedVector(30, 30);
DIABOLICAL_VECTOR_2_10000 = createNastyNestedVector(2, 10000);
DIABOLICAL_MAP_30_30 = createNastyNestedMap(30, 30);
DIABOLICAL_MAP_2_10000 = createNastyNestedMap(2, 10000);

{
byte [] bs=new byte[Constants.MAX_BIG_INTEGER_LENGTH];
bs[0]=-128; // set sign bit for max sized negative number
ABlob blob=Blob.wrap(bs);
CVMBigInteger b=CVMBigInteger.create(blob);
MIN_BIGINT=b;
MAX_BIGINT=(CVMBigInteger) b.inc().negate();
}
}

/**
Expand Down

0 comments on commit bfbd52b

Please sign in to comment.