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

     1  package num
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/num/labs"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  func TestAbs(t *testing.T) {
    11  	type testCase struct {
    12  		name string
    13  		args any
    14  		want any
    15  	}
    16  	tests := []testCase{
    17  		{
    18  			name: "bool",
    19  			args: []bool{false, true},
    20  			want: []bool{false, true},
    21  		},
    22  		{
    23  			name: "string",
    24  			args: []string{"1", "2"},
    25  			want: []string{"1", "2"},
    26  		},
    27  		{
    28  			name: "float32",
    29  			args: []float32{-0.1, 1.0, -2.00, -3},
    30  			want: []float32{0.1, 1.0, 2.00, 3.0},
    31  		},
    32  		{
    33  			name: "float64",
    34  			args: []float64{-0.1, 1.0, -2.00, -3},
    35  			want: []float64{0.1, 1.0, 2.00, 3.0},
    36  		},
    37  	}
    38  
    39  	for _, tt := range tests {
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			var got any
    42  			switch args := tt.args.(type) {
    43  			case []float32:
    44  				got = Abs(args)
    45  			case []float64:
    46  				got = Abs(args)
    47  			case []int:
    48  				got = Abs(args)
    49  			case []int8:
    50  				got = Abs(args)
    51  			case []int16:
    52  				got = Abs(args)
    53  			case []int32:
    54  				got = Abs(args)
    55  			case []int64:
    56  				got = Abs(args)
    57  			case []uint:
    58  				got = Abs(args)
    59  			case []uint8:
    60  				got = Abs(args)
    61  			case []uint16:
    62  				got = Abs(args)
    63  			case []uint32:
    64  				got = Abs(args)
    65  			case []uint64:
    66  				got = Abs(args)
    67  			case []uintptr:
    68  				got = Abs(args)
    69  			case []bool:
    70  				got = Abs(args)
    71  			case []string:
    72  				got = Abs(args)
    73  			default:
    74  				// 其它类型原样返回
    75  				panic(fmt.Errorf("不支持的类型: %T", args))
    76  			}
    77  
    78  			if !reflect.DeepEqual(got, tt.want) {
    79  				t.Errorf("Abs() = %v, want %v", got, tt.want)
    80  			}
    81  		})
    82  	}
    83  }
    84  
    85  func TestAbs1(t *testing.T) {
    86  	type testCase struct {
    87  		Name     string
    88  		Args     any
    89  		Want     any
    90  		TestFunc func(v any) any
    91  	}
    92  
    93  	tests := []testCase{
    94  		{
    95  			Name: "bool",
    96  			Args: []bool{false, true},
    97  			Want: []bool{false, true},
    98  			TestFunc: func(v any) any {
    99  				return Abs(v.([]bool))
   100  			},
   101  		},
   102  		{
   103  			Name: "string",
   104  			Args: []string{"1"},
   105  			Want: []string{"1"},
   106  			TestFunc: func(v any) any {
   107  				return Abs(v.([]string))
   108  			},
   109  		},
   110  		{
   111  			Name: "float32",
   112  			Args: []float32{-0.1, 1.0, -2.00, -3},
   113  			Want: []float32{0.1, 1.0, 2.00, 3.0},
   114  			TestFunc: func(v any) any {
   115  				return Abs(v.([]float32))
   116  			},
   117  		},
   118  		{
   119  			Name: "float64",
   120  			Args: []float64{-0.1, 1.0, -2.00, -3, NaN()},
   121  			Want: []float64{0.1, 1.0, 2.00, 3.0, NaN()},
   122  			TestFunc: func(v any) any {
   123  				return Abs(v.([]float64))
   124  			},
   125  		},
   126  	}
   127  
   128  	for _, tt := range tests {
   129  		t.Run(tt.Name, func(t *testing.T) {
   130  			if got := tt.TestFunc(tt.Args); !labs.DeepEqual(got, tt.Want) {
   131  				t.Errorf("Abs() = %v, want %v", got, tt.Want)
   132  			}
   133  		})
   134  	}
   135  }