forked from influxdata/tdigest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
centroid_test.go
122 lines (118 loc) · 2.24 KB
/
centroid_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
package tdigest_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/influxdata/tdigest"
)
func TestCentroid_Add(t *testing.T) {
tests := []struct {
name string
c tdigest.Centroid
r tdigest.Centroid
want tdigest.Centroid
wantErr bool
errStr string
}{
{
name: "error when weight is zero",
r: tdigest.Centroid{
Weight: -1.0,
},
wantErr: true,
errStr: "centroid weight cannot be less than zero",
},
{
name: "zero weight",
c: tdigest.Centroid{
Weight: 0.0,
Mean: 1.0,
},
r: tdigest.Centroid{
Weight: 1.0,
Mean: 2.0,
},
want: tdigest.Centroid{
Weight: 1.0,
Mean: 2.0,
},
},
{
name: "weight order of magnitude",
c: tdigest.Centroid{
Weight: 1,
Mean: 1,
},
r: tdigest.Centroid{
Weight: 10,
Mean: 10,
},
want: tdigest.Centroid{
Weight: 11,
Mean: 9.181818181818182,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &tt.c
if err := c.Add(tt.r); (err != nil) != tt.wantErr {
t.Errorf("Centroid.Add() error = %v, wantErr %v", err, tt.wantErr)
} else if tt.wantErr && err.Error() != tt.errStr {
t.Errorf("Centroid.Add() error.Error() = %s, errStr %v", err.Error(), tt.errStr)
}
if !cmp.Equal(tt.c, tt.want) {
t.Errorf("unexprected centroid -want/+got\n%s", cmp.Diff(tt.want, tt.c))
}
})
}
}
func TestNewCentroidList(t *testing.T) {
tests := []struct {
name string
centroids []tdigest.Centroid
want tdigest.CentroidList
}{
{
name: "empty list",
},
{
name: "priority should be by mean ascending",
centroids: []tdigest.Centroid{
{
Mean: 2.0,
},
{
Mean: 1.0,
},
},
want: tdigest.CentroidList{
{
Mean: 1.0,
},
{
Mean: 2.0,
},
},
},
{
name: "single element should be identity",
centroids: []tdigest.Centroid{
{
Mean: 1.0,
},
},
want: tdigest.CentroidList{
{
Mean: 1.0,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tdigest.NewCentroidList(tt.centroids); !cmp.Equal(tt.want, got) {
t.Errorf("NewCentroidList() = -want/+got %s", cmp.Diff(tt.want, got))
}
})
}
}