summaryrefslogtreecommitdiff
path: root/ms/ms_test.go
diff options
context:
space:
mode:
authorWill Hawkins <[email protected]>2022-11-05 20:37:48 -0400
committerWill Hawkins <[email protected]>2022-11-05 20:39:40 -0400
commit4508e87a57c54675ac9d94818ea5e0f4c0f7d4e6 (patch)
treea884bbdb461f12406ace8b8b8a79ae8b9c00c206 /ms/ms_test.go
parent1f36afaf4e2c79aa4bc80f9bc8320ea28cc51f6f (diff)
[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).
Diffstat (limited to 'ms/ms_test.go')
-rw-r--r--ms/ms_test.go83
1 files changed, 83 insertions, 0 deletions
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)
+ }
+}