Skip to content

Commit e081f2c

Browse files
committed
Fixed CGRect : Equatable
Fix for 2 bugs: 1. ```swift CGRect(x: 0, y: 0, width: -5, height: -5) == CGRect(x: -5, y: -5, width: 5, height: 5) // used to return false // should standardize before comparison first ``` 2. ```swift let a = CGRect(x: CGFloat.infinity, y: 1, width: 2, height: 3) let b = CGRect(x: 1, y: CGFloat.infinity, width: 2, height: 3) a == b // used to return false // should return true if both `a` and `b` have any origin's value set to +∞ ```
1 parent 2b82be3 commit e081f2c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

Foundation/NSGeometry.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,11 @@ extension CGRect {
399399

400400
extension CGRect: Equatable {
401401
public static func ==(lhs: CGRect, rhs: CGRect) -> Bool {
402-
return lhs.origin == rhs.origin && lhs.size == rhs.size
402+
if lhs.isNull && rhs.isNull { return true }
403+
404+
let r1 = lhs.standardized
405+
let r2 = rhs.standardized
406+
return r1.origin == r2.origin && r1.size == r2.size
403407
}
404408
}
405409

TestFoundation/TestNSGeometry.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class TestNSGeometry : XCTestCase {
5858
("test_CGRect_IsNull", test_CGRect_IsNull),
5959
("test_CGRect_IsInfinite", test_CGRect_IsInfinite),
6060
("test_CGRect_IsEmpty", test_CGRect_IsEmpty),
61+
("test_CGRect_Equatable", test_CGRect_Equatable),
6162
("test_CGRect_CalculatedGeometricProperties", test_CGRect_CalculatedGeometricProperties),
6263
("test_CGRect_Standardized", test_CGRect_Standardized),
6364
("test_CGRect_Integral", test_CGRect_Integral),
@@ -307,6 +308,37 @@ class TestNSGeometry : XCTestCase {
307308
XCTAssertFalse(CGRect.infinite.isEmpty)
308309
}
309310

311+
func test_CGRect_Equatable() {
312+
XCTAssertEqual(CGRect(x: 10, y: 20, width: 30, height: 40), CGRect(x: 10, y: 20, width: 30, height: 40))
313+
XCTAssertEqual(CGRect(x: -10, y: -20, width: -30, height: -40), CGRect(x: -10, y: -20, width: -30, height: -40))
314+
XCTAssertEqual(CGRect(x: -10, y: -20, width: 30, height: 40), CGRect(x: 20, y: 20, width: -30, height: -40))
315+
316+
XCTAssertNotEqual(CGRect(x: 10, y: 20, width: 30, height: 40), CGRect(x: 10, y: 20, width: 30, height: -40))
317+
XCTAssertNotEqual(CGRect(x: 10, y: 20, width: 30, height: 40), CGRect(x: 10, y: 20, width: -30, height: 40))
318+
XCTAssertNotEqual(CGRect(x: 10, y: 20, width: 30, height: 40), CGRect(x: 10, y: -20, width: 30, height: 40))
319+
XCTAssertNotEqual(CGRect(x: 10, y: 20, width: 30, height: 40), CGRect(x: -10, y: 20, width: 30, height: 40))
320+
321+
XCTAssertEqual(CGRect.infinite, CGRect.infinite)
322+
XCTAssertEqual(CGRect.null, CGRect.null)
323+
XCTAssertNotEqual(CGRect.infinite, CGRect.null)
324+
325+
var r1 = CGRect.null
326+
r1.size = CGSize(width: 20, height: 20)
327+
XCTAssertEqual(r1, CGRect.null)
328+
329+
var r2 = CGRect.null
330+
r2.origin.x = 20
331+
XCTAssertEqual(r2, CGRect.null)
332+
333+
var r3 = CGRect.null
334+
r3.origin.y = 20
335+
XCTAssertEqual(r3, CGRect.null)
336+
337+
var r4 = CGRect.null
338+
r4.origin = CGPoint(x: 10, y: 20)
339+
XCTAssertNotEqual(r4, CGRect.null)
340+
}
341+
310342
func test_CGRect_CalculatedGeometricProperties() {
311343
let ε = CGFloat(0.00001)
312344

0 commit comments

Comments
 (0)