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:
Robert Thompson committed 2020-01-16 15:15:37 -05:00
1 parent 7ca51a3e50
commit fac716fdb2
2 files changed
+24 -2

No files matched your search

+6 -2
View File
@@ -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
}
}
+18
View File
@@ -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")!