//
// CellGridTests.cs
//
// -------------------------------------------------------------------
// History:
// 2008-02-25 Kwak Original File
// -------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using GameOfLife.Core;
namespace GameOfLife.UnitTests
{
[TestFixture]
public class CellGridTests
{
/// <summary>
/// Constructors should specify the width and height of the grid.
/// </summary>
[Test]
public void ConstructorShouldSpecifyTheWidthAndHeightOfTheGrid()
{
PetriDish grid = new PetriDish(5, 10);
Assert.AreEqual(10, grid.Height, "wrong height");
Assert.AreEqual(5, grid.Width, "wrong width");
}
/// <summary>
/// Singles the cell grid should have no neighbors.
/// </summary>
[Test]
public void SingleCellGridShouldHaveNoNeighbors()
{
PetriDish grid = new PetriDish(1, 1);
Cell theCell = grid[0, 0];
Assert.AreEqual(0, theCell.Position, "wrong position");
Assert.IsNotNull(theCell.Neighbors, "null neighbors");
Assert.AreEqual(0, theCell.Neighbors.Count, "wrong # of neighbors");
}
/// <summary>
/// Centers the cell in grid should have eight neighbors.
/// </summary>
[Test]
public void CenterCellInGridShouldHaveEightNeighbors()
{
PetriDish grid = new PetriDish(3, 3);
Cell centerCell = grid[1, 1];
Assert.IsNotNull(centerCell, "center cell null");
Assert.AreEqual(8, centerCell.Neighbors.Count, "wrong number of neighbors");
}
/// <summary>
/// Outs the of bounds index should return null cell.
/// </summary>
[Test]
public void OutOfBoundsReturnsNullCell()
{
PetriDish grid = new PetriDish(1, 1);
Cell centerCell = grid[1, 1];
Assert.IsNull(centerCell, "not null");
}
/// <summary>
/// Nexts the generation of cell turns cell on.
/// </summary>
[Test]
public void NextGenerationOfCellTurnsCellOn()
{
/*
* Starting position:
* . o
* o o
*
* Finished:
* o o
* o o
*/
PetriDish grid = new PetriDish(2, 2);
grid[0, 1].GiveLife();
grid[1, 0].GiveLife();
grid[1, 1].GiveLife();
Cell aCell = grid[0, 0];
Assert.AreEqual(0, aCell.Age, "cell already alive?");
grid.NextGeneration();
Assert.AreEqual(1, aCell.Age, "cell not alive?");
}
/// <summary>
/// Cells the with two living neighbors survives.
/// </summary>
[Test]
public void CellWithTwoLivingNeighborsSurvives()
{
/*
* Starting position:
* . o
* o o
*
* Finished:
* o o
* o o
*/
PetriDish grid = new PetriDish(2, 2);
grid[0, 1].GiveLife();
grid[1, 0].GiveLife();
grid[1, 1].GiveLife();
Cell aCell = grid[0, 1];
Assert.IsNotNull(aCell);
Assert.AreEqual(1, aCell.Age, "cell not alive?");
Assert.AreEqual(2, aCell.NumberOfLivingNeighbors, "cell should have 2 living neighbors");
grid.NextGeneration();
Assert.AreEqual(2, aCell.Age, "cell should be 2");
}
[Test]
public void CellWithThreeLivingNeighborsShouldSurvive()
{
/*
* Starting position:
* o o
* o o
*
* Finished:
* o o
* o o
*/
PetriDish grid = new PetriDish(2, 2);
grid[0, 0].GiveLife();
grid[0, 1].GiveLife();
grid[1, 0].GiveLife();
grid[1, 1].GiveLife();
Cell aCell = grid[0, 1];
Assert.IsNotNull(aCell);
Assert.AreEqual(1, aCell.Age, "cell not alive?");
Assert.AreEqual(3, aCell.NumberOfLivingNeighbors, "cell should have 3 living neighbors");
grid.NextGeneration();
Assert.AreEqual(2, aCell.Age, "cell should be 2");
}
[Test]
public void CellsWithNoLivingNeighborsDies()
{
PetriDish grid = new PetriDish(2, 2);
grid[0, 0].GiveLife();
Cell aCell = grid[0, 0];
Assert.IsNotNull(aCell);
Assert.AreEqual(1, aCell.Age, "cell not alive?");
Assert.AreEqual(0, aCell.NumberOfLivingNeighbors, "living neighbors");
grid.NextGeneration();
Assert.AreEqual(0, aCell.Age, "the cell should be dead");
}
[Test]
public void CellWithOneLivingNeighborDies()
{
PetriDish grid = new PetriDish(2, 2);
grid[0, 0].GiveLife();
grid[0, 1].GiveLife();
Cell aCell = grid[0, 0];
Assert.IsNotNull(aCell);
Assert.AreEqual(1, aCell.Age, "cell not alive?");
Assert.AreEqual(1, aCell.NumberOfLivingNeighbors, "living neighbors");
grid.NextGeneration();
Assert.AreEqual(0, aCell.Age, "the cell should be dead");
}
[Test]
public void CellWithFourLivingNeighborsDies()
{
/*
* Start:
* o o o
* o o .
*
* Finish:
* o . o
* o . o
*
*/
PetriDish grid = new PetriDish(3, 2);
grid[0, 0].GiveLife();
grid[0, 1].GiveLife();
grid[0, 2].GiveLife();
grid[1, 0].GiveLife();
grid[1, 1].GiveLife();
Cell aCell = grid[0, 1];
Assert.AreEqual(1, aCell.Age, "cell should be 1");
Assert.AreEqual(4, aCell.NumberOfLivingNeighbors, "should have 4 living neighbors");
grid.NextGeneration();
Assert.AreEqual(0, aCell.Age, "cell should have died");
Assert.AreEqual(4, aCell.NumberOfLivingNeighbors, "cell should still have 4 living neighbors");
}
[Test]
public void CellSpawnsAtAgeLimit()
{
/* Start:
o o .
o 0 .
. . .
*
. . o
. . o
o o o
*/
PetriDish grid = new PetriDish(3, 3);
grid[0, 0].GiveLife();
grid[0, 1].GiveLife();
grid[1, 0].GiveLife();
grid[1, 1].GiveLife();
grid.AgeLimit = 3;
grid.NextGeneration();
grid.NextGeneration();
grid.NextGeneration();
Assert.AreEqual(0, grid[1,1].Age, "old cell still alive");
Assert.AreEqual(0, grid[0, 0].Age, "0,0 cell not 0");
Assert.AreEqual(1, grid[2, 0].Age, "2,0 cell not 1");
Assert.AreEqual(1, grid[2, 2].Age, "2,2 cell not 1");
}
[Test]
public void NumberOfLivingCellsReturnsNumberOfCellsAgeGreaterThanZero()
{
PetriDish grid = new PetriDish(1, 2);
grid[0, 0].GiveLife();
Assert.AreEqual(1, grid.NumberOfLivingCells, "not 1");
}
[Test]
public void ResetPicksNewGrid()
{
PetriDish grid = new PetriDish(10, 10);
Assert.AreEqual(0, grid.NumberOfLivingCells, "construction does not make cells live");
grid.Reset();
Assert.AreNotEqual(0, grid.NumberOfLivingCells, "resetting the grid should make some cells live");
}
}
}