-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzz_test.go
78 lines (67 loc) · 1.36 KB
/
fuzz_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
package dscope
import (
"fmt"
"math/rand"
"reflect"
"testing"
)
func FuzzFork(f *testing.F) {
type Type struct {
Type reflect.Type
Value func(r int) any
}
allTypes := []Type{
{
Type: reflect.TypeFor[int](),
Value: func(r int) any { return r },
},
{
Type: reflect.TypeFor[int64](),
Value: func(r int) any { return int64(r) },
},
{
Type: reflect.TypeFor[uint64](),
Value: func(r int) any { return uint64(r) },
},
{
Type: reflect.TypeFor[string](),
Value: func(r int) any { return fmt.Sprintf("%d", r) },
},
}
scope := New()
f.Fuzz(func(t *testing.T, r int) {
rand.Shuffle(len(allTypes), func(i, j int) {
allTypes[i], allTypes[j] = allTypes[j], allTypes[i]
})
types := allTypes[:1+rand.Intn(len(allTypes)-1)]
fn := reflect.MakeFunc(
reflect.FuncOf(
[]reflect.Type{},
func() (ret []reflect.Type) {
for _, t := range types {
ret = append(ret, t.Type)
}
return
}(),
false,
),
func(_ []reflect.Value) (ret []reflect.Value) {
for _, t := range types {
ret = append(ret, reflect.ValueOf(t.Value(r)))
}
return
},
).Interface()
scope = scope.Fork(fn)
for _, typ := range types {
ptr := reflect.New(typ.Type)
scope.Assign(ptr.Interface())
if !reflect.DeepEqual(
ptr.Elem().Interface(),
typ.Value(r),
) {
t.Fatal()
}
}
})
}