swift-numerics icon indicating copy to clipboard operation
swift-numerics copied to clipboard

[BigInt tests] đź’€ Binary `*`, `/` and `%`

Open LiarPrincess opened this issue 2 years ago • 1 comments
trafficstars

Please read the #242 Using tests from “Violet - Python VM written in Swift” before.


đź’€ Crash

func test_div_crash() {
  let lhs = BigInt("-9223372036854775808")!
  let rhs = BigInt("-1")!
  _ = lhs / rhs // Overflow
}

❌ Failures

func test_div_sign() {
  // positive / negative = negative
  var lhs = BigInt("18446744073709551615")!
  var rhs = BigInt("-1")!
  var expected = BigInt("-18446744073709551615")!
  XCTAssertEqual(lhs / rhs, expected)

  // negative / positive = negative
  lhs = BigInt("-340282366920938463481821351505477763074")!
  rhs = BigInt("18446744073709551629")!
  expected = BigInt("-18446744073709551604")!
  XCTAssertEqual(lhs / rhs, expected)
}

LiarPrincess avatar Jan 18 '23 15:01 LiarPrincess

I added the test for rounding toward 0: |quotient * rhs| <= |lhs|, but the current implementation of BigInt.magnitude does not work, so this test will fail though not because of div.

LiarPrincess avatar Feb 03 '23 12:02 LiarPrincess