gitee.com/quant1x/num@v0.3.2/rolling_test.go (about)

     1  package num
     2  
     3  import (
     4  	"gitee.com/quant1x/num/labs"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func TestRolling(t *testing.T) {
    10  	testSliceFloat := []float64{1, 2, 3, 4, 5, 6}
    11  	//expected := []float64{1, 2, 3, 4, 5, 6}
    12  	expected := [][]float64{
    13  		{},
    14  		{},
    15  		{1, 2, 3},
    16  		{2, 3, 4},
    17  		{3, 4, 5},
    18  		{4, 5, 6},
    19  	}
    20  	output := Rolling(testSliceFloat, 3)
    21  	if reflect.DeepEqual(expected, output) != true {
    22  		t.Errorf("Got %v, want %v", output, expected)
    23  	}
    24  
    25  	output = Rolling(testSliceFloat, 3)
    26  	if reflect.DeepEqual(expected, output) != true {
    27  		t.Errorf("Got %v, want %v", output, expected)
    28  	}
    29  
    30  	output = Rolling(testSliceFloat, []int{3, 3, 3, 3, 3, 3})
    31  	if reflect.DeepEqual(expected, output) != true {
    32  		t.Errorf("Got %v, want %v", output, expected)
    33  	}
    34  
    35  	output = Rolling(testSliceFloat, []float64{3, 3, 3, 3, 3, 3})
    36  	if reflect.DeepEqual(expected, output) != true {
    37  		t.Errorf("Got %v, want %v", output, expected)
    38  	}
    39  }
    40  
    41  func Test_v3Rolling(t *testing.T) {
    42  	type args[E BaseType] struct {
    43  		S     []E
    44  		N     any
    45  		apply func(N DType, values ...E) E
    46  	}
    47  	type testCase[E BaseType] struct {
    48  		name string
    49  		args args[E]
    50  		want []E
    51  	}
    52  	tests := []testCase[float64]{
    53  		{
    54  			name: "ma",
    55  			args: args[float64]{
    56  				S: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
    57  				N: 5,
    58  				apply: func(N DType, values ...float64) float64 {
    59  					return Sum(values) / N
    60  				},
    61  			},
    62  			want: []float64{Float64NaN(), Float64NaN(), Float64NaN(), Float64NaN(), 3, 4, 5, 6, 7, 8},
    63  		},
    64  	}
    65  	for _, tt := range tests {
    66  		t.Run(tt.name, func(t *testing.T) {
    67  			if got := v3Rolling(tt.args.S, tt.args.N, tt.args.apply); !labs.DeepEqual(got, tt.want) {
    68  				t.Errorf("v3Rolling() = %v, want %v", got, tt.want)
    69  			}
    70  		})
    71  	}
    72  }