Skip to content

Commit 79cea88

Browse files
committed
test: Add tests for SmoothedData
1 parent f5b2085 commit 79cea88

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

Source/ORTS.Common/SmoothedData.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// COPYRIGHT 2010, 2011 by the Open Rails project.
1+
// COPYRIGHT 2010, 2011 by the Open Rails project.
22
//
33
// This file is part of Open Rails.
44
//
@@ -24,16 +24,19 @@ namespace ORTS.Common
2424
{
2525
public class SmoothedData
2626
{
27-
public readonly float SmoothPeriodS = 3;
27+
const float DefaultSmoothPeriodS = 3;
28+
29+
public readonly float SmoothPeriodS;
30+
2831
protected float value = float.NaN;
2932
protected float smoothedValue = float.NaN;
3033

3134
public SmoothedData()
35+
: this(DefaultSmoothPeriodS)
3236
{
3337
}
3438

3539
public SmoothedData(float smoothPeriodS)
36-
: this()
3740
{
3841
SmoothPeriodS = smoothPeriodS;
3942
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// COPYRIGHT 2022 by the Open Rails project.
2+
//
3+
// This file is part of Open Rails.
4+
//
5+
// Open Rails is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Open Rails is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
17+
18+
using System.Linq;
19+
using Xunit;
20+
21+
namespace Tests.Orts.Common
22+
{
23+
public static class SmoothedData
24+
{
25+
[Theory]
26+
// FPS-like tests
27+
[InlineData(5, 3, 0.318)]
28+
[InlineData(10, 3, 0.337)]
29+
[InlineData(30, 3, 0.350)]
30+
[InlineData(60, 3, 0.353)]
31+
[InlineData(120, 3, 0.355)]
32+
// Physics-like tests
33+
[InlineData(60, 1, 0.000)] // Exhaust particles
34+
[InlineData(60, 2, 0.066)] // Smoke colour
35+
[InlineData(60, 45, 8.007)] // Field rate
36+
[InlineData(60, 150, 9.355)] // Burn rate
37+
[InlineData(60, 240, 9.592)] // Boiler heat
38+
public static void SmoothedFPS(int fps, float smoothPeriodS, float expected)
39+
{
40+
var period = (float)(1d / fps);
41+
var smoothed = new ORTS.Common.SmoothedData(smoothPeriodS);
42+
smoothed.Update(0, 10);
43+
foreach (var i in Enumerable.Range(0, 10 * fps)) smoothed.Update(period, 0);
44+
Assert.Equal(expected, smoothed.SmoothedValue, 3);
45+
}
46+
}
47+
}

Source/Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<Compile Include="DynamicPrecisionEqualityComparer.cs" />
6666
<Compile Include="Orts.Parsers.Msts\StfReader.cs" />
6767
<Compile Include="Orts.Common\Conversions.cs" />
68+
<Compile Include="Orts.Common\SmoothedData.cs" />
6869
<Compile Include="Orts.Parsers.OR\TimetableReaderTests.cs" />
6970
<Compile Include="Properties\AssemblyInfo.cs" />
7071
<Compile Include="TestFile.cs" />

0 commit comments

Comments
 (0)