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