Skip to content

Commit

Permalink
add test for zero allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
awalterschulze committed Jan 29, 2025
1 parent f16fb74 commit 38cac8c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions json/alloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,39 @@
package json_test

import (
"fmt"
"math/rand"
"testing"
"time"

jsonparser "github.com/katydid/parser-go-json/json"
)

func TestNoAllocs(t *testing.T) {
num := 1000
r := rand.New(rand.NewSource(time.Now().UnixNano()))
js := randJsons(r, num)
jparser := jsonparser.NewJsonParser()

const runsPerTest = 100
checkNoAllocs := func(f func()) func(t *testing.T) {
return func(t *testing.T) {
t.Helper()
if allocs := testing.AllocsPerRun(runsPerTest, f); allocs != 0 {
t.Errorf("got %v allocs, want 0 allocs", allocs)
}
}
}
for i := 0; i < num; i++ {
t.Run(fmt.Sprintf("%d", i), checkNoAllocs(func() {
if err := jparser.Init(js[i]); err != nil {
t.Fatal(err)
}
walk(jparser)
}))
}
}

func BenchmarkAlloc(b *testing.B) {
num := 1000
r := rand.New(rand.NewSource(time.Now().UnixNano()))
Expand Down

0 comments on commit 38cac8c

Please sign in to comment.