Skip to content

Instantly share code, notes, and snippets.

@chischaschos
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chischaschos/9055568 to your computer and use it in GitHub Desktop.
Save chischaschos/9055568 to your computer and use it in GitHub Desktop.
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Before;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertArrayEquals;
/**
* Tests for {@link Percolation}.
*
* @author larin.s931@gmail.com (chischachos)
*/
@RunWith(JUnit4.class)
public class PercolationTest {
@Rule
public ExpectedException noException = ExpectedException.none();
private Percolation percolation;
@Before
public void setup() {
percolation = new Percolation(5);
}
@Test
public void isOpenWithinLimits() {
for(int x = 1; x <= 5; x++) {
for(int y = 1; y <= 5; y++) {
assertFalse(percolation.isOpen(y, x));
}
}
}
@Test(expected = IndexOutOfBoundsException.class)
public void isOpenOutOfLimits1() {
percolation.isOpen(0, 0);
}
@Test(expected = IndexOutOfBoundsException.class)
public void isOpenOutOfLimits2() {
percolation.isOpen(1, 0);
}
@Test(expected = IndexOutOfBoundsException.class)
public void isOpenOutOfLimits3() {
percolation.isOpen(6, 6);
}
@Test(expected = IndexOutOfBoundsException.class)
public void isOpenOutOfLimits4() {
percolation.isOpen(1, 6);
}
@Test
public void openWithinLimits() {
percolation.open(1, 1);
assertTrue(percolation.isOpen(1, 1));
percolation.open(2, 2);
assertTrue(percolation.isOpen(2, 2));
}
@Test(expected = IndexOutOfBoundsException.class)
public void openOutOfLimits1() {
percolation.open(0, 0);
}
@Test(expected = IndexOutOfBoundsException.class)
public void openOutOfLimits2() {
percolation.open(1, 0);
}
@Test(expected = IndexOutOfBoundsException.class)
public void openOutOfLimits3() {
percolation.open(6, 6);
}
@Test(expected = IndexOutOfBoundsException.class)
public void openOutOfLimits4() {
percolation.open(1, 6);
}
@Test
public void isFullWithinLimits() {
for(int x = 1; x <= 5; x++) {
for(int y = 1; y <= 5; y++) {
assertFalse(percolation.isFull(y, x));
}
}
}
@Test(expected = IndexOutOfBoundsException.class)
public void isFullOutOfLimits1() {
percolation.isFull(0, 0);
}
@Test(expected = IndexOutOfBoundsException.class)
public void isFullOutOfLimits2() {
percolation.isFull(1, 0);
}
@Test(expected = IndexOutOfBoundsException.class)
public void isFullOutOfLimits3() {
percolation.isFull(6, 6);
}
@Test(expected = IndexOutOfBoundsException.class)
public void isFullOutOfLimits4() {
percolation.isFull(1, 6);
}
@Test
public void openFullSite() {
percolation.open(1, 1);
assertTrue(percolation.isFull(1, 1));
percolation.open(2, 1);
assertTrue(percolation.isFull(2, 1));
percolation.open(3, 2);
assertFalse(percolation.isFull(3, 2));
}
@Test
public void test1() {
assertFalse(percolation.percolates());
percolation.open(1, 1);
assertFalse(percolation.percolates());
percolation.open(2, 1);
assertFalse(percolation.percolates());
percolation.open(3, 1);
assertFalse(percolation.percolates());
percolation.open(3, 2);
assertFalse(percolation.percolates());
percolation.open(4, 2);
assertFalse(percolation.percolates());
percolation.open(5, 2);
assertTrue(percolation.percolates());
}
@Test public void test2() {
Percolation percolation = new Percolation(6);
percolation.open(1, 6);
assertTrue(percolation.isOpen(1, 6));
assertTrue(percolation.isFull(1, 6));
assertFalse(percolation.percolates());
percolation = new Percolation(8);
percolation.open(1, 3);
assertTrue(percolation.isOpen(1, 3));
assertTrue(percolation.isFull(1, 3));
assertFalse(percolation.percolates());
percolation.open(1, 6);
assertTrue(percolation.isOpen(1, 6));
assertTrue(percolation.isFull(1, 6));
assertFalse(percolation.percolates());
}
//@Test
//@Ignore
//public void translateCoordinate() {
//assertEquals(0, percolation.translateCoordinate(1, 1));
//assertEquals(1, percolation.translateCoordinate(1, 2));
//assertEquals(5, percolation.translateCoordinate(2, 1));
//assertEquals(6, percolation.translateCoordinate(2, 2));
//assertEquals(24, percolation.translateCoordinate(5, 5));
//assertEquals(-1, percolation.translateCoordinate(0, 1));
//assertEquals(-1, percolation.translateCoordinate(1, 0));
//assertEquals(-1, percolation.translateCoordinate(3, 6));
//}
//@Test
//@Ignore
//public void getNeighbours() {
//int neighbours[] = new int[] {-1, 1, 5, -1};
//assertArrayEquals(neighbours, percolation.getNeighbours(1, 1));
//}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment