Skip to content

Commit

Permalink
Property-based tests for majors & strings
Browse files Browse the repository at this point in the history
As a bonus, this gets rid of `math/rand` and the associated `golangci-lint` complaint
  • Loading branch information
CryptoCopter committed Apr 5, 2024
1 parent 10e1b5c commit 7e28b01
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
module github.com/dtn7/cboring

go 1.22

require pgregory.net/rapid v1.1.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw=
pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
31 changes: 20 additions & 11 deletions major_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cboring
import (
"bufio"
"bytes"
"math/rand"
"reflect"
"testing"

"pgregory.net/rapid"
)

func TestReadMajorsSmall(t *testing.T) {
Expand Down Expand Up @@ -166,14 +167,17 @@ func TestReadExampleArray(t *testing.T) {
}
}

func TestReadBigData(t *testing.T) {
var size = 1024
var payload = make([]byte, size)
rand.Seed(0)
rand.Read(payload)
const (
dataMinSize = 0
dataMaxSize = 1024
dataMinElems = 0
dataMaxElems = 50000
)

var elems = []int{100, 500, 1000, 5000, 10000, 50000}
for _, elem := range elems {
func TestReadBigData(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
payload := rapid.SliceOfN(rapid.Byte(), dataMinSize, dataMaxSize).Draw(t, "Payload")
elem := rapid.IntRange(dataMinElems, dataMaxElems).Draw(t, "Elements")
buff := new(bytes.Buffer)
bw := bufio.NewWriter(buff)

Expand All @@ -192,9 +196,14 @@ func TestReadBigData(t *testing.T) {
tmp, err := ReadByteString(br)
if err != nil {
t.Fatalf("Reading no %d errored: %v", i, err)
} else if len(tmp) != size {
t.Fatalf("Length no %d mismatches: %d != %d", i, len(tmp), size)
} else if len(tmp) != len(payload) {
t.Fatalf("Length no %d mismatches: %d != %d", i, len(tmp), len(payload))
}
for j := 0; j < len(tmp); j++ {
if tmp[j] != payload[j] {
t.Fatalf("Wrong value at %d: %d != %d", i, j, len(tmp))
}
}
}
}
})
}
26 changes: 11 additions & 15 deletions strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cboring
import (
"bytes"
"fmt"
"math/rand"
"reflect"
"testing"

"pgregory.net/rapid"
)

func TestByteString(t *testing.T) {
Expand Down Expand Up @@ -44,20 +45,15 @@ func TestByteString(t *testing.T) {
}
}

func BenchmarkByteString(b *testing.B) {
sizes := []int{
// Ridiculously small
0, 1, 128, 256,
// Kibibytes
1024, 10240, 102400,
// Mebibytes
1048576, 10485760, 104857600,
}
const (
stringsMinSize = 0
stringMaxSize = 104857600
)

for _, size := range sizes {
rndData := make([]byte, size)
rand.Seed(0)
rand.Read(rndData)
func BenchmarkByteString(b *testing.B) {
rapid.Check(b, func(t *rapid.T) {
rndData := rapid.SliceOfN(rapid.Byte(), stringsMinSize, stringMaxSize).Draw(t, "rndData")
size := len(rndData)

b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -87,7 +83,7 @@ func BenchmarkByteString(b *testing.B) {
}
}
})
}
})
}

func TestReadTextString(t *testing.T) {
Expand Down

0 comments on commit 7e28b01

Please sign in to comment.