Skip to content

Commit

Permalink
LF: add test for bigNumeric operations (#9310)
Browse files Browse the repository at this point in the history
CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
remyhaemmerle-da authored Apr 6, 2021
1 parent fd63cf0 commit 7b3b669
Show file tree
Hide file tree
Showing 6 changed files with 427 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ abstract class NumericModule {
final def assertFromUnscaledBigDecimal(x: BigDec): Numeric =
assertRight(fromUnscaledBigDecimal(x))

final def toUnscaledString(x: Numeric): String = {
final def toUnscaledString(x: BigDecimal): String = {
// Strip the trailing zeros (which BigDecimal keeps if the string
// it was created from had them), and use the plain notation rather
// than scientific notation.
Expand Down
25 changes: 24 additions & 1 deletion daml-lf/interpreter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
load(
"//bazel_tools:scala.bzl",
"da_scala_library",
"da_scala_test",
"da_scala_test_suite",
"lf_scalacopts",
)
Expand Down Expand Up @@ -39,10 +40,15 @@ da_scala_library(
],
)

bigNumericTests = "src/test/scala/com/digitalasset/daml/lf/speedy/SBuiltinBigNumericTest.scala"

da_scala_test_suite(
name = "tests",
size = "small",
srcs = glob(["src/test/**/*.scala"]),
srcs = glob(
["src/test/**/*.scala"],
exclude = [bigNumericTests],
),
scala_deps = [
"@maven//:org_scala_lang_modules_scala_collection_compat",
"@maven//:org_scalacheck_scalacheck",
Expand All @@ -66,6 +72,23 @@ da_scala_test_suite(
],
)

da_scala_test(
name = "test_bignumeric",
srcs = [bigNumericTests],
scala_deps = [
"@maven//:org_scalatest_scalatest",
"@maven//:org_scalaz_scalaz_core",
],
scalacopts = lf_scalacopts,
deps = [
":interpreter",
"//daml-lf/data",
"//daml-lf/language",
"//daml-lf/parser",
"//daml-lf/transaction",
],
)

scala_repl(
name = "interpreter@repl",
deps = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ private[lf] object SBuiltin {
case SParty(p) => p
case SUnit => s"<unit>"
case SDate(date) => date.toString
case SBigNumeric(x) => Numeric.toString(x)
case SBigNumeric(x) => Numeric.toUnscaledString(x)
case SContractId(_) | SNumeric(_) => crash("litToText: literal not supported")
case otherwise =>
throw SErrorCrash(
Expand Down Expand Up @@ -923,11 +923,7 @@ private[lf] object SBuiltin {

final object SBToBigNumericNumeric extends SBuiltinPure(2) {
override private[speedy] def executePure(args: util.ArrayList[SValue]): SBigNumeric = {
// should not fail
rightOrArithmeticError(
"overflow/underflow",
SBigNumeric.fromBigDecimal(getSNumeric(args, 1)),
)
SBigNumeric.fromNumeric(getSNumeric(args, 1))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,23 +242,25 @@ object SValue {
override def toString: String = s"SBigNumeric($value)"
}
object SBigNumeric {
// TODO https://github.com/digital-asset/daml/issues/8719
// Decide what are the actual bound for BigDecimal
val MaxScale = 1 << 10
val MaxPrecision = MaxScale << 2
val MaxPrecision = 1 << 16
val MaxScale = MaxPrecision / 2
val MinScale = -MaxPrecision / 2 + 1

def unapply(value: SBigNumeric): Some[java.math.BigDecimal] =
Some(value.value)

def fromBigDecimal(x: java.math.BigDecimal): Either[String, SBigNumeric] = {
val norm = x.stripTrailingZeros()
Either.cond(
test = norm.scale <= MaxScale && norm.precision + norm.scale < MaxScale,
test = norm.scale <= MaxScale && norm.precision - norm.scale <= MaxScale,
right = new SBigNumeric(norm),
left = "non valid BigNumeric",
)
}

def fromNumeric(x: Numeric) =
new SBigNumeric(x.stripTrailingZeros())

def assertFromBigDecimal(x: java.math.BigDecimal): SBigNumeric =
data.assertRight(fromBigDecimal(x))

Expand Down
Loading

0 comments on commit 7b3b669

Please sign in to comment.