swift-numerics
swift-numerics copied to clipboard
[BigInt tests] ❌🐰 Int properties
trafficstars
Please read the #242 Using tests from “Violet - Python VM written in Swift” before.
🐰 Discussion
-
bitWidth- current implementation rounds up to nearest multiple of 64 (I assume that this is becauseUInt.bitWidth == 64on my platform). In Violet I went with counting min required bit count to represent a given number.func test_trivial() { let zero = BigInt(0) XCTAssertEqual(zero.bitWidth, 1) // 0 is just 0 let plus1 = BigInt(1) XCTAssertEqual(plus1.bitWidth, 2) // 1 needs '0' prefix -> '01' let minus1 = BigInt(-1) XCTAssertEqual(minus1.bitWidth, 1) // -1 is just 1 } -
trailingZeroBitCount- what should be the value for 0? Current implementation gives64(same reason as forbitWidth), Violet returns1.// There is an edge case for '0': // - 'int' is finite, so they can return 'bitWidth' // - 'BigInt' is infinite, but we can't return this // // So, trailingZeroBitCount is equal to bitWidth, see: // https://developer.apple.com/documentation/swift/binaryinteger/trailingzerobitcount func test_zero() { let zero = BigInt(0) XCTAssertEqual(zero.trailingZeroBitCount, 1) }
Let me know what you think, I will fix the tests.
❌ Failures
func test_magnitude() {
let big = BigInt(-9223372036854775808)
let expect = UInt64(9223372036854775808)
XCTAssertEqual(big.magnitude, BigInt(expect))
}