apple / swift-numerics

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

[BigInt tests] đź’€ Init from int #254

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 for all of the let n = BigInt(some_integer) variants.

đź’€ Crash

func test_initFromInt_crash() {
  // 18446744073709551615 = UInt64.max
  let int: UInt64 = 18446744073709551615
  let big = BigInt(int)
  let revert = UInt64(big)
}

❌ Failures

func test_initFromInt_exactly() {
  let int: UInt64 = 18446744073709551614
  let big = BigInt(exactly: int)!
  let revert = UInt64(exactly: big)
  XCTAssertEqual(int, revert)
}

func test_initFromInt_clamping() {
  let int: UInt64 = 18446744073709551614
  let big = BigInt(clamping: int)
  let revert = UInt64(clamping: big)
  XCTAssertEqual(int, revert)
}

func test_initFromInt_truncatingIfNeeded() {
  let int: UInt64 = 18446744073709551615
  let big = BigInt(truncatingIfNeeded: int)
  let intString = String(int, radix: 10, uppercase: false)
  let bigString = String(big, radix: 10, uppercase: false)
  XCTAssertEqual(bigString, intString)
}