91 lines
3.7 KiB
C#
91 lines
3.7 KiB
C#
using GAJ3T.MusicGen.RiffGrooveBuilder.Core.Composition;
|
|
|
|
namespace GAJ3T.MusicGen.RiffGrooveBuilder.UnitTests.Composition;
|
|
|
|
public sealed class BeatFractionTests
|
|
{
|
|
[Theory]
|
|
[InlineData(2, 4, 1, 2)]
|
|
[InlineData(-6, 9, -2, 3)]
|
|
[InlineData(0, 123, 0, 1)]
|
|
[InlineData(long.MinValue, 2, long.MinValue / 2, 1)]
|
|
public void Normalizes(long numerator, long denominator, long expectedNumerator, long expectedDenominator)
|
|
{
|
|
var fraction = new BeatFraction(numerator, denominator);
|
|
Assert.Equal(expectedNumerator, fraction.Numerator);
|
|
Assert.Equal(expectedDenominator, fraction.Denominator);
|
|
Assert.Equal(new BeatFraction(expectedNumerator, expectedDenominator).GetHashCode(), fraction.GetHashCode());
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0)]
|
|
[InlineData(-1)]
|
|
[InlineData(long.MinValue)]
|
|
public void RejectsNonpositiveDenominator(long denominator) =>
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => new BeatFraction(1, denominator));
|
|
|
|
[Fact]
|
|
public void DefaultIsNormalizedZero()
|
|
{
|
|
Assert.Equal(new BeatFraction(0), default(BeatFraction));
|
|
Assert.Equal(1, default(BeatFraction).Denominator);
|
|
Assert.Equal(0, default(BeatFraction).ToTicks());
|
|
}
|
|
|
|
[Fact]
|
|
public void ArithmeticAndComparisonAreExact()
|
|
{
|
|
var half = new BeatFraction(1, 2);
|
|
var third = new BeatFraction(1, 3);
|
|
Assert.Equal(new BeatFraction(5, 6), half + third);
|
|
Assert.Equal(new BeatFraction(1, 6), half - third);
|
|
Assert.Equal(new BeatFraction(3, 2), half * 3);
|
|
Assert.Equal(new BeatFraction(1, 6), half / 3);
|
|
Assert.True(third < half);
|
|
Assert.True(half > third);
|
|
Assert.True(half <= new BeatFraction(2, 4));
|
|
Assert.True(half >= new BeatFraction(2, 4));
|
|
Assert.True(half != third);
|
|
Assert.True(half == new BeatFraction(2, 4));
|
|
Assert.Equal(-1, third.CompareTo(half));
|
|
Assert.Equal("1/2", half.ToString());
|
|
Assert.False(half.Equals(null));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(1, 2, 480)]
|
|
[InlineData(1, 3, 320)]
|
|
[InlineData(16, 1, 15360)]
|
|
[InlineData(1, 960, 1)]
|
|
public void ConvertsOnlyExactTicks(long numerator, long denominator, long ticks) =>
|
|
Assert.Equal(ticks, new BeatFraction(numerator, denominator).ToTicks());
|
|
|
|
[Theory]
|
|
[InlineData(1, 7)]
|
|
[InlineData(1, 1920)]
|
|
public void RejectsFractionalTicks(long numerator, long denominator) =>
|
|
Assert.Throws<ArgumentException>(() => new BeatFraction(numerator, denominator).ToTicks());
|
|
|
|
[Fact]
|
|
public void CheckedOperationsRejectOverflow()
|
|
{
|
|
Assert.Throws<OverflowException>(() => new BeatFraction(long.MaxValue) + new BeatFraction(1));
|
|
Assert.Throws<OverflowException>(() => new BeatFraction(long.MinValue) - new BeatFraction(1));
|
|
Assert.Throws<OverflowException>(() => new BeatFraction(long.MaxValue) * 2);
|
|
Assert.Throws<OverflowException>(() => new BeatFraction(1, long.MaxValue) / 2);
|
|
Assert.Throws<OverflowException>(() => new BeatFraction(long.MaxValue).ToTicks());
|
|
Assert.Throws<OverflowException>(() => new BeatFraction(long.MinValue) * -1);
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => new BeatFraction(1) / 0);
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => new BeatFraction(1) / -1);
|
|
}
|
|
|
|
[Fact]
|
|
public void WideIntermediatesDoNotRejectRepresentableNormalizedResults()
|
|
{
|
|
var large = new BeatFraction(long.MaxValue, 2);
|
|
Assert.Equal(new BeatFraction(long.MaxValue), large + large);
|
|
Assert.True(new BeatFraction(long.MaxValue, long.MaxValue - 1) > new BeatFraction(long.MaxValue - 1, long.MaxValue));
|
|
Assert.Equal(new BeatFraction(1), new BeatFraction(1, long.MaxValue) * long.MaxValue);
|
|
}
|
|
}
|