Skip to content

Commit 3a29017

Browse files
committed
Merge pr/47
2 parents bf7e3ff + c184cf8 commit 3a29017

File tree

4 files changed

+178
-26
lines changed

4 files changed

+178
-26
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
5BF7AEC11BCD51F9008F214A /* NSValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDC3F4C1BCC5DCB00ED97BB /* NSValue.swift */; };
198198
844DC3331C17584F005611F9 /* TestNSScanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 844DC3321C17584F005611F9 /* TestNSScanner.swift */; };
199199
84BA558E1C16F90900F48C54 /* TestNSTimeZone.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84BA558D1C16F90900F48C54 /* TestNSTimeZone.swift */; };
200+
88D28DE71C13AE9000494606 /* TestNSGeometry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88D28DE61C13AE9000494606 /* TestNSGeometry.swift */; };
200201
C93559291C12C49F009FD6A9 /* TestNSAffineTransform.swift in Sources */ = {isa = PBXBuildFile; fileRef = C93559281C12C49F009FD6A9 /* TestNSAffineTransform.swift */; };
201202
DCDBB8331C1768AC00313299 /* TestNSData.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCDBB8321C1768AC00313299 /* TestNSData.swift */; };
202203
E876A73E1C1180E000F279EC /* TestNSRange.swift in Sources */ = {isa = PBXBuildFile; fileRef = E876A73D1C1180E000F279EC /* TestNSRange.swift */; };
@@ -524,6 +525,7 @@
524525
5BF7AEC21BCD568D008F214A /* ForSwiftFoundationOnly.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ForSwiftFoundationOnly.h; sourceTree = "<group>"; };
525526
844DC3321C17584F005611F9 /* TestNSScanner.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSScanner.swift; sourceTree = "<group>"; };
526527
84BA558D1C16F90900F48C54 /* TestNSTimeZone.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSTimeZone.swift; sourceTree = "<group>"; };
528+
88D28DE61C13AE9000494606 /* TestNSGeometry.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSGeometry.swift; sourceTree = "<group>"; };
527529
C93559281C12C49F009FD6A9 /* TestNSAffineTransform.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSAffineTransform.swift; sourceTree = "<group>"; };
528530
DCDBB8321C1768AC00313299 /* TestNSData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSData.swift; sourceTree = "<group>"; };
529531
E876A73D1C1180E000F279EC /* TestNSRange.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSRange.swift; sourceTree = "<group>"; };
@@ -1016,9 +1018,12 @@
10161018
children = (
10171019
C93559281C12C49F009FD6A9 /* TestNSAffineTransform.swift */,
10181020
EA66F63C1BF1619600136161 /* TestNSArray.swift */,
1021+
5BC1D8BC1BF3ADFE009D3973 /* TestNSCharacterSet.swift */,
10191022
52829AD61C160D64003BC4EF /* TestNSCalendar.swift */,
10201023
EA66F63D1BF1619600136161 /* TestNSDictionary.swift */,
10211024
848A30571C137B3500C83206 /* TestNSHTTPCookie.swift */,
1025+
525AECEB1BF2C96400D15BB0 /* TestNSFileManager.swift */,
1026+
88D28DE61C13AE9000494606 /* TestNSGeometry.swift */,
10221027
EA66F63E1BF1619600136161 /* TestNSIndexSet.swift */,
10231028
EA66F63F1BF1619600136161 /* TestNSNumber.swift */,
10241029
4DC1D07F1C12EEEF00B5948A /* TestNSPipe.swift */,
@@ -1027,8 +1032,6 @@
10271032
EA66F6411BF1619600136161 /* TestNSSet.swift */,
10281033
EA66F6421BF1619600136161 /* TestNSString.swift */,
10291034
EA66F6431BF1619600136161 /* TestNSURL.swift */,
1030-
5BC1D8BC1BF3ADFE009D3973 /* TestNSCharacterSet.swift */,
1031-
525AECEB1BF2C96400D15BB0 /* TestNSFileManager.swift */,
10321035
5B40F9F11C125187000E72E3 /* TestNSXMLParser.swift */,
10331036
22B9C1E01C165D7A00DECFF9 /* TestNSDate.swift */,
10341037
DCDBB8321C1768AC00313299 /* TestNSData.swift */,
@@ -1734,6 +1737,7 @@
17341737
5BC1D8BE1BF3B09E009D3973 /* TestNSCharacterSet.swift in Sources */,
17351738
EA66F6561BF1619600136161 /* TestNSString.swift in Sources */,
17361739
5B40F9F21C125187000E72E3 /* TestNSXMLParser.swift in Sources */,
1740+
88D28DE71C13AE9000494606 /* TestNSGeometry.swift in Sources */,
17371741
EA66F64C1BF1619600136161 /* TestNSDictionary.swift in Sources */,
17381742
EA66F6581BF1619600136161 /* TestNSURL.swift in Sources */,
17391743
EA66F6441BF1619600136161 /* main.swift in Sources */,

Foundation/NSGeometry.swift

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public struct CGPoint {
4747
public var x: CGFloat
4848
public var y: CGFloat
4949
public init() {
50-
self.x = CGFloat()
51-
self.y = CGFloat()
50+
self.init(x: CGFloat(), y: CGFloat())
5251
}
5352
public init(x: CGFloat, y: CGFloat) {
5453
self.x = x
@@ -59,15 +58,14 @@ public struct CGPoint {
5958
extension CGPoint: Equatable { }
6059

6160
public func ==(lhs: CGPoint, rhs: CGPoint) -> Bool {
62-
return (lhs.x == rhs.x) && (lhs.y == rhs.y)
61+
return lhs.x == rhs.x && lhs.y == rhs.y
6362
}
6463

6564
public struct CGSize {
6665
public var width: CGFloat
6766
public var height: CGFloat
6867
public init() {
69-
self.width = CGFloat()
70-
self.height = CGFloat()
68+
self.init(width: CGFloat(), height: CGFloat())
7169
}
7270
public init(width: CGFloat, height: CGFloat) {
7371
self.width = width
@@ -78,15 +76,14 @@ public struct CGSize {
7876
extension CGSize: Equatable { }
7977

8078
public func ==(lhs: CGSize, rhs: CGSize) -> Bool {
81-
return (lhs.width == rhs.width) && (lhs.height == rhs.height)
79+
return lhs.width == rhs.width && lhs.height == rhs.height
8280
}
8381

8482
public struct CGRect {
8583
public var origin: CGPoint
8684
public var size: CGSize
8785
public init() {
88-
self.origin = CGPoint()
89-
self.size = CGSize()
86+
self.init(origin: CGPoint(), size: CGSize())
9087
}
9188
public init(origin: CGPoint, size: CGSize) {
9289
self.origin = origin
@@ -97,7 +94,7 @@ public struct CGRect {
9794
extension CGRect: Equatable { }
9895

9996
public func ==(lhs: CGRect, rhs: CGRect) -> Bool {
100-
return (lhs.origin == rhs.origin) && (lhs.size == rhs.size)
97+
return lhs.origin == rhs.origin && lhs.size == rhs.size
10198
}
10299

103100
public typealias NSPoint = CGPoint
@@ -149,10 +146,7 @@ public struct NSEdgeInsets {
149146
public var right: CGFloat
150147

151148
public init() {
152-
self.top = CGFloat()
153-
self.left = CGFloat()
154-
self.bottom = CGFloat()
155-
self.right = CGFloat()
149+
self.init(top: CGFloat(), left: CGFloat(), bottom: CGFloat(), right: CGFloat())
156150
}
157151

158152
public init(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) {
@@ -187,8 +181,9 @@ public struct NSAlignmentOptions : OptionSetType {
187181
public static let AlignMaxYNearest = NSAlignmentOptions(rawValue: 1 << 19)
188182
public static let AlignWidthNearest = NSAlignmentOptions(rawValue: 1 << 20)
189183
public static let AlignHeightNearest = NSAlignmentOptions(rawValue: 1 << 21)
190-
191-
public static let AlignRectFlipped = NSAlignmentOptions(rawValue: 1 << 63) // pass this if the rect is in a flipped coordinate system. This allows 0.5 to be treated in a visually consistent way.
184+
185+
// pass this if the rect is in a flipped coordinate system. This allows 0.5 to be treated in a visually consistent way.
186+
public static let AlignRectFlipped = NSAlignmentOptions(rawValue: 1 << 63)
192187

193188
// convenience combinations
194189
public static let AlignAllEdgesInward = [NSAlignmentOptions.AlignMinXInward, NSAlignmentOptions.AlignMaxXInward, NSAlignmentOptions.AlignMinYInward, NSAlignmentOptions.AlignMaxYInward]
@@ -201,11 +196,17 @@ public let NSZeroSize: NSSize = NSSize()
201196
public let NSZeroRect: NSRect = NSRect()
202197
public let NSEdgeInsetsZero: NSEdgeInsets = NSEdgeInsets()
203198

204-
public func NSMakePoint(x: CGFloat, _ y: CGFloat) -> NSPoint { return NSPoint(x: x, y: y) }
199+
public func NSMakePoint(x: CGFloat, _ y: CGFloat) -> NSPoint {
200+
return NSPoint(x: x, y: y)
201+
}
205202

206-
public func NSMakeSize(w: CGFloat, _ h: CGFloat) -> NSSize { return NSSize(width: w, height: h) }
203+
public func NSMakeSize(w: CGFloat, _ h: CGFloat) -> NSSize {
204+
return NSSize(width: w, height: h)
205+
}
207206

208-
public func NSMakeRect(x: CGFloat, _ y: CGFloat, _ w: CGFloat, _ h: CGFloat) -> NSRect { return NSRect(origin: NSPoint(x: x, y: y), size: NSSize(width: w, height: h)) }
207+
public func NSMakeRect(x: CGFloat, _ y: CGFloat, _ w: CGFloat, _ h: CGFloat) -> NSRect {
208+
return NSRect(origin: NSPoint(x: x, y: y), size: NSSize(width: w, height: h))
209+
}
209210

210211
public func NSMaxX(aRect: NSRect) -> CGFloat { return CGFloat(aRect.origin.x.native + aRect.size.width.native) }
211212

@@ -235,20 +236,27 @@ public func NSSizeFromCGSize(cgsize: CGSize) -> NSSize { return cgsize }
235236

236237
public func NSSizeToCGSize(nssize: NSSize) -> CGSize { return nssize }
237238

238-
public func NSEdgeInsetsMake(top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> NSEdgeInsets { return NSEdgeInsets(top: top, left: left, bottom: bottom, right: right) }
239+
public func NSEdgeInsetsMake(top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> NSEdgeInsets {
240+
return NSEdgeInsets(top: top, left: left, bottom: bottom, right: right)
241+
}
239242

240243
public func NSEqualPoints(aPoint: NSPoint, _ bPoint: NSPoint) -> Bool { return aPoint == bPoint }
244+
241245
public func NSEqualSizes(aSize: NSSize, _ bSize: NSSize) -> Bool { return aSize == bSize }
246+
242247
public func NSEqualRects(aRect: NSRect, _ bRect: NSRect) -> Bool { return aRect == bRect }
248+
243249
public func NSIsEmptyRect(aRect: NSRect) -> Bool { return (aRect.size.width.native <= 0) || (aRect.size.height.native <= 0) }
244-
public func NSEdgeInsetsEqual(aInsets: NSEdgeInsets, _ bInsets: NSEdgeInsets) -> Bool { return (aInsets.top == bInsets.top) && (aInsets.left == bInsets.left) && (aInsets.bottom == bInsets.bottom) && (aInsets.right == bInsets.right) }
250+
251+
public func NSEdgeInsetsEqual(aInsets: NSEdgeInsets, _ bInsets: NSEdgeInsets) -> Bool {
252+
return (aInsets.top == bInsets.top) && (aInsets.left == bInsets.left) && (aInsets.bottom == bInsets.bottom) && (aInsets.right == bInsets.right)
253+
}
245254

246255
public func NSInsetRect(aRect: NSRect, _ dX: CGFloat, _ dY: CGFloat) -> NSRect {
247256
let x = CGFloat(aRect.origin.x.native + dX.native)
248257
let y = CGFloat(aRect.origin.y.native + dY.native)
249258
let w = CGFloat(aRect.size.width.native - (dX.native * 2))
250259
let h = CGFloat(aRect.size.height.native - (dY.native * 2))
251-
252260
return NSMakeRect(x, y, w, h)
253261
}
254262

@@ -307,5 +315,3 @@ extension NSCoder {
307315
public func decodeSizeForKey(key: String) -> NSSize { NSUnimplemented() }
308316
public func decodeRectForKey(key: String) -> NSRect { NSUnimplemented() }
309317
}
310-
311-

TestFoundation/TestNSGeometry.swift

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
11+
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
12+
import Foundation
13+
import XCTest
14+
#else
15+
import SwiftFoundation
16+
import SwiftXCTest
17+
#endif
18+
19+
20+
class TestNSGeometry : XCTestCase {
21+
22+
var allTests : [(String, () -> ())] {
23+
return [
24+
("test_CGFloat_BasicConstruction", test_CGFloat_BasicConstruction),
25+
("test_CGFloat_Equality", test_CGFloat_Equality),
26+
("test_CGFloat_LessThanOrEqual", test_CGFloat_LessThanOrEqual),
27+
("test_CGFloat_GreaterThanOrEqual", test_CGFloat_GreaterThanOrEqual),
28+
("test_CGPoint_BasicConstruction", test_CGPoint_BasicConstruction),
29+
("test_CGSize_BasicConstruction", test_CGSize_BasicConstruction),
30+
("test_CGRect_BasicConstruction", test_CGRect_BasicConstruction),
31+
("test_NSMakePoint", test_NSMakePoint),
32+
("test_NSMakeSize", test_NSMakeSize),
33+
("test_NSMakeRect", test_NSMakeRect),
34+
]
35+
}
36+
37+
func test_CGFloat_BasicConstruction() {
38+
XCTAssertEqual(CGFloat().native, 0.0)
39+
XCTAssertEqual(CGFloat(Double(3.0)).native, 3.0)
40+
}
41+
42+
func test_CGFloat_Equality() {
43+
XCTAssertEqual(CGFloat(), CGFloat())
44+
XCTAssertEqual(CGFloat(1.0), CGFloat(1.0))
45+
XCTAssertEqual(CGFloat(-42.0), CGFloat(-42.0))
46+
47+
XCTAssertNotEqual(CGFloat(1.0), CGFloat(1.4))
48+
XCTAssertNotEqual(CGFloat(37.3), CGFloat(-42.0))
49+
XCTAssertNotEqual(CGFloat(1.345), CGFloat())
50+
}
51+
52+
func test_CGFloat_LessThanOrEqual() {
53+
let w = CGFloat(-4.5)
54+
let x = CGFloat(1.0)
55+
let y = CGFloat(2.2)
56+
57+
XCTAssertLessThanOrEqual(CGFloat(), CGFloat())
58+
XCTAssertLessThanOrEqual(w, w)
59+
XCTAssertLessThanOrEqual(y, y)
60+
61+
XCTAssertLessThan(w, x)
62+
XCTAssertLessThanOrEqual(w, x)
63+
XCTAssertLessThan(x, y)
64+
XCTAssertLessThanOrEqual(x, y)
65+
XCTAssertLessThan(w, y)
66+
XCTAssertLessThanOrEqual(w, y)
67+
}
68+
69+
func test_CGFloat_GreaterThanOrEqual() {
70+
let w = CGFloat(-4.5)
71+
let x = CGFloat(1.0)
72+
let y = CGFloat(2.2)
73+
74+
XCTAssertGreaterThanOrEqual(CGFloat(), CGFloat())
75+
XCTAssertGreaterThanOrEqual(w, w)
76+
XCTAssertGreaterThanOrEqual(y, y)
77+
78+
XCTAssertGreaterThan(x, w)
79+
XCTAssertGreaterThanOrEqual(x, w)
80+
XCTAssertGreaterThan(y, x)
81+
XCTAssertGreaterThanOrEqual(y, x)
82+
XCTAssertGreaterThan(y, w)
83+
XCTAssertGreaterThanOrEqual(y, w)
84+
}
85+
86+
func test_CGPoint_BasicConstruction() {
87+
let p1 = CGPoint()
88+
XCTAssertEqual(p1.x, CGFloat(0.0))
89+
XCTAssertEqual(p1.y, CGFloat(0.0))
90+
91+
let p2 = CGPoint(x: CGFloat(3.6), y: CGFloat(4.5))
92+
XCTAssertEqual(p2.x, CGFloat(3.6))
93+
XCTAssertEqual(p2.y, CGFloat(4.5))
94+
}
95+
96+
func test_CGSize_BasicConstruction() {
97+
let s1 = CGSize()
98+
XCTAssertEqual(s1.width, CGFloat(0.0))
99+
XCTAssertEqual(s1.height, CGFloat(0.0))
100+
101+
let s2 = CGSize(width: CGFloat(3.6), height: CGFloat(4.5))
102+
XCTAssertEqual(s2.width, CGFloat(3.6))
103+
XCTAssertEqual(s2.height, CGFloat(4.5))
104+
}
105+
106+
func test_CGRect_BasicConstruction() {
107+
let r1 = CGRect()
108+
XCTAssertEqual(r1.origin.x, CGFloat(0.0))
109+
XCTAssertEqual(r1.origin.y, CGFloat(0.0))
110+
XCTAssertEqual(r1.size.width, CGFloat(0.0))
111+
XCTAssertEqual(r1.size.height, CGFloat(0.0))
112+
113+
let p = CGPoint(x: CGFloat(2.2), y: CGFloat(3.0))
114+
let s = CGSize(width: CGFloat(5.0), height: CGFloat(5.0))
115+
let r2 = CGRect(origin: p, size: s)
116+
XCTAssertEqual(r2.origin.x, p.x)
117+
XCTAssertEqual(r2.origin.y, p.y)
118+
XCTAssertEqual(r2.size.width, s.width)
119+
XCTAssertEqual(r2.size.height, s.height)
120+
}
121+
122+
func test_NSMakePoint() {
123+
let p2 = NSMakePoint(CGFloat(3.6), CGFloat(4.5))
124+
XCTAssertEqual(p2.x, CGFloat(3.6))
125+
XCTAssertEqual(p2.y, CGFloat(4.5))
126+
}
127+
128+
func test_NSMakeSize() {
129+
let s2 = NSMakeSize(CGFloat(3.6), CGFloat(4.5))
130+
XCTAssertEqual(s2.width, CGFloat(3.6))
131+
XCTAssertEqual(s2.height, CGFloat(4.5))
132+
}
133+
134+
func test_NSMakeRect() {
135+
let r2 = NSMakeRect(CGFloat(2.2), CGFloat(3.0), CGFloat(5.0), CGFloat(5.0))
136+
XCTAssertEqual(r2.origin.x, CGFloat(2.2))
137+
XCTAssertEqual(r2.origin.y, CGFloat(3.0))
138+
XCTAssertEqual(r2.size.width, CGFloat(5.0))
139+
XCTAssertEqual(r2.size.height, CGFloat(5.0))
140+
}
141+
}

TestFoundation/main.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ XCTMain([
4040
TestNSData(),
4141
TestNSTimeZone(),
4242
TestNSScanner(),
43-
TestNSHTTPCookie()
43+
TestNSHTTPCookie(),
44+
TestNSGeometry(),
4445
])
4546

0 commit comments

Comments
 (0)