47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package calc_test
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
|
|
"go.maze.io/harmon/internal/calc"
|
|
)
|
|
|
|
func TestMean(t *testing.T) {
|
|
testCases := []struct {
|
|
Test []float32
|
|
Want float64
|
|
}{
|
|
{nil, 0},
|
|
{[]float32{3, 1, 4, 1, 5, 9}, 3.8333333333333335},
|
|
{[]float32{1e20, 3, 1, 4, 1, 5, 9, -1e20}, 2.875},
|
|
{[]float32{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, .11}, 3.674},
|
|
{[]float32{10, 20, 30, 40, 50, -100, 4.7, -11e2}, -130.66250002384186},
|
|
}
|
|
for _, test := range testCases {
|
|
if v := calc.Mean(test.Test); !almostEq(v, test.Want) {
|
|
t.Errorf("expected Mean(%v) to return %g, got %g", test.Test, test.Want, v)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRMS(t *testing.T) {
|
|
testCases := []struct {
|
|
Test []float32
|
|
Want float64
|
|
}{
|
|
{[]float32{3, 1, 4, 1, 5, 9}, 4.708148963941845},
|
|
{[]float32{1e20, 3, 1, 4, 1, 5, 9, -1e20}, 5.000000100204387e+19},
|
|
{[]float32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 6.2048368229954285},
|
|
}
|
|
for _, test := range testCases {
|
|
if v := calc.RMS(test.Test); !almostEq(v, test.Want) {
|
|
t.Errorf("expected RMS(%v) to return %g, got %g", test.Test, test.Want, v)
|
|
}
|
|
}
|
|
}
|
|
|
|
func almostEq(a, b float64) bool {
|
|
return math.Abs(a-b) < 1e-5
|
|
}
|