More tests, including finally getting into implementation of real reqs

This commit is contained in:
Dan Buch 2012-12-09 00:22:10 -05:00
parent 8a858a9a11
commit f6546e7186
2 changed files with 70 additions and 25 deletions

View File

@ -16,6 +16,7 @@ func init() {
tsis.Set(1, 0, 1) tsis.Set(1, 0, 1)
tsis.Set(0, 1, 1) tsis.Set(0, 1, 1)
tsis.Set(1, 1, 1) tsis.Set(1, 1, 1)
tsis.Set(2, 2, 1)
} }
func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) { func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) {
@ -25,24 +26,62 @@ func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) {
} }
} }
func TestNewGameOfLifeHasGameStateOfSameDimensions(t *testing.T) {
game := NewGameOfLife(16, 16)
if len(game.State.Rows) < 16 {
t.Fail()
}
if len(game.State.Rows[0].Cols) < 16 {
t.Fail()
}
}
func TestGameStateCanImportState(t *testing.T) {
game := NewGameOfLife(16, 16)
err := game.State.ImportState(TEST_SIMPLE_INITIAL_STATE)
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 TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) { func TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) {
state := NewGameState(16, 16) game := NewGameOfLife(16, 16)
state.ImportState(TEST_SIMPLE_INITIAL_STATE) err := game.State.ImportState(TEST_SIMPLE_INITIAL_STATE)
if err != nil {
if value, err := state.Get(0, 0); err != nil || value != 1 { t.Error(err)
t.Fail()
} }
if value, err := state.Get(1, 0); err != nil || value != 1 { game.EvaluateGeneration()
t.Fail()
value, err := game.State.Get(2, 2)
if err != nil {
t.Error(err)
} }
if value, err := state.Get(0, 1); err != nil || value != 1 { if value != 0 {
t.Fail() t.Errorf("%v != 0", value)
}
if value, err := state.Get(1, 1); err != nil || value != 1 {
t.Fail()
} }
} }
@ -50,7 +89,7 @@ func TestNewGameStatesAreAllDead(t *testing.T) {
state := NewGameState(4, 4) state := NewGameState(4, 4)
for x := 0; x < 3; x++ { for x := 0; x < 3; x++ {
for y := 0; y < 3; y++ { for y := 0; y < 3; y++ {
if state.Rows[y].Cols[x] != 0 { if value, err := state.Get(x, y); err != nil || value != 0 {
t.Fail() t.Fail()
} }
} }

View File

@ -7,20 +7,21 @@ import (
type GameOfLife struct { type GameOfLife struct {
Height int Height int
Width int Width int
State *GameState
} }
type GameGridRow struct { type gameGridRow struct {
Cols []int Cols []int
} }
type GameState struct { type GameState struct {
Rows []*GameGridRow Rows []*gameGridRow
} }
func NewGameState(height, width int) *GameState { func NewGameState(height, width int) *GameState {
state := &GameState{} state := &GameState{}
for i := 0; i < height; i++ { for i := 0; i < height; i++ {
row := &GameGridRow{} row := &gameGridRow{}
for j := 0; j < width; j++ { for j := 0; j < width; j++ {
row.Cols = append(row.Cols, 0) row.Cols = append(row.Cols, 0)
} }
@ -29,7 +30,7 @@ func NewGameState(height, width int) *GameState {
return state return state
} }
func (state *GameState) GetRow(y int) (*GameGridRow, error) { func (state *GameState) GetRow(y int) (*gameGridRow, error) {
if len(state.Rows) < y+1 { if len(state.Rows) < y+1 {
return nil, errors.New("y coordinate is out of bounds!") return nil, errors.New("y coordinate is out of bounds!")
} }
@ -72,13 +73,6 @@ func (state *GameState) Deaden(x, y int) error {
return state.Set(x, y, 0) return state.Set(x, y, 0)
} }
func NewGameOfLife(height, width int) *GameOfLife {
return &GameOfLife{
Height: height,
Width: width,
}
}
func (state *GameState) ImportState(other *GameState) (err error) { func (state *GameState) ImportState(other *GameState) (err error) {
for y, row := range other.Rows { for y, row := range other.Rows {
for x, cell := range row.Cols { for x, cell := range row.Cols {
@ -89,3 +83,15 @@ func (state *GameState) ImportState(other *GameState) (err error) {
} }
return nil return nil
} }
func NewGameOfLife(height, width int) *GameOfLife {
return &GameOfLife{
Height: height,
Width: width,
State: NewGameState(height, width),
}
}
func (game *GameOfLife) EvaluateGeneration() {
return
}