1 // BoardTest.java
2 
3 package net.sf.gogui.go;
4 
5 import static net.sf.gogui.go.GoColor.BLACK;
6 import static net.sf.gogui.go.GoColor.WHITE;
7 import static net.sf.gogui.go.GoColor.EMPTY;
8 
9 public final class BoardTest
10     extends junit.framework.TestCase
11 {
main(String args[])12     public static void main(String args[])
13     {
14         junit.textui.TestRunner.run(suite());
15     }
16 
suite()17     public static junit.framework.Test suite()
18     {
19         return new junit.framework.TestSuite(BoardTest.class);
20     }
21 
testBothPassed()22     public void testBothPassed()
23     {
24         Board board = new Board(19);
25         assertFalse(board.bothPassed());
26         board.play(BLACK, GoPoint.get(0, 0));
27         assertFalse(board.bothPassed());
28         board.play(WHITE, null);
29         assertFalse(board.bothPassed());
30         board.play(BLACK, null);
31         assertTrue(board.bothPassed());
32         board.play(WHITE, null);
33         assertTrue(board.bothPassed());
34     }
35 
testCapture()36     public void testCapture()
37     {
38         Board board = new Board(19);
39         board.play(BLACK, GoPoint.get(0, 0));
40         board.play(BLACK, GoPoint.get(1, 0));
41         board.play(WHITE, GoPoint.get(0, 1));
42         board.play(WHITE, GoPoint.get(1, 1));
43         board.play(WHITE, GoPoint.get(2, 0));
44         assertEquals(EMPTY, board.getColor(GoPoint.get(0, 0)));
45         assertEquals(EMPTY, board.getColor(GoPoint.get(1, 0)));
46         assertEquals(WHITE, board.getColor(GoPoint.get(0, 1)));
47         assertEquals(WHITE, board.getColor(GoPoint.get(1, 1)));
48         assertEquals(WHITE, board.getColor(GoPoint.get(2, 0)));
49         assertEquals(2, board.getCaptured(BLACK));
50         assertEquals(0, board.getCaptured(WHITE));
51     }
52 
testContains()53     public void testContains()
54     {
55         Board board = new Board(19);
56         assertTrue(board.contains(GoPoint.get(0, 0)));
57         assertTrue(board.contains(GoPoint.get(0, 18)));
58         assertTrue(board.contains(GoPoint.get(18, 0)));
59         assertTrue(board.contains(GoPoint.get(18, 18)));
60         assertFalse(board.contains(GoPoint.get(0, 19)));
61         assertFalse(board.contains(GoPoint.get(19, 0)));
62         assertFalse(board.contains(GoPoint.get(19, 19)));
63         assertFalse(board.contains(GoPoint.get(20, 20)));
64     }
65 
66     /** Test Board.getKilled(). */
testGetKilled()67     public void testGetKilled()
68     {
69         Board board = new Board(19);
70         // 4 . . . . .
71         // 3 . . O . .
72         // 2 O O @ O .
73         // 1 @ @ . . .
74         //   A B C D E
75         PointList black = new PointList();
76         PointList white = new PointList();
77         black.add(GoPoint.get(0, 0));
78         black.add(GoPoint.get(1, 0));
79         black.add(GoPoint.get(2, 1));
80         white.add(GoPoint.get(0, 1));
81         white.add(GoPoint.get(1, 1));
82         white.add(GoPoint.get(2, 2));
83         white.add(GoPoint.get(3, 1));
84         board.setup(black, white, BLACK);
85         board.play(WHITE, GoPoint.get(2, 0));
86         ConstPointList killed = board.getKilled();
87         assertEquals(3, killed.size());
88         assertTrue(killed.contains(GoPoint.get(0, 0)));
89         assertTrue(killed.contains(GoPoint.get(1, 0)));
90         assertTrue(killed.contains(GoPoint.get(2, 1)));
91         board.undo();
92         board.play(WHITE, GoPoint.get(3, 0));
93         assertTrue(board.getKilled().isEmpty());
94     }
95 
96     /** Test Board.getSuicide(). */
testGetSuicide()97     public void testGetSuicide()
98     {
99         Board board = new Board(19);
100         // 4 . . . .
101         // 3 O . . .
102         // 2 @ O . .
103         // 1 . @ O .
104         //   A B C D
105         PointList black = new PointList();
106         PointList white = new PointList();
107         black.add(GoPoint.get(0, 1));
108         black.add(GoPoint.get(1, 0));
109         white.add(GoPoint.get(0, 2));
110         white.add(GoPoint.get(1, 1));
111         white.add(GoPoint.get(2, 0));
112         board.setup(black, white, BLACK);
113         board.play(BLACK, GoPoint.get(0, 0));
114         ConstPointList suicide = board.getSuicide();
115         assertEquals(3, suicide.size());
116         assertTrue(suicide.contains(GoPoint.get(0, 0)));
117         assertTrue(suicide.contains(GoPoint.get(0, 1)));
118         assertTrue(suicide.contains(GoPoint.get(1, 0)));
119     }
120 
121     /** Test Board.isKo(). */
testIsKo()122     public void testIsKo()
123     {
124         Board board = new Board(19);
125         // 3 . . . .
126         // 2 @ O . .
127         // 1 . @ O .
128         //   A B C D
129         PointList black = new PointList();
130         PointList white = new PointList();
131         black.add(GoPoint.get(0, 1));
132         black.add(GoPoint.get(1, 0));
133         white.add(GoPoint.get(1, 1));
134         white.add(GoPoint.get(2, 0));
135         board.setup(black, white, BLACK);
136         assertFalse(board.isKo(GoPoint.get(0, 0)));
137         board.play(WHITE, GoPoint.get(0, 0));
138         assertTrue(board.isKo(GoPoint.get(1, 0)));
139         board.play(BLACK, GoPoint.get(5, 5));
140         assertFalse(board.isKo(GoPoint.get(1, 0)));
141         board.undo();
142         assertTrue(board.isKo(GoPoint.get(1, 0)));
143     }
144 
testIsSuicide()145     public void testIsSuicide()
146     {
147         Board board = new Board(19);
148         assertFalse(board.isSuicide(WHITE, GoPoint.get(0, 0)));
149         board.play(BLACK, GoPoint.get(0, 1));
150         assertFalse(board.isSuicide(WHITE, GoPoint.get(0, 0)));
151         board.play(BLACK, GoPoint.get(1, 1));
152         assertFalse(board.isSuicide(WHITE, GoPoint.get(0, 0)));
153         board.play(BLACK, GoPoint.get(2, 0));
154         assertFalse(board.isSuicide(WHITE, GoPoint.get(0, 0)));
155         board.play(WHITE, GoPoint.get(1, 0));
156         assertTrue(board.isSuicide(WHITE, GoPoint.get(0, 0)));
157         board.play(BLACK, GoPoint.get(0, 0));
158         assertTrue(board.isSuicide(WHITE, GoPoint.get(1, 0)));
159     }
160 
testGetLastMove()161     public void testGetLastMove()
162     {
163         Board board = new Board(19);
164         assertNull(board.getLastMove());
165         board.play(BLACK, GoPoint.get(0, 0));
166         assertEquals(Move.get(BLACK, 0, 0), board.getLastMove());
167         board.setup(new PointList(GoPoint.get(1, 1)), null, BLACK);
168         assertNull(board.getLastMove());
169     }
170 
171     /** Test that playing on a occupied field does not fail.
172         Board.play spciefies that a play never fails.
173         Also tests that the old stone is correctly restored. */
testPlayOnOccupied()174     public void testPlayOnOccupied()
175     {
176         Board board = new Board(19);
177         GoPoint point = GoPoint.get(0, 0);
178         board.play(WHITE, point);
179         board.play(BLACK, point);
180         board.undo();
181         assertEquals(WHITE, board.getColor(point));
182     }
183 
184     /** Test that setup does not cause suicide. */
testSetupSuicide()185     public void testSetupSuicide()
186     {
187         Board board = new Board(19);
188         board.play(BLACK, GoPoint.get(1, 0));
189         board.play(BLACK, GoPoint.get(0, 1));
190         board.setup(null, new PointList(GoPoint.get(0, 0)), BLACK);
191         assertEquals(WHITE, board.getColor(GoPoint.get(0, 0)));
192     }
193 
194     /** Test that setup does not capture anything. */
testSetupCapture()195     public void testSetupCapture()
196     {
197         Board board = new Board(19);
198         board.play(BLACK, GoPoint.get(1, 0));
199         board.play(WHITE, GoPoint.get(2, 0));
200         board.play(BLACK, GoPoint.get(0, 1));
201         board.play(WHITE, GoPoint.get(1, 1));
202         board.setup(null, new PointList(GoPoint.get(0, 0)), BLACK);
203         assertEquals(WHITE, board.getColor(GoPoint.get(0, 0)));
204     }
205 
testSetupHandicap()206     public void testSetupHandicap()
207     {
208         Board board = new Board(19);
209         PointList stones = new PointList();
210         stones.add(GoPoint.get(4, 4));
211         stones.add(GoPoint.get(5, 5));
212         board.setupHandicap(stones);
213         assertEquals(WHITE, board.getToMove());
214         assertEquals(BLACK, board.getColor(GoPoint.get(4, 4)));
215         assertEquals(BLACK, board.getColor(GoPoint.get(5, 5)));
216         assertEquals(stones, board.getSetup(BLACK));
217     }
218 
testToMove()219     public void testToMove()
220     {
221         Board board = new Board(19);
222         assertEquals(BLACK, board.getToMove());
223         board.play(BLACK, GoPoint.get(0, 0));
224         assertEquals(WHITE, board.getToMove());
225         board.setup(new PointList(GoPoint.get(1, 1)), null, BLACK);
226         assertEquals(BLACK, board.getToMove());
227     }
228 
testUndo()229     public void testUndo()
230     {
231         Board board = new Board(19);
232         board.play(BLACK, GoPoint.get(0, 0));
233         board.undo();
234         assertEquals(EMPTY, board.getColor(GoPoint.get(0, 0)));
235         assertEquals(BLACK, board.getToMove());
236     }
237 }
238