-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
fixedlength_test.go
128 lines (111 loc) · 2.75 KB
/
fixedlength_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package fixedlength2
import (
"bytes"
"io"
"io/ioutil"
"testing"
"github.com/bradleyjkemp/cupaloy"
"github.com/jf-tech/go-corelib/jsons"
"github.com/jf-tech/omniparser"
"github.com/jf-tech/omniparser/extensions/omniv21/samples"
"github.com/jf-tech/omniparser/transformctx"
)
type testCase struct {
schemaFile string
inputFile string
schema omniparser.Schema
input []byte
}
const (
test1_Single_Row = iota
test2_Multi_Rows
test3_Header_Footer
test4_Nested
)
var tests = []testCase{
{
// test1_Single_Row
schemaFile: "./1_single_row.schema.json",
inputFile: "./1_single_row.input.txt",
},
{
// test2_Multi_Rows
schemaFile: "./2_multi_rows.schema.json",
inputFile: "./2_multi_rows.input.txt",
},
{
// test3_Header_Footer
schemaFile: "./3_header_footer.schema.json",
inputFile: "./3_header_footer.input.txt",
},
{
// test4_Nested
schemaFile: "./4_nested.schema.json",
inputFile: "./4_nested.input.txt",
},
}
func init() {
for i := range tests {
schema, err := ioutil.ReadFile(tests[i].schemaFile)
if err != nil {
panic(err)
}
tests[i].schema, err = omniparser.NewSchema("bench", bytes.NewReader(schema))
if err != nil {
panic(err)
}
tests[i].input, err = ioutil.ReadFile(tests[i].inputFile)
if err != nil {
panic(err)
}
}
}
func (tst testCase) doTest(t *testing.T) {
cupaloy.SnapshotT(t, jsons.BPJ(samples.SampleTestCommon(t, tst.schemaFile, tst.inputFile)))
}
func (tst testCase) doBenchmark(b *testing.B) {
for i := 0; i < b.N; i++ {
transform, err := tst.schema.NewTransform(
"bench", bytes.NewReader(tst.input), &transformctx.Ctx{})
if err != nil {
b.FailNow()
}
for {
_, err = transform.Read()
if err == io.EOF {
break
}
if err != nil {
b.FailNow()
}
}
}
}
func Test1_Single_Row(t *testing.T) {
tests[test1_Single_Row].doTest(t)
}
func Test2_Multi_Rows(t *testing.T) {
tests[test2_Multi_Rows].doTest(t)
}
func Test3_Header_Footer(t *testing.T) {
tests[test3_Header_Footer].doTest(t)
}
func Test4_Nested(t *testing.T) {
tests[test4_Nested].doTest(t)
}
// Benchmark1_Single_Row-8 25951 45776 ns/op 28213 B/op 645 allocs/op
func Benchmark1_Single_Row(b *testing.B) {
tests[test1_Single_Row].doBenchmark(b)
}
// Benchmark2_Multi_Rows-8 18583 64240 ns/op 34056 B/op 636 allocs/op
func Benchmark2_Multi_Rows(b *testing.B) {
tests[test2_Multi_Rows].doBenchmark(b)
}
// Benchmark3_Header_Footer-8 7306 158797 ns/op 78666 B/op 1587 allocs/op
func Benchmark3_Header_Footer(b *testing.B) {
tests[test3_Header_Footer].doBenchmark(b)
}
// Benchmark4_Nested-8 10000 107955 ns/op 80929 B/op 1515 allocs/op
func Benchmark4_Nested(b *testing.B) {
tests[test4_Nested].doBenchmark(b)
}