141 lines
4.1 KiB
C#
141 lines
4.1 KiB
C#
using NUnit.Framework;
|
|
namespace InverseOfLife.Test;
|
|
|
|
[TestFixture]
|
|
public class GeneTests
|
|
{
|
|
[Test]
|
|
public void Gene_Constructor_FromBoard_CorrectEncoding()
|
|
{
|
|
int resolution = 4;
|
|
var board = new Board(8, 8);
|
|
board.Toggle(1, 1);
|
|
board.Toggle(5, 5);
|
|
|
|
var gene = new Gene(resolution, board);
|
|
|
|
int encodedBits = gene.RiboseSequence.Sum(b => CountSetBits(b));
|
|
Assert.That(encodedBits, Is.EqualTo(2), "Encoded bits should match the number of live cells.");
|
|
}
|
|
|
|
[Test]
|
|
public void Gene_Constructor_FromSequence_CorrectRestoration()
|
|
{
|
|
int resolution = 4;
|
|
var expectedBoard = new Board(8, 8);
|
|
expectedBoard.Toggle(1, 1);
|
|
expectedBoard.Toggle(5, 5);
|
|
var gene = new Gene(resolution, expectedBoard);
|
|
|
|
var restoredBoard = gene.Restore(8,8);
|
|
|
|
Assert.That(restoredBoard.Lives, Is.EquivalentTo(expectedBoard.Lives), "Restored board should match the original board.");
|
|
}
|
|
|
|
[Test]
|
|
public void Gene_Constructor_EmptyBoard_RestoreEmptyBoard()
|
|
{
|
|
int resolution = 4;
|
|
var board = new Board(8, 8);
|
|
|
|
var gene = new Gene(resolution, board);
|
|
var restoredBoard = gene.Restore(8,8);
|
|
|
|
Assert.IsEmpty(restoredBoard.Lives, "Restored board should be empty for an empty input board.");
|
|
}
|
|
|
|
[Test]
|
|
public void Gene_Restore_LargerBoard()
|
|
{
|
|
int resolution = 4;
|
|
var board = new Board(16, 16);
|
|
board.Toggle(2, 2);
|
|
board.Toggle(10, 10);
|
|
|
|
var gene = new Gene(resolution, board);
|
|
var restoredBoard = gene.Restore(16,16);
|
|
|
|
Assert.IsTrue(restoredBoard.Lives.Contains((2, 2)), "Cell (2, 2) should be alive.");
|
|
Assert.IsTrue(restoredBoard.Lives.Contains((10, 10)), "Cell (10, 10) should be alive.");
|
|
Assert.That(restoredBoard.Lives, Is.EquivalentTo(board.Lives), "Restored board should match the original board.");
|
|
}
|
|
|
|
[Test]
|
|
public void Gene_Restore_BoundaryCells()
|
|
{
|
|
|
|
int resolution = 4;
|
|
var board = new Board(8, 8);
|
|
board.Toggle(0, 0);
|
|
board.Toggle(7, 7);
|
|
|
|
var gene = new Gene(resolution, board);
|
|
var restoredBoard = gene.Restore(8,8);
|
|
|
|
Assert.IsTrue(restoredBoard.Lives.Contains((0, 0)), "Top-left corner cell should be alive.");
|
|
Assert.IsTrue(restoredBoard.Lives.Contains((7, 7)), "Bottom-right corner cell should be alive.");
|
|
Assert.That(restoredBoard.Lives, Is.EquivalentTo(board.Lives), "Restored board should match the original board.");
|
|
}
|
|
|
|
[Test]
|
|
public void Gene_Restore_NonMultipleWidth()
|
|
{
|
|
int resolution = 4;
|
|
int width = 10;
|
|
int height = 8;
|
|
var board = new Board(width, height);
|
|
board.Toggle(9, 1);
|
|
board.Toggle(1, 7);
|
|
|
|
var gene = new Gene(resolution, board);
|
|
var restoredBoard = gene.Restore(width, height);
|
|
|
|
Assert.IsTrue(restoredBoard.Lives.Contains((9, 1)));
|
|
Assert.IsTrue(restoredBoard.Lives.Contains((1, 7)));
|
|
}
|
|
|
|
[Test]
|
|
public void Gene_Restore_NonMultipleHeight()
|
|
{
|
|
int resolution = 4;
|
|
int width = 8;
|
|
int height = 9;
|
|
var board = new Board(width, height);
|
|
board.Toggle(7, 8);
|
|
board.Toggle(0, 0);
|
|
|
|
var gene = new Gene(resolution, board);
|
|
var restoredBoard = gene.Restore(width, height);
|
|
|
|
Assert.IsTrue(restoredBoard.Lives.Contains((7, 8)));
|
|
Assert.IsTrue(restoredBoard.Lives.Contains((0, 0)));
|
|
}
|
|
[Test]
|
|
public void Gene_Restore_NonMultipleWidthAndHeight()
|
|
{
|
|
int resolution = 4;
|
|
int width = 10;
|
|
int height = 9;
|
|
var board = new Board(width, height);
|
|
board.Toggle(9, 8);
|
|
board.Toggle(0, 0);
|
|
|
|
var gene = new Gene(resolution, board);
|
|
var restoredBoard = gene.Restore(width, height);
|
|
|
|
Assert.IsTrue(restoredBoard.Lives.Contains((9, 8)));
|
|
Assert.IsTrue(restoredBoard.Lives.Contains((0, 0)));
|
|
}
|
|
|
|
private int CountSetBits(byte b)
|
|
{
|
|
int count = 0;
|
|
while (b > 0)
|
|
{
|
|
count += b & 1;
|
|
b >>= 1;
|
|
}
|
|
return count;
|
|
}
|
|
}
|