Implementierung Tests keepAliveWithTwoNeighbours() (fails) + keepAliveWithThreeNeighbours() (passes)

This commit is contained in:
Susanne 2025-02-16 16:30:31 +01:00
parent 2763a28619
commit ee5469dbe3
4 changed files with 26 additions and 11 deletions

View File

@ -54,9 +54,7 @@ public class Life implements ILife {
for (int x = 0; x < grid[y].length; x++) { for (int x = 0; x < grid[y].length; x++) {
if (countAliveNeighbours(x, y) == 3) { if (countAliveNeighbours(x, y) == 3) {
next.setAlive(x, y); next.setAlive(x, y);
} } else if (countAliveNeighbours(x, y) < 2 || countAliveNeighbours(x, y) > 3) {
// Refactoring: vermeide zwei fast identische Codeblöcke, also Dopplung, mittels zusammenziehen der if's
else if (countAliveNeighbours(x, y) < 2 || countAliveNeighbours(x, y) > 3) {
next.setDead(x, y); next.setDead(x, y);
} }
} }

View File

@ -37,9 +37,32 @@ public class LifeTest {
} }
// Testfall "keepAliveCell()" aufgeteilt in zwei einzelne Tests, da es ja eigentlich üblich ist, dass je Test
// nur ein einzelnes Szenario überprüft wird
@Test @Test
public void keepAliveCell() { public void keepAliveWithTwoNeighbours() {
// Zellen mit zwei oder drei Nachbarn bleiben am Leben. // failed, weil bislang nur Zellen mit genau 3 Nachbarn in nextGen auf alive gesetzt werden,
// andernfalls standardmäßig mit false, also tot, initialisiert
Life l = new Life();
l.setAlive(1, 1);
l.setAlive(0, 0);
l.setAlive(0, 1);
ILife nextGen = l.nextGeneration();
assertTrue(nextGen.isAlive(1, 1));
}
@Test
public void keepAliveWithThreeNeighbours() {
// passed, weil Zellen mit genau 3 Nachbarn bereits in nextGen auf alive gesetzt werden
Life l = new Life();
l.setAlive(1, 1);
l.setAlive(0, 0);
l.setAlive(0, 1);
l.setAlive(0, 2);
ILife nextGen = l.nextGeneration();
assertTrue(nextGen.isAlive(1, 1));
} }
@ -60,11 +83,5 @@ public class LifeTest {
// Assert: Zelle (1,1) sollte tot sein // Assert: Zelle (1,1) sollte tot sein
assertFalse(nextGen.isAlive(1, 1)); assertFalse(nextGen.isAlive(1, 1));
// Test passed wieder sofort, obwohl ich noch keine weitere Überprüfung eingebaut habe, aus zuvor beschriebenem
// Grund, dass Zellen in der nextGen grundsätzlich mit false initialisiert werden, solange man sie nicht
// explizit mit setAlive() auf true setzt.
} }
} }