-
Notifications
You must be signed in to change notification settings - Fork 2
/
PercolationTest.java
139 lines (112 loc) · 3.61 KB
/
PercolationTest.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
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
/**
* Created by Mike Milonakis on 11/10/2016.
*/
public class PercolationTest {
private Percolation percolation;
@Before
public void setUp() throws Exception {
percolation = new Percolation(5);
}
@Test(expected = IllegalArgumentException.class)
public void Create_NegativeGridValue_ExceptionThrown() {
new Percolation(-5);
}
@Test
public void Create_Grid_AllSitesBlocked() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; i <= 5; i++) {
Assert.assertFalse(percolation.isOpen(i, j));
}
}
}
@Test(expected = IndexOutOfBoundsException.class)
public void IsOpen_IllegalValue_ExceptionThrown() {
percolation.isOpen(0, 0);
}
@Test
public void Open_regularSite_BecomesOpen() {
percolation.open(1,1);
Assert.assertTrue(percolation.isOpen(1,1));
}
@Test(expected = IndexOutOfBoundsException.class)
public void IsFull_IllegalValue_Exceptionthrown() {
percolation.isFull(6, 6);
}
@Test
public void IsFull_SiteInBottomRowIsOpen_ReturnsTrue() {
percolation.open(1, 5);
Assert.assertTrue(percolation.isFull(1, 5));
Assert.assertFalse(percolation.isFull(1, 4));
}
@Test
public void Percolates_NoBottomSiteFull_ReturnsFalse() {
percolation.open(5, 5);
percolation.open(4, 5);
percolation.open(3, 5);
percolation.open(2, 5);
Assert.assertFalse(percolation.percolates());
Assert.assertFalse(percolation.isFull(1, 5));
}
@Test
public void Percolates_BottomSiteFull_ReturnsTrue() {
percolation.open(5, 5);
percolation.open(4, 5);
percolation.open(3, 5);
percolation.open(2, 5);
percolation.open(1, 5);
Assert.assertTrue(percolation.percolates());
}
@Test
public void Percolates_Input1_ReturnsFalse() {
readInputFromTestFile("input1.txt");
Assert.assertFalse(percolation.percolates());
}
@Test
public void Percolates_Input8_ReturnsTrue() {
readInputFromTestFile("input8.txt");
Assert.assertTrue(percolation.percolates());
}
@Test
public void Percolates_Input50_ReturnsTrue() {
readInputFromTestFile("input50.txt");
Assert.assertTrue(percolation.percolates());
}
@Test
public void Percolates_Input8No_ReturnsFalse() {
readInputFromTestFile("input8-no.txt");
Assert.assertFalse(percolation.percolates());
}
@Test
public void Percolates_Input10No_ReturnsFalse() {
readInputFromTestFile("input10-no.txt");
Assert.assertFalse(percolation.percolates());
}
private void readInputFromTestFile(String testFile) {
try (Stream<String> stream = Files.lines(Paths.get(getClass().getResource
(testFile).getPath()))) {
stream.forEach(this::performAction);
}
catch (IOException e) {
e.printStackTrace();
}
}
private void performAction(String s) {
String[] numbers = s.trim().split("\\s+");
if (numbers.length > 0) {
if (numbers.length == 1) {
percolation = new Percolation(Integer.parseInt(numbers[0]));
}
else if (numbers.length == 2) {
percolation.open(Integer.parseInt(numbers[0]),
Integer.parseInt(numbers[1]));
}
}
}
}