From 4508e87a57c54675ac9d94818ea5e0f4c0f7d4e6 Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Sat, 5 Nov 2022 20:37:48 -0400 Subject: [Refactor] Rename/update MovingAverage to MathematicalSeries We want the MovingAverage functionality to be more generic and useful than just doing a moving average calculation. The new functionality allows for the calculation of the standard deviation and supports a generic type (so it can be used with integers and floats). --- ms/ms_test.go | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 ms/ms_test.go (limited to 'ms/ms_test.go') diff --git a/ms/ms_test.go b/ms/ms_test.go new file mode 100644 index 0000000..0ba6da6 --- /dev/null +++ b/ms/ms_test.go @@ -0,0 +1,83 @@ +package ms + +import ( + "testing" + + "github.com/network-quality/goresponsiveness/utilities" +) + +func Test_TooFewInstantsSequentialIncreasesLessThanAlwaysFalse(test *testing.T) { + series := NewMathematicalSeries[float64](500) + series.AddElement(0.0) + if islt, _ := series.AllSequentialIncreasesLessThan(6.0); islt { + test.Fatalf("Too few instants should always yield false when asking if sequential increases are less than a value.") + } +} + +func Test_SequentialIncreasesAlwaysLessThan(test *testing.T) { + series := NewMathematicalSeries[float64](40) + previous := float64(1.0) + for _ = range utilities.Iota(1, 80) { + previous *= 1.059 + series.AddElement(float64(previous)) + } + if islt, maxSeqIncrease := series.AllSequentialIncreasesLessThan(6.0); !islt { + test.Fatalf("Sequential increases are not always less than 6.0 (%f).", maxSeqIncrease) + } +} + +func Test_SequentialIncreasesAlwaysLessThanWithWraparound(test *testing.T) { + series := NewMathematicalSeries[float64](20) + previous := float64(1.0) + for range utilities.Iota(1, 20) { + previous *= 1.15 + series.AddElement(float64(previous)) + } + + // All those measurements should be ejected by the following + // loop! + for range utilities.Iota(1, 20) { + previous *= 1.10 + series.AddElement(float64(previous)) + } + + if islt, maxSeqIncrease := series.AllSequentialIncreasesLessThan(11.0); !islt { + test.Fatalf("Sequential increases are not always less than 11.0 in wraparound situation (%f v 11.0).", maxSeqIncrease) + } +} + +func Test_SequentialIncreasesAlwaysLessThanWithWraparoundInverse(test *testing.T) { + series := NewMathematicalSeries[float64](20) + previous := float64(1.0) + for range utilities.Iota(1, 20) { + previous *= 1.15 + series.AddElement(float64(previous)) + } + + // *Not* all those measurements should be ejected by the following + // loop! + for range utilities.Iota(1, 15) { + previous *= 1.10 + series.AddElement(float64(previous)) + } + + if islt, maxSeqIncrease := series.AllSequentialIncreasesLessThan(11.0); islt { + test.Fatalf("Sequential increases are (unexpectedly) always less than 11.0 in wraparound situation: %f v 11.0.", maxSeqIncrease) + } +} + +func Test_StandardDeviationLessThan_Float(test *testing.T) { + series := NewMathematicalSeries[float64](5) + // 5.7, 1.0, 8.6, 7.4, 2.2 + series.AddElement(5.7) + series.AddElement(1.0) + series.AddElement(8.6) + series.AddElement(7.4) + series.AddElement(2.2) + + if islt, sd := series.StandardDeviationLessThan(2.94); !islt { + test.Fatalf("Standard deviation max calculation failed: %v.", sd) + } else { + test.Logf("Standard deviation calculation result: %v", sd) + } +} -- cgit v1.2.3