-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardTest.java
155 lines (133 loc) · 4.27 KB
/
BoardTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import static org.junit.Assert.assertEquals;
import java.awt.Color;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
import junit.framework.TestCase;
public class BoardTest extends TestCase {
@Test
// Check empty board.
public void testEmptyBoard ( ) {
Board b = new Board ( );
assertTrue (b.isOK ( ));
checkCollection (b, 0, 0); // applies more tests
assertTrue (!b.formsTriangle (new Connector (1, 2), Color.RED));
}
@Test
// Check one-connector board.
public void test1Connector ( ) {
Board b = new Board ( );
b.add (new Connector (1, 2), Color.RED);
assertTrue (b.isOK ( ));
checkCollection (b, 1, 0);
Iterator<Connector> iter = b.connectors (Color.RED);
assertTrue (iter.hasNext ( ));
Connector cnctr = iter.next ( );
assertEquals (b.colorOf (cnctr), Color.RED);
assertEquals (new Connector (1, 2), cnctr);
assertTrue (!iter.hasNext ( ));
assertTrue (!b.formsTriangle (new Connector(1,3), Color.RED));
assertTrue (!b.formsTriangle (new Connector(5,6), Color.RED));
assertTrue (!b.choice ( ).equals (new Connector (1, 2)));
assertEquals (b.colorOf (b.choice ( )), Color.WHITE);
}
@Test
public void testIteratorsConcat()
{
List<Connector> l1 = new ArrayList<Connector>();
List<Connector> l2 = new ArrayList<Connector>();
List<Connector> l3 = new ArrayList<Connector>();
l1.add(new Connector(1, 2));
l1.add(new Connector(2, 3));
l1.add(new Connector(3, 4));
l1.add(new Connector(4, 5));
l3.add(new Connector(5, 6));
Board.IteratorOfIterators it = new Board.IteratorOfIterators(l1.iterator(), l2.iterator(), l3.iterator());
int count = 0;
while (it.hasNext())
{
it.next();
count++;
}
assertEquals(5, count);
}
@Test
public void testIsOK()
{
// Basic empty board test
Board b = new Board();
assertTrue(b.isOK());
// Having more blue than red is impossible
b = new Board();
b.add(new Connector(1, 4), Color.BLUE);
b.add(new Connector(2, 4), Color.BLUE);
b.add(new Connector(3, 4), Color.BLUE);
b.add(new Connector(4, 5), Color.RED);
assertFalse(b.isOK());
// You can only have 1 more red than blue
b = new Board();
b.add(new Connector(1, 4), Color.RED);
b.add(new Connector(2, 4), Color.RED);
b.add(new Connector(3, 4), Color.RED);
b.add(new Connector(4, 5), Color.BLUE);
assertFalse(b.isOK());
// Should work
b = new Board();
b.add(new Connector(1, 2), Color.RED);
b.add(new Connector(2, 3), Color.RED);
b.add(new Connector(3, 1), Color.RED);
b.add(new Connector(3, 5), Color.BLUE);
b.add(new Connector(3, 4), Color.BLUE);
assertTrue(b.isOK());
// Test a blue triangle
b = new Board();
b.add(new Connector(1, 2), Color.BLUE);
b.add(new Connector(2, 3), Color.BLUE);
b.add(new Connector(3, 1), Color.BLUE);
b.add(new Connector(3, 5), Color.RED);
b.add(new Connector(3, 4), Color.RED);
assertFalse(b.isOK());
// Test a normal board
b = new Board();
b.add(new Connector(1, 2), Color.BLUE);
b.add(new Connector(2, 3), Color.BLUE);
b.add(new Connector(3, 5), Color.BLUE);
b.add(new Connector(3, 6), Color.RED);
b.add(new Connector(3, 4), Color.RED);
b.add(new Connector(5, 6), Color.RED);
b.add(new Connector(5, 1), Color.RED);
assertTrue(b.isOK());
}
// More tests go here.
// (a useful helper method)
// Make the following checks on a board that should be legal:
// Check connector counts (# reds + # blues + # uncolored should be 15.
// Check red vs. blue counts.
// Check for duplicate connectors.
// Check for a blue triangle, which shouldn't exist.
private void checkCollection (Board b, int redCount, int blueCount) {
assertEquals(true, b.isOK());
int redBoardCount = iteratorLength(b.connectors(Color.RED));
int blueBoardCount = iteratorLength(b.connectors(Color.BLUE));
int whiteBoardCount = iteratorLength(b.connectors(Color.WHITE));
assertEquals(15, redBoardCount + blueBoardCount + whiteBoardCount);
HashSet<Connector> set = new HashSet<Connector>();
for (Iterator<Connector> it = b.connectors(); it.hasNext();)
{
Connector c = it.next();
assertEquals(false, set.contains(c));
set.add(c);
}
}
private int iteratorLength(Iterator<Connector> it) {
int count = 0;
while (it.hasNext())
{
it.next();
count++;
}
return count;
}
}