using Amphora; using System; using UnitTestSharp; using static Susurrus.UnitTests.TestSounds; namespace Susurrus.UnitTests { public class DecodedSoundTests : TestFixture { public class CtorTests : TestFixture { public void AssignsAllFourComponents() { byte[] bytes = new byte[8]; DecodedSound sound = new DecodedSound(new ByteView(bytes), sampleRate: 22050, channelCount: 2, format: SampleFormat.Float32); CheckTrue(sound.Samples.IsOver(bytes)); CheckEqual(22050, sound.SampleRate); CheckEqual(2, sound.ChannelCount); CheckEqual(SampleFormat.Float32, sound.Format); } public void KeepsTheWindowOfTheViewItWasGiven() { ByteView window = new ByteView(new byte[10], startIndex: 2, endIndex: 6); DecodedSound sound = new DecodedSound(window, sampleRate: 44100, channelCount: 1, format: SampleFormat.Pcm16); CheckEqual(2, sound.Samples.StartIndex); CheckEqual(6, sound.Samples.EndIndex); CheckEqual(2, sound.FrameCount); } public void AcceptsNoSamples() { CheckEqual(0, new DecodedSound(Silence(0), 44100, 1, SampleFormat.Pcm16).FrameCount); } public void RejectsADefaultView() { CheckThrow(typeof(ArgumentException)); new DecodedSound(default(ByteView), 44100, 1, SampleFormat.Pcm16); } public void RejectsZeroSampleRate() { CheckThrow(typeof(ArgumentOutOfRangeException)); new DecodedSound(Silence(2), sampleRate: 0, channelCount: 1, format: SampleFormat.Pcm16); } public void RejectsZeroChannels() { CheckThrow(typeof(ArgumentOutOfRangeException)); new DecodedSound(Silence(2), sampleRate: 44100, channelCount: 0, format: SampleFormat.Pcm16); } public void RejectsAnUndefinedFormat() { CheckThrow(typeof(ArgumentOutOfRangeException)); new DecodedSound(Silence(2), sampleRate: 44100, channelCount: 1, format: (SampleFormat)99); } public void RejectsBytesThatEndMidFrame() { CheckThrow(typeof(ArgumentException)); new DecodedSound(Silence(6), sampleRate: 44100, channelCount: 2, format: SampleFormat.Pcm16); } } public class FrameCountTests : TestFixture { public void CountsEachFrameOnceAcrossChannels() { CheckEqual(3, new DecodedSound(Silence(24), 44100, channelCount: 2, format: SampleFormat.Float32) .FrameCount); } public void CountsPcm16Frames() { CheckEqual(5, new DecodedSound(Silence(10), 44100, channelCount: 1, format: SampleFormat.Pcm16) .FrameCount); } } public class DurationTests : TestFixture { public void IsFramesDividedByTheSampleRate() { CheckEqual(TimeSpan.FromSeconds(0.5), new DecodedSound(Silence(22050 * 2), sampleRate: 44100, channelCount: 1, format: SampleFormat.Pcm16).Duration); } public void TruncatesToWholeTicks() { CheckEqual(TimeSpan.FromTicks(3333333), new DecodedSound(Silence(2), sampleRate: 3, channelCount: 1, format: SampleFormat.Pcm16).Duration); } public void IsZeroWithNoSamples() { CheckEqual(TimeSpan.Zero, new DecodedSound(Silence(0), 44100, 1, SampleFormat.Pcm16).Duration); } } public class GetBytesPerSampleTests : TestFixture { public void Pcm16IsTwo() { CheckEqual(2, DecodedSound.GetBytesPerSample(SampleFormat.Pcm16)); } public void Float32IsFour() { CheckEqual(4, DecodedSound.GetBytesPerSample(SampleFormat.Float32)); } public void RejectsAnUndefinedFormat() { CheckThrow(typeof(ArgumentOutOfRangeException)); DecodedSound.GetBytesPerSample((SampleFormat)99); } } } }