From c1eb3a69e2610b262204edbab8916af1d4ca0591 Mon Sep 17 00:00:00 2001 From: Alexis Drai Date: Tue, 24 Jan 2023 14:32:20 +0100 Subject: [PATCH] :white_check_mark: Board --- .../xcschemes/connect4_lib.xcscheme | 88 +++++++++++++++++++ .../Sources/connect4_lib/Board.swift | 4 +- .../Tests/connect4_libTests/BoardTest.swift | 53 +++++++++-- 3 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 Connect4/connect4_lib/.swiftpm/xcode/xcshareddata/xcschemes/connect4_lib.xcscheme diff --git a/Connect4/connect4_lib/.swiftpm/xcode/xcshareddata/xcschemes/connect4_lib.xcscheme b/Connect4/connect4_lib/.swiftpm/xcode/xcshareddata/xcschemes/connect4_lib.xcscheme new file mode 100644 index 0000000..ffc9c61 --- /dev/null +++ b/Connect4/connect4_lib/.swiftpm/xcode/xcshareddata/xcschemes/connect4_lib.xcscheme @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Connect4/connect4_lib/Sources/connect4_lib/Board.swift b/Connect4/connect4_lib/Sources/connect4_lib/Board.swift index ba2b0d6..a909ea6 100644 --- a/Connect4/connect4_lib/Sources/connect4_lib/Board.swift +++ b/Connect4/connect4_lib/Sources/connect4_lib/Board.swift @@ -17,7 +17,9 @@ public struct Board : CustomStringConvertible { } public init?(withGrid grid: [[Int?]]) { - guard(grid.allSatisfy{ $0.count == grid[0].count }) else { return nil } + guard(grid.count >= 3 + && grid[0].count >= 3 + && grid.allSatisfy{ $0.count == grid[0].count }) else { return nil } self.nbRows = grid.count self.nbCols = grid[0].count self._nbFree = nbRows * nbCols diff --git a/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift b/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift index 641ec0c..af2e962 100644 --- a/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift +++ b/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift @@ -1,8 +1,47 @@ -// -// File.swift -// -// -// Created by etudiant on 2023-01-23. -// - import Foundation +import XCTest +import connect4_lib + +final class BoardTest: XCTestCase { + + func testInit() throws { + func expect(initBoardWithNbRows nbRows: Int, + andNbCols nbCols: Int, + shouldNotBeNil: Bool) { + let board = Board(withRows: nbRows, andWithCols: nbCols) + if !shouldNotBeNil { + XCTAssertNil(board) + return + } + XCTAssertNotNil(board) + XCTAssertEqual(nbCols, board?.nbCols) + XCTAssertEqual(nbRows, board?.nbRows) + } + + expect(initBoardWithNbRows: 6, andNbCols: 7, shouldNotBeNil: true) + expect(initBoardWithNbRows: -1, andNbCols: 7, shouldNotBeNil: false) + expect(initBoardWithNbRows: 6, andNbCols: -9, shouldNotBeNil: false) + expect(initBoardWithNbRows: 0, andNbCols: 7, shouldNotBeNil: false) + expect(initBoardWithNbRows: 6, andNbCols: 0, shouldNotBeNil: false) + } + + func testInitLoad() throws { + func expect(withBoard orig: [[Int?]], shouldNotBeNil: Bool) { + let board = Board(withGrid: orig) + if !shouldNotBeNil { + XCTAssertNil(board) + return + } + XCTAssertNotNil(board) + XCTAssertEqual(orig[0].count, board?.nbCols) + XCTAssertEqual(orig.count, board?.nbRows) + } + + expect(withBoard: [[0, 1, 2], [0, 0, 0], [0, 0, 0]], shouldNotBeNil: true) + expect(withBoard: [], shouldNotBeNil: false) + expect(withBoard: [[], []], shouldNotBeNil: false) + expect(withBoard: [[0, 1], [0, 0]], shouldNotBeNil: false) + expect(withBoard: [[0, 1, 2], [0, 0, 0, 0]], shouldNotBeNil: false) + } + +}