Skip to content

Commit

Permalink
test: add zero allocation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Nov 7, 2021
1 parent 8f56b14 commit 1d24770
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
72 changes: 72 additions & 0 deletions alloc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//go:build !race && !gofuzz
// +build !race,!gofuzz

package jx

import (
"testing"
)

const defaultAllocRuns = 10

func zeroAlloc(t *testing.T, f func()) {
t.Helper()
avg := testing.AllocsPerRun(defaultAllocRuns, f)
if avg > 0 {
t.Errorf("Allocated %f bytes per run", avg)
}
}

func zeroAllocDec(t *testing.T, buf []byte, f func(d *Decoder) error) {
t.Helper()
d := DecodeBytes(buf)
zeroAlloc(t, func() {
d.ResetBytes(buf)
if err := f(d); err != nil {
t.Fatal(err)
}
})
}

func zeroAllocDecStr(t *testing.T, s string, f func(d *Decoder) error) {
t.Helper()
zeroAllocDec(t, []byte(s), f)
}

func TestZeroAlloc(t *testing.T) {
// Tests that checks for zero allocations.
t.Run("Decoder", func(t *testing.T) {
t.Run("Validate", func(t *testing.T) {
zeroAllocDec(t, data, func(d *Decoder) error {
return d.Validate()
})
})
t.Run("Int", func(t *testing.T) {
zeroAllocDecStr(t, "12345", func(d *Decoder) error {
v, err := d.Int()
if v != 12345 {
t.Fatal(v)
}
return err
})
})
t.Run("StrBytes", func(t *testing.T) {
zeroAllocDecStr(t, `"hello"`, func(d *Decoder) error {
v, err := d.StrBytes()
if string(v) != "hello" {
t.Fatal(string(v))
}
return err
})
})
t.Run("Str", func(t *testing.T) {
zeroAllocDecStr(t, `"hello"`, func(d *Decoder) error {
v, err := d.Str()
if v != "hello" {
t.Fatal(err)
}
return err
})
})
})
}
5 changes: 4 additions & 1 deletion go.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

set -e

# test with -race
echo "test"
go test --timeout 5m ./...

echo "test -race"
go test --timeout 5m -race ./...

0 comments on commit 1d24770

Please sign in to comment.