-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontinuous_averager_test.go
More file actions
54 lines (38 loc) · 1.12 KB
/
continuous_averager_test.go
File metadata and controls
54 lines (38 loc) · 1.12 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
// Copyright (C) 2019-2024, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package math
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestAverager(t *testing.T) {
require := require.New(t)
halflife := time.Second
currentTime := time.Now()
a := NewSyncAverager(NewAverager(0, halflife, currentTime))
require.Zero(a.Read())
currentTime = currentTime.Add(halflife)
a.Observe(1, currentTime)
require.Equal(1.0/1.5, a.Read())
}
func TestAveragerTimeTravel(t *testing.T) {
require := require.New(t)
halflife := time.Second
currentTime := time.Now()
a := NewSyncAverager(NewAverager(1, halflife, currentTime))
require.Equal(float64(1), a.Read())
currentTime = currentTime.Add(-halflife)
a.Observe(0, currentTime)
require.Equal(1.0/1.5, a.Read())
}
func TestUninitializedAverager(t *testing.T) {
require := require.New(t)
halfLife := time.Second
currentTime := time.Now()
firstObservation := float64(10)
a := NewUninitializedAverager(halfLife)
require.Zero(a.Read())
a.Observe(firstObservation, currentTime)
require.Equal(firstObservation, a.Read())
}