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 +∞
```
diff --git a/Foundation/NSGeometry.swift b/Foundation/NSGeometry.swift
index 62eefd9..3420df6 100644
--- a/Foundation/NSGeometry.swift
+++ b/Foundation/NSGeometry.swift
@@ -399,7 +399,11 @@
extension CGRect: Equatable {
public static func ==(lhs: CGRect, rhs: CGRect) -> Bool {
- return lhs.origin == rhs.origin && lhs.size == rhs.size
+ if lhs.isNull && rhs.isNull { return true }
+
+ let r1 = lhs.standardized
+ let r2 = rhs.standardized
+ return r1.origin == r2.origin && r1.size == r2.size
}
}
diff --git a/TestFoundation/TestNSGeometry.swift b/TestFoundation/TestNSGeometry.swift
index bc10e42..3286cac 100644
--- a/TestFoundation/TestNSGeometry.swift
+++ b/TestFoundation/TestNSGeometry.swift
@@ -58,6 +58,7 @@
("test_CGRect_IsNull", test_CGRect_IsNull),
("test_CGRect_IsInfinite", test_CGRect_IsInfinite),
("test_CGRect_IsEmpty", test_CGRect_IsEmpty),
+ ("test_CGRect_Equatable", test_CGRect_Equatable),
("test_CGRect_CalculatedGeometricProperties", test_CGRect_CalculatedGeometricProperties),
("test_CGRect_Standardized", test_CGRect_Standardized),
("test_CGRect_Integral", test_CGRect_Integral),
@@ -307,6 +308,37 @@
XCTAssertFalse(CGRect.infinite.isEmpty)
}
+ func test_CGRect_Equatable() {
+ XCTAssertEqual(CGRect(x: 10, y: 20, width: 30, height: 40), CGRect(x: 10, y: 20, width: 30, height: 40))
+ XCTAssertEqual(CGRect(x: -10, y: -20, width: -30, height: -40), CGRect(x: -10, y: -20, width: -30, height: -40))
+ XCTAssertEqual(CGRect(x: -10, y: -20, width: 30, height: 40), CGRect(x: 20, y: 20, width: -30, height: -40))
+
+ XCTAssertNotEqual(CGRect(x: 10, y: 20, width: 30, height: 40), CGRect(x: 10, y: 20, width: 30, height: -40))
+ XCTAssertNotEqual(CGRect(x: 10, y: 20, width: 30, height: 40), CGRect(x: 10, y: 20, width: -30, height: 40))
+ XCTAssertNotEqual(CGRect(x: 10, y: 20, width: 30, height: 40), CGRect(x: 10, y: -20, width: 30, height: 40))
+ XCTAssertNotEqual(CGRect(x: 10, y: 20, width: 30, height: 40), CGRect(x: -10, y: 20, width: 30, height: 40))
+
+ XCTAssertEqual(CGRect.infinite, CGRect.infinite)
+ XCTAssertEqual(CGRect.null, CGRect.null)
+ XCTAssertNotEqual(CGRect.infinite, CGRect.null)
+
+ var r1 = CGRect.null
+ r1.size = CGSize(width: 20, height: 20)
+ XCTAssertEqual(r1, CGRect.null)
+
+ var r2 = CGRect.null
+ r2.origin.x = 20
+ XCTAssertEqual(r2, CGRect.null)
+
+ var r3 = CGRect.null
+ r3.origin.y = 20
+ XCTAssertEqual(r3, CGRect.null)
+
+ var r4 = CGRect.null
+ r4.origin = CGPoint(x: 10, y: 20)
+ XCTAssertNotEqual(r4, CGRect.null)
+ }
+
func test_CGRect_CalculatedGeometricProperties() {
let ε = CGFloat(0.00001)