Skip to content

Commit

Permalink
improve Nat.scala test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Apr 6, 2024
1 parent af6c13a commit 1a249de
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions core/src/test/scala/org/bykn/bosatsu/NatTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class NatTest extends munit.ScalaCheckSuite {
lazy val genNat: Gen[Nat] = {
val recur = Gen.lzy(genNat)
Gen.frequency(
(5, Gen.chooseNum(0, Long.MaxValue).map(Nat.fromLong(_))),
// make sure to exercise the cached table
(1, Gen.chooseNum(0, 1024, 1).map(Nat.fromInt(_))),
(5, Gen.chooseNum(0, Long.MaxValue, Int.MaxValue.toLong, Int.MaxValue.toLong + 1).map(Nat.fromLong(_))),
(1, Gen.zip(recur, recur).map { case (a, b) => a + b }),
(1, Gen.zip(recur, recur).map { case (a, b) => a * b })
)
Expand Down Expand Up @@ -78,13 +80,9 @@ class NatTest extends munit.ScalaCheckSuite {
}

property("x * y homomorphism") {
forAll { (i0: Long, j0: Long) =>
val i = i0 & Long.MaxValue
val j = j0 & Long.MaxValue
val ni = Nat.fromLong(i)
val nj = Nat.fromLong(j)
forAll(genNat, genNat) { (ni, nj) =>
val nk = ni * nj
assertEquals(nk.toBigInt, BigInt(i) * BigInt(j))
assertEquals(nk.toBigInt, ni.toBigInt * nj.toBigInt)
}
}

Expand All @@ -96,6 +94,14 @@ class NatTest extends munit.ScalaCheckSuite {
}
}

property("x.dec == x - 1 when x > 0") {
forAll(genNat) { n =>
val i = n.dec.toBigInt
if (n == Nat.zero) assertEquals(i, BigInt(0))
else assertEquals(i, n.toBigInt - 1)
}
}

property("x.shift_32 == x.toBigInt * 2^32") {
val n1 = BigInt("8588888260151524380556863712485265508")
val shift = n1 << 32
Expand Down Expand Up @@ -132,4 +138,17 @@ class NatTest extends munit.ScalaCheckSuite {
assert((n.dec.inc == n) || n.isZero)
}
}

property("if the value is > Long.MaxValue maybeLong = None") {
forAll(genNat) { n =>
val bi = n.toBigInt
val ml = n.maybeLong
assertEquals(ml.isEmpty, bi > Long.MaxValue)
}
}
property("the string repr matches toBigInt") {
forAll(genNat) { n =>
assertEquals(n.toString, n.toBigInt.toString)
}
}
}

0 comments on commit 1a249de

Please sign in to comment.