package conway_test import ( "fmt" "strings" "testing" ) import ( . "github.com/meatballhat/box-o-sand/conway" ) var TEST_SIMPLE_INITIAL_STATE = NewGameState(16, 16) func init() { s := TEST_SIMPLE_INITIAL_STATE s.Set(0, 0, 1) s.Set(0, 1, 1) s.Set(0, 2, 1) s.Set(1, 0, 1) s.Set(1, 1, 1) s.Set(2, 2, 1) s.Set(3, 0, 1) s.Set(3, 2, 1) s.Set(13, 13, 1) s.Set(13, 14, 1) s.Set(14, 14, 1) s.Set(14, 15, 1) s.Set(15, 13, 1) s.Set(15, 15, 1) } func newTestGameState() (*GameState, error) { state := NewGameState(16, 16) err := state.Import(TEST_SIMPLE_INITIAL_STATE) if err != nil { return nil, err } return state, nil } func newTestGameOfLife() (*GameOfLife, error) { game := NewGameOfLife(16, 16) err := game.State.Import(TEST_SIMPLE_INITIAL_STATE) if err != nil { return nil, err } return game, nil } func TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) { game, err := newTestGameOfLife() if err != nil { t.Error(err) return } game.EvaluateGeneration() value, err := game.State.Get(3, 2) if err != nil { t.Error(err) return } if value != 0 { t.Errorf("%v != 0", value) return } } func TestLiveCellWithTwoOrThreeLiveNeighborsLivesOn(t *testing.T) { game, err := newTestGameOfLife() if err != nil { t.Error(err) return } game.EvaluateGeneration() value, err := game.State.Get(0, 0) if err != nil { t.Error(err) return } if value != 1 { t.Errorf("%v != 1", value) return } } func TestLiveCellWithMoreThanThreeLiveNeighborsDies(t *testing.T) { game, err := newTestGameOfLife() if err != nil { t.Error(err) return } game.EvaluateGeneration() value, err := game.State.Get(0, 1) if err != nil { t.Error(err) return } if value != 0 { t.Errorf("%v != 0", value) return } } func TestDeadCellWithExactlyThreeLiveNeighborsBecomesAlive(t *testing.T) { game, err := newTestGameOfLife() if err != nil { t.Error(err) return } game.EvaluateGeneration() value, err := game.State.Get(3, 1) if err != nil { t.Error(err) return } if value != 1 { t.Errorf("%v != 1", value) return } } func TestGameOfLifeCanDisplayItself(t *testing.T) { game, err := newTestGameOfLife() if err != nil { t.Error(err) return } grid := fmt.Sprintf("%s\n", game) gridLen := len(grid) if gridLen < 256 { t.Errorf("%v < 256", gridLen) t.Fail() } } func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) { game, err := newTestGameOfLife() if err != nil { t.Error(err) return } if game.State.Height() != 16 || game.State.Width() != 16 { t.Fail() } } func TestNewGameOfLifeHasGameStateOfSameDimensions(t *testing.T) { game, err := newTestGameOfLife() if err != nil { t.Error(err) return } if len(game.State.Rows) < 16 { t.Fail() } if len(game.State.Rows[0].Cols) < 16 { t.Fail() } } func TestGameStateCanImportState(t *testing.T) { game, err := newTestGameOfLife() if err != nil { t.Fail() } if value, err := game.State.Get(0, 0); err != nil || value != 1 { t.Fail() } if value, err := game.State.Get(1, 0); err != nil || value != 1 { t.Fail() } if value, err := game.State.Get(0, 1); err != nil || value != 1 { t.Fail() } if value, err := game.State.Get(1, 1); err != nil || value != 1 { t.Fail() } if value, err := game.State.Get(2, 2); err != nil || value != 1 { t.Fail() } } func TestGameOfLifeCanImportRandomState(t *testing.T) { game, err := newTestGameOfLife() if err != nil { t.Error(err) return } err = game.ImportRandomState() if err != nil { t.Error(err) return } zeroCount := 0 for y := 0; y < 16; y++ { for x := 0; x < 16; x++ { value, err := game.State.Get(x, y) if err != nil { t.Error(err) return } if value == 0 { zeroCount++ } } } if zeroCount == 256 { t.Fail() } } func TestGameStateCanDisplayItself(t *testing.T) { state, err := newTestGameState() if err != nil { t.Error(err) return } grid := fmt.Sprintf("%s\n", state) lenGrid := len(grid) if lenGrid < 256 { t.Errorf("%v < 256", lenGrid) return } nLines := strings.Count(grid, "\n") if nLines < 16 { t.Errorf("%v < 16 (lines)", nLines) return } } func TestNewGameStatesAreAllDead(t *testing.T) { state := NewGameState(4, 4) for x := 0; x < 3; x++ { for y := 0; y < 3; y++ { if value, err := state.Get(x, y); err != nil || value != 0 { t.Fail() } } } } func TestGameStateCanGetCellValueByCoords(t *testing.T) { state := NewGameState(8, 8) if value, err := state.Get(2, 2); err != nil || value != 0 { t.Fail() } } func TestGameStateCanSetCellValueByCoords(t *testing.T) { state := NewGameState(8, 8) if err := state.Set(2, 5, 1); err != nil { t.Fail() } if value, err := state.Get(2, 5); err != nil || value != 1 { t.Fail() } } func TestGameStateCanEnlivenCellsByCoords(t *testing.T) { state := NewGameState(8, 8) if err := state.Enliven(0, 1); err != nil { t.Fail() } if value, err := state.Get(0, 1); err != nil || value != 1 { t.Fail() } } func TestGameStateCanDeadenCellsByCoords(t *testing.T) { state := NewGameState(8, 8) if err := state.Enliven(0, 1); err != nil { t.Fail() } if value, err := state.Get(0, 1); err != nil || value != 1 { t.Fail() } if err := state.Deaden(0, 1); err != nil { t.Fail() } if value, err := state.Get(0, 1); err != nil || value != 0 { t.Fail() } } func TestNewGenerationScoreCardIsAllZeros(t *testing.T) { gen := NewGenerationScoreCard(4, 4) for y := 0; y < gen.Height(); y++ { for x := 0; x < gen.Width(); x++ { if value, err := gen.Get(x, y); err != nil || value != 0 { t.Fail() } } } } func TestGenerationScoreCardCalculateResultsInCorrectNeighborCounts(t *testing.T) { genScore := NewGenerationScoreCard(16, 16) state, err := newTestGameState() if err != nil { t.Error(err) return } genScore.Calculate(state) value, err := genScore.Get(0, 0) if err != nil { t.Error(err) return } else if value != 3 { t.Errorf("[0, 0] value %v != 3", value) return } value, err = genScore.Get(3, 2) if err != nil { t.Error(err) return } else if value != 1 { t.Errorf("[3, 2] value %d != 1", value) return } } func TestStateCanMutate(t *testing.T) { state, err := newTestGameState() if err != nil { t.Error(err) return } curCksum, err := state.Checksum() if err != nil { t.Error(err) return } err = state.Mutate() if err != nil { t.Error(err) return } newCksum, err := state.Checksum() if err != nil { t.Error(err) return } if curCksum == newCksum { t.Errorf("Checksum did not change! %v", newCksum) } } func TestStateProvidesCellIteratorChannel(t *testing.T) { state, err := newTestGameState() if err != nil { t.Error(err) return } cells, err := state.Cells() if err != nil { t.Error(err) return } cellCount := 0 for _ = range cells { cellCount++ } if cellCount != 256 { t.Fail() } } func TestGameStateCellsCanHaveTheirValueSet(t *testing.T) { state, err := newTestGameState() if err != nil { t.Error(err) return } cells, err := state.Cells() if err != nil { t.Error(err) return } for cell := range cells { cell.SetValue(1) } cells, err = state.Cells() if err != nil { t.Error(err) return } for cell := range cells { if cell.Value != 1 { t.Fail() } } } func TestGameOfLifeEmitsGenerationTicksUntilStasis(t *testing.T) { game, err := newTestGameOfLife() if err != nil { t.Error(err) return } genTicks, err := game.Generations() if err != nil { t.Error(err) return } nGens := 0 for gen := range genTicks { if gen.N > 3000 { t.Errorf("%v is too many generations!", gen.N) return } nGens = gen.N } if nGens < 3 { t.Errorf("%v is too few generations!", nGens) } }