Skip to content

Commit 6c7a33f

Browse files
committed
tests for the rules
1 parent 46ea46b commit 6c7a33f

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

cmd/game/rules_test.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package game
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestNeighborsTableDriven(t *testing.T) {
9+
tests := []struct {
10+
point Point
11+
expected int
12+
}{
13+
{Point{Row: 0, Col: 0}, 2}, // top-left corner
14+
{Point{Row: 0, Col: 1}, 3}, // top edge
15+
{Point{Row: 0, Col: 8}, 2}, // top-right corner (assuming 19x19 board)
16+
{Point{Row: 1, Col: 0}, 3}, // left edge
17+
{Point{Row: 1, Col: 1}, 4}, // center
18+
{Point{Row: 8, Col: 0}, 2}, // bottom-left corner
19+
{Point{Row: 8, Col: 8}, 2}, // bottom-right corner
20+
{Point{Row: 8, Col: 1}, 3}, // bottom edge
21+
{Point{Row: 1, Col: 8}, 3}, // right edge
22+
}
23+
for _, tc := range tests {
24+
t.Run(fmt.Sprintf("%+v", tc.point), func(t *testing.T) {
25+
checkNeighbors(t, tc.point, tc.expected)
26+
})
27+
}
28+
}
29+
30+
func checkNeighbors(t *testing.T, p Point, expected int) {
31+
n := Neighbors(p)
32+
if len(n) != expected {
33+
t.Errorf("Expected %d neighbors for %+v, got %d", expected, p, len(n))
34+
}
35+
}
36+
37+
func TestIsLegalMove(t *testing.T) {
38+
var b Board
39+
prev := b
40+
move := Point{Row: 4, Col: 4}
41+
if !IsLegalMove(b, move, Black, prev) {
42+
t.Errorf("Expected move to be legal")
43+
}
44+
}
45+
46+
func TestIsLegalMoveOccupied(t *testing.T) {
47+
var b Board
48+
b[4][4] = Black
49+
prev := b
50+
move := Point{Row: 4, Col: 4}
51+
if IsLegalMove(b, move, White, prev) {
52+
t.Errorf("Expected move to be illegal (occupied)")
53+
}
54+
}
55+
56+
func TestIsLegalMoveSuicide(t *testing.T) {
57+
var b Board
58+
// Surround (3,3) with Black stones
59+
b[2][3], b[3][2], b[3][4], b[4][3] = Black, Black, Black, Black
60+
prev := b
61+
move := Point{Row: 3, Col: 3}
62+
if IsLegalMove(b, move, White, prev) {
63+
t.Errorf("Expected move to be illegal (suicide)")
64+
}
65+
}
66+
67+
func TestIsLegalMoveKo(t *testing.T) {
68+
var b Board
69+
// Setup a simple Ko situation:
70+
// Black at (1,0), (0,1)
71+
// White at (0,0), (1,1)
72+
b[1][0] = Black
73+
b[0][1] = Black
74+
b[0][0] = White
75+
b[1][1] = White
76+
77+
// Previous board state before White captured at (0,0)
78+
var prev Board
79+
prev[1][0] = Black
80+
prev[0][1] = Black
81+
prev[1][1] = White
82+
83+
// Now Black tries to recapture at (0,0) immediately (should be illegal due to Ko)
84+
move := Point{Row: 0, Col: 0}
85+
if IsLegalMove(b, move, Black, prev) {
86+
t.Errorf("Expected move to be illegal due to Ko rule")
87+
}
88+
}
89+
90+
func TestGroupAndLibertiesSingleStone(t *testing.T) {
91+
var b Board
92+
b[0][0] = Black
93+
stones, libs := Group(b, Point{0, 0})
94+
if len(stones) != 1 || len(libs) != 2 {
95+
t.Errorf("Expected 1 stone and 2 liberties, got %d stones and %d liberties", len(stones), len(libs))
96+
}
97+
}
98+
99+
func TestGroupAndLibertiesConnectedStones(t *testing.T) {
100+
var b Board
101+
b[1][1] = Black
102+
b[1][2] = Black
103+
stones, libs := Group(b, Point{1, 1})
104+
if len(stones) != 2 {
105+
t.Errorf("Expected 2 stones, got %d", len(stones))
106+
}
107+
// (1,0), (0,1), (2,1), (1,3), (0,2), (2,2) are liberties
108+
if len(libs) != 6 {
109+
t.Errorf("Expected 6 liberties, got %d", len(libs))
110+
}
111+
}
112+
113+
func TestGroupAndLibertiesSurroundedGroup(t *testing.T) {
114+
var b Board
115+
b[2][2] = Black
116+
b[2][3] = Black
117+
b[1][2], b[1][3], b[2][1], b[2][4], b[3][2], b[3][3] = White, White, White, White, White, White
118+
stones, libs := Group(b, Point{2, 2})
119+
if len(stones) != 2 {
120+
t.Errorf("Expected 2 stones, got %d", len(stones))
121+
}
122+
if len(libs) != 0 {
123+
t.Errorf("Expected 0 liberties, got %d", len(libs))
124+
}
125+
}

0 commit comments

Comments
 (0)