/
example_test.go
72 lines (59 loc) · 1.11 KB
/
example_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
package pter
import (
"encoding/json"
"math/rand"
"reflect"
"testing"
fuzz "github.com/google/gofuzz"
)
func TestAddSpec(t *testing.T) {
add := func(a, b int) int {
return a + b
}
assertion := func(a, b int) bool {
return add(a, b) == a+b
}
QuickCheck(t, assertion, &Config{
MaxCount: 10000,
})
}
type AType struct {
Name string
Typ int
I64Ptr *int64
Map map[string]string
Slice []string
}
func TestJsonSpecWithCustomFuzzer(t *testing.T) {
assertion := func(a *AType) bool {
bs, err := json.Marshal(a)
if err != nil {
return false
}
got := &AType{}
err = json.Unmarshal(bs, got)
if err != nil {
return false
}
return reflect.DeepEqual(a, got)
}
f := fuzz.New().NilChance(0)
QuickCheck(t, assertion, &Config{
MaxCount: 2000,
Fuzzer: f,
})
}
func TestMultiplySpecWithCustomArgsFn(t *testing.T) {
multiply := func(a, b int) int {
return a * b
}
assertion := func(a, b int) bool {
return multiply(a, b) == a*b
}
QuickCheck(t, assertion, &Config{
MaxCount: 10000,
Args: func() []interface{} {
return []interface{}{rand.Int(), rand.Int()}
},
})
}