-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiroot_test.go
More file actions
113 lines (101 loc) · 2.28 KB
/
multiroot_test.go
File metadata and controls
113 lines (101 loc) · 2.28 KB
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
package jsonvector
import (
"bytes"
"math/rand"
"os"
"path/filepath"
"strconv"
"testing"
"github.com/koykov/vector"
)
type multirootStage struct {
key string
buf []byte
}
var (
multiroots [][]byte
multirootsFmt [][]byte
multirootStages []multirootStage
multirootStagesReg = map[string]int{}
)
func init() {
_ = filepath.Walk("testdata/multiroot", func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".json" {
b, _ := os.ReadFile(path)
multiroots = append(multiroots, b)
vec := NewVector()
if err = vec.ParseCopy(b); err != nil {
os.Exit(1)
}
var buf bytes.Buffer
_ = vec.Beautify(&buf)
multirootsFmt = append(multirootsFmt, buf.Bytes())
return nil
}
return nil
})
for i := 10; i <= 100; i += 10 {
key := strconv.Itoa(i)
st := multirootStage{key: key}
for j := 0; j < i; j++ {
st.buf = append(st.buf, multiroots[j%len(multiroots)]...)
if j > 0 {
switch rand.Intn(5) {
case 0:
st.buf = append(st.buf, '\n')
case 1:
st.buf = append(st.buf, '\r')
case 2:
st.buf = append(st.buf, '\t')
case 3:
st.buf = append(st.buf, ' ')
case 4:
st.buf = append(st.buf, "\n\r\n\n\r\r\t\t "...)
}
}
}
multirootStages = append(multirootStages, st)
multirootStagesReg[key] = len(multirootStages) - 1
}
}
func TestMultiroot(t *testing.T) {
for i := 10; i <= 100; i += 10 {
key := strconv.Itoa(i)
t.Run(key, func(t *testing.T) {
idx := multirootStagesReg[key]
st := &multirootStages[idx]
vec := Acquire()
defer Release(vec)
if err := vec.ParseCopy(st.buf); err != nil {
t.Error(err)
}
vec.Each(func(idx int, root *vector.Node) {
var buf bytes.Buffer
_ = root.Beautify(&buf)
origin := multirootsFmt[idx%len(multirootsFmt)]
fmtv := buf.Bytes()
if !bytes.Equal(origin, fmtv) {
t.Errorf("key %s root %d", key, idx)
}
})
})
}
}
func BenchmarkMultiroot(b *testing.B) {
for i := 10; i <= 100; i += 10 {
key := strconv.Itoa(i)
b.Run(key, func(b *testing.B) {
idx := multirootStagesReg[key]
st := &multirootStages[idx]
vec := Acquire()
defer Release(vec)
b.ReportAllocs()
for j := 0; j < b.N; j++ {
vec.Reset()
if err := vec.Parse(st.buf); err != nil {
b.Error(err)
}
}
})
}
}