Skip to content

Commit

Permalink
more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonWaldherr committed Nov 6, 2018
1 parent 956d01c commit 9acc964
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.idea
cmd/cgol/cgol
tmp.gif
*.gif
9 changes: 3 additions & 6 deletions gif/gif.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,15 @@ func buildImage(arr [][]int) *image.Paletted {
func writeGif(name string, g *gif.GIF) {
w, err := os.Create(name + ".gif")
if err != nil {
fmt.Println("os.Create")
panic(err)
fmt.Printf("os.Create Error: %v\n", err)
}
defer func() {
if err := w.Close(); err != nil {
fmt.Println("w.Close")
panic(err)
fmt.Printf("w.Close Error: %v\n", err)
}
}()
err = gif.EncodeAll(w, g)
if err != nil {
fmt.Println("gif.EncodeAll")
panic(err)
fmt.Printf("gif.EncodeAll Error: %v\n", err)
}
}
10 changes: 10 additions & 0 deletions gif/gif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ func Test_GIF(t *testing.T) {
gv.AddFrame([][]int{{0, 1, 0}, {0, 0, 1}, {1, 1, 1}})
gv.Complete()
}

func Test_Complete(t *testing.T) {
var gv = &GifVisualizer{}
gv.Setup("//")

gv.AddFrame([][]int{{0, 1, 0}, {0, 0, 1}, {1, 1, 1}})
gv.AddFrame([][]int{{1, 0, 1}, {1, 1, 0}, {0, 0, 0}})
gv.AddFrame([][]int{{0, 1, 0}, {0, 0, 1}, {1, 1, 1}})
gv.Complete()
}
20 changes: 12 additions & 8 deletions life/life.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func (field *Field) getVitality(x, y int) int {
return field.cells[y][x]
}

func (field *Field) nextVitality(x, y int) int {
// LivingNeighbors returns the number of living neighbors of a cell
func (field *Field) LivingNeighbors(x, y int) int {
alive := 0
for i := -1; i <= 1; i++ {
for j := -1; j <= 1; j++ {
Expand All @@ -34,12 +35,15 @@ func (field *Field) nextVitality(x, y int) int {
}
}
}
vitality := field.getVitality(x, y)
if alive == 3 || alive == 2 && (vitality > 0) {
if vitality < 8 {
return vitality + 1
}
return vitality
return alive
}

// NextVitality returns the vitality of a cell in the next round
func (field *Field) NextVitality(x, y int) int {
livingNeighbors := field.LivingNeighbors(x, y)
isLiving := field.getVitality(x, y) > 0
if livingNeighbors == 3 || (livingNeighbors == 2 && isLiving) {
return 1
}
return 0
}
Expand All @@ -49,7 +53,7 @@ func (field *Field) NextRound() *Field {
newFieldVar := newField(field.width, field.height)
for y := 0; y < field.height; y++ {
for x := 0; x < field.width; x++ {
newFieldVar.setVitality(x, y, field.nextVitality(x, y))
newFieldVar.setVitality(x, y, field.NextVitality(x, y))
}
}
return newFieldVar
Expand Down
2 changes: 2 additions & 0 deletions life/life_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ func Test_LoadErrors(t *testing.T) {
folder := getFolder()

field := LoadFirstRound(64, 64, "foo")
field = LoadFirstRound(64, 64, "/")
field = LoadFirstRound(64, 64, folder)
field = LoadFirstRound(64, 64, folder+"24.txt")
field = LoadFirstRound(64, 64, folder+"25.rle")
field = LoadFirstRound(64, 64, folder+"10.rle")

for i := 0; i != 64; i++ {
field = field.NextRound()
Expand Down

0 comments on commit 9acc964

Please sign in to comment.