caio.co/de/go-tdigest

Add tests for the panic() codepaths

Pretty much useless, but I was curious how to test for that. A bit
awkward at first though it kind of makes sense.
Id
252e36f865c811951875f440ba3f4d16f1e0eeb5
Author
Caio
Commit time
2015-09-08T09:38:30-03:00

Modified tdigest_test.go

@@ -204,6 +204,36
}
}

+func shouldPanic(f func(), t *testing.T, message string) {
+ defer func() {
+ tryRecover := recover()
+ if tryRecover == nil {
+ t.Errorf(message)
+ }
+ }()
+ f()
+}
+
+func TestPanic(t *testing.T) {
+ shouldPanic(func() {
+ New(0.5)
+ }, t, "Compression < 1 should panic!")
+
+ tdigest := New(100)
+
+ shouldPanic(func() {
+ tdigest.Quantile(-42)
+ }, t, "Quantile < 0 should panic!")
+
+ shouldPanic(func() {
+ tdigest.Quantile(42)
+ }, t, "Quantile > 1 should panic!")
+
+ shouldPanic(func() {
+ tdigest.findNearestCentroids(0.2)
+ }, t, "findNearestCentroids on empty summary should panic!")
+}
+
func benchmarkAdd(compression float64, b *testing.B) {
t := New(compression)