Skip to content

Commit 6940c1c

Browse files
committed
CGPoint extended initializers implemented
1 parent 79322e9 commit 6940c1c

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Foundation/NSGeometry.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ public struct CGPoint {
2525
}
2626
}
2727

28+
extension CGPoint {
29+
public static var zero: CGPoint {
30+
return CGPoint(x: CGFloat(0), y: CGFloat(0))
31+
}
32+
33+
public init(x: Int, y: Int) {
34+
self.init(x: CGFloat(x), y: CGFloat(y))
35+
}
36+
37+
public init(x: Double, y: Double) {
38+
self.init(x: CGFloat(x), y: CGFloat(y))
39+
}
40+
}
41+
2842
extension CGPoint: Equatable {
2943
public static func ==(lhs: CGPoint, rhs: CGPoint) -> Bool {
3044
return lhs.x == rhs.x && lhs.y == rhs.y

TestFoundation/TestNSGeometry.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class TestNSGeometry : XCTestCase {
2727
("test_CGFloat_LessThanOrEqual", test_CGFloat_LessThanOrEqual),
2828
("test_CGFloat_GreaterThanOrEqual", test_CGFloat_GreaterThanOrEqual),
2929
("test_CGPoint_BasicConstruction", test_CGPoint_BasicConstruction),
30+
("test_CGPoint_ExtendedConstruction", test_CGPoint_ExtendedConstruction),
3031
("test_CGSize_BasicConstruction", test_CGSize_BasicConstruction),
3132
("test_CGRect_BasicConstruction", test_CGRect_BasicConstruction),
3233
("test_NSEdgeInsets_BasicConstruction", test_NSEdgeInsets_BasicConstruction),
@@ -112,6 +113,20 @@ class TestNSGeometry : XCTestCase {
112113
XCTAssertEqual(p2.x, CGFloat(3.6))
113114
XCTAssertEqual(p2.y, CGFloat(4.5))
114115
}
116+
117+
func test_CGPoint_ExtendedConstruction() {
118+
let p1 = CGPoint.zero
119+
XCTAssertEqual(p1.x, CGFloat(0))
120+
XCTAssertEqual(p1.y, CGFloat(0))
121+
122+
let p2 = CGPoint(x: Int(3), y: Int(4))
123+
XCTAssertEqual(p2.x, CGFloat(3))
124+
XCTAssertEqual(p2.y, CGFloat(4))
125+
126+
let p3 = CGPoint(x: Double(3.6), y: Double(4.5))
127+
XCTAssertEqual(p3.x, CGFloat(3.6))
128+
XCTAssertEqual(p3.y, CGFloat(4.5))
129+
}
115130

116131
func test_CGSize_BasicConstruction() {
117132
let s1 = CGSize()

0 commit comments

Comments
 (0)