using System; using UnitTestSharp; namespace Konis.UnitTests { public class ParticleStoreTests : TestFixture { private static Particle Aged(float age) { return new Particle { Age = age }; } public class CtorTests : TestFixture { public void StartsEmptyAtTheCapacityGiven() { var store = new ParticleStore(capacity: 5, columnCount: 0); CheckEqual(0, store.Count); CheckEqual(5, store.Capacity); CheckEqual(5, store.Items.Length); } public void MakesEachColumnAsLongAsTheCapacity() { var store = new ParticleStore(capacity: 5, columnCount: 2); CheckEqual(2, store.Columns.Length); CheckEqual(5, store.Columns[0].Length); CheckEqual(5, store.Columns[1].Length); } public void ACapacityOfZero_IsAllowed() { CheckEqual(0, new ParticleStore(capacity: 0, columnCount: 1).Capacity); } public void ANegativeCapacity_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new ParticleStore(capacity: -1, columnCount: 0); } public void ANegativeColumnCount_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new ParticleStore(capacity: 1, columnCount: -1); } } public class AddTests : TestFixture { public void ReturnsTheNextIndexAndKeepsTheParticle() { var store = new ParticleStore(capacity: 3, columnCount: 0); CheckEqual(0, store.Add(Aged(1))); CheckEqual(1, store.Add(Aged(2))); CheckEqual(2, store.Count); CheckEqual(2f, store.Items[1].Age); } public void ToAFullStore_ReturnsMinusOneAndChangesNothing() { var store = new ParticleStore(capacity: 1, columnCount: 0); store.Add(Aged(1)); CheckEqual(-1, store.Add(Aged(2))); CheckEqual(1, store.Count); CheckEqual(1f, store.Items[0].Age); } public void ToAStoreOfZeroCapacity_ReturnsMinusOne() { CheckEqual(-1, new ParticleStore(capacity: 0, columnCount: 0).Add(Aged(1))); } public void ZeroesTheColumnEntriesOfTheNewParticle() { var store = new ParticleStore(capacity: 2, columnCount: 1); store.Add(Aged(1)); store.Add(Aged(2)); store.Columns[0][1] = 7; store.RemoveAt(1); CheckEqual(1, store.Add(Aged(3))); CheckEqual(0f, store.Columns[0][1]); } public void LeavesTheColumnEntriesOfOtherParticlesAlone() { var store = new ParticleStore(capacity: 2, columnCount: 1); store.Add(Aged(1)); store.Columns[0][0] = 4; store.Add(Aged(2)); CheckEqual(4f, store.Columns[0][0]); } } public class RemoveAtTests : TestFixture { public void FromTheMiddle_MovesTheLastParticleIntoItsPlace() { var store = new ParticleStore(capacity: 4, columnCount: 0); store.Add(Aged(1)); store.Add(Aged(2)); store.Add(Aged(3)); store.RemoveAt(0); CheckEqual(2, store.Count); CheckEqual(3f, store.Items[0].Age); CheckEqual(2f, store.Items[1].Age); } public void OfTheLastParticle_JustDropsIt() { var store = new ParticleStore(capacity: 4, columnCount: 0); store.Add(Aged(1)); store.Add(Aged(2)); store.RemoveAt(1); CheckEqual(1, store.Count); CheckEqual(1f, store.Items[0].Age); } public void OfTheOnlyParticle_LeavesTheStoreEmpty() { var store = new ParticleStore(capacity: 1, columnCount: 0); store.Add(Aged(1)); store.RemoveAt(0); CheckEqual(0, store.Count); } public void MovesTheLastParticlesColumnEntriesToo() { var store = new ParticleStore(capacity: 4, columnCount: 2); store.Add(Aged(1)); store.Add(Aged(2)); store.Add(Aged(3)); store.Columns[0][2] = 30; store.Columns[1][2] = 31; store.RemoveAt(0); CheckEqual(30f, store.Columns[0][0]); CheckEqual(31f, store.Columns[1][0]); } public void OfTheLastParticle_LeavesTheOthersColumnEntriesAlone() { var store = new ParticleStore(capacity: 4, columnCount: 1); store.Add(Aged(1)); store.Add(Aged(2)); store.Columns[0][0] = 10; store.RemoveAt(1); CheckEqual(10f, store.Columns[0][0]); } public void ANegativeIndex_Throws() { var store = new ParticleStore(capacity: 2, columnCount: 0); store.Add(Aged(1)); CheckThrow(typeof(ArgumentOutOfRangeException)); store.RemoveAt(-1); } public void AnIndexAtTheCount_Throws() { var store = new ParticleStore(capacity: 2, columnCount: 0); store.Add(Aged(1)); CheckThrow(typeof(ArgumentOutOfRangeException)); store.RemoveAt(1); } public void FromAnEmptyStore_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new ParticleStore(capacity: 2, columnCount: 0).RemoveAt(0); } } } }