68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package rrd
|
|
|
|
import "testing"
|
|
|
|
func f(v float64) *float64 { return &v }
|
|
|
|
func TestDownsampleNoop(t *testing.T) {
|
|
in := []Point{{T: 1, V: f(1)}, {T: 2, V: f(2)}, {T: 3, V: f(3)}}
|
|
out := downsample(in, 10)
|
|
if len(out) != len(in) {
|
|
t.Fatalf("expected no-op for points <= target, got %d points", len(out))
|
|
}
|
|
}
|
|
|
|
func TestDownsampleAveragesBuckets(t *testing.T) {
|
|
// 10 points, 0..9, bucketed down to 5: expect roughly paired averages.
|
|
in := make([]Point, 10)
|
|
for i := range in {
|
|
in[i] = Point{T: int64(i), V: f(float64(i))}
|
|
}
|
|
out := downsample(in, 5)
|
|
if len(out) != 5 {
|
|
t.Fatalf("expected 5 points, got %d", len(out))
|
|
}
|
|
want := []float64{0.5, 2.5, 4.5, 6.5, 8.5}
|
|
for i, pt := range out {
|
|
if pt.V == nil {
|
|
t.Fatalf("bucket %d: expected a value, got nil", i)
|
|
}
|
|
if *pt.V != want[i] {
|
|
t.Errorf("bucket %d: got %v, want %v", i, *pt.V, want[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDownsampleTreatsNullsAsZeroInsideNonEmptyBucket(t *testing.T) {
|
|
in := []Point{{T: 1, V: f(2)}, {T: 2, V: nil}, {T: 3, V: f(4)}, {T: 4, V: nil}}
|
|
out := downsample(in, 1)
|
|
if len(out) != 1 {
|
|
t.Fatalf("expected 1 bucket, got %d", len(out))
|
|
}
|
|
if out[0].V == nil || *out[0].V != 1.5 {
|
|
t.Fatalf("expected NULL-as-zero bucket average (1.5), got %v", out[0].V)
|
|
}
|
|
}
|
|
|
|
func TestDownsampleAllNullBucketStaysNull(t *testing.T) {
|
|
in := []Point{{T: 1, V: nil}, {T: 2, V: nil}}
|
|
out := downsample(in, 1)
|
|
if len(out) != 1 {
|
|
t.Fatalf("expected 1 bucket, got %d", len(out))
|
|
}
|
|
if out[0].V != nil {
|
|
t.Fatalf("expected nil for an all-null bucket, got %v", *out[0].V)
|
|
}
|
|
}
|
|
|
|
func TestDownsampleMaxKeepsPeak(t *testing.T) {
|
|
in := []Point{{T: 1, V: f(1)}, {T: 2, V: f(7)}, {T: 3, V: nil}, {T: 4, V: f(3)}}
|
|
out := downsampleMax(in, 1)
|
|
if len(out) != 1 {
|
|
t.Fatalf("expected 1 bucket, got %d", len(out))
|
|
}
|
|
if out[0].V == nil || *out[0].V != 7 {
|
|
t.Fatalf("expected bucket peak 7, got %v", out[0].V)
|
|
}
|
|
}
|