-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcomplexs_test.go
262 lines (257 loc) · 8.62 KB
/
complexs_test.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package typ
import (
"database/sql/driver"
"fmt"
"math"
"math/cmplx"
"reflect"
"testing"
)
var (
complexReflectTypes = []reflect.Type{
getDefaultType(reflect.Complex64),
getDefaultType(reflect.Complex128),
}
)
func init() {
// Test Data
matrixSuite.Register(getDefaultType(reflect.Complex128), []dataItem{
{reflect.ValueOf(complex128(0)), nil},
{reflect.ValueOf(complex(float64(MinFloat64), float64(MinFloat64))), nil},
{reflect.ValueOf(complex(float64(MaxFloat64), float64(MaxFloat64))), nil},
{reflect.ValueOf(cmplx.Inf()), nil},
{reflect.ValueOf(cmplx.NaN()), nil},
})
matrixSuite.Register(getDefaultType(reflect.Complex64), []dataItem{
{reflect.ValueOf(complex64(0)), nil},
{reflect.ValueOf(complex(float32(MinFloat32), float32(MinFloat32))), nil},
{reflect.ValueOf(complex(float32(MaxFloat32), float32(MaxFloat32))), nil},
{reflect.ValueOf(complex64(cmplx.Inf())), nil},
{reflect.ValueOf(complex64(cmplx.NaN())), nil},
})
// Converters
// - to bool
complexBoolConverter := func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
rv := reflect.ValueOf(from)
c, b := rv.Complex(), false
bp := matrixSuite.GetOptByType(opts, reflect.TypeOf(BoolPositive{}))
bh := matrixSuite.GetOptByType(opts, reflect.TypeOf(BoolHumanize{}))
if bp != nil || bh != nil {
if real(c) > 0 {
b = true
}
} else {
if c != 0 {
b = true
}
}
return b, true
}
matrixSuite.SetConverters(complexReflectTypes, boolReflectTypes, complexBoolConverter)
// - to float
complexFloatConverter := func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
rv := reflect.ValueOf(from)
c, f := rv.Complex(), float64(0)
var s bool
if s = isSafeComplexToFloat(c, bitSizeMap[to.Kind()]); s {
f = float64(real(c))
}
switch to.Kind() {
case reflect.Float32:
return float32(f), s
case reflect.Float64:
return float64(f), s
}
return nil, false
}
matrixSuite.SetConverters(complexReflectTypes, floatReflectTypes, complexFloatConverter)
// - to int
complexIntConverter := func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
rv := reflect.ValueOf(from)
c, i := rv.Complex(), int64(0)
var s bool
if s = isSafeComplexToInt(c, bitSizeMap[rv.Kind()], bitSizeMap[to.Kind()]); s {
i = int64(real(c))
}
switch to.Kind() {
case reflect.Int:
return int(i), s
case reflect.Int64:
return int64(i), s
case reflect.Int32:
return int32(i), s
case reflect.Int16:
return int16(i), s
case reflect.Int8:
return int8(i), s
}
return nil, false
}
matrixSuite.SetConverters(complexReflectTypes, intReflectTypes, complexIntConverter)
// - to uint
complexUintConverter := func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
rv := reflect.ValueOf(from)
c, i := rv.Complex(), uint64(0)
var s bool
if s = isSafeComplexToUint(c, bitSizeMap[rv.Kind()], bitSizeMap[to.Kind()]); s {
i = uint64(real(c))
}
switch to.Kind() {
case reflect.Uint:
return uint(i), s
case reflect.Uint64:
return uint64(i), s
case reflect.Uint32:
return uint32(i), s
case reflect.Uint16:
return uint16(i), s
case reflect.Uint8:
return uint8(i), s
}
return nil, false
}
matrixSuite.SetConverters(complexReflectTypes, uintReflectTypes, complexUintConverter)
// - to string
complexStringConverter := func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
return fmt.Sprintf("%v", from), true
}
matrixSuite.SetConverters(complexReflectTypes, stringReflectTypes, complexStringConverter)
// - to &NullComplex*{}, &NotNullComplex*{}
matrixSuite.SetConverters(complexReflectTypes, complexNullReflectTypes, func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
rv := reflect.ValueOf(from)
switch {
case rv.Kind() == reflect.Complex64 && to == reflect.TypeOf(&NullComplex64{}):
v := complex64(rv.Complex())
return &NullComplex64{Complex64Common{P: &v}}, true
case rv.Kind() == reflect.Complex128 && to == reflect.TypeOf(&NullComplex{}):
v := rv.Complex()
return &NullComplex{ComplexCommon{P: &v}}, true
case rv.Kind() == reflect.Complex64 && to == reflect.TypeOf(&NotNullComplex64{}):
v := complex64(rv.Complex())
return &NotNullComplex64{Complex64Common{P: &v}}, true
case rv.Kind() == reflect.Complex128 && to == reflect.TypeOf(&NotNullComplex{}):
v := rv.Complex()
return &NotNullComplex{ComplexCommon{P: &v}}, true
}
return nil, false
})
// - to SQLValueType
matrixSuite.SetConverters(complexReflectTypes, sqlValueReflectTypes, func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
rv := reflect.ValueOf(from)
switch {
case rv.Kind() == reflect.Complex64:
nv := Complex64Float64(complex64(rv.Complex()))
return SQLValueType{driver.Value(nv.V()), from}, nv.Valid()
case rv.Kind() == reflect.Complex128:
nv := ComplexFloat64(rv.Complex())
return SQLValueType{driver.Value(nv.V()), from}, nv.Valid()
}
return nil, false
})
// For other types
matrixSuite.SetConverters(interfaceReflectTypes, complexReflectTypes, func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
return nil, false
})
matrixSuite.SetComparators(func(a interface{}, b interface{}) bool {
arv := reflect.ValueOf(a)
arb := reflect.ValueOf(b)
if arv.Kind() == arb.Kind() {
cflags := func(c complex128) uint8 {
var flags uint8
if math.IsNaN(real(c)) {
flags |= 1
}
if math.IsNaN(imag(c)) {
flags |= 2
}
if math.IsInf(real(c), -1) {
flags |= 4
}
if math.IsInf(imag(c), -1) {
flags |= 8
}
if math.IsInf(real(c), 1) {
flags |= 16
}
if math.IsInf(imag(c), 1) {
flags |= 32
}
return flags
}
return cflags(arv.Complex()) == cflags(arb.Complex())
}
return false
}, complexReflectTypes)
}
func TestComplex(t *testing.T) {
testData := matrixSuite.Generate()
dCmplx64 := complex(float32(magicNumber), float32(0))
dCmplx128 := complex(float64(magicNumber), float64(0))
for _, di := range testData {
testOfDefault(t, di.value.Interface(), "Complex64", dCmplx64)
testOfDefault(t, di.value.Interface(), "Complex", dCmplx128)
testOfPassedErr(t, NewType(di.value.Interface(), errPassed), "Complex64", errPassed)
testOfPassedErr(t, NewType(di.value.Interface(), errPassed), "Complex", errPassed)
testOfDefaultErr(t, di.value.Interface(), "Complex64", dCmplx64, ErrDefaultValue)
testOfDefaultErr(t, di.value.Interface(), "Complex", dCmplx128, ErrDefaultValue)
switch di.value.Kind() {
case reflect.Int:
testNative(t, IntComplex64, []interface{}{di.value.Int(), dCmplx64})
testNative(t, IntComplex, []interface{}{di.value.Int(), dCmplx128})
case reflect.Uint:
testNative(t, UintComplex64, []interface{}{di.value.Uint(), dCmplx64})
testNative(t, UintComplex, []interface{}{di.value.Uint(), dCmplx128})
case reflect.Float32:
testNative(t, Float32Complex64, []interface{}{float32(di.value.Float()), dCmplx64})
case reflect.Float64:
testNative(t, FloatComplex64, []interface{}{di.value.Float(), dCmplx64})
case reflect.String:
testNative(t, StringComplex64, []interface{}{di.value.String(), dCmplx64})
testNative(t, StringComplex, []interface{}{di.value.String(), dCmplx128})
case reflect.Complex128:
testNative(t, Complex64, []interface{}{di.value.Complex(), complex64(dCmplx64)})
}
}
testOfDefaultNil(t, "Complex64")
testOfDefaultNil(t, "Complex")
}
// BenchmarkOfComplex/StringComplex64-8 1000000 1318 ns/op
// BenchmarkOfComplex/Complex64-8 5000000 253 ns/op
// BenchmarkOfComplex/NativeFloat32Complex64-8 20000000 105 ns/op
// BenchmarkOfComplex/StringComplex-8 1000000 1666 ns/op
// BenchmarkOfComplex/Complex-8 10000000 158 ns/op
// BenchmarkOfComplex/NativeFloatComplex-8 10000000 130 ns/op
func BenchmarkOfComplex(b *testing.B) {
c64 := "(3.4028235e+38+3.4028235e+38i)"
c128 := "(1.7976931348623157e+308+1.7976931348623157e+308i)"
b.Run("StringComplex64", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Of(c64).Complex64()
}
})
b.Run("Complex64", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Of(MaxFloat32).Complex64()
}
})
b.Run("NativeFloat32Complex64", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Float32Complex64(MaxFloat32)
}
})
b.Run("StringComplex", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Of(c128).Complex()
}
})
b.Run("Complex", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Of(MaxFloat64).Complex()
}
})
b.Run("NativeFloatComplex", func(b *testing.B) {
for i := 0; i < b.N; i++ {
FloatComplex64(MaxFloat64)
}
})
}