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

     1  package num
     2  
     3  import (
     4  	"gitee.com/quant1x/num/labs"
     5  	"slices"
     6  	"testing"
     7  )
     8  
     9  func TestShift(t *testing.T) {
    10  	type args struct {
    11  		x any
    12  		y any
    13  	}
    14  	type testCase struct {
    15  		Name     string
    16  		Args     args
    17  		Want     any
    18  		TestFunc func(v any) any
    19  	}
    20  	tests := []testCase{
    21  		{
    22  			Name: "shift-left-1",
    23  			Args: args{
    24  				x: []int32{-1, 1, -2, -3},
    25  				y: 1,
    26  			},
    27  			Want: []int32{0, -1, 1, -2},
    28  			TestFunc: func(v any) any {
    29  				args := v.(args)
    30  				return Shift(args.x.([]int32), args.y.(int))
    31  			},
    32  		},
    33  		{
    34  			Name: "int32-right-1",
    35  			Args: args{
    36  				x: []int32{-1, 1, -2, -3, 1, 5},
    37  				y: -1,
    38  			},
    39  			Want: []int32{1, -2, -3, 1, 5, 0},
    40  			TestFunc: func(v any) any {
    41  				args := v.(args)
    42  				return Shift(args.x.([]int32), args.y.(int))
    43  			},
    44  		},
    45  	}
    46  
    47  	for _, tt := range tests {
    48  		t.Run(tt.Name, func(t *testing.T) {
    49  			if got := tt.TestFunc(tt.Args); !labs.DeepEqual(got, tt.Want) {
    50  				t.Errorf("Shift() = %v, want %v", got, tt.Want)
    51  			}
    52  		})
    53  	}
    54  }
    55  
    56  func TestShiftV4(t *testing.T) {
    57  	type args struct {
    58  		x any
    59  		y any
    60  	}
    61  	type testCase struct {
    62  		Name     string
    63  		Args     args
    64  		Want     any
    65  		TestFunc func(v any) any
    66  	}
    67  	tests := []testCase{
    68  		{
    69  			Name: "shift-left-1",
    70  			Args: args{
    71  				x: []int32{-1, 1, -2, -3},
    72  				y: 1,
    73  			},
    74  			Want: []int32{0, -1, 1, -2},
    75  			TestFunc: func(v any) any {
    76  				args := v.(args)
    77  				return v4Shift(args.x.([]int32), args.y.(int))
    78  			},
    79  		},
    80  		{
    81  			Name: "int32-right-1",
    82  			Args: args{
    83  				x: []int32{-1, 1, -2, -3, 1, 5},
    84  				y: -1,
    85  			},
    86  			Want: []int32{1, -2, -3, 1, 5, 0},
    87  			TestFunc: func(v any) any {
    88  				args := v.(args)
    89  				return v4Shift(args.x.([]int32), args.y.(int))
    90  			},
    91  		},
    92  		{
    93  			Name: "float64-vector-v1",
    94  			Args: args{
    95  				x: []float64{-1, 1, -2, -3, 1, 5},
    96  				y: []int{-1, 1, -2, -3, 1, 5},
    97  			},
    98  			Want: []float64{1, -1, 1, Float64NaN(), -3, -1},
    99  			TestFunc: func(v any) any {
   100  				args := v.(args)
   101  				return v1Shift(args.x.([]float64), args.y.([]int))
   102  			},
   103  		},
   104  		{
   105  			Name: "int32-vector-v4",
   106  			Args: args{
   107  				x: []int32{-1, 1, -2, -3, 1, 5},
   108  				y: []int32{-1, 1, -2, -3, 1, 5},
   109  			},
   110  			Want: []int32{1, -1, 1, Int32NaN, -3, -1},
   111  			TestFunc: func(v any) any {
   112  				args := v.(args)
   113  				return v4Shift(args.x.([]int32), args.y.([]int32))
   114  			},
   115  		},
   116  	}
   117  
   118  	for _, tt := range tests {
   119  		t.Run(tt.Name, func(t *testing.T) {
   120  			if got := tt.TestFunc(tt.Args); !labs.DeepEqual(got, tt.Want) {
   121  				t.Errorf("v4Shift() = %v, want %v", got, tt.Want)
   122  			}
   123  		})
   124  	}
   125  }
   126  
   127  func BenchmarkShift_init(b *testing.B) {
   128  	testalignOnce.Do(initTestData)
   129  }
   130  
   131  func BenchmarkShift_release(b *testing.B) {
   132  	testalignOnce.Do(initTestData)
   133  	f64s := slices.Clone(testDataFloat64)
   134  	for i := 0; i < b.N; i++ {
   135  		Shift(f64s, 1)
   136  	}
   137  }
   138  
   139  func BenchmarkShift_v1(b *testing.B) {
   140  	testalignOnce.Do(initTestData)
   141  	f64s := slices.Clone(testDataFloat64)
   142  	for i := 0; i < b.N; i++ {
   143  		v1Shift(f64s, 1)
   144  	}
   145  }
   146  
   147  func BenchmarkShift_v2(b *testing.B) {
   148  	testalignOnce.Do(initTestData)
   149  	f64s := slices.Clone(testDataFloat64)
   150  	for i := 0; i < b.N; i++ {
   151  		v2Shift(f64s, 1)
   152  	}
   153  }
   154  
   155  func BenchmarkShift_v3(b *testing.B) {
   156  	testalignOnce.Do(initTestData)
   157  	f64s := slices.Clone(testDataFloat64)
   158  	for i := 0; i < b.N; i++ {
   159  		v3Shift(f64s, 1)
   160  	}
   161  }
   162  
   163  func BenchmarkShift_v4(b *testing.B) {
   164  	testalignOnce.Do(initTestData)
   165  	f64s := slices.Clone(testDataFloat64)
   166  	for i := 0; i < b.N; i++ {
   167  		v4Shift(f64s, 1)
   168  	}
   169  }