Skip to content

Commit

Permalink
Initial fuzz tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peterhellberg committed Sep 6, 2023
1 parent 930b95b commit 5c78fd1
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package sqids

import (
"fmt"
"testing"
)

func FuzzDecode(f *testing.F) {
s, err := New()
if err != nil {
f.Fatalf("unexpected error: %v", err)
}

f.Fuzz(func(t *testing.T, id string) {
s.Decode(id)
})
}

func FuzzEncode(f *testing.F) {
s, err := New()
if err != nil {
f.Fatalf("unexpected error: %v", err)
}

f.Fuzz(func(t *testing.T, u uint64) {
s.Encode([]uint64{u})
})
}

func FuzzNewEncodeDecode(f *testing.F) {
f.Add(defaultAlphabet, uint64(1))

f.Fuzz(func(t *testing.T, alphabet string, u uint64) {
s, err := New(Options{
Alphabet: alphabet,
})
if err == nil {
id, err := s.Encode([]uint64{u})
if err == nil {
d := s.Decode(id)

if d[0] != u {
panic(fmt.Sprintf("%d != %d", d[0], u))
}
}
}
})
}

0 comments on commit 5c78fd1

Please sign in to comment.