apple / swift-numerics

Advanced mathematical types and functions for Swift
Apache License 2.0
1.66k stars 140 forks source link

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

Open LiarPrincess opened 1 year ago

LiarPrincess commented 1 year ago

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 commented 1 year ago

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.