Adjust, simplify and make the test code more flexible to some degree

This commit is contained in:
Salman Ahmed 2020-04-29 04:58:08 +03:00
parent 00918ecb6d
commit 587f946dc6
No known key found for this signature in database
GPG Key ID: ED81FD33FD9B58BC
2 changed files with 103 additions and 115 deletions

View File

@ -1,115 +0,0 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using NUnit.Framework;
using osu.Game.Lists;
namespace osu.Game.Tests.Lists
{
[TestFixture]
public class IntervalListTest
{
// this is intended to be unordered to test adding intervals in unordered way.
private static readonly (double, double)[] test_intervals =
{
(-9.1d, -8.3d),
(-3.4d, 2.1d),
(9.0d, 50.0d),
(5.25d, 10.50d),
};
[Test]
public void TestCheckValueInsideSingleInterval()
{
var list = new IntervalList<double> { { 1.0d, 2.0d } };
Assert.IsTrue(list.IsInAnyInterval(1.0d));
Assert.IsTrue(list.IsInAnyInterval(1.5d));
Assert.IsTrue(list.IsInAnyInterval(2.0d));
}
[Test]
public void TestCheckValuesInsideIntervals()
{
var list = new IntervalList<double>();
foreach (var (start, end) in test_intervals)
list.Add(start, end);
Assert.IsTrue(list.IsInAnyInterval(-8.75d));
Assert.IsTrue(list.IsInAnyInterval(1.0d));
Assert.IsTrue(list.IsInAnyInterval(7.89d));
Assert.IsTrue(list.IsInAnyInterval(9.8d));
Assert.IsTrue(list.IsInAnyInterval(15.83d));
}
[Test]
public void TestCheckValuesInRandomOrder()
{
var list = new IntervalList<double>();
foreach (var (start, end) in test_intervals)
list.Add(start, end);
Assert.IsTrue(list.IsInAnyInterval(9.8d));
Assert.IsTrue(list.IsInAnyInterval(7.89d));
Assert.IsTrue(list.IsInAnyInterval(1.0d));
Assert.IsTrue(list.IsInAnyInterval(15.83d));
Assert.IsTrue(list.IsInAnyInterval(-8.75d));
}
[Test]
public void TestCheckValuesOutOfIntervals()
{
var list = new IntervalList<double>();
foreach (var (start, end) in test_intervals)
list.Add(start, end);
Assert.IsFalse(list.IsInAnyInterval(-9.2d));
Assert.IsFalse(list.IsInAnyInterval(2.2d));
Assert.IsFalse(list.IsInAnyInterval(5.15d));
Assert.IsFalse(list.IsInAnyInterval(51.2d));
}
[Test]
public void TestCheckValueAfterRemovedInterval()
{
var list = new IntervalList<int> { { 50, 100 }, { 150, 200 }, { 250, 300 } };
Assert.IsTrue(list.IsInAnyInterval(75));
Assert.IsTrue(list.IsInAnyInterval(175));
Assert.IsTrue(list.IsInAnyInterval(275));
list.Remove(list[1]);
Assert.IsFalse(list.IsInAnyInterval(175));
Assert.IsTrue(list.IsInAnyInterval(75));
Assert.IsTrue(list.IsInAnyInterval(275));
}
[Test]
public void TestCheckValueAfterAddedInterval()
{
var list = new IntervalList<int> { { 50, 100 }, { 250, 300 } };
Assert.IsFalse(list.IsInAnyInterval(175));
Assert.IsTrue(list.IsInAnyInterval(75));
Assert.IsTrue(list.IsInAnyInterval(275));
list.Add(150, 200);
Assert.IsTrue(list.IsInAnyInterval(175));
}
[Test]
public void TestReversedIntervalThrows()
{
var list = new IntervalList<int>();
Assert.Throws<ArgumentException>(() => list.Add(50, 25));
Assert.Throws<ArgumentException>(() => list.Add(new Interval<int>(50, 25)));
}
}
}

View File

@ -0,0 +1,103 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Utils;
using osu.Game.Utils;
namespace osu.Game.Tests.NonVisual
{
[TestFixture]
public class PeriodTrackerTest
{
private static readonly Period[] test_single_period = { new Period(1.0, 2.0) };
// this is intended to be unordered to test adding periods in unordered way.
private static readonly Period[] test_periods =
{
new Period(-9.1, -8.3),
new Period(-3.4, 2.1),
new Period(9.0, 50.0),
new Period(5.25, 10.50)
};
[Test]
public void TestCheckValueInsideSinglePeriod()
{
var tracker = new PeriodTracker { Periods = test_single_period };
var period = test_single_period.Single();
Assert.IsTrue(tracker.Contains(period.Start));
Assert.IsTrue(tracker.Contains(getMidTime(period)));
Assert.IsTrue(tracker.Contains(period.End));
}
[Test]
public void TestCheckValuesInsidePeriods()
{
var tracker = new PeriodTracker { Periods = test_periods };
foreach (var period in test_periods)
Assert.IsTrue(tracker.Contains(getMidTime(period)));
}
[Test]
public void TestCheckValuesInRandomOrder()
{
var tracker = new PeriodTracker { Periods = test_periods };
foreach (var period in test_periods.OrderBy(_ => RNG.Next()))
Assert.IsTrue(tracker.Contains(getMidTime(period)));
}
[Test]
public void TestCheckValuesOutOfPeriods()
{
var tracker = new PeriodTracker
{
Periods = new[]
{
new Period(1.0, 2.0),
new Period(3.0, 4.0)
}
};
Assert.IsFalse(tracker.Contains(0.9), "Time before first period is being considered inside");
Assert.IsFalse(tracker.Contains(2.1), "Time right after first period is being considered inside");
Assert.IsFalse(tracker.Contains(2.9), "Time right before second period is being considered inside");
Assert.IsFalse(tracker.Contains(4.1), "Time after last period is being considered inside");
}
[Test]
public void TestNullRemovesExistingPeriods()
{
var tracker = new PeriodTracker { Periods = test_single_period };
var period = test_single_period.Single();
Assert.IsTrue(tracker.Contains(getMidTime(period)));
tracker.Periods = null;
Assert.IsFalse(tracker.Contains(getMidTime(period)));
}
[Test]
public void TestReversedPeriodHandling()
{
var tracker = new PeriodTracker();
Assert.Throws<ArgumentException>(() =>
{
tracker.Periods = new[]
{
new Period(2.0, 1.0)
};
});
}
private double getMidTime(Period period) => period.Start + (period.End - period.Start) / 2;
}
}