-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistogram_test.go
112 lines (99 loc) · 2.62 KB
/
histogram_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
package graphblast
import (
"errors"
"strings"
"testing"
)
func TestHistogramAdd(t *testing.T) {
hist := NewHistogram()
hist.Add(1, nil)
if len(hist.Values) != 1 {
t.Error("histogram did not record count")
}
if hist.Values["1"] != 1 {
t.Error("histogram recorded wrong count")
}
if hist.Min != 1 || hist.Max != 1 || hist.Sum != 1 {
t.Error("histogram recorded wrong value stat")
}
if hist.Count != 1 || hist.Filtered != 0 || hist.Errors != 0 {
t.Error("histogram recorded wrong count stat")
}
hist.Add(-0.5, nil)
if len(hist.Values) != 2 {
t.Error("histogram did not record count")
}
if hist.Values["-1"] != 1 {
t.Error("histogram recorded wrong count")
}
if hist.Min != -0.5 || hist.Max != 1 || hist.Sum != 0.5 {
t.Error("histogram recorded wrong value stat")
}
if hist.Count != 2 || hist.Filtered != 0 || hist.Errors != 0 {
t.Error("histogram recorded wrong count stat")
}
}
func TestHistogramAddError(t *testing.T) {
hist := NewHistogram()
hist.Add(1, errors.New("fail"))
if len(hist.Values) != 0 {
t.Error("histogram recorded count for error")
}
if hist.Count != 0 || hist.Filtered != 0 || hist.Errors != 1 {
t.Error("histogram recorded wrong count stat")
}
}
func TestHistogramAddFiltered(t *testing.T) {
hist := NewHistogram()
hist.Allowed = Range{Countable(-1), Countable(0)}
hist.Add(1, nil)
if len(hist.Values) != 0 {
t.Error("histogram recorded count for filtered")
}
if hist.Count != 0 || hist.Filtered != 1 || hist.Errors != 0 {
t.Error("histogram recorded wrong count stat")
}
}
func TestHistogramChanged(t *testing.T) {
hist := NewHistogram()
changed, next := hist.Changed(0)
if changed {
t.Error("histogram should be unchanged")
}
if next != 0 {
t.Error("Changed should return indicator when nothing has changed")
}
hist.Add(1, nil)
changed, next = hist.Changed(0)
if !changed {
t.Error("histogram should be changed")
}
if next <= 0 {
t.Error("Changed should return a new indicator after changes")
}
}
func TestHistogramRead(t *testing.T) {
hist := NewHistogram()
reader := strings.NewReader("5\n5\n")
hist.Read(reader)
if hist.Count != 2 {
t.Error("Read failed to read the input fully")
}
if hist.Errors != 0 {
t.Error("Read failed to read the input without errors")
}
if hist.Values["5"] != 2 {
t.Error("Read failed to read the correct values")
}
reader = strings.NewReader("5\na\n")
hist.Read(reader)
if hist.Count != 3 {
t.Error("Read failed to read the good part of a bad input")
}
if hist.Errors != 1 {
t.Error("Read failed to signal an error in the input")
}
if hist.Values["5"] != 3 {
t.Error("Read failed to read the correct values")
}
}