Skip to content

Commit c18cda8

Browse files
authored
Merge pull request swiftlang#1135 from bubski/cg-api
2 parents 8715257 + 43f5a34 commit c18cda8

File tree

3 files changed

+146
-6
lines changed

3 files changed

+146
-6
lines changed

Foundation/NSGeometry.swift

Lines changed: 58 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
@@ -103,6 +117,20 @@ public struct CGSize {
103117
}
104118
}
105119

120+
extension CGSize {
121+
public static var zero: CGSize {
122+
return CGSize(width: CGFloat(0), height: CGFloat(0))
123+
}
124+
125+
public init(width: Int, height: Int) {
126+
self.init(width: CGFloat(width), height: CGFloat(height))
127+
}
128+
129+
public init(width: Double, height: Double) {
130+
self.init(width: CGFloat(width), height: CGFloat(height))
131+
}
132+
}
133+
106134
extension CGSize: Equatable {
107135
public static func ==(lhs: CGSize, rhs: CGSize) -> Bool {
108136
return lhs.width == rhs.width && lhs.height == rhs.height
@@ -181,6 +209,36 @@ public struct CGRect {
181209
}
182210
}
183211

212+
extension CGRect {
213+
public static var zero: CGRect {
214+
return CGRect(origin: CGPoint(), size: CGSize())
215+
}
216+
217+
public init(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) {
218+
self.init(origin: CGPoint(x: x, y: y), size: CGSize(width: width, height: height))
219+
}
220+
221+
public init(x: Double, y: Double, width: Double, height: Double) {
222+
self.init(origin: CGPoint(x: x, y: y), size: CGSize(width: width, height: height))
223+
}
224+
225+
public init(x: Int, y: Int, width: Int, height: Int) {
226+
self.init(origin: CGPoint(x: x, y: y), size: CGSize(width: width, height: height))
227+
}
228+
}
229+
230+
extension CGRect {
231+
public static let null = CGRect(x: CGFloat.infinity,
232+
y: CGFloat.infinity,
233+
width: CGFloat(0),
234+
height: CGFloat(0))
235+
236+
public static let infinite = CGRect(x: -CGFloat.greatestFiniteMagnitude / 2,
237+
y: -CGFloat.greatestFiniteMagnitude / 2,
238+
width: CGFloat.greatestFiniteMagnitude,
239+
height: CGFloat.greatestFiniteMagnitude)
240+
}
241+
184242
extension CGRect: Equatable {
185243
public static func ==(lhs: CGRect, rhs: CGRect) -> Bool {
186244
return lhs.origin == rhs.origin && lhs.size == rhs.size

TestFoundation/TestCodable.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,21 @@ func expectRoundTripEquality<T : Codable>(of value: T, encode: (T) throws -> Dat
5252
}
5353

5454
func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T) where T : Equatable {
55+
let inf = "INF", negInf = "-INF", nan = "NaN"
5556
let encode = { (_ value: T) throws -> Data in
56-
return try JSONEncoder().encode(value)
57+
let encoder = JSONEncoder()
58+
encoder.nonConformingFloatEncodingStrategy = .convertToString(positiveInfinity: inf,
59+
negativeInfinity: negInf,
60+
nan: nan)
61+
return try encoder.encode(value)
5762
}
5863

5964
let decode = { (_ data: Data) throws -> T in
60-
return try JSONDecoder().decode(T.self, from: data)
65+
let decoder = JSONDecoder()
66+
decoder.nonConformingFloatDecodingStrategy = .convertFromString(positiveInfinity: inf,
67+
negativeInfinity: negInf,
68+
nan: nan)
69+
return try decoder.decode(T.self, from: data)
6170
}
6271

6372
expectRoundTripEquality(of: value, encode: encode, decode: decode)
@@ -224,6 +233,7 @@ class TestCodable : XCTestCase {
224233
// MARK: - CGPoint
225234
lazy var cgpointValues: [CGPoint] = [
226235
CGPoint(),
236+
CGPoint.zero,
227237
CGPoint(x: 10, y: 20),
228238
CGPoint(x: -10, y: -20),
229239
// Disabled due to limit on magnitude in JSON. See SR-5346
@@ -239,6 +249,7 @@ class TestCodable : XCTestCase {
239249
// MARK: - CGSize
240250
lazy var cgsizeValues: [CGSize] = [
241251
CGSize(),
252+
CGSize.zero,
242253
CGSize(width: 30, height: 40),
243254
CGSize(width: -30, height: -40),
244255
// Disabled due to limit on magnitude in JSON. See SR-5346
@@ -254,13 +265,12 @@ class TestCodable : XCTestCase {
254265
// MARK: - CGRect
255266
lazy var cgrectValues: [CGRect] = [
256267
CGRect(),
268+
CGRect.zero,
257269
CGRect(origin: CGPoint(x: 10, y: 20), size: CGSize(width: 30, height: 40)),
258270
CGRect(origin: CGPoint(x: -10, y: -20), size: CGSize(width: -30, height: -40)),
271+
CGRect.null,
259272
// Disabled due to limit on magnitude in JSON. See SR-5346
260-
// CGRect(origin: CGPoint(x: -.greatestFiniteMagnitude / 2,
261-
// y: -.greatestFiniteMagnitude / 2),
262-
// size: CGSize(width: .greatestFiniteMagnitude,
263-
// height: .greatestFiniteMagnitude)),
273+
// CGRect.infinite
264274
]
265275

266276
func test_CGRect_JSON() {

TestFoundation/TestNSGeometry.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ 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),
32+
("test_CGSize_ExtendedConstruction", test_CGSize_ExtendedConstruction),
3133
("test_CGRect_BasicConstruction", test_CGRect_BasicConstruction),
34+
("test_CGRect_ExtendedConstruction", test_CGRect_ExtendedConstruction),
35+
("test_CGRect_SpecialValues", test_CGRect_SpecialValues),
3236
("test_NSEdgeInsets_BasicConstruction", test_NSEdgeInsets_BasicConstruction),
3337
("test_NSEdgeInsetsEqual", test_NSEdgeInsetsEqual),
3438
("test_NSMakePoint", test_NSMakePoint),
@@ -112,6 +116,20 @@ class TestNSGeometry : XCTestCase {
112116
XCTAssertEqual(p2.x, CGFloat(3.6))
113117
XCTAssertEqual(p2.y, CGFloat(4.5))
114118
}
119+
120+
func test_CGPoint_ExtendedConstruction() {
121+
let p1 = CGPoint.zero
122+
XCTAssertEqual(p1.x, CGFloat(0))
123+
XCTAssertEqual(p1.y, CGFloat(0))
124+
125+
let p2 = CGPoint(x: Int(3), y: Int(4))
126+
XCTAssertEqual(p2.x, CGFloat(3))
127+
XCTAssertEqual(p2.y, CGFloat(4))
128+
129+
let p3 = CGPoint(x: Double(3.6), y: Double(4.5))
130+
XCTAssertEqual(p3.x, CGFloat(3.6))
131+
XCTAssertEqual(p3.y, CGFloat(4.5))
132+
}
115133

116134
func test_CGSize_BasicConstruction() {
117135
let s1 = CGSize()
@@ -122,6 +140,20 @@ class TestNSGeometry : XCTestCase {
122140
XCTAssertEqual(s2.width, CGFloat(3.6))
123141
XCTAssertEqual(s2.height, CGFloat(4.5))
124142
}
143+
144+
func test_CGSize_ExtendedConstruction() {
145+
let s1 = CGSize.zero
146+
XCTAssertEqual(s1.width, CGFloat(0))
147+
XCTAssertEqual(s1.height, CGFloat(0))
148+
149+
let s2 = CGSize(width: Int(3), height: Int(4))
150+
XCTAssertEqual(s2.width, CGFloat(3))
151+
XCTAssertEqual(s2.height, CGFloat(4))
152+
153+
let s3 = CGSize(width: Double(3.6), height: Double(4.5))
154+
XCTAssertEqual(s3.width, CGFloat(3.6))
155+
XCTAssertEqual(s3.height, CGFloat(4.5))
156+
}
125157

126158
func test_CGRect_BasicConstruction() {
127159
let r1 = CGRect()
@@ -138,6 +170,46 @@ class TestNSGeometry : XCTestCase {
138170
XCTAssertEqual(r2.size.width, s.width)
139171
XCTAssertEqual(r2.size.height, s.height)
140172
}
173+
174+
func test_CGRect_ExtendedConstruction() {
175+
let r1 = CGRect.zero
176+
XCTAssertEqual(r1.origin.x, CGFloat(0.0))
177+
XCTAssertEqual(r1.origin.y, CGFloat(0.0))
178+
XCTAssertEqual(r1.size.width, CGFloat(0.0))
179+
XCTAssertEqual(r1.size.height, CGFloat(0.0))
180+
181+
let r2 = CGRect(x: CGFloat(1.2), y: CGFloat(2.3), width: CGFloat(3.4), height: CGFloat(4.5))
182+
XCTAssertEqual(r2.origin.x, CGFloat(1.2))
183+
XCTAssertEqual(r2.origin.y, CGFloat(2.3))
184+
XCTAssertEqual(r2.size.width, CGFloat(3.4))
185+
XCTAssertEqual(r2.size.height, CGFloat(4.5))
186+
187+
let r3 = CGRect(x: Double(1.2), y: Double(2.3), width: Double(3.4), height: Double(4.5))
188+
XCTAssertEqual(r3.origin.x, CGFloat(1.2))
189+
XCTAssertEqual(r3.origin.y, CGFloat(2.3))
190+
XCTAssertEqual(r3.size.width, CGFloat(3.4))
191+
XCTAssertEqual(r3.size.height, CGFloat(4.5))
192+
193+
let r4 = CGRect(x: Int(1), y: Int(2), width: Int(3), height: Int(4))
194+
XCTAssertEqual(r4.origin.x, CGFloat(1))
195+
XCTAssertEqual(r4.origin.y, CGFloat(2))
196+
XCTAssertEqual(r4.size.width, CGFloat(3))
197+
XCTAssertEqual(r4.size.height, CGFloat(4))
198+
}
199+
200+
func test_CGRect_SpecialValues() {
201+
let r1 = CGRect.null
202+
XCTAssertEqual(r1.origin.x, CGFloat.infinity)
203+
XCTAssertEqual(r1.origin.y, CGFloat.infinity)
204+
XCTAssertEqual(r1.size.width, CGFloat(0.0))
205+
XCTAssertEqual(r1.size.height, CGFloat(0.0))
206+
207+
let r2 = CGRect.infinite
208+
XCTAssertEqual(r2.origin.x, -CGFloat.greatestFiniteMagnitude / 2)
209+
XCTAssertEqual(r2.origin.y, -CGFloat.greatestFiniteMagnitude / 2)
210+
XCTAssertEqual(r2.size.width, CGFloat.greatestFiniteMagnitude)
211+
XCTAssertEqual(r2.size.height, CGFloat.greatestFiniteMagnitude)
212+
}
141213

142214
func test_NSEdgeInsets_BasicConstruction() {
143215
let i1 = NSEdgeInsets()

0 commit comments

Comments
 (0)