apple / swift-numerics

Advanced mathematical types and functions for Swift
Apache License 2.0
1.68k stars 145 forks source link

[BigInt tests] ❌ Nodejs tests #243

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.


Tests generated by Node.js.

It does require Node.js installed (obviously), but we could solve it with Docker. So, if you want I cann add appropriate Dockerfile.

❌ Failures

There are about 6700 failures, I will not go into details of all of them.

func test_node_div_incorrectSign() {
  // 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)
}

func test_node_mod_incorrectSign() {
  // SMALL % BIG = SMALL
  // We need to satisfy: BIG * 0 + result = SMALL -> result = SMALL
  // The same, but on the standard Swift.Int to prove the point:
  XCTAssertEqual(-1 % 123, -1)
  XCTAssertEqual(-1 % -123, -1)
  // In general the 'reminder' follows the 'lhs' sign (round toward 0).
  // Except for the case where 'lhs' is negative and 'reminder' is 0.

  var lhs = BigInt("-1")!
  var rhs = BigInt("18446744073709551615")!
  XCTAssertEqual(lhs % rhs, lhs)

  // Also fails if 'rhs' is negative
  lhs = BigInt("-7730941133")!
  rhs = BigInt("-18446744073709551615")!
  XCTAssertEqual(lhs % rhs, lhs)
}

// From my observations all of the `xor` tests are failing.
func test_node_xor() {
  var lhs = BigInt("0")!
  var rhs = BigInt("1")!
  var expected = BigInt("1")!
  XCTAssertEqual(lhs ^ rhs, expected)
  XCTAssertEqual(0 ^ 1, 1) // Proof

  lhs = BigInt("0")!
  rhs = BigInt("-1")!
  expected = BigInt("-1")!
  XCTAssertEqual(lhs ^ rhs, expected)
  XCTAssertEqual(0 ^ -1, -1) // Proof
}
mgriebling commented 1 year ago

These tests now all pass with #261 pull request.