using System; using UnitTestSharp; namespace Susurrus.XAudio2.UnitTests { public class SourceVoicePoolTests : TestFixture { public static SourceVoicePool PoolOf(TestDevice device, int count) { SourceVoice[] voices = new SourceVoice[count]; for (int i = 0; i < count; ++i) { voices[i] = device.MonoVoice(); } return new SourceVoicePool(channelCount: 1, SampleFormat.Pcm16, voices); } public class CtorTests : TestFixture { public void RecordsItsFormatAndCapacity() { using (TestDevice device = TestDevice.Open()) { if (device == null) { return; } SourceVoicePool pool = PoolOf(device, count: 3); CheckEqual(1, pool.ChannelCount); CheckEqual(SampleFormat.Pcm16, pool.Format); CheckEqual(3, pool.Capacity); pool.Destroy(); } } public void NullVoices_Throws() { CheckThrow(typeof(ArgumentNullException)); new SourceVoicePool(channelCount: 1, SampleFormat.Pcm16, voices: null); } } public class TakeTests : TestFixture { public void HandsOutEachVoiceOnceThenNull() { using (TestDevice device = TestDevice.Open()) { if (device == null) { return; } SourceVoicePool pool = PoolOf(device, count: 2); SourceVoice first = pool.Take(); SourceVoice second = pool.Take(); CheckNotNull(first); CheckNotNull(second); CheckTrue(first != second); CheckNull(pool.Take()); pool.Destroy(); } } } public class ReturnTests : TestFixture { public void MakesTheVoiceAvailableAgain() { using (TestDevice device = TestDevice.Open()) { if (device == null) { return; } SourceVoicePool pool = PoolOf(device, count: 1); SourceVoice taken = pool.Take(); pool.Return(taken); CheckTrue(taken == pool.Take()); pool.Destroy(); } } public void AVoiceThatIsIdle_Throws() { using (TestDevice device = TestDevice.Open()) { if (device == null) { return; } SourceVoicePool pool = PoolOf(device, count: 2); SourceVoice taken = pool.Take(); pool.Return(taken); CheckThrow(typeof(ArgumentException)); pool.Return(taken); } } public void AVoiceFromElsewhere_Throws() { using (TestDevice device = TestDevice.Open()) { if (device == null) { return; } SourceVoicePool pool = PoolOf(device, count: 1); pool.Take(); SourceVoice stranger = device.MonoVoice(); CheckThrow(typeof(ArgumentException)); pool.Return(stranger); } } } public class DestroyTests : TestFixture { public void DestroysVoicesInUseAndIdle() { using (TestDevice device = TestDevice.Open()) { if (device == null) { return; } SourceVoicePool pool = PoolOf(device, count: 2); SourceVoice inUse = pool.Take(); SourceVoice idle = pool.Take(); pool.Return(idle); pool.Destroy(); CheckTrue(inUse.IsDestroyed); CheckTrue(idle.IsDestroyed); CheckNull(pool.Take()); } } } } }