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

     1  package num
     2  
     3  import (
     4  	"gitee.com/quant1x/num/labs"
     5  	"slices"
     6  	"testing"
     7  )
     8  
     9  func TestAdd(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: "int32",
    23  			Args: args{
    24  				x: []int32{-1, 1, -2, -3},
    25  				y: []int32{0, 1, 2.00, -3},
    26  			},
    27  			Want: []int32{-1, 2, 0, -6},
    28  			TestFunc: func(v any) any {
    29  				vs := v.(args)
    30  				return Add(vs.x.([]int32), vs.y.([]int32))
    31  			},
    32  		},
    33  		{
    34  			Name: "int32-align",
    35  			Args: args{
    36  				x: []int32{-1, 1, -2, -3, 1, 5},
    37  				y: []int32{0, 1, 2.00, -3, 1},
    38  			},
    39  			Want: []int32{-1, 2, 0, -6, 2, 5},
    40  			TestFunc: func(v any) any {
    41  				vs := v.(args)
    42  				return Add(vs.x.([]int32), vs.y.([]int32))
    43  			},
    44  		},
    45  		{
    46  			Name: "float32",
    47  			Args: args{
    48  				x: []float32{-0.1, 1.0, -2.00, -3},
    49  				y: []float32{-0.1, 1.0, -2.00, -3},
    50  			},
    51  			Want: []float32{-0.2, 2.0, -4.00, -6},
    52  			TestFunc: func(v any) any {
    53  				vs := v.(args)
    54  				return Add(vs.x.([]float32), vs.y.([]float32))
    55  			},
    56  		},
    57  		{
    58  			Name: "float64",
    59  			Args: args{
    60  				x: []float64{-0.1, 1.0, -2.00, -3},
    61  				y: []float64{-0.1, 1.0, -2.00, -3},
    62  			},
    63  			Want: []float64{-0.2, 2.0, -4.00, -6},
    64  			TestFunc: func(v any) any {
    65  				vs := v.(args)
    66  				return Add(vs.x.([]float64), vs.y.([]float64))
    67  			},
    68  		},
    69  		{
    70  			Name: "float64-const-float64",
    71  			Args: args{
    72  				x: []float64{-0.1, 1.0, -2.00, -3},
    73  				y: float64(1),
    74  			},
    75  			Want: []float64{0.9, 2.0, -1.00, -2},
    76  			TestFunc: func(v any) any {
    77  				vs := v.(args)
    78  				return Add(vs.x.([]float64), vs.y.(float64))
    79  			},
    80  		},
    81  		{
    82  			Name: "float64-add-float32",
    83  			Args: args{
    84  				x: []float64{-0.1, 1.0, -2.00, -3},
    85  				y: []float32{1.00},
    86  			},
    87  			Want: []float64{0.9, Float64NaN(), Float64NaN(), Float64NaN()},
    88  			TestFunc: func(v any) any {
    89  				vs := v.(args)
    90  				return Add(vs.x.([]float64), vs.y)
    91  			},
    92  		},
    93  	}
    94  
    95  	for _, tt := range tests {
    96  		t.Run(tt.Name, func(t *testing.T) {
    97  			if got := tt.TestFunc(tt.Args); !labs.DeepEqual(got, tt.Want) {
    98  				t.Errorf("Add() = %v, want %v", got, tt.Want)
    99  			}
   100  		})
   101  	}
   102  }
   103  
   104  func BenchmarkAdd_init(b *testing.B) {
   105  	testalignOnce.Do(initTestData)
   106  }
   107  
   108  func BenchmarkAdd_release(b *testing.B) {
   109  	testalignOnce.Do(initTestData)
   110  	x := slices.Clone(testDataFloat64)
   111  	y := slices.Clone(testDataFloat64y)
   112  	for n := 0; n < b.N; n++ {
   113  		_ = Add(x, y)
   114  	}
   115  }
   116  
   117  func BenchmarkAdd_v1(b *testing.B) {
   118  	testalignOnce.Do(initTestData)
   119  	x := slices.Clone(testDataFloat64)
   120  	y := slices.Clone(testDataFloat64y)
   121  	for n := 0; n < b.N; n++ {
   122  		_ = v1Add(x, y)
   123  	}
   124  }