Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

life_test.gno

3.66 Kb · 167 lines
  1package life
  2
  3import (
  4	"testing"
  5)
  6
  7func TestBlinker(t *testing.T) {
  8	initBoard(7, 7)
  9	// Horizontal blinker centred at (3,3)
 10	SetCell(2, 3, true)
 11	SetCell(3, 3, true)
 12	SetCell(4, 3, true)
 13
 14	if population != 3 {
 15		t.Errorf("initial population: want 3, got %d", population)
 16	}
 17
 18	Step()
 19
 20	// Step 1: vertical at (3,2),(3,3),(3,4)
 21	if !alive(3, 2) {
 22		t.Error("want (3,2) alive after step 1")
 23	}
 24	if !alive(3, 3) {
 25		t.Error("want (3,3) alive after step 1")
 26	}
 27	if !alive(3, 4) {
 28		t.Error("want (3,4) alive after step 1")
 29	}
 30	if alive(2, 3) || alive(4, 3) {
 31		t.Error("want (2,3) and (4,3) dead after step 1")
 32	}
 33	if population != 3 {
 34		t.Errorf("population after step 1: want 3, got %d", population)
 35	}
 36
 37	Step()
 38
 39	// Step 2: horizontal again
 40	if !alive(2, 3) || !alive(3, 3) || !alive(4, 3) {
 41		t.Error("want horizontal blinker after step 2")
 42	}
 43	if alive(3, 2) || alive(3, 4) {
 44		t.Error("want (3,2) and (3,4) dead after step 2")
 45	}
 46	if generation != 2 {
 47		t.Errorf("generation: want 2, got %d", generation)
 48	}
 49}
 50
 51func TestBlockStillLife(t *testing.T) {
 52	initBoard(6, 6)
 53	// 2×2 block at (2,2)
 54	SetCell(2, 2, true)
 55	SetCell(3, 2, true)
 56	SetCell(2, 3, true)
 57	SetCell(3, 3, true)
 58
 59	for i := 0; i < 5; i++ {
 60		Step()
 61		if population != 4 {
 62			t.Errorf("step %d: block changed, population=%d", i+1, population)
 63		}
 64		if !alive(2, 2) || !alive(3, 2) || !alive(2, 3) || !alive(3, 3) {
 65			t.Errorf("step %d: block cell died", i+1)
 66		}
 67	}
 68}
 69
 70func TestGliderSurvives(t *testing.T) {
 71	initBoard(20, 20)
 72	LoadPreset("glider")
 73
 74	if population != 5 {
 75		t.Errorf("glider initial population: want 5, got %d", population)
 76	}
 77
 78	// A glider has period 4 and shifts (1,1) every 4 steps; it should
 79	// survive many generations on a large-enough board.
 80	for i := 0; i < 20; i++ {
 81		Step()
 82	}
 83	if population != 5 {
 84		t.Errorf("glider after 20 steps: want population 5, got %d", population)
 85	}
 86	if generation != 20 {
 87		t.Errorf("generation: want 20, got %d", generation)
 88	}
 89}
 90
 91func TestSetCell(t *testing.T) {
 92	initBoard(5, 5)
 93
 94	SetCell(2, 2, true)
 95	if !alive(2, 2) {
 96		t.Error("want (2,2) alive after SetCell true")
 97	}
 98	if population != 1 {
 99		t.Errorf("population: want 1, got %d", population)
100	}
101
102	SetCell(2, 2, false)
103	if alive(2, 2) {
104		t.Error("want (2,2) dead after SetCell false")
105	}
106	if population != 0 {
107		t.Errorf("population: want 0, got %d", population)
108	}
109
110	// Out-of-bounds should be a no-op
111	SetCell(100, 100, true)
112	if population != 0 {
113		t.Error("out-of-bounds SetCell must not change population")
114	}
115}
116
117func TestReset(t *testing.T) {
118	initBoard(10, 10)
119	LoadPreset("block")
120
121	if population == 0 {
122		t.Error("want non-zero population after loading block preset")
123	}
124
125	Reset(5, 5)
126
127	if population != 0 {
128		t.Errorf("want 0 population after reset, got %d", population)
129	}
130	if generation != 0 {
131		t.Errorf("want 0 generation after reset, got %d", generation)
132	}
133	if width != 5 || height != 5 {
134		t.Errorf("want 5×5 board after reset, got %d×%d", width, height)
135	}
136}
137
138func TestCountNeighbors(t *testing.T) {
139	initBoard(5, 5)
140	// Live cells at (1,1),(2,1),(3,1) — blinker
141	SetCell(1, 1, true)
142	SetCell(2, 1, true)
143	SetCell(3, 1, true)
144
145	// Centre cell (2,1) has 2 live neighbours: (1,1) and (3,1)
146	if n := countNeighbors(2, 1); n != 2 {
147		t.Errorf("center of blinker: want 2 neighbours, got %d", n)
148	}
149	// Cell below center (2,2) has 3 live neighbours
150	if n := countNeighbors(2, 2); n != 3 {
151		t.Errorf("cell below center: want 3 neighbours, got %d", n)
152	}
153}
154
155func TestMultiStep(t *testing.T) {
156	initBoard(20, 20)
157	LoadPreset("glider")
158
159	MultiStep(4)
160
161	if generation != 4 {
162		t.Errorf("generation after MultiStep(4): want 4, got %d", generation)
163	}
164	if population != 5 {
165		t.Errorf("glider population after 4 steps: want 5, got %d", population)
166	}
167}