Skip to content

Commit f89c10a

Browse files
fix zero allocation tests
1 parent 78d11f6 commit f89c10a

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

json/json.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ type jsonParser struct {
4141

4242
// NewParser returns a new JSON parser.
4343
func NewParser() Interface {
44+
p := pool.New()
4445
return &jsonParser{
4546
stack: make([]state, 0, 10),
46-
parser: parse.NewParser(nil),
47-
pool: pool.New(),
47+
parser: parse.NewParserWithCustomAllocator(nil, p.Alloc),
48+
pool: p,
4849
}
4950
}
5051

@@ -58,7 +59,6 @@ func (p *jsonParser) Init(buf []byte) error {
5859
}
5960

6061
func (p *jsonParser) nextAtStartState(action action) error {
61-
fmt.Printf("nextAtStartState\n")
6262
switch action {
6363
case nextAction:
6464
parseKind, err := p.parser.Next()
@@ -101,7 +101,6 @@ func (p *jsonParser) nextAtStartState(action action) error {
101101
}
102102

103103
func (p *jsonParser) nextInLeafState(action action) error {
104-
fmt.Printf("nextInLeafState\n")
105104
switch action {
106105
case nextAction:
107106
// We already parsed the leaf, so there is no next element.
@@ -122,7 +121,6 @@ func (p *jsonParser) nextInLeafState(action action) error {
122121
}
123122

124123
func (p *jsonParser) nextAtEOF(action action) error {
125-
fmt.Printf("nextAtEOF\n")
126124
switch action {
127125
case nextAction:
128126
// If Next is called too many times, just keep on return EOF
@@ -143,7 +141,6 @@ func (p *jsonParser) nextAtEOF(action action) error {
143141
}
144142

145143
func (p *jsonParser) nextInObjectAtKeyState(action action) error {
146-
fmt.Printf("nextInObjectAtKeyState\n")
147144
// inObjectAtKeyStateKind represents that we have scanned a key
148145
switch action {
149146
case nextAction:
@@ -191,7 +188,6 @@ func (p *jsonParser) nextInObjectAtKeyState(action action) error {
191188
}
192189

193190
func (p *jsonParser) nextInObjectAtValueState(action action) error {
194-
fmt.Printf("nextInObjectAtValueState\n")
195191
// inObjectAtValueStateKind represents that we have scanned a value and Up was called.
196192
switch action {
197193
case nextAction:
@@ -224,7 +220,6 @@ func (p *jsonParser) nextInObjectAtValueState(action action) error {
224220
}
225221

226222
func (p *jsonParser) nextInArrayIndexState(action action) error {
227-
fmt.Printf("nextInArrayIndexState\n")
228223
// inArrayIndexState represents that we have scanned an element, if it was null, bool, number or string and the first key of an object or .
229224
switch action {
230225
case nextAction:
@@ -305,7 +300,6 @@ func (p *jsonParser) nextInArrayIndexState(action action) error {
305300
}
306301

307302
func (p *jsonParser) nextInArrayAfterIndexState(action action) error {
308-
fmt.Printf("nextInArrayAfterIndexState\n")
309303
// This is after Up was called on an element.
310304
switch action {
311305
case nextAction:
@@ -343,7 +337,6 @@ func (p *jsonParser) eof() error {
343337
}
344338

345339
func (p *jsonParser) next() error {
346-
fmt.Printf("next\n")
347340
action := p.action
348341
// do not forget to reset action
349342
p.action = nextAction
@@ -367,22 +360,18 @@ func (p *jsonParser) next() error {
367360
}
368361

369362
func (p *jsonParser) Next() error {
370-
fmt.Printf("Next\n")
371363
return p.next()
372364
}
373365

374366
func (p *jsonParser) Down() {
375-
fmt.Printf("Down\n")
376367
p.action = downAction
377368
}
378369

379370
func (p *jsonParser) Up() {
380-
fmt.Printf("Up\n")
381371
p.action = upAction
382372
}
383373

384374
func (p *jsonParser) push() error {
385-
fmt.Printf("push\n")
386375
// Append the current state to the stack.
387376
p.stack = append(p.stack, p.state)
388377
p.state.kind = atStartStateKind
@@ -391,7 +380,6 @@ func (p *jsonParser) push() error {
391380
}
392381

393382
func (p *jsonParser) pop() error {
394-
fmt.Printf("pop\n")
395383
if len(p.stack) == 0 {
396384
return errPop
397385
}

json/json_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package json
1717

1818
import (
19-
"fmt"
2019
"testing"
2120
)
2221

@@ -152,7 +151,6 @@ func TestParseArrayElements(t *testing.T) {
152151
t.Fatal(err)
153152
}
154153

155-
fmt.Printf("first element\n")
156154
assertNoErr(t, p.Next)
157155
expect(t, p.Int, 0)
158156
p.Down()
@@ -161,7 +159,6 @@ func TestParseArrayElements(t *testing.T) {
161159
expectEOF(t, p.Next)
162160
p.Up()
163161

164-
fmt.Printf("second element\n")
165162
assertNoErr(t, p.Next)
166163
expect(t, p.Int, 1)
167164
p.Down()
@@ -170,7 +167,6 @@ func TestParseArrayElements(t *testing.T) {
170167
expectEOF(t, p.Next)
171168
p.Up()
172169

173-
fmt.Printf("third element\n")
174170
assertNoErr(t, p.Next)
175171
expect(t, p.Int, 2)
176172
p.Down()
@@ -184,7 +180,6 @@ func TestParseArrayElements(t *testing.T) {
184180
expectEOF(t, p.Next)
185181
p.Up()
186182

187-
fmt.Printf("fourth element\n")
188183
assertNoErr(t, p.Next)
189184
expect(t, p.Int, 3)
190185
p.Down()

json/rand_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/katydid/parser-go/parser/debug"
2323
)
2424

25-
func TestParseRandom(t *testing.T) {
25+
func TestParseRandomValues(t *testing.T) {
2626
r := rand.NewRand()
2727
numValues := 10000
2828
values := rand.Values(r, numValues)
@@ -40,3 +40,22 @@ func TestParseRandom(t *testing.T) {
4040
})
4141
}
4242
}
43+
44+
func TestRandomlyParseRandomValues(t *testing.T) {
45+
r := rand.NewRand()
46+
numValues := 10000
47+
values := rand.Values(r, numValues)
48+
p := NewParser()
49+
50+
for i := 0; i < numValues; i++ {
51+
name := testrun.Name(values[i])
52+
t.Run(name, func(t *testing.T) {
53+
if err := p.Init(values[i]); err != nil {
54+
t.Fatalf("seed = %v, err = %v, input = %v", r.Seed(), err, string(values[i]))
55+
}
56+
if err := debug.RandomWalk(p, r, 2, 2); err != nil {
57+
t.Fatalf("seed = %v, err = %v", r.Seed(), err)
58+
}
59+
})
60+
}
61+
}

json/skip_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package json
1717

1818
import (
19-
"fmt"
2019
"testing"
2120
)
2221

@@ -61,7 +60,6 @@ func TestSkipArrayElements(t *testing.T) {
6160
t.Fatal(err)
6261
}
6362

64-
fmt.Printf("first element\n")
6563
assertNoErr(t, p.Next)
6664
expect(t, p.Int, 0)
6765
p.Down()
@@ -70,7 +68,6 @@ func TestSkipArrayElements(t *testing.T) {
7068
// expectEOF(t, p.Next)
7169
p.Up()
7270

73-
fmt.Printf("second element\n")
7471
assertNoErr(t, p.Next)
7572
expect(t, p.Int, 1)
7673
p.Down()
@@ -79,7 +76,6 @@ func TestSkipArrayElements(t *testing.T) {
7976
expectEOF(t, p.Next)
8077
p.Up()
8178

82-
fmt.Printf("third element\n")
8379
assertNoErr(t, p.Next)
8480
expect(t, p.Int, 2)
8581
p.Down()
@@ -93,7 +89,6 @@ func TestSkipArrayElements(t *testing.T) {
9389
// expectEOF(t, p.Next)
9490
p.Up()
9591

96-
fmt.Printf("fourth element\n")
9792
assertNoErr(t, p.Next)
9893
expect(t, p.Int, 3)
9994
p.Down()

0 commit comments

Comments
 (0)