using System; using UnitTestSharp; namespace Susurrus.UnitTests { public class PanTests : TestFixture { public static bool Near(float expected, float actual) { return Math.Abs(expected - actual) < 1e-6f; } public static bool MatrixIs( PanMatrix matrix, float leftToLeft, float leftToRight, float rightToLeft, float rightToRight) { return Near(leftToLeft, matrix.LeftToLeft) && Near(leftToRight, matrix.LeftToRight) && Near(rightToLeft, matrix.RightToLeft) && Near(rightToRight, matrix.RightToRight); } public class CenterTests : TestFixture { public void IsZero() { CheckEqual(0f, Pan.Center); } } public class CheckTests : TestFixture { public void TheCenter_IsAllowed() { Pan.Check(0f, "pan"); } public void AllTheWayLeft_IsAllowed() { Pan.Check(-1f, "pan"); } public void AllTheWayRight_IsAllowed() { Pan.Check(1f, "pan"); } public void BeyondLeft_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Pan.Check(-1.01f, "pan"); } public void BeyondRight_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Pan.Check(1.01f, "pan"); } public void NotANumber_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Pan.Check(float.NaN, "pan"); } public void NamesTheParameterItWasGiven() { string thrownFor = null; try { Pan.Check(2f, "balance"); } catch (ArgumentOutOfRangeException exception) { thrownFor = exception.ParamName; } CheckEqual("balance", thrownFor); } } public class MatrixTests : TestFixture { public const float Half = 0.70710678f; public void Mono_InTheCenter_SplitsEvenlyAtConstantPower() { CheckTrue(MatrixIs(Pan.Matrix(0f, sourceChannels: 1), Half, Half, 0f, 0f)); } public void Mono_AllTheWayLeft_PlaysOnlyOnTheLeft() { CheckTrue(MatrixIs(Pan.Matrix(-1f, sourceChannels: 1), 1f, 0f, 0f, 0f)); } public void Mono_AllTheWayRight_PlaysOnlyOnTheRight() { CheckTrue(MatrixIs(Pan.Matrix(1f, sourceChannels: 1), 0f, 1f, 0f, 0f)); } public void Mono_AtEitherEnd_SilencesTheFarSideExactly() { CheckEqual(0f, Pan.Matrix(-1f, sourceChannels: 1).LeftToRight); CheckEqual(0f, Pan.Matrix(1f, sourceChannels: 1).LeftToLeft); } public void Stereo_AtEitherEnd_SilencesTheFarSideExactly() { CheckEqual(0f, Pan.Matrix(-1f, sourceChannels: 2).RightToRight); CheckEqual(0f, Pan.Matrix(1f, sourceChannels: 2).LeftToLeft); } public void Mono_HalfwayRight_KeepsItsPower() { PanMatrix matrix = Pan.Matrix(0.5f, sourceChannels: 1); CheckTrue(Near(1f, matrix.LeftToLeft * matrix.LeftToLeft + matrix.LeftToRight * matrix.LeftToRight)); CheckTrue(matrix.LeftToRight > matrix.LeftToLeft); } public void Stereo_InTheCenter_PassesThroughUnchanged() { CheckTrue(MatrixIs(Pan.Matrix(0f, sourceChannels: 2), 1f, 0f, 0f, 1f)); } public void Stereo_AllTheWayLeft_FoldsTheRightChannelIntoTheLeft() { CheckTrue(MatrixIs(Pan.Matrix(-1f, sourceChannels: 2), 1f, 0f, 1f, 0f)); } public void Stereo_AllTheWayRight_FoldsTheLeftChannelIntoTheRight() { CheckTrue(MatrixIs(Pan.Matrix(1f, sourceChannels: 2), 0f, 1f, 0f, 1f)); } public void Stereo_HalfwayLeft_MovesTheRightChannelTowardTheLeft() { CheckTrue(MatrixIs(Pan.Matrix(-0.5f, sourceChannels: 2), 1f, 0f, Half, Half)); } public void Stereo_HalfwayRight_MovesTheLeftChannelTowardTheRight() { CheckTrue(MatrixIs(Pan.Matrix(0.5f, sourceChannels: 2), Half, Half, 0f, 1f)); } public void PanOutOfRange_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Pan.Matrix(1.5f, sourceChannels: 1); } public void NoChannels_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Pan.Matrix(0f, sourceChannels: 0); } public void ThreeChannels_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Pan.Matrix(0f, sourceChannels: 3); } } } }