mirror of
https://github.com/apple/swift-numerics
synced 2026-11-25 04:47:53 +00:00
Fix implementation of <
Previously, all BigInts were less than themselves. This is no longer true. Discovered thanks to @lorentey and @benrimmington re: discussion on explicit implementation of `!=`.
This commit is contained in:
1 parent
7ca51a3e50
commit
fac716fdb2
2 files changed
+24
-2
No files matched your search
@@ -77,11 +77,15 @@ extension BigInt: Comparable {
|
||||
}
|
||||
|
||||
for i in stride(from: lhs.words.count - 1, through: 0, by: -1) {
|
||||
if lhs.words[i] > rhs.words[i] { return false }
|
||||
if lhs.words[i] > rhs.words[i] {
|
||||
return false
|
||||
} else if lhs.words[i] < rhs.words[i] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -111,6 +111,24 @@ final class BigIntTests: XCTestCase {
|
||||
baz.negate()
|
||||
XCTAssertEqual(baz, bar)
|
||||
}
|
||||
|
||||
func testComparable() {
|
||||
let foo = BigInt("1234567890123456789012345678901234567890")!
|
||||
let bar = foo * foo
|
||||
|
||||
XCTAssertLessThan(foo, bar)
|
||||
XCTAssertFalse(foo < foo)
|
||||
XCTAssertFalse(bar < bar)
|
||||
XCTAssertFalse(foo > foo)
|
||||
XCTAssertFalse(bar > bar)
|
||||
XCTAssertGreaterThan(bar, foo)
|
||||
|
||||
let baz = bar * -1
|
||||
|
||||
XCTAssertLessThan(baz, foo)
|
||||
XCTAssertNotEqual(bar, baz)
|
||||
XCTAssertFalse(baz < baz)
|
||||
}
|
||||
|
||||
func testCodable() throws {
|
||||
let lowerBound = BigInt("-1234567890123456789012345678901234567890")!
|
||||
|
||||
Reference in new issue
Block a user