gitee.com/quant1x/num@v0.3.2/arithmetics_sub_test.go (about) 1 package num 2 3 import ( 4 "gitee.com/quant1x/num/labs" 5 "testing" 6 ) 7 8 func TestSub(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{-2, 0, -4, 0}, 27 TestFunc: func(v any) any { 28 vs := v.(args) 29 return Sub(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{-2, 0, -4, 0, 0}, 39 TestFunc: func(v any) any { 40 vs := v.(args) 41 return Sub(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, 0, 0, 0}, 51 TestFunc: func(v any) any { 52 vs := v.(args) 53 return Sub(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, 0, 0, 0}, 63 TestFunc: func(v any) any { 64 vs := v.(args) 65 return Sub(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{-1.1, 0.0, -3.00, -4}, 75 TestFunc: func(v any) any { 76 vs := v.(args) 77 return Sub(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{-1.1, Float64NaN(), Float64NaN(), Float64NaN()}, 87 TestFunc: func(v any) any { 88 vs := v.(args) 89 return Sub(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("Sub() = %v, want %v", got, tt.Want) 98 } 99 }) 100 } 101 }