github.com/Anderson-Lu/gobox@v0.0.0-20191127065433-3e6c4c2da420/number/variance_test.go (about) 1 package number 2 3 import "testing" 4 5 type D struct { 6 Num float64 7 } 8 9 type demo []D 10 11 func (s demo) Len() int { 12 return len(s) 13 } 14 15 func (s demo) Get(i int) float64 { 16 return s[i].Num 17 } 18 19 func TestCalcAverage(t *testing.T) { 20 21 s := demo{D{Num: 1.0}, D{Num: 2.0}, D{Num: 3.0}, D{Num: 4.0}, D{Num: 5.0}} 22 23 type args struct { 24 data Avg 25 } 26 tests := []struct { 27 name string 28 args args 29 want float64 30 }{ 31 { 32 name: "计算平均数", 33 args: args{ 34 data: s, 35 }, 36 want: 3.0, 37 }, 38 } 39 for _, tt := range tests { 40 t.Run(tt.name, func(t *testing.T) { 41 if got := CalcAverage(tt.args.data); got != tt.want { 42 t.Errorf("CalcAverage() = %v, want %v", got, tt.want) 43 } 44 }) 45 } 46 } 47 48 func TestCalcVariance(t *testing.T) { 49 s := demo{D{Num: 1.0}, D{Num: 2.0}, D{Num: 3.0}, D{Num: 4.0}, D{Num: 5.0}} 50 type args struct { 51 data Variance 52 } 53 tests := []struct { 54 name string 55 args args 56 want float64 57 }{ 58 { 59 name: "计算方差", 60 args: args{ 61 data: s, 62 }, 63 want: 2.0, 64 }, 65 } 66 for _, tt := range tests { 67 t.Run(tt.name, func(t *testing.T) { 68 if got := CalcVariance(tt.args.data); got != tt.want { 69 t.Errorf("CalcVariance() = %v, want %v", got, tt.want) 70 } 71 }) 72 } 73 }