2012-12-09 02:47:21 +00:00
|
|
|
package conway_test
|
|
|
|
|
|
|
|
import (
|
2012-12-10 03:51:10 +00:00
|
|
|
"fmt"
|
2012-12-16 14:56:38 +00:00
|
|
|
"math/rand"
|
2012-12-10 04:20:43 +00:00
|
|
|
"strings"
|
2012-12-09 03:44:45 +00:00
|
|
|
"testing"
|
2012-12-09 02:47:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
import (
|
2012-12-11 23:28:10 +00:00
|
|
|
. "github.com/meatballhat/box-o-sand/conway"
|
2012-12-09 02:47:21 +00:00
|
|
|
)
|
|
|
|
|
2012-12-09 03:44:45 +00:00
|
|
|
var TEST_SIMPLE_INITIAL_STATE = NewGameState(16, 16)
|
|
|
|
|
|
|
|
func init() {
|
2012-12-10 04:20:43 +00:00
|
|
|
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)
|
2012-12-09 03:44:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
func newTestGameState() *GameState {
|
2012-12-12 02:04:47 +00:00
|
|
|
state := NewGameState(16, 16)
|
2012-12-16 17:17:44 +00:00
|
|
|
state.Import(TEST_SIMPLE_INITIAL_STATE)
|
|
|
|
return state
|
2012-12-12 02:04:47 +00:00
|
|
|
}
|
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
func newTestGameOfLife() *GameOfLife {
|
2012-12-09 03:44:45 +00:00
|
|
|
game := NewGameOfLife(16, 16)
|
2012-12-16 17:17:44 +00:00
|
|
|
game.State.Import(TEST_SIMPLE_INITIAL_STATE)
|
|
|
|
return game
|
2012-12-12 02:04:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) {
|
2012-12-16 17:17:44 +00:00
|
|
|
game := newTestGameOfLife()
|
2012-12-09 03:44:45 +00:00
|
|
|
|
2012-12-10 03:16:16 +00:00
|
|
|
game.EvaluateGeneration()
|
2012-12-09 04:08:40 +00:00
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
cell := game.State.Get(3, 2)
|
|
|
|
if cell.Value != 0 {
|
|
|
|
t.Errorf("%v != 0", cell.Value)
|
2012-12-10 03:16:16 +00:00
|
|
|
return
|
2012-12-09 04:08:40 +00:00
|
|
|
}
|
2012-12-09 05:22:10 +00:00
|
|
|
}
|
2012-12-09 04:08:40 +00:00
|
|
|
|
2012-12-10 03:16:16 +00:00
|
|
|
func TestLiveCellWithTwoOrThreeLiveNeighborsLivesOn(t *testing.T) {
|
2012-12-16 17:17:44 +00:00
|
|
|
game := newTestGameOfLife()
|
2012-12-09 05:22:10 +00:00
|
|
|
|
2012-12-10 03:16:16 +00:00
|
|
|
game.EvaluateGeneration()
|
2012-12-09 04:08:40 +00:00
|
|
|
|
2012-12-16 21:04:54 +00:00
|
|
|
cell := game.State.Get(0, 2)
|
2012-12-16 17:17:44 +00:00
|
|
|
if cell.Value != 1 {
|
|
|
|
t.Errorf("%v != 1", cell.Value)
|
2012-12-10 03:16:16 +00:00
|
|
|
return
|
2012-12-09 05:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-10 03:16:16 +00:00
|
|
|
func TestLiveCellWithMoreThanThreeLiveNeighborsDies(t *testing.T) {
|
2012-12-16 17:17:44 +00:00
|
|
|
game := newTestGameOfLife()
|
2012-12-09 05:22:10 +00:00
|
|
|
|
|
|
|
game.EvaluateGeneration()
|
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
cell := game.State.Get(0, 1)
|
|
|
|
if cell.Value != 0 {
|
|
|
|
t.Errorf("%v != 0", cell.Value)
|
2012-12-10 02:05:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-10 03:33:42 +00:00
|
|
|
func TestDeadCellWithExactlyThreeLiveNeighborsBecomesAlive(t *testing.T) {
|
2012-12-16 17:17:44 +00:00
|
|
|
game := newTestGameOfLife()
|
2012-12-10 03:33:42 +00:00
|
|
|
|
|
|
|
game.EvaluateGeneration()
|
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
cell := game.State.Get(3, 1)
|
|
|
|
if cell.Value != 1 {
|
|
|
|
t.Errorf("%v != 1", cell.Value)
|
2012-12-10 03:33:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-16 22:59:54 +00:00
|
|
|
func TestGameOfLifeCanDisplayItselfAsAString(t *testing.T) {
|
2012-12-16 17:17:44 +00:00
|
|
|
game := newTestGameOfLife()
|
2012-12-10 03:51:10 +00:00
|
|
|
|
|
|
|
grid := fmt.Sprintf("%s\n", game)
|
|
|
|
|
|
|
|
gridLen := len(grid)
|
|
|
|
if gridLen < 256 {
|
|
|
|
t.Errorf("%v < 256", gridLen)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-16 22:59:54 +00:00
|
|
|
func TestGameOfLifeCanDisplayItselfAsAnImage(t *testing.T) {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2012-12-10 03:16:16 +00:00
|
|
|
func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) {
|
2012-12-16 17:17:44 +00:00
|
|
|
game := newTestGameOfLife()
|
2012-12-12 02:04:47 +00:00
|
|
|
|
2012-12-10 03:16:16 +00:00
|
|
|
if game.State.Height() != 16 || game.State.Width() != 16 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
2012-12-10 02:05:40 +00:00
|
|
|
|
2012-12-10 03:16:16 +00:00
|
|
|
func TestNewGameOfLifeHasGameStateOfSameDimensions(t *testing.T) {
|
2012-12-16 17:17:44 +00:00
|
|
|
game := newTestGameOfLife()
|
2012-12-10 03:16:16 +00:00
|
|
|
|
|
|
|
if len(game.State.Rows) < 16 {
|
|
|
|
t.Fail()
|
2012-12-10 02:05:40 +00:00
|
|
|
}
|
|
|
|
|
2012-12-16 14:56:38 +00:00
|
|
|
if len(game.State.Rows[0].Cells) < 16 {
|
2012-12-10 03:16:16 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
2012-12-10 02:05:40 +00:00
|
|
|
|
2012-12-10 03:16:16 +00:00
|
|
|
func TestGameStateCanImportState(t *testing.T) {
|
2012-12-16 17:17:44 +00:00
|
|
|
game := newTestGameOfLife()
|
2012-12-10 02:05:40 +00:00
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
if game.State.Get(0, 0).Value != 1 {
|
2012-12-10 03:16:16 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
if game.State.Get(1, 0).Value != 1 {
|
2012-12-10 03:16:16 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
if game.State.Get(0, 1).Value != 1 {
|
2012-12-10 03:16:16 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
if game.State.Get(1, 1).Value != 1 {
|
2012-12-10 03:16:16 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
if game.State.Get(2, 2).Value != 1 {
|
2012-12-10 03:16:16 +00:00
|
|
|
t.Fail()
|
2012-12-09 05:22:10 +00:00
|
|
|
}
|
2012-12-09 03:44:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-10 04:20:43 +00:00
|
|
|
func TestGameOfLifeCanImportRandomState(t *testing.T) {
|
2012-12-16 17:17:44 +00:00
|
|
|
game := newTestGameOfLife()
|
2012-12-10 04:20:43 +00:00
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
err := game.ImportRandomState()
|
2012-12-10 04:20:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
zeroCount := 0
|
|
|
|
|
|
|
|
for y := 0; y < 16; y++ {
|
|
|
|
for x := 0; x < 16; x++ {
|
2012-12-16 17:17:44 +00:00
|
|
|
cell := game.State.Get(x, y)
|
|
|
|
if cell.Value == 0 {
|
2012-12-10 04:20:43 +00:00
|
|
|
zeroCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if zeroCount == 256 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-16 22:59:54 +00:00
|
|
|
func TestGameStateCanDisplayItselfAsAString(t *testing.T) {
|
2012-12-16 17:17:44 +00:00
|
|
|
state := newTestGameState()
|
2012-12-10 04:20:43 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-16 22:59:54 +00:00
|
|
|
func TestGameStateCanDisplayItselfAsAnImage(t *testing.T) {
|
|
|
|
state := newTestGameState()
|
|
|
|
|
|
|
|
img, err := state.Image(1, 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bounds := img.Bounds()
|
|
|
|
if bounds.Max.Y < 16 {
|
|
|
|
t.Errorf("image.Max.Y < 16!: %d", bounds.Max.Y)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if bounds.Max.X < 16 {
|
|
|
|
t.Errorf("image.Max.X < 16!: %d", bounds.Max.X)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-09 03:44:45 +00:00
|
|
|
func TestNewGameStatesAreAllDead(t *testing.T) {
|
|
|
|
state := NewGameState(4, 4)
|
|
|
|
for x := 0; x < 3; x++ {
|
|
|
|
for y := 0; y < 3; y++ {
|
2012-12-16 17:17:44 +00:00
|
|
|
if state.Get(x, y).Value != 0 {
|
2012-12-09 03:44:45 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-09 04:08:40 +00:00
|
|
|
func TestGameStateCanGetCellValueByCoords(t *testing.T) {
|
|
|
|
state := NewGameState(8, 8)
|
2012-12-16 17:17:44 +00:00
|
|
|
if state.Get(2, 2).Value != 0 {
|
2012-12-09 04:08:40 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGameStateCanSetCellValueByCoords(t *testing.T) {
|
|
|
|
state := NewGameState(8, 8)
|
2012-12-16 17:17:44 +00:00
|
|
|
state.Set(2, 5, 1)
|
2012-12-09 04:08:40 +00:00
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
if state.Get(2, 5).Value != 1 {
|
2012-12-09 04:08:40 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-09 03:44:45 +00:00
|
|
|
func TestGameStateCanEnlivenCellsByCoords(t *testing.T) {
|
|
|
|
state := NewGameState(8, 8)
|
2012-12-16 17:17:44 +00:00
|
|
|
state.Enliven(0, 1)
|
2012-12-09 03:44:45 +00:00
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
if state.Get(0, 1).Value != 1 {
|
2012-12-09 03:44:45 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGameStateCanDeadenCellsByCoords(t *testing.T) {
|
|
|
|
state := NewGameState(8, 8)
|
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
state.Enliven(0, 1)
|
2012-12-09 03:44:45 +00:00
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
if state.Get(0, 1).Value != 1 {
|
2012-12-09 03:44:45 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
state.Deaden(0, 1)
|
2012-12-09 03:44:45 +00:00
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
if state.Get(0, 1).Value != 0 {
|
2012-12-09 03:44:45 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2012-12-09 02:47:21 +00:00
|
|
|
}
|
2012-12-10 02:05:40 +00:00
|
|
|
|
|
|
|
func TestNewGenerationScoreCardIsAllZeros(t *testing.T) {
|
|
|
|
gen := NewGenerationScoreCard(4, 4)
|
|
|
|
|
|
|
|
for y := 0; y < gen.Height(); y++ {
|
|
|
|
for x := 0; x < gen.Width(); x++ {
|
2012-12-16 17:17:44 +00:00
|
|
|
if gen.Get(x, y).Value != 0 {
|
2012-12-10 02:05:40 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerationScoreCardCalculateResultsInCorrectNeighborCounts(t *testing.T) {
|
|
|
|
genScore := NewGenerationScoreCard(16, 16)
|
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
state := newTestGameState()
|
2012-12-10 02:05:40 +00:00
|
|
|
|
|
|
|
genScore.Calculate(state)
|
2012-12-16 17:17:44 +00:00
|
|
|
|
|
|
|
cell := genScore.Get(0, 0)
|
2012-12-16 21:04:54 +00:00
|
|
|
if cell.Value != 4 {
|
|
|
|
t.Errorf("[0, 0] value %v != 4", cell.Value)
|
2012-12-10 02:05:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
cell = genScore.Get(3, 2)
|
|
|
|
if cell.Value != 1 {
|
|
|
|
t.Errorf("[3, 2] value %d != 1", cell.Value)
|
2012-12-10 02:05:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2012-12-10 05:19:31 +00:00
|
|
|
|
|
|
|
func TestStateCanMutate(t *testing.T) {
|
2012-12-16 17:17:44 +00:00
|
|
|
state := newTestGameState()
|
2012-12-10 05:19:31 +00:00
|
|
|
|
2012-12-12 03:30:34 +00:00
|
|
|
curCksum, err := state.Checksum()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
state.Mutate()
|
2012-12-10 05:19:31 +00:00
|
|
|
|
2012-12-12 03:30:34 +00:00
|
|
|
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) {
|
2012-12-16 17:17:44 +00:00
|
|
|
state := newTestGameState()
|
2012-12-12 03:30:34 +00:00
|
|
|
|
|
|
|
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) {
|
2012-12-16 17:17:44 +00:00
|
|
|
state := newTestGameState()
|
2012-12-12 03:30:34 +00:00
|
|
|
|
|
|
|
cells, err := state.Cells()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for cell := range cells {
|
2012-12-16 17:17:44 +00:00
|
|
|
cell.Value = 1
|
2012-12-12 03:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cells, err = state.Cells()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for cell := range cells {
|
|
|
|
if cell.Value != 1 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
2012-12-10 05:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-12 04:35:49 +00:00
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
func TestGameStateCellsCanBeFetchedByXAndYCoords(t *testing.T) {
|
|
|
|
state := newTestGameState()
|
|
|
|
|
|
|
|
if state.Get(3, 4) == nil {
|
|
|
|
t.Fail()
|
2012-12-12 04:35:49 +00:00
|
|
|
}
|
2012-12-16 17:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGameOfLifeEmitsGenerationTicksUntilStasis(t *testing.T) {
|
|
|
|
game := newTestGameOfLife()
|
2012-12-12 04:35:49 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2012-12-16 14:56:38 +00:00
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
func TestGameStateCoordsAreNeverOutOfBounds(t *testing.T) {
|
|
|
|
state := newTestGameState()
|
2012-12-16 14:56:38 +00:00
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
oobX, oobY := state.Width()+rand.Intn(99), state.Height()+rand.Intn(99)
|
2012-12-16 14:56:38 +00:00
|
|
|
|
|
|
|
newVal := rand.Intn(9999)
|
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
state.Set(oobX, oobY, newVal)
|
|
|
|
cell := state.Get(oobX, oobY)
|
2012-12-16 14:56:38 +00:00
|
|
|
|
2012-12-16 17:17:44 +00:00
|
|
|
if cell == nil {
|
|
|
|
t.Fail()
|
2012-12-16 14:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cell.Value != newVal {
|
2012-12-16 17:17:44 +00:00
|
|
|
t.Errorf("(%d, %d) != %d: %d", oobX, oobY, newVal, cell.Value)
|
2012-12-16 14:56:38 +00:00
|
|
|
}
|
|
|
|
}
|