-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaverage_test.go
204 lines (186 loc) · 5.07 KB
/
average_test.go
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package counter
import (
"fmt"
"math"
"math/bits"
"testing"
)
const requiredPercentAccuracy = 0.99999 // 99.999% ("five nines")
// assertAccurate checks that the actual value differs from the expected
// within acceptable range.
func assertAccurate(t *testing.T, expected, actual float64, failMsg string) {
if (actual / expected) < requiredPercentAccuracy {
t.Fatalf(failMsg)
}
}
func TestAverageAddLayering(t *testing.T) {
cases := []struct {
value, expectedAverage float64
}{
{value: 10, expectedAverage: 10},
{value: 5, expectedAverage: 7.5},
{value: 8, expectedAverage: 23.0 / 3},
}
average := new(Average)
for i, c := range cases {
err := average.Add(c.value)
if err != nil {
t.Fatalf("case %d failed: unexpected error: %v", i, err)
}
assertAccurate(t, c.expectedAverage, average.value,
fmt.Sprintf(`case %d failed:
adding value: %f
expected average: %f
actual average: %f`,
i, c.value, c.expectedAverage, average.value),
)
}
}
func TestAverageAccuracy(t *testing.T) {
// Averaging the same value repeatedly will surface any noticeable
// loss in accuracy from imprecise float representation.
avg := new(Average)
const (
val = 5555
floatVal = float64(val)
// The following was an arbitrarily chosen large number.
// This test may need to increase this depending on expected
// use. If it increases, this test should potentially use
// if testing.Short() { t.Skip() }
// due to increased runtime.
requiredRounds = 1e6 // 1 million actions
)
for i := 0; i < requiredRounds; i++ {
err := avg.Add(val)
if err != nil {
t.Fatalf("case %d failed: unexpected error %v", i, err)
}
assertAccurate(t, floatVal, avg.value,
fmt.Sprintf("Loss of precision exceeded acceptable values by %d round: %f",
i, (avg.value/floatVal)),
)
}
}
// maxInt calculates the maximum integer value regardless of
// the operating system.
func maxInt() int {
// Operations on signed integers behave differently from unsigned integers,
// so we will use unsigned and convert at the end.
// The end of this section discusses the two operators used
// in this function: https://golang.org/ref/spec#Arithmetic_operators
//
// Relies on the fact that uint and int are the same number of bits,
// differing by a sign flag on the leftmost bit for int.
// See: https://golang.org/ref/spec#Numeric_types
allOnes := ^uint(0)
// Leftmost bit of 1 will indicate a negative, so we need to change
// it to 0. Shift operation fills "empty" space with 0.
toggledSign := allOnes >> 1
return int(toggledSign)
}
func TestMaxInt(t *testing.T) {
actual := maxInt()
var expected int64 = math.MaxInt32
if bits.UintSize == 64 {
// This line will not compile on a 32 bit system if the type of
// "expected" is not int64. It will compile without error on a
// 64 bit system if type is int.
expected = math.MaxInt64
}
if int64(actual) != expected {
t.Fatalf("Maxint (%d) was not correctly calculated for Uintsize %d", actual, bits.UintSize)
}
}
func TestAverageEdgeCases(t *testing.T) {
// The expected values here were calculated using Wolfram Alpha,
// as most calculators will display less precision.
tts := []struct {
name string
avg Average
addValue float64
shouldErr bool
errorMsg string
expectedAvg Average
}{
{
name: "start max average",
avg: Average{
count: 11,
value: float64(math.MaxInt64),
},
addValue: 1,
expectedAvg: Average{
count: 12,
value: 8454757700450211157 + 5.0/12,
},
}, {
name: "start max count",
avg: Average{
count: maxInt(),
value: 10,
},
addValue: 1,
shouldErr: true,
errorMsg: "Average.count overflow - cannot add to this Average",
}, {
name: "zero addition",
avg: Average{
count: 5,
value: 10,
},
addValue: 0,
shouldErr: true,
errorMsg: "Average.Add called with non-positive value 0.000000",
}, {
name: "negative addition",
avg: Average{
count: 5,
value: 10,
},
addValue: -1,
shouldErr: true,
errorMsg: "Average.Add called with non-positive value -1.000000",
}, {
name: "max addition",
avg: Average{
count: 500,
value: 10,
},
addValue: float64(math.MaxInt64),
expectedAvg: Average{
count: 501,
value: 18409924225259043 + 265.0/501,
},
},
}
for _, tt := range tts {
t.Run(tt.name, func(t *testing.T) {
err := tt.avg.Add(tt.addValue)
if !tt.shouldErr && err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tt.shouldErr {
assertEqual(t, tt.errorMsg, err.Error(), "error message")
return
}
assertAccurate(t, tt.expectedAvg.value, tt.avg.value,
fmt.Sprintf("\nexpected average value: %+v\nactual average value: %+v",
tt.expectedAvg, tt.avg))
assertEqual(t, tt.expectedAvg.count, tt.avg.count, "average count")
})
}
}
func TestAverageHelpers(t *testing.T) {
const (
val = 8
ct = 30
)
actual := NewAverage(val, ct)
expected := &Average{
value: val,
count: ct,
}
assertEqual(t, *expected, *actual, "average")
assertEqual(t, actual.Value(), actual.value, "value")
assertEqual(t, actual.Count(), actual.count, "count")
}