From 89a414dd8b397b4fcdea651688a6d9c1bc5f2e0c Mon Sep 17 00:00:00 2001 From: Dmitri Gribenko Date: Thu, 4 Aug 2016 00:18:14 -0700 Subject: [PATCH 1/4] Remove redundant tests for SpriteKit These exact tests already exist in validation-test/stdlib/SpriteKit.swift. --- test/1_stdlib/SpriteKit.swift | 71 -------------------------- validation-test/stdlib/SpriteKit.swift | 17 +++++- 2 files changed, 15 insertions(+), 73 deletions(-) delete mode 100644 test/1_stdlib/SpriteKit.swift diff --git a/test/1_stdlib/SpriteKit.swift b/test/1_stdlib/SpriteKit.swift deleted file mode 100644 index e41ae035b8a12..0000000000000 --- a/test/1_stdlib/SpriteKit.swift +++ /dev/null @@ -1,71 +0,0 @@ -// RUN: %target-run-simple-swift | FileCheck %s -// REQUIRES: executable_test - -// REQUIRES: objc_interop - -// watchOS does not have SpriteKit. -// UNSUPPORTED: OS=watchos - -import StdlibUnittest - -import Foundation -import SpriteKit - -// Check that the subscript is there. -@available(OSX,introduced: 10.10) -@available(iOS,introduced: 8.0) -@available(tvOS,introduced: 8.0) -@available(watchOS,introduced: 2.0) -func testSubscript(_ node: SKNode) { - var _: [SKNode] = node["me"] -} - -// SKColor is NSColor on OS X and UIColor on iOS. - -var r = CGFloat(0) -var g = CGFloat(0) -var b = CGFloat(0) -var a = CGFloat(0) -var color = SKColor.red -color.getRed(&r, green:&g, blue:&b, alpha:&a) -print("color \(r) \(g) \(b) \(a)") -// CHECK: color 1.0 0.0 0.0 1.0 - -#if os(OSX) -func f(_ c: NSColor) { - print("colortastic") -} -#endif -#if os(iOS) || os(tvOS) -func f(_ c: UIColor) { - print("colortastic") -} -#endif -f(color) -// CHECK: colortastic - -var SpriteKitTests = TestSuite("SpriteKit") - -if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) { - SpriteKitTests.test("SKNode.setValue(_:forAttribute:)") { - let node = SKNode() - let attrVal = SKAttributeValue(float: 2.0) - node.setValue(attrVal, forAttribute: "test") - expectEqual(node.attributeValues["test"], attrVal) - } - - SpriteKitTests.test("SKWarpGeometryGrid") { - var warpGrid = SKWarpGeometryGrid(columns: 3, rows: 4) - expectEqual(warpGrid.numberOfColumns, 3) - - expectEqual(warpGrid.sourcePosition(at: 0).x, 0.0) - warpGrid = warpGrid.replacingBySourcePositions(positions: [float2(30.0)]) - expectEqual(warpGrid.sourcePosition(at: 0).x, 30.0) - - expectEqual(warpGrid.destPosition(at: 0).x, 0.0) - warpGrid = warpGrid.replacingByDestinationPositions(positions: [float2(30.0)]) - expectEqual(warpGrid.destPosition(at: 0).x, 30.0) - } -} - -runAllTests() diff --git a/validation-test/stdlib/SpriteKit.swift b/validation-test/stdlib/SpriteKit.swift index 6e4685fc8ec24..fe1378963fa00 100644 --- a/validation-test/stdlib/SpriteKit.swift +++ b/validation-test/stdlib/SpriteKit.swift @@ -52,9 +52,9 @@ if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) { expectEqual(node.attributeValues["test"], attrVal) } - SpriteKitTests.test("SKWarpGeometryGrid") { + SpriteKitTests.test("SKWarpGeometryGrid/1") { var warpGrid = SKWarpGeometryGrid(columns: 1, rows: 1) - expectEqual(warpGrid.numberOfColumns, 1) + expectEqual(warpGrid.numberOfColumns, 1) expectEqual(warpGrid.sourcePosition(at: 0).x, 0.0) warpGrid = warpGrid.replacingBySourcePositions(positions: [float2(1.0), float2(2.0), float2(3.0), float2(4.0)]) @@ -76,6 +76,19 @@ if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) { expectEqual(warpGrid.destPosition(at: 0).x, 1.0) expectEqual(warpGrid.sourcePosition(at: 0).x, 2.0) } + + SpriteKitTests.test("SKWarpGeometryGrid/2") { + var warpGrid = SKWarpGeometryGrid(columns: 3, rows: 4) + expectEqual(warpGrid.numberOfColumns, 3) + + expectEqual(warpGrid.sourcePosition(at: 0).x, 0.0) + warpGrid = warpGrid.replacingBySourcePositions(positions: [float2(30.0)]) + expectEqual(warpGrid.sourcePosition(at: 0).x, 30.0) + + expectEqual(warpGrid.destPosition(at: 0).x, 0.0) + warpGrid = warpGrid.replacingByDestinationPositions(positions: [float2(30.0)]) + expectEqual(warpGrid.destPosition(at: 0).x, 30.0) + } } runAllTests() From 610dc149033e3d696970a1c321f4d9f096d573bc Mon Sep 17 00:00:00 2001 From: Dmitri Gribenko Date: Wed, 3 Aug 2016 18:57:39 -0700 Subject: [PATCH 2/4] stdlib: add tests for Stride types on integers --- validation-test/stdlib/FixedPoint.swift.gyb | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/validation-test/stdlib/FixedPoint.swift.gyb b/validation-test/stdlib/FixedPoint.swift.gyb index 731480479d9f2..c6c2801887959 100644 --- a/validation-test/stdlib/FixedPoint.swift.gyb +++ b/validation-test/stdlib/FixedPoint.swift.gyb @@ -9,6 +9,7 @@ var FixedPoint = TestSuite("FixedPoint") %{ import gyb +from SwiftIntTypes import all_integer_types test_bit_patterns = [ 0x0000000000000000, @@ -61,6 +62,32 @@ def get_fixed_point_hash(bit_pattern, src_bits, word_bits): }% +//===----------------------------------------------------------------------===// +// 'Int.Stride' type +//===----------------------------------------------------------------------===// + +FixedPoint.test("Integers.Stride") { +#if arch(i386) || arch(arm) + +% for self_ty in all_integer_types(32): +% Self = self_ty.stdlib_name + expectEqualType(Int.self, ${Self}.Stride.self) +% end + +#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) + +% for self_ty in all_integer_types(64): +% Self = self_ty.stdlib_name + expectEqualType(Int.self, ${Self}.Stride.self) +% end + +#else + +_UnimplementedError() + +#endif +} + //===----------------------------------------------------------------------===// // 'Int(truncatingBitPattern:)' initializer //===----------------------------------------------------------------------===// From 593915a21aaf7e32edf74ca2fb0aa8a93b51aae1 Mon Sep 17 00:00:00 2001 From: Dmitri Gribenko Date: Wed, 3 Aug 2016 22:38:28 -0700 Subject: [PATCH 3/4] StdlibCollectionUnittest: reduce boilerplate --- .../LoggingWrappers.swift.gyb | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/stdlib/private/StdlibCollectionUnittest/LoggingWrappers.swift.gyb b/stdlib/private/StdlibCollectionUnittest/LoggingWrappers.swift.gyb index 94d11e5e8e1cf..d5548338abb25 100644 --- a/stdlib/private/StdlibCollectionUnittest/LoggingWrappers.swift.gyb +++ b/stdlib/private/StdlibCollectionUnittest/LoggingWrappers.swift.gyb @@ -12,6 +12,7 @@ %{ from gyb_stdlib_support import TRAVERSALS, collectionForTraversal +from gyb_stdlib_unittest_support import TRACE, stackTrace, trace }% import StdlibUnittest @@ -666,12 +667,7 @@ public struct ${Self}< //===----------------------------------------------------------------------===// public func expectCustomizable( - _: T, _ counters: TypeIndexed, - //===--- TRACE boilerplate ----------------------------------------------===// - _ message: @autoclosure () -> String = "", - showFrame: Bool = true, - stackTrace: SourceLocStack = SourceLocStack(), - file: String = #file, line: UInt = #line + _: T, _ counters: TypeIndexed, ${TRACE} ) where T : LoggingType, T.Base : Wrapper, T.Base : LoggingType, @@ -683,12 +679,7 @@ public func expectCustomizable( } public func expectNotCustomizable( - _: T, _ counters: TypeIndexed, - //===--- TRACE boilerplate ----------------------------------------------===// - _ message: @autoclosure () -> String = "", - showFrame: Bool = true, - stackTrace: SourceLocStack = SourceLocStack(), - file: String = #file, line: UInt = #line + _: T, _ counters: TypeIndexed, ${TRACE} ) where T : LoggingType, T.Base : Wrapper, T.Base : LoggingType, From c0ffd05a49fdf3c1d9c2e43d4092255d7d4f4433 Mon Sep 17 00:00:00 2001 From: Dmitri Gribenko Date: Thu, 4 Aug 2016 10:36:19 -0700 Subject: [PATCH 4/4] Convert CoreGraphics tests to StdlibUnittest --- test/1_stdlib/CGGeometry.swift | 242 --------- .../stdlib/CoreGraphics-execute.swift | 486 ++++++++++++++++-- 2 files changed, 457 insertions(+), 271 deletions(-) delete mode 100644 test/1_stdlib/CGGeometry.swift diff --git a/test/1_stdlib/CGGeometry.swift b/test/1_stdlib/CGGeometry.swift deleted file mode 100644 index 8bdd31067c247..0000000000000 --- a/test/1_stdlib/CGGeometry.swift +++ /dev/null @@ -1,242 +0,0 @@ -// RUN: %target-run-simple-swift | FileCheck %s -// REQUIRES: executable_test - -// REQUIRES: objc_interop - -import CoreGraphics - -func print_(_ r: CGPoint, _ prefix: String) { - print("\(prefix) \(r.x) \(r.y)") -} -func print_(_ r: CGSize, _ prefix: String) { - print("\(prefix) \(r.width) \(r.height)") -} - -func print_(_ r: CGVector, _ prefix: String) { - print("\(prefix) \(r.dx) \(r.dy)") -} - -func print_(_ r: CGRect, _ prefix: String) { - print("\(prefix) \(r.origin.x) \(r.origin.y) \(r.size.width) \(r.size.height)") -} - -let int1: Int = 1 -let int2: Int = 2 -let int3: Int = 3 -let int4: Int = 4 - -let cgfloat1: CGFloat = 1 -let cgfloat2: CGFloat = 2 -let cgfloat3: CGFloat = 3 -let cgfloat4: CGFloat = 4 - -let double1: Double = 1 -let double2: Double = 2 -let double3: Double = 3 -let double4: Double = 4 - -print("You may begin.") -// CHECK: You may begin. - - -var pt: CGPoint - -pt = CGPoint(x: 1.25, y: 2.25) -print_(pt, "named float literals") -pt = CGPoint(x: 1, y: 2) -print_(pt, "named int literals") -pt = CGPoint(x: cgfloat1, y: cgfloat2) -print_(pt, "named cgfloats") -pt = CGPoint(x: double1, y: double2) -print_(pt, "named doubles") -pt = CGPoint(x: int1, y: int2) -print_(pt, "named ints") -// CHECK-NEXT: named float literals 1.25 2.25 -// CHECK-NEXT: named int literals 1.0 2.0 -// CHECK-NEXT: named cgfloats 1.0 2.0 -// CHECK-NEXT: named doubles 1.0 2.0 -// CHECK-NEXT: named ints 1.0 2.0 - -assert(pt != CGPoint.zero) - - -var size: CGSize - -size = CGSize(width:-1.25, height:-2.25) -print_(size, "named float literals") -size = CGSize(width:-1, height:-2) -print_(size, "named int literals") -size = CGSize(width:cgfloat1, height:cgfloat2) -print_(size, "named cgfloats") -size = CGSize(width:double1, height:double2) -print_(size, "named doubles") -size = CGSize(width: int1, height: int2) -print_(size, "named ints") -// CHECK-NEXT: named float literals -1.25 -2.25 -// CHECK-NEXT: named int literals -1.0 -2.0 -// CHECK-NEXT: named cgfloats 1.0 2.0 -// CHECK-NEXT: named doubles 1.0 2.0 -// CHECK-NEXT: named ints 1.0 2.0 - -assert(size != CGSize.zero) - - -var vector: CGVector - -vector = CGVector(dx: -111.25, dy: -222.25) -print_(vector, "named float literals") -vector = CGVector(dx: -111, dy: -222) -print_(vector, "named int literals") -vector = CGVector(dx: cgfloat1, dy: cgfloat2) -print_(vector, "named cgfloats") -vector = CGVector(dx: double1, dy: double2) -print_(vector, "named doubles") -vector = CGVector(dx: int1, dy: int2) -print_(vector, "named ints") -// CHECK-NEXT: named float literals -111.25 -222.25 -// CHECK-NEXT: named int literals -111.0 -222.0 -// CHECK-NEXT: named cgfloats 1.0 2.0 -// CHECK-NEXT: named doubles 1.0 2.0 -// CHECK-NEXT: named ints 1.0 2.0 - -assert(vector != CGVector.zero) - - -var rect: CGRect - -pt = CGPoint(x: 10.25, y: 20.25) -size = CGSize(width: 30.25, height: 40.25) -rect = CGRect(origin: pt, size: size) -print_(rect, "point+size") -rect = CGRect(origin:pt, size:size) -print_(rect, "named point+size") -// CHECK-NEXT: point+size 10.25 20.25 30.25 40.25 -// CHECK-NEXT: named point+size 10.25 20.25 30.25 40.25 - -rect = CGRect(x:10.25, y:20.25, width:30.25, height:40.25) -print_(rect, "named float literals") -rect = CGRect(x:10, y:20, width:30, height:40) -print_(rect, "named int literals") -rect = CGRect(x:cgfloat1, y:cgfloat2, width:cgfloat3, height:cgfloat4) -print_(rect, "named cgfloats") -rect = CGRect(x:double1, y:double2, width:double3, height:double4) -print_(rect, "named doubles") -rect = CGRect(x:int1, y:int2, width:int3, height:int4) -print_(rect, "named ints") -// CHECK-NEXT: named float literals 10.25 20.25 30.25 40.25 -// CHECK-NEXT: named int literals 10.0 20.0 30.0 40.0 -// CHECK-NEXT: named cgfloats 1.0 2.0 3.0 4.0 -// CHECK-NEXT: named doubles 1.0 2.0 3.0 4.0 -// CHECK-NEXT: named ints 1.0 2.0 3.0 4.0 - -assert(rect == rect) -assert(rect != CGRect.zero) -assert(!rect.isNull) -assert(!rect.isEmpty) -assert(!rect.isInfinite) -assert(CGRect.null.isNull) -assert(CGRect.zero.isEmpty) -assert(CGRect.infinite.isInfinite) - - -var unstandard = CGRect(x: 10, y: 20, width: -30, height: -50) -var standard = unstandard.standardized -print_(unstandard, "unstandard") -print_(standard, "standard") -// CHECK-NEXT: unstandard 10.0 20.0 -30.0 -50.0 -// CHECK-NEXT: standard -20.0 -30.0 30.0 50.0 - -assert(unstandard.width == 30) -assert(unstandard.size.width == -30) -assert(standard.width == 30) -assert(standard.size.width == 30) - -assert(unstandard.height == 50) -assert(unstandard.size.height == -50) -assert(standard.height == 50) -assert(standard.size.height == 50) - -assert(unstandard.minX == -20) -assert(unstandard.midX == -5) -assert(unstandard.maxX == 10) - -assert(unstandard.minY == -30) -assert(unstandard.midY == -5) -assert(unstandard.maxY == 20) - -assert(unstandard == standard) -assert(unstandard.standardized == standard) - -unstandard = unstandard.standardized -print_(unstandard, "standardized unstandard") -// CHECK-NEXT: standardized unstandard -20.0 -30.0 30.0 50.0 - -rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) -print_(rect.insetBy(dx: 1, dy: -2), "insetBy") -// CHECK-NEXT: insetBy 12.25 20.25 31.25 48.25 -rect = rect.insetBy(dx: 1, dy: -2) -print_(rect, "insetInPlace") -// CHECK-NEXT: insetInPlace 12.25 20.25 31.25 48.25 - -rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) -print_(rect.offsetBy(dx: 3, dy: -4), "offsetBy") -// CHECK-NEXT: offsetBy 14.25 18.25 33.25 44.25 -rect = rect.offsetBy(dx: 3, dy: -4) -print_(rect, "offsetInPlace") -// CHECK-NEXT: offsetInPlace 14.25 18.25 33.25 44.25 - -rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) -print_(rect.integral, "integral") -// CHECK-NEXT: integral 11.0 22.0 34.0 45.0 -rect = rect.integral -print_(rect, "makeIntegralInPlace") -// CHECK-NEXT: makeIntegralInPlace 11.0 22.0 34.0 45.0 - -let smallRect = CGRect(x: 10, y: 25, width: 5, height: -5) -let bigRect = CGRect(x: 1, y: 2, width: 101, height: 102) -let distantRect = CGRect(x: 1000, y: 2000, width: 1, height: 1) - -rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) -print_(rect.union(smallRect), "union small") -print_(rect.union(bigRect), "union big") -print_(rect.union(distantRect), "union distant") -// CHECK-NEXT: union small 10.0 20.0 34.5 46.5 -// CHECK-NEXT: union big 1.0 2.0 101.0 102.0 -// CHECK-NEXT: union distant 11.25 22.25 989.75 1978.75 -rect = rect.union(smallRect) -rect = rect.union(bigRect) -rect = rect.union(distantRect) -print_(rect, "formUnion") -// CHECK-NEXT: formUnion 1.0 2.0 1000.0 1999.0 - -rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) -print_(rect.intersection(smallRect), "intersect small") -print_(rect.intersection(bigRect), "intersect big") -print_(rect.intersection(distantRect), "intersect distant") -// CHECK-NEXT: intersect small 11.25 22.25 3.75 2.75 -// CHECK-NEXT: intersect big 11.25 22.25 33.25 44.25 -// CHECK-NEXT: intersect distant inf inf 0.0 0.0 -assert(rect.intersects(smallRect)) -rect = rect.intersection(smallRect) -assert(!rect.isEmpty) -assert(rect.intersects(bigRect)) -rect = rect.intersection(bigRect) -assert(!rect.isEmpty) -assert(!rect.intersects(distantRect)) -rect = rect.intersection(distantRect) -assert(rect.isEmpty) - - -rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) -assert(rect.contains(CGPoint(x: 15, y: 25))) -assert(!rect.contains(CGPoint(x: -15, y: 25))) -assert(bigRect.contains(rect)) -assert(!rect.contains(bigRect)) - - -rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) -var (slice, remainder) = rect.divided(atDistance: 5, from:CGRectEdge.minXEdge) -print_(slice, "slice") -print_(remainder, "remainder") -// CHECK-NEXT: slice 11.25 22.25 5.0 44.25 -// CHECK-NEXT: remainder 16.25 22.25 28.25 44.25 diff --git a/validation-test/stdlib/CoreGraphics-execute.swift b/validation-test/stdlib/CoreGraphics-execute.swift index f4602c4f92f09..c2dd9c3208ee3 100644 --- a/validation-test/stdlib/CoreGraphics-execute.swift +++ b/validation-test/stdlib/CoreGraphics-execute.swift @@ -1,52 +1,480 @@ // RUN: %target-run-simple-swift // REQUIRES: executable_test - // REQUIRES: objc_interop // REQUIRES: OS=macosx + import CoreGraphics import StdlibUnittest +let CoreGraphicsTests = TestSuite("CoreGraphics") + //===----------------------------------------------------------------------===// // CGAffineTransform //===----------------------------------------------------------------------===// -// Equatable conformance via CGAffineTransformEqualToTransform -let bourne = CGAffineTransform(a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0) -expectEqual(bourne, CGAffineTransform.identity) +CoreGraphicsTests.test("CGAffineTransform/Equatable") { + checkEquatable([ + CGAffineTransform(a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0), + CGAffineTransform.identity, + CGAffineTransform(a: 1, b: 10, c: 10, d: 1, tx: 0, ty: 0), + CGAffineTransform(a: 1, b: 10, c: 10, d: 1, tx: 0, ty: 0), + ] as [CGAffineTransform], + oracle: { $0 / 2 == $1 / 2 }) +} //===----------------------------------------------------------------------===// // CGColor //===----------------------------------------------------------------------===// -// CGColor.components -let red = CGColor(red: 1, green: 0, blue: 0, alpha: 1) -let components = red.components! -expectEqual(components.count, 4) -expectEqual(components[0], 1) -expectEqual(components[1], 0) -expectEqual(components[2], 0) -expectEqual(components[3], 1) +CoreGraphicsTests.test("CGColor/Equatable") { + checkEquatable([ + CGColor(red: 1, green: 0, blue: 0, alpha: 1), + CGColor(red: 1, green: 0, blue: 0, alpha: 0), + CGColor(red: 0, green: 1, blue: 0, alpha: 1), + CGColor(red: 0, green: 1, blue: 0, alpha: 0), + CGColor(red: 0, green: 0, blue: 1, alpha: 1), + CGColor(red: 0, green: 0, blue: 1, alpha: 0), + ] as [CGColor], + oracle: { $0 == $1 }) +} + +CoreGraphicsTests.test("CGColor.components") { + let red = CGColor(red: 1, green: 0, blue: 0, alpha: 1) + let components = red.components! + expectEqual(components.count, 4) + expectEqual(components[0], 1) + expectEqual(components[1], 0) + expectEqual(components[2], 0) + expectEqual(components[3], 1) +} + +//===----------------------------------------------------------------------===// +// CGPoint +//===----------------------------------------------------------------------===// + +CoreGraphicsTests.test("CGPoint/Equatable") { + checkEquatable([ + CGPoint(x: 0, y: 0), + + CGPoint(x: -1, y: -1), + CGPoint(x: -1, y: 0), + CGPoint(x: 0, y: -1), + + CGPoint(x: 1, y: 1), + CGPoint(x: 1, y: 0), + CGPoint(x: 0, y: 1), + + CGPoint(x: 1.nextUp, y: 1.nextUp), + CGPoint(x: 1.nextUp, y: 0), + CGPoint(x: 0, y: 1.nextUp), + + CGPoint(x: CGFloat.greatestFiniteMagnitude, y: 0), + ] as [CGPoint], + oracle: { $0 == $1 }) +} + +CoreGraphicsTests.test("CGPoint.init(x:y:)") { + var fractional = CGPoint() + fractional.x = 1.25 + fractional.y = 2.25 + + var negativeFractional = CGPoint() + negativeFractional.x = -1.25 + negativeFractional.y = -2.25 + + var integral = CGPoint() + integral.x = 1.0 + integral.y = 2.0 + + var negativeIntegral = CGPoint() + negativeIntegral.x = -1.0 + negativeIntegral.y = -2.0 + + // Initialize from floating point literals. + expectEqual(fractional, CGPoint(x: 1.25, y: 2.25)) + expectEqual(negativeFractional, CGPoint(x: -1.25, y: -2.25)) + + // Initialize from integer literals. + expectEqual(integral, CGPoint(x: 1, y: 2)) + expectEqual(negativeIntegral, CGPoint(x: -1, y: -2)) + + expectEqual(fractional, CGPoint(x: 1.25 as CGFloat, y: 2.25 as CGFloat)) + expectEqual(fractional, CGPoint(x: 1.25 as Double, y: 2.25 as Double)) + expectEqual(integral, CGPoint(x: 1 as Int, y: 2 as Int)) +} + +CoreGraphicsTests.test("CGPoint.dictionaryRepresentation, CGPoint.init(dictionaryRepresentation:)") { + let point = CGPoint(x: 1, y: 2) + let dict = point.dictionaryRepresentation + let newPoint = CGPoint(dictionaryRepresentation: dict) + expectOptionalEqual(point, newPoint) +} + +CoreGraphicsTests.test("CGPoint.zero") { + expectEqual(0.0, CGPoint.zero.x) + expectEqual(0.0, CGPoint.zero.y) +} + +//===----------------------------------------------------------------------===// +// CGSize +//===----------------------------------------------------------------------===// + +CoreGraphicsTests.test("CGSize/Equatable") { + checkEquatable([ + CGSize(width: 0, height: 0), + + CGSize(width: -1, height: -1), + CGSize(width: -1, height: 0), + CGSize(width: 0, height: -1), + + CGSize(width: 1, height: 1), + CGSize(width: 1, height: 0), + CGSize(width: 0, height: 1), + + CGSize(width: 1.nextUp, height: 1.nextUp), + CGSize(width: 1.nextUp, height: 0), + CGSize(width: 0, height: 1.nextUp), + + CGSize(width: CGFloat.greatestFiniteMagnitude, height: 0), + ] as [CGSize], + oracle: { $0 == $1 }) +} + +CoreGraphicsTests.test("CGSize.init(width:height:)") { + var fractional = CGSize() + fractional.width = 1.25 + fractional.height = 2.25 + + var negativeFractional = CGSize() + negativeFractional.width = -1.25 + negativeFractional.height = -2.25 + + var integral = CGSize() + integral.width = 1.0 + integral.height = 2.0 + + var negativeIntegral = CGSize() + negativeIntegral.width = -1.0 + negativeIntegral.height = -2.0 + + // Initialize from floating point literals. + expectEqual(fractional, CGSize(width: 1.25, height: 2.25)) + expectEqual(negativeFractional, CGSize(width: -1.25, height: -2.25)) + + // Initialize from integer literals. + expectEqual(integral, CGSize(width: 1, height: 2)) + expectEqual(negativeIntegral, CGSize(width: -1, height: -2)) + + expectEqual(fractional, CGSize(width: 1.25 as CGFloat, height: 2.25 as CGFloat)) + expectEqual(fractional, CGSize(width: 1.25 as Double, height: 2.25 as Double)) + expectEqual(integral, CGSize(width: 1 as Int, height: 2 as Int)) +} + +CoreGraphicsTests.test("CGSize.dictionaryRepresentation, CGSize.init(dictionaryRepresentation:)") { + let size = CGSize(width: 3, height: 4) + let dict = size.dictionaryRepresentation + let newSize = CGSize(dictionaryRepresentation: dict) + expectOptionalEqual(size, newSize) +} + +CoreGraphicsTests.test("CGSize.zero") { + expectEqual(0.0, CGSize.zero.width) + expectEqual(0.0, CGSize.zero.height) +} + +//===----------------------------------------------------------------------===// +// CGRect +//===----------------------------------------------------------------------===// + +CoreGraphicsTests.test("CGRect/Equatable") { + checkEquatable([ + CGRect.null, + CGRect(x: 0, y: 0, width: 0, height: 0), + + CGRect(x: 1.25, y: 2.25, width: 3.25, height: 4.25), + CGRect(x: -1.25, y: -2.25, width: -3.25, height: -4.25), + + CGRect(x: 1, y: 2, width: 3, height: 4), + CGRect(x: -1, y: -2, width: -3, height: -4), + ] as [CGRect], + oracle: { $0 == $1 }) +} + +CoreGraphicsTests.test("CGRect.init(x:y:width:height:)") { + var fractional = CGRect() + fractional.origin = CGPoint(x: 1.25, y: 2.25) + fractional.size = CGSize(width: 3.25, height: 4.25) + + var negativeFractional = CGRect() + negativeFractional.origin = CGPoint(x: -1.25, y: -2.25) + negativeFractional.size = CGSize(width: -3.25, height: -4.25) + + var integral = CGRect() + integral.origin = CGPoint(x: 1.0, y: 2.0) + integral.size = CGSize(width: 3.0, height: 4.0) -// CGColor Equatable conformance via CGColorEqualToColor -let 赤 = CGColor(red: 1, green: 0, blue: 0, alpha: 1) -expectEqual(red, 赤) + var negativeIntegral = CGRect() + negativeIntegral.origin = CGPoint(x: -1.0, y: -2.0) + negativeIntegral.size = CGSize(width: -3.0, height: -4.0) + + // Initialize from floating point literals. + expectEqual(fractional, CGRect(x: 1.25, y: 2.25, width: 3.25, height: 4.25)) + expectEqual( + negativeFractional, + CGRect(x: -1.25, y: -2.25, width: -3.25, height: -4.25)) + + // Initialize from integer literals. + expectEqual(integral, CGRect(x: 1, y: 2, width: 3, height: 4)) + expectEqual(negativeIntegral, CGRect(x: -1, y: -2, width: -3, height: -4)) + + expectEqual( + fractional, + CGRect( + x: 1.25 as CGFloat, y: 2.25 as CGFloat, + width: 3.25 as CGFloat, height: 4.25 as CGFloat)) + expectEqual( + fractional, + CGRect( + x: 1.25 as Double, y: 2.25 as Double, + width: 3.25 as Double, height: 4.25 as Double)) + expectEqual( + integral, + CGRect( + x: 1 as Int, y: 2 as Int, + width: 3 as Int, height: 4 as Int)) +} + +CoreGraphicsTests.test("CGRect.init(origin:size:)") { + let point = CGPoint(x: 1.25, y: 2.25) + let size = CGSize(width: 3.25, height: 4.25) + expectEqual( + CGRect(x: 1.25, y: 2.25, width: 3.25, height: 4.25), + CGRect(origin: point, size: size)) +} + +CoreGraphicsTests.test("CGRect.dictionaryRepresentation, CGRect.init(dictionaryRepresentation:)") { + let point = CGPoint(x: 1, y: 2) + let size = CGSize(width: 3, height: 4) + let rect = CGRect(origin: point, size: size) + let dict = rect.dictionaryRepresentation + let newRect = CGRect(dictionaryRepresentation: dict) + expectOptionalEqual(rect, newRect) +} + +CoreGraphicsTests.test("CGRect.isNull") { + expectFalse(CGRect.infinite.isNull) + expectTrue(CGRect.null.isNull) + expectFalse(CGRect.zero.isNull) + expectFalse(CGRect(x: 0, y: 0, width: 10, height: 20).isNull) +} + +CoreGraphicsTests.test("CGRect.isEmpty") { + expectFalse(CGRect.infinite.isEmpty) + expectTrue(CGRect.null.isEmpty) + expectTrue(CGRect.zero.isEmpty) + expectFalse(CGRect(x: 0, y: 0, width: 10, height: 20).isEmpty) +} + +CoreGraphicsTests.test("CGRect.isInfinite") { + expectTrue(CGRect.infinite.isInfinite) + expectFalse(CGRect.null.isInfinite) + expectFalse(CGRect.zero.isInfinite) + expectFalse(CGRect(x: 0, y: 0, width: 10, height: 20).isInfinite) +} + +CoreGraphicsTests.test("CGRect.contains(CGPoint)") { + let rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) + expectTrue(rect.contains(CGPoint(x: 15, y: 25))) + expectFalse(rect.contains(CGPoint(x: -15, y: 25))) +} + +CoreGraphicsTests.test("CGRect.contains(CGRect)") { + let rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) + let bigRect = CGRect(x: 1, y: 2, width: 101, height: 102) + expectTrue(bigRect.contains(rect)) + expectFalse(rect.contains(bigRect)) +} + +CoreGraphicsTests.test("CGRect.divided()") { + let rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) + let (slice, remainder) = + rect.divided(atDistance: 5, from: CGRectEdge.minXEdge) + expectEqual(CGRect(x: 11.25, y: 22.25, width: 5.0, height: 44.25), slice) + expectEqual(CGRect(x: 16.25, y: 22.25, width: 28.25, height: 44.25), remainder) +} + +CoreGraphicsTests.test("CGRect.standardized") { + var unstandard = CGRect(x: 10, y: 20, width: -30, height: -50) + var standard = unstandard.standardized + + expectEqual(CGPoint(x: 10, y: 20), unstandard.origin) + expectEqual(CGPoint(x: -20, y: -30), standard.origin) + + expectEqual(CGSize(width: -30, height: -50), unstandard.size) + expectEqual(CGSize(width: 30, height: 50), standard.size) + + expectEqual(unstandard, standard) + expectEqual(standard, standard.standardized) + + expectEqual(30, unstandard.width) + expectEqual(30, standard.width) + + expectEqual(50, unstandard.height) + expectEqual(50, standard.height) + + expectEqual(-20, unstandard.minX) + expectEqual(-5, unstandard.midX) + expectEqual(10, unstandard.maxX) + + expectEqual(-20, standard.minX) + expectEqual(-5, standard.midX) + expectEqual(10, standard.maxX) + + expectEqual(-30, unstandard.minY) + expectEqual(-5, unstandard.midY) + expectEqual(20, unstandard.maxY) + + expectEqual(-30, standard.minY) + expectEqual(-5, standard.midY) + expectEqual(20, standard.maxY) +} + +CoreGraphicsTests.test("CGRect.insetBy(self:dx:dy:)") { + let rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) + expectEqual( + CGRect(x: 12.25, y: 20.25, width: 31.25, height: 48.25), + rect.insetBy(dx: 1, dy: -2)) +} + +CoreGraphicsTests.test("CGRect.offsetBy(self:dx:dy:)") { + let rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) + expectEqual( + CGRect(x: 14.25, y: 18.25, width: 33.25, height: 44.25), + rect.offsetBy(dx: 3, dy: -4)) +} + +CoreGraphicsTests.test("CGRect.integral") { + let rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) + expectEqual( + CGRect(x: 11, y: 22, width: 34, height: 45), + rect.integral) +} + +CoreGraphicsTests.test("CGRect.union(_:)") { + let smallRect = CGRect(x: 10, y: 25, width: 5, height: -5) + let bigRect = CGRect(x: 1, y: 2, width: 101, height: 102) + let distantRect = CGRect(x: 1000, y: 2000, width: 1, height: 1) + + let rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) + + expectEqual( + CGRect(x: 10.0, y: 20.0, width: 34.5, height: 46.5), + rect.union(smallRect)) + expectEqual( + CGRect(x: 1.0, y: 2.0, width: 101.0, height: 102.0), + rect.union(bigRect)) + expectEqual( + CGRect(x: 11.25, y: 22.25, width: 989.75, height: 1978.75), + rect.union(distantRect)) + + expectEqual( + CGRect(x: 1.0, y: 2.0, width: 1000.0, height: 1999.0), + rect.union(smallRect).union(bigRect).union(distantRect)) +} + +CoreGraphicsTests.test("CGRect.intersection(_:)") { + let smallRect = CGRect(x: 10, y: 25, width: 5, height: -5) + let bigRect = CGRect(x: 1, y: 2, width: 101, height: 102) + let distantRect = CGRect(x: 1000, y: 2000, width: 1, height: 1) + + var rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25) + + expectTrue(rect.intersects(smallRect)) + expectEqual( + CGRect(x: 11.25, y: 22.25, width: 3.75, height: 2.75), + rect.intersection(smallRect)) + + expectTrue(rect.intersects(bigRect)) + expectEqual( + CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25), + rect.intersection(bigRect)) + + expectFalse(rect.intersects(distantRect)) + expectEqual(CGRect.null, rect.intersection(distantRect)) + + expectFalse( + rect + .intersection(smallRect) + .intersection(bigRect) + .isEmpty) + + expectTrue( + rect + .intersection(smallRect) + .intersection(bigRect) + .intersection(distantRect) + .isEmpty) +} //===----------------------------------------------------------------------===// -// CGGeometry +// CGVector //===----------------------------------------------------------------------===// -// other tests elsewhere for basics, only the new stuff here -let point = CGPoint(x: 1, y: 2) -let pDict = point.dictionaryRepresentation -let newPoint = CGPoint(dictionaryRepresentation: pDict) -expectOptionalEqual(point, newPoint) +CoreGraphicsTests.test("CGVector/Equatable") { + checkEquatable([ + CGVector(dx: 0, dy: 0), + + CGVector(dx: -1, dy: -1), + CGVector(dx: -1, dy: 0), + CGVector(dx: 0, dy: -1), + + CGVector(dx: 1, dy: 1), + CGVector(dx: 1, dy: 0), + CGVector(dx: 0, dy: 1), + + CGVector(dx: 1.nextUp, dy: 1.nextUp), + CGVector(dx: 1.nextUp, dy: 0), + CGVector(dx: 0, dy: 1.nextUp), + + CGVector(dx: CGFloat.greatestFiniteMagnitude, dy: 0), + ] as [CGVector], + oracle: { $0 == $1 }) +} + +CoreGraphicsTests.test("CGVector.init(dx:dy:)") { + var fractional = CGVector() + fractional.dx = 1.25 + fractional.dy = 2.25 + + var negativeFractional = CGVector() + negativeFractional.dx = -1.25 + negativeFractional.dy = -2.25 + + var integral = CGVector() + integral.dx = 1.0 + integral.dy = 2.0 + + var negativeIntegral = CGVector() + negativeIntegral.dx = -1.0 + negativeIntegral.dy = -2.0 + + // Initialize from floating point literals. + expectEqual(fractional, CGVector(dx: 1.25, dy: 2.25)) + expectEqual(negativeFractional, CGVector(dx: -1.25, dy: -2.25)) + + // Initialize from integer literals. + expectEqual(integral, CGVector(dx: 1, dy: 2)) + expectEqual(negativeIntegral, CGVector(dx: -1, dy: -2)) + + expectEqual(fractional, CGVector(dx: 1.25 as CGFloat, dy: 2.25 as CGFloat)) + expectEqual(fractional, CGVector(dx: 1.25 as Double, dy: 2.25 as Double)) + expectEqual(integral, CGVector(dx: 1 as Int, dy: 2 as Int)) +} + +CoreGraphicsTests.test("CGVector.zero") { + expectEqual(0.0, CGVector.zero.dx) + expectEqual(0.0, CGVector.zero.dy) +} -let size = CGSize(width: 3, height: 4) -let sDict = size.dictionaryRepresentation -let newSize = CGSize(dictionaryRepresentation: sDict) -expectOptionalEqual(size, newSize) +runAllTests() -let rect = CGRect(origin: point, size: size) -let rDict = rect.dictionaryRepresentation -let newRect = CGRect(dictionaryRepresentation: rDict) -expectOptionalEqual(rect, newRect)