using Blacklight.Core.Drawables; using System; using System.Drawing; using UnitTestSharp; namespace Blacklight.Core.UnitTests.Drawables { public class SpriteBatchStoreTests : TestFixture { private static SpriteBatch.Entry EntryAt(int x) { return new SpriteBatch.Entry { SourceOrigin = new Point(x, 0) }; } public class CtorTests : TestFixture { public void Parameterless_HasNoOwner() { CheckNull(new SpriteBatch.Store().Owner); } public void TakesTheOwner() { var container = new EntityContainer(); CheckEqual(container, new SpriteBatch.Store(owner: container).Owner); } } public class CreateTests : TestFixture { public void HandsOutAnEmptyBatchOfTheGivenCapacity() { SpriteBatch batch = new SpriteBatch.Store().Create(capacity: 6); CheckEqual(0, batch.Count); CheckEqual(6, batch.Capacity); } public void BatchesKeepTheirOwnEntries() { var store = new SpriteBatch.Store(); SpriteBatch first = store.Create(capacity: 2); SpriteBatch second = store.Create(capacity: 2); first.Add(EntryAt(1)); first.Add(EntryAt(2)); second.Add(EntryAt(3)); second.Add(EntryAt(4)); CheckEqual(1, first.Entries[0].SourceOrigin.X); CheckEqual(2, first.Entries[1].SourceOrigin.X); CheckEqual(3, second.Entries[0].SourceOrigin.X); CheckEqual(4, second.Entries[1].SourceOrigin.X); } public void ManyBatches_KeepTheirOwnSettings() { var store = new SpriteBatch.Store(); var batches = new SpriteBatch[10]; for (int i = 0; i < batches.Length; ++i) { batches[i] = store.Create(capacity: 0); batches[i].CoverageGamma = i + 1; } for (int i = 0; i < batches.Length; ++i) { CheckEqual(i + 1, batches[i].CoverageGamma); } } public void NegativeCapacity_Throws() { var store = new SpriteBatch.Store(); CheckThrow(typeof(ArgumentOutOfRangeException)); store.Create(capacity: -1); } } public class HeaderOfTests : TestFixture { public void BatchFromAnotherStore_Throws() { SpriteBatch batch = new SpriteBatch.Store().Create(capacity: 0); var otherStore = new SpriteBatch.Store(); otherStore.Create(capacity: 0); CheckThrow(typeof(ArgumentException)); otherStore.HeaderOf(batch); } } public class AddEntryToTests : TestFixture { public void NewestBatch_GrowsWithoutDisturbingOlderBatches() { var store = new SpriteBatch.Store(); SpriteBatch older = store.Create(capacity: 2); SpriteBatch newest = store.Create(capacity: 0); older.Add(EntryAt(1)); older.Add(EntryAt(2)); for (int i = 0; i < 100; ++i) { newest.Add(EntryAt(100 + i)); } CheckEqual(1, older.Entries[0].SourceOrigin.X); CheckEqual(2, older.Entries[1].SourceOrigin.X); CheckEqual(100, newest.Count); for (int i = 0; i < 100; ++i) { CheckEqual(100 + i, newest.Entries[i].SourceOrigin.X); } } public void BatchCreatedAfterAGrowth_StartsPastTheGrownEntries() { var store = new SpriteBatch.Store(); SpriteBatch grown = store.Create(capacity: 0); for (int i = 0; i < 5; ++i) { grown.Add(EntryAt(i)); } SpriteBatch later = store.Create(capacity: 8); for (int i = 0; i < 8; ++i) { later.Add(EntryAt(100 + i)); } for (int i = 0; i < 5; ++i) { CheckEqual(i, grown.Entries[i].SourceOrigin.X); } } public void FullOlderBatch_Throws() { var store = new SpriteBatch.Store(); SpriteBatch older = store.Create(capacity: 1); store.Create(capacity: 1); older.Add(EntryAt(1)); CheckThrow(typeof(InvalidOperationException)); older.Add(EntryAt(2)); } } public class ClearTests : TestFixture { public void EarlierBatch_ThrowsWhenRead() { var store = new SpriteBatch.Store(); SpriteBatch earlier = store.Create(capacity: 1); store.Clear(); CheckThrow(typeof(InvalidOperationException)); _ = earlier.Count; } public void EarlierBatch_ThrowsWhenChanged() { var store = new SpriteBatch.Store(); SpriteBatch earlier = store.Create(capacity: 1); store.Clear(); CheckThrow(typeof(InvalidOperationException)); earlier.CoverageGamma = 2; } public void EarlierBatch_ThrowsWhenAddedTo() { var store = new SpriteBatch.Store(); SpriteBatch earlier = store.Create(capacity: 1); store.Clear(); CheckThrow(typeof(InvalidOperationException)); earlier.Add(EntryAt(1)); } public void EarlierBatch_ThrowsWhenItsEntriesAreRead() { var store = new SpriteBatch.Store(); SpriteBatch earlier = store.Create(capacity: 1); store.Clear(); CheckThrow(typeof(InvalidOperationException)); _ = earlier.Entries; } public void BatchInAReusedSlot_StartsFresh() { var store = new SpriteBatch.Store(); SpriteBatch earlier = store.Create(capacity: 4); earlier.CoverageGamma = 3; earlier.Add(EntryAt(1)); store.Clear(); SpriteBatch later = store.Create(capacity: 4); CheckEqual(0, later.Count); CheckEqual(1, later.CoverageGamma); } } public class ClearAllocationTests : TestFixture { private SpriteBatch.Store store; public override void TestSetup() { store = new SpriteBatch.Store(); } [AllocationTest] public void RefillingAfterwards_AllocatesNothing() { store.Clear(); SpriteBatch grown = store.Create(capacity: 4); for (int i = 0; i < 32; ++i) { grown.Add(EntryAt(i)); } store.Create(capacity: 8); CheckNoAllocations(); } } } }