Skip to content

Commit 5b6ff9a

Browse files
committed
add test case
1 parent 45b6f88 commit 5b6ff9a

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Tests/BigNumberTests/BIntTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,33 @@ class BIntTests: XCTestCase {
185185
XCTAssertEqual(a, 15)
186186
}
187187

188+
/// Bug: comparing an Int variable with a BInt whose value exceeds Int.max
189+
/// uses the stdlib cross-type BinaryInteger comparison, which reads `words`.
190+
/// If `words` doesn't return proper two's complement, the stdlib misinterprets
191+
/// a large positive BInt as negative.
192+
func testCrossTypeComparisonWithLetConstant() {
193+
let bint = BInt("11173433833219812840")!
194+
195+
// Literal comparison — Swift can infer 100 as BInt, uses BInt<BInt (correct)
196+
XCTAssertTrue(100 < bint, "literal 100 should be less than bint")
197+
198+
// Variable comparison — uses stdlib's generic BinaryInteger<BinaryInteger (buggy path)
199+
let hundred = 100
200+
XCTAssertTrue(hundred < bint, "Int variable 100 should be less than bint")
201+
202+
// Additional edge cases: values around 2^63
203+
let edgeBint = BInt(1) << 63 // 2^63, exceeds Int.max
204+
let fifty = 50
205+
XCTAssertTrue(fifty < edgeBint, "Int variable should be less than 2^63")
206+
XCTAssertTrue(0 < edgeBint, "zero should be less than 2^63")
207+
let zero = 0
208+
XCTAssertTrue(zero < edgeBint, "Int variable 0 should be less than 2^63")
209+
210+
// Negative BInt should be less than positive Int
211+
let negativeBint = BInt("-11173433833219812840")!
212+
let ten = 10
213+
XCTAssertTrue(negativeBint < ten, "negative bint should be less than Int 10")
214+
XCTAssertFalse(ten < negativeBint, "Int 10 should not be less than negative bint")
215+
}
216+
188217
}

0 commit comments

Comments
 (0)