chore: add test case for SampleSnapshot.Sum

This commit is contained in:
levisyin 2024-05-24 12:09:49 +08:00
parent 61b3d93bb0
commit cc792610f4

View file

@ -103,20 +103,25 @@ func TestExpDecaySample(t *testing.T) {
} }
snap := sample.Snapshot() snap := sample.Snapshot()
if have, want := int(snap.Count()), tc.updates; have != want { if have, want := int(snap.Count()), tc.updates; have != want {
t.Errorf("have %d want %d", have, want) t.Errorf("unexpected count: have %d want %d", have, want)
} }
if have, want := snap.Size(), min(tc.updates, tc.reservoirSize); have != want { if have, want := snap.Size(), min(tc.updates, tc.reservoirSize); have != want {
t.Errorf("have %d want %d", have, want) t.Errorf("unexpected size: have %d want %d", have, want)
} }
values := snap.(*sampleSnapshot).values values := snap.(*sampleSnapshot).values
if have, want := len(values), min(tc.updates, tc.reservoirSize); have != want { if have, want := len(values), min(tc.updates, tc.reservoirSize); have != want {
t.Errorf("have %d want %d", have, want) t.Errorf("unexpected values length: have %d want %d", have, want)
} }
sum := int64(0)
for _, v := range values { for _, v := range values {
sum += v
if v > int64(tc.updates) || v < 0 { if v > int64(tc.updates) || v < 0 {
t.Errorf("out of range [0, %d]: %v", tc.updates, v) t.Errorf("out of range [0, %d]: %v", tc.updates, v)
} }
} }
if have, want := snap.Sum(), sum; have != want {
t.Errorf("unexpected sum: have %d want %d", have, want)
}
} }
} }