Make Point and Size honest to goodness structs

This commit is contained in:
Eryn Wells 2018-11-10 13:50:28 -05:00
parent 5607f01ac1
commit 6b6e317a9c
2 changed files with 61 additions and 57 deletions

View file

@ -14,52 +14,50 @@ public typealias Point = DiamondSquareAlgorithm.Box.Point
public typealias Size = DiamondSquareAlgorithm.Box.Size
class DiamondSquareBoxTests: XCTestCase {
fileprivate let box = Box(origin: Point(x: 3, y: 4), size: Size(w: 5, h: 5))
func testMidpoint() {
let box = Box(origin: (x: 3, y: 4), size: (w: 5, h: 5))
let midpoint = box.midpoint
XCTAssertEqual(midpoint.x, 6)
XCTAssertEqual(midpoint.y, 7)
}
func testSubdivision() {
let box = Box(origin: (x: 3, y: 4), size: (w: 5, h: 5))
let subdivs = box.subdivisions
XCTAssertEqual(subdivs.count, 4)
XCTAssertEqual(subdivs[0], Box(origin: (x: 3, y: 4), size: (w: 3, h: 3)))
XCTAssertEqual(subdivs[1], Box(origin: (x: 6, y: 4), size: (w: 3, h: 3)))
XCTAssertEqual(subdivs[2], Box(origin: (x: 3, y: 7), size: (w: 3, h: 3)))
XCTAssertEqual(subdivs[3], Box(origin: (x: 6, y: 7), size: (w: 3, h: 3)))
XCTAssertEqual(subdivs[0], Box(origin: Point(x: 3, y: 4), size: Size(w: 3, h: 3)))
XCTAssertEqual(subdivs[1], Box(origin: Point(x: 6, y: 4), size: Size(w: 3, h: 3)))
XCTAssertEqual(subdivs[2], Box(origin: Point(x: 3, y: 7), size: Size(w: 3, h: 3)))
XCTAssertEqual(subdivs[3], Box(origin: Point(x: 6, y: 7), size: Size(w: 3, h: 3)))
}
func testBFS() {
let box = Box(origin: (x: 3, y: 4), size: (w: 5, h: 5))
var expectedBoxes: [Box] = [
box,
Box(origin: (x: 3, y: 4), size: (w: 3, h: 3)),
Box(origin: (x: 6, y: 4), size: (w: 3, h: 3)),
Box(origin: (x: 3, y: 7), size: (w: 3, h: 3)),
Box(origin: (x: 6, y: 7), size: (w: 3, h: 3)),
Box(origin: Point(x: 3, y: 4), size: Size(w: 3, h: 3)),
Box(origin: Point(x: 6, y: 4), size: Size(w: 3, h: 3)),
Box(origin: Point(x: 3, y: 7), size: Size(w: 3, h: 3)),
Box(origin: Point(x: 6, y: 7), size: Size(w: 3, h: 3)),
Box(origin: (x: 3, y: 4), size: (w: 2, h: 2)),
Box(origin: (x: 5, y: 4), size: (w: 2, h: 2)),
Box(origin: (x: 3, y: 6), size: (w: 2, h: 2)),
Box(origin: (x: 5, y: 6), size: (w: 2, h: 2)),
Box(origin: Point(x: 3, y: 4), size: Size(w: 2, h: 2)),
Box(origin: Point(x: 5, y: 4), size: Size(w: 2, h: 2)),
Box(origin: Point(x: 3, y: 6), size: Size(w: 2, h: 2)),
Box(origin: Point(x: 5, y: 6), size: Size(w: 2, h: 2)),
Box(origin: (x: 6, y: 4), size: (w: 2, h: 2)),
Box(origin: (x: 8, y: 4), size: (w: 2, h: 2)),
Box(origin: (x: 6, y: 6), size: (w: 2, h: 2)),
Box(origin: (x: 8, y: 6), size: (w: 2, h: 2)),
Box(origin: Point(x: 6, y: 4), size: Size(w: 2, h: 2)),
Box(origin: Point(x: 8, y: 4), size: Size(w: 2, h: 2)),
Box(origin: Point(x: 6, y: 6), size: Size(w: 2, h: 2)),
Box(origin: Point(x: 8, y: 6), size: Size(w: 2, h: 2)),
Box(origin: (x: 3, y: 7), size: (w: 2, h: 2)),
Box(origin: (x: 5, y: 7), size: (w: 2, h: 2)),
Box(origin: (x: 3, y: 9), size: (w: 2, h: 2)),
Box(origin: (x: 5, y: 9), size: (w: 2, h: 2)),
Box(origin: Point(x: 3, y: 7), size: Size(w: 2, h: 2)),
Box(origin: Point(x: 5, y: 7), size: Size(w: 2, h: 2)),
Box(origin: Point(x: 3, y: 9), size: Size(w: 2, h: 2)),
Box(origin: Point(x: 5, y: 9), size: Size(w: 2, h: 2)),
Box(origin: (x: 6, y: 7), size: (w: 2, h: 2)),
Box(origin: (x: 8, y: 7), size: (w: 2, h: 2)),
Box(origin: (x: 6, y: 9), size: (w: 2, h: 2)),
Box(origin: (x: 8, y: 9), size: (w: 2, h: 2)),
Box(origin: Point(x: 6, y: 7), size: Size(w: 2, h: 2)),
Box(origin: Point(x: 8, y: 7), size: Size(w: 2, h: 2)),
Box(origin: Point(x: 6, y: 9), size: Size(w: 2, h: 2)),
Box(origin: Point(x: 8, y: 9), size: Size(w: 2, h: 2)),
].reversed()
box.breadthFirstSearch { (box: Box) -> (Void) in
@ -73,28 +71,24 @@ class DiamondSquareBoxTests: XCTestCase {
// MARK: Sides
func testNorth() {
let box = Box(origin: (x: 3, y: 4), size: (w: 5, h: 5))
let pt = box.north
XCTAssertEqual(pt.x, 6)
XCTAssertEqual(pt.y, 4)
}
func testWest() {
let box = Box(origin: (x: 3, y: 4), size: (w: 5, h: 5))
let pt = box.west
XCTAssertEqual(pt.x, 3)
XCTAssertEqual(pt.y, 7)
}
func testSouth() {
let box = Box(origin: (x: 3, y: 4), size: (w: 5, h: 5))
let pt = box.south
XCTAssertEqual(pt.x, 6)
XCTAssertEqual(pt.y, 9)
}
func testEast() {
let box = Box(origin: (x: 3, y: 4), size: (w: 5, h: 5))
let pt = box.east
XCTAssertEqual(pt.x, 8)
XCTAssertEqual(pt.y, 7)
@ -103,28 +97,24 @@ class DiamondSquareBoxTests: XCTestCase {
// MARK: Corners
func testNorthwest() {
let box = Box(origin: (x: 3, y: 4), size: (w: 5, h: 5))
let pt = box.northwest
XCTAssertEqual(pt.x, 3)
XCTAssertEqual(pt.y, 4)
}
func testNortheast() {
let box = Box(origin: (x: 3, y: 4), size: (w: 5, h: 5))
let pt = box.northeast
XCTAssertEqual(pt.x, 8)
XCTAssertEqual(pt.y, 4)
}
func testSouthwest() {
let box = Box(origin: (x: 3, y: 4), size: (w: 5, h: 5))
let pt = box.southwest
XCTAssertEqual(pt.x, 3)
XCTAssertEqual(pt.y, 9)
}
func testSoutheast() {
let box = Box(origin: (x: 3, y: 4), size: (w: 5, h: 5))
let pt = box.southeast
XCTAssertEqual(pt.x, 8)
XCTAssertEqual(pt.y, 9)