-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
complex_test.go
107 lines (96 loc) · 2.49 KB
/
complex_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
package dd_test
import (
"encoding/json"
"fmt"
"go/parser"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"testing"
"github.com/Code-Hex/dd"
"github.com/google/go-cmp/cmp"
)
//go:generate go run cmd/wantdumper/main.go
var addressReplaceRegexp = regexp.MustCompile(`uintptr\((0x[\da-f]+)\)`)
type Entry struct {
name string
value interface{}
want []byte
}
func makeEntries() ([]Entry, error) {
entries, err := os.ReadDir("testdata")
if err != nil {
return nil, fmt.Errorf("failed to read directory: %w", err)
}
ret := make([]Entry, len(entries))
for i, entry := range entries {
jsonFile := filepath.Join("testdata", entry.Name(), "data.json")
content, err := ioutil.ReadFile(jsonFile)
if err != nil {
return nil, fmt.Errorf("failed to read file %q: %w", jsonFile, err)
}
wantFile := filepath.Join("testdata", entry.Name(), "want.txt")
want, err := ioutil.ReadFile(wantFile)
if err != nil {
return nil, fmt.Errorf("failed to read file %q: %w", wantFile, err)
}
var unmarshaled interface{}
if err := json.Unmarshal(content, &unmarshaled); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
ret[i] = Entry{
name: entry.Name(),
value: unmarshaled,
want: want,
}
}
return ret, nil
}
func TestComplex(t *testing.T) {
entries, err := makeEntries()
if err != nil {
t.Fatal(err)
}
for _, entry := range entries {
entry := entry
t.Run(entry.name, func(t *testing.T) {
got := dd.Dump(entry.value)
// check syntax is valid
if _, err := parser.ParseExpr(got); err != nil {
t.Log(got)
t.Fatal(err)
}
// replace addresses
replacedWant := addressReplaceRegexp.ReplaceAll(entry.want, []byte("0x0"))
replacedGot := addressReplaceRegexp.ReplaceAllString(got, "0x0")
if diff := cmp.Diff(string(replacedWant), replacedGot); diff != "" {
t.Fatalf("(-want, +got)\n%s", diff)
}
})
}
}
// 2022-03-20
// goos: darwin
// goarch: arm64
// pkg: github.com/Code-Hex/dd
// BenchmarkComplex/simple-8 45826 25505 ns/op 18303 B/op 405 allocs/op
// BenchmarkComplex/twitter-search-adaptive-8 25 47194118 ns/op 45719134 B/op 243727 allocs/op
// PASS
// ok github.com/Code-Hex/dd 3.076s
func BenchmarkComplex(b *testing.B) {
entries, err := makeEntries()
if err != nil {
b.Fatal(err)
}
for _, entry := range entries {
entry := entry
b.Run(entry.name, func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
dd.Dump(entry.value)
}
})
}
}