Skip to content

Commit 50d03ae

Browse files
authored
Rect: correct equality implementation
Equality for Rect needs to take into account standardization of the rectangle. This adjusts the implementation accordingly. In order to account for the recursion between `isNull`, `standardized` and `==`, extract a comparator for `Rect`.
1 parent 41e1199 commit 50d03ae

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Sources/SwiftWin32/CG/Rect.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,23 @@ public struct Rect {
206206

207207
/// Returns whether the rectangle is equal to the null rectangle.
208208
public var isNull: Bool {
209-
return self == .null
209+
return self.pureEqualityCheck(.null)
210+
}
211+
212+
@inline(__always) private func pureEqualityCheck(_ rhs: Rect)
213+
-> Bool {
214+
return (self.origin == rhs.origin) && (self.size == rhs.size)
210215
}
211216
}
212217

218+
219+
213220
extension Rect: Equatable {
221+
// MARK - Operator Functions
222+
public static func == (lhs: Rect, rhs: Rect) -> Bool {
223+
let lhs: Rect = lhs.standardized, rhs: Rect = rhs.standardized
224+
return lhs.pureEqualityCheck(rhs)
225+
}
214226
}
215227

216228
extension Rect: CustomDebugStringConvertible {

Tests/CoreGraphicsTests/CoreGraphicsTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,19 @@ final class CoreGraphicsTests: XCTestCase {
203203
XCTAssertFalse(r4.intersects(r1))
204204
}
205205

206+
func testRectNonstandardEquality() {
207+
let r1: Rect = Rect(x: 0, y: 0, width: 10, height: 10)
208+
let r2: Rect = Rect(x: 10, y: 10, width: -10, height: -10)
209+
let r3: Rect = Rect(x: 10, y: 10, width: -9.9, height: -10)
210+
211+
XCTAssertTrue(r1 == r2)
212+
XCTAssertFalse(r2 == r3)
213+
214+
let null1: Rect = Rect.null
215+
let null2: Rect = Rect.null
216+
XCTAssertTrue(null1 == null2)
217+
}
218+
206219
static var allTests = [
207220
("testAffineTransformIdentity", testAffineTransformIdentity),
208221
("testAffineTransformIdentityIsIdentity", testAffineTransformIdentityIsIdentity),
@@ -216,5 +229,6 @@ final class CoreGraphicsTests: XCTestCase {
216229
("testRectInsetBy", testRectInsetBy),
217230
("testRectIntersection", testRectIntersection),
218231
("testRectIntersects", testRectIntersects),
232+
("testRectNonstandardEquality", testRectNonstandardEquality),
219233
]
220234
}

0 commit comments

Comments
 (0)