-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovingAverageTests.cs
More file actions
81 lines (70 loc) · 1.78 KB
/
Copy pathMovingAverageTests.cs
File metadata and controls
81 lines (70 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System.Linq;
using NUnit.Framework;
namespace yield
{
[TestFixture]
public class MovingAverageTests
{
[Test]
public void SmoothEmptySequence()
{
CheckAverage(10, new double[] { }, new double[] { });
}
[Test]
public void SmoothSingleZero()
{
CheckAverage(10, new[] { 0.0 }, new[] { 0.0 });
}
[Test]
public void SmoothSingleNonZeroValue()
{
CheckAverage(10, new[] { 100.0 }, new[] { 100.0 });
}
[Test]
public void SmoothTwoValues()
{
CheckAverage(2, new[] { 0, 20.0 }, new[] { 0, 10.0 });
}
[Test]
public void SmoothTwoValues2()
{
CheckAverage(2, new[] { 10, 0.0 }, new[] { 10, 5.0 });
}
[Test]
public void SmoothTwoValuesWithSmallWindow()
{
CheckAverage(1, new[] { 10, 0.0 }, new[] { 10, 0.0 });
}
[Test]
public void SmoothWithWindow2()
{
CheckAverage(2, new double[] { 1, 2, 3, 4, 5, 6 }, new double[] { 1, 1.5, 2.5, 3.5, 4.5, 5.5 });
}
[Test]
public void SmoothWithWindow3()
{
CheckAverage(3, new double[] { 3, 3, 3, 6, 6, 6 }, new double[] { 3, 3, 3, 4, 5, 6 });
}
[Test]
public void SmoothWithLargeWindow()
{
CheckAverage(100500, new double[] { 1, 2, 3, 4, 5, 6 }, new double[] { 1, 1.5, 2, 2.5, 3, 3.5 });
}
private void CheckAverage(int windowWidth, double[] ys, double[] expectedYs)
{
var dataPoints = ys.Select((v, index) => new DataPoint(GetX(index), v));
var actual = Factory.CreateAnalyzer().MovingAverage(dataPoints, windowWidth).ToList();
Assert.AreEqual(ys.Length, actual.Count);
for (var i = 0; i < actual.Count; i++)
{
Assert.AreEqual(GetX(i), actual[i].X, 1e-7);
Assert.AreEqual(ys[i], actual[i].OriginalY, 1e-7);
Assert.AreEqual(expectedYs[i], actual[i].AvgSmoothedY, 1e-7);
}
}
private double GetX(int index)
{
return (index - 3.0) / 2;
}
}
}