|
|
|
@ -4,7 +4,7 @@ import XCTest
|
|
|
|
|
final class EmptyBoardTests: XCTestCase {
|
|
|
|
|
private var board: Board!
|
|
|
|
|
|
|
|
|
|
override func setUp() {
|
|
|
|
|
override func setUpWithError() throws {
|
|
|
|
|
super.setUp()
|
|
|
|
|
|
|
|
|
|
guard let board = Board(columns: 5, rows: 6) else {
|
|
|
|
@ -26,41 +26,128 @@ final class EmptyBoardTests: XCTestCase {
|
|
|
|
|
func testEmptyByDefault() throws {
|
|
|
|
|
XCTAssertNil(board[1, 1])
|
|
|
|
|
let result = board.countPieces()
|
|
|
|
|
XCTAssertEqual(result.a, 0)
|
|
|
|
|
XCTAssertEqual(result.b, 0)
|
|
|
|
|
XCTAssertEqual(result, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testSetGet() throws {
|
|
|
|
|
board[1, 1] = Piece.PlayerA;
|
|
|
|
|
func testCustomSubscript() throws {
|
|
|
|
|
for r in 0..<board.rows {
|
|
|
|
|
for c in 0..<board.columns {
|
|
|
|
|
for p: Player in [.A, .B] {
|
|
|
|
|
try self.setUpWithError()
|
|
|
|
|
|
|
|
|
|
XCTAssertEqual(board[1, 1], .PlayerA)
|
|
|
|
|
try self._testCustomSubscript(at: Coords(c, r), player: p)
|
|
|
|
|
|
|
|
|
|
try self.tearDownWithError()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func _testCustomSubscript(at: Coords, player: Player) throws {
|
|
|
|
|
board[at] = Piece(owner: player)
|
|
|
|
|
|
|
|
|
|
XCTAssertEqual(board[at]?.owner, player)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testCounts() throws {
|
|
|
|
|
board[1, 2] = .PlayerB
|
|
|
|
|
board[1, 2] = Piece(owner: .B)
|
|
|
|
|
board[0, 2] = Piece(owner: .A)
|
|
|
|
|
board[1, 1] = Piece(owner: .A)
|
|
|
|
|
|
|
|
|
|
let counts = board.countPieces()
|
|
|
|
|
XCTAssertEqual(counts.a, 0)
|
|
|
|
|
XCTAssertEqual(counts.b, 1)
|
|
|
|
|
XCTAssertEqual(board.countPieces { piece in piece.owner == .A }, 2)
|
|
|
|
|
XCTAssertEqual(board.countPieces { piece in piece.owner == .B }, 1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testInsertTopNoPush() {
|
|
|
|
|
XCTAssert(board.insert(piece: .PlayerA, side: .Top, offset: 2))
|
|
|
|
|
XCTAssertEqual(board[2, 0], .PlayerA)
|
|
|
|
|
func testInsertsSides() throws {
|
|
|
|
|
for (side, offset, expected): (Direction, Int, (Int, Int)) in [
|
|
|
|
|
(.Top, 0, (0, 0)),
|
|
|
|
|
(.Left, 0, (0, 0)),
|
|
|
|
|
(.Bottom, 0, (0, 5)),
|
|
|
|
|
(.Right, 0, (4, 0)),
|
|
|
|
|
/* ... */
|
|
|
|
|
(.Top, 4, (4, 0)),
|
|
|
|
|
(.Left, 5, (0, 5)),
|
|
|
|
|
(.Bottom, 4, (4, 5)),
|
|
|
|
|
(.Right, 5, (4, 5)),
|
|
|
|
|
] {
|
|
|
|
|
try self.setUpWithError()
|
|
|
|
|
|
|
|
|
|
try self._testInsertSide(side: side, offset: offset, expectedCoords: Coords(pair: expected))
|
|
|
|
|
|
|
|
|
|
try self.tearDownWithError()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func _testInsertSide(side: Direction, offset: Int, expectedCoords: Coords) throws {
|
|
|
|
|
let result = board.getInsertionCoordinates(direction: side, offset: offset)
|
|
|
|
|
|
|
|
|
|
func testInsertBottomNoPush() {
|
|
|
|
|
XCTAssert(board.insert(piece: .PlayerB, side: .Bottom, offset: 2))
|
|
|
|
|
XCTAssertEqual(board[2, board.rows - 1], .PlayerB)
|
|
|
|
|
// Note: Not sure I want Coordinates to be a simple tuple or a struct, as both have advantages that the other dont:
|
|
|
|
|
// Tuples can be initialized like so (col, row) which I find quite handy
|
|
|
|
|
// Structs supports == operators
|
|
|
|
|
// ...
|
|
|
|
|
XCTAssertEqual(result.col, expectedCoords.col)
|
|
|
|
|
XCTAssertEqual(result.row, expectedCoords.row)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testInsertLeftNoPush() {
|
|
|
|
|
XCTAssert(board.insert(piece: .PlayerA, side: .Left, offset: 2))
|
|
|
|
|
XCTAssertEqual(board[0, 2], .PlayerA)
|
|
|
|
|
func testInsertsFallNoPiece() throws {
|
|
|
|
|
for (from, direction, expected): ((Int, Int), Direction, (Int, Int)) in [
|
|
|
|
|
((0, 0), .Bottom, (0, 5)),
|
|
|
|
|
((0, 0), .Left, (0, 0)),
|
|
|
|
|
((0, 0), .Right, (4, 0)),
|
|
|
|
|
((0, 0), .Top, (0, 0)),
|
|
|
|
|
/* ... */
|
|
|
|
|
((4, 5), .Bottom, (4, 5)),
|
|
|
|
|
((4, 5), .Left, (0, 5)),
|
|
|
|
|
((4, 5), .Right, (4, 5)),
|
|
|
|
|
((4, 5), .Top, (4, 0)),
|
|
|
|
|
] {
|
|
|
|
|
try self.setUpWithError()
|
|
|
|
|
|
|
|
|
|
try self._testInsertFall(from: Coords(pair: from), dir: direction, expectedCoords: .Border(at: Coords(pair: expected)))
|
|
|
|
|
|
|
|
|
|
try self.tearDownWithError()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testInsertsFallOnPiece() throws {
|
|
|
|
|
for (from, direction, (expected, touched)): ((Int, Int), Direction, ((Int, Int), (Int, Int))) in [
|
|
|
|
|
((0, 0), .Bottom, ((0, 1), (0, 2))),
|
|
|
|
|
((0, 0), .Right, ((1, 0), (2, 0))),
|
|
|
|
|
((4, 5), .Left, ((3, 5), (2, 5))),
|
|
|
|
|
((4, 5), .Top, ((4, 3), (4, 2))),
|
|
|
|
|
] {
|
|
|
|
|
try self.setUpWithError()
|
|
|
|
|
|
|
|
|
|
// Place some pieces
|
|
|
|
|
for pos in [
|
|
|
|
|
(2, 0),
|
|
|
|
|
(2, 1),
|
|
|
|
|
(0, 2), (1, 2), (2, 2), (3, 2), (4, 2),
|
|
|
|
|
(2, 3),
|
|
|
|
|
(2, 4),
|
|
|
|
|
(2, 5),
|
|
|
|
|
] {
|
|
|
|
|
// ensure it works with any player
|
|
|
|
|
board[Coords(pair: pos)] = if (pos.0 + pos.1) & 1 == 1 {
|
|
|
|
|
Piece(owner: .A)
|
|
|
|
|
} else {
|
|
|
|
|
Piece(owner: .B)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testInsertRightNoPush() {
|
|
|
|
|
XCTAssert(board.insert(piece: .PlayerB, side: .Right, offset: 2))
|
|
|
|
|
XCTAssertEqual(board[board.columns - 1, 2], .PlayerB)
|
|
|
|
|
try self._testInsertFall(
|
|
|
|
|
from: Coords(pair: from),
|
|
|
|
|
dir: direction,
|
|
|
|
|
expectedCoords: .Piece(at: Coords(pair: expected), touched: Coords(pair: touched))
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
try self.tearDownWithError()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func _testInsertFall(from: Coords, dir: Direction, expectedCoords: Board.FallResult) throws {
|
|
|
|
|
let result = board.fallCoordinates(initialCoords: from, direction: dir)
|
|
|
|
|
|
|
|
|
|
XCTAssertEqual(result, expectedCoords)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|