Skip to content

Commit 7e28b01

Browse files
committed
Property-based tests for majors & strings
As a bonus, this gets rid of `math/rand` and the associated `golangci-lint` complaint
1 parent 10e1b5c commit 7e28b01

File tree

4 files changed

+37
-26
lines changed

4 files changed

+37
-26
lines changed

go.mod

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
module github.com/dtn7/cboring
2+
3+
go 1.22
4+
5+
require pgregory.net/rapid v1.1.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw=
2+
pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=

major_test.go

+20-11
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package cboring
33
import (
44
"bufio"
55
"bytes"
6-
"math/rand"
76
"reflect"
87
"testing"
8+
9+
"pgregory.net/rapid"
910
)
1011

1112
func TestReadMajorsSmall(t *testing.T) {
@@ -166,14 +167,17 @@ func TestReadExampleArray(t *testing.T) {
166167
}
167168
}
168169

169-
func TestReadBigData(t *testing.T) {
170-
var size = 1024
171-
var payload = make([]byte, size)
172-
rand.Seed(0)
173-
rand.Read(payload)
170+
const (
171+
dataMinSize = 0
172+
dataMaxSize = 1024
173+
dataMinElems = 0
174+
dataMaxElems = 50000
175+
)
174176

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

@@ -192,9 +196,14 @@ func TestReadBigData(t *testing.T) {
192196
tmp, err := ReadByteString(br)
193197
if err != nil {
194198
t.Fatalf("Reading no %d errored: %v", i, err)
195-
} else if len(tmp) != size {
196-
t.Fatalf("Length no %d mismatches: %d != %d", i, len(tmp), size)
199+
} else if len(tmp) != len(payload) {
200+
t.Fatalf("Length no %d mismatches: %d != %d", i, len(tmp), len(payload))
201+
}
202+
for j := 0; j < len(tmp); j++ {
203+
if tmp[j] != payload[j] {
204+
t.Fatalf("Wrong value at %d: %d != %d", i, j, len(tmp))
205+
}
197206
}
198207
}
199-
}
208+
})
200209
}

strings_test.go

+11-15
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package cboring
33
import (
44
"bytes"
55
"fmt"
6-
"math/rand"
76
"reflect"
87
"testing"
8+
9+
"pgregory.net/rapid"
910
)
1011

1112
func TestByteString(t *testing.T) {
@@ -44,20 +45,15 @@ func TestByteString(t *testing.T) {
4445
}
4546
}
4647

47-
func BenchmarkByteString(b *testing.B) {
48-
sizes := []int{
49-
// Ridiculously small
50-
0, 1, 128, 256,
51-
// Kibibytes
52-
1024, 10240, 102400,
53-
// Mebibytes
54-
1048576, 10485760, 104857600,
55-
}
48+
const (
49+
stringsMinSize = 0
50+
stringMaxSize = 104857600
51+
)
5652

57-
for _, size := range sizes {
58-
rndData := make([]byte, size)
59-
rand.Seed(0)
60-
rand.Read(rndData)
53+
func BenchmarkByteString(b *testing.B) {
54+
rapid.Check(b, func(t *rapid.T) {
55+
rndData := rapid.SliceOfN(rapid.Byte(), stringsMinSize, stringMaxSize).Draw(t, "rndData")
56+
size := len(rndData)
6157

6258
b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
6359
for i := 0; i < b.N; i++ {
@@ -87,7 +83,7 @@ func BenchmarkByteString(b *testing.B) {
8783
}
8884
}
8985
})
90-
}
86+
})
9187
}
9288

9389
func TestReadTextString(t *testing.T) {

0 commit comments

Comments
 (0)