summaryrefslogtreecommitdiff
path: root/ms/ms_test.go
blob: 6f1e8078081f63e022c3db3a72fdd35aff5062c9 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package ms

import (
	"reflect"
	"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_StandardDeviationCalculation(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 _, sd := series.StandardDeviation(); !utilities.ApproximatelyEqual(2.93, sd, 0.01) {
		test.Fatalf("Standard deviation max calculation failed: %v.", sd)
	} else {
		test.Logf("Standard deviation calculation result: %v", sd)
	}
}

func Test_RotatingValues(test *testing.T) {
	series := NewMathematicalSeries[int](5)

	series.AddElement(1)
	series.AddElement(2)
	series.AddElement(3)
	series.AddElement(4)
	series.AddElement(5)

	series.AddElement(6)
	series.AddElement(7)

	if !reflect.DeepEqual([]int{6, 7, 3, 4, 5}, series.Values()) {
		test.Fatalf("Adding values does not properly erase earlier values.")
	}
}
func Test_Size(test *testing.T) {
	series := NewMathematicalSeries[int](5)

	series.AddElement(1)
	series.AddElement(2)
	series.AddElement(3)
	series.AddElement(4)
	series.AddElement(5)

	series.AddElement(6)
	series.AddElement(7)

	if series.Size() != 5 {
		test.Fatalf("Series size calculations failed.")
	}
}