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

     1  package num
     2  
     3  import (
     4  	"gitee.com/quant1x/num/labs"
     5  	"testing"
     6  )
     7  
     8  func TestMul(t *testing.T) {
     9  	type args struct {
    10  		x any
    11  		y any
    12  	}
    13  	type testCase struct {
    14  		Name     string
    15  		Args     args
    16  		Want     any
    17  		TestFunc func(v any) any
    18  	}
    19  	tests := []testCase{
    20  		{
    21  			Name: "int32",
    22  			Args: args{
    23  				x: []int32{-1, 1, -2, -3},
    24  				y: []int32{1, 1, 2.00, -3},
    25  			},
    26  			Want: []int32{-1, 1, -4, 9},
    27  			TestFunc: func(v any) any {
    28  				vs := v.(args)
    29  				return Mul(vs.x.([]int32), vs.y.([]int32))
    30  			},
    31  		},
    32  		{
    33  			Name: "int32-align",
    34  			Args: args{
    35  				x: []int32{-1, 1, -2, -3, 1},
    36  				y: []int32{1, 1, 2.00, -3, 1, 5},
    37  			},
    38  			Want: []int32{-1, 1, -4, 9, 1},
    39  			TestFunc: func(v any) any {
    40  				vs := v.(args)
    41  				return Mul(vs.x.([]int32), vs.y.([]int32))
    42  			},
    43  		},
    44  		{
    45  			Name: "float32",
    46  			Args: args{
    47  				x: []float32{-0.1, 1.0, -2.00, -3},
    48  				y: []float32{-0.1, 1.0, -2.00, -3},
    49  			},
    50  			Want: []float32{0.010000001, 1, 4, 9},
    51  			TestFunc: func(v any) any {
    52  				vs := v.(args)
    53  				return Mul(vs.x.([]float32), vs.y.([]float32))
    54  			},
    55  		},
    56  		{
    57  			Name: "float64",
    58  			Args: args{
    59  				x: []float64{-0.1, 1.0, -2.00, -3},
    60  				y: []float64{-0.1, 1.0, -2.00, -3},
    61  			},
    62  			Want: []float64{0.010000000000000002, 1, 4, 9},
    63  			TestFunc: func(v any) any {
    64  				vs := v.(args)
    65  				return Mul(vs.x.([]float64), vs.y.([]float64))
    66  			},
    67  		},
    68  		{
    69  			Name: "float64-const-float64",
    70  			Args: args{
    71  				x: []float64{-0.1, 1.0, -2.00, -3},
    72  				y: float64(1),
    73  			},
    74  			Want: []float64{-0.1, 1.0, -2.00, -3},
    75  			TestFunc: func(v any) any {
    76  				vs := v.(args)
    77  				return Mul(vs.x.([]float64), vs.y.(float64))
    78  			},
    79  		},
    80  		{
    81  			Name: "float64-add-float32",
    82  			Args: args{
    83  				x: []float64{-0.1, 1.0, -2.00, -3},
    84  				y: []float32{1.00},
    85  			},
    86  			Want: []float64{-0.1, Float64NaN(), Float64NaN(), Float64NaN()},
    87  			TestFunc: func(v any) any {
    88  				vs := v.(args)
    89  				return Mul(vs.x.([]float64), vs.y)
    90  			},
    91  		},
    92  	}
    93  
    94  	for _, tt := range tests {
    95  		t.Run(tt.Name, func(t *testing.T) {
    96  			if got := tt.TestFunc(tt.Args); !labs.DeepEqual(got, tt.Want) {
    97  				t.Errorf("Mul() = %v, want %v", got, tt.Want)
    98  			}
    99  		})
   100  	}
   101  }