-
Notifications
You must be signed in to change notification settings - Fork 1
/
as_test.go
89 lines (74 loc) · 2.38 KB
/
as_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
package as_test
import (
"math"
"reflect"
"testing"
"github.com/lunemec/as"
)
// assertError tests if function called with given parameters raises error, if not
// it is considered failed test.
func assertError(t *testing.T, fn interface{}, args interface{}) {
t.Helper()
outType, err := callfn(fn, args)
if err == nil {
t.Errorf("There should be error when converting %+v (%T) to %v but err is %v", args, args, outType, err)
}
}
// assertNoError tests if function called with given parameters raises error, if not
// it is considered failed test.
func assertNoError(t *testing.T, fn interface{}, args interface{}) {
t.Helper()
outType, err := callfn(fn, args)
if err != nil {
t.Errorf("There should be NO error when converting %+v (%T) to %v: %s", args, args, outType, err)
}
}
func callfn(fn interface{}, args interface{}) (reflect.Type, interface{}) {
v := reflect.ValueOf(fn)
if t := v.Kind(); t != reflect.Func {
panic("assertError expect function as 2nd argument!")
}
outValue := v.Call([]reflect.Value{reflect.ValueOf(args)})
outType := outValue[0].Type()
err := outValue[1].Interface()
return outType, err
}
func TestT(t *testing.T) {
assertNoError(t, as.T[int], int64(math.MaxInt64))
assertNoError(t, as.T[int], int64(math.MinInt64))
assertError(t, as.T[int], uint64(math.MaxUint64))
// uintptr is not supported, will error
assertError(t, as.T[uintptr], uint64(math.MaxUint64))
}
func TestTCustomType(t *testing.T) {
type aliasedInt int
assertNoError(t, as.T[int], aliasedInt(math.MaxInt64))
assertNoError(t, as.T[int], aliasedInt(math.MinInt64))
}
func TestTIntoCustomType(t *testing.T) {
type aliasedInt int
type aliasedIntOut int
assertNoError(t, as.T[aliasedIntOut], aliasedInt(math.MaxInt64))
assertNoError(t, as.T[aliasedIntOut], aliasedInt(math.MinInt64))
}
var out int
// BenchmarkT-8 91144112 12.90 ns/op 8 B/op 0 allocs/op
// After changes to indirect:
// BenchmarkT-8 24196993 49.55 ns/op 24 B/op 2 allocs/op
func BenchmarkT(b *testing.B) {
var t int
for n := 0; n < b.N; n++ {
t, _ = as.T[int](n)
}
out = t
}
// BenchmarkInt-8 108703077 10.98 ns/op 8 B/op 0 allocs/op
// After changes to indirect:
// BenchmarkInt-8 42888086 28.00 ns/op 16 B/op 1 allocs/op
func BenchmarkInt(b *testing.B) {
var t int
for n := 0; n < b.N; n++ {
t, _ = as.Int(n)
}
out = t
}