Created
August 12, 2019 23:43
-
-
Save azakharov3/0f1c62636345465a5e965cfaa17eab48 to your computer and use it in GitHub Desktop.
PercolationTest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import static junit.framework.TestCase.assertEquals; | |
import static junit.framework.TestCase.assertFalse; | |
import static junit.framework.TestCase.assertNotNull; | |
import static junit.framework.TestCase.assertTrue; | |
public class PercolationTest { | |
@Test | |
public void isOpen_initialFalse() { | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Assert | |
for (int i = 1; i <= n; i++) { | |
for (int j = 1; j <= n; j++) { | |
assertFalse(percolation.isOpen(i, j)); | |
} | |
} | |
} | |
@Test | |
public void isOpen_RowToLarge_ThrowsOnOutOfBoundaryRow() { | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
IllegalArgumentException exception = null; | |
try { | |
percolation.isOpen(4, 1); | |
} catch (IllegalArgumentException e) { | |
exception = e; | |
} | |
// Assert | |
assertNotNull(exception); | |
} | |
@Test | |
public void isOpen_ColumnToLarge_ThrowsOnOutOfBoundaryColumn() { | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
IllegalArgumentException exception = null; | |
try { | |
percolation.isOpen(1, 4); | |
} catch (IllegalArgumentException e) { | |
exception = e; | |
} | |
// Assert | |
assertNotNull(exception); | |
} | |
@Test | |
public void open() { | |
// Arrange | |
Percolation percolation = new Percolation(3); | |
// Act | |
percolation.open(1, 1); | |
// Assert | |
assertTrue(percolation.isOpen(1, 1)); | |
} | |
@Test | |
public void open_rowTooSmall_throwsOnOutOfBoundaryRow() { | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
IllegalArgumentException exception = null; | |
try { | |
percolation.open(-1, 1); | |
} catch (IllegalArgumentException e) { | |
exception = e; | |
} | |
// Assert | |
assertNotNull(exception); | |
} | |
@Test | |
public void isFull_initialFalse() { | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Assert | |
for (int i = 1; i <= n; i++) { | |
for (int j = 1; j <= n; j++) { | |
assertFalse(percolation.isFull(i, j)); | |
} | |
} | |
} | |
@Test | |
public void isFull_rowTooLarge_throwsOnOutOfBoundaryRow() { | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
IllegalArgumentException exception = null; | |
try { | |
percolation.isFull(4, 1); | |
} catch (IllegalArgumentException e) { | |
exception = e; | |
} | |
// Assert | |
assertNotNull(exception); | |
} | |
@Test | |
public void isFull_ColumnToLarge_ThrowsOnOutOfBoundaryColumn() { | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
IllegalArgumentException exception = null; | |
try { | |
percolation.isFull(1, 4); | |
} catch (IllegalArgumentException e) { | |
exception = e; | |
} | |
// Assert | |
assertNotNull(exception); | |
} | |
@Test | |
public void isFull_falseOnIsolatedSite() { | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(2, 2); | |
// Assert | |
assertFalse(percolation.isFull(2, 2)); | |
} | |
/** | |
* [1][ ][ ] | |
* [ ][ ][ ] | |
* [ ][ ][ ] | |
*/ | |
@Test | |
public void isFull_trueScenario01(){ | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(1, 1); | |
// Assert | |
assertTrue(percolation.isFull(1, 1)); | |
} | |
/** | |
* [ ][1][ ] | |
* [ ][ ][ ] | |
* [ ][ ][ ] | |
*/ | |
@Test | |
public void isFull_trueScenario02(){ | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(1, 2); | |
// Assert | |
assertTrue(percolation.isFull(1, 2)); | |
} | |
/** | |
* [ ][ ][1] | |
* [ ][ ][ ] | |
* [ ][ ][ ] | |
*/ | |
@Test | |
public void isFull_trueScenario03(){ | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(1, 3); | |
// Assert | |
assertTrue(percolation.isFull(1, 3)); | |
} | |
/** | |
* [ ][ ][1] | |
* [ ][ ][2] | |
* [ ][ ][ ] | |
*/ | |
@Test | |
public void isFull_trueScenario04(){ | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(1, 3); | |
percolation.open(2, 3); | |
// Assert | |
assertTrue(percolation.isFull(1, 3)); | |
assertTrue(percolation.isFull(2, 3)); | |
} | |
/** | |
* [ ][ ][2] | |
* [ ][ ][1] | |
* [ ][ ][ ] | |
*/ | |
@Test | |
public void isFull_trueScenario05(){ | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(2, 3); | |
percolation.open(1, 3); | |
// Assert | |
assertTrue(percolation.isFull(2, 3)); | |
assertTrue(percolation.isFull(1, 3)); | |
} | |
/** | |
* [ ][ ][2] | |
* [ ][ ][1] | |
* [ ][ ][3] | |
*/ | |
@Test | |
public void isFull_trueScenario06(){ | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(2, 3); // 1 | |
percolation.open(1, 3); // 2 | |
percolation.open(3, 3); // 3 | |
// Assert | |
assertTrue(percolation.isFull(2, 3)); | |
assertTrue(percolation.isFull(1, 3)); | |
assertTrue(percolation.isFull(3, 3)); | |
} | |
/** | |
* [ ][ ][2] | |
* [ ][3][1] | |
* [ ][ ][ ] | |
*/ | |
@Test | |
public void isFull_trueScenario07(){ | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(2, 3); // 1 | |
percolation.open(1, 3); // 2 | |
percolation.open(2, 2); // 3 | |
// Assert | |
assertTrue(percolation.isFull(2, 3)); | |
assertTrue(percolation.isFull(1, 3)); | |
assertTrue(percolation.isFull(2, 2)); | |
} | |
/** | |
* [ ][ ][2] | |
* [4][3][1] | |
* [ ][ ][ ] | |
*/ | |
@Test | |
public void isFull_trueScenario08(){ | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(2, 3); // 1 | |
percolation.open(1, 3); // 2 | |
percolation.open(2, 2); // 3 | |
percolation.open(2, 1); // 4 | |
// Assert | |
assertTrue(percolation.isFull(2, 3)); | |
assertTrue(percolation.isFull(1, 3)); | |
assertTrue(percolation.isFull(2, 2)); | |
assertTrue(percolation.isFull(2, 1)); | |
} | |
/** | |
* [ ][ ][2] | |
* [3][4][1] | |
* [ ][ ][ ] | |
*/ | |
@Test | |
public void isFull_trueScenario09(){ | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(2, 3); // 1 | |
percolation.open(1, 3); // 2 | |
percolation.open(2, 1); // 3 | |
percolation.open(2, 2); // 4 | |
// Assert | |
assertTrue(percolation.isFull(2, 3)); | |
assertTrue(percolation.isFull(1, 3)); | |
assertTrue(percolation.isFull(2, 1)); | |
assertTrue(percolation.isFull(2, 2)); | |
} | |
/** | |
* [ ][ ][2] | |
* [3][4][1] | |
* [5][ ][ ] | |
*/ | |
@Test | |
public void isFull_trueScenario10(){ | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(2, 3); // 1 | |
percolation.open(1, 3); // 2 | |
percolation.open(2, 1); // 3 | |
percolation.open(2, 2); // 4 | |
percolation.open(3, 1); // 5 | |
// Assert | |
assertTrue(percolation.isFull(2, 3)); | |
assertTrue(percolation.isFull(1, 3)); | |
assertTrue(percolation.isFull(2, 1)); | |
assertTrue(percolation.isFull(2, 2)); | |
assertTrue(percolation.isFull(3, 1)); | |
} | |
/** | |
* [ ][2][ ] | |
* [ ][1][ ] | |
* [ ][ ][ ] | |
*/ | |
@Test | |
public void isFull_trueScenario11(){ | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(2, 2); // 2 | |
percolation.open(1, 2); // 3 | |
// Assert | |
assertTrue(percolation.isFull(2, 2)); | |
assertTrue(percolation.isFull(1, 2)); | |
} | |
/** | |
* [ ][3][ ] | |
* [ ][2][ ] | |
* [ ][1][ ] | |
*/ | |
@Test | |
public void isFull_trueScenario12(){ | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(3, 2); // 1 | |
percolation.open(2, 2); // 2 | |
percolation.open(1, 2); // 3 | |
// Assert | |
assertTrue(percolation.isFull(3, 2)); | |
assertTrue(percolation.isFull(2, 2)); | |
assertTrue(percolation.isFull(1, 2)); | |
} | |
/** | |
* [1][ ][ ] | |
* [2][ ][ ] | |
* [3][ ][3] | |
*/ | |
@Test | |
public void isFull_backWash() { | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(1, 1); // 1 | |
percolation.open(2, 1); // 2 | |
percolation.open(3, 1); // 3 | |
percolation.open(3, 3); // 4 | |
// Assert | |
assertTrue(percolation.isFull(1, 1)); | |
assertTrue(percolation.isFull(2, 1)); | |
assertTrue(percolation.isFull(3, 1)); | |
assertFalse(percolation.isFull(3, 3)); | |
} | |
@Test | |
public void numberOfOpenSites_Initially_Zero() { | |
// Arrange | |
int n = 3; | |
// Act | |
Percolation percolation = new Percolation(n); | |
// Assert | |
assertEquals(0, percolation.numberOfOpenSites()); | |
} | |
@Test | |
public void numberOfOpenSites_afterOpeningFourSites_IsFour(){ | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(1,1); | |
percolation.open(1,3); | |
percolation.open(3,1); | |
percolation.open(3,3); | |
// Assert | |
assertEquals(4, percolation.numberOfOpenSites()); | |
} | |
@Test | |
public void numberOfOpenSites_openSameSiteTwice(){ | |
// Arrange | |
int n = 3; | |
Percolation percolation = new Percolation(n); | |
// Act | |
percolation.open(1,1); | |
percolation.open(1,1); | |
// Assert | |
assertEquals(1, percolation.numberOfOpenSites()); | |
} | |
@Test | |
public void percolates_initially_false(){ | |
// Act | |
Percolation percolation = new Percolation(3); | |
// Assert | |
assertFalse(percolation.percolates()); | |
} | |
/** | |
* [1][ ][ ] | |
* [2][ ][ ] | |
* [ ][ ][ ] | |
*/ | |
@Test | |
public void percolates_notReachingToTheButoom1_False(){ | |
// Arrange | |
Percolation percolation = new Percolation(3); | |
// Act | |
percolation.open(1, 1); | |
percolation.open(1, 2); | |
// Assert | |
assertFalse(percolation.percolates()); | |
} | |
/** | |
* [1][ ][ ][ ] | |
* [2][ ][ ][ ] | |
* [3][ ][ ][4] | |
* [ ][ ][ ][5] | |
*/ | |
@Test | |
public void percolates_notReachingToTheButoom2_False(){ | |
// Arrange | |
Percolation percolation = new Percolation(4); | |
// Act | |
percolation.open(1, 1); | |
percolation.open(2, 1); | |
percolation.open(3, 1); | |
percolation.open(3, 2); | |
percolation.open(3, 4); | |
percolation.open(4, 4); | |
// Assert | |
assertFalse(percolation.percolates()); | |
} | |
/** | |
* [3][ ][ ] | |
* [1][ ][ ] | |
* [2][ ][ ] | |
*/ | |
@Test | |
public void percolates_reachingToTheButoom_True(){ | |
// Arrange | |
Percolation percolation = new Percolation(3); | |
// Act | |
percolation.open(2, 1); | |
percolation.open(3, 1); | |
percolation.open(1, 1); | |
// Assert | |
assertTrue(percolation.percolates()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment