|
| 1 | +import Foundation |
| 2 | +import Testing |
| 3 | + |
| 4 | +@testable import GameOfLife |
| 5 | + |
| 6 | +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false |
| 7 | + |
| 8 | +@Suite |
| 9 | +struct GameOfLifeTests { |
| 10 | + |
| 11 | + @Test("empty matrix") |
| 12 | + func testEmptyMatrix() { |
| 13 | + let initial: [[Int]] = [] |
| 14 | + let expected: [[Int]] = [] |
| 15 | + #expect(tick(initial) == expected) |
| 16 | + } |
| 17 | + |
| 18 | + @Test("live cells with zero live neighbors die", .enabled(if: RUNALL)) |
| 19 | + func testLiveCellsWithZeroLiveNeighborsDie() { |
| 20 | + let initial: [[Int]] = [[0, 0, 0], [0, 1, 0], [0, 0, 0]] |
| 21 | + let expected: [[Int]] = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] |
| 22 | + #expect(tick(initial) == expected) |
| 23 | + } |
| 24 | + |
| 25 | + @Test("live cells with only one live neighbor die", .enabled(if: RUNALL)) |
| 26 | + func testLiveCellsWithOnlyOneLiveNeighborDie() { |
| 27 | + let initial: [[Int]] = [[0, 0, 0], [0, 1, 0], [0, 1, 0]] |
| 28 | + let expected: [[Int]] = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] |
| 29 | + #expect(tick(initial) == expected) |
| 30 | + } |
| 31 | + |
| 32 | + @Test("live cells with two live neighbors stay alive", .enabled(if: RUNALL)) |
| 33 | + func testLiveCellsWithTwoLiveNeighborsStayAlive() { |
| 34 | + let initial: [[Int]] = [[1, 0, 1], [1, 0, 1], [1, 0, 1]] |
| 35 | + let expected: [[Int]] = [[0, 0, 0], [1, 0, 1], [0, 0, 0]] |
| 36 | + #expect(tick(initial) == expected) |
| 37 | + } |
| 38 | + |
| 39 | + @Test("live cells with three live neighbors stay alive", .enabled(if: RUNALL)) |
| 40 | + func testLiveCellsWithThreeLiveNeighborsStayAlive() { |
| 41 | + let initial: [[Int]] = [[0, 1, 0], [1, 0, 0], [1, 1, 0]] |
| 42 | + let expected: [[Int]] = [[0, 0, 0], [1, 0, 0], [1, 1, 0]] |
| 43 | + #expect(tick(initial) == expected) |
| 44 | + } |
| 45 | + |
| 46 | + @Test("dead cells with three live neighbors become alive", .enabled(if: RUNALL)) |
| 47 | + func testDeadCellsWithThreeLiveNeighborsBecomeAlive() { |
| 48 | + let initial: [[Int]] = [[1, 1, 0], [0, 0, 0], [1, 0, 0]] |
| 49 | + let expected: [[Int]] = [[0, 0, 0], [1, 1, 0], [0, 0, 0]] |
| 50 | + #expect(tick(initial) == expected) |
| 51 | + } |
| 52 | + |
| 53 | + @Test("live cells with four or more neighbors die", .enabled(if: RUNALL)) |
| 54 | + func testLiveCellsWithFourOrMoreNeighborsDie() { |
| 55 | + let initial: [[Int]] = [[1, 1, 1], [1, 1, 1], [1, 1, 1]] |
| 56 | + let expected: [[Int]] = [[1, 0, 1], [0, 0, 0], [1, 0, 1]] |
| 57 | + #expect(tick(initial) == expected) |
| 58 | + } |
| 59 | + |
| 60 | + @Test("bigger matrix", .enabled(if: RUNALL)) |
| 61 | + func testBiggerMatrix() { |
| 62 | + let initial: [[Int]] = [ |
| 63 | + [1, 1, 0, 1, 1, 0, 0, 0], [1, 0, 1, 1, 0, 0, 0, 0], [1, 1, 1, 0, 0, 1, 1, 1], |
| 64 | + [0, 0, 0, 0, 0, 1, 1, 0], [1, 0, 0, 0, 1, 1, 0, 0], [1, 1, 0, 0, 0, 1, 1, 1], |
| 65 | + [0, 0, 1, 0, 1, 0, 0, 1], [1, 0, 0, 0, 0, 0, 1, 1], |
| 66 | + ] |
| 67 | + let expected: [[Int]] = [ |
| 68 | + [1, 1, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 0], [1, 0, 1, 1, 1, 1, 0, 1], |
| 69 | + [1, 0, 0, 0, 0, 0, 0, 1], [1, 1, 0, 0, 1, 0, 0, 1], [1, 1, 0, 1, 0, 0, 0, 1], |
| 70 | + [1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1], |
| 71 | + ] |
| 72 | + #expect(tick(initial) == expected) |
| 73 | + } |
| 74 | +} |
0 commit comments