Skip to content

Instantly share code, notes, and snippets.

@jbrains
Forked from patrickwilsonwelsh/RulesForNextGenerationTest
Created January 20, 2011 21:13
Show Gist options
  • Save jbrains/788684 to your computer and use it in GitHub Desktop.
Save jbrains/788684 to your computer and use it in GitHub Desktop.

Revisions

  1. jbrains revised this gist Jan 20, 2011. 6 changed files with 59 additions and 25 deletions.
    30 changes: 30 additions & 0 deletions Cell.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    package rules;

    public class Cell {
    protected int numberOfLiveNeighbors;
    private CellState state;

    public Cell() {
    this.state = CellState.DEAD;
    }

    public Cell withLiveNeighbors(int numberOfLiveNeighbors) {
    this.numberOfLiveNeighbors = numberOfLiveNeighbors;
    return this;
    }

    public Cell thatIsAlive() {
    this.state = CellState.ALIVE;
    return this;
    }

    public boolean isAlive() {
    return (state == CellState.ALIVE);
    }

    @Override
    public String toString() {
    return "I'm " + state + " and I have " + numberOfLiveNeighbors
    + " live neighbors.";
    }
    }
    File renamed without changes.
    29 changes: 29 additions & 0 deletions EvolveCellOneGeneration.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    package rules

    import spock.lang.Specification
    import spock.lang.Unroll

    class EvolveCellOneGeneration extends Specification {
    @Unroll("a dead cell with #liveNeighbors live neighbors dies")
    def "rules for cells to live or die"() {
    given:
    def inNextGeneration = new RulesForNextGeneration()
    def cell = initiallyAlive ? new Cell().thatIsAlive().withLiveNeighbors(liveNeighbors) : new Cell().withLiveNeighbors(liveNeighbors)

    expect:
    inNextGeneration.itTurnsOutThat(cell).comesAlive() == endsUpAlive

    where:
    liveNeighbors | initiallyAlive | endsUpAlive
    0 | false | false
    1 | false | false
    2 | false | false
    3 | false | true
    4 | false | false
    0 | true | false
    1 | true | false
    2 | true | true
    3 | true | true
    4 | true | false
    }
    }
    File renamed without changes.
    25 changes: 0 additions & 25 deletions gistfile3.java
    Original file line number Diff line number Diff line change
    @@ -1,25 +0,0 @@
    package rules;

    public class Cell {
    protected int numberOfLiveNeighbors;
    private CellState state;

    public Cell() {
    this.state = CellState.DEAD;
    }

    public Cell withLiveNeighbors(int numberOfLiveNeighbors) {
    this.numberOfLiveNeighbors = numberOfLiveNeighbors;
    return this;
    }

    public Cell thatIsAlive() {
    this.state = CellState.ALIVE;
    return this;
    }

    public boolean isAlive() {
    return (state == CellState.ALIVE);
    }

    }
  2. @patrickwilsonwelsh patrickwilsonwelsh created this gist Jan 20, 2011.
    88 changes: 88 additions & 0 deletions RulesForNextGenerationTest
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    package rules;

    import static org.junit.Assert.*;

    import org.junit.Before;
    import org.junit.Test;

    public class RulesForNextGenerationTest {
    private RulesForNextGeneration inNextGeneration;

    @Before
    public void setup() {
    inNextGeneration = new RulesForNextGeneration();
    }

    @Test
    public void deadCellWith_0_LiveNeighbors_StaysDead() throws Exception {
    assertIsDeadNextTime(new Cell().withLiveNeighbors(0));
    }

    @Test
    public void deadCellWith_1_LiveNeighbors_StaysDead() throws Exception {
    assertIsDeadNextTime(new Cell().withLiveNeighbors(1));
    }

    @Test
    public void deadCellWith_2_LiveNeighbors_StaysDead() throws Exception {
    assertIsDeadNextTime(new Cell().withLiveNeighbors(2));
    }

    @Test
    public void deadCellWith_3_LiveNeighbors_ComesAlive() throws Exception {
    assertIsAliveNextTime(new Cell().withLiveNeighbors(3));
    }

    @Test
    public void deadCellWith_4_LiveNeighbors_staysdead() throws Exception {
    assertIsDeadNextTime(new Cell().withLiveNeighbors(4));
    }

    @Test
    public void liveCellWith_0_LiveNeighbors_Dies() throws Exception {
    assertIsDeadNextTime(new Cell().thatIsAlive().withLiveNeighbors(0));
    }

    @Test
    public void liveCellWith_1_LiveNeighbors_Dies() throws Exception {
    assertIsDeadNextTime(new Cell().thatIsAlive().withLiveNeighbors(1));
    }

    @Test
    public void liveCellWith_2_LiveNeighbors_StaysAlive() throws Exception {
    assertIsAliveNextTime(new Cell().thatIsAlive().withLiveNeighbors(2));
    }

    @Test
    public void liveCellWith_3_LiveNeighbors_StaysAlive() throws Exception {
    assertIsAliveNextTime(new Cell().thatIsAlive().withLiveNeighbors(3));
    }

    @Test
    public void liveCellWith_4_LiveNeighbors_Dies() throws Exception {
    assertIsDeadNextTime(new Cell().thatIsAlive().withLiveNeighbors(4));
    }

    private void assertIsAliveNextTime(Cell cell) {
    assertTrue(inNextGeneration.itTurnsOutThat(cell).comesAlive());
    }

    private void assertIsDeadNextTime(Cell cell) {
    assertFalse(inNextGeneration.itTurnsOutThat(cell).comesAlive());
    }

    // Test list

    // TODO a dead cell with 0 live neighbors stays dead.
    // TODO a dead cell with 1 live neighbors stays dead.
    // TODO a dead cell with 2 live neighbors stays dead.
    // TODO a dead cell with 3 live neighbors comes alive.
    // TODO a dead cell with 4 live neighbors stays dead.

    // TODO a live cell with 0 live neighbors dies.
    // TODO a live cell with 1 live neighbor dies.
    // TODO a live cell with 2 live neighbors stays alive.
    // TODO a live cell with 3 live neighbors stays alive.
    // TODO a live cell with 4 live neighbors dies.

    }
    15 changes: 15 additions & 0 deletions gistfile2.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    package rules;

    public class RulesForNextGeneration {
    private Cell cell;

    public RulesForNextGeneration itTurnsOutThat(Cell cell) {
    this.cell = cell;
    return this;
    }

    public boolean comesAlive() {
    if (cell.isAlive() && cell.numberOfLiveNeighbors == 2) return true;
    return (cell.numberOfLiveNeighbors == 3);
    }
    }
    25 changes: 25 additions & 0 deletions gistfile3.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    package rules;

    public class Cell {
    protected int numberOfLiveNeighbors;
    private CellState state;

    public Cell() {
    this.state = CellState.DEAD;
    }

    public Cell withLiveNeighbors(int numberOfLiveNeighbors) {
    this.numberOfLiveNeighbors = numberOfLiveNeighbors;
    return this;
    }

    public Cell thatIsAlive() {
    this.state = CellState.ALIVE;
    return this;
    }

    public boolean isAlive() {
    return (state == CellState.ALIVE);
    }

    }
    5 changes: 5 additions & 0 deletions gistfile4.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    package rules;

    public enum CellState {
    DEAD, ALIVE
    }