2020-04-29 01:58:08 +00:00
|
|
|
// 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
|
|
|
|
{
|
2020-05-01 23:33:33 +00:00
|
|
|
private static readonly Period[] single_period = { new Period(1.0, 2.0) };
|
2020-04-29 01:58:08 +00:00
|
|
|
|
2020-05-01 23:33:33 +00:00
|
|
|
private static readonly Period[] unordered_periods =
|
2020-04-29 01:58:08 +00:00
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2020-05-01 23:33:33 +00:00
|
|
|
var tracker = new PeriodTracker(single_period);
|
2020-04-29 01:58:08 +00:00
|
|
|
|
2020-05-01 23:33:33 +00:00
|
|
|
var period = single_period.Single();
|
2020-04-29 02:08:38 +00:00
|
|
|
Assert.IsTrue(tracker.IsInAny(period.Start));
|
2020-05-01 23:33:33 +00:00
|
|
|
Assert.IsTrue(tracker.IsInAny(getMidpoint(period)));
|
2020-04-29 02:08:38 +00:00
|
|
|
Assert.IsTrue(tracker.IsInAny(period.End));
|
2020-04-29 01:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestCheckValuesInsidePeriods()
|
|
|
|
{
|
2020-05-01 23:33:33 +00:00
|
|
|
var tracker = new PeriodTracker(unordered_periods);
|
2020-04-29 01:58:08 +00:00
|
|
|
|
2020-05-01 23:33:33 +00:00
|
|
|
foreach (var period in unordered_periods)
|
|
|
|
Assert.IsTrue(tracker.IsInAny(getMidpoint(period)));
|
2020-04-29 01:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestCheckValuesInRandomOrder()
|
|
|
|
{
|
2020-05-01 23:33:33 +00:00
|
|
|
var tracker = new PeriodTracker(unordered_periods);
|
2020-04-29 01:58:08 +00:00
|
|
|
|
2020-05-01 23:33:33 +00:00
|
|
|
foreach (var period in unordered_periods.OrderBy(_ => RNG.Next()))
|
|
|
|
Assert.IsTrue(tracker.IsInAny(getMidpoint(period)));
|
2020-04-29 01:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestCheckValuesOutOfPeriods()
|
|
|
|
{
|
2020-05-01 23:33:33 +00:00
|
|
|
var tracker = new PeriodTracker(new[]
|
2020-04-29 01:58:08 +00:00
|
|
|
{
|
2020-05-01 23:33:33 +00:00
|
|
|
new Period(1.0, 2.0),
|
|
|
|
new Period(3.0, 4.0)
|
|
|
|
});
|
2020-04-29 01:58:08 +00:00
|
|
|
|
2020-04-29 02:08:38 +00:00
|
|
|
Assert.IsFalse(tracker.IsInAny(0.9), "Time before first period is being considered inside");
|
2020-04-29 01:58:08 +00:00
|
|
|
|
2020-04-29 02:08:38 +00:00
|
|
|
Assert.IsFalse(tracker.IsInAny(2.1), "Time right after first period is being considered inside");
|
|
|
|
Assert.IsFalse(tracker.IsInAny(2.9), "Time right before second period is being considered inside");
|
2020-04-29 01:58:08 +00:00
|
|
|
|
2020-04-29 02:08:38 +00:00
|
|
|
Assert.IsFalse(tracker.IsInAny(4.1), "Time after last period is being considered inside");
|
2020-04-29 01:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestReversedPeriodHandling()
|
|
|
|
{
|
|
|
|
Assert.Throws<ArgumentException>(() =>
|
|
|
|
{
|
2020-05-01 23:33:33 +00:00
|
|
|
_ = new PeriodTracker(new[]
|
2020-04-29 01:58:08 +00:00
|
|
|
{
|
|
|
|
new Period(2.0, 1.0)
|
2020-05-01 23:33:33 +00:00
|
|
|
});
|
2020-04-29 01:58:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-01 23:33:33 +00:00
|
|
|
private double getMidpoint(Period period) => period.Start + (period.End - period.Start) / 2;
|
2020-04-29 01:58:08 +00:00
|
|
|
}
|
|
|
|
}
|