using Azimuth; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UnitTestSharp; using System.Drawing; namespace Blacklight.SharpDX9.UnitTesting { public class TextureSlotManagerTests : TestFixture { public class TestTexture : Blacklight.Core.Texture { public TestTexture() : this(new Size()) { } public TestTexture(Size size) { this.Size = size; } } public class AddSlotTests : TestFixture { TestTexture texture1; TestTexture texture2; public override void TestSetup() { texture1 = new TestTexture(new Size(3, 4)); texture2 = new TestTexture(new Size(7, 11)); } public void Empty() { var manager = new TextureSlotManager(); CheckEqual(0, manager.Count); } public void TexturesSet() { var manager = new TextureSlotManager(); manager.AddSlot(texture1, TextureSlotManager.SlotType.FillTexture); manager.AddSlot(texture2, TextureSlotManager.SlotType.FillTexture); CheckEqual(2, manager.Count); } public void Deduplicated() { var manager = new TextureSlotManager(); manager.AddSlot(texture1, TextureSlotManager.SlotType.FillTexture); manager.AddSlot(texture1, TextureSlotManager.SlotType.FillTexture); CheckEqual(1, manager.Count); } public void NullTexture() { var manager = new TextureSlotManager(); CheckThrow(typeof(ArgumentNullException)); manager.AddSlot(null, TextureSlotManager.SlotType.FillTexture); } public void SlotTypeNone() { var manager = new TextureSlotManager(); CheckThrow(typeof(ArgumentException)); manager.AddSlot(texture1, TextureSlotManager.SlotType.None); } public void ReturnsSlotIndex() { var manager = new TextureSlotManager(); CheckEqual(0, manager.AddSlot(texture1, TextureSlotManager.SlotType.FillTexture)); CheckEqual(0, manager.AddSlot(texture1, TextureSlotManager.SlotType.FillTexture)); CheckEqual(1, manager.AddSlot(texture2, TextureSlotManager.SlotType.FillTexture)); } public void FillingWithTexturesReturnsAllSlots() { var manager = new TextureSlotManager(); for (int i = 0; i < TextureSlotManager.MaxTextureCount; ++i) { CheckEqual(i, manager.AddSlot(new TestTexture(), TextureSlotManager.SlotType.FillTexture)); } } public void ReturnsInvalidWhenFull() { var manager = new TextureSlotManager(); for (int i = 0; i < TextureSlotManager.MaxTextureCount; ++i) { manager.AddSlot(new TestTexture(), TextureSlotManager.SlotType.FillTexture); } CheckEqual( TextureSlotManager.InvalidTextureSlot, manager.AddSlot(new TestTexture(), TextureSlotManager.SlotType.FillTexture)); } public void StoresTextureSize() { var manager = new TextureSlotManager(); manager.AddSlot(new TestTexture(new Size(3, 7)), TextureSlotManager.SlotType.FillTexture); var expected = new float[] { 3, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; CheckEqual(TextureSlotManager.MaxTextureCount * 4, expected.Length); CheckEqual(expected, manager.TextureSizes); } public void StoresAllTextureSizesCorrectly() { var manager = new TextureSlotManager(); for (int i = 0; i < TextureSlotManager.MaxTextureCount; ++i) { manager.AddSlot( new TestTexture(new Size(3 * i + 3, 7 * i + 7)), TextureSlotManager.SlotType.FillTexture); } var expected = new float[] { 3, 7, 0, 0, 6, 14, 0, 0, 9, 21, 0, 0, 12, 28, 0, 0, 15, 35, 0, 0, 18, 42, 0, 0, 21, 49, 0, 0, 24, 56, 0, 0, 27, 63, 0, 0, 30, 70, 0, 0, 33, 77, 0, 0, 36, 84, 0, 0, 39, 91, 0, 0, 42, 98, 0, 0, 45, 105, 0, 0, 48, 112, 0, 0, }; CheckEqual(TextureSlotManager.MaxTextureCount * 4, expected.Length); CheckEqual(expected, manager.TextureSizes); } public void StoresCorrectTextureType() { var manager = new TextureSlotManager(); manager.AddSlot(texture1, TextureSlotManager.SlotType.FillTexture); manager.AddSlot(texture2, TextureSlotManager.SlotType.SpriteSheet); CheckEqual(TextureSlotManager.SlotType.FillTexture, manager.GetSlot(0).type); CheckEqual(TextureSlotManager.SlotType.SpriteSheet, manager.GetSlot(1).type); } } public class ClearTests : TestFixture { public void ClearSetsCountToZero() { var manager = new TextureSlotManager(); manager.AddSlot(new TestTexture(), TextureSlotManager.SlotType.FillTexture); manager.AddSlot(new TestTexture(), TextureSlotManager.SlotType.FillTexture); manager.Clear(); CheckEqual(0, manager.Count); } public void ClearZeroesTextureSizes() { var manager = new TextureSlotManager(); manager.AddSlot(new TestTexture(new Size(3, 4)), TextureSlotManager.SlotType.FillTexture); manager.AddSlot(new TestTexture(new Size(7, 11)), TextureSlotManager.SlotType.FillTexture); manager.Clear(); var expected = new float[4 * TextureSlotManager.MaxTextureCount]; CheckEqual(expected, manager.TextureSizes); } } public class GetSlotTests : TestFixture { public void CannotReadPastEnd() { var manager = new TextureSlotManager(); CheckThrow(typeof(ArgumentOutOfRangeException)); manager.GetSlot(0); } public void CanReadInRange() { var texture = new TestTexture(); var manager = new TextureSlotManager(); manager.AddSlot(texture, TextureSlotManager.SlotType.SpriteSheet); var slot = manager.GetSlot(0); CheckEqual(texture, slot.texture); CheckEqual(TextureSlotManager.SlotType.SpriteSheet, slot.type); } } public class BeginTransactionTests : TestFixture { public void ChangesAreLive() { var texture = new TestTexture(); var manager = new TextureSlotManager(); using (var transaction = manager.BeginTransaction()) { transaction.AddSlot(texture, TextureSlotManager.SlotType.FillTexture); CheckEqual(1, manager.Count); } } public void ChangesPersist() { var texture = new TestTexture(); var manager = new TextureSlotManager(); using (var transaction = manager.BeginTransaction()) { transaction.AddSlot(new TestTexture(new Size(3, 4)), TextureSlotManager.SlotType.FillTexture); } CheckEqual(1, manager.Count); CheckEqual(3, manager.TextureSizes[0]); CheckEqual(4, manager.TextureSizes[1]); } public void AbortsChangesIfFailure() { var manager = new TextureSlotManager(); using (var transaction = manager.BeginTransaction()) { for (int i = 0; i < TextureSlotManager.MaxTextureCount + 1; ++i) { transaction.AddSlot(new TestTexture(new Size(3, 4)), TextureSlotManager.SlotType.FillTexture); } } CheckEqual(0, manager.Count); CheckEqual(0, manager.TextureSizes[0]); CheckEqual(0, manager.TextureSizes[1]); } public void NullIsNotFailure() { var manager = new TextureSlotManager(); using (var transaction = manager.BeginTransaction()) { CheckEqual(TextureSlotManager.InvalidTextureSlot, transaction.AddSlot(null, TextureSlotManager.SlotType.FillTexture)); CheckEqual(0, manager.Count); } CheckEqual(0, manager.Count); } } } }